blob: bbd75f1d00262a07696d9d038b2c88a908f1646e [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
Radek Krejcif8dc59a2020-11-25 13:47:44 +010015#define _POSIX_C_SOURCE 200809L /* strdup, strndup */
Michal Vasko03ff5a72019-09-11 13:49:33 +020016
17/* needed by libmath functions isfinite(), isinf(), isnan(), signbit(), ... */
18#define _ISOC99_SOURCE
Radek Krejcib1646a92018-11-02 16:08:26 +010019
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010021
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010023#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#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 Krejci47fab892020-11-05 17:02:41 +010036#include "out.h"
Radek Krejci7931b192020-06-25 17:05:03 +020037#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020038#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020039#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020040#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020041#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020042#include "tree.h"
43#include "tree_data_internal.h"
44#include "tree_schema_internal.h"
45#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020046
Michal Vasko004d3152020-06-11 19:59:22 +020047static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx);
Michal Vasko40308e72020-10-20 16:38:40 +020048static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype,
49 struct lyxp_set *set, uint32_t options);
Michal Vasko03ff5a72019-09-11 13:49:33 +020050
51/**
52 * @brief Print the type of an XPath \p set.
53 *
54 * @param[in] set Set to use.
55 * @return Set type string.
56 */
57static const char *
58print_set_type(struct lyxp_set *set)
59{
60 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020061 case LYXP_SET_NODE_SET:
62 return "node set";
63 case LYXP_SET_SCNODE_SET:
64 return "schema node set";
65 case LYXP_SET_BOOLEAN:
66 return "boolean";
67 case LYXP_SET_NUMBER:
68 return "number";
69 case LYXP_SET_STRING:
70 return "string";
71 }
72
73 return NULL;
74}
75
Michal Vasko24cddf82020-06-01 08:17:01 +020076const char *
77lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020078{
79 switch (tok) {
80 case LYXP_TOKEN_PAR1:
81 return "(";
82 case LYXP_TOKEN_PAR2:
83 return ")";
84 case LYXP_TOKEN_BRACK1:
85 return "[";
86 case LYXP_TOKEN_BRACK2:
87 return "]";
88 case LYXP_TOKEN_DOT:
89 return ".";
90 case LYXP_TOKEN_DDOT:
91 return "..";
92 case LYXP_TOKEN_AT:
93 return "@";
94 case LYXP_TOKEN_COMMA:
95 return ",";
96 case LYXP_TOKEN_NAMETEST:
97 return "NameTest";
98 case LYXP_TOKEN_NODETYPE:
99 return "NodeType";
100 case LYXP_TOKEN_FUNCNAME:
101 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200102 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200103 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200104 case LYXP_TOKEN_OPER_EQUAL:
105 return "Operator(Equal)";
106 case LYXP_TOKEN_OPER_NEQUAL:
107 return "Operator(Non-equal)";
108 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200109 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200110 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200111 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200112 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200113 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200114 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200115 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200116 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200117 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200118 case LYXP_TOKEN_LITERAL:
119 return "Literal";
120 case LYXP_TOKEN_NUMBER:
121 return "Number";
122 default:
123 LOGINT(NULL);
124 return "";
125 }
126}
127
128/**
129 * @brief Print the whole expression \p exp to debug output.
130 *
131 * @param[in] exp Expression to use.
132 */
133static void
Michal Vasko40308e72020-10-20 16:38:40 +0200134print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200135{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100136#define MSG_BUFFER_SIZE 128
137 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100138 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200139
Radek Krejci52b6d512020-10-12 12:33:17 +0200140 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200141 return;
142 }
143
144 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
145 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200146 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200147 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100148 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200149 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
150 for (j = 1; exp->repeat[i][j]; ++j) {
151 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
152 }
153 strcat(tmp, ")");
154 }
155 LOGDBG(LY_LDGXPATH, tmp);
156 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100157#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200158}
159
160#ifndef NDEBUG
161
162/**
163 * @brief Print XPath set content to debug output.
164 *
165 * @param[in] set Set to print.
166 */
167static void
168print_set_debug(struct lyxp_set *set)
169{
170 uint32_t i;
171 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200172 struct lyxp_set_node *item;
173 struct lyxp_set_scnode *sitem;
174
Radek Krejci52b6d512020-10-12 12:33:17 +0200175 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200176 return;
177 }
178
179 switch (set->type) {
180 case LYXP_SET_NODE_SET:
181 LOGDBG(LY_LDGXPATH, "set NODE SET:");
182 for (i = 0; i < set->used; ++i) {
183 item = &set->val.nodes[i];
184
185 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100186 case LYXP_NODE_NONE:
187 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
188 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200189 case LYXP_NODE_ROOT:
190 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
191 break;
192 case LYXP_NODE_ROOT_CONFIG:
193 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
194 break;
195 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100196 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200197 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200198 item->node->schema->name, LYD_CANON_VALUE(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100199 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200200 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200201 item->node->schema->name, LYD_CANON_VALUE(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200202 } else {
203 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
204 }
205 break;
206 case LYXP_NODE_TEXT:
207 if (item->node->schema->nodetype & LYS_ANYDATA) {
208 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200209 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200210 } else {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200211 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 +0200212 }
213 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100214 case LYXP_NODE_META:
215 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
Michal Vasko69730152020-10-09 16:30:07 +0200216 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200217 break;
218 }
219 }
220 break;
221
222 case LYXP_SET_SCNODE_SET:
223 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
224 for (i = 0; i < set->used; ++i) {
225 sitem = &set->val.scnodes[i];
226
227 switch (sitem->type) {
228 case LYXP_NODE_ROOT:
229 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
230 break;
231 case LYXP_NODE_ROOT_CONFIG:
232 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
233 break;
234 case LYXP_NODE_ELEM:
235 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
236 break;
237 default:
238 LOGINT(NULL);
239 break;
240 }
241 }
242 break;
243
Michal Vasko03ff5a72019-09-11 13:49:33 +0200244 case LYXP_SET_BOOLEAN:
245 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200246 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200247 break;
248
249 case LYXP_SET_STRING:
250 LOGDBG(LY_LDGXPATH, "set STRING");
251 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
252 break;
253
254 case LYXP_SET_NUMBER:
255 LOGDBG(LY_LDGXPATH, "set NUMBER");
256
257 if (isnan(set->val.num)) {
258 str = strdup("NaN");
259 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
260 str = strdup("0");
261 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
262 str = strdup("Infinity");
263 } else if (isinf(set->val.num) && signbit(set->val.num)) {
264 str = strdup("-Infinity");
265 } else if ((long long)set->val.num == set->val.num) {
266 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
267 str = NULL;
268 }
269 } else {
270 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
271 str = NULL;
272 }
273 }
274 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
275
276 LOGDBG(LY_LDGXPATH, "\t%s", str);
277 free(str);
278 }
279}
280
281#endif
282
283/**
284 * @brief Realloc the string \p str.
285 *
286 * @param[in] ctx libyang context for logging.
287 * @param[in] needed How much free space is required.
288 * @param[in,out] str Pointer to the string to use.
289 * @param[in,out] used Used bytes in \p str.
290 * @param[in,out] size Allocated bytes in \p str.
291 * @return LY_ERR
292 */
293static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100294cast_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 +0200295{
296 if (*size - *used < needed) {
297 do {
298 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
299 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
300 return LY_EINVAL;
301 }
302 *size += LYXP_STRING_CAST_SIZE_STEP;
303 } while (*size - *used < needed);
304 *str = ly_realloc(*str, *size * sizeof(char));
305 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
306 }
307
308 return LY_SUCCESS;
309}
310
311/**
312 * @brief Cast nodes recursively to one string @p str.
313 *
314 * @param[in] node Node to cast.
315 * @param[in] fake_cont Whether to put the data into a "fake" container.
316 * @param[in] root_type Type of the XPath root.
317 * @param[in] indent Current indent.
318 * @param[in,out] str Resulting string.
319 * @param[in,out] used Used bytes in @p str.
320 * @param[in,out] size Allocated bytes in @p str.
321 * @return LY_ERR
322 */
323static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200324cast_string_recursive(const struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
Radek Krejci0f969882020-08-21 16:56:47 +0200325 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200326{
Radek Krejci7f769d72020-07-11 23:13:56 +0200327 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200328 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200329 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200330 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200331 struct lyd_node_any *any;
332 LY_ERR rc;
333
334 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
335 return LY_SUCCESS;
336 }
337
338 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200339 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200340 LY_CHECK_RET(rc);
341 strcpy(*str + (*used - 1), "\n");
342 ++(*used);
343
344 ++indent;
345 }
346
347 switch (node->schema->nodetype) {
348 case LYS_CONTAINER:
349 case LYS_LIST:
350 case LYS_RPC:
351 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200352 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200353 LY_CHECK_RET(rc);
354 strcpy(*str + (*used - 1), "\n");
355 ++(*used);
356
Radek Krejcia1c1e542020-09-29 16:06:52 +0200357 for (child = lyd_child(node); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200358 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
359 LY_CHECK_RET(rc);
360 }
361
362 break;
363
364 case LYS_LEAF:
365 case LYS_LEAFLIST:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200366 value_str = LYD_CANON_VALUE(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200367
368 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200369 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 +0200370 memset(*str + (*used - 1), ' ', indent * 2);
371 *used += indent * 2;
372
373 /* print value */
374 if (*used == 1) {
375 sprintf(*str + (*used - 1), "%s", value_str);
376 *used += strlen(value_str);
377 } else {
378 sprintf(*str + (*used - 1), "%s\n", value_str);
379 *used += strlen(value_str) + 1;
380 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200381
382 break;
383
384 case LYS_ANYXML:
385 case LYS_ANYDATA:
386 any = (struct lyd_node_any *)node;
387 if (!(void *)any->value.tree) {
388 /* no content */
389 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200390 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200391 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200392 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100393
Michal Vasko60ea6352020-06-29 13:39:39 +0200394 if (any->value_type == LYD_ANYDATA_LYB) {
395 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200396 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 +0200397 /* successfully parsed */
398 free(any->value.mem);
399 any->value.tree = tree;
400 any->value_type = LYD_ANYDATA_DATATREE;
401 }
Radek Krejci7931b192020-06-25 17:05:03 +0200402 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200403 }
404
Michal Vasko03ff5a72019-09-11 13:49:33 +0200405 switch (any->value_type) {
406 case LYD_ANYDATA_STRING:
407 case LYD_ANYDATA_XML:
408 case LYD_ANYDATA_JSON:
409 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200410 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200411 break;
412 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200413 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200414 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200415 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100416 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200417 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200418 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200419 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200420 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200421 }
422 }
423
424 line = strtok_r(buf, "\n", &ptr);
425 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200426 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200427 if (rc != LY_SUCCESS) {
428 free(buf);
429 return rc;
430 }
431 memset(*str + (*used - 1), ' ', indent * 2);
432 *used += indent * 2;
433
434 strcpy(*str + (*used - 1), line);
435 *used += strlen(line);
436
437 strcpy(*str + (*used - 1), "\n");
438 *used += 1;
439 } while ((line = strtok_r(NULL, "\n", &ptr)));
440
441 free(buf);
442 break;
443
444 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200445 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200446 }
447
448 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200449 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200450 LY_CHECK_RET(rc);
451 strcpy(*str + (*used - 1), "\n");
452 ++(*used);
453
454 --indent;
455 }
456
457 return LY_SUCCESS;
458}
459
460/**
461 * @brief Cast an element into a string.
462 *
463 * @param[in] node Node to cast.
464 * @param[in] fake_cont Whether to put the data into a "fake" container.
465 * @param[in] root_type Type of the XPath root.
466 * @param[out] str Element cast to dynamically-allocated string.
467 * @return LY_ERR
468 */
469static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200470cast_string_elem(struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200471{
472 uint16_t used, size;
473 LY_ERR rc;
474
475 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200476 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200477 (*str)[0] = '\0';
478 used = 1;
479 size = LYXP_STRING_CAST_SIZE_START;
480
481 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
482 if (rc != LY_SUCCESS) {
483 free(*str);
484 return rc;
485 }
486
487 if (size > used) {
488 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200489 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200490 }
491 return LY_SUCCESS;
492}
493
494/**
495 * @brief Cast a LYXP_SET_NODE_SET set into a string.
496 * Context position aware.
497 *
498 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200499 * @param[out] str Cast dynamically-allocated string.
500 * @return LY_ERR
501 */
502static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100503cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200504{
Michal Vaskoaa677062021-03-09 13:52:53 +0100505 if (!set->used) {
506 *str = strdup("");
507 if (!*str) {
508 LOGMEM_RET(set->ctx);
509 }
510 return LY_SUCCESS;
511 }
512
Michal Vasko03ff5a72019-09-11 13:49:33 +0200513 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100514 case LYXP_NODE_NONE:
515 /* invalid */
516 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200517 case LYXP_NODE_ROOT:
518 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100519 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200520 case LYXP_NODE_ELEM:
521 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100522 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100523 case LYXP_NODE_META:
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200524 *str = strdup(set->val.meta[0].meta->value.canonical);
525 if (!*str) {
526 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200527 }
528 return LY_SUCCESS;
529 }
530
531 LOGINT_RET(set->ctx);
532}
533
534/**
535 * @brief Cast a string into an XPath number.
536 *
537 * @param[in] str String to use.
538 * @return Cast number.
539 */
540static long double
541cast_string_to_number(const char *str)
542{
543 long double num;
544 char *ptr;
545
546 errno = 0;
547 num = strtold(str, &ptr);
548 if (errno || *ptr) {
549 num = NAN;
550 }
551 return num;
552}
553
554/**
555 * @brief Callback for checking value equality.
556 *
Michal Vasko62524a92021-02-26 10:08:50 +0100557 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200558 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200559 * @param[in] val1_p First value.
560 * @param[in] val2_p Second value.
561 * @param[in] mod Whether hash table is being modified.
562 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200563 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200564 */
Radek Krejci857189e2020-09-01 13:26:36 +0200565static ly_bool
566set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200567{
568 struct lyxp_set_hash_node *val1, *val2;
569
570 val1 = (struct lyxp_set_hash_node *)val1_p;
571 val2 = (struct lyxp_set_hash_node *)val2_p;
572
573 if ((val1->node == val2->node) && (val1->type == val2->type)) {
574 return 1;
575 }
576
577 return 0;
578}
579
580/**
581 * @brief Insert node and its hash into set.
582 *
583 * @param[in] set et to insert to.
584 * @param[in] node Node with hash.
585 * @param[in] type Node type.
586 */
587static void
588set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
589{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200590 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200591 uint32_t i, hash;
592 struct lyxp_set_hash_node hnode;
593
594 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
595 /* create hash table and add all the nodes */
596 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
597 for (i = 0; i < set->used; ++i) {
598 hnode.node = set->val.nodes[i].node;
599 hnode.type = set->val.nodes[i].type;
600
601 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
602 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
603 hash = dict_hash_multi(hash, NULL, 0);
604
605 r = lyht_insert(set->ht, &hnode, hash, NULL);
606 assert(!r);
607 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200608
Michal Vasko9d6befd2019-12-11 14:56:56 +0100609 if (hnode.node == node) {
610 /* it was just added, do not add it twice */
611 node = NULL;
612 }
613 }
614 }
615
616 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200617 /* add the new node into hash table */
618 hnode.node = node;
619 hnode.type = type;
620
621 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
622 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
623 hash = dict_hash_multi(hash, NULL, 0);
624
625 r = lyht_insert(set->ht, &hnode, hash, NULL);
626 assert(!r);
627 (void)r;
628 }
629}
630
631/**
632 * @brief Remove node and its hash from set.
633 *
634 * @param[in] set Set to remove from.
635 * @param[in] node Node to remove.
636 * @param[in] type Node type.
637 */
638static void
639set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
640{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200641 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200642 struct lyxp_set_hash_node hnode;
643 uint32_t hash;
644
645 if (set->ht) {
646 hnode.node = node;
647 hnode.type = type;
648
649 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
650 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
651 hash = dict_hash_multi(hash, NULL, 0);
652
653 r = lyht_remove(set->ht, &hnode, hash);
654 assert(!r);
655 (void)r;
656
657 if (!set->ht->used) {
658 lyht_free(set->ht);
659 set->ht = NULL;
660 }
661 }
662}
663
664/**
665 * @brief Check whether node is in set based on its hash.
666 *
667 * @param[in] set Set to search in.
668 * @param[in] node Node to search for.
669 * @param[in] type Node type.
670 * @param[in] skip_idx Index in @p set to skip.
671 * @return LY_ERR
672 */
673static LY_ERR
674set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
675{
676 struct lyxp_set_hash_node hnode, *match_p;
677 uint32_t hash;
678
679 hnode.node = node;
680 hnode.type = type;
681
682 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
683 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
684 hash = dict_hash_multi(hash, NULL, 0);
685
686 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
687 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
688 /* we found it on the index that should be skipped, find another */
689 hnode = *match_p;
690 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
691 /* none other found */
692 return LY_SUCCESS;
693 }
694 }
695
696 return LY_EEXIST;
697 }
698
699 /* not found */
700 return LY_SUCCESS;
701}
702
Michal Vaskod3678892020-05-21 10:06:58 +0200703void
704lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200705{
706 if (!set) {
707 return;
708 }
709
710 if (set->type == LYXP_SET_NODE_SET) {
711 free(set->val.nodes);
712 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200713 } else if (set->type == LYXP_SET_SCNODE_SET) {
714 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200715 lyht_free(set->ht);
716 } else {
717 if (set->type == LYXP_SET_STRING) {
718 free(set->val.str);
719 }
720 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200721 }
Michal Vaskod3678892020-05-21 10:06:58 +0200722
723 set->val.nodes = NULL;
724 set->used = 0;
725 set->size = 0;
726 set->ht = NULL;
727 set->ctx_pos = 0;
728 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200729}
730
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100731/**
732 * @brief Free dynamically-allocated set.
733 *
734 * @param[in] set Set to free.
735 */
736static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200737lyxp_set_free(struct lyxp_set *set)
738{
739 if (!set) {
740 return;
741 }
742
Michal Vaskod3678892020-05-21 10:06:58 +0200743 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200744 free(set);
745}
746
747/**
748 * @brief Initialize set context.
749 *
750 * @param[in] new Set to initialize.
751 * @param[in] set Arbitrary initialized set.
752 */
753static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200754set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200755{
756 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200757 if (set) {
758 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200759 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100760 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200761 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100762 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200763 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200764 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200765 new->prefix_data = set->prefix_data;
Michal Vasko02a77382019-09-12 11:47:35 +0200766 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200767}
768
769/**
770 * @brief Create a deep copy of a set.
771 *
772 * @param[in] set Set to copy.
773 * @return Copy of @p set.
774 */
775static struct lyxp_set *
776set_copy(struct lyxp_set *set)
777{
778 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100779 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200780
781 if (!set) {
782 return NULL;
783 }
784
785 ret = malloc(sizeof *ret);
786 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
787 set_init(ret, set);
788
789 if (set->type == LYXP_SET_SCNODE_SET) {
790 ret->type = set->type;
791
792 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100793 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
794 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200795 uint32_t idx;
796 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
797 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100798 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200799 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200800 lyxp_set_free(ret);
801 return NULL;
802 }
Michal Vaskoba716542019-12-16 10:01:58 +0100803 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200804 }
805 }
806 } else if (set->type == LYXP_SET_NODE_SET) {
807 ret->type = set->type;
808 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
809 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
810 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
811
812 ret->used = ret->size = set->used;
813 ret->ctx_pos = set->ctx_pos;
814 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100815 if (set->ht) {
816 ret->ht = lyht_dup(set->ht);
817 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200818 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200819 memcpy(ret, set, sizeof *ret);
820 if (set->type == LYXP_SET_STRING) {
821 ret->val.str = strdup(set->val.str);
822 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
823 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200824 }
825
826 return ret;
827}
828
829/**
830 * @brief Fill XPath set with a string. Any current data are disposed of.
831 *
832 * @param[in] set Set to fill.
833 * @param[in] string String to fill into \p set.
834 * @param[in] str_len Length of \p string. 0 is a valid value!
835 */
836static void
837set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
838{
Michal Vaskod3678892020-05-21 10:06:58 +0200839 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200840
841 set->type = LYXP_SET_STRING;
842 if ((str_len == 0) && (string[0] != '\0')) {
843 string = "";
844 }
845 set->val.str = strndup(string, str_len);
846}
847
848/**
849 * @brief Fill XPath set with a number. Any current data are disposed of.
850 *
851 * @param[in] set Set to fill.
852 * @param[in] number Number to fill into \p set.
853 */
854static void
855set_fill_number(struct lyxp_set *set, long double number)
856{
Michal Vaskod3678892020-05-21 10:06:58 +0200857 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200858
859 set->type = LYXP_SET_NUMBER;
860 set->val.num = number;
861}
862
863/**
864 * @brief Fill XPath set with a boolean. Any current data are disposed of.
865 *
866 * @param[in] set Set to fill.
867 * @param[in] boolean Boolean to fill into \p set.
868 */
869static void
Radek Krejci857189e2020-09-01 13:26:36 +0200870set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200871{
Michal Vaskod3678892020-05-21 10:06:58 +0200872 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200873
874 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200875 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200876}
877
878/**
879 * @brief Fill XPath set with the value from another set (deep assign).
880 * Any current data are disposed of.
881 *
882 * @param[in] trg Set to fill.
883 * @param[in] src Source set to copy into \p trg.
884 */
885static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200886set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200887{
888 if (!trg || !src) {
889 return;
890 }
891
892 if (trg->type == LYXP_SET_NODE_SET) {
893 free(trg->val.nodes);
894 } else if (trg->type == LYXP_SET_STRING) {
895 free(trg->val.str);
896 }
897 set_init(trg, src);
898
899 if (src->type == LYXP_SET_SCNODE_SET) {
900 trg->type = LYXP_SET_SCNODE_SET;
901 trg->used = src->used;
902 trg->size = src->used;
903
904 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
905 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
906 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
907 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200908 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200909 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200910 set_fill_number(trg, src->val.num);
911 } else if (src->type == LYXP_SET_STRING) {
912 set_fill_string(trg, src->val.str, strlen(src->val.str));
913 } else {
914 if (trg->type == LYXP_SET_NODE_SET) {
915 free(trg->val.nodes);
916 } else if (trg->type == LYXP_SET_STRING) {
917 free(trg->val.str);
918 }
919
Michal Vaskod3678892020-05-21 10:06:58 +0200920 assert(src->type == LYXP_SET_NODE_SET);
921
922 trg->type = LYXP_SET_NODE_SET;
923 trg->used = src->used;
924 trg->size = src->used;
925 trg->ctx_pos = src->ctx_pos;
926 trg->ctx_size = src->ctx_size;
927
928 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
929 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
930 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
931 if (src->ht) {
932 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200933 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200934 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200935 }
936 }
937}
938
939/**
940 * @brief Clear context of all schema nodes.
941 *
942 * @param[in] set Set to clear.
943 */
944static void
945set_scnode_clear_ctx(struct lyxp_set *set)
946{
947 uint32_t i;
948
949 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100950 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
951 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
952 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
953 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200954 }
955 }
956}
957
958/**
959 * @brief Remove a node from a set. Removing last node changes
960 * set into LYXP_SET_EMPTY. Context position aware.
961 *
962 * @param[in] set Set to use.
963 * @param[in] idx Index from @p set of the node to be removed.
964 */
965static void
966set_remove_node(struct lyxp_set *set, uint32_t idx)
967{
968 assert(set && (set->type == LYXP_SET_NODE_SET));
969 assert(idx < set->used);
970
971 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
972
973 --set->used;
974 if (set->used) {
975 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
976 (set->used - idx) * sizeof *set->val.nodes);
977 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200978 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200979 }
980}
981
982/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100983 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200984 *
985 * @param[in] set Set to use.
986 * @param[in] idx Index from @p set of the node to be removed.
987 */
988static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100989set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200990{
991 assert(set && (set->type == LYXP_SET_NODE_SET));
992 assert(idx < set->used);
993
Michal Vasko2caefc12019-11-14 16:07:56 +0100994 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
995 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
996 }
997 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +0200998}
999
1000/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001001 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001002 * set into LYXP_SET_EMPTY. Context position aware.
1003 *
1004 * @param[in] set Set to consolidate.
1005 */
1006static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001007set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001008{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001009 uint32_t i, orig_used, end = 0;
1010 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001011
Michal Vaskod3678892020-05-21 10:06:58 +02001012 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001013
1014 orig_used = set->used;
1015 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001016 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001017 start = -1;
1018 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001019 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001020 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001021 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001022 end = i;
1023 ++i;
1024 break;
1025 }
1026
1027 ++i;
1028 if (i == orig_used) {
1029 end = i;
1030 }
1031 } while (i < orig_used);
1032
1033 if (start > -1) {
1034 /* move the whole chunk of valid nodes together */
1035 if (set->used != (unsigned)start) {
1036 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1037 }
1038 set->used += end - start;
1039 }
1040 }
Michal Vasko57eab132019-09-24 11:46:26 +02001041}
1042
1043/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001044 * @brief Check for duplicates in a node set.
1045 *
1046 * @param[in] set Set to check.
1047 * @param[in] node Node to look for in @p set.
1048 * @param[in] node_type Type of @p node.
1049 * @param[in] skip_idx Index from @p set to skip.
1050 * @return LY_ERR
1051 */
1052static LY_ERR
1053set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1054{
1055 uint32_t i;
1056
Michal Vasko2caefc12019-11-14 16:07:56 +01001057 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001058 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1059 }
1060
1061 for (i = 0; i < set->used; ++i) {
1062 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1063 continue;
1064 }
1065
1066 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1067 return LY_EEXIST;
1068 }
1069 }
1070
1071 return LY_SUCCESS;
1072}
1073
Radek Krejci857189e2020-09-01 13:26:36 +02001074ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001075lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1076 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001077{
1078 uint32_t i;
1079
1080 for (i = 0; i < set->used; ++i) {
1081 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1082 continue;
1083 }
1084
1085 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001086 if (index_p) {
1087 *index_p = i;
1088 }
1089 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001090 }
1091 }
1092
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001093 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001094}
1095
Michal Vaskoecd62de2019-11-13 12:35:11 +01001096void
1097lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001098{
1099 uint32_t orig_used, i, j;
1100
Michal Vaskod3678892020-05-21 10:06:58 +02001101 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001102
Michal Vaskod3678892020-05-21 10:06:58 +02001103 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001104 return;
1105 }
1106
Michal Vaskod3678892020-05-21 10:06:58 +02001107 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001108 memcpy(set1, set2, sizeof *set1);
1109 return;
1110 }
1111
1112 if (set1->used + set2->used > set1->size) {
1113 set1->size = set1->used + set2->used;
1114 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1115 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1116 }
1117
1118 orig_used = set1->used;
1119
1120 for (i = 0; i < set2->used; ++i) {
1121 for (j = 0; j < orig_used; ++j) {
1122 /* detect duplicities */
1123 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1124 break;
1125 }
1126 }
1127
Michal Vasko0587bce2020-12-10 12:19:21 +01001128 if (j < orig_used) {
1129 /* node is there, but update its status if needed */
1130 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1131 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
1132 }
1133 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001134 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1135 ++set1->used;
1136 }
1137 }
1138
Michal Vaskod3678892020-05-21 10:06:58 +02001139 lyxp_set_free_content(set2);
1140 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001141}
1142
1143/**
1144 * @brief Insert a node into a set. Context position aware.
1145 *
1146 * @param[in] set Set to use.
1147 * @param[in] node Node to insert to @p set.
1148 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1149 * @param[in] node_type Node type of @p node.
1150 * @param[in] idx Index in @p set to insert into.
1151 */
1152static void
1153set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1154{
Michal Vaskod3678892020-05-21 10:06:58 +02001155 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001156
Michal Vaskod3678892020-05-21 10:06:58 +02001157 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001158 /* first item */
1159 if (idx) {
1160 /* no real harm done, but it is a bug */
1161 LOGINT(set->ctx);
1162 idx = 0;
1163 }
1164 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1165 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1166 set->type = LYXP_SET_NODE_SET;
1167 set->used = 0;
1168 set->size = LYXP_SET_SIZE_START;
1169 set->ctx_pos = 1;
1170 set->ctx_size = 1;
1171 set->ht = NULL;
1172 } else {
1173 /* not an empty set */
1174 if (set->used == set->size) {
1175
1176 /* set is full */
1177 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1178 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1179 set->size += LYXP_SET_SIZE_STEP;
1180 }
1181
1182 if (idx > set->used) {
1183 LOGINT(set->ctx);
1184 idx = set->used;
1185 }
1186
1187 /* make space for the new node */
1188 if (idx < set->used) {
1189 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1190 }
1191 }
1192
1193 /* finally assign the value */
1194 set->val.nodes[idx].node = (struct lyd_node *)node;
1195 set->val.nodes[idx].type = node_type;
1196 set->val.nodes[idx].pos = pos;
1197 ++set->used;
1198
Michal Vasko2caefc12019-11-14 16:07:56 +01001199 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1200 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1201 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001202}
1203
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001204LY_ERR
1205lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001206{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001207 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001208
1209 assert(set->type == LYXP_SET_SCNODE_SET);
1210
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001211 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001212 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001213 } else {
1214 if (set->used == set->size) {
1215 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes);
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001216 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001217 set->size += LYXP_SET_SIZE_STEP;
1218 }
1219
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001220 index = set->used;
1221 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1222 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001223 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001224 ++set->used;
1225 }
1226
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001227 if (index_p) {
1228 *index_p = index;
1229 }
1230
1231 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001232}
1233
1234/**
1235 * @brief Replace a node in a set with another. Context position aware.
1236 *
1237 * @param[in] set Set to use.
1238 * @param[in] node Node to insert to @p set.
1239 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1240 * @param[in] node_type Node type of @p node.
1241 * @param[in] idx Index in @p set of the node to replace.
1242 */
1243static void
1244set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1245{
1246 assert(set && (idx < set->used));
1247
Michal Vasko2caefc12019-11-14 16:07:56 +01001248 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1249 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1250 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001251 set->val.nodes[idx].node = (struct lyd_node *)node;
1252 set->val.nodes[idx].type = node_type;
1253 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001254 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1255 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1256 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001257}
1258
1259/**
1260 * @brief Set all nodes with ctx 1 to a new unique context value.
1261 *
1262 * @param[in] set Set to modify.
1263 * @return New context value.
1264 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001265static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001266set_scnode_new_in_ctx(struct lyxp_set *set)
1267{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001268 uint32_t i;
1269 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001270
1271 assert(set->type == LYXP_SET_SCNODE_SET);
1272
Radek Krejcif13b87b2020-12-01 22:02:17 +01001273 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001274retry:
1275 for (i = 0; i < set->used; ++i) {
1276 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1277 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1278 goto retry;
1279 }
1280 }
1281 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001282 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001283 set->val.scnodes[i].in_ctx = ret_ctx;
1284 }
1285 }
1286
1287 return ret_ctx;
1288}
1289
1290/**
1291 * @brief Get unique @p node position in the data.
1292 *
1293 * @param[in] node Node to find.
1294 * @param[in] node_type Node type of @p node.
1295 * @param[in] root Root node.
1296 * @param[in] root_type Type of the XPath @p root node.
1297 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1298 * be used to increase efficiency and start the DFS from this node.
1299 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1300 * @return Node position.
1301 */
1302static uint32_t
1303get_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 +02001304 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001305{
Michal Vasko56daf732020-08-10 10:57:18 +02001306 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001307 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001308 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001309
1310 assert(prev && prev_pos && !root->prev->next);
1311
1312 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1313 return 0;
1314 }
1315
1316 if (*prev) {
1317 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001318 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001319 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001320 goto dfs_search;
1321 }
1322
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001323 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001324 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001325dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001326 if (*prev && !elem) {
1327 /* resume previous DFS */
1328 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1329 LYD_TREE_DFS_continue = 0;
1330 }
1331
Michal Vasko03ff5a72019-09-11 13:49:33 +02001332 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001333 /* skip */
1334 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001335 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001336 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001337 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001338 break;
1339 }
Michal Vasko56daf732020-08-10 10:57:18 +02001340 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001341 }
Michal Vasko56daf732020-08-10 10:57:18 +02001342
1343 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001344 }
1345
1346 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001347 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001348 break;
1349 }
1350 }
1351
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001352 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001353 if (!(*prev)) {
1354 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001355 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001356 return 0;
1357 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001358 /* start the search again from the beginning */
1359 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001360
Michal Vasko56daf732020-08-10 10:57:18 +02001361 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001362 pos = 1;
1363 goto dfs_search;
1364 }
1365 }
1366
1367 /* remember the last found node for next time */
1368 *prev = node;
1369 *prev_pos = pos;
1370
1371 return pos;
1372}
1373
1374/**
1375 * @brief Assign (fill) missing node positions.
1376 *
1377 * @param[in] set Set to fill positions in.
1378 * @param[in] root Context root node.
1379 * @param[in] root_type Context root type.
1380 * @return LY_ERR
1381 */
1382static LY_ERR
1383set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1384{
1385 const struct lyd_node *prev = NULL, *tmp_node;
1386 uint32_t i, tmp_pos = 0;
1387
1388 for (i = 0; i < set->used; ++i) {
1389 if (!set->val.nodes[i].pos) {
1390 tmp_node = NULL;
1391 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001392 case LYXP_NODE_META:
1393 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001394 if (!tmp_node) {
1395 LOGINT_RET(root->schema->module->ctx);
1396 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001397 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001398 case LYXP_NODE_ELEM:
1399 case LYXP_NODE_TEXT:
1400 if (!tmp_node) {
1401 tmp_node = set->val.nodes[i].node;
1402 }
1403 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1404 break;
1405 default:
1406 /* all roots have position 0 */
1407 break;
1408 }
1409 }
1410 }
1411
1412 return LY_SUCCESS;
1413}
1414
1415/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001416 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001417 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001418 * @param[in] meta Metadata to use.
1419 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001420 */
1421static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001422get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001423{
1424 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001425 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001426
Michal Vasko9f96a052020-03-10 09:41:45 +01001427 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001428 ++pos;
1429 }
1430
Michal Vasko9f96a052020-03-10 09:41:45 +01001431 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001432 return pos;
1433}
1434
1435/**
1436 * @brief Compare 2 nodes in respect to XPath document order.
1437 *
1438 * @param[in] item1 1st node.
1439 * @param[in] item2 2nd node.
1440 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1441 */
1442static int
1443set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1444{
Michal Vasko9f96a052020-03-10 09:41:45 +01001445 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001446
1447 if (item1->pos < item2->pos) {
1448 return -1;
1449 }
1450
1451 if (item1->pos > item2->pos) {
1452 return 1;
1453 }
1454
1455 /* node positions are equal, the fun case */
1456
1457 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1458 /* special case since text nodes are actually saved as their parents */
1459 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1460 if (item1->type == LYXP_NODE_ELEM) {
1461 assert(item2->type == LYXP_NODE_TEXT);
1462 return -1;
1463 } else {
1464 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1465 return 1;
1466 }
1467 }
1468
Michal Vasko9f96a052020-03-10 09:41:45 +01001469 /* we need meta positions now */
1470 if (item1->type == LYXP_NODE_META) {
1471 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001472 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001473 if (item2->type == LYXP_NODE_META) {
1474 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001475 }
1476
Michal Vasko9f96a052020-03-10 09:41:45 +01001477 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001478 /* check for duplicates */
1479 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001480 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001481 return 0;
1482 }
1483
Michal Vasko9f96a052020-03-10 09:41:45 +01001484 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001485 /* elem is always first, 2nd node is after it */
1486 if (item1->type == LYXP_NODE_ELEM) {
1487 assert(item2->type != LYXP_NODE_ELEM);
1488 return -1;
1489 }
1490
Michal Vasko9f96a052020-03-10 09:41:45 +01001491 /* 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 +02001492 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001493 if (((item1->type == LYXP_NODE_TEXT) &&
1494 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1495 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1496 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1497 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001498 return 1;
1499 }
1500
Michal Vasko9f96a052020-03-10 09:41:45 +01001501 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001502 /* 2nd is after 1st */
1503 return -1;
1504}
1505
1506/**
1507 * @brief Set cast for comparisons.
1508 *
1509 * @param[in] trg Target set to cast source into.
1510 * @param[in] src Source set.
1511 * @param[in] type Target set type.
1512 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001513 * @return LY_ERR
1514 */
1515static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001516set_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 +02001517{
1518 assert(src->type == LYXP_SET_NODE_SET);
1519
1520 set_init(trg, src);
1521
1522 /* insert node into target set */
1523 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1524
1525 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001526 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001527}
1528
Michal Vasko4c7763f2020-07-27 17:40:37 +02001529/**
1530 * @brief Set content canonization for comparisons.
1531 *
1532 * @param[in] trg Target set to put the canononized source into.
1533 * @param[in] src Source set.
1534 * @param[in] xp_node Source XPath node/meta to use for canonization.
1535 * @return LY_ERR
1536 */
1537static LY_ERR
1538set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1539{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001540 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001541 struct lyd_value val;
1542 struct ly_err_item *err = NULL;
1543 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001544 LY_ERR rc;
1545
1546 /* is there anything to canonize even? */
1547 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1548 /* do we have a type to use for canonization? */
1549 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1550 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1551 } else if (xp_node->type == LYXP_NODE_META) {
1552 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1553 }
1554 }
1555 if (!type) {
1556 goto fill;
1557 }
1558
1559 if (src->type == LYXP_SET_NUMBER) {
1560 /* canonize number */
1561 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1562 LOGMEM(src->ctx);
1563 return LY_EMEM;
1564 }
1565 } else {
1566 /* canonize string */
1567 str = strdup(src->val.str);
1568 }
1569
1570 /* ignore errors, the value may not satisfy schema constraints */
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001571 rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_STORE_DYNAMIC, src->format, src->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001572 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001573 ly_err_free(err);
1574 if (rc) {
1575 /* invalid value */
Radek IÅ¡a48460222021-03-02 15:18:32 +01001576 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001577 goto fill;
1578 }
1579
Michal Vasko4c7763f2020-07-27 17:40:37 +02001580 /* use the canonized value */
1581 set_init(trg, src);
1582 trg->type = src->type;
1583 if (src->type == LYXP_SET_NUMBER) {
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001584 trg->val.num = strtold(val.canonical, &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001585 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1586 } else {
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001587 trg->val.str = strdup(val.canonical);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001588 }
1589 type->plugin->free(src->ctx, &val);
1590 return LY_SUCCESS;
1591
1592fill:
1593 /* no canonization needed/possible */
1594 set_fill_set(trg, src);
1595 return LY_SUCCESS;
1596}
1597
Michal Vasko03ff5a72019-09-11 13:49:33 +02001598#ifndef NDEBUG
1599
1600/**
1601 * @brief Bubble sort @p set into XPath document order.
1602 * Context position aware. Unused in the 'Release' build target.
1603 *
1604 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001605 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1606 */
1607static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001608set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001609{
1610 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001611 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001612 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001613 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001614 struct lyxp_set_node item;
1615 struct lyxp_set_hash_node hnode;
1616 uint64_t hash;
1617
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001618 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001619 return 0;
1620 }
1621
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001622 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001623 for (root = set->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001624 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001625
1626 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001627 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001628 return -1;
1629 }
1630
1631 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1632 print_set_debug(set);
1633
1634 for (i = 0; i < set->used; ++i) {
1635 inverted = 0;
1636 change = 0;
1637
1638 for (j = 1; j < set->used - i; ++j) {
1639 /* compare node positions */
1640 if (inverted) {
1641 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1642 } else {
1643 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1644 }
1645
1646 /* swap if needed */
1647 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1648 change = 1;
1649
1650 item = set->val.nodes[j - 1];
1651 set->val.nodes[j - 1] = set->val.nodes[j];
1652 set->val.nodes[j] = item;
1653 } else {
1654 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1655 inverted = !inverted;
1656 }
1657 }
1658
1659 ++ret;
1660
1661 if (!change) {
1662 break;
1663 }
1664 }
1665
1666 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1667 print_set_debug(set);
1668
1669 /* check node hashes */
1670 if (set->used >= LYD_HT_MIN_ITEMS) {
1671 assert(set->ht);
1672 for (i = 0; i < set->used; ++i) {
1673 hnode.node = set->val.nodes[i].node;
1674 hnode.type = set->val.nodes[i].type;
1675
1676 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1677 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1678 hash = dict_hash_multi(hash, NULL, 0);
1679
1680 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1681 }
1682 }
1683
1684 return ret - 1;
1685}
1686
1687/**
1688 * @brief Remove duplicate entries in a sorted node set.
1689 *
1690 * @param[in] set Sorted set to check.
1691 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1692 */
1693static LY_ERR
1694set_sorted_dup_node_clean(struct lyxp_set *set)
1695{
1696 uint32_t i = 0;
1697 LY_ERR ret = LY_SUCCESS;
1698
1699 if (set->used > 1) {
1700 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001701 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1702 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001703 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001704 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001705 }
Michal Vasko57eab132019-09-24 11:46:26 +02001706 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001707 }
1708 }
1709
Michal Vasko2caefc12019-11-14 16:07:56 +01001710 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001711 return ret;
1712}
1713
1714#endif
1715
1716/**
1717 * @brief Merge 2 sorted sets into one.
1718 *
1719 * @param[in,out] trg Set to merge into. Duplicates are removed.
1720 * @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 +02001721 * @return LY_ERR
1722 */
1723static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001724set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001725{
1726 uint32_t i, j, k, count, dup_count;
1727 int cmp;
1728 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001729
Michal Vaskod3678892020-05-21 10:06:58 +02001730 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001731 return LY_EINVAL;
1732 }
1733
Michal Vaskod3678892020-05-21 10:06:58 +02001734 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001735 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001736 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001737 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001738 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001739 return LY_SUCCESS;
1740 }
1741
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001742 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001743 for (root = trg->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001744 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001745
1746 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001747 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001748 return LY_EINT;
1749 }
1750
1751#ifndef NDEBUG
1752 LOGDBG(LY_LDGXPATH, "MERGE target");
1753 print_set_debug(trg);
1754 LOGDBG(LY_LDGXPATH, "MERGE source");
1755 print_set_debug(src);
1756#endif
1757
1758 /* make memory for the merge (duplicates are not detected yet, so space
1759 * will likely be wasted on them, too bad) */
1760 if (trg->size - trg->used < src->used) {
1761 trg->size = trg->used + src->used;
1762
1763 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1764 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1765 }
1766
1767 i = 0;
1768 j = 0;
1769 count = 0;
1770 dup_count = 0;
1771 do {
1772 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1773 if (!cmp) {
1774 if (!count) {
1775 /* duplicate, just skip it */
1776 ++i;
1777 ++j;
1778 } else {
1779 /* we are copying something already, so let's copy the duplicate too,
1780 * we are hoping that afterwards there are some more nodes to
1781 * copy and this way we can copy them all together */
1782 ++count;
1783 ++dup_count;
1784 ++i;
1785 ++j;
1786 }
1787 } else if (cmp < 0) {
1788 /* inserting src node into trg, just remember it for now */
1789 ++count;
1790 ++i;
1791
1792 /* insert the hash now */
1793 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1794 } else if (count) {
1795copy_nodes:
1796 /* time to actually copy the nodes, we have found the largest block of nodes */
1797 memmove(&trg->val.nodes[j + (count - dup_count)],
1798 &trg->val.nodes[j],
1799 (trg->used - j) * sizeof *trg->val.nodes);
1800 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1801
1802 trg->used += count - dup_count;
1803 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1804 j += count - dup_count;
1805
1806 count = 0;
1807 dup_count = 0;
1808 } else {
1809 ++j;
1810 }
1811 } while ((i < src->used) && (j < trg->used));
1812
1813 if ((i < src->used) || count) {
1814 /* insert all the hashes first */
1815 for (k = i; k < src->used; ++k) {
1816 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1817 }
1818
1819 /* loop ended, but we need to copy something at trg end */
1820 count += src->used - i;
1821 i = src->used;
1822 goto copy_nodes;
1823 }
1824
1825 /* we are inserting hashes before the actual node insert, which causes
1826 * situations when there were initially not enough items for a hash table,
1827 * but even after some were inserted, hash table was not created (during
1828 * insertion the number of items is not updated yet) */
1829 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1830 set_insert_node_hash(trg, NULL, 0);
1831 }
1832
1833#ifndef NDEBUG
1834 LOGDBG(LY_LDGXPATH, "MERGE result");
1835 print_set_debug(trg);
1836#endif
1837
Michal Vaskod3678892020-05-21 10:06:58 +02001838 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001839 return LY_SUCCESS;
1840}
1841
1842/*
1843 * (re)parse functions
1844 *
1845 * Parse functions parse the expression into
1846 * tokens (syntactic analysis).
1847 *
1848 * Reparse functions perform semantic analysis
1849 * (do not save the result, just a check) of
1850 * the expression and fill repeat indices.
1851 */
1852
Michal Vasko14676352020-05-29 11:35:55 +02001853LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001854lyxp_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 +02001855{
Michal Vasko004d3152020-06-11 19:59:22 +02001856 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001857 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001858 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001859 }
Michal Vasko14676352020-05-29 11:35:55 +02001860 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001861 }
1862
Michal Vasko004d3152020-06-11 19:59:22 +02001863 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001864 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001865 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001866 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001867 }
Michal Vasko14676352020-05-29 11:35:55 +02001868 return LY_ENOT;
1869 }
1870
1871 return LY_SUCCESS;
1872}
1873
Michal Vasko004d3152020-06-11 19:59:22 +02001874LY_ERR
1875lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1876{
1877 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1878
1879 /* skip the token */
1880 ++(*tok_idx);
1881
1882 return LY_SUCCESS;
1883}
1884
Michal Vasko14676352020-05-29 11:35:55 +02001885/* just like lyxp_check_token() but tests for 2 tokens */
1886static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001887exp_check_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok1,
Radek Krejci0f969882020-08-21 16:56:47 +02001888 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001889{
Michal Vasko004d3152020-06-11 19:59:22 +02001890 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001891 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001892 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001893 }
1894 return LY_EINCOMPLETE;
1895 }
1896
Michal Vasko004d3152020-06-11 19:59:22 +02001897 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001898 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001899 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001900 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001901 }
1902 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001903 }
1904
1905 return LY_SUCCESS;
1906}
1907
1908/**
1909 * @brief Stack operation push on the repeat array.
1910 *
1911 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001912 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001913 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1914 */
1915static void
Michal Vasko004d3152020-06-11 19:59:22 +02001916exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001917{
1918 uint16_t i;
1919
Michal Vasko004d3152020-06-11 19:59:22 +02001920 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001921 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001922 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1923 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1924 exp->repeat[tok_idx][i] = repeat_op_idx;
1925 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001926 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001927 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1928 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1929 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001930 }
1931}
1932
1933/**
1934 * @brief Reparse Predicate. Logs directly on error.
1935 *
1936 * [7] Predicate ::= '[' Expr ']'
1937 *
1938 * @param[in] ctx Context for logging.
1939 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001940 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001941 * @return LY_ERR
1942 */
1943static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001944reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001945{
1946 LY_ERR rc;
1947
Michal Vasko004d3152020-06-11 19:59:22 +02001948 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001949 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001950 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001951
Michal Vasko004d3152020-06-11 19:59:22 +02001952 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001953 LY_CHECK_RET(rc);
1954
Michal Vasko004d3152020-06-11 19:59:22 +02001955 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001956 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001957 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001958
1959 return LY_SUCCESS;
1960}
1961
1962/**
1963 * @brief Reparse RelativeLocationPath. Logs directly on error.
1964 *
1965 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1966 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1967 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1968 *
1969 * @param[in] ctx Context for logging.
1970 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001971 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001972 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1973 */
1974static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001975reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001976{
1977 LY_ERR rc;
1978
Michal Vasko004d3152020-06-11 19:59:22 +02001979 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001980 LY_CHECK_RET(rc);
1981
1982 goto step;
1983 do {
1984 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001985 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001986
Michal Vasko004d3152020-06-11 19:59:22 +02001987 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001988 LY_CHECK_RET(rc);
1989step:
1990 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001991 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001992 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001993 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001994 break;
1995
1996 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001997 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001998 break;
1999
2000 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002001 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002002
Michal Vasko004d3152020-06-11 19:59:22 +02002003 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002004 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002005 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002006 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002007 return LY_EVALID;
2008 }
Radek Krejci0f969882020-08-21 16:56:47 +02002009 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002010 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002011 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002012 goto reparse_predicate;
2013 break;
2014
2015 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002016 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002017
2018 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002019 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002020 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002021 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002022
2023 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002024 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002025 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002026 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002027
2028reparse_predicate:
2029 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002030 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2031 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002032 LY_CHECK_RET(rc);
2033 }
2034 break;
2035 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002036 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002037 return LY_EVALID;
2038 }
Michal Vasko004d3152020-06-11 19:59:22 +02002039 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002040
2041 return LY_SUCCESS;
2042}
2043
2044/**
2045 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2046 *
2047 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2048 *
2049 * @param[in] ctx Context for logging.
2050 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002051 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002052 * @return LY_ERR
2053 */
2054static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002055reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002056{
2057 LY_ERR rc;
2058
Michal Vasko004d3152020-06-11 19:59:22 +02002059 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 +02002060
2061 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002062 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002063 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002064 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002065
Michal Vasko004d3152020-06-11 19:59:22 +02002066 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002067 return LY_SUCCESS;
2068 }
Michal Vasko004d3152020-06-11 19:59:22 +02002069 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002070 case LYXP_TOKEN_DOT:
2071 case LYXP_TOKEN_DDOT:
2072 case LYXP_TOKEN_AT:
2073 case LYXP_TOKEN_NAMETEST:
2074 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002075 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002076 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002077 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002078 default:
2079 break;
2080 }
2081
Michal Vasko03ff5a72019-09-11 13:49:33 +02002082 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002083 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002084 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002085
Michal Vasko004d3152020-06-11 19:59:22 +02002086 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002087 LY_CHECK_RET(rc);
2088 }
2089
2090 return LY_SUCCESS;
2091}
2092
2093/**
2094 * @brief Reparse FunctionCall. Logs directly on error.
2095 *
2096 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2097 *
2098 * @param[in] ctx Context for logging.
2099 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002100 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002101 * @return LY_ERR
2102 */
2103static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002104reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002105{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002106 int8_t min_arg_count = -1;
2107 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002108 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002109 LY_ERR rc;
2110
Michal Vasko004d3152020-06-11 19:59:22 +02002111 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002112 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002113 func_tok_idx = *tok_idx;
2114 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002115 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002116 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
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]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002120 min_arg_count = 1;
2121 max_arg_count = 1;
2122 }
2123 break;
2124 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002125 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
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]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002129 min_arg_count = 0;
2130 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002131 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002132 min_arg_count = 0;
2133 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002134 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002135 min_arg_count = 0;
2136 max_arg_count = 0;
2137 }
2138 break;
2139 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002140 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002141 min_arg_count = 1;
2142 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002143 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002144 min_arg_count = 0;
2145 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002146 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
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]], "round", 5)) {
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]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002153 min_arg_count = 1;
2154 max_arg_count = 1;
2155 }
2156 break;
2157 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002158 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002159 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002160 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002161 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002162 min_arg_count = 0;
2163 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002164 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002165 min_arg_count = 0;
2166 max_arg_count = 1;
2167 }
2168 break;
2169 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002170 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002171 min_arg_count = 1;
2172 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002173 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002174 min_arg_count = 1;
2175 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002176 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002177 min_arg_count = 0;
2178 max_arg_count = 0;
2179 }
2180 break;
2181 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002182 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002183 min_arg_count = 2;
2184 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002185 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002186 min_arg_count = 0;
2187 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002188 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002189 min_arg_count = 2;
2190 max_arg_count = 2;
2191 }
2192 break;
2193 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002194 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002195 min_arg_count = 2;
2196 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002197 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002198 min_arg_count = 3;
2199 max_arg_count = 3;
2200 }
2201 break;
2202 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002203 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
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]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002207 min_arg_count = 1;
2208 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002209 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002210 min_arg_count = 2;
2211 max_arg_count = 2;
2212 }
2213 break;
2214 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002215 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002216 min_arg_count = 2;
2217 max_arg_count = 2;
2218 }
2219 break;
2220 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002221 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002222 min_arg_count = 2;
2223 max_arg_count = 2;
2224 }
2225 break;
2226 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002227 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002228 min_arg_count = 0;
2229 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002230 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002231 min_arg_count = 0;
2232 max_arg_count = 1;
2233 }
2234 break;
2235 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002236 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002237 min_arg_count = 0;
2238 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002239 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002240 min_arg_count = 2;
2241 max_arg_count = 2;
2242 }
2243 break;
2244 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002245 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002246 min_arg_count = 2;
2247 max_arg_count = 2;
2248 }
2249 break;
2250 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002251 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002252 min_arg_count = 2;
2253 max_arg_count = 2;
2254 }
2255 break;
2256 }
2257 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002258 LOGVAL(ctx, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002259 return LY_EINVAL;
2260 }
Michal Vasko004d3152020-06-11 19:59:22 +02002261 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002262
2263 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002264 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002265 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002266 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002267
2268 /* ( Expr ( ',' Expr )* )? */
2269 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002270 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002271 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002272 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002273 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002274 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002275 LY_CHECK_RET(rc);
2276 }
Michal Vasko004d3152020-06-11 19:59:22 +02002277 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2278 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002279
2280 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002281 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002282 LY_CHECK_RET(rc);
2283 }
2284
2285 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002286 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002287 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002288 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002289
Radek Krejci857189e2020-09-01 13:26:36 +02002290 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002291 LOGVAL(ctx, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002292 return LY_EVALID;
2293 }
2294
2295 return LY_SUCCESS;
2296}
2297
2298/**
2299 * @brief Reparse PathExpr. Logs directly on error.
2300 *
2301 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2302 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2303 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2304 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2305 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2306 *
2307 * @param[in] ctx Context for logging.
2308 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002309 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002310 * @return LY_ERR
2311 */
2312static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002313reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002314{
2315 LY_ERR rc;
2316
Michal Vasko004d3152020-06-11 19:59:22 +02002317 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002318 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002319 }
2320
Michal Vasko004d3152020-06-11 19:59:22 +02002321 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002322 case LYXP_TOKEN_PAR1:
2323 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002324 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002325
Michal Vasko004d3152020-06-11 19:59:22 +02002326 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002327 LY_CHECK_RET(rc);
2328
Michal Vasko004d3152020-06-11 19:59:22 +02002329 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002330 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002331 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002332 goto predicate;
2333 break;
2334 case LYXP_TOKEN_DOT:
2335 case LYXP_TOKEN_DDOT:
2336 case LYXP_TOKEN_AT:
2337 case LYXP_TOKEN_NAMETEST:
2338 case LYXP_TOKEN_NODETYPE:
2339 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002340 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002341 LY_CHECK_RET(rc);
2342 break;
2343 case LYXP_TOKEN_FUNCNAME:
2344 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002345 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002346 LY_CHECK_RET(rc);
2347 goto predicate;
2348 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002349 case LYXP_TOKEN_OPER_PATH:
2350 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002351 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002352 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002353 LY_CHECK_RET(rc);
2354 break;
2355 case LYXP_TOKEN_LITERAL:
2356 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002357 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002358 goto predicate;
2359 break;
2360 case LYXP_TOKEN_NUMBER:
2361 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002362 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002363 goto predicate;
2364 break;
2365 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002366 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002367 return LY_EVALID;
2368 }
2369
2370 return LY_SUCCESS;
2371
2372predicate:
2373 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002374 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2375 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002376 LY_CHECK_RET(rc);
2377 }
2378
2379 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002380 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002381
2382 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002383 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002384
Michal Vasko004d3152020-06-11 19:59:22 +02002385 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002386 LY_CHECK_RET(rc);
2387 }
2388
2389 return LY_SUCCESS;
2390}
2391
2392/**
2393 * @brief Reparse UnaryExpr. Logs directly on error.
2394 *
2395 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2396 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2397 *
2398 * @param[in] ctx Context for logging.
2399 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002400 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002401 * @return LY_ERR
2402 */
2403static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002404reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002405{
2406 uint16_t prev_exp;
2407 LY_ERR rc;
2408
2409 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002410 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002411 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2412 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002413 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002414 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002415 }
2416
2417 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002418 prev_exp = *tok_idx;
2419 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002420 LY_CHECK_RET(rc);
2421
2422 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002423 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002424 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002425 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002426
Michal Vasko004d3152020-06-11 19:59:22 +02002427 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002428 LY_CHECK_RET(rc);
2429 }
2430
2431 return LY_SUCCESS;
2432}
2433
2434/**
2435 * @brief Reparse AdditiveExpr. Logs directly on error.
2436 *
2437 * [15] AdditiveExpr ::= MultiplicativeExpr
2438 * | AdditiveExpr '+' MultiplicativeExpr
2439 * | AdditiveExpr '-' MultiplicativeExpr
2440 * [16] MultiplicativeExpr ::= UnaryExpr
2441 * | MultiplicativeExpr '*' UnaryExpr
2442 * | MultiplicativeExpr 'div' UnaryExpr
2443 * | MultiplicativeExpr 'mod' UnaryExpr
2444 *
2445 * @param[in] ctx Context for logging.
2446 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002447 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002448 * @return LY_ERR
2449 */
2450static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002451reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002452{
2453 uint16_t prev_add_exp, prev_mul_exp;
2454 LY_ERR rc;
2455
Michal Vasko004d3152020-06-11 19:59:22 +02002456 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002457 goto reparse_multiplicative_expr;
2458
2459 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002460 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2461 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002462 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002463 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002464
2465reparse_multiplicative_expr:
2466 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002467 prev_mul_exp = *tok_idx;
2468 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002469 LY_CHECK_RET(rc);
2470
2471 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002472 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2473 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002474 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002475 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002476
Michal Vasko004d3152020-06-11 19:59:22 +02002477 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002478 LY_CHECK_RET(rc);
2479 }
2480 }
2481
2482 return LY_SUCCESS;
2483}
2484
2485/**
2486 * @brief Reparse EqualityExpr. Logs directly on error.
2487 *
2488 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2489 * | EqualityExpr '!=' RelationalExpr
2490 * [14] RelationalExpr ::= AdditiveExpr
2491 * | RelationalExpr '<' AdditiveExpr
2492 * | RelationalExpr '>' AdditiveExpr
2493 * | RelationalExpr '<=' AdditiveExpr
2494 * | RelationalExpr '>=' AdditiveExpr
2495 *
2496 * @param[in] ctx Context for logging.
2497 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002498 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002499 * @return LY_ERR
2500 */
2501static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002502reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002503{
2504 uint16_t prev_eq_exp, prev_rel_exp;
2505 LY_ERR rc;
2506
Michal Vasko004d3152020-06-11 19:59:22 +02002507 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002508 goto reparse_additive_expr;
2509
2510 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002511 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002512 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002513 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002514
2515reparse_additive_expr:
2516 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002517 prev_rel_exp = *tok_idx;
2518 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002519 LY_CHECK_RET(rc);
2520
2521 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002522 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002523 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002524 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002525
Michal Vasko004d3152020-06-11 19:59:22 +02002526 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002527 LY_CHECK_RET(rc);
2528 }
2529 }
2530
2531 return LY_SUCCESS;
2532}
2533
2534/**
2535 * @brief Reparse OrExpr. Logs directly on error.
2536 *
2537 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2538 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2539 *
2540 * @param[in] ctx Context for logging.
2541 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002542 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002543 * @return LY_ERR
2544 */
2545static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002546reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002547{
2548 uint16_t prev_or_exp, prev_and_exp;
2549 LY_ERR rc;
2550
Michal Vasko004d3152020-06-11 19:59:22 +02002551 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002552 goto reparse_equality_expr;
2553
2554 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002555 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 +02002556 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002557 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002558
2559reparse_equality_expr:
2560 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002561 prev_and_exp = *tok_idx;
2562 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002563 LY_CHECK_RET(rc);
2564
2565 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002566 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 +02002567 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002568 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002569
Michal Vasko004d3152020-06-11 19:59:22 +02002570 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002571 LY_CHECK_RET(rc);
2572 }
2573 }
2574
2575 return LY_SUCCESS;
2576}
Radek Krejcib1646a92018-11-02 16:08:26 +01002577
2578/**
2579 * @brief Parse NCName.
2580 *
2581 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002582 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002583 */
Radek Krejcid4270262019-01-07 15:07:25 +01002584static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002585parse_ncname(const char *ncname)
2586{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002587 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002588 size_t size;
2589 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002590
2591 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2592 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2593 return len;
2594 }
2595
2596 do {
2597 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002598 if (!*ncname) {
2599 break;
2600 }
Radek Krejcid4270262019-01-07 15:07:25 +01002601 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002602 } while (is_xmlqnamechar(uc) && (uc != ':'));
2603
2604 return len;
2605}
2606
2607/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002608 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002609 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002610 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002611 * @param[in] exp Expression to use.
2612 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002613 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002614 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002615 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002616 */
2617static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002618exp_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 +01002619{
2620 uint32_t prev;
2621
2622 if (exp->used == exp->size) {
2623 prev = exp->size;
2624 exp->size += LYXP_EXPR_SIZE_STEP;
2625 if (prev > exp->size) {
2626 LOGINT(ctx);
2627 return LY_EINT;
2628 }
2629
2630 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2631 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2632 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2633 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2634 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2635 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2636 }
2637
2638 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002639 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002640 exp->tok_len[exp->used] = tok_len;
2641 ++exp->used;
2642 return LY_SUCCESS;
2643}
2644
2645void
Michal Vasko14676352020-05-29 11:35:55 +02002646lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002647{
2648 uint16_t i;
2649
2650 if (!expr) {
2651 return;
2652 }
2653
2654 lydict_remove(ctx, expr->expr);
2655 free(expr->tokens);
2656 free(expr->tok_pos);
2657 free(expr->tok_len);
2658 if (expr->repeat) {
2659 for (i = 0; i < expr->used; ++i) {
2660 free(expr->repeat[i]);
2661 }
2662 }
2663 free(expr->repeat);
2664 free(expr);
2665}
2666
Radek Krejcif03a9e22020-09-18 20:09:31 +02002667LY_ERR
2668lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr_str, size_t expr_len, ly_bool reparse, struct lyxp_expr **expr_p)
Radek Krejcib1646a92018-11-02 16:08:26 +01002669{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002670 LY_ERR ret = LY_SUCCESS;
2671 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002672 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002673 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002674 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002675 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002676
Radek Krejcif03a9e22020-09-18 20:09:31 +02002677 assert(expr_p);
2678
2679 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002680 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002681 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002682 }
2683
2684 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002685 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002686 }
2687 if (expr_len > UINT16_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002688 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002689 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002690 }
2691
2692 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002693 expr = calloc(1, sizeof *expr);
2694 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2695 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2696 expr->used = 0;
2697 expr->size = LYXP_EXPR_SIZE_START;
2698 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2699 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002700
Radek Krejcif03a9e22020-09-18 20:09:31 +02002701 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2702 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002703
Radek Krejcif03a9e22020-09-18 20:09:31 +02002704 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2705 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002706
Michal Vasko004d3152020-06-11 19:59:22 +02002707 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002708 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002709
Radek Krejcif03a9e22020-09-18 20:09:31 +02002710 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002711 ++parsed;
2712 }
2713
2714 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002715 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002716
2717 /* '(' */
2718 tok_len = 1;
2719 tok_type = LYXP_TOKEN_PAR1;
2720
Radek Krejcif03a9e22020-09-18 20:09:31 +02002721 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002722 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002723 if (((expr->tok_len[expr->used - 1] == 4) &&
2724 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2725 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2726 ((expr->tok_len[expr->used - 1] == 7) &&
2727 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002728 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002729 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002730 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002731 }
2732 prev_function_check = 0;
2733 }
2734
Radek Krejcif03a9e22020-09-18 20:09:31 +02002735 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002736
2737 /* ')' */
2738 tok_len = 1;
2739 tok_type = LYXP_TOKEN_PAR2;
2740
Radek Krejcif03a9e22020-09-18 20:09:31 +02002741 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002742
2743 /* '[' */
2744 tok_len = 1;
2745 tok_type = LYXP_TOKEN_BRACK1;
2746
Radek Krejcif03a9e22020-09-18 20:09:31 +02002747 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002748
2749 /* ']' */
2750 tok_len = 1;
2751 tok_type = LYXP_TOKEN_BRACK2;
2752
Radek Krejcif03a9e22020-09-18 20:09:31 +02002753 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002754
2755 /* '..' */
2756 tok_len = 2;
2757 tok_type = LYXP_TOKEN_DDOT;
2758
Radek Krejcif03a9e22020-09-18 20:09:31 +02002759 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002760
2761 /* '.' */
2762 tok_len = 1;
2763 tok_type = LYXP_TOKEN_DOT;
2764
Radek Krejcif03a9e22020-09-18 20:09:31 +02002765 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002766
2767 /* '@' */
2768 tok_len = 1;
2769 tok_type = LYXP_TOKEN_AT;
2770
Radek Krejcif03a9e22020-09-18 20:09:31 +02002771 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002772
2773 /* ',' */
2774 tok_len = 1;
2775 tok_type = LYXP_TOKEN_COMMA;
2776
Radek Krejcif03a9e22020-09-18 20:09:31 +02002777 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002778
2779 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002780 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2781 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002782 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002783 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002784 ++tok_len;
2785 tok_type = LYXP_TOKEN_LITERAL;
2786
Radek Krejcif03a9e22020-09-18 20:09:31 +02002787 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002788
2789 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002790 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2791 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002792 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002793 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002794 ++tok_len;
2795 tok_type = LYXP_TOKEN_LITERAL;
2796
Radek Krejcif03a9e22020-09-18 20:09:31 +02002797 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002798
2799 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002800 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2801 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002802 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002803 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002804 }
2805 tok_type = LYXP_TOKEN_NUMBER;
2806
Radek Krejcif03a9e22020-09-18 20:09:31 +02002807 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002808
2809 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002810 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002811 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002812 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002813 } else {
2814 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002815 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002816 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002817
Radek Krejcif03a9e22020-09-18 20:09:31 +02002818 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002819
Michal Vasko3e48bf32020-06-01 08:39:07 +02002820 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002821 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002822 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2823
Radek Krejcif03a9e22020-09-18 20:09:31 +02002824 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002825
2826 /* Operator '<=', '>=' */
2827 tok_len = 2;
2828 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002829
Radek Krejcif03a9e22020-09-18 20:09:31 +02002830 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002831
2832 /* Operator '|' */
2833 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002834 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002835
Radek Krejcif03a9e22020-09-18 20:09:31 +02002836 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002837
2838 /* Operator '+', '-' */
2839 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002840 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002841
Radek Krejcif03a9e22020-09-18 20:09:31 +02002842 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002843
Michal Vasko3e48bf32020-06-01 08:39:07 +02002844 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002845 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002846 tok_type = LYXP_TOKEN_OPER_EQUAL;
2847
Radek Krejcif03a9e22020-09-18 20:09:31 +02002848 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002849
2850 /* Operator '<', '>' */
2851 tok_len = 1;
2852 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002853
Michal Vasko69730152020-10-09 16:30:07 +02002854 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2855 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2856 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2857 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2858 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2859 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2860 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2861 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2862 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2863 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2864 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2865 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002866
2867 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002868 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002869 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002870 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002871
Radek Krejcif03a9e22020-09-18 20:09:31 +02002872 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002873 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002874 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002875
Radek Krejcif03a9e22020-09-18 20:09:31 +02002876 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002877 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002878 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002879
Radek Krejcif03a9e22020-09-18 20:09:31 +02002880 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002881 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002882 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002883
2884 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002885 LOGVAL(ctx, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
Michal Vasko69730152020-10-09 16:30:07 +02002886 expr_str[parsed], expr_str[parsed], expr->tok_len[expr->used - 1], &expr->expr[expr->tok_pos[expr->used - 1]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002887 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002888 goto error;
2889 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002890 LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002891 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002892 goto error;
2893 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002894 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002895
2896 /* NameTest '*' */
2897 tok_len = 1;
2898 tok_type = LYXP_TOKEN_NAMETEST;
2899
2900 } else {
2901
2902 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002903 long int ncname_len = parse_ncname(&expr_str[parsed]);
2904 LY_CHECK_ERR_GOTO(ncname_len < 0,
Radek Krejci2efc45b2020-12-22 16:25:44 +01002905 LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002906 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002907 tok_len = ncname_len;
2908
Radek Krejcif03a9e22020-09-18 20:09:31 +02002909 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002910 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002911 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002912 ++tok_len;
2913 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002914 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
2915 LY_CHECK_ERR_GOTO(ncname_len < 0,
Radek Krejci2efc45b2020-12-22 16:25:44 +01002916 LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002917 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002918 tok_len += ncname_len;
2919 }
2920 /* remove old flag to prevent ambiguities */
2921 prev_function_check = 0;
2922 tok_type = LYXP_TOKEN_NAMETEST;
2923 } else {
2924 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2925 prev_function_check = 1;
2926 tok_type = LYXP_TOKEN_NAMETEST;
2927 }
2928 }
2929
2930 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002931 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002932 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002933 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002934 ++parsed;
2935 }
2936
Radek Krejcif03a9e22020-09-18 20:09:31 +02002937 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002938
Michal Vasko004d3152020-06-11 19:59:22 +02002939 if (reparse) {
2940 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002941 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2942 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002943
Michal Vasko004d3152020-06-11 19:59:22 +02002944 /* fill repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002945 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error);
2946 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002947 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002948 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002949 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002950 goto error;
2951 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002952 }
2953
Radek Krejcif03a9e22020-09-18 20:09:31 +02002954 print_expr_struct_debug(expr);
2955 *expr_p = expr;
2956 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002957
2958error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002959 lyxp_expr_free(ctx, expr);
2960 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002961}
2962
Michal Vasko1734be92020-09-22 08:55:10 +02002963LY_ERR
2964lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02002965{
Michal Vasko1734be92020-09-22 08:55:10 +02002966 LY_ERR ret = LY_SUCCESS;
2967 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02002968 uint32_t i, j;
2969
Michal Vasko7f45cf22020-10-01 12:49:44 +02002970 if (!exp) {
2971 goto cleanup;
2972 }
2973
Michal Vasko004d3152020-06-11 19:59:22 +02002974 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02002975 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002976
2977 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
Michal Vasko1734be92020-09-22 08:55:10 +02002978 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002979 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2980
2981 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
Michal Vasko1734be92020-09-22 08:55:10 +02002982 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002983 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2984
2985 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
Michal Vasko1734be92020-09-22 08:55:10 +02002986 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002987 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2988
2989 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002990 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002991 for (i = 0; i < exp->used; ++i) {
2992 if (!exp->repeat[i]) {
2993 dup->repeat[i] = NULL;
2994 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02002995 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002996 /* the ending 0 as well */
2997 ++j;
2998
Michal Vasko99c71642020-07-03 13:33:36 +02002999 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02003000 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003001 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3002 dup->repeat[i][j - 1] = 0;
3003 }
3004 }
3005
3006 dup->used = exp->used;
3007 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003008 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003009
Michal Vasko1734be92020-09-22 08:55:10 +02003010cleanup:
3011 if (ret) {
3012 lyxp_expr_free(ctx, dup);
3013 } else {
3014 *dup_p = dup;
3015 }
3016 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003017}
3018
Michal Vasko03ff5a72019-09-11 13:49:33 +02003019/*
3020 * warn functions
3021 *
3022 * Warn functions check specific reasonable conditions for schema XPath
3023 * and print a warning if they are not satisfied.
3024 */
3025
3026/**
3027 * @brief Get the last-added schema node that is currently in the context.
3028 *
3029 * @param[in] set Set to search in.
3030 * @return Last-added schema context node, NULL if no node is in context.
3031 */
3032static struct lysc_node *
3033warn_get_scnode_in_ctx(struct lyxp_set *set)
3034{
3035 uint32_t i;
3036
3037 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3038 return NULL;
3039 }
3040
3041 i = set->used;
3042 do {
3043 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003044 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003045 /* if there are more, simply return the first found (last added) */
3046 return set->val.scnodes[i].scnode;
3047 }
3048 } while (i);
3049
3050 return NULL;
3051}
3052
3053/**
3054 * @brief Test whether a type is numeric - integer type or decimal64.
3055 *
3056 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003057 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003058 */
Radek Krejci857189e2020-09-01 13:26:36 +02003059static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003060warn_is_numeric_type(struct lysc_type *type)
3061{
3062 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003063 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003064 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003065
3066 switch (type->basetype) {
3067 case LY_TYPE_DEC64:
3068 case LY_TYPE_INT8:
3069 case LY_TYPE_UINT8:
3070 case LY_TYPE_INT16:
3071 case LY_TYPE_UINT16:
3072 case LY_TYPE_INT32:
3073 case LY_TYPE_UINT32:
3074 case LY_TYPE_INT64:
3075 case LY_TYPE_UINT64:
3076 return 1;
3077 case LY_TYPE_UNION:
3078 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003079 LY_ARRAY_FOR(uni->types, u) {
3080 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003081 if (ret) {
3082 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003083 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003084 }
3085 }
3086 /* did not find any suitable type */
3087 return 0;
3088 case LY_TYPE_LEAFREF:
3089 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3090 default:
3091 return 0;
3092 }
3093}
3094
3095/**
3096 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3097 *
3098 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003099 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003100 */
Radek Krejci857189e2020-09-01 13:26:36 +02003101static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003102warn_is_string_type(struct lysc_type *type)
3103{
3104 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003105 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003106 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003107
3108 switch (type->basetype) {
3109 case LY_TYPE_BITS:
3110 case LY_TYPE_ENUM:
3111 case LY_TYPE_IDENT:
3112 case LY_TYPE_INST:
3113 case LY_TYPE_STRING:
3114 return 1;
3115 case LY_TYPE_UNION:
3116 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003117 LY_ARRAY_FOR(uni->types, u) {
3118 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003119 if (ret) {
3120 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003121 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003122 }
3123 }
3124 /* did not find any suitable type */
3125 return 0;
3126 case LY_TYPE_LEAFREF:
3127 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3128 default:
3129 return 0;
3130 }
3131}
3132
3133/**
3134 * @brief Test whether a type is one specific type.
3135 *
3136 * @param[in] type Type to test.
3137 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003138 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003139 */
Radek Krejci857189e2020-09-01 13:26:36 +02003140static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003141warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3142{
3143 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003144 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003145 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003146
3147 if (type->basetype == base) {
3148 return 1;
3149 } else if (type->basetype == LY_TYPE_UNION) {
3150 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003151 LY_ARRAY_FOR(uni->types, u) {
3152 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003153 if (ret) {
3154 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003155 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003156 }
3157 }
3158 /* did not find any suitable type */
3159 return 0;
3160 } else if (type->basetype == LY_TYPE_LEAFREF) {
3161 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3162 }
3163
3164 return 0;
3165}
3166
3167/**
3168 * @brief Get next type of a (union) type.
3169 *
3170 * @param[in] type Base type.
3171 * @param[in] prev_type Previously returned type.
3172 * @return Next type or NULL.
3173 */
3174static struct lysc_type *
3175warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3176{
3177 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003178 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003179 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003180
3181 switch (type->basetype) {
3182 case LY_TYPE_UNION:
3183 uni = (struct lysc_type_union *)type;
3184 if (!prev_type) {
3185 return uni->types[0];
3186 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003187 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003188 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003189 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003190 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003191 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003192 found = 1;
3193 }
3194 }
3195 return NULL;
3196 default:
3197 if (prev_type) {
3198 assert(type == prev_type);
3199 return NULL;
3200 } else {
3201 return type;
3202 }
3203 }
3204}
3205
3206/**
3207 * @brief Test whether 2 types have a common type.
3208 *
3209 * @param[in] type1 First type.
3210 * @param[in] type2 Second type.
3211 * @return 1 if they do, 0 otherwise.
3212 */
3213static int
3214warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3215{
3216 struct lysc_type *t1, *rt1, *t2, *rt2;
3217
3218 t1 = NULL;
3219 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3220 if (t1->basetype == LY_TYPE_LEAFREF) {
3221 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3222 } else {
3223 rt1 = t1;
3224 }
3225
3226 t2 = NULL;
3227 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3228 if (t2->basetype == LY_TYPE_LEAFREF) {
3229 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3230 } else {
3231 rt2 = t2;
3232 }
3233
3234 if (rt2->basetype == rt1->basetype) {
3235 /* match found */
3236 return 1;
3237 }
3238 }
3239 }
3240
3241 return 0;
3242}
3243
3244/**
3245 * @brief Check both operands of comparison operators.
3246 *
3247 * @param[in] ctx Context for errors.
3248 * @param[in] set1 First operand set.
3249 * @param[in] set2 Second operand set.
3250 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3251 * @param[in] expr Start of the expression to print with the warning.
3252 * @param[in] tok_pos Token position.
3253 */
3254static void
Radek Krejci857189e2020-09-01 13:26:36 +02003255warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr, uint16_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003256{
3257 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003258 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003259
3260 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3261 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3262
3263 if (!node1 && !node2) {
3264 /* no node-sets involved, nothing to do */
3265 return;
3266 }
3267
3268 if (node1) {
3269 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3270 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3271 warning = 1;
3272 leaves = 0;
3273 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3274 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3275 warning = 1;
3276 }
3277 }
3278
3279 if (node2) {
3280 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3281 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3282 warning = 1;
3283 leaves = 0;
3284 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3285 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3286 warning = 1;
3287 }
3288 }
3289
3290 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003291 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3292 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3293 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3294 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003295 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3296 warning = 1;
3297 }
3298 }
3299
3300 if (warning) {
3301 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3302 }
3303}
3304
3305/**
3306 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3307 *
3308 * @param[in] exp Parsed XPath expression.
3309 * @param[in] set Set with the leaf/leaf-list.
3310 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3311 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3312 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3313 */
3314static void
Michal Vasko40308e72020-10-20 16:38:40 +02003315warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3316 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003317{
3318 struct lysc_node *scnode;
3319 struct lysc_type *type;
3320 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003321 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003322 LY_ERR rc;
3323 struct ly_err_item *err = NULL;
3324
Michal Vasko69730152020-10-09 16:30:07 +02003325 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3326 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003327 /* check that the node can have the specified value */
3328 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3329 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3330 } else {
3331 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3332 }
3333 if (!value) {
3334 LOGMEM(set->ctx);
3335 return;
3336 }
3337
3338 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3339 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003340 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003341 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003342 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003343 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003344 }
3345
3346 type = ((struct lysc_node_leaf *)scnode)->type;
3347 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003348 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003349 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003350 if (rc == LY_EINCOMPLETE) {
3351 rc = LY_SUCCESS;
3352 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003353
3354 if (err) {
3355 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3356 ly_err_free(err);
3357 } else if (rc != LY_SUCCESS) {
3358 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3359 }
3360 if (rc != LY_SUCCESS) {
3361 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003362 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003363 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003364 } else {
3365 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003366 }
3367 }
3368 free(value);
3369 }
3370}
3371
3372/*
3373 * XPath functions
3374 */
3375
3376/**
3377 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3378 * depending on whether the first node bit value from the second argument is set.
3379 *
3380 * @param[in] args Array of arguments.
3381 * @param[in] arg_count Count of elements in @p args.
3382 * @param[in,out] set Context and result set at the same time.
3383 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003384 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003385 */
3386static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003387xpath_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 +02003388{
3389 struct lyd_node_term *leaf;
3390 struct lysc_node_leaf *sleaf;
3391 struct lysc_type_bits *bits;
3392 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003393 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003394
3395 if (options & LYXP_SCNODE_ALL) {
3396 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3397 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003398 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3399 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 +02003400 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3401 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003402 }
3403
3404 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3405 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3406 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 +02003407 } else if (!warn_is_string_type(sleaf->type)) {
3408 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003409 }
3410 }
3411 set_scnode_clear_ctx(set);
3412 return rc;
3413 }
3414
Michal Vaskod3678892020-05-21 10:06:58 +02003415 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003416 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "bit-is-set(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003417 return LY_EVALID;
3418 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003419 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003420 LY_CHECK_RET(rc);
3421
3422 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003423 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003424 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003425 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3426 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003427 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003428 LY_ARRAY_FOR(bits->bits, u) {
3429 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003430 set_fill_boolean(set, 1);
3431 break;
3432 }
3433 }
3434 }
3435 }
3436
3437 return LY_SUCCESS;
3438}
3439
3440/**
3441 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3442 * with the argument converted to boolean.
3443 *
3444 * @param[in] args Array of arguments.
3445 * @param[in] arg_count Count of elements in @p args.
3446 * @param[in,out] set Context and result set at the same time.
3447 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003448 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003449 */
3450static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003451xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003452{
3453 LY_ERR rc;
3454
3455 if (options & LYXP_SCNODE_ALL) {
3456 set_scnode_clear_ctx(set);
3457 return LY_SUCCESS;
3458 }
3459
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003460 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003461 LY_CHECK_RET(rc);
3462 set_fill_set(set, args[0]);
3463
3464 return LY_SUCCESS;
3465}
3466
3467/**
3468 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3469 * with the first argument rounded up to the nearest integer.
3470 *
3471 * @param[in] args Array of arguments.
3472 * @param[in] arg_count Count of elements in @p args.
3473 * @param[in,out] set Context and result set at the same time.
3474 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003475 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003476 */
3477static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003478xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003479{
3480 struct lysc_node_leaf *sleaf;
3481 LY_ERR rc = LY_SUCCESS;
3482
3483 if (options & LYXP_SCNODE_ALL) {
3484 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3485 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003486 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3487 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 +02003488 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3489 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003490 }
3491 set_scnode_clear_ctx(set);
3492 return rc;
3493 }
3494
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003495 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003496 LY_CHECK_RET(rc);
3497 if ((long long)args[0]->val.num != args[0]->val.num) {
3498 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3499 } else {
3500 set_fill_number(set, args[0]->val.num);
3501 }
3502
3503 return LY_SUCCESS;
3504}
3505
3506/**
3507 * @brief Execute the XPath concat(string, string, string*) function.
3508 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3509 *
3510 * @param[in] args Array of arguments.
3511 * @param[in] arg_count Count of elements in @p args.
3512 * @param[in,out] set Context and result set at the same time.
3513 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003514 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003515 */
3516static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003517xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003518{
3519 uint16_t i;
3520 char *str = NULL;
3521 size_t used = 1;
3522 LY_ERR rc = LY_SUCCESS;
3523 struct lysc_node_leaf *sleaf;
3524
3525 if (options & LYXP_SCNODE_ALL) {
3526 for (i = 0; i < arg_count; ++i) {
3527 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3528 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3529 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003530 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003531 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003532 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 +02003533 }
3534 }
3535 }
3536 set_scnode_clear_ctx(set);
3537 return rc;
3538 }
3539
3540 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003541 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003542 if (rc != LY_SUCCESS) {
3543 free(str);
3544 return rc;
3545 }
3546
3547 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3548 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3549 strcpy(str + used - 1, args[i]->val.str);
3550 used += strlen(args[i]->val.str);
3551 }
3552
3553 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003554 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003555 set->type = LYXP_SET_STRING;
3556 set->val.str = str;
3557
3558 return LY_SUCCESS;
3559}
3560
3561/**
3562 * @brief Execute the XPath contains(string, string) function.
3563 * Returns LYXP_SET_BOOLEAN whether the second argument can
3564 * be found in the first or not.
3565 *
3566 * @param[in] args Array of arguments.
3567 * @param[in] arg_count Count of elements in @p args.
3568 * @param[in,out] set Context and result set at the same time.
3569 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003570 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003571 */
3572static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003573xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003574{
3575 struct lysc_node_leaf *sleaf;
3576 LY_ERR rc = LY_SUCCESS;
3577
3578 if (options & LYXP_SCNODE_ALL) {
3579 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3580 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3581 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 +02003582 } else if (!warn_is_string_type(sleaf->type)) {
3583 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003584 }
3585 }
3586
3587 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3588 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3589 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 +02003590 } else if (!warn_is_string_type(sleaf->type)) {
3591 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003592 }
3593 }
3594 set_scnode_clear_ctx(set);
3595 return rc;
3596 }
3597
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003598 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003599 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003600 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003601 LY_CHECK_RET(rc);
3602
3603 if (strstr(args[0]->val.str, args[1]->val.str)) {
3604 set_fill_boolean(set, 1);
3605 } else {
3606 set_fill_boolean(set, 0);
3607 }
3608
3609 return LY_SUCCESS;
3610}
3611
3612/**
3613 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3614 * with the size of the node-set from the argument.
3615 *
3616 * @param[in] args Array of arguments.
3617 * @param[in] arg_count Count of elements in @p args.
3618 * @param[in,out] set Context and result set at the same time.
3619 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003620 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003621 */
3622static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003623xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003624{
3625 struct lysc_node *scnode = NULL;
3626 LY_ERR rc = LY_SUCCESS;
3627
3628 if (options & LYXP_SCNODE_ALL) {
3629 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3630 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003631 }
3632 set_scnode_clear_ctx(set);
3633 return rc;
3634 }
3635
Michal Vasko03ff5a72019-09-11 13:49:33 +02003636 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003637 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003638 return LY_EVALID;
3639 }
3640
3641 set_fill_number(set, args[0]->used);
3642 return LY_SUCCESS;
3643}
3644
3645/**
3646 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3647 * with the context with the intial node.
3648 *
3649 * @param[in] args Array of arguments.
3650 * @param[in] arg_count Count of elements in @p args.
3651 * @param[in,out] set Context and result set at the same time.
3652 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003653 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003654 */
3655static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003656xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003657{
3658 if (arg_count || args) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003659 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003660 return LY_EVALID;
3661 }
3662
3663 if (options & LYXP_SCNODE_ALL) {
3664 set_scnode_clear_ctx(set);
3665
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003666 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003667 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003668 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003669
3670 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003671 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003672 }
3673
3674 return LY_SUCCESS;
3675}
3676
3677/**
3678 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3679 * leafref or instance-identifier target node(s).
3680 *
3681 * @param[in] args Array of arguments.
3682 * @param[in] arg_count Count of elements in @p args.
3683 * @param[in,out] set Context and result set at the same time.
3684 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003685 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003686 */
3687static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003688xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003689{
3690 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003691 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003692 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003693 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003694 struct ly_path *p;
3695 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003696 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003697 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003698 LY_ERR rc = LY_SUCCESS;
3699
3700 if (options & LYXP_SCNODE_ALL) {
3701 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3702 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003703 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3704 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 +02003705 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3706 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
Michal Vasko69730152020-10-09 16:30:07 +02003707 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003708 }
3709 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003710 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003711 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003712 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003713
3714 /* it was already evaluated on schema, it must succeed */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003715 rc = ly_path_compile(set->ctx, lref->cur_mod, &sleaf->node, lref->path, LY_PATH_LREF_TRUE, oper,
3716 LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, NULL, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003717 assert(!rc);
3718
3719 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003720 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003721 ly_path_free(set->ctx, p);
3722
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003723 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003724 }
3725
Michal Vasko03ff5a72019-09-11 13:49:33 +02003726 return rc;
3727 }
3728
Michal Vaskod3678892020-05-21 10:06:58 +02003729 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003730 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003731 return LY_EVALID;
3732 }
3733
Michal Vaskod3678892020-05-21 10:06:58 +02003734 lyxp_set_free_content(set);
3735 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003736 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3737 sleaf = (struct lysc_node_leaf *)leaf->schema;
3738 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3739 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3740 /* find leafref target */
Michal Vasko9e685082021-01-29 14:49:09 +01003741 if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
3742 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003743 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003744 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003745 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003746 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003747 } else {
3748 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003749 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003750 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Michal Vasko69730152020-10-09 16:30:07 +02003751 LYD_CANON_VALUE(leaf));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003752 return LY_EVALID;
3753 }
3754 }
Michal Vasko004d3152020-06-11 19:59:22 +02003755
3756 /* insert it */
3757 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003758 }
3759 }
3760
3761 return LY_SUCCESS;
3762}
3763
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003764static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003765xpath_derived_(struct lyxp_set **args, struct lyxp_set *set, uint32_t options, ly_bool self_match, const char *func)
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003766{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01003767 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003768 LY_ARRAY_COUNT_TYPE u;
3769 struct lyd_node_term *leaf;
3770 struct lysc_node_leaf *sleaf;
3771 struct lyd_meta *meta;
3772 struct lyd_value data = {0}, *val;
3773 struct ly_err_item *err = NULL;
3774 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003775 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003776
3777 if (options & LYXP_SCNODE_ALL) {
3778 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3779 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
3780 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3781 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003782 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003783 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3784 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3785 }
3786
3787 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3788 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3789 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003790 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003791 } else if (!warn_is_string_type(sleaf->type)) {
3792 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3793 }
3794 }
3795 set_scnode_clear_ctx(set);
3796 return rc;
3797 }
3798
3799 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003800 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "derived-from(-or-self)(node-set, string)");
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003801 return LY_EVALID;
3802 }
3803 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3804 LY_CHECK_RET(rc);
3805
3806 set_fill_boolean(set, 0);
3807 found = 0;
3808 for (i = 0; i < args[0]->used; ++i) {
3809 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3810 continue;
3811 }
3812
3813 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3814 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3815 sleaf = (struct lysc_node_leaf *)leaf->schema;
3816 val = &leaf->value;
3817 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3818 /* uninteresting */
3819 continue;
3820 }
3821
3822 /* store args[1] as ident */
3823 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str),
Michal Vasko405cc9e2020-12-01 12:01:27 +01003824 0, set->format, set->prefix_data, LYD_HINT_DATA, leaf->schema, &data, NULL, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003825 } else {
3826 meta = args[0]->val.meta[i].meta;
3827 val = &meta->value;
3828 if (val->realtype->basetype != LY_TYPE_IDENT) {
3829 /* uninteresting */
3830 continue;
3831 }
3832
3833 /* store args[1] as ident */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003834 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), 0,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003835 set->format, set->prefix_data, LYD_HINT_DATA, meta->parent->schema, &data, NULL, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003836 }
3837
3838 if (err) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01003839 ly_err_print(set->ctx, err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003840 ly_err_free(err);
3841 }
3842 LY_CHECK_RET(rc);
3843
3844 /* finally check the identity itself */
3845 if (self_match && (data.ident == val->ident)) {
3846 set_fill_boolean(set, 1);
3847 found = 1;
3848 }
3849 if (!found) {
3850 LY_ARRAY_FOR(data.ident->derived, u) {
3851 if (data.ident->derived[u] == val->ident) {
3852 set_fill_boolean(set, 1);
3853 found = 1;
3854 break;
3855 }
3856 }
3857 }
3858
3859 /* free temporary value */
3860 val->realtype->plugin->free(set->ctx, &data);
3861 if (found) {
3862 break;
3863 }
3864 }
3865
3866 return LY_SUCCESS;
3867}
3868
Michal Vasko03ff5a72019-09-11 13:49:33 +02003869/**
3870 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3871 * on whether the first argument nodes contain a node of an identity derived from the second
3872 * argument identity.
3873 *
3874 * @param[in] args Array of arguments.
3875 * @param[in] arg_count Count of elements in @p args.
3876 * @param[in,out] set Context and result set at the same time.
3877 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003878 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003879 */
3880static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003881xpath_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 +02003882{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003883 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003884}
3885
3886/**
3887 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3888 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3889 * the second argument identity.
3890 *
3891 * @param[in] args Array of arguments.
3892 * @param[in] arg_count Count of elements in @p args.
3893 * @param[in,out] set Context and result set at the same time.
3894 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003895 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003896 */
3897static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003898xpath_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 +02003899{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003900 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003901}
3902
3903/**
3904 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3905 * with the integer value of the first node's enum value, otherwise NaN.
3906 *
3907 * @param[in] args Array of arguments.
3908 * @param[in] arg_count Count of elements in @p args.
3909 * @param[in,out] set Context and result set at the same time.
3910 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003911 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003912 */
3913static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003914xpath_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 +02003915{
3916 struct lyd_node_term *leaf;
3917 struct lysc_node_leaf *sleaf;
3918 LY_ERR rc = LY_SUCCESS;
3919
3920 if (options & LYXP_SCNODE_ALL) {
3921 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3922 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003923 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3924 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 +02003925 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3926 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003927 }
3928 set_scnode_clear_ctx(set);
3929 return rc;
3930 }
3931
Michal Vaskod3678892020-05-21 10:06:58 +02003932 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003933 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003934 return LY_EVALID;
3935 }
3936
3937 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003938 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003939 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3940 sleaf = (struct lysc_node_leaf *)leaf->schema;
3941 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3942 set_fill_number(set, leaf->value.enum_item->value);
3943 }
3944 }
3945
3946 return LY_SUCCESS;
3947}
3948
3949/**
3950 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3951 * with false value.
3952 *
3953 * @param[in] args Array of arguments.
3954 * @param[in] arg_count Count of elements in @p args.
3955 * @param[in,out] set Context and result set at the same time.
3956 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003957 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003958 */
3959static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003960xpath_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 +02003961{
3962 if (options & LYXP_SCNODE_ALL) {
3963 set_scnode_clear_ctx(set);
3964 return LY_SUCCESS;
3965 }
3966
3967 set_fill_boolean(set, 0);
3968 return LY_SUCCESS;
3969}
3970
3971/**
3972 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3973 * with the first argument floored (truncated).
3974 *
3975 * @param[in] args Array of arguments.
3976 * @param[in] arg_count Count of elements in @p args.
3977 * @param[in,out] set Context and result set at the same time.
3978 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003979 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003980 */
3981static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003982xpath_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 +02003983{
3984 LY_ERR rc;
3985
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003986 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003987 LY_CHECK_RET(rc);
3988 if (isfinite(args[0]->val.num)) {
3989 set_fill_number(set, (long long)args[0]->val.num);
3990 }
3991
3992 return LY_SUCCESS;
3993}
3994
3995/**
3996 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3997 * whether the language of the text matches the one from the argument.
3998 *
3999 * @param[in] args Array of arguments.
4000 * @param[in] arg_count Count of elements in @p args.
4001 * @param[in,out] set Context and result set at the same time.
4002 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004003 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004004 */
4005static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004006xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004007{
4008 const struct lyd_node *node;
4009 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004010 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004011 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004012 LY_ERR rc = LY_SUCCESS;
4013
4014 if (options & LYXP_SCNODE_ALL) {
4015 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4016 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4017 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 +02004018 } else if (!warn_is_string_type(sleaf->type)) {
4019 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004020 }
4021 }
4022 set_scnode_clear_ctx(set);
4023 return rc;
4024 }
4025
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004026 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004027 LY_CHECK_RET(rc);
4028
Michal Vasko03ff5a72019-09-11 13:49:33 +02004029 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004030 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004031 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004032 } else if (!set->used) {
4033 set_fill_boolean(set, 0);
4034 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004035 }
4036
4037 switch (set->val.nodes[0].type) {
4038 case LYXP_NODE_ELEM:
4039 case LYXP_NODE_TEXT:
4040 node = set->val.nodes[0].node;
4041 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004042 case LYXP_NODE_META:
4043 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004044 break;
4045 default:
4046 /* nothing to do with roots */
4047 set_fill_boolean(set, 0);
4048 return LY_SUCCESS;
4049 }
4050
Michal Vasko9f96a052020-03-10 09:41:45 +01004051 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004052 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004053 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004054 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004055 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004056 break;
4057 }
4058 }
4059
Michal Vasko9f96a052020-03-10 09:41:45 +01004060 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004061 break;
4062 }
4063 }
4064
4065 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004066 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004067 set_fill_boolean(set, 0);
4068 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004069 uint64_t i;
4070
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004071 val = meta->value.canonical;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004072 for (i = 0; args[0]->val.str[i]; ++i) {
4073 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4074 set_fill_boolean(set, 0);
4075 break;
4076 }
4077 }
4078 if (!args[0]->val.str[i]) {
4079 if (!val[i] || (val[i] == '-')) {
4080 set_fill_boolean(set, 1);
4081 } else {
4082 set_fill_boolean(set, 0);
4083 }
4084 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004085 }
4086
4087 return LY_SUCCESS;
4088}
4089
4090/**
4091 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4092 * with the context size.
4093 *
4094 * @param[in] args Array of arguments.
4095 * @param[in] arg_count Count of elements in @p args.
4096 * @param[in,out] set Context and result set at the same time.
4097 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004098 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004099 */
4100static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004101xpath_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 +02004102{
4103 if (options & LYXP_SCNODE_ALL) {
4104 set_scnode_clear_ctx(set);
4105 return LY_SUCCESS;
4106 }
4107
Michal Vasko03ff5a72019-09-11 13:49:33 +02004108 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004109 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004110 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004111 } else if (!set->used) {
4112 set_fill_number(set, 0);
4113 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004114 }
4115
4116 set_fill_number(set, set->ctx_size);
4117 return LY_SUCCESS;
4118}
4119
4120/**
4121 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4122 * with the node name without namespace from the argument or the context.
4123 *
4124 * @param[in] args Array of arguments.
4125 * @param[in] arg_count Count of elements in @p args.
4126 * @param[in,out] set Context and result set at the same time.
4127 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004128 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004129 */
4130static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004131xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004132{
4133 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004134
Michal Vasko03ff5a72019-09-11 13:49:33 +02004135 /* suppress unused variable warning */
4136 (void)options;
4137
4138 if (options & LYXP_SCNODE_ALL) {
4139 set_scnode_clear_ctx(set);
4140 return LY_SUCCESS;
4141 }
4142
4143 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004144 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004145 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004146 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004147 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004148 } else if (!args[0]->used) {
4149 set_fill_string(set, "", 0);
4150 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004151 }
4152
4153 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004154 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004155
4156 item = &args[0]->val.nodes[0];
4157 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004158 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004159 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004160 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004161 } else if (!set->used) {
4162 set_fill_string(set, "", 0);
4163 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004164 }
4165
4166 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004167 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004168
4169 item = &set->val.nodes[0];
4170 }
4171
4172 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004173 case LYXP_NODE_NONE:
4174 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004175 case LYXP_NODE_ROOT:
4176 case LYXP_NODE_ROOT_CONFIG:
4177 case LYXP_NODE_TEXT:
4178 set_fill_string(set, "", 0);
4179 break;
4180 case LYXP_NODE_ELEM:
4181 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4182 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004183 case LYXP_NODE_META:
4184 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004185 break;
4186 }
4187
4188 return LY_SUCCESS;
4189}
4190
4191/**
4192 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4193 * with the node name fully qualified (with namespace) from the argument or the context.
4194 *
4195 * @param[in] args Array of arguments.
4196 * @param[in] arg_count Count of elements in @p args.
4197 * @param[in,out] set Context and result set at the same time.
4198 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004199 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004200 */
4201static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004202xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004203{
4204 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004205 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004206 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004207 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004208
4209 if (options & LYXP_SCNODE_ALL) {
4210 set_scnode_clear_ctx(set);
4211 return LY_SUCCESS;
4212 }
4213
4214 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004215 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004216 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004217 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004218 } else if (!args[0]->used) {
4219 set_fill_string(set, "", 0);
4220 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004221 }
4222
4223 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004224 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004225
4226 item = &args[0]->val.nodes[0];
4227 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004228 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004229 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004230 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004231 } else if (!set->used) {
4232 set_fill_string(set, "", 0);
4233 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004234 }
4235
4236 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004237 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004238
4239 item = &set->val.nodes[0];
4240 }
4241
4242 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004243 case LYXP_NODE_NONE:
4244 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004245 case LYXP_NODE_ROOT:
4246 case LYXP_NODE_ROOT_CONFIG:
4247 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004248 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004249 break;
4250 case LYXP_NODE_ELEM:
4251 mod = item->node->schema->module;
4252 name = item->node->schema->name;
4253 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004254 case LYXP_NODE_META:
4255 mod = ((struct lyd_meta *)item->node)->annotation->module;
4256 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004257 break;
4258 }
4259
4260 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004261 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004262 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4263 set_fill_string(set, str, strlen(str));
4264 free(str);
4265 } else {
4266 set_fill_string(set, "", 0);
4267 }
4268
4269 return LY_SUCCESS;
4270}
4271
4272/**
4273 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4274 * with the namespace of the node from the argument or the context.
4275 *
4276 * @param[in] args Array of arguments.
4277 * @param[in] arg_count Count of elements in @p args.
4278 * @param[in,out] set Context and result set at the same time.
4279 * @param[in] options XPath options.
4280 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4281 */
4282static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004283xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004284{
4285 struct lyxp_set_node *item;
4286 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004287
Michal Vasko03ff5a72019-09-11 13:49:33 +02004288 /* suppress unused variable warning */
4289 (void)options;
4290
4291 if (options & LYXP_SCNODE_ALL) {
4292 set_scnode_clear_ctx(set);
4293 return LY_SUCCESS;
4294 }
4295
4296 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004297 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004298 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004299 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004300 return LY_EVALID;
4301 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004302 set_fill_string(set, "", 0);
4303 return LY_SUCCESS;
4304 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004305
4306 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004307 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004308
4309 item = &args[0]->val.nodes[0];
4310 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004311 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004312 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004313 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004314 } else if (!set->used) {
4315 set_fill_string(set, "", 0);
4316 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004317 }
4318
4319 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004320 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004321
4322 item = &set->val.nodes[0];
4323 }
4324
4325 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004326 case LYXP_NODE_NONE:
4327 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004328 case LYXP_NODE_ROOT:
4329 case LYXP_NODE_ROOT_CONFIG:
4330 case LYXP_NODE_TEXT:
4331 set_fill_string(set, "", 0);
4332 break;
4333 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004334 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004335 if (item->type == LYXP_NODE_ELEM) {
4336 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004337 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004338 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004339 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004340 }
4341
4342 set_fill_string(set, mod->ns, strlen(mod->ns));
4343 break;
4344 }
4345
4346 return LY_SUCCESS;
4347}
4348
4349/**
4350 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4351 * with only nodes from the context. In practice it either leaves the context
4352 * as it is or returns an empty node set.
4353 *
4354 * @param[in] args Array of arguments.
4355 * @param[in] arg_count Count of elements in @p args.
4356 * @param[in,out] set Context and result set at the same time.
4357 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004358 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004359 */
4360static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004361xpath_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 +02004362{
4363 if (options & LYXP_SCNODE_ALL) {
4364 set_scnode_clear_ctx(set);
4365 return LY_SUCCESS;
4366 }
4367
4368 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004369 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004370 }
4371 return LY_SUCCESS;
4372}
4373
4374/**
4375 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4376 * with normalized value (no leading, trailing, double white spaces) of the node
4377 * from the argument or the context.
4378 *
4379 * @param[in] args Array of arguments.
4380 * @param[in] arg_count Count of elements in @p args.
4381 * @param[in,out] set Context and result set at the same time.
4382 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004383 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004384 */
4385static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004386xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004387{
4388 uint16_t i, new_used;
4389 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004390 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004391 struct lysc_node_leaf *sleaf;
4392 LY_ERR rc = LY_SUCCESS;
4393
4394 if (options & LYXP_SCNODE_ALL) {
4395 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4396 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4397 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 +02004398 } else if (!warn_is_string_type(sleaf->type)) {
4399 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004400 }
4401 }
4402 set_scnode_clear_ctx(set);
4403 return rc;
4404 }
4405
4406 if (arg_count) {
4407 set_fill_set(set, args[0]);
4408 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004409 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004410 LY_CHECK_RET(rc);
4411
4412 /* is there any normalization necessary? */
4413 for (i = 0; set->val.str[i]; ++i) {
4414 if (is_xmlws(set->val.str[i])) {
4415 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4416 have_spaces = 1;
4417 break;
4418 }
4419 space_before = 1;
4420 } else {
4421 space_before = 0;
4422 }
4423 }
4424
4425 /* yep, there is */
4426 if (have_spaces) {
4427 /* it's enough, at least one character will go, makes space for ending '\0' */
4428 new = malloc(strlen(set->val.str) * sizeof(char));
4429 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4430 new_used = 0;
4431
4432 space_before = 0;
4433 for (i = 0; set->val.str[i]; ++i) {
4434 if (is_xmlws(set->val.str[i])) {
4435 if ((i == 0) || space_before) {
4436 space_before = 1;
4437 continue;
4438 } else {
4439 space_before = 1;
4440 }
4441 } else {
4442 space_before = 0;
4443 }
4444
4445 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4446 ++new_used;
4447 }
4448
4449 /* at worst there is one trailing space now */
4450 if (new_used && is_xmlws(new[new_used - 1])) {
4451 --new_used;
4452 }
4453
4454 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4455 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4456 new[new_used] = '\0';
4457
4458 free(set->val.str);
4459 set->val.str = new;
4460 }
4461
4462 return LY_SUCCESS;
4463}
4464
4465/**
4466 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4467 * with the argument converted to boolean and logically inverted.
4468 *
4469 * @param[in] args Array of arguments.
4470 * @param[in] arg_count Count of elements in @p args.
4471 * @param[in,out] set Context and result set at the same time.
4472 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004473 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004474 */
4475static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004476xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004477{
4478 if (options & LYXP_SCNODE_ALL) {
4479 set_scnode_clear_ctx(set);
4480 return LY_SUCCESS;
4481 }
4482
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004483 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004484 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004485 set_fill_boolean(set, 0);
4486 } else {
4487 set_fill_boolean(set, 1);
4488 }
4489
4490 return LY_SUCCESS;
4491}
4492
4493/**
4494 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4495 * with the number representation of either the argument or the context.
4496 *
4497 * @param[in] args Array of arguments.
4498 * @param[in] arg_count Count of elements in @p args.
4499 * @param[in,out] set Context and result set at the same time.
4500 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004501 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004502 */
4503static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004504xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004505{
4506 LY_ERR rc;
4507
4508 if (options & LYXP_SCNODE_ALL) {
4509 set_scnode_clear_ctx(set);
4510 return LY_SUCCESS;
4511 }
4512
4513 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004514 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004515 LY_CHECK_RET(rc);
4516 set_fill_set(set, args[0]);
4517 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004518 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004519 LY_CHECK_RET(rc);
4520 }
4521
4522 return LY_SUCCESS;
4523}
4524
4525/**
4526 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4527 * with the context position.
4528 *
4529 * @param[in] args Array of arguments.
4530 * @param[in] arg_count Count of elements in @p args.
4531 * @param[in,out] set Context and result set at the same time.
4532 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004533 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004534 */
4535static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004536xpath_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 +02004537{
4538 if (options & LYXP_SCNODE_ALL) {
4539 set_scnode_clear_ctx(set);
4540 return LY_SUCCESS;
4541 }
4542
Michal Vasko03ff5a72019-09-11 13:49:33 +02004543 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004544 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004545 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004546 } else if (!set->used) {
4547 set_fill_number(set, 0);
4548 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004549 }
4550
4551 set_fill_number(set, set->ctx_pos);
4552
4553 /* UNUSED in 'Release' build type */
4554 (void)options;
4555 return LY_SUCCESS;
4556}
4557
4558/**
4559 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4560 * depending on whether the second argument regex matches the first argument string. For details refer to
4561 * YANG 1.1 RFC section 10.2.1.
4562 *
4563 * @param[in] args Array of arguments.
4564 * @param[in] arg_count Count of elements in @p args.
4565 * @param[in,out] set Context and result set at the same time.
4566 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004567 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004568 */
4569static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004570xpath_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 +02004571{
4572 struct lysc_pattern **patterns = NULL, **pattern;
4573 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004574 LY_ERR rc = LY_SUCCESS;
4575 struct ly_err_item *err;
4576
4577 if (options & LYXP_SCNODE_ALL) {
4578 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4579 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4580 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 +02004581 } else if (!warn_is_string_type(sleaf->type)) {
4582 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004583 }
4584 }
4585
4586 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4587 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4588 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 +02004589 } else if (!warn_is_string_type(sleaf->type)) {
4590 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004591 }
4592 }
4593 set_scnode_clear_ctx(set);
4594 return rc;
4595 }
4596
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004597 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004598 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004599 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004600 LY_CHECK_RET(rc);
4601
4602 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek IÅ¡a45802b52021-02-09 09:21:58 +01004603 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004604 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004605 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Radek Krejciddace2c2021-01-08 11:30:56 +01004606 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004607 if (rc != LY_SUCCESS) {
4608 LY_ARRAY_FREE(patterns);
4609 return rc;
4610 }
4611
4612 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4613 pcre2_code_free((*pattern)->code);
4614 free(*pattern);
4615 LY_ARRAY_FREE(patterns);
4616 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004617 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004618 ly_err_free(err);
4619 return rc;
4620 }
4621
4622 if (rc == LY_EVALID) {
4623 ly_err_free(err);
4624 set_fill_boolean(set, 0);
4625 } else {
4626 set_fill_boolean(set, 1);
4627 }
4628
4629 return LY_SUCCESS;
4630}
4631
4632/**
4633 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4634 * with the rounded first argument. For details refer to
4635 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4636 *
4637 * @param[in] args Array of arguments.
4638 * @param[in] arg_count Count of elements in @p args.
4639 * @param[in,out] set Context and result set at the same time.
4640 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004641 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004642 */
4643static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004644xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004645{
4646 struct lysc_node_leaf *sleaf;
4647 LY_ERR rc = LY_SUCCESS;
4648
4649 if (options & LYXP_SCNODE_ALL) {
4650 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4651 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004652 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4653 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 +02004654 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4655 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004656 }
4657 set_scnode_clear_ctx(set);
4658 return rc;
4659 }
4660
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004661 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004662 LY_CHECK_RET(rc);
4663
4664 /* cover only the cases where floor can't be used */
4665 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4666 set_fill_number(set, -0.0f);
4667 } else {
4668 args[0]->val.num += 0.5;
4669 rc = xpath_floor(args, 1, args[0], options);
4670 LY_CHECK_RET(rc);
4671 set_fill_number(set, args[0]->val.num);
4672 }
4673
4674 return LY_SUCCESS;
4675}
4676
4677/**
4678 * @brief Execute the XPath starts-with(string, string) function.
4679 * Returns LYXP_SET_BOOLEAN whether the second argument is
4680 * the prefix of the first or not.
4681 *
4682 * @param[in] args Array of arguments.
4683 * @param[in] arg_count Count of elements in @p args.
4684 * @param[in,out] set Context and result set at the same time.
4685 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004686 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004687 */
4688static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004689xpath_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 +02004690{
4691 struct lysc_node_leaf *sleaf;
4692 LY_ERR rc = LY_SUCCESS;
4693
4694 if (options & LYXP_SCNODE_ALL) {
4695 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4696 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4697 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 +02004698 } else if (!warn_is_string_type(sleaf->type)) {
4699 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004700 }
4701 }
4702
4703 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4704 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4705 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 +02004706 } else if (!warn_is_string_type(sleaf->type)) {
4707 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004708 }
4709 }
4710 set_scnode_clear_ctx(set);
4711 return rc;
4712 }
4713
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004714 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004715 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004716 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004717 LY_CHECK_RET(rc);
4718
4719 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4720 set_fill_boolean(set, 0);
4721 } else {
4722 set_fill_boolean(set, 1);
4723 }
4724
4725 return LY_SUCCESS;
4726}
4727
4728/**
4729 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4730 * with the string representation of 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(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004740{
4741 LY_ERR rc;
4742
4743 if (options & LYXP_SCNODE_ALL) {
4744 set_scnode_clear_ctx(set);
4745 return LY_SUCCESS;
4746 }
4747
4748 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004749 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004750 LY_CHECK_RET(rc);
4751 set_fill_set(set, args[0]);
4752 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004753 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004754 LY_CHECK_RET(rc);
4755 }
4756
4757 return LY_SUCCESS;
4758}
4759
4760/**
4761 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4762 * with the length of the string in either the argument or the context.
4763 *
4764 * @param[in] args Array of arguments.
4765 * @param[in] arg_count Count of elements in @p args.
4766 * @param[in,out] set Context and result set at the same time.
4767 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004768 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004769 */
4770static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004771xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004772{
4773 struct lysc_node_leaf *sleaf;
4774 LY_ERR rc = LY_SUCCESS;
4775
4776 if (options & LYXP_SCNODE_ALL) {
4777 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4778 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4779 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 +02004780 } else if (!warn_is_string_type(sleaf->type)) {
4781 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004782 }
4783 }
4784 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4785 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4786 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 +02004787 } else if (!warn_is_string_type(sleaf->type)) {
4788 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004789 }
4790 }
4791 set_scnode_clear_ctx(set);
4792 return rc;
4793 }
4794
4795 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004796 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004797 LY_CHECK_RET(rc);
4798 set_fill_number(set, strlen(args[0]->val.str));
4799 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004800 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004801 LY_CHECK_RET(rc);
4802 set_fill_number(set, strlen(set->val.str));
4803 }
4804
4805 return LY_SUCCESS;
4806}
4807
4808/**
4809 * @brief Execute the XPath substring(string, number, number?) function.
4810 * Returns LYXP_SET_STRING substring of the first argument starting
4811 * on the second argument index ending on the third argument index,
4812 * indexed from 1. For exact definition refer to
4813 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4814 *
4815 * @param[in] args Array of arguments.
4816 * @param[in] arg_count Count of elements in @p args.
4817 * @param[in,out] set Context and result set at the same time.
4818 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004819 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004820 */
4821static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004822xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004823{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004824 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004825 uint16_t str_start, str_len, pos;
4826 struct lysc_node_leaf *sleaf;
4827 LY_ERR rc = LY_SUCCESS;
4828
4829 if (options & LYXP_SCNODE_ALL) {
4830 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4831 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4832 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 +02004833 } else if (!warn_is_string_type(sleaf->type)) {
4834 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004835 }
4836 }
4837
4838 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4839 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4840 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 +02004841 } else if (!warn_is_numeric_type(sleaf->type)) {
4842 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004843 }
4844 }
4845
Michal Vasko69730152020-10-09 16:30:07 +02004846 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4847 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004848 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4849 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 +02004850 } else if (!warn_is_numeric_type(sleaf->type)) {
4851 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004852 }
4853 }
4854 set_scnode_clear_ctx(set);
4855 return rc;
4856 }
4857
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004858 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004859 LY_CHECK_RET(rc);
4860
4861 /* start */
4862 if (xpath_round(&args[1], 1, args[1], options)) {
4863 return -1;
4864 }
4865 if (isfinite(args[1]->val.num)) {
4866 start = args[1]->val.num - 1;
4867 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004868 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004869 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004870 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004871 }
4872
4873 /* len */
4874 if (arg_count == 3) {
4875 rc = xpath_round(&args[2], 1, args[2], options);
4876 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004877 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004878 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004879 } else if (isfinite(args[2]->val.num)) {
4880 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004881 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004882 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004883 }
4884 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004885 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004886 }
4887
4888 /* find matching character positions */
4889 str_start = 0;
4890 str_len = 0;
4891 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4892 if (pos < start) {
4893 ++str_start;
4894 } else if (pos < start + len) {
4895 ++str_len;
4896 } else {
4897 break;
4898 }
4899 }
4900
4901 set_fill_string(set, args[0]->val.str + str_start, str_len);
4902 return LY_SUCCESS;
4903}
4904
4905/**
4906 * @brief Execute the XPath substring-after(string, string) function.
4907 * Returns LYXP_SET_STRING with the string succeeding the occurance
4908 * of the second argument in the first or an empty string.
4909 *
4910 * @param[in] args Array of arguments.
4911 * @param[in] arg_count Count of elements in @p args.
4912 * @param[in,out] set Context and result set at the same time.
4913 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004914 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004915 */
4916static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004917xpath_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 +02004918{
4919 char *ptr;
4920 struct lysc_node_leaf *sleaf;
4921 LY_ERR rc = LY_SUCCESS;
4922
4923 if (options & LYXP_SCNODE_ALL) {
4924 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4925 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4926 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 +02004927 } else if (!warn_is_string_type(sleaf->type)) {
4928 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004929 }
4930 }
4931
4932 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4933 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4934 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 +02004935 } else if (!warn_is_string_type(sleaf->type)) {
4936 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004937 }
4938 }
4939 set_scnode_clear_ctx(set);
4940 return rc;
4941 }
4942
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004943 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004944 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004945 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004946 LY_CHECK_RET(rc);
4947
4948 ptr = strstr(args[0]->val.str, args[1]->val.str);
4949 if (ptr) {
4950 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4951 } else {
4952 set_fill_string(set, "", 0);
4953 }
4954
4955 return LY_SUCCESS;
4956}
4957
4958/**
4959 * @brief Execute the XPath substring-before(string, string) function.
4960 * Returns LYXP_SET_STRING with the string preceding the occurance
4961 * of the second argument in the first or an empty string.
4962 *
4963 * @param[in] args Array of arguments.
4964 * @param[in] arg_count Count of elements in @p args.
4965 * @param[in,out] set Context and result set at the same time.
4966 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004967 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004968 */
4969static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004970xpath_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 +02004971{
4972 char *ptr;
4973 struct lysc_node_leaf *sleaf;
4974 LY_ERR rc = LY_SUCCESS;
4975
4976 if (options & LYXP_SCNODE_ALL) {
4977 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4978 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4979 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 +02004980 } else if (!warn_is_string_type(sleaf->type)) {
4981 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004982 }
4983 }
4984
4985 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4986 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4987 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 +02004988 } else if (!warn_is_string_type(sleaf->type)) {
4989 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004990 }
4991 }
4992 set_scnode_clear_ctx(set);
4993 return rc;
4994 }
4995
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004996 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004997 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004998 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004999 LY_CHECK_RET(rc);
5000
5001 ptr = strstr(args[0]->val.str, args[1]->val.str);
5002 if (ptr) {
5003 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5004 } else {
5005 set_fill_string(set, "", 0);
5006 }
5007
5008 return LY_SUCCESS;
5009}
5010
5011/**
5012 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5013 * with the sum of all the nodes in the context.
5014 *
5015 * @param[in] args Array of arguments.
5016 * @param[in] arg_count Count of elements in @p args.
5017 * @param[in,out] set Context and result set at the same time.
5018 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005019 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005020 */
5021static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005022xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005023{
5024 long double num;
5025 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005026 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005027 struct lyxp_set set_item;
5028 struct lysc_node_leaf *sleaf;
5029 LY_ERR rc = LY_SUCCESS;
5030
5031 if (options & LYXP_SCNODE_ALL) {
5032 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5033 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005034 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005035 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5036 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5037 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005038 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005039 } else if (!warn_is_numeric_type(sleaf->type)) {
5040 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005041 }
5042 }
5043 }
5044 }
5045 set_scnode_clear_ctx(set);
5046 return rc;
5047 }
5048
5049 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005050
5051 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005052 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005053 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005054 } else if (!args[0]->used) {
5055 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005056 }
5057
Michal Vasko5c4e5892019-11-14 12:31:38 +01005058 set_init(&set_item, set);
5059
Michal Vasko03ff5a72019-09-11 13:49:33 +02005060 set_item.type = LYXP_SET_NODE_SET;
5061 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5062 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5063
5064 set_item.used = 1;
5065 set_item.size = 1;
5066
5067 for (i = 0; i < args[0]->used; ++i) {
5068 set_item.val.nodes[0] = args[0]->val.nodes[i];
5069
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005070 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005071 LY_CHECK_RET(rc);
5072 num = cast_string_to_number(str);
5073 free(str);
5074 set->val.num += num;
5075 }
5076
5077 free(set_item.val.nodes);
5078
5079 return LY_SUCCESS;
5080}
5081
5082/**
5083 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5084 * with the text content of the nodes in the context.
5085 *
5086 * @param[in] args Array of arguments.
5087 * @param[in] arg_count Count of elements in @p args.
5088 * @param[in,out] set Context and result set at the same time.
5089 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005090 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005091 */
5092static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005093xpath_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 +02005094{
5095 uint32_t i;
5096
5097 if (options & LYXP_SCNODE_ALL) {
5098 set_scnode_clear_ctx(set);
5099 return LY_SUCCESS;
5100 }
5101
Michal Vasko03ff5a72019-09-11 13:49:33 +02005102 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005103 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005104 return LY_EVALID;
5105 }
5106
Michal Vaskod989ba02020-08-24 10:59:24 +02005107 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005108 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005109 case LYXP_NODE_NONE:
5110 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005111 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005112 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5113 set->val.nodes[i].type = LYXP_NODE_TEXT;
5114 ++i;
5115 break;
5116 }
Radek Krejci0f969882020-08-21 16:56:47 +02005117 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005118 case LYXP_NODE_ROOT:
5119 case LYXP_NODE_ROOT_CONFIG:
5120 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005121 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005122 set_remove_node(set, i);
5123 break;
5124 }
5125 }
5126
5127 return LY_SUCCESS;
5128}
5129
5130/**
5131 * @brief Execute the XPath translate(string, string, string) function.
5132 * Returns LYXP_SET_STRING with the first argument with the characters
5133 * from the second argument replaced by those on the corresponding
5134 * positions in the third argument.
5135 *
5136 * @param[in] args Array of arguments.
5137 * @param[in] arg_count Count of elements in @p args.
5138 * @param[in,out] set Context and result set at the same time.
5139 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005140 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005141 */
5142static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005143xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005144{
5145 uint16_t i, j, new_used;
5146 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005147 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005148 struct lysc_node_leaf *sleaf;
5149 LY_ERR rc = LY_SUCCESS;
5150
5151 if (options & LYXP_SCNODE_ALL) {
5152 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5153 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5154 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 +02005155 } else if (!warn_is_string_type(sleaf->type)) {
5156 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005157 }
5158 }
5159
5160 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5161 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5162 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 +02005163 } else if (!warn_is_string_type(sleaf->type)) {
5164 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005165 }
5166 }
5167
5168 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5169 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5170 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 +02005171 } else if (!warn_is_string_type(sleaf->type)) {
5172 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005173 }
5174 }
5175 set_scnode_clear_ctx(set);
5176 return rc;
5177 }
5178
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005179 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005180 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005181 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005182 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005183 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005184 LY_CHECK_RET(rc);
5185
5186 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5187 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5188 new_used = 0;
5189
5190 have_removed = 0;
5191 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005192 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005193
5194 for (j = 0; args[1]->val.str[j]; ++j) {
5195 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5196 /* removing this char */
5197 if (j >= strlen(args[2]->val.str)) {
5198 have_removed = 1;
5199 found = 1;
5200 break;
5201 }
5202 /* replacing this char */
5203 new[new_used] = args[2]->val.str[j];
5204 ++new_used;
5205 found = 1;
5206 break;
5207 }
5208 }
5209
5210 /* copying this char */
5211 if (!found) {
5212 new[new_used] = args[0]->val.str[i];
5213 ++new_used;
5214 }
5215 }
5216
5217 if (have_removed) {
5218 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5219 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5220 }
5221 new[new_used] = '\0';
5222
Michal Vaskod3678892020-05-21 10:06:58 +02005223 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005224 set->type = LYXP_SET_STRING;
5225 set->val.str = new;
5226
5227 return LY_SUCCESS;
5228}
5229
5230/**
5231 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5232 * with true value.
5233 *
5234 * @param[in] args Array of arguments.
5235 * @param[in] arg_count Count of elements in @p args.
5236 * @param[in,out] set Context and result set at the same time.
5237 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005238 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005239 */
5240static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005241xpath_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 +02005242{
5243 if (options & LYXP_SCNODE_ALL) {
5244 set_scnode_clear_ctx(set);
5245 return LY_SUCCESS;
5246 }
5247
5248 set_fill_boolean(set, 1);
5249 return LY_SUCCESS;
5250}
5251
5252/*
5253 * moveto functions
5254 *
5255 * They and only they actually change the context (set).
5256 */
5257
5258/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005259 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005260 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005261 * XPath @p set is expected to be a (sc)node set!
5262 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005263 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5264 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005265 * @param[in] set Set with general XPath context.
5266 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005267 * @param[out] moveto_mod Expected module of a matching node.
5268 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005269 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005270static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005271moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5272 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005273{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005274 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005275 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005276 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005277
Michal Vasko2104e9f2020-03-06 08:23:25 +01005278 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5279
Michal Vasko6346ece2019-09-24 13:12:53 +02005280 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5281 /* specific module */
5282 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005283 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005284
Michal Vasko004d3152020-06-11 19:59:22 +02005285 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005286 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005287 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005288 return LY_EVALID;
5289 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005290
Michal Vasko6346ece2019-09-24 13:12:53 +02005291 *qname += pref_len + 1;
5292 *qname_len -= pref_len + 1;
5293 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5294 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005295 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005296 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005297 switch (set->format) {
5298 case LY_PREF_SCHEMA:
5299 case LY_PREF_SCHEMA_RESOLVED:
5300 /* current module */
5301 mod = set->cur_mod;
5302 break;
5303 case LY_PREF_JSON:
5304 /* inherit parent (context node) module */
5305 if (ctx_scnode) {
5306 mod = ctx_scnode->module;
5307 } else {
5308 mod = NULL;
5309 }
5310 break;
5311 case LY_PREF_XML:
5312 /* not defined */
5313 LOGINT_RET(set->ctx);
5314 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005315 }
5316
Michal Vasko6346ece2019-09-24 13:12:53 +02005317 *moveto_mod = mod;
5318 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005319}
5320
5321/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005322 * @brief Move context @p set to the root. Handles absolute path.
5323 * Result is LYXP_SET_NODE_SET.
5324 *
5325 * @param[in,out] set Set to use.
5326 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005327 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005328 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005329static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005330moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005331{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005332 if (!set) {
Michal Vaskob0099a92020-08-31 14:55:23 +02005333 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005334 }
5335
5336 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005337 set_scnode_clear_ctx(set);
Michal Vaskob0099a92020-08-31 14:55:23 +02005338 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005339 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005340 set->type = LYXP_SET_NODE_SET;
5341 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005342 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005343 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005344
5345 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005346}
5347
5348/**
5349 * @brief Check @p node as a part of NameTest processing.
5350 *
5351 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005352 * @param[in] ctx_node Context node.
5353 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005354 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005355 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005356 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005357 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5358 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005359 */
5360static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005361moveto_node_check(const struct lyd_node *node, const struct lyd_node *ctx_node, const struct lyxp_set *set,
Michal Vaskocdad7122020-11-09 21:04:44 +01005362 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005363{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005364 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5365 switch (set->format) {
5366 case LY_PREF_SCHEMA:
5367 case LY_PREF_SCHEMA_RESOLVED:
5368 /* use current module */
5369 moveto_mod = set->cur_mod;
5370 break;
5371 case LY_PREF_JSON:
5372 /* inherit module of the context node, if any */
5373 if (ctx_node) {
5374 moveto_mod = ctx_node->schema->module;
5375 }
5376 break;
5377 case LY_PREF_XML:
5378 /* not defined */
5379 LOGINT(set->ctx);
5380 return LY_EINVAL;
5381 }
5382 }
5383
Michal Vasko03ff5a72019-09-11 13:49:33 +02005384 /* module check */
5385 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005386 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005387 }
5388
Michal Vasko5c4e5892019-11-14 12:31:38 +01005389 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005390 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005391 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005392 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5393 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005394 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005395 }
5396
5397 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005398 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005399 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005400 }
5401
Michal Vaskoa1424542019-11-14 16:08:52 +01005402 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005403 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005404 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005405 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005406
5407 /* match */
5408 return LY_SUCCESS;
5409}
5410
5411/**
5412 * @brief Check @p node as a part of schema NameTest processing.
5413 *
5414 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005415 * @param[in] ctx_scnode Context node.
5416 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005417 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005418 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005419 * @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 +02005420 */
5421static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005422moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set,
Radek Krejci0f969882020-08-21 16:56:47 +02005423 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005424{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005425 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5426 switch (set->format) {
5427 case LY_PREF_SCHEMA:
5428 case LY_PREF_SCHEMA_RESOLVED:
5429 /* use current module */
5430 moveto_mod = set->cur_mod;
5431 break;
5432 case LY_PREF_JSON:
5433 /* inherit module of the context node, if any */
5434 if (ctx_scnode) {
5435 moveto_mod = ctx_scnode->module;
5436 }
5437 break;
5438 case LY_PREF_XML:
5439 /* not defined */
5440 LOGINT(set->ctx);
5441 return LY_EINVAL;
5442 }
5443 }
5444
Michal Vasko03ff5a72019-09-11 13:49:33 +02005445 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005446 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005447 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005448 }
5449
5450 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005451 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005452 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005453 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005454 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005455 }
5456
5457 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005458 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005459 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005460 }
5461
5462 /* match */
5463 return LY_SUCCESS;
5464}
5465
5466/**
Michal Vaskod3678892020-05-21 10:06:58 +02005467 * @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 +02005468 *
5469 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005470 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005471 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005472 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005473 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5474 */
5475static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005476moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005477{
Michal Vaskof03ed032020-03-04 13:31:44 +01005478 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005479 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005480 LY_ERR rc;
5481
Michal Vaskod3678892020-05-21 10:06:58 +02005482 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005483 return LY_SUCCESS;
5484 }
5485
5486 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005487 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005488 return LY_EVALID;
5489 }
5490
Michal Vasko03ff5a72019-09-11 13:49:33 +02005491 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005492 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005493
5494 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 +01005495 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005496
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005497 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005498 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005499 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005500 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005501 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005502 ctx_node = set->val.nodes[i].node;
5503 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005504 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005505
Michal Vaskod3678892020-05-21 10:06:58 +02005506 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005507 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005508 if (rc == LY_SUCCESS) {
5509 if (!replaced) {
5510 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5511 replaced = 1;
5512 } else {
5513 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005514 }
Michal Vaskod3678892020-05-21 10:06:58 +02005515 ++i;
5516 } else if (rc == LY_EINCOMPLETE) {
5517 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005518 }
5519 }
5520
5521 if (!replaced) {
5522 /* no match */
5523 set_remove_node(set, i);
5524 }
5525 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005526
5527 return LY_SUCCESS;
5528}
5529
5530/**
Michal Vaskod3678892020-05-21 10:06:58 +02005531 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5532 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005533 *
5534 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005535 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005536 * @param[in] predicates If @p scnode is ::LYS_LIST or ::LYS_LEAFLIST, the predicates specifying a single instance.
Michal Vaskocdad7122020-11-09 21:04:44 +01005537 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005538 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5539 */
5540static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005541moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5542 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005543{
Michal Vasko004d3152020-06-11 19:59:22 +02005544 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005545 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005546 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005547 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005548
Michal Vasko004d3152020-06-11 19:59:22 +02005549 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005550
5551 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005552 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005553 }
5554
5555 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005556 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005557 ret = LY_EVALID;
5558 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005559 }
5560
5561 /* context check for all the nodes since we have the schema node */
5562 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5563 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005564 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005565 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5566 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005567 lyxp_set_free_content(set);
5568 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005569 }
5570
5571 /* create specific data instance if needed */
5572 if (scnode->nodetype == LYS_LIST) {
5573 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5574 } else if (scnode->nodetype == LYS_LEAFLIST) {
5575 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005576 }
5577
5578 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005579 siblings = NULL;
5580
5581 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5582 assert(!set->val.nodes[i].node);
5583
5584 /* search in all the trees */
5585 siblings = set->tree;
5586 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5587 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005588 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005589 }
5590
5591 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005592 if (inst) {
5593 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005594 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005595 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005596 }
5597
5598 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005599 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005600 ret = LY_EINCOMPLETE;
5601 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005602 }
5603
5604 if (sub) {
5605 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005606 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005607 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005608 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005609 /* no match */
5610 set_remove_node(set, i);
5611 }
5612 }
5613
Michal Vasko004d3152020-06-11 19:59:22 +02005614cleanup:
5615 lyd_free_tree(inst);
5616 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005617}
5618
5619/**
5620 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5621 *
5622 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005623 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005624 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005625 * @param[in] options XPath options.
5626 * @return LY_ERR
5627 */
5628static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005629moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005630{
Radek Krejci857189e2020-09-01 13:26:36 +02005631 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005632 uint32_t getnext_opts;
5633 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005634 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005635 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005636 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005637
Michal Vaskod3678892020-05-21 10:06:58 +02005638 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005639 return LY_SUCCESS;
5640 }
5641
5642 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005643 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005644 return LY_EVALID;
5645 }
5646
Michal Vaskocafad9d2019-11-07 15:20:03 +01005647 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005648 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005649 if (options & LYXP_SCNODE_OUTPUT) {
5650 getnext_opts |= LYS_GETNEXT_OUTPUT;
5651 }
5652
Michal Vasko03ff5a72019-09-11 13:49:33 +02005653 orig_used = set->used;
5654 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005655 uint32_t idx;
5656
Radek Krejcif13b87b2020-12-01 22:02:17 +01005657 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5658 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005659 continue;
5660 }
5661
5662 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005663 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005664 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005665 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005666 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005667
5668 start_parent = set->val.scnodes[i].scnode;
5669
5670 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005671 /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set),
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005672 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005673 mod_idx = 0;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005674 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005675 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005676 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005677 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005678 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005679 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5680
Michal Vasko03ff5a72019-09-11 13:49:33 +02005681 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005682 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005683 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005684 temp_ctx = 1;
5685 }
5686 }
5687 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005688 }
5689
Michal Vasko519fd602020-05-26 12:17:39 +02005690 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5691 iter = NULL;
5692 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005693 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005694 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5695
5696 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005697 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005698 temp_ctx = 1;
5699 }
5700 }
5701 }
5702 }
5703 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005704
5705 /* correct temporary in_ctx values */
5706 if (temp_ctx) {
5707 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005708 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5709 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005710 }
5711 }
5712 }
5713
5714 return LY_SUCCESS;
5715}
5716
5717/**
Michal Vaskod3678892020-05-21 10:06:58 +02005718 * @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 +02005719 * Context position aware.
5720 *
5721 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005722 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005723 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005724 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005725 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5726 */
5727static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005728moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005729{
5730 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005731 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005732 struct lyxp_set ret_set;
5733 LY_ERR rc;
5734
Michal Vaskod3678892020-05-21 10:06:58 +02005735 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005736 return LY_SUCCESS;
5737 }
5738
5739 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005740 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005741 return LY_EVALID;
5742 }
5743
Michal Vasko9f96a052020-03-10 09:41:45 +01005744 /* replace the original nodes (and throws away all text and meta nodes, root is replaced by a child) */
Michal Vaskocdad7122020-11-09 21:04:44 +01005745 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005746 LY_CHECK_RET(rc);
5747
Michal Vasko6346ece2019-09-24 13:12:53 +02005748 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005749 set_init(&ret_set, set);
5750 for (i = 0; i < set->used; ++i) {
5751
5752 /* TREE DFS */
5753 start = set->val.nodes[i].node;
5754 for (elem = next = start; elem; elem = next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005755 rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005756 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005757 /* add matching node into result set */
5758 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5759 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5760 /* the node is a duplicate, we'll process it later in the set */
5761 goto skip_children;
5762 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005763 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005764 return rc;
5765 } else if (rc == LY_EINVAL) {
5766 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005767 }
5768
5769 /* TREE DFS NEXT ELEM */
5770 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005771 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005772 if (!next) {
5773skip_children:
5774 /* no children, so try siblings, but only if it's not the start,
5775 * that is considered to be the root and it's siblings are not traversed */
5776 if (elem != start) {
5777 next = elem->next;
5778 } else {
5779 break;
5780 }
5781 }
5782 while (!next) {
5783 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01005784 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005785 /* we are done, no next element to process */
5786 break;
5787 }
5788 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01005789 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005790 next = elem->next;
5791 }
5792 }
5793 }
5794
5795 /* make the temporary set the current one */
5796 ret_set.ctx_pos = set->ctx_pos;
5797 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005798 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005799 memcpy(set, &ret_set, sizeof *set);
5800
5801 return LY_SUCCESS;
5802}
5803
5804/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005805 * @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 +02005806 *
5807 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005808 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005809 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005810 * @param[in] options XPath options.
5811 * @return LY_ERR
5812 */
5813static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005814moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005815{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005816 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005817 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005818 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005819
Michal Vaskod3678892020-05-21 10:06:58 +02005820 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005821 return LY_SUCCESS;
5822 }
5823
5824 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005825 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005826 return LY_EVALID;
5827 }
5828
Michal Vasko03ff5a72019-09-11 13:49:33 +02005829 orig_used = set->used;
5830 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005831 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5832 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005833 continue;
5834 }
5835
5836 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005837 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005838 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005839 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005840 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005841
5842 /* TREE DFS */
5843 start = set->val.scnodes[i].scnode;
5844 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005845 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5846 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005847 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005848 }
5849
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005850 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005851 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005852 uint32_t idx;
5853
5854 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005855 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005856 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005857 /* we will process it later in the set */
5858 goto skip_children;
5859 }
5860 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005861 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005862 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005863 } else if (rc == LY_EINVAL) {
5864 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005865 }
5866
5867next_iter:
5868 /* TREE DFS NEXT ELEM */
5869 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005870 next = lysc_node_child(elem);
5871 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5872 next = next->next;
5873 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5874 next = next->next;
5875 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005876 if (!next) {
5877skip_children:
5878 /* no children, so try siblings, but only if it's not the start,
5879 * that is considered to be the root and it's siblings are not traversed */
5880 if (elem != start) {
5881 next = elem->next;
5882 } else {
5883 break;
5884 }
5885 }
5886 while (!next) {
5887 /* no siblings, go back through the parents */
5888 if (elem->parent == start) {
5889 /* we are done, no next element to process */
5890 break;
5891 }
5892 /* parent is already processed, go to its sibling */
5893 elem = elem->parent;
5894 next = elem->next;
5895 }
5896 }
5897 }
5898
5899 return LY_SUCCESS;
5900}
5901
5902/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005903 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005904 * Indirectly context position aware.
5905 *
5906 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005907 * @param[in] mod Matching metadata module, NULL for any.
5908 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005909 * @return LY_ERR
5910 */
5911static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005912moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005913{
Michal Vasko9f96a052020-03-10 09:41:45 +01005914 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005915
Michal Vaskod3678892020-05-21 10:06:58 +02005916 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005917 return LY_SUCCESS;
5918 }
5919
5920 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005921 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005922 return LY_EVALID;
5923 }
5924
Radek Krejci1deb5be2020-08-26 16:43:36 +02005925 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005926 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005927
5928 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5929 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005930 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005931 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005932
5933 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005934 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005935 continue;
5936 }
5937
Michal Vaskod3678892020-05-21 10:06:58 +02005938 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005939 /* match */
5940 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005941 set->val.meta[i].meta = sub;
5942 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005943 /* pos does not change */
5944 replaced = 1;
5945 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005946 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 +02005947 }
5948 ++i;
5949 }
5950 }
5951 }
5952
5953 if (!replaced) {
5954 /* no match */
5955 set_remove_node(set, i);
5956 }
5957 }
5958
5959 return LY_SUCCESS;
5960}
5961
5962/**
5963 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005964 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005965 *
5966 * @param[in,out] set1 Set to use for the result.
5967 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005968 * @return LY_ERR
5969 */
5970static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005971moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005972{
5973 LY_ERR rc;
5974
Michal Vaskod3678892020-05-21 10:06:58 +02005975 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005976 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005977 return LY_EVALID;
5978 }
5979
5980 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005981 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005982 return LY_SUCCESS;
5983 }
5984
Michal Vaskod3678892020-05-21 10:06:58 +02005985 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005986 memcpy(set1, set2, sizeof *set1);
5987 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005988 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005989 return LY_SUCCESS;
5990 }
5991
5992 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005993 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005994
5995 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005996 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005997 LY_CHECK_RET(rc);
5998
5999 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006000 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006001
6002 return LY_SUCCESS;
6003}
6004
6005/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006006 * @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 +02006007 * Context position aware.
6008 *
6009 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006010 * @param[in] mod Matching metadata module, NULL for any.
6011 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006012 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006013 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6014 */
6015static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006016moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006017{
Michal Vasko9f96a052020-03-10 09:41:45 +01006018 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006019 struct lyxp_set *set_all_desc = NULL;
6020 LY_ERR rc;
6021
Michal Vaskod3678892020-05-21 10:06:58 +02006022 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006023 return LY_SUCCESS;
6024 }
6025
6026 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006027 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006028 return LY_EVALID;
6029 }
6030
Michal Vasko03ff5a72019-09-11 13:49:33 +02006031 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6032 * but it likely won't be used much, so it's a waste of time */
6033 /* copy the context */
6034 set_all_desc = set_copy(set);
6035 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006036 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006037 if (rc != LY_SUCCESS) {
6038 lyxp_set_free(set_all_desc);
6039 return rc;
6040 }
6041 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006042 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006043 if (rc != LY_SUCCESS) {
6044 lyxp_set_free(set_all_desc);
6045 return rc;
6046 }
6047 lyxp_set_free(set_all_desc);
6048
Radek Krejci1deb5be2020-08-26 16:43:36 +02006049 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006050 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006051
6052 /* only attributes of an elem can be in the result, skip all the rest,
6053 * we have all attributes qualified in lyd tree */
6054 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006055 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006056 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006057 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006058 continue;
6059 }
6060
Michal Vaskod3678892020-05-21 10:06:58 +02006061 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006062 /* match */
6063 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006064 set->val.meta[i].meta = sub;
6065 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006066 /* pos does not change */
6067 replaced = 1;
6068 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006069 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 +02006070 }
6071 ++i;
6072 }
6073 }
6074 }
6075
6076 if (!replaced) {
6077 /* no match */
6078 set_remove_node(set, i);
6079 }
6080 }
6081
6082 return LY_SUCCESS;
6083}
6084
6085/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006086 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6087 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006088 *
6089 * @param[in] parent Current parent.
6090 * @param[in] parent_pos Position of @p parent.
6091 * @param[in] parent_type Node type of @p parent.
6092 * @param[in,out] to_set Set to use.
6093 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006094 * @param[in] options XPath options.
6095 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6096 */
6097static LY_ERR
6098moveto_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 +02006099 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006100{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006101 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006102 LY_ERR rc;
6103
6104 switch (parent_type) {
6105 case LYXP_NODE_ROOT:
6106 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006107 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006108
Michal Vasko61ac2f62020-05-25 12:39:51 +02006109 /* add all top-level nodes as elements */
6110 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006111 break;
6112 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006113 /* add just the text node of this term element node */
6114 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006115 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6116 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6117 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006118 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006119 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006120
6121 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006122 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006123 break;
6124 default:
6125 LOGINT_RET(parent->schema->module->ctx);
6126 }
6127
Michal Vasko61ac2f62020-05-25 12:39:51 +02006128 /* add all top-level nodes as elements */
6129 LY_LIST_FOR(first, iter) {
6130 /* context check */
6131 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6132 continue;
6133 }
6134
6135 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006136 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006137 return LY_EINCOMPLETE;
6138 }
6139
6140 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6141 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6142
6143 /* also add all the children of this node, recursively */
6144 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6145 LY_CHECK_RET(rc);
6146 }
6147 }
6148
Michal Vasko03ff5a72019-09-11 13:49:33 +02006149 return LY_SUCCESS;
6150}
6151
6152/**
6153 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6154 * (or LYXP_SET_EMPTY). Context position aware.
6155 *
6156 * @param[in,out] set Set to use.
6157 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6158 * @param[in] options XPath options.
6159 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6160 */
6161static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006162moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006163{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006164 struct lyxp_set ret_set;
6165 LY_ERR rc;
6166
Michal Vaskod3678892020-05-21 10:06:58 +02006167 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006168 return LY_SUCCESS;
6169 }
6170
6171 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006172 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006173 return LY_EVALID;
6174 }
6175
6176 /* nothing to do */
6177 if (!all_desc) {
6178 return LY_SUCCESS;
6179 }
6180
Michal Vasko03ff5a72019-09-11 13:49:33 +02006181 /* add all the children, they get added recursively */
6182 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006183 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006184 /* copy the current node to tmp */
6185 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6186
6187 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006188 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006189 continue;
6190 }
6191
Michal Vasko03ff5a72019-09-11 13:49:33 +02006192 /* add all the children */
6193 rc = moveto_self_add_children_r(set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, &ret_set,
Michal Vasko69730152020-10-09 16:30:07 +02006194 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006195 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006196 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006197 return rc;
6198 }
6199 }
6200
6201 /* use the temporary set as the current one */
6202 ret_set.ctx_pos = set->ctx_pos;
6203 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006204 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006205 memcpy(set, &ret_set, sizeof *set);
6206
6207 return LY_SUCCESS;
6208}
6209
6210/**
6211 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6212 * (or LYXP_SET_EMPTY).
6213 *
6214 * @param[in,out] set Set to use.
6215 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6216 * @param[in] options XPath options.
6217 * @return LY_ERR
6218 */
6219static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006220moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006221{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006222 uint32_t getnext_opts;
6223 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006224 const struct lysc_node *iter, *start_parent;
6225 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006226
Michal Vaskod3678892020-05-21 10:06:58 +02006227 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006228 return LY_SUCCESS;
6229 }
6230
6231 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006232 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006233 return LY_EVALID;
6234 }
6235
Michal Vasko519fd602020-05-26 12:17:39 +02006236 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006237 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006238 if (options & LYXP_SCNODE_OUTPUT) {
6239 getnext_opts |= LYS_GETNEXT_OUTPUT;
6240 }
6241
6242 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006243 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006244 if (!all_desc) {
6245 /* traverse the start node */
6246 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6247 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6248 }
6249 continue;
6250 }
6251
Radek Krejcif13b87b2020-12-01 22:02:17 +01006252 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6253 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006254 continue;
6255 }
6256
Michal Vasko519fd602020-05-26 12:17:39 +02006257 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006258 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006259 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006260 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006261 }
6262
Michal Vasko519fd602020-05-26 12:17:39 +02006263 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006264
Michal Vasko519fd602020-05-26 12:17:39 +02006265 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6266 /* it can actually be in any module, it's all <running> */
6267 mod_idx = 0;
6268 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6269 iter = NULL;
6270 /* module may not be implemented */
6271 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6272 /* context check */
6273 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6274 continue;
6275 }
6276
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006277 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006278 /* throw away the insert index, we want to consider that node again, recursively */
6279 }
6280 }
6281
6282 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6283 iter = NULL;
6284 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006285 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006286 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006287 continue;
6288 }
6289
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006290 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006291 }
6292 }
6293 }
6294
6295 return LY_SUCCESS;
6296}
6297
6298/**
6299 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6300 * (or LYXP_SET_EMPTY). Context position aware.
6301 *
6302 * @param[in] set Set to use.
6303 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6304 * @param[in] options XPath options.
6305 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6306 */
6307static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006308moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006309{
6310 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006311 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006312 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006313
Michal Vaskod3678892020-05-21 10:06:58 +02006314 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006315 return LY_SUCCESS;
6316 }
6317
6318 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006319 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006320 return LY_EVALID;
6321 }
6322
6323 if (all_desc) {
6324 /* <path>//.. == <path>//./.. */
6325 rc = moveto_self(set, 1, options);
6326 LY_CHECK_RET(rc);
6327 }
6328
Radek Krejci1deb5be2020-08-26 16:43:36 +02006329 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006330 node = set->val.nodes[i].node;
6331
6332 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9e685082021-01-29 14:49:09 +01006333 new_node = lyd_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006334 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6335 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006336 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6337 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006338 if (!new_node) {
6339 LOGINT_RET(set->ctx);
6340 }
6341 } else {
6342 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006343 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006344 continue;
6345 }
6346
Michal Vaskoa1424542019-11-14 16:08:52 +01006347 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006348 if (!(options & LYXP_IGNORE_WHEN) && new_node && lysc_has_when(new_node->schema) && !(new_node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006349 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006350 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006351
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006352 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006353 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006354 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006355
Michal Vasko03ff5a72019-09-11 13:49:33 +02006356 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006357 /* 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 +02006358 new_type = LYXP_NODE_ELEM;
6359 }
6360
Michal Vasko03ff5a72019-09-11 13:49:33 +02006361 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006362 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006363 } else {
6364 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006365 }
6366 }
6367
Michal Vasko2caefc12019-11-14 16:07:56 +01006368 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006369 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006370
6371 return LY_SUCCESS;
6372}
6373
6374/**
6375 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6376 * (or LYXP_SET_EMPTY).
6377 *
6378 * @param[in] set Set to use.
6379 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6380 * @param[in] options XPath options.
6381 * @return LY_ERR
6382 */
6383static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006384moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006385{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006386 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006387 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006388 const struct lysc_node *node, *new_node;
6389 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006390
Michal Vaskod3678892020-05-21 10:06:58 +02006391 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006392 return LY_SUCCESS;
6393 }
6394
6395 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006396 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006397 return LY_EVALID;
6398 }
6399
6400 if (all_desc) {
6401 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006402 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006403 }
6404
Michal Vasko03ff5a72019-09-11 13:49:33 +02006405 orig_used = set->used;
6406 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006407 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6408 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006409 continue;
6410 }
6411
6412 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006413 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006414 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006415 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006416 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006417
6418 node = set->val.scnodes[i].scnode;
6419
6420 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006421 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006422 } else {
6423 /* root does not have a parent */
6424 continue;
6425 }
6426
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006427 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006428 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006429 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006430
Michal Vasko03ff5a72019-09-11 13:49:33 +02006431 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006432 /* 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 +02006433 new_type = LYXP_NODE_ELEM;
6434 }
6435
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006436 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6437 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006438 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006439 temp_ctx = 1;
6440 }
6441 }
6442
6443 if (temp_ctx) {
6444 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006445 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6446 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006447 }
6448 }
6449 }
6450
6451 return LY_SUCCESS;
6452}
6453
6454/**
6455 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6456 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6457 *
6458 * @param[in,out] set1 Set to use for the result.
6459 * @param[in] set2 Set acting as the second operand for @p op.
6460 * @param[in] op Comparison operator to process.
6461 * @param[in] options XPath options.
6462 * @return LY_ERR
6463 */
6464static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006465moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006466{
6467 /*
6468 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6469 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6470 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6471 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6472 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6473 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6474 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6475 *
6476 * '=' or '!='
6477 * BOOLEAN + BOOLEAN
6478 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6479 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6480 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6481 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6482 * NUMBER + NUMBER
6483 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6484 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6485 * STRING + STRING
6486 *
6487 * '<=', '<', '>=', '>'
6488 * NUMBER + NUMBER
6489 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6490 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6491 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6492 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6493 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6494 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6495 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6496 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6497 */
6498 struct lyxp_set iter1, iter2;
6499 int result;
6500 int64_t i;
6501 LY_ERR rc;
6502
Michal Vasko004d3152020-06-11 19:59:22 +02006503 memset(&iter1, 0, sizeof iter1);
6504 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006505
6506 /* iterative evaluation with node-sets */
6507 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6508 if (set1->type == LYXP_SET_NODE_SET) {
6509 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006510 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006511 switch (set2->type) {
6512 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006513 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006514 break;
6515 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006516 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006517 break;
6518 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006519 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006520 break;
6521 }
6522 LY_CHECK_RET(rc);
6523
Michal Vasko4c7763f2020-07-27 17:40:37 +02006524 /* canonize set2 */
6525 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6526
6527 /* compare recursively */
6528 rc = moveto_op_comp(&iter1, &iter2, op, options);
6529 lyxp_set_free_content(&iter2);
6530 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006531
6532 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006533 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006534 set_fill_boolean(set1, 1);
6535 return LY_SUCCESS;
6536 }
6537 }
6538 } else {
6539 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006540 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006541 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006542 case LYXP_SET_NUMBER:
6543 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6544 break;
6545 case LYXP_SET_BOOLEAN:
6546 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6547 break;
6548 default:
6549 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6550 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006551 }
6552 LY_CHECK_RET(rc);
6553
Michal Vasko4c7763f2020-07-27 17:40:37 +02006554 /* canonize set1 */
6555 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 +02006556
Michal Vasko4c7763f2020-07-27 17:40:37 +02006557 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006558 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006559 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006560 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006561
6562 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006563 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006564 set_fill_boolean(set1, 1);
6565 return LY_SUCCESS;
6566 }
6567 }
6568 }
6569
6570 /* false for all nodes */
6571 set_fill_boolean(set1, 0);
6572 return LY_SUCCESS;
6573 }
6574
6575 /* first convert properly */
6576 if ((op[0] == '=') || (op[0] == '!')) {
6577 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006578 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6579 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006580 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006581 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006582 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006583 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006584 LY_CHECK_RET(rc);
6585 } /* else we have 2 strings */
6586 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006587 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006588 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006589 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006590 LY_CHECK_RET(rc);
6591 }
6592
6593 assert(set1->type == set2->type);
6594
6595 /* compute result */
6596 if (op[0] == '=') {
6597 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006598 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006599 } else if (set1->type == LYXP_SET_NUMBER) {
6600 result = (set1->val.num == set2->val.num);
6601 } else {
6602 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006603 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006604 }
6605 } else if (op[0] == '!') {
6606 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006607 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006608 } else if (set1->type == LYXP_SET_NUMBER) {
6609 result = (set1->val.num != set2->val.num);
6610 } else {
6611 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006612 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006613 }
6614 } else {
6615 assert(set1->type == LYXP_SET_NUMBER);
6616 if (op[0] == '<') {
6617 if (op[1] == '=') {
6618 result = (set1->val.num <= set2->val.num);
6619 } else {
6620 result = (set1->val.num < set2->val.num);
6621 }
6622 } else {
6623 if (op[1] == '=') {
6624 result = (set1->val.num >= set2->val.num);
6625 } else {
6626 result = (set1->val.num > set2->val.num);
6627 }
6628 }
6629 }
6630
6631 /* assign result */
6632 if (result) {
6633 set_fill_boolean(set1, 1);
6634 } else {
6635 set_fill_boolean(set1, 0);
6636 }
6637
6638 return LY_SUCCESS;
6639}
6640
6641/**
6642 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6643 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6644 *
6645 * @param[in,out] set1 Set to use for the result.
6646 * @param[in] set2 Set acting as the second operand for @p op.
6647 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006648 * @return LY_ERR
6649 */
6650static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006651moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006652{
6653 LY_ERR rc;
6654
6655 /* unary '-' */
6656 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006657 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006658 LY_CHECK_RET(rc);
6659 set1->val.num *= -1;
6660 lyxp_set_free(set2);
6661 return LY_SUCCESS;
6662 }
6663
6664 assert(set1 && set2);
6665
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006666 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006667 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006668 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006669 LY_CHECK_RET(rc);
6670
6671 switch (op[0]) {
6672 /* '+' */
6673 case '+':
6674 set1->val.num += set2->val.num;
6675 break;
6676
6677 /* '-' */
6678 case '-':
6679 set1->val.num -= set2->val.num;
6680 break;
6681
6682 /* '*' */
6683 case '*':
6684 set1->val.num *= set2->val.num;
6685 break;
6686
6687 /* 'div' */
6688 case 'd':
6689 set1->val.num /= set2->val.num;
6690 break;
6691
6692 /* 'mod' */
6693 case 'm':
6694 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6695 break;
6696
6697 default:
6698 LOGINT_RET(set1->ctx);
6699 }
6700
6701 return LY_SUCCESS;
6702}
6703
6704/*
6705 * eval functions
6706 *
6707 * They execute a parsed XPath expression on some data subtree.
6708 */
6709
6710/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006711 * @brief Evaluate Predicate. Logs directly on error.
6712 *
Michal Vaskod3678892020-05-21 10:06:58 +02006713 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006714 *
6715 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006716 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006717 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6718 * @param[in] options XPath options.
6719 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6720 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6721 */
6722static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006723eval_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options, ly_bool parent_pos_pred)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006724{
6725 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006726 uint16_t orig_exp;
6727 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006728 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006729 struct lyxp_set set2;
6730 struct lyd_node *orig_parent;
6731
6732 /* '[' */
6733 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006734 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006735 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006736
6737 if (!set) {
6738only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006739 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006740 LY_CHECK_RET(rc);
6741 } else if (set->type == LYXP_SET_NODE_SET) {
6742 /* 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 +01006743 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006744
6745 /* empty set, nothing to evaluate */
6746 if (!set->used) {
6747 goto only_parse;
6748 }
6749
Michal Vasko004d3152020-06-11 19:59:22 +02006750 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006751 orig_pos = 0;
6752 orig_size = set->used;
6753 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006754 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006755 set_init(&set2, set);
6756 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6757 /* remember the node context position for position() and context size for last(),
6758 * predicates should always be evaluated with respect to the child axis (since we do
6759 * not support explicit axes) so we assign positions based on their parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006760 if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) {
6761 orig_parent = lyd_parent(set->val.nodes[i].node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006762 orig_pos = 1;
6763 } else {
6764 ++orig_pos;
6765 }
6766
6767 set2.ctx_pos = orig_pos;
6768 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006769 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006770
Michal Vasko004d3152020-06-11 19:59:22 +02006771 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006772 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006773 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006774 return rc;
6775 }
6776
6777 /* number is a position */
6778 if (set2.type == LYXP_SET_NUMBER) {
6779 if ((long long)set2.val.num == orig_pos) {
6780 set2.val.num = 1;
6781 } else {
6782 set2.val.num = 0;
6783 }
6784 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006785 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006786
6787 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006788 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006789 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006790 }
6791 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006792 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006793
6794 } else if (set->type == LYXP_SET_SCNODE_SET) {
6795 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006796 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006797 /* there is a currently-valid node */
6798 break;
6799 }
6800 }
6801 /* empty set, nothing to evaluate */
6802 if (i == set->used) {
6803 goto only_parse;
6804 }
6805
Michal Vasko004d3152020-06-11 19:59:22 +02006806 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006807
Michal Vasko03ff5a72019-09-11 13:49:33 +02006808 /* set special in_ctx to all the valid snodes */
6809 pred_in_ctx = set_scnode_new_in_ctx(set);
6810
6811 /* use the valid snodes one-by-one */
6812 for (i = 0; i < set->used; ++i) {
6813 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6814 continue;
6815 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006816 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006817
Michal Vasko004d3152020-06-11 19:59:22 +02006818 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006819
Michal Vasko004d3152020-06-11 19:59:22 +02006820 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006821 LY_CHECK_RET(rc);
6822
6823 set->val.scnodes[i].in_ctx = pred_in_ctx;
6824 }
6825
6826 /* restore the state as it was before the predicate */
6827 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006828 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
6829 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006830 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006831 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006832 }
6833 }
6834
6835 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006836 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006837 set_fill_set(&set2, set);
6838
Michal Vasko004d3152020-06-11 19:59:22 +02006839 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006840 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006841 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006842 return rc;
6843 }
6844
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006845 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006846 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006847 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006848 }
Michal Vaskod3678892020-05-21 10:06:58 +02006849 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006850 }
6851
6852 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006853 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006854 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006855 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006856 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006857
6858 return LY_SUCCESS;
6859}
6860
6861/**
Michal Vaskod3678892020-05-21 10:06:58 +02006862 * @brief Evaluate Literal. Logs directly on error.
6863 *
6864 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006865 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006866 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6867 */
6868static void
Michal Vasko40308e72020-10-20 16:38:40 +02006869eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006870{
6871 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006872 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006873 set_fill_string(set, "", 0);
6874 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006875 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 +02006876 }
6877 }
6878 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006879 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006880 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006881}
6882
6883/**
Michal Vasko004d3152020-06-11 19:59:22 +02006884 * @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 +02006885 *
Michal Vasko004d3152020-06-11 19:59:22 +02006886 * @param[in] exp Full parsed XPath expression.
6887 * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006888 * @param[in] ctx_node Found schema node as the context for the predicate.
6889 * @param[in] cur_mod Current module for the expression.
6890 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006891 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006892 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006893 * @param[out] predicates Parsed predicates.
6894 * @param[out] pred_type Type of @p predicates.
6895 * @return LY_SUCCESS on success,
6896 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006897 */
6898static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006899eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *ctx_node,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006900 const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_PREFIX_FORMAT format, void *prefix_data,
6901 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006902{
Michal Vasko004d3152020-06-11 19:59:22 +02006903 LY_ERR ret = LY_SUCCESS;
6904 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006905 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006906 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006907 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006908 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006909
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006910 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006911
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006912 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006913 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006914 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006915 return LY_EINVAL;
6916 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006917 for (key_count = 0, key = lysc_node_child(ctx_node); key && (key->flags & LYS_KEY); key = key->next, ++key_count) {}
Michal Vasko004d3152020-06-11 19:59:22 +02006918 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006919
Michal Vasko004d3152020-06-11 19:59:22 +02006920 /* learn where the predicates end */
6921 e_idx = *tok_idx;
6922 while (key_count) {
6923 /* '[' */
6924 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6925 return LY_EINVAL;
6926 }
6927 ++e_idx;
6928
6929 /* ']' */
6930 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6931 ++e_idx;
6932 }
6933 ++e_idx;
6934
6935 /* another presumably key predicate parsed */
6936 --key_count;
6937 }
Michal Vasko004d3152020-06-11 19:59:22 +02006938 } else {
6939 /* learn just where this single predicate ends */
6940 e_idx = *tok_idx;
6941
Michal Vaskod3678892020-05-21 10:06:58 +02006942 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006943 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6944 return LY_EINVAL;
6945 }
6946 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006947
6948 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006949 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6950 ++e_idx;
6951 }
6952 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006953 }
6954
Michal Vasko004d3152020-06-11 19:59:22 +02006955 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6956 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6957
6958 /* turn logging off */
6959 prev_lo = ly_log_options(0);
6960
6961 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006962 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
6963 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02006964
6965 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006966 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
6967 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006968 LY_CHECK_GOTO(ret, cleanup);
6969
6970 /* success, the predicate must include all the needed information for hash-based search */
6971 *tok_idx = e_idx;
6972
6973cleanup:
6974 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006975 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02006976 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006977}
6978
6979/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006980 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
6981 * data node(s) could be found using a single hash-based search.
6982 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01006983 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006984 * @param[in] node Next context node to check.
6985 * @param[in] name Expected node name.
6986 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01006987 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006988 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006989 * @param[in] format Prefix format.
6990 * @param[in,out] found Previously found node, is updated.
6991 * @return LY_SUCCESS on success,
6992 * @return LY_ENOT if the whole check failed and hashes cannot be used.
6993 */
6994static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01006995eval_name_test_with_predicate_get_scnode(const struct ly_ctx *ctx, const struct lyd_node *node, const char *name,
6996 uint16_t name_len, const struct lys_module *moveto_mod, enum lyxp_node_type root_type, LY_PREFIX_FORMAT format,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006997 const struct lysc_node **found)
6998{
6999 const struct lysc_node *scnode;
7000 const struct lys_module *mod;
7001 uint32_t idx = 0;
7002
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007003 assert((format == LY_PREF_JSON) || moveto_mod);
7004
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007005continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007006 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007007 if (!node) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007008 if ((format == LY_PREF_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007009 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007010 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007011 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7012 if (scnode) {
7013 /* we have found a match */
7014 break;
7015 }
7016 }
7017
Michal Vasko7d1d0912020-10-16 08:38:30 +02007018 if (!scnode) {
7019 /* all modules searched */
7020 idx = 0;
7021 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007022 } else {
7023 /* search in top-level */
7024 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7025 }
7026 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007027 if ((format == LY_PREF_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007028 /* we must adjust the module to inherit the one from the context node */
7029 moveto_mod = node->schema->module;
7030 }
7031
7032 /* search in children, do not repeat the same search */
7033 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007034 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007035
7036 /* additional context check */
7037 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7038 scnode = NULL;
7039 }
7040
7041 if (scnode) {
7042 if (*found) {
7043 /* we found a schema node with the same name but at different level, give up, too complicated
7044 * (more hash-based searches would be required, not supported) */
7045 return LY_ENOT;
7046 } else {
7047 /* remember the found schema node and continue to make sure it can be used */
7048 *found = scnode;
7049 }
7050 }
7051
7052 if (idx) {
7053 /* continue searching all the following models */
7054 goto continue_search;
7055 }
7056
7057 return LY_SUCCESS;
7058}
7059
7060/**
Michal Vaskod3678892020-05-21 10:06:58 +02007061 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7062 *
7063 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7064 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7065 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7066 *
7067 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007068 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007069 * @param[in] attr_axis Whether to search attributes or standard nodes.
7070 * @param[in] all_desc Whether to search all the descendants or children only.
7071 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7072 * @param[in] options XPath options.
7073 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7074 */
7075static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007076eval_name_test_with_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool attr_axis, ly_bool all_desc,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007077 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007078{
Michal Vaskod3678892020-05-21 10:06:58 +02007079 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007080 const char *ncname, *ncname_dict = NULL;
7081 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007082 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007083 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007084 struct ly_path_predicate *predicates = NULL;
7085 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007086 LY_ERR rc = LY_SUCCESS;
7087
7088 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007089 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007090 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007091
7092 if (!set) {
7093 goto moveto;
7094 }
7095
Michal Vasko004d3152020-06-11 19:59:22 +02007096 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7097 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007098
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007099 if ((ncname[0] == '*') && (ncname_len == 1)) {
7100 /* all nodes will match */
7101 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007102 }
7103
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007104 /* parse (and skip) module name */
7105 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007106 LY_CHECK_GOTO(rc, cleanup);
7107
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007108 if (((set->format == LY_PREF_JSON) || moveto_mod) && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007109 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007110 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007111 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7112 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007113 /* check failed */
7114 scnode = NULL;
7115 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007116 }
7117 }
7118
Michal Vasko004d3152020-06-11 19:59:22 +02007119 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7120 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007121 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7122 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007123 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007124 scnode = NULL;
7125 }
7126 }
7127 }
7128
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007129 if (!scnode) {
7130 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007131 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007132 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007133 }
7134
7135moveto:
7136 /* move to the attribute(s), data node(s), or schema node(s) */
7137 if (attr_axis) {
7138 if (set && (options & LYXP_SCNODE_ALL)) {
7139 set_scnode_clear_ctx(set);
7140 } else {
7141 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007142 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007143 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007144 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007145 }
7146 LY_CHECK_GOTO(rc, cleanup);
7147 }
7148 } else {
7149 if (set && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007150 int64_t i;
7151
Michal Vaskod3678892020-05-21 10:06:58 +02007152 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007153 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007154 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007155 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007156 }
7157 LY_CHECK_GOTO(rc, cleanup);
7158
7159 for (i = set->used - 1; i > -1; --i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007160 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM) {
Michal Vaskod3678892020-05-21 10:06:58 +02007161 break;
7162 }
7163 }
7164 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007165 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vasko90f12dc2020-12-03 14:20:42 +01007166 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".",
7167 ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007168 free(path);
7169 }
7170 } else {
7171 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007172 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007173 } else {
7174 if (scnode) {
7175 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007176 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007177 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007178 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007179 }
7180 }
7181 LY_CHECK_GOTO(rc, cleanup);
7182 }
7183 }
7184
7185 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007186 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7187 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007188 LY_CHECK_RET(rc);
7189 }
7190
7191cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007192 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007193 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007194 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007195 }
Michal Vaskod3678892020-05-21 10:06:58 +02007196 return rc;
7197}
7198
7199/**
7200 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7201 *
7202 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7203 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7204 * [8] NodeType ::= 'text' | 'node'
7205 *
7206 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007207 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007208 * @param[in] attr_axis Whether to search attributes or standard nodes.
7209 * @param[in] all_desc Whether to search all the descendants or children only.
7210 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7211 * @param[in] options XPath options.
7212 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7213 */
7214static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007215eval_node_type_with_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool attr_axis, ly_bool all_desc,
Radek Krejci1deb5be2020-08-26 16:43:36 +02007216 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007217{
7218 LY_ERR rc;
7219
7220 /* TODO */
7221 (void)attr_axis;
7222 (void)all_desc;
7223
7224 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007225 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007226 if (set->type == LYXP_SET_SCNODE_SET) {
7227 set_scnode_clear_ctx(set);
7228 /* just for the debug message below */
7229 set = NULL;
7230 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007231 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007232 rc = xpath_node(NULL, 0, set, options);
7233 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007234 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007235 rc = xpath_text(NULL, 0, set, options);
7236 }
7237 LY_CHECK_RET(rc);
7238 }
7239 }
7240 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007241 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007242 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007243
7244 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007245 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007246 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007247 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007248 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007249
7250 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007251 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007252 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007253 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007254 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007255
7256 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007257 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7258 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007259 LY_CHECK_RET(rc);
7260 }
7261
7262 return LY_SUCCESS;
7263}
7264
7265/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007266 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7267 *
7268 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7269 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007270 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007271 *
7272 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007273 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007274 * @param[in] all_desc Whether to search all the descendants or children only.
7275 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7276 * @param[in] options XPath options.
7277 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7278 */
7279static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007280eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7281 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007282{
Radek Krejci857189e2020-09-01 13:26:36 +02007283 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007284 LY_ERR rc;
7285
7286 goto step;
7287 do {
7288 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007289 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007290 all_desc = 0;
7291 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007292 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007293 all_desc = 1;
7294 }
7295 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007296 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007297 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007298
7299step:
Michal Vaskod3678892020-05-21 10:06:58 +02007300 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007301 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007302 attr_axis = 1;
7303
7304 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007305 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007306 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007307 } else {
7308 attr_axis = 0;
7309 }
7310
Michal Vasko03ff5a72019-09-11 13:49:33 +02007311 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007312 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007313 case LYXP_TOKEN_DOT:
7314 /* evaluate '.' */
7315 if (set && (options & LYXP_SCNODE_ALL)) {
7316 rc = moveto_scnode_self(set, all_desc, options);
7317 } else {
7318 rc = moveto_self(set, all_desc, options);
7319 }
7320 LY_CHECK_RET(rc);
7321 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007322 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007323 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007324 break;
7325
7326 case LYXP_TOKEN_DDOT:
7327 /* evaluate '..' */
7328 if (set && (options & LYXP_SCNODE_ALL)) {
7329 rc = moveto_scnode_parent(set, all_desc, options);
7330 } else {
7331 rc = moveto_parent(set, all_desc, options);
7332 }
7333 LY_CHECK_RET(rc);
7334 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007335 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007336 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007337 break;
7338
Michal Vasko03ff5a72019-09-11 13:49:33 +02007339 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007340 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007341 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007342 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007343 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007344
Michal Vaskod3678892020-05-21 10:06:58 +02007345 case LYXP_TOKEN_NODETYPE:
7346 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007347 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007348 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007349 break;
7350
7351 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007352 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007353 }
Michal Vasko004d3152020-06-11 19:59:22 +02007354 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007355
7356 return LY_SUCCESS;
7357}
7358
7359/**
7360 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7361 *
7362 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7363 *
7364 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007365 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007366 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7367 * @param[in] options XPath options.
7368 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7369 */
7370static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007371eval_absolute_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007372{
Radek Krejci857189e2020-09-01 13:26:36 +02007373 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007374
7375 if (set) {
7376 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007377 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007378 }
7379
7380 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007381 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007382 /* evaluate '/' - deferred */
7383 all_desc = 0;
7384 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007385 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007386 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007387
Michal Vasko004d3152020-06-11 19:59:22 +02007388 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007389 return LY_SUCCESS;
7390 }
Michal Vasko004d3152020-06-11 19:59:22 +02007391 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007392 case LYXP_TOKEN_DOT:
7393 case LYXP_TOKEN_DDOT:
7394 case LYXP_TOKEN_AT:
7395 case LYXP_TOKEN_NAMETEST:
7396 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007397 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007398 break;
7399 default:
7400 break;
7401 }
7402
Michal Vasko03ff5a72019-09-11 13:49:33 +02007403 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007404 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007405 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7406 all_desc = 1;
7407 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007408 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007409 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007410
Michal Vaskob0099a92020-08-31 14:55:23 +02007411 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007412 }
7413
7414 return LY_SUCCESS;
7415}
7416
7417/**
7418 * @brief Evaluate FunctionCall. Logs directly on error.
7419 *
Michal Vaskod3678892020-05-21 10:06:58 +02007420 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007421 *
7422 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007423 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007424 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7425 * @param[in] options XPath options.
7426 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7427 */
7428static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007429eval_function_call(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007430{
7431 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007432
Radek Krejci1deb5be2020-08-26 16:43:36 +02007433 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007434 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007435 struct lyxp_set **args = NULL, **args_aux;
7436
7437 if (set) {
7438 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007439 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007440 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007441 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007442 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007443 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007444 xpath_func = &xpath_sum;
7445 }
7446 break;
7447 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007448 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007449 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007450 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007451 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007452 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007453 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007454 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007455 xpath_func = &xpath_true;
7456 }
7457 break;
7458 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007459 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007460 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007461 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007462 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007463 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007464 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007465 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007466 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007467 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007468 xpath_func = &xpath_deref;
7469 }
7470 break;
7471 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007472 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007473 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007474 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007475 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007476 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007477 xpath_func = &xpath_string;
7478 }
7479 break;
7480 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007481 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007482 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007483 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007484 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007485 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007486 xpath_func = &xpath_current;
7487 }
7488 break;
7489 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007490 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007491 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007492 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007493 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007494 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007495 xpath_func = &xpath_re_match;
7496 }
7497 break;
7498 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007499 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007500 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007501 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007502 xpath_func = &xpath_translate;
7503 }
7504 break;
7505 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007506 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007507 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007508 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007509 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007510 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007511 xpath_func = &xpath_bit_is_set;
7512 }
7513 break;
7514 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007515 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007516 xpath_func = &xpath_starts_with;
7517 }
7518 break;
7519 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007520 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007521 xpath_func = &xpath_derived_from;
7522 }
7523 break;
7524 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007525 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007526 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007527 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007528 xpath_func = &xpath_string_length;
7529 }
7530 break;
7531 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007532 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007533 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007534 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007535 xpath_func = &xpath_substring_after;
7536 }
7537 break;
7538 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007539 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007540 xpath_func = &xpath_substring_before;
7541 }
7542 break;
7543 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007544 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545 xpath_func = &xpath_derived_from_or_self;
7546 }
7547 break;
7548 }
7549
7550 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007551 LOGVAL(set->ctx, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007552 return LY_EVALID;
7553 }
7554 }
7555
7556 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007557 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007558 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007559
7560 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007561 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007562 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007563 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007564 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007565
7566 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007567 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007568 if (set) {
7569 args = malloc(sizeof *args);
7570 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7571 arg_count = 1;
7572 args[0] = set_copy(set);
7573 if (!args[0]) {
7574 rc = LY_EMEM;
7575 goto cleanup;
7576 }
7577
Michal Vasko004d3152020-06-11 19:59:22 +02007578 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007579 LY_CHECK_GOTO(rc, cleanup);
7580 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007581 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007582 LY_CHECK_GOTO(rc, cleanup);
7583 }
7584 }
Michal Vasko004d3152020-06-11 19:59:22 +02007585 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007586 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007587 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007588 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007589
7590 if (set) {
7591 ++arg_count;
7592 args_aux = realloc(args, arg_count * sizeof *args);
7593 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7594 args = args_aux;
7595 args[arg_count - 1] = set_copy(set);
7596 if (!args[arg_count - 1]) {
7597 rc = LY_EMEM;
7598 goto cleanup;
7599 }
7600
Michal Vasko004d3152020-06-11 19:59:22 +02007601 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007602 LY_CHECK_GOTO(rc, cleanup);
7603 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007604 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007605 LY_CHECK_GOTO(rc, cleanup);
7606 }
7607 }
7608
7609 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007610 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007611 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007612 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007613 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007614
7615 if (set) {
7616 /* evaluate function */
7617 rc = xpath_func(args, arg_count, set, options);
7618
7619 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007620 /* merge all nodes from arg evaluations */
7621 for (i = 0; i < arg_count; ++i) {
7622 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007623 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007624 }
7625 }
7626 } else {
7627 rc = LY_SUCCESS;
7628 }
7629
7630cleanup:
7631 for (i = 0; i < arg_count; ++i) {
7632 lyxp_set_free(args[i]);
7633 }
7634 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007635 return rc;
7636}
7637
7638/**
7639 * @brief Evaluate Number. Logs directly on error.
7640 *
7641 * @param[in] ctx Context for errors.
7642 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007643 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007644 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7645 * @return LY_ERR
7646 */
7647static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007648eval_number(struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007649{
7650 long double num;
7651 char *endptr;
7652
7653 if (set) {
7654 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007655 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007656 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007657 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7658 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007659 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007660 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007661 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007662 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7663 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007664 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007665 return LY_EVALID;
7666 }
7667
7668 set_fill_number(set, num);
7669 }
7670
7671 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007672 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007673 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007674 return LY_SUCCESS;
7675}
7676
7677/**
7678 * @brief Evaluate PathExpr. Logs directly on error.
7679 *
Michal Vaskod3678892020-05-21 10:06:58 +02007680 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007681 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7682 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7683 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007684 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007685 *
7686 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007687 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007688 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7689 * @param[in] options XPath options.
7690 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7691 */
7692static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007693eval_path_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007694{
Radek Krejci857189e2020-09-01 13:26:36 +02007695 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007696 LY_ERR rc;
7697
Michal Vasko004d3152020-06-11 19:59:22 +02007698 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007699 case LYXP_TOKEN_PAR1:
7700 /* '(' Expr ')' */
7701
7702 /* '(' */
7703 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007704 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007705 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007706
7707 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007708 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007709 LY_CHECK_RET(rc);
7710
7711 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007712 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007713 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007714 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007715 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007716
7717 parent_pos_pred = 0;
7718 goto predicate;
7719
7720 case LYXP_TOKEN_DOT:
7721 case LYXP_TOKEN_DDOT:
7722 case LYXP_TOKEN_AT:
7723 case LYXP_TOKEN_NAMETEST:
7724 case LYXP_TOKEN_NODETYPE:
7725 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007726 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007727 LY_CHECK_RET(rc);
7728 break;
7729
7730 case LYXP_TOKEN_FUNCNAME:
7731 /* FunctionCall */
7732 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007733 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007734 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007735 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007736 }
7737 LY_CHECK_RET(rc);
7738
7739 parent_pos_pred = 1;
7740 goto predicate;
7741
Michal Vasko3e48bf32020-06-01 08:39:07 +02007742 case LYXP_TOKEN_OPER_PATH:
7743 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007744 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007745 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007746 LY_CHECK_RET(rc);
7747 break;
7748
7749 case LYXP_TOKEN_LITERAL:
7750 /* Literal */
7751 if (!set || (options & LYXP_SCNODE_ALL)) {
7752 if (set) {
7753 set_scnode_clear_ctx(set);
7754 }
Michal Vasko004d3152020-06-11 19:59:22 +02007755 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007756 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007757 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007758 }
7759
7760 parent_pos_pred = 1;
7761 goto predicate;
7762
7763 case LYXP_TOKEN_NUMBER:
7764 /* Number */
7765 if (!set || (options & LYXP_SCNODE_ALL)) {
7766 if (set) {
7767 set_scnode_clear_ctx(set);
7768 }
Michal Vasko004d3152020-06-11 19:59:22 +02007769 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007770 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007771 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007772 }
7773 LY_CHECK_RET(rc);
7774
7775 parent_pos_pred = 1;
7776 goto predicate;
7777
7778 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01007779 LOGVAL(set->ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007780 return LY_EVALID;
7781 }
7782
7783 return LY_SUCCESS;
7784
7785predicate:
7786 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007787 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7788 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007789 LY_CHECK_RET(rc);
7790 }
7791
7792 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007793 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007794
7795 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007796 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007797 all_desc = 0;
7798 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007799 all_desc = 1;
7800 }
7801
7802 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007803 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007804 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007805
Michal Vasko004d3152020-06-11 19:59:22 +02007806 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007807 LY_CHECK_RET(rc);
7808 }
7809
7810 return LY_SUCCESS;
7811}
7812
7813/**
7814 * @brief Evaluate UnionExpr. Logs directly on error.
7815 *
Michal Vaskod3678892020-05-21 10:06:58 +02007816 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007817 *
7818 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007819 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007820 * @param[in] repeat How many times this expression is repeated.
7821 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7822 * @param[in] options XPath options.
7823 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7824 */
7825static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007826eval_union_expr(const 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 +02007827{
7828 LY_ERR rc = LY_SUCCESS;
7829 struct lyxp_set orig_set, set2;
7830 uint16_t i;
7831
7832 assert(repeat);
7833
7834 set_init(&orig_set, set);
7835 set_init(&set2, set);
7836
7837 set_fill_set(&orig_set, set);
7838
Michal Vasko004d3152020-06-11 19:59:22 +02007839 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007840 LY_CHECK_GOTO(rc, cleanup);
7841
7842 /* ('|' PathExpr)* */
7843 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007844 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007845 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007846 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007847 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007848
7849 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007850 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007851 LY_CHECK_GOTO(rc, cleanup);
7852 continue;
7853 }
7854
7855 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007856 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007857 LY_CHECK_GOTO(rc, cleanup);
7858
7859 /* eval */
7860 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007861 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007862 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007863 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007864 LY_CHECK_GOTO(rc, cleanup);
7865 }
7866 }
7867
7868cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007869 lyxp_set_free_content(&orig_set);
7870 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007871 return rc;
7872}
7873
7874/**
7875 * @brief Evaluate UnaryExpr. Logs directly on error.
7876 *
Michal Vaskod3678892020-05-21 10:06:58 +02007877 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007878 *
7879 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007880 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007881 * @param[in] repeat How many times this expression is repeated.
7882 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7883 * @param[in] options XPath options.
7884 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7885 */
7886static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007887eval_unary_expr(const 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 +02007888{
7889 LY_ERR rc;
7890 uint16_t this_op, i;
7891
7892 assert(repeat);
7893
7894 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007895 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007896 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007897 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 +02007898
7899 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007900 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007901 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007902 }
7903
Michal Vasko004d3152020-06-11 19:59:22 +02007904 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007905 LY_CHECK_RET(rc);
7906
7907 if (set && (repeat % 2)) {
7908 if (options & LYXP_SCNODE_ALL) {
7909 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7910 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007911 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007912 LY_CHECK_RET(rc);
7913 }
7914 }
7915
7916 return LY_SUCCESS;
7917}
7918
7919/**
7920 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7921 *
Michal Vaskod3678892020-05-21 10:06:58 +02007922 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007923 * | MultiplicativeExpr '*' UnaryExpr
7924 * | MultiplicativeExpr 'div' UnaryExpr
7925 * | MultiplicativeExpr 'mod' UnaryExpr
7926 *
7927 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007928 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007929 * @param[in] repeat How many times this expression is repeated.
7930 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7931 * @param[in] options XPath options.
7932 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7933 */
7934static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007935eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
7936 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007937{
7938 LY_ERR rc;
7939 uint16_t this_op;
7940 struct lyxp_set orig_set, set2;
7941 uint16_t i;
7942
7943 assert(repeat);
7944
7945 set_init(&orig_set, set);
7946 set_init(&set2, set);
7947
7948 set_fill_set(&orig_set, set);
7949
Michal Vasko004d3152020-06-11 19:59:22 +02007950 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007951 LY_CHECK_GOTO(rc, cleanup);
7952
7953 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7954 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007955 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007956
Michal Vasko004d3152020-06-11 19:59:22 +02007957 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007958 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007959 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007960 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007961
7962 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007963 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007964 LY_CHECK_GOTO(rc, cleanup);
7965 continue;
7966 }
7967
7968 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007969 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007970 LY_CHECK_GOTO(rc, cleanup);
7971
7972 /* eval */
7973 if (options & LYXP_SCNODE_ALL) {
7974 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007975 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007976 set_scnode_clear_ctx(set);
7977 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007978 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007979 LY_CHECK_GOTO(rc, cleanup);
7980 }
7981 }
7982
7983cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007984 lyxp_set_free_content(&orig_set);
7985 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007986 return rc;
7987}
7988
7989/**
7990 * @brief Evaluate AdditiveExpr. Logs directly on error.
7991 *
Michal Vaskod3678892020-05-21 10:06:58 +02007992 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007993 * | AdditiveExpr '+' MultiplicativeExpr
7994 * | AdditiveExpr '-' MultiplicativeExpr
7995 *
7996 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007997 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007998 * @param[in] repeat How many times this expression is repeated.
7999 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8000 * @param[in] options XPath options.
8001 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8002 */
8003static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008004eval_additive_expr(const 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 +02008005{
8006 LY_ERR rc;
8007 uint16_t this_op;
8008 struct lyxp_set orig_set, set2;
8009 uint16_t i;
8010
8011 assert(repeat);
8012
8013 set_init(&orig_set, set);
8014 set_init(&set2, set);
8015
8016 set_fill_set(&orig_set, set);
8017
Michal Vasko004d3152020-06-11 19:59:22 +02008018 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008019 LY_CHECK_GOTO(rc, cleanup);
8020
8021 /* ('+' / '-' MultiplicativeExpr)* */
8022 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008023 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008024
Michal Vasko004d3152020-06-11 19:59:22 +02008025 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008026 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008027 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008028 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008029
8030 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008031 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008032 LY_CHECK_GOTO(rc, cleanup);
8033 continue;
8034 }
8035
8036 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008037 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008038 LY_CHECK_GOTO(rc, cleanup);
8039
8040 /* eval */
8041 if (options & LYXP_SCNODE_ALL) {
8042 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008043 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008044 set_scnode_clear_ctx(set);
8045 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008046 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008047 LY_CHECK_GOTO(rc, cleanup);
8048 }
8049 }
8050
8051cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008052 lyxp_set_free_content(&orig_set);
8053 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008054 return rc;
8055}
8056
8057/**
8058 * @brief Evaluate RelationalExpr. Logs directly on error.
8059 *
Michal Vaskod3678892020-05-21 10:06:58 +02008060 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008061 * | RelationalExpr '<' AdditiveExpr
8062 * | RelationalExpr '>' AdditiveExpr
8063 * | RelationalExpr '<=' AdditiveExpr
8064 * | RelationalExpr '>=' AdditiveExpr
8065 *
8066 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008067 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008068 * @param[in] repeat How many times this expression is repeated.
8069 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8070 * @param[in] options XPath options.
8071 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8072 */
8073static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008074eval_relational_expr(const 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 +02008075{
8076 LY_ERR rc;
8077 uint16_t this_op;
8078 struct lyxp_set orig_set, set2;
8079 uint16_t i;
8080
8081 assert(repeat);
8082
8083 set_init(&orig_set, set);
8084 set_init(&set2, set);
8085
8086 set_fill_set(&orig_set, set);
8087
Michal Vasko004d3152020-06-11 19:59:22 +02008088 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008089 LY_CHECK_GOTO(rc, cleanup);
8090
8091 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8092 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008093 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008094
Michal Vasko004d3152020-06-11 19:59:22 +02008095 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008096 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008097 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008098 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008099
8100 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008101 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, 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_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008108 LY_CHECK_GOTO(rc, cleanup);
8109
8110 /* eval */
8111 if (options & LYXP_SCNODE_ALL) {
8112 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008113 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008114 set_scnode_clear_ctx(set);
8115 } else {
8116 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8117 LY_CHECK_GOTO(rc, cleanup);
8118 }
8119 }
8120
8121cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008122 lyxp_set_free_content(&orig_set);
8123 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008124 return rc;
8125}
8126
8127/**
8128 * @brief Evaluate EqualityExpr. Logs directly on error.
8129 *
Michal Vaskod3678892020-05-21 10:06:58 +02008130 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008131 * | EqualityExpr '!=' RelationalExpr
8132 *
8133 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008134 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008135 * @param[in] repeat How many times this expression is repeated.
8136 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8137 * @param[in] options XPath options.
8138 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8139 */
8140static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008141eval_equality_expr(const 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 +02008142{
8143 LY_ERR rc;
8144 uint16_t this_op;
8145 struct lyxp_set orig_set, set2;
8146 uint16_t i;
8147
8148 assert(repeat);
8149
8150 set_init(&orig_set, set);
8151 set_init(&set2, set);
8152
8153 set_fill_set(&orig_set, set);
8154
Michal Vasko004d3152020-06-11 19:59:22 +02008155 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008156 LY_CHECK_GOTO(rc, cleanup);
8157
8158 /* ('=' / '!=' RelationalExpr)* */
8159 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008160 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008161
Michal Vasko004d3152020-06-11 19:59:22 +02008162 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008163 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008164 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008165 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008166
8167 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008168 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008169 LY_CHECK_GOTO(rc, cleanup);
8170 continue;
8171 }
8172
8173 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008174 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008175 LY_CHECK_GOTO(rc, cleanup);
8176
8177 /* eval */
8178 if (options & LYXP_SCNODE_ALL) {
8179 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008180 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8181 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008182 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008183 set_scnode_clear_ctx(set);
8184 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008185 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8186 LY_CHECK_GOTO(rc, cleanup);
8187 }
8188 }
8189
8190cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008191 lyxp_set_free_content(&orig_set);
8192 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008193 return rc;
8194}
8195
8196/**
8197 * @brief Evaluate AndExpr. Logs directly on error.
8198 *
Michal Vaskod3678892020-05-21 10:06:58 +02008199 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
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] repeat How many times this expression is repeated.
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
Michal Vasko40308e72020-10-20 16:38:40 +02008209eval_and_expr(const 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 +02008210{
8211 LY_ERR rc;
8212 struct lyxp_set orig_set, set2;
8213 uint16_t i;
8214
8215 assert(repeat);
8216
8217 set_init(&orig_set, set);
8218 set_init(&set2, set);
8219
8220 set_fill_set(&orig_set, set);
8221
Michal Vasko004d3152020-06-11 19:59:22 +02008222 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008223 LY_CHECK_GOTO(rc, cleanup);
8224
8225 /* cast to boolean, we know that will be the final result */
8226 if (set && (options & LYXP_SCNODE_ALL)) {
8227 set_scnode_clear_ctx(set);
8228 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008229 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008230 }
8231
8232 /* ('and' EqualityExpr)* */
8233 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008234 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8235 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008236 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008237 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008238
8239 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008240 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8241 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008242 LY_CHECK_GOTO(rc, cleanup);
8243 continue;
8244 }
8245
8246 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008247 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008248 LY_CHECK_GOTO(rc, cleanup);
8249
8250 /* eval - just get boolean value actually */
8251 if (set->type == LYXP_SET_SCNODE_SET) {
8252 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008253 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008254 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008255 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008256 set_fill_set(set, &set2);
8257 }
8258 }
8259
8260cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008261 lyxp_set_free_content(&orig_set);
8262 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008263 return rc;
8264}
8265
8266/**
8267 * @brief Evaluate OrExpr. Logs directly on error.
8268 *
Michal Vaskod3678892020-05-21 10:06:58 +02008269 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008270 *
8271 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008272 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008273 * @param[in] repeat How many times this expression is repeated.
8274 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8275 * @param[in] options XPath options.
8276 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8277 */
8278static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008279eval_or_expr(const 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 +02008280{
8281 LY_ERR rc;
8282 struct lyxp_set orig_set, set2;
8283 uint16_t i;
8284
8285 assert(repeat);
8286
8287 set_init(&orig_set, set);
8288 set_init(&set2, set);
8289
8290 set_fill_set(&orig_set, set);
8291
Michal Vasko004d3152020-06-11 19:59:22 +02008292 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008293 LY_CHECK_GOTO(rc, cleanup);
8294
8295 /* cast to boolean, we know that will be the final result */
8296 if (set && (options & LYXP_SCNODE_ALL)) {
8297 set_scnode_clear_ctx(set);
8298 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008299 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008300 }
8301
8302 /* ('or' AndExpr)* */
8303 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008304 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8305 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008306 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008307 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008308
8309 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008310 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8311 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008312 LY_CHECK_GOTO(rc, cleanup);
8313 continue;
8314 }
8315
8316 set_fill_set(&set2, &orig_set);
8317 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8318 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008319 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008320 LY_CHECK_GOTO(rc, cleanup);
8321
8322 /* eval - just get boolean value actually */
8323 if (set->type == LYXP_SET_SCNODE_SET) {
8324 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008325 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008326 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008327 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008328 set_fill_set(set, &set2);
8329 }
8330 }
8331
8332cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008333 lyxp_set_free_content(&orig_set);
8334 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008335 return rc;
8336}
8337
8338/**
Michal Vasko004d3152020-06-11 19:59:22 +02008339 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008340 *
8341 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008342 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008343 * @param[in] etype Expression type to evaluate.
8344 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8345 * @param[in] options XPath options.
8346 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8347 */
8348static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008349eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8350 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008351{
8352 uint16_t i, count;
8353 enum lyxp_expr_type next_etype;
8354 LY_ERR rc;
8355
8356 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008357 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008358 next_etype = LYXP_EXPR_NONE;
8359 } else {
8360 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008361 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008362
8363 /* select one-priority lower because etype expression called us */
8364 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008365 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008366 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008367 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008368 } else {
8369 next_etype = LYXP_EXPR_NONE;
8370 }
8371 }
8372
8373 /* decide what expression are we parsing based on the repeat */
8374 switch (next_etype) {
8375 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008376 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008377 break;
8378 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008379 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008380 break;
8381 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008382 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008383 break;
8384 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008385 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008386 break;
8387 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008388 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008389 break;
8390 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008391 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008392 break;
8393 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008394 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008395 break;
8396 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008397 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008398 break;
8399 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008400 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008401 break;
8402 default:
8403 LOGINT_RET(set->ctx);
8404 }
8405
8406 return rc;
8407}
8408
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008409/**
8410 * @brief Get root type.
8411 *
8412 * @param[in] ctx_node Context node.
8413 * @param[in] ctx_scnode Schema context node.
8414 * @param[in] options XPath options.
8415 * @return Root type.
8416 */
8417static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008418lyxp_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 +01008419{
Michal Vasko6b26e742020-07-17 15:02:10 +02008420 const struct lysc_node *op;
8421
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008422 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008423 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008424 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008425
8426 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008427 /* general root that can access everything */
8428 return LYXP_NODE_ROOT;
8429 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8430 /* root context node can access only config data (because we said so, it is unspecified) */
8431 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008432 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008433 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008434 }
8435
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008436 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008437 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008438 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008439
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008440 if (op || !(options & LYXP_SCHEMA)) {
8441 /* general root that can access everything */
8442 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05008443 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008444 /* root context node can access only config data (because we said so, it is unspecified) */
8445 return LYXP_NODE_ROOT_CONFIG;
8446 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008447 return LYXP_NODE_ROOT;
8448}
8449
Michal Vasko03ff5a72019-09-11 13:49:33 +02008450LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008451lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
8452 LY_PREFIX_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node, const struct lyd_node *tree,
8453 struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008454{
Michal Vasko004d3152020-06-11 19:59:22 +02008455 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008456 LY_ERR rc;
8457
Michal Vasko400e9672021-01-11 13:39:17 +01008458 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008459 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8460 LOGARG(NULL, "Current module must be set if schema format is used.");
8461 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008462 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008463
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008464 if (tree) {
8465 /* adjust the pointer to be the first top-level sibling */
8466 while (tree->parent) {
8467 tree = lyd_parent(tree);
8468 }
8469 tree = lyd_first_sibling(tree);
8470 }
8471
Michal Vasko03ff5a72019-09-11 13:49:33 +02008472 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008473 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008474 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008475 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8476 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8477
Michal Vasko400e9672021-01-11 13:39:17 +01008478 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008479 set->cur_node = ctx_node;
8480 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008481 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8482 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008483 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008484 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008485 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008486 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008487
Radek Krejciddace2c2021-01-08 11:30:56 +01008488 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008489
Michal Vasko03ff5a72019-09-11 13:49:33 +02008490 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008491 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008492 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008493 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008494 }
8495
Radek Krejciddace2c2021-01-08 11:30:56 +01008496 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008497 return rc;
8498}
8499
8500#if 0
8501
8502/* full xml printing of set elements, not used currently */
8503
8504void
8505lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8506{
8507 uint32_t i;
8508 char *str_num;
8509 struct lyout out;
8510
8511 memset(&out, 0, sizeof out);
8512
8513 out.type = LYOUT_STREAM;
8514 out.method.f = f;
8515
8516 switch (set->type) {
8517 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008518 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008519 break;
8520 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008521 ly_print_(&out, "Boolean XPath set:\n");
8522 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008523 break;
8524 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008525 ly_print_(&out, "String XPath set:\n");
8526 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008527 break;
8528 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008529 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008530
8531 if (isnan(set->value.num)) {
8532 str_num = strdup("NaN");
8533 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8534 str_num = strdup("0");
8535 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8536 str_num = strdup("Infinity");
8537 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8538 str_num = strdup("-Infinity");
8539 } else if ((long long)set->value.num == set->value.num) {
8540 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8541 str_num = NULL;
8542 }
8543 } else {
8544 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8545 str_num = NULL;
8546 }
8547 }
8548 if (!str_num) {
8549 LOGMEM;
8550 return;
8551 }
Michal Vasko5233e962020-08-14 14:26:20 +02008552 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008553 free(str_num);
8554 break;
8555 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008556 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008557
8558 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008559 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008560 switch (set->node_type[i]) {
8561 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008562 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008563 break;
8564 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008565 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008566 break;
8567 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008568 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008569 break;
8570 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008571 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008572 break;
8573 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008574 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008575 break;
8576 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008577 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008578 break;
8579 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008580 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008581 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008582 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008583 break;
8584 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008585 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 +02008586 break;
8587 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008588 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 +02008589 break;
8590 }
8591 }
8592 break;
8593 }
8594}
8595
8596#endif
8597
8598LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008599lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008600{
8601 long double num;
8602 char *str;
8603 LY_ERR rc;
8604
8605 if (!set || (set->type == target)) {
8606 return LY_SUCCESS;
8607 }
8608
8609 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008610 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008611
8612 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008613 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008614 return LY_EINVAL;
8615 }
8616
8617 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008618 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008619 switch (set->type) {
8620 case LYXP_SET_NUMBER:
8621 if (isnan(set->val.num)) {
8622 set->val.str = strdup("NaN");
8623 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8624 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8625 set->val.str = strdup("0");
8626 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8627 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8628 set->val.str = strdup("Infinity");
8629 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8630 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8631 set->val.str = strdup("-Infinity");
8632 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8633 } else if ((long long)set->val.num == set->val.num) {
8634 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8635 LOGMEM_RET(set->ctx);
8636 }
8637 set->val.str = str;
8638 } else {
8639 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8640 LOGMEM_RET(set->ctx);
8641 }
8642 set->val.str = str;
8643 }
8644 break;
8645 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008646 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008647 set->val.str = strdup("true");
8648 } else {
8649 set->val.str = strdup("false");
8650 }
8651 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8652 break;
8653 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008654 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008655 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008656
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008657 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008658 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008659 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008660 set->val.str = str;
8661 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008662 default:
8663 LOGINT_RET(set->ctx);
8664 }
8665 set->type = LYXP_SET_STRING;
8666 }
8667
8668 /* to NUMBER */
8669 if (target == LYXP_SET_NUMBER) {
8670 switch (set->type) {
8671 case LYXP_SET_STRING:
8672 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008673 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008674 set->val.num = num;
8675 break;
8676 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008677 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008678 set->val.num = 1;
8679 } else {
8680 set->val.num = 0;
8681 }
8682 break;
8683 default:
8684 LOGINT_RET(set->ctx);
8685 }
8686 set->type = LYXP_SET_NUMBER;
8687 }
8688
8689 /* to BOOLEAN */
8690 if (target == LYXP_SET_BOOLEAN) {
8691 switch (set->type) {
8692 case LYXP_SET_NUMBER:
8693 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008694 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008695 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008696 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008697 }
8698 break;
8699 case LYXP_SET_STRING:
8700 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008701 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008702 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008703 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008704 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008705 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008706 }
8707 break;
8708 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008709 if (set->used) {
8710 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008711 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008712 } else {
8713 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008714 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008715 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008716 break;
8717 default:
8718 LOGINT_RET(set->ctx);
8719 }
8720 set->type = LYXP_SET_BOOLEAN;
8721 }
8722
Michal Vasko03ff5a72019-09-11 13:49:33 +02008723 return LY_SUCCESS;
8724}
8725
8726LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008727lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
8728 LY_PREFIX_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
8729 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008730{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008731 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008732 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008733
Michal Vasko400e9672021-01-11 13:39:17 +01008734 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008735 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8736 LOGARG(NULL, "Current module must be set if schema format is used.");
8737 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008738 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008739
8740 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008741 memset(set, 0, sizeof *set);
8742 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008743 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8744 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode ? LYXP_NODE_ELEM : set->root_type, NULL));
Radek Krejcif13b87b2020-12-01 22:02:17 +01008745 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008746
Michal Vasko400e9672021-01-11 13:39:17 +01008747 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008748 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008749 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008750 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8751 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008752 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008753 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008754 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008755
Radek Krejciddace2c2021-01-08 11:30:56 +01008756 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008757
Michal Vasko03ff5a72019-09-11 13:49:33 +02008758 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01008759 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
8760
Radek Krejciddace2c2021-01-08 11:30:56 +01008761 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008762 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008763}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008764
8765API const char *
8766lyxp_get_expr(const struct lyxp_expr *path)
8767{
8768 if (!path) {
8769 return NULL;
8770 }
8771
8772 return path->expr;
8773}