blob: 9fe638f6bc819dd44500aec7fb4c3345d34b5913 [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 Vasko03ff5a72019-09-11 13:49:33 +0200505 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100506 case LYXP_NODE_NONE:
507 /* invalid */
508 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200509 case LYXP_NODE_ROOT:
510 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100511 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200512 case LYXP_NODE_ELEM:
513 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100514 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100515 case LYXP_NODE_META:
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200516 *str = strdup(set->val.meta[0].meta->value.canonical);
517 if (!*str) {
518 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200519 }
520 return LY_SUCCESS;
521 }
522
523 LOGINT_RET(set->ctx);
524}
525
526/**
527 * @brief Cast a string into an XPath number.
528 *
529 * @param[in] str String to use.
530 * @return Cast number.
531 */
532static long double
533cast_string_to_number(const char *str)
534{
535 long double num;
536 char *ptr;
537
538 errno = 0;
539 num = strtold(str, &ptr);
540 if (errno || *ptr) {
541 num = NAN;
542 }
543 return num;
544}
545
546/**
547 * @brief Callback for checking value equality.
548 *
Michal Vasko62524a92021-02-26 10:08:50 +0100549 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200550 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200551 * @param[in] val1_p First value.
552 * @param[in] val2_p Second value.
553 * @param[in] mod Whether hash table is being modified.
554 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200555 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200556 */
Radek Krejci857189e2020-09-01 13:26:36 +0200557static ly_bool
558set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200559{
560 struct lyxp_set_hash_node *val1, *val2;
561
562 val1 = (struct lyxp_set_hash_node *)val1_p;
563 val2 = (struct lyxp_set_hash_node *)val2_p;
564
565 if ((val1->node == val2->node) && (val1->type == val2->type)) {
566 return 1;
567 }
568
569 return 0;
570}
571
572/**
573 * @brief Insert node and its hash into set.
574 *
575 * @param[in] set et to insert to.
576 * @param[in] node Node with hash.
577 * @param[in] type Node type.
578 */
579static void
580set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
581{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200582 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200583 uint32_t i, hash;
584 struct lyxp_set_hash_node hnode;
585
586 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
587 /* create hash table and add all the nodes */
588 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
589 for (i = 0; i < set->used; ++i) {
590 hnode.node = set->val.nodes[i].node;
591 hnode.type = set->val.nodes[i].type;
592
593 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
594 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
595 hash = dict_hash_multi(hash, NULL, 0);
596
597 r = lyht_insert(set->ht, &hnode, hash, NULL);
598 assert(!r);
599 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200600
Michal Vasko9d6befd2019-12-11 14:56:56 +0100601 if (hnode.node == node) {
602 /* it was just added, do not add it twice */
603 node = NULL;
604 }
605 }
606 }
607
608 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200609 /* add the new node into hash table */
610 hnode.node = node;
611 hnode.type = type;
612
613 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
614 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
615 hash = dict_hash_multi(hash, NULL, 0);
616
617 r = lyht_insert(set->ht, &hnode, hash, NULL);
618 assert(!r);
619 (void)r;
620 }
621}
622
623/**
624 * @brief Remove node and its hash from set.
625 *
626 * @param[in] set Set to remove from.
627 * @param[in] node Node to remove.
628 * @param[in] type Node type.
629 */
630static void
631set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
632{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200633 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200634 struct lyxp_set_hash_node hnode;
635 uint32_t hash;
636
637 if (set->ht) {
638 hnode.node = node;
639 hnode.type = type;
640
641 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
642 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
643 hash = dict_hash_multi(hash, NULL, 0);
644
645 r = lyht_remove(set->ht, &hnode, hash);
646 assert(!r);
647 (void)r;
648
649 if (!set->ht->used) {
650 lyht_free(set->ht);
651 set->ht = NULL;
652 }
653 }
654}
655
656/**
657 * @brief Check whether node is in set based on its hash.
658 *
659 * @param[in] set Set to search in.
660 * @param[in] node Node to search for.
661 * @param[in] type Node type.
662 * @param[in] skip_idx Index in @p set to skip.
663 * @return LY_ERR
664 */
665static LY_ERR
666set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
667{
668 struct lyxp_set_hash_node hnode, *match_p;
669 uint32_t hash;
670
671 hnode.node = node;
672 hnode.type = type;
673
674 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
675 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
676 hash = dict_hash_multi(hash, NULL, 0);
677
678 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
679 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
680 /* we found it on the index that should be skipped, find another */
681 hnode = *match_p;
682 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
683 /* none other found */
684 return LY_SUCCESS;
685 }
686 }
687
688 return LY_EEXIST;
689 }
690
691 /* not found */
692 return LY_SUCCESS;
693}
694
Michal Vaskod3678892020-05-21 10:06:58 +0200695void
696lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200697{
698 if (!set) {
699 return;
700 }
701
702 if (set->type == LYXP_SET_NODE_SET) {
703 free(set->val.nodes);
704 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200705 } else if (set->type == LYXP_SET_SCNODE_SET) {
706 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200707 lyht_free(set->ht);
708 } else {
709 if (set->type == LYXP_SET_STRING) {
710 free(set->val.str);
711 }
712 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200713 }
Michal Vaskod3678892020-05-21 10:06:58 +0200714
715 set->val.nodes = NULL;
716 set->used = 0;
717 set->size = 0;
718 set->ht = NULL;
719 set->ctx_pos = 0;
720 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200721}
722
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100723/**
724 * @brief Free dynamically-allocated set.
725 *
726 * @param[in] set Set to free.
727 */
728static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200729lyxp_set_free(struct lyxp_set *set)
730{
731 if (!set) {
732 return;
733 }
734
Michal Vaskod3678892020-05-21 10:06:58 +0200735 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200736 free(set);
737}
738
739/**
740 * @brief Initialize set context.
741 *
742 * @param[in] new Set to initialize.
743 * @param[in] set Arbitrary initialized set.
744 */
745static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200746set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200747{
748 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200749 if (set) {
750 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200751 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100752 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200753 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100754 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200755 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200756 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200757 new->prefix_data = set->prefix_data;
Michal Vasko02a77382019-09-12 11:47:35 +0200758 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200759}
760
761/**
762 * @brief Create a deep copy of a set.
763 *
764 * @param[in] set Set to copy.
765 * @return Copy of @p set.
766 */
767static struct lyxp_set *
768set_copy(struct lyxp_set *set)
769{
770 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100771 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200772
773 if (!set) {
774 return NULL;
775 }
776
777 ret = malloc(sizeof *ret);
778 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
779 set_init(ret, set);
780
781 if (set->type == LYXP_SET_SCNODE_SET) {
782 ret->type = set->type;
783
784 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100785 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
786 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200787 uint32_t idx;
788 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
789 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100790 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200791 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200792 lyxp_set_free(ret);
793 return NULL;
794 }
Michal Vaskoba716542019-12-16 10:01:58 +0100795 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200796 }
797 }
798 } else if (set->type == LYXP_SET_NODE_SET) {
799 ret->type = set->type;
800 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
801 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
802 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
803
804 ret->used = ret->size = set->used;
805 ret->ctx_pos = set->ctx_pos;
806 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100807 if (set->ht) {
808 ret->ht = lyht_dup(set->ht);
809 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200810 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200811 memcpy(ret, set, sizeof *ret);
812 if (set->type == LYXP_SET_STRING) {
813 ret->val.str = strdup(set->val.str);
814 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
815 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200816 }
817
818 return ret;
819}
820
821/**
822 * @brief Fill XPath set with a string. Any current data are disposed of.
823 *
824 * @param[in] set Set to fill.
825 * @param[in] string String to fill into \p set.
826 * @param[in] str_len Length of \p string. 0 is a valid value!
827 */
828static void
829set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
830{
Michal Vaskod3678892020-05-21 10:06:58 +0200831 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200832
833 set->type = LYXP_SET_STRING;
834 if ((str_len == 0) && (string[0] != '\0')) {
835 string = "";
836 }
837 set->val.str = strndup(string, str_len);
838}
839
840/**
841 * @brief Fill XPath set with a number. Any current data are disposed of.
842 *
843 * @param[in] set Set to fill.
844 * @param[in] number Number to fill into \p set.
845 */
846static void
847set_fill_number(struct lyxp_set *set, long double number)
848{
Michal Vaskod3678892020-05-21 10:06:58 +0200849 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200850
851 set->type = LYXP_SET_NUMBER;
852 set->val.num = number;
853}
854
855/**
856 * @brief Fill XPath set with a boolean. Any current data are disposed of.
857 *
858 * @param[in] set Set to fill.
859 * @param[in] boolean Boolean to fill into \p set.
860 */
861static void
Radek Krejci857189e2020-09-01 13:26:36 +0200862set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200863{
Michal Vaskod3678892020-05-21 10:06:58 +0200864 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200865
866 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200867 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200868}
869
870/**
871 * @brief Fill XPath set with the value from another set (deep assign).
872 * Any current data are disposed of.
873 *
874 * @param[in] trg Set to fill.
875 * @param[in] src Source set to copy into \p trg.
876 */
877static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200878set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200879{
880 if (!trg || !src) {
881 return;
882 }
883
884 if (trg->type == LYXP_SET_NODE_SET) {
885 free(trg->val.nodes);
886 } else if (trg->type == LYXP_SET_STRING) {
887 free(trg->val.str);
888 }
889 set_init(trg, src);
890
891 if (src->type == LYXP_SET_SCNODE_SET) {
892 trg->type = LYXP_SET_SCNODE_SET;
893 trg->used = src->used;
894 trg->size = src->used;
895
896 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
897 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
898 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
899 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200900 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200901 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200902 set_fill_number(trg, src->val.num);
903 } else if (src->type == LYXP_SET_STRING) {
904 set_fill_string(trg, src->val.str, strlen(src->val.str));
905 } else {
906 if (trg->type == LYXP_SET_NODE_SET) {
907 free(trg->val.nodes);
908 } else if (trg->type == LYXP_SET_STRING) {
909 free(trg->val.str);
910 }
911
Michal Vaskod3678892020-05-21 10:06:58 +0200912 assert(src->type == LYXP_SET_NODE_SET);
913
914 trg->type = LYXP_SET_NODE_SET;
915 trg->used = src->used;
916 trg->size = src->used;
917 trg->ctx_pos = src->ctx_pos;
918 trg->ctx_size = src->ctx_size;
919
920 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
921 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
922 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
923 if (src->ht) {
924 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200925 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200926 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200927 }
928 }
929}
930
931/**
932 * @brief Clear context of all schema nodes.
933 *
934 * @param[in] set Set to clear.
935 */
936static void
937set_scnode_clear_ctx(struct lyxp_set *set)
938{
939 uint32_t i;
940
941 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100942 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
943 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
944 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
945 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200946 }
947 }
948}
949
950/**
951 * @brief Remove a node from a set. Removing last node changes
952 * set into LYXP_SET_EMPTY. Context position aware.
953 *
954 * @param[in] set Set to use.
955 * @param[in] idx Index from @p set of the node to be removed.
956 */
957static void
958set_remove_node(struct lyxp_set *set, uint32_t idx)
959{
960 assert(set && (set->type == LYXP_SET_NODE_SET));
961 assert(idx < set->used);
962
963 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
964
965 --set->used;
966 if (set->used) {
967 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
968 (set->used - idx) * sizeof *set->val.nodes);
969 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200970 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200971 }
972}
973
974/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100975 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200976 *
977 * @param[in] set Set to use.
978 * @param[in] idx Index from @p set of the node to be removed.
979 */
980static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100981set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200982{
983 assert(set && (set->type == LYXP_SET_NODE_SET));
984 assert(idx < set->used);
985
Michal Vasko2caefc12019-11-14 16:07:56 +0100986 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
987 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
988 }
989 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +0200990}
991
992/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100993 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +0200994 * set into LYXP_SET_EMPTY. Context position aware.
995 *
996 * @param[in] set Set to consolidate.
997 */
998static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100999set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001000{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001001 uint32_t i, orig_used, end = 0;
1002 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001003
Michal Vaskod3678892020-05-21 10:06:58 +02001004 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001005
1006 orig_used = set->used;
1007 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001008 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001009 start = -1;
1010 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001011 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001012 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001013 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001014 end = i;
1015 ++i;
1016 break;
1017 }
1018
1019 ++i;
1020 if (i == orig_used) {
1021 end = i;
1022 }
1023 } while (i < orig_used);
1024
1025 if (start > -1) {
1026 /* move the whole chunk of valid nodes together */
1027 if (set->used != (unsigned)start) {
1028 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1029 }
1030 set->used += end - start;
1031 }
1032 }
Michal Vasko57eab132019-09-24 11:46:26 +02001033}
1034
1035/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001036 * @brief Check for duplicates in a node set.
1037 *
1038 * @param[in] set Set to check.
1039 * @param[in] node Node to look for in @p set.
1040 * @param[in] node_type Type of @p node.
1041 * @param[in] skip_idx Index from @p set to skip.
1042 * @return LY_ERR
1043 */
1044static LY_ERR
1045set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1046{
1047 uint32_t i;
1048
Michal Vasko2caefc12019-11-14 16:07:56 +01001049 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001050 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1051 }
1052
1053 for (i = 0; i < set->used; ++i) {
1054 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1055 continue;
1056 }
1057
1058 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1059 return LY_EEXIST;
1060 }
1061 }
1062
1063 return LY_SUCCESS;
1064}
1065
Radek Krejci857189e2020-09-01 13:26:36 +02001066ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001067lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1068 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001069{
1070 uint32_t i;
1071
1072 for (i = 0; i < set->used; ++i) {
1073 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1074 continue;
1075 }
1076
1077 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001078 if (index_p) {
1079 *index_p = i;
1080 }
1081 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001082 }
1083 }
1084
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001085 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001086}
1087
Michal Vaskoecd62de2019-11-13 12:35:11 +01001088void
1089lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001090{
1091 uint32_t orig_used, i, j;
1092
Michal Vaskod3678892020-05-21 10:06:58 +02001093 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001094
Michal Vaskod3678892020-05-21 10:06:58 +02001095 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001096 return;
1097 }
1098
Michal Vaskod3678892020-05-21 10:06:58 +02001099 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001100 memcpy(set1, set2, sizeof *set1);
1101 return;
1102 }
1103
1104 if (set1->used + set2->used > set1->size) {
1105 set1->size = set1->used + set2->used;
1106 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1107 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1108 }
1109
1110 orig_used = set1->used;
1111
1112 for (i = 0; i < set2->used; ++i) {
1113 for (j = 0; j < orig_used; ++j) {
1114 /* detect duplicities */
1115 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1116 break;
1117 }
1118 }
1119
Michal Vasko0587bce2020-12-10 12:19:21 +01001120 if (j < orig_used) {
1121 /* node is there, but update its status if needed */
1122 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1123 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
1124 }
1125 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001126 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1127 ++set1->used;
1128 }
1129 }
1130
Michal Vaskod3678892020-05-21 10:06:58 +02001131 lyxp_set_free_content(set2);
1132 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001133}
1134
1135/**
1136 * @brief Insert a node into a set. Context position aware.
1137 *
1138 * @param[in] set Set to use.
1139 * @param[in] node Node to insert to @p set.
1140 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1141 * @param[in] node_type Node type of @p node.
1142 * @param[in] idx Index in @p set to insert into.
1143 */
1144static void
1145set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1146{
Michal Vaskod3678892020-05-21 10:06:58 +02001147 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001148
Michal Vaskod3678892020-05-21 10:06:58 +02001149 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001150 /* first item */
1151 if (idx) {
1152 /* no real harm done, but it is a bug */
1153 LOGINT(set->ctx);
1154 idx = 0;
1155 }
1156 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1157 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1158 set->type = LYXP_SET_NODE_SET;
1159 set->used = 0;
1160 set->size = LYXP_SET_SIZE_START;
1161 set->ctx_pos = 1;
1162 set->ctx_size = 1;
1163 set->ht = NULL;
1164 } else {
1165 /* not an empty set */
1166 if (set->used == set->size) {
1167
1168 /* set is full */
1169 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1170 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1171 set->size += LYXP_SET_SIZE_STEP;
1172 }
1173
1174 if (idx > set->used) {
1175 LOGINT(set->ctx);
1176 idx = set->used;
1177 }
1178
1179 /* make space for the new node */
1180 if (idx < set->used) {
1181 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1182 }
1183 }
1184
1185 /* finally assign the value */
1186 set->val.nodes[idx].node = (struct lyd_node *)node;
1187 set->val.nodes[idx].type = node_type;
1188 set->val.nodes[idx].pos = pos;
1189 ++set->used;
1190
Michal Vasko2caefc12019-11-14 16:07:56 +01001191 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1192 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1193 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001194}
1195
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001196LY_ERR
1197lyxp_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 +02001198{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001199 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001200
1201 assert(set->type == LYXP_SET_SCNODE_SET);
1202
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001203 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001204 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001205 } else {
1206 if (set->used == set->size) {
1207 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 +02001208 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001209 set->size += LYXP_SET_SIZE_STEP;
1210 }
1211
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001212 index = set->used;
1213 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1214 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001215 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001216 ++set->used;
1217 }
1218
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001219 if (index_p) {
1220 *index_p = index;
1221 }
1222
1223 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001224}
1225
1226/**
1227 * @brief Replace a node in a set with another. Context position aware.
1228 *
1229 * @param[in] set Set to use.
1230 * @param[in] node Node to insert to @p set.
1231 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1232 * @param[in] node_type Node type of @p node.
1233 * @param[in] idx Index in @p set of the node to replace.
1234 */
1235static void
1236set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1237{
1238 assert(set && (idx < set->used));
1239
Michal Vasko2caefc12019-11-14 16:07:56 +01001240 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1241 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1242 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001243 set->val.nodes[idx].node = (struct lyd_node *)node;
1244 set->val.nodes[idx].type = node_type;
1245 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001246 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1247 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1248 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001249}
1250
1251/**
1252 * @brief Set all nodes with ctx 1 to a new unique context value.
1253 *
1254 * @param[in] set Set to modify.
1255 * @return New context value.
1256 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001257static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001258set_scnode_new_in_ctx(struct lyxp_set *set)
1259{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001260 uint32_t i;
1261 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001262
1263 assert(set->type == LYXP_SET_SCNODE_SET);
1264
Radek Krejcif13b87b2020-12-01 22:02:17 +01001265 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001266retry:
1267 for (i = 0; i < set->used; ++i) {
1268 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1269 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1270 goto retry;
1271 }
1272 }
1273 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001274 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001275 set->val.scnodes[i].in_ctx = ret_ctx;
1276 }
1277 }
1278
1279 return ret_ctx;
1280}
1281
1282/**
1283 * @brief Get unique @p node position in the data.
1284 *
1285 * @param[in] node Node to find.
1286 * @param[in] node_type Node type of @p node.
1287 * @param[in] root Root node.
1288 * @param[in] root_type Type of the XPath @p root node.
1289 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1290 * be used to increase efficiency and start the DFS from this node.
1291 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1292 * @return Node position.
1293 */
1294static uint32_t
1295get_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 +02001296 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001297{
Michal Vasko56daf732020-08-10 10:57:18 +02001298 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001299 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001300 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001301
1302 assert(prev && prev_pos && !root->prev->next);
1303
1304 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1305 return 0;
1306 }
1307
1308 if (*prev) {
1309 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001310 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001311 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001312 goto dfs_search;
1313 }
1314
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001315 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001316 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001317dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001318 if (*prev && !elem) {
1319 /* resume previous DFS */
1320 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1321 LYD_TREE_DFS_continue = 0;
1322 }
1323
Michal Vasko03ff5a72019-09-11 13:49:33 +02001324 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001325 /* skip */
1326 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001327 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001328 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001329 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001330 break;
1331 }
Michal Vasko56daf732020-08-10 10:57:18 +02001332 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001333 }
Michal Vasko56daf732020-08-10 10:57:18 +02001334
1335 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001336 }
1337
1338 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001339 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001340 break;
1341 }
1342 }
1343
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001344 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001345 if (!(*prev)) {
1346 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001347 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001348 return 0;
1349 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001350 /* start the search again from the beginning */
1351 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001352
Michal Vasko56daf732020-08-10 10:57:18 +02001353 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001354 pos = 1;
1355 goto dfs_search;
1356 }
1357 }
1358
1359 /* remember the last found node for next time */
1360 *prev = node;
1361 *prev_pos = pos;
1362
1363 return pos;
1364}
1365
1366/**
1367 * @brief Assign (fill) missing node positions.
1368 *
1369 * @param[in] set Set to fill positions in.
1370 * @param[in] root Context root node.
1371 * @param[in] root_type Context root type.
1372 * @return LY_ERR
1373 */
1374static LY_ERR
1375set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1376{
1377 const struct lyd_node *prev = NULL, *tmp_node;
1378 uint32_t i, tmp_pos = 0;
1379
1380 for (i = 0; i < set->used; ++i) {
1381 if (!set->val.nodes[i].pos) {
1382 tmp_node = NULL;
1383 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001384 case LYXP_NODE_META:
1385 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001386 if (!tmp_node) {
1387 LOGINT_RET(root->schema->module->ctx);
1388 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001389 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001390 case LYXP_NODE_ELEM:
1391 case LYXP_NODE_TEXT:
1392 if (!tmp_node) {
1393 tmp_node = set->val.nodes[i].node;
1394 }
1395 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1396 break;
1397 default:
1398 /* all roots have position 0 */
1399 break;
1400 }
1401 }
1402 }
1403
1404 return LY_SUCCESS;
1405}
1406
1407/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001408 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001409 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001410 * @param[in] meta Metadata to use.
1411 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001412 */
1413static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001414get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001415{
1416 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001417 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001418
Michal Vasko9f96a052020-03-10 09:41:45 +01001419 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001420 ++pos;
1421 }
1422
Michal Vasko9f96a052020-03-10 09:41:45 +01001423 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001424 return pos;
1425}
1426
1427/**
1428 * @brief Compare 2 nodes in respect to XPath document order.
1429 *
1430 * @param[in] item1 1st node.
1431 * @param[in] item2 2nd node.
1432 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1433 */
1434static int
1435set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1436{
Michal Vasko9f96a052020-03-10 09:41:45 +01001437 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001438
1439 if (item1->pos < item2->pos) {
1440 return -1;
1441 }
1442
1443 if (item1->pos > item2->pos) {
1444 return 1;
1445 }
1446
1447 /* node positions are equal, the fun case */
1448
1449 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1450 /* special case since text nodes are actually saved as their parents */
1451 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1452 if (item1->type == LYXP_NODE_ELEM) {
1453 assert(item2->type == LYXP_NODE_TEXT);
1454 return -1;
1455 } else {
1456 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1457 return 1;
1458 }
1459 }
1460
Michal Vasko9f96a052020-03-10 09:41:45 +01001461 /* we need meta positions now */
1462 if (item1->type == LYXP_NODE_META) {
1463 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001464 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001465 if (item2->type == LYXP_NODE_META) {
1466 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001467 }
1468
Michal Vasko9f96a052020-03-10 09:41:45 +01001469 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001470 /* check for duplicates */
1471 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001472 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001473 return 0;
1474 }
1475
Michal Vasko9f96a052020-03-10 09:41:45 +01001476 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001477 /* elem is always first, 2nd node is after it */
1478 if (item1->type == LYXP_NODE_ELEM) {
1479 assert(item2->type != LYXP_NODE_ELEM);
1480 return -1;
1481 }
1482
Michal Vasko9f96a052020-03-10 09:41:45 +01001483 /* 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 +02001484 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001485 if (((item1->type == LYXP_NODE_TEXT) &&
1486 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1487 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1488 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1489 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001490 return 1;
1491 }
1492
Michal Vasko9f96a052020-03-10 09:41:45 +01001493 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001494 /* 2nd is after 1st */
1495 return -1;
1496}
1497
1498/**
1499 * @brief Set cast for comparisons.
1500 *
1501 * @param[in] trg Target set to cast source into.
1502 * @param[in] src Source set.
1503 * @param[in] type Target set type.
1504 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001505 * @return LY_ERR
1506 */
1507static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001508set_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 +02001509{
1510 assert(src->type == LYXP_SET_NODE_SET);
1511
1512 set_init(trg, src);
1513
1514 /* insert node into target set */
1515 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1516
1517 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001518 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001519}
1520
Michal Vasko4c7763f2020-07-27 17:40:37 +02001521/**
1522 * @brief Set content canonization for comparisons.
1523 *
1524 * @param[in] trg Target set to put the canononized source into.
1525 * @param[in] src Source set.
1526 * @param[in] xp_node Source XPath node/meta to use for canonization.
1527 * @return LY_ERR
1528 */
1529static LY_ERR
1530set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1531{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001532 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001533 struct lyd_value val;
1534 struct ly_err_item *err = NULL;
1535 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001536 LY_ERR rc;
1537
1538 /* is there anything to canonize even? */
1539 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1540 /* do we have a type to use for canonization? */
1541 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1542 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1543 } else if (xp_node->type == LYXP_NODE_META) {
1544 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1545 }
1546 }
1547 if (!type) {
1548 goto fill;
1549 }
1550
1551 if (src->type == LYXP_SET_NUMBER) {
1552 /* canonize number */
1553 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1554 LOGMEM(src->ctx);
1555 return LY_EMEM;
1556 }
1557 } else {
1558 /* canonize string */
1559 str = strdup(src->val.str);
1560 }
1561
1562 /* ignore errors, the value may not satisfy schema constraints */
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001563 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 +01001564 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001565 ly_err_free(err);
1566 if (rc) {
1567 /* invalid value */
Radek IÅ¡a48460222021-03-02 15:18:32 +01001568 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001569 goto fill;
1570 }
1571
Michal Vasko4c7763f2020-07-27 17:40:37 +02001572 /* use the canonized value */
1573 set_init(trg, src);
1574 trg->type = src->type;
1575 if (src->type == LYXP_SET_NUMBER) {
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001576 trg->val.num = strtold(val.canonical, &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001577 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1578 } else {
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001579 trg->val.str = strdup(val.canonical);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001580 }
1581 type->plugin->free(src->ctx, &val);
1582 return LY_SUCCESS;
1583
1584fill:
1585 /* no canonization needed/possible */
1586 set_fill_set(trg, src);
1587 return LY_SUCCESS;
1588}
1589
Michal Vasko03ff5a72019-09-11 13:49:33 +02001590#ifndef NDEBUG
1591
1592/**
1593 * @brief Bubble sort @p set into XPath document order.
1594 * Context position aware. Unused in the 'Release' build target.
1595 *
1596 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001597 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1598 */
1599static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001600set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001601{
1602 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001603 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001604 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001605 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001606 struct lyxp_set_node item;
1607 struct lyxp_set_hash_node hnode;
1608 uint64_t hash;
1609
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001610 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001611 return 0;
1612 }
1613
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001614 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001615 for (root = set->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001616 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001617
1618 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001619 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001620 return -1;
1621 }
1622
1623 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1624 print_set_debug(set);
1625
1626 for (i = 0; i < set->used; ++i) {
1627 inverted = 0;
1628 change = 0;
1629
1630 for (j = 1; j < set->used - i; ++j) {
1631 /* compare node positions */
1632 if (inverted) {
1633 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1634 } else {
1635 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1636 }
1637
1638 /* swap if needed */
1639 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1640 change = 1;
1641
1642 item = set->val.nodes[j - 1];
1643 set->val.nodes[j - 1] = set->val.nodes[j];
1644 set->val.nodes[j] = item;
1645 } else {
1646 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1647 inverted = !inverted;
1648 }
1649 }
1650
1651 ++ret;
1652
1653 if (!change) {
1654 break;
1655 }
1656 }
1657
1658 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1659 print_set_debug(set);
1660
1661 /* check node hashes */
1662 if (set->used >= LYD_HT_MIN_ITEMS) {
1663 assert(set->ht);
1664 for (i = 0; i < set->used; ++i) {
1665 hnode.node = set->val.nodes[i].node;
1666 hnode.type = set->val.nodes[i].type;
1667
1668 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1669 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1670 hash = dict_hash_multi(hash, NULL, 0);
1671
1672 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1673 }
1674 }
1675
1676 return ret - 1;
1677}
1678
1679/**
1680 * @brief Remove duplicate entries in a sorted node set.
1681 *
1682 * @param[in] set Sorted set to check.
1683 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1684 */
1685static LY_ERR
1686set_sorted_dup_node_clean(struct lyxp_set *set)
1687{
1688 uint32_t i = 0;
1689 LY_ERR ret = LY_SUCCESS;
1690
1691 if (set->used > 1) {
1692 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001693 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1694 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001695 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001696 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001697 }
Michal Vasko57eab132019-09-24 11:46:26 +02001698 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001699 }
1700 }
1701
Michal Vasko2caefc12019-11-14 16:07:56 +01001702 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001703 return ret;
1704}
1705
1706#endif
1707
1708/**
1709 * @brief Merge 2 sorted sets into one.
1710 *
1711 * @param[in,out] trg Set to merge into. Duplicates are removed.
1712 * @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 +02001713 * @return LY_ERR
1714 */
1715static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001716set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001717{
1718 uint32_t i, j, k, count, dup_count;
1719 int cmp;
1720 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001721
Michal Vaskod3678892020-05-21 10:06:58 +02001722 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001723 return LY_EINVAL;
1724 }
1725
Michal Vaskod3678892020-05-21 10:06:58 +02001726 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001727 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001728 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001729 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001730 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001731 return LY_SUCCESS;
1732 }
1733
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001734 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001735 for (root = trg->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001736 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001737
1738 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001739 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001740 return LY_EINT;
1741 }
1742
1743#ifndef NDEBUG
1744 LOGDBG(LY_LDGXPATH, "MERGE target");
1745 print_set_debug(trg);
1746 LOGDBG(LY_LDGXPATH, "MERGE source");
1747 print_set_debug(src);
1748#endif
1749
1750 /* make memory for the merge (duplicates are not detected yet, so space
1751 * will likely be wasted on them, too bad) */
1752 if (trg->size - trg->used < src->used) {
1753 trg->size = trg->used + src->used;
1754
1755 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1756 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1757 }
1758
1759 i = 0;
1760 j = 0;
1761 count = 0;
1762 dup_count = 0;
1763 do {
1764 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1765 if (!cmp) {
1766 if (!count) {
1767 /* duplicate, just skip it */
1768 ++i;
1769 ++j;
1770 } else {
1771 /* we are copying something already, so let's copy the duplicate too,
1772 * we are hoping that afterwards there are some more nodes to
1773 * copy and this way we can copy them all together */
1774 ++count;
1775 ++dup_count;
1776 ++i;
1777 ++j;
1778 }
1779 } else if (cmp < 0) {
1780 /* inserting src node into trg, just remember it for now */
1781 ++count;
1782 ++i;
1783
1784 /* insert the hash now */
1785 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1786 } else if (count) {
1787copy_nodes:
1788 /* time to actually copy the nodes, we have found the largest block of nodes */
1789 memmove(&trg->val.nodes[j + (count - dup_count)],
1790 &trg->val.nodes[j],
1791 (trg->used - j) * sizeof *trg->val.nodes);
1792 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1793
1794 trg->used += count - dup_count;
1795 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1796 j += count - dup_count;
1797
1798 count = 0;
1799 dup_count = 0;
1800 } else {
1801 ++j;
1802 }
1803 } while ((i < src->used) && (j < trg->used));
1804
1805 if ((i < src->used) || count) {
1806 /* insert all the hashes first */
1807 for (k = i; k < src->used; ++k) {
1808 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1809 }
1810
1811 /* loop ended, but we need to copy something at trg end */
1812 count += src->used - i;
1813 i = src->used;
1814 goto copy_nodes;
1815 }
1816
1817 /* we are inserting hashes before the actual node insert, which causes
1818 * situations when there were initially not enough items for a hash table,
1819 * but even after some were inserted, hash table was not created (during
1820 * insertion the number of items is not updated yet) */
1821 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1822 set_insert_node_hash(trg, NULL, 0);
1823 }
1824
1825#ifndef NDEBUG
1826 LOGDBG(LY_LDGXPATH, "MERGE result");
1827 print_set_debug(trg);
1828#endif
1829
Michal Vaskod3678892020-05-21 10:06:58 +02001830 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001831 return LY_SUCCESS;
1832}
1833
1834/*
1835 * (re)parse functions
1836 *
1837 * Parse functions parse the expression into
1838 * tokens (syntactic analysis).
1839 *
1840 * Reparse functions perform semantic analysis
1841 * (do not save the result, just a check) of
1842 * the expression and fill repeat indices.
1843 */
1844
Michal Vasko14676352020-05-29 11:35:55 +02001845LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001846lyxp_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 +02001847{
Michal Vasko004d3152020-06-11 19:59:22 +02001848 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001849 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001850 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001851 }
Michal Vasko14676352020-05-29 11:35:55 +02001852 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001853 }
1854
Michal Vasko004d3152020-06-11 19:59:22 +02001855 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001856 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001857 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001858 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001859 }
Michal Vasko14676352020-05-29 11:35:55 +02001860 return LY_ENOT;
1861 }
1862
1863 return LY_SUCCESS;
1864}
1865
Michal Vasko004d3152020-06-11 19:59:22 +02001866LY_ERR
1867lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1868{
1869 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1870
1871 /* skip the token */
1872 ++(*tok_idx);
1873
1874 return LY_SUCCESS;
1875}
1876
Michal Vasko14676352020-05-29 11:35:55 +02001877/* just like lyxp_check_token() but tests for 2 tokens */
1878static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001879exp_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 +02001880 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001881{
Michal Vasko004d3152020-06-11 19:59:22 +02001882 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001883 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001884 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001885 }
1886 return LY_EINCOMPLETE;
1887 }
1888
Michal Vasko004d3152020-06-11 19:59:22 +02001889 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001890 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001891 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001892 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001893 }
1894 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001895 }
1896
1897 return LY_SUCCESS;
1898}
1899
1900/**
1901 * @brief Stack operation push on the repeat array.
1902 *
1903 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001904 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001905 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1906 */
1907static void
Michal Vasko004d3152020-06-11 19:59:22 +02001908exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001909{
1910 uint16_t i;
1911
Michal Vasko004d3152020-06-11 19:59:22 +02001912 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001913 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001914 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1915 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1916 exp->repeat[tok_idx][i] = repeat_op_idx;
1917 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001918 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001919 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1920 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1921 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001922 }
1923}
1924
1925/**
1926 * @brief Reparse Predicate. Logs directly on error.
1927 *
1928 * [7] Predicate ::= '[' Expr ']'
1929 *
1930 * @param[in] ctx Context for logging.
1931 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001932 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001933 * @return LY_ERR
1934 */
1935static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001936reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001937{
1938 LY_ERR rc;
1939
Michal Vasko004d3152020-06-11 19:59:22 +02001940 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001941 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001942 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001943
Michal Vasko004d3152020-06-11 19:59:22 +02001944 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001945 LY_CHECK_RET(rc);
1946
Michal Vasko004d3152020-06-11 19:59:22 +02001947 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001948 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001949 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001950
1951 return LY_SUCCESS;
1952}
1953
1954/**
1955 * @brief Reparse RelativeLocationPath. Logs directly on error.
1956 *
1957 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1958 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1959 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1960 *
1961 * @param[in] ctx Context for logging.
1962 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001963 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001964 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1965 */
1966static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001967reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001968{
1969 LY_ERR rc;
1970
Michal Vasko004d3152020-06-11 19:59:22 +02001971 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001972 LY_CHECK_RET(rc);
1973
1974 goto step;
1975 do {
1976 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001977 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001978
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);
1981step:
1982 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001983 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001984 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001985 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001986 break;
1987
1988 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001989 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001990 break;
1991
1992 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02001993 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001994
Michal Vasko004d3152020-06-11 19:59:22 +02001995 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001996 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001997 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001998 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 +02001999 return LY_EVALID;
2000 }
Radek Krejci0f969882020-08-21 16:56:47 +02002001 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002002 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002003 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002004 goto reparse_predicate;
2005 break;
2006
2007 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002008 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002009
2010 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002011 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002012 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002013 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002014
2015 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002016 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002017 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002018 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002019
2020reparse_predicate:
2021 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002022 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2023 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002024 LY_CHECK_RET(rc);
2025 }
2026 break;
2027 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002028 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 +02002029 return LY_EVALID;
2030 }
Michal Vasko004d3152020-06-11 19:59:22 +02002031 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002032
2033 return LY_SUCCESS;
2034}
2035
2036/**
2037 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2038 *
2039 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2040 *
2041 * @param[in] ctx Context for logging.
2042 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002043 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002044 * @return LY_ERR
2045 */
2046static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002047reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002048{
2049 LY_ERR rc;
2050
Michal Vasko004d3152020-06-11 19:59:22 +02002051 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 +02002052
2053 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002054 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002055 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002056 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002057
Michal Vasko004d3152020-06-11 19:59:22 +02002058 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002059 return LY_SUCCESS;
2060 }
Michal Vasko004d3152020-06-11 19:59:22 +02002061 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002062 case LYXP_TOKEN_DOT:
2063 case LYXP_TOKEN_DDOT:
2064 case LYXP_TOKEN_AT:
2065 case LYXP_TOKEN_NAMETEST:
2066 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002067 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002068 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002069 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002070 default:
2071 break;
2072 }
2073
Michal Vasko03ff5a72019-09-11 13:49:33 +02002074 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002075 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002076 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002077
Michal Vasko004d3152020-06-11 19:59:22 +02002078 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002079 LY_CHECK_RET(rc);
2080 }
2081
2082 return LY_SUCCESS;
2083}
2084
2085/**
2086 * @brief Reparse FunctionCall. Logs directly on error.
2087 *
2088 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2089 *
2090 * @param[in] ctx Context for logging.
2091 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002092 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002093 * @return LY_ERR
2094 */
2095static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002096reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002097{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002098 int8_t min_arg_count = -1;
2099 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002100 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002101 LY_ERR rc;
2102
Michal Vasko004d3152020-06-11 19:59:22 +02002103 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002104 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002105 func_tok_idx = *tok_idx;
2106 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002107 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002108 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002109 min_arg_count = 1;
2110 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002111 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002112 min_arg_count = 1;
2113 max_arg_count = 1;
2114 }
2115 break;
2116 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002117 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002118 min_arg_count = 1;
2119 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002120 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002121 min_arg_count = 0;
2122 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002123 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002124 min_arg_count = 0;
2125 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002126 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002127 min_arg_count = 0;
2128 max_arg_count = 0;
2129 }
2130 break;
2131 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002132 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002133 min_arg_count = 1;
2134 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002135 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002136 min_arg_count = 0;
2137 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002138 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002139 min_arg_count = 1;
2140 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002141 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002142 min_arg_count = 1;
2143 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002144 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002145 min_arg_count = 1;
2146 max_arg_count = 1;
2147 }
2148 break;
2149 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002150 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002151 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002152 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002153 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002154 min_arg_count = 0;
2155 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002156 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002157 min_arg_count = 0;
2158 max_arg_count = 1;
2159 }
2160 break;
2161 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002162 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002163 min_arg_count = 1;
2164 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002165 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002166 min_arg_count = 1;
2167 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002168 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002169 min_arg_count = 0;
2170 max_arg_count = 0;
2171 }
2172 break;
2173 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002174 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002175 min_arg_count = 2;
2176 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002177 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002178 min_arg_count = 0;
2179 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002180 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002181 min_arg_count = 2;
2182 max_arg_count = 2;
2183 }
2184 break;
2185 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002186 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002187 min_arg_count = 2;
2188 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002189 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002190 min_arg_count = 3;
2191 max_arg_count = 3;
2192 }
2193 break;
2194 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002195 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002196 min_arg_count = 0;
2197 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002198 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002199 min_arg_count = 1;
2200 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002201 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002202 min_arg_count = 2;
2203 max_arg_count = 2;
2204 }
2205 break;
2206 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002207 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002208 min_arg_count = 2;
2209 max_arg_count = 2;
2210 }
2211 break;
2212 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002213 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002214 min_arg_count = 2;
2215 max_arg_count = 2;
2216 }
2217 break;
2218 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002219 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002220 min_arg_count = 0;
2221 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002222 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002223 min_arg_count = 0;
2224 max_arg_count = 1;
2225 }
2226 break;
2227 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002228 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002229 min_arg_count = 0;
2230 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002231 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002232 min_arg_count = 2;
2233 max_arg_count = 2;
2234 }
2235 break;
2236 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002237 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002238 min_arg_count = 2;
2239 max_arg_count = 2;
2240 }
2241 break;
2242 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002243 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002244 min_arg_count = 2;
2245 max_arg_count = 2;
2246 }
2247 break;
2248 }
2249 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002250 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 +02002251 return LY_EINVAL;
2252 }
Michal Vasko004d3152020-06-11 19:59:22 +02002253 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002254
2255 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002256 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002257 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002258 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002259
2260 /* ( Expr ( ',' Expr )* )? */
2261 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002262 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002263 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002264 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002265 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002266 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002267 LY_CHECK_RET(rc);
2268 }
Michal Vasko004d3152020-06-11 19:59:22 +02002269 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2270 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002271
2272 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002273 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002274 LY_CHECK_RET(rc);
2275 }
2276
2277 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002278 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002279 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002280 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002281
Radek Krejci857189e2020-09-01 13:26:36 +02002282 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002283 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 +02002284 return LY_EVALID;
2285 }
2286
2287 return LY_SUCCESS;
2288}
2289
2290/**
2291 * @brief Reparse PathExpr. Logs directly on error.
2292 *
2293 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2294 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2295 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2296 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2297 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2298 *
2299 * @param[in] ctx Context for logging.
2300 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002301 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002302 * @return LY_ERR
2303 */
2304static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002305reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002306{
2307 LY_ERR rc;
2308
Michal Vasko004d3152020-06-11 19:59:22 +02002309 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002310 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002311 }
2312
Michal Vasko004d3152020-06-11 19:59:22 +02002313 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002314 case LYXP_TOKEN_PAR1:
2315 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002316 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002317
Michal Vasko004d3152020-06-11 19:59:22 +02002318 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002319 LY_CHECK_RET(rc);
2320
Michal Vasko004d3152020-06-11 19:59:22 +02002321 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002322 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002323 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002324 goto predicate;
2325 break;
2326 case LYXP_TOKEN_DOT:
2327 case LYXP_TOKEN_DDOT:
2328 case LYXP_TOKEN_AT:
2329 case LYXP_TOKEN_NAMETEST:
2330 case LYXP_TOKEN_NODETYPE:
2331 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002332 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002333 LY_CHECK_RET(rc);
2334 break;
2335 case LYXP_TOKEN_FUNCNAME:
2336 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002337 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002338 LY_CHECK_RET(rc);
2339 goto predicate;
2340 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002341 case LYXP_TOKEN_OPER_PATH:
2342 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002343 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002344 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002345 LY_CHECK_RET(rc);
2346 break;
2347 case LYXP_TOKEN_LITERAL:
2348 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002349 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002350 goto predicate;
2351 break;
2352 case LYXP_TOKEN_NUMBER:
2353 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002354 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002355 goto predicate;
2356 break;
2357 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002358 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 +02002359 return LY_EVALID;
2360 }
2361
2362 return LY_SUCCESS;
2363
2364predicate:
2365 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002366 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2367 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002368 LY_CHECK_RET(rc);
2369 }
2370
2371 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002372 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002373
2374 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002375 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002376
Michal Vasko004d3152020-06-11 19:59:22 +02002377 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002378 LY_CHECK_RET(rc);
2379 }
2380
2381 return LY_SUCCESS;
2382}
2383
2384/**
2385 * @brief Reparse UnaryExpr. Logs directly on error.
2386 *
2387 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2388 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2389 *
2390 * @param[in] ctx Context for logging.
2391 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002392 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002393 * @return LY_ERR
2394 */
2395static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002396reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002397{
2398 uint16_t prev_exp;
2399 LY_ERR rc;
2400
2401 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002402 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002403 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2404 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002405 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002406 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002407 }
2408
2409 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002410 prev_exp = *tok_idx;
2411 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002412 LY_CHECK_RET(rc);
2413
2414 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002415 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002416 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002417 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002418
Michal Vasko004d3152020-06-11 19:59:22 +02002419 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002420 LY_CHECK_RET(rc);
2421 }
2422
2423 return LY_SUCCESS;
2424}
2425
2426/**
2427 * @brief Reparse AdditiveExpr. Logs directly on error.
2428 *
2429 * [15] AdditiveExpr ::= MultiplicativeExpr
2430 * | AdditiveExpr '+' MultiplicativeExpr
2431 * | AdditiveExpr '-' MultiplicativeExpr
2432 * [16] MultiplicativeExpr ::= UnaryExpr
2433 * | MultiplicativeExpr '*' UnaryExpr
2434 * | MultiplicativeExpr 'div' UnaryExpr
2435 * | MultiplicativeExpr 'mod' UnaryExpr
2436 *
2437 * @param[in] ctx Context for logging.
2438 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002439 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002440 * @return LY_ERR
2441 */
2442static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002443reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002444{
2445 uint16_t prev_add_exp, prev_mul_exp;
2446 LY_ERR rc;
2447
Michal Vasko004d3152020-06-11 19:59:22 +02002448 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002449 goto reparse_multiplicative_expr;
2450
2451 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002452 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2453 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002454 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002455 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002456
2457reparse_multiplicative_expr:
2458 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002459 prev_mul_exp = *tok_idx;
2460 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002461 LY_CHECK_RET(rc);
2462
2463 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002464 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2465 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002466 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002467 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002468
Michal Vasko004d3152020-06-11 19:59:22 +02002469 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002470 LY_CHECK_RET(rc);
2471 }
2472 }
2473
2474 return LY_SUCCESS;
2475}
2476
2477/**
2478 * @brief Reparse EqualityExpr. Logs directly on error.
2479 *
2480 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2481 * | EqualityExpr '!=' RelationalExpr
2482 * [14] RelationalExpr ::= AdditiveExpr
2483 * | RelationalExpr '<' AdditiveExpr
2484 * | RelationalExpr '>' AdditiveExpr
2485 * | RelationalExpr '<=' AdditiveExpr
2486 * | RelationalExpr '>=' AdditiveExpr
2487 *
2488 * @param[in] ctx Context for logging.
2489 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002490 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002491 * @return LY_ERR
2492 */
2493static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002494reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002495{
2496 uint16_t prev_eq_exp, prev_rel_exp;
2497 LY_ERR rc;
2498
Michal Vasko004d3152020-06-11 19:59:22 +02002499 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002500 goto reparse_additive_expr;
2501
2502 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002503 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002504 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002505 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002506
2507reparse_additive_expr:
2508 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002509 prev_rel_exp = *tok_idx;
2510 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002511 LY_CHECK_RET(rc);
2512
2513 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002514 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002515 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002516 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002517
Michal Vasko004d3152020-06-11 19:59:22 +02002518 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002519 LY_CHECK_RET(rc);
2520 }
2521 }
2522
2523 return LY_SUCCESS;
2524}
2525
2526/**
2527 * @brief Reparse OrExpr. Logs directly on error.
2528 *
2529 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2530 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2531 *
2532 * @param[in] ctx Context for logging.
2533 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002534 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002535 * @return LY_ERR
2536 */
2537static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002538reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002539{
2540 uint16_t prev_or_exp, prev_and_exp;
2541 LY_ERR rc;
2542
Michal Vasko004d3152020-06-11 19:59:22 +02002543 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002544 goto reparse_equality_expr;
2545
2546 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002547 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 +02002548 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002549 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002550
2551reparse_equality_expr:
2552 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002553 prev_and_exp = *tok_idx;
2554 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002555 LY_CHECK_RET(rc);
2556
2557 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002558 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 +02002559 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002560 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002561
Michal Vasko004d3152020-06-11 19:59:22 +02002562 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002563 LY_CHECK_RET(rc);
2564 }
2565 }
2566
2567 return LY_SUCCESS;
2568}
Radek Krejcib1646a92018-11-02 16:08:26 +01002569
2570/**
2571 * @brief Parse NCName.
2572 *
2573 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002574 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002575 */
Radek Krejcid4270262019-01-07 15:07:25 +01002576static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002577parse_ncname(const char *ncname)
2578{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002579 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002580 size_t size;
2581 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002582
2583 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2584 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2585 return len;
2586 }
2587
2588 do {
2589 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002590 if (!*ncname) {
2591 break;
2592 }
Radek Krejcid4270262019-01-07 15:07:25 +01002593 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002594 } while (is_xmlqnamechar(uc) && (uc != ':'));
2595
2596 return len;
2597}
2598
2599/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002600 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002601 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002602 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002603 * @param[in] exp Expression to use.
2604 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002605 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002606 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002607 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002608 */
2609static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002610exp_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 +01002611{
2612 uint32_t prev;
2613
2614 if (exp->used == exp->size) {
2615 prev = exp->size;
2616 exp->size += LYXP_EXPR_SIZE_STEP;
2617 if (prev > exp->size) {
2618 LOGINT(ctx);
2619 return LY_EINT;
2620 }
2621
2622 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2623 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2624 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2625 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2626 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2627 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2628 }
2629
2630 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002631 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002632 exp->tok_len[exp->used] = tok_len;
2633 ++exp->used;
2634 return LY_SUCCESS;
2635}
2636
2637void
Michal Vasko14676352020-05-29 11:35:55 +02002638lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002639{
2640 uint16_t i;
2641
2642 if (!expr) {
2643 return;
2644 }
2645
2646 lydict_remove(ctx, expr->expr);
2647 free(expr->tokens);
2648 free(expr->tok_pos);
2649 free(expr->tok_len);
2650 if (expr->repeat) {
2651 for (i = 0; i < expr->used; ++i) {
2652 free(expr->repeat[i]);
2653 }
2654 }
2655 free(expr->repeat);
2656 free(expr);
2657}
2658
Radek Krejcif03a9e22020-09-18 20:09:31 +02002659LY_ERR
2660lyxp_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 +01002661{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002662 LY_ERR ret = LY_SUCCESS;
2663 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002664 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002665 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002666 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002667 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002668
Radek Krejcif03a9e22020-09-18 20:09:31 +02002669 assert(expr_p);
2670
2671 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002672 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002673 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002674 }
2675
2676 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002677 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002678 }
2679 if (expr_len > UINT16_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002680 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002681 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002682 }
2683
2684 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002685 expr = calloc(1, sizeof *expr);
2686 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2687 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2688 expr->used = 0;
2689 expr->size = LYXP_EXPR_SIZE_START;
2690 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2691 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002692
Radek Krejcif03a9e22020-09-18 20:09:31 +02002693 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2694 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002695
Radek Krejcif03a9e22020-09-18 20:09:31 +02002696 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2697 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002698
Michal Vasko004d3152020-06-11 19:59:22 +02002699 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002700 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002701
Radek Krejcif03a9e22020-09-18 20:09:31 +02002702 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002703 ++parsed;
2704 }
2705
2706 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002707 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002708
2709 /* '(' */
2710 tok_len = 1;
2711 tok_type = LYXP_TOKEN_PAR1;
2712
Radek Krejcif03a9e22020-09-18 20:09:31 +02002713 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002714 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002715 if (((expr->tok_len[expr->used - 1] == 4) &&
2716 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2717 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2718 ((expr->tok_len[expr->used - 1] == 7) &&
2719 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002720 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002721 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002722 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002723 }
2724 prev_function_check = 0;
2725 }
2726
Radek Krejcif03a9e22020-09-18 20:09:31 +02002727 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002728
2729 /* ')' */
2730 tok_len = 1;
2731 tok_type = LYXP_TOKEN_PAR2;
2732
Radek Krejcif03a9e22020-09-18 20:09:31 +02002733 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002734
2735 /* '[' */
2736 tok_len = 1;
2737 tok_type = LYXP_TOKEN_BRACK1;
2738
Radek Krejcif03a9e22020-09-18 20:09:31 +02002739 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002740
2741 /* ']' */
2742 tok_len = 1;
2743 tok_type = LYXP_TOKEN_BRACK2;
2744
Radek Krejcif03a9e22020-09-18 20:09:31 +02002745 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002746
2747 /* '..' */
2748 tok_len = 2;
2749 tok_type = LYXP_TOKEN_DDOT;
2750
Radek Krejcif03a9e22020-09-18 20:09:31 +02002751 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002752
2753 /* '.' */
2754 tok_len = 1;
2755 tok_type = LYXP_TOKEN_DOT;
2756
Radek Krejcif03a9e22020-09-18 20:09:31 +02002757 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002758
2759 /* '@' */
2760 tok_len = 1;
2761 tok_type = LYXP_TOKEN_AT;
2762
Radek Krejcif03a9e22020-09-18 20:09:31 +02002763 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002764
2765 /* ',' */
2766 tok_len = 1;
2767 tok_type = LYXP_TOKEN_COMMA;
2768
Radek Krejcif03a9e22020-09-18 20:09:31 +02002769 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002770
2771 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002772 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2773 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002774 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002775 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002776 ++tok_len;
2777 tok_type = LYXP_TOKEN_LITERAL;
2778
Radek Krejcif03a9e22020-09-18 20:09:31 +02002779 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002780
2781 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002782 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2783 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002784 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002785 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002786 ++tok_len;
2787 tok_type = LYXP_TOKEN_LITERAL;
2788
Radek Krejcif03a9e22020-09-18 20:09:31 +02002789 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002790
2791 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002792 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2793 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002794 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002795 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002796 }
2797 tok_type = LYXP_TOKEN_NUMBER;
2798
Radek Krejcif03a9e22020-09-18 20:09:31 +02002799 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002800
2801 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002802 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002803 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002804 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002805 } else {
2806 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002807 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002808 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002809
Radek Krejcif03a9e22020-09-18 20:09:31 +02002810 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002811
Michal Vasko3e48bf32020-06-01 08:39:07 +02002812 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002813 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002814 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2815
Radek Krejcif03a9e22020-09-18 20:09:31 +02002816 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002817
2818 /* Operator '<=', '>=' */
2819 tok_len = 2;
2820 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002821
Radek Krejcif03a9e22020-09-18 20:09:31 +02002822 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002823
2824 /* Operator '|' */
2825 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002826 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002827
Radek Krejcif03a9e22020-09-18 20:09:31 +02002828 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002829
2830 /* Operator '+', '-' */
2831 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002832 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002833
Radek Krejcif03a9e22020-09-18 20:09:31 +02002834 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002835
Michal Vasko3e48bf32020-06-01 08:39:07 +02002836 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002837 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002838 tok_type = LYXP_TOKEN_OPER_EQUAL;
2839
Radek Krejcif03a9e22020-09-18 20:09:31 +02002840 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002841
2842 /* Operator '<', '>' */
2843 tok_len = 1;
2844 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002845
Michal Vasko69730152020-10-09 16:30:07 +02002846 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2847 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2848 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2849 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2850 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2851 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2852 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2853 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2854 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2855 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2856 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2857 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002858
2859 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002860 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002861 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002862 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002863
Radek Krejcif03a9e22020-09-18 20:09:31 +02002864 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002865 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002866 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002867
Radek Krejcif03a9e22020-09-18 20:09:31 +02002868 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002869 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002870 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002871
Radek Krejcif03a9e22020-09-18 20:09:31 +02002872 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002873 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002874 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002875
2876 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002877 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 +02002878 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 +02002879 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002880 goto error;
2881 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002882 LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002883 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002884 goto error;
2885 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002886 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002887
2888 /* NameTest '*' */
2889 tok_len = 1;
2890 tok_type = LYXP_TOKEN_NAMETEST;
2891
2892 } else {
2893
2894 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002895 long int ncname_len = parse_ncname(&expr_str[parsed]);
2896 LY_CHECK_ERR_GOTO(ncname_len < 0,
Radek Krejci2efc45b2020-12-22 16:25:44 +01002897 LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002898 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002899 tok_len = ncname_len;
2900
Radek Krejcif03a9e22020-09-18 20:09:31 +02002901 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002902 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002903 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002904 ++tok_len;
2905 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002906 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
2907 LY_CHECK_ERR_GOTO(ncname_len < 0,
Radek Krejci2efc45b2020-12-22 16:25:44 +01002908 LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002909 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002910 tok_len += ncname_len;
2911 }
2912 /* remove old flag to prevent ambiguities */
2913 prev_function_check = 0;
2914 tok_type = LYXP_TOKEN_NAMETEST;
2915 } else {
2916 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2917 prev_function_check = 1;
2918 tok_type = LYXP_TOKEN_NAMETEST;
2919 }
2920 }
2921
2922 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002923 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002924 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002925 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002926 ++parsed;
2927 }
2928
Radek Krejcif03a9e22020-09-18 20:09:31 +02002929 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002930
Michal Vasko004d3152020-06-11 19:59:22 +02002931 if (reparse) {
2932 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002933 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2934 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002935
Michal Vasko004d3152020-06-11 19:59:22 +02002936 /* fill repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002937 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error);
2938 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002939 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002940 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002941 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002942 goto error;
2943 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002944 }
2945
Radek Krejcif03a9e22020-09-18 20:09:31 +02002946 print_expr_struct_debug(expr);
2947 *expr_p = expr;
2948 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002949
2950error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002951 lyxp_expr_free(ctx, expr);
2952 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002953}
2954
Michal Vasko1734be92020-09-22 08:55:10 +02002955LY_ERR
2956lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02002957{
Michal Vasko1734be92020-09-22 08:55:10 +02002958 LY_ERR ret = LY_SUCCESS;
2959 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02002960 uint32_t i, j;
2961
Michal Vasko7f45cf22020-10-01 12:49:44 +02002962 if (!exp) {
2963 goto cleanup;
2964 }
2965
Michal Vasko004d3152020-06-11 19:59:22 +02002966 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02002967 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002968
2969 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
Michal Vasko1734be92020-09-22 08:55:10 +02002970 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002971 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2972
2973 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
Michal Vasko1734be92020-09-22 08:55:10 +02002974 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002975 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2976
2977 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
Michal Vasko1734be92020-09-22 08:55:10 +02002978 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002979 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2980
2981 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002982 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002983 for (i = 0; i < exp->used; ++i) {
2984 if (!exp->repeat[i]) {
2985 dup->repeat[i] = NULL;
2986 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02002987 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002988 /* the ending 0 as well */
2989 ++j;
2990
Michal Vasko99c71642020-07-03 13:33:36 +02002991 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002992 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002993 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
2994 dup->repeat[i][j - 1] = 0;
2995 }
2996 }
2997
2998 dup->used = exp->used;
2999 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003000 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003001
Michal Vasko1734be92020-09-22 08:55:10 +02003002cleanup:
3003 if (ret) {
3004 lyxp_expr_free(ctx, dup);
3005 } else {
3006 *dup_p = dup;
3007 }
3008 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003009}
3010
Michal Vasko03ff5a72019-09-11 13:49:33 +02003011/*
3012 * warn functions
3013 *
3014 * Warn functions check specific reasonable conditions for schema XPath
3015 * and print a warning if they are not satisfied.
3016 */
3017
3018/**
3019 * @brief Get the last-added schema node that is currently in the context.
3020 *
3021 * @param[in] set Set to search in.
3022 * @return Last-added schema context node, NULL if no node is in context.
3023 */
3024static struct lysc_node *
3025warn_get_scnode_in_ctx(struct lyxp_set *set)
3026{
3027 uint32_t i;
3028
3029 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3030 return NULL;
3031 }
3032
3033 i = set->used;
3034 do {
3035 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003036 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003037 /* if there are more, simply return the first found (last added) */
3038 return set->val.scnodes[i].scnode;
3039 }
3040 } while (i);
3041
3042 return NULL;
3043}
3044
3045/**
3046 * @brief Test whether a type is numeric - integer type or decimal64.
3047 *
3048 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003049 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003050 */
Radek Krejci857189e2020-09-01 13:26:36 +02003051static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003052warn_is_numeric_type(struct lysc_type *type)
3053{
3054 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003055 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003056 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003057
3058 switch (type->basetype) {
3059 case LY_TYPE_DEC64:
3060 case LY_TYPE_INT8:
3061 case LY_TYPE_UINT8:
3062 case LY_TYPE_INT16:
3063 case LY_TYPE_UINT16:
3064 case LY_TYPE_INT32:
3065 case LY_TYPE_UINT32:
3066 case LY_TYPE_INT64:
3067 case LY_TYPE_UINT64:
3068 return 1;
3069 case LY_TYPE_UNION:
3070 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003071 LY_ARRAY_FOR(uni->types, u) {
3072 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003073 if (ret) {
3074 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003075 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003076 }
3077 }
3078 /* did not find any suitable type */
3079 return 0;
3080 case LY_TYPE_LEAFREF:
3081 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3082 default:
3083 return 0;
3084 }
3085}
3086
3087/**
3088 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3089 *
3090 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003091 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003092 */
Radek Krejci857189e2020-09-01 13:26:36 +02003093static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003094warn_is_string_type(struct lysc_type *type)
3095{
3096 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003097 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003098 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003099
3100 switch (type->basetype) {
3101 case LY_TYPE_BITS:
3102 case LY_TYPE_ENUM:
3103 case LY_TYPE_IDENT:
3104 case LY_TYPE_INST:
3105 case LY_TYPE_STRING:
3106 return 1;
3107 case LY_TYPE_UNION:
3108 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003109 LY_ARRAY_FOR(uni->types, u) {
3110 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003111 if (ret) {
3112 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003113 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003114 }
3115 }
3116 /* did not find any suitable type */
3117 return 0;
3118 case LY_TYPE_LEAFREF:
3119 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3120 default:
3121 return 0;
3122 }
3123}
3124
3125/**
3126 * @brief Test whether a type is one specific type.
3127 *
3128 * @param[in] type Type to test.
3129 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003130 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003131 */
Radek Krejci857189e2020-09-01 13:26:36 +02003132static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003133warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3134{
3135 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003136 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003137 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003138
3139 if (type->basetype == base) {
3140 return 1;
3141 } else if (type->basetype == LY_TYPE_UNION) {
3142 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003143 LY_ARRAY_FOR(uni->types, u) {
3144 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003145 if (ret) {
3146 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003147 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003148 }
3149 }
3150 /* did not find any suitable type */
3151 return 0;
3152 } else if (type->basetype == LY_TYPE_LEAFREF) {
3153 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3154 }
3155
3156 return 0;
3157}
3158
3159/**
3160 * @brief Get next type of a (union) type.
3161 *
3162 * @param[in] type Base type.
3163 * @param[in] prev_type Previously returned type.
3164 * @return Next type or NULL.
3165 */
3166static struct lysc_type *
3167warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3168{
3169 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003170 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003171 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003172
3173 switch (type->basetype) {
3174 case LY_TYPE_UNION:
3175 uni = (struct lysc_type_union *)type;
3176 if (!prev_type) {
3177 return uni->types[0];
3178 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003179 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003180 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003181 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003182 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003183 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003184 found = 1;
3185 }
3186 }
3187 return NULL;
3188 default:
3189 if (prev_type) {
3190 assert(type == prev_type);
3191 return NULL;
3192 } else {
3193 return type;
3194 }
3195 }
3196}
3197
3198/**
3199 * @brief Test whether 2 types have a common type.
3200 *
3201 * @param[in] type1 First type.
3202 * @param[in] type2 Second type.
3203 * @return 1 if they do, 0 otherwise.
3204 */
3205static int
3206warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3207{
3208 struct lysc_type *t1, *rt1, *t2, *rt2;
3209
3210 t1 = NULL;
3211 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3212 if (t1->basetype == LY_TYPE_LEAFREF) {
3213 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3214 } else {
3215 rt1 = t1;
3216 }
3217
3218 t2 = NULL;
3219 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3220 if (t2->basetype == LY_TYPE_LEAFREF) {
3221 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3222 } else {
3223 rt2 = t2;
3224 }
3225
3226 if (rt2->basetype == rt1->basetype) {
3227 /* match found */
3228 return 1;
3229 }
3230 }
3231 }
3232
3233 return 0;
3234}
3235
3236/**
3237 * @brief Check both operands of comparison operators.
3238 *
3239 * @param[in] ctx Context for errors.
3240 * @param[in] set1 First operand set.
3241 * @param[in] set2 Second operand set.
3242 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3243 * @param[in] expr Start of the expression to print with the warning.
3244 * @param[in] tok_pos Token position.
3245 */
3246static void
Radek Krejci857189e2020-09-01 13:26:36 +02003247warn_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 +02003248{
3249 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003250 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003251
3252 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3253 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3254
3255 if (!node1 && !node2) {
3256 /* no node-sets involved, nothing to do */
3257 return;
3258 }
3259
3260 if (node1) {
3261 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3262 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3263 warning = 1;
3264 leaves = 0;
3265 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3266 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3267 warning = 1;
3268 }
3269 }
3270
3271 if (node2) {
3272 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3273 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3274 warning = 1;
3275 leaves = 0;
3276 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3277 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3278 warning = 1;
3279 }
3280 }
3281
3282 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003283 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3284 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3285 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3286 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003287 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3288 warning = 1;
3289 }
3290 }
3291
3292 if (warning) {
3293 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3294 }
3295}
3296
3297/**
3298 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3299 *
3300 * @param[in] exp Parsed XPath expression.
3301 * @param[in] set Set with the leaf/leaf-list.
3302 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3303 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3304 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3305 */
3306static void
Michal Vasko40308e72020-10-20 16:38:40 +02003307warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3308 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003309{
3310 struct lysc_node *scnode;
3311 struct lysc_type *type;
3312 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003313 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003314 LY_ERR rc;
3315 struct ly_err_item *err = NULL;
3316
Michal Vasko69730152020-10-09 16:30:07 +02003317 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3318 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003319 /* check that the node can have the specified value */
3320 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3321 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3322 } else {
3323 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3324 }
3325 if (!value) {
3326 LOGMEM(set->ctx);
3327 return;
3328 }
3329
3330 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3331 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003332 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003333 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003334 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003335 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003336 }
3337
3338 type = ((struct lysc_node_leaf *)scnode)->type;
3339 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003340 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003341 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003342 if (rc == LY_EINCOMPLETE) {
3343 rc = LY_SUCCESS;
3344 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003345
3346 if (err) {
3347 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3348 ly_err_free(err);
3349 } else if (rc != LY_SUCCESS) {
3350 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3351 }
3352 if (rc != LY_SUCCESS) {
3353 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003354 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003355 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003356 } else {
3357 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003358 }
3359 }
3360 free(value);
3361 }
3362}
3363
3364/*
3365 * XPath functions
3366 */
3367
3368/**
3369 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3370 * depending on whether the first node bit value from the second argument is set.
3371 *
3372 * @param[in] args Array of arguments.
3373 * @param[in] arg_count Count of elements in @p args.
3374 * @param[in,out] set Context and result set at the same time.
3375 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003376 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003377 */
3378static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003379xpath_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 +02003380{
3381 struct lyd_node_term *leaf;
3382 struct lysc_node_leaf *sleaf;
3383 struct lysc_type_bits *bits;
3384 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003385 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003386
3387 if (options & LYXP_SCNODE_ALL) {
3388 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3389 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003390 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3391 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 +02003392 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3393 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003394 }
3395
3396 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3397 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3398 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 +02003399 } else if (!warn_is_string_type(sleaf->type)) {
3400 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003401 }
3402 }
3403 set_scnode_clear_ctx(set);
3404 return rc;
3405 }
3406
Michal Vaskod3678892020-05-21 10:06:58 +02003407 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003408 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 +02003409 return LY_EVALID;
3410 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003411 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003412 LY_CHECK_RET(rc);
3413
3414 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003415 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003416 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003417 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3418 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003419 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003420 LY_ARRAY_FOR(bits->bits, u) {
3421 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003422 set_fill_boolean(set, 1);
3423 break;
3424 }
3425 }
3426 }
3427 }
3428
3429 return LY_SUCCESS;
3430}
3431
3432/**
3433 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3434 * with the argument converted to boolean.
3435 *
3436 * @param[in] args Array of arguments.
3437 * @param[in] arg_count Count of elements in @p args.
3438 * @param[in,out] set Context and result set at the same time.
3439 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003440 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003441 */
3442static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003443xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003444{
3445 LY_ERR rc;
3446
3447 if (options & LYXP_SCNODE_ALL) {
3448 set_scnode_clear_ctx(set);
3449 return LY_SUCCESS;
3450 }
3451
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003452 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003453 LY_CHECK_RET(rc);
3454 set_fill_set(set, args[0]);
3455
3456 return LY_SUCCESS;
3457}
3458
3459/**
3460 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3461 * with the first argument rounded up to the nearest integer.
3462 *
3463 * @param[in] args Array of arguments.
3464 * @param[in] arg_count Count of elements in @p args.
3465 * @param[in,out] set Context and result set at the same time.
3466 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003467 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003468 */
3469static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003470xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003471{
3472 struct lysc_node_leaf *sleaf;
3473 LY_ERR rc = LY_SUCCESS;
3474
3475 if (options & LYXP_SCNODE_ALL) {
3476 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3477 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003478 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3479 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 +02003480 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3481 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003482 }
3483 set_scnode_clear_ctx(set);
3484 return rc;
3485 }
3486
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003487 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003488 LY_CHECK_RET(rc);
3489 if ((long long)args[0]->val.num != args[0]->val.num) {
3490 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3491 } else {
3492 set_fill_number(set, args[0]->val.num);
3493 }
3494
3495 return LY_SUCCESS;
3496}
3497
3498/**
3499 * @brief Execute the XPath concat(string, string, string*) function.
3500 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3501 *
3502 * @param[in] args Array of arguments.
3503 * @param[in] arg_count Count of elements in @p args.
3504 * @param[in,out] set Context and result set at the same time.
3505 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003506 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003507 */
3508static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003509xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003510{
3511 uint16_t i;
3512 char *str = NULL;
3513 size_t used = 1;
3514 LY_ERR rc = LY_SUCCESS;
3515 struct lysc_node_leaf *sleaf;
3516
3517 if (options & LYXP_SCNODE_ALL) {
3518 for (i = 0; i < arg_count; ++i) {
3519 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3520 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3521 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003522 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003523 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003524 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 +02003525 }
3526 }
3527 }
3528 set_scnode_clear_ctx(set);
3529 return rc;
3530 }
3531
3532 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003533 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003534 if (rc != LY_SUCCESS) {
3535 free(str);
3536 return rc;
3537 }
3538
3539 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3540 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3541 strcpy(str + used - 1, args[i]->val.str);
3542 used += strlen(args[i]->val.str);
3543 }
3544
3545 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003546 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003547 set->type = LYXP_SET_STRING;
3548 set->val.str = str;
3549
3550 return LY_SUCCESS;
3551}
3552
3553/**
3554 * @brief Execute the XPath contains(string, string) function.
3555 * Returns LYXP_SET_BOOLEAN whether the second argument can
3556 * be found in the first or not.
3557 *
3558 * @param[in] args Array of arguments.
3559 * @param[in] arg_count Count of elements in @p args.
3560 * @param[in,out] set Context and result set at the same time.
3561 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003562 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003563 */
3564static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003565xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003566{
3567 struct lysc_node_leaf *sleaf;
3568 LY_ERR rc = LY_SUCCESS;
3569
3570 if (options & LYXP_SCNODE_ALL) {
3571 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3572 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3573 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 +02003574 } else if (!warn_is_string_type(sleaf->type)) {
3575 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003576 }
3577 }
3578
3579 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3580 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3581 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 +02003582 } else if (!warn_is_string_type(sleaf->type)) {
3583 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003584 }
3585 }
3586 set_scnode_clear_ctx(set);
3587 return rc;
3588 }
3589
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003590 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003591 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003592 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003593 LY_CHECK_RET(rc);
3594
3595 if (strstr(args[0]->val.str, args[1]->val.str)) {
3596 set_fill_boolean(set, 1);
3597 } else {
3598 set_fill_boolean(set, 0);
3599 }
3600
3601 return LY_SUCCESS;
3602}
3603
3604/**
3605 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3606 * with the size of the node-set from the argument.
3607 *
3608 * @param[in] args Array of arguments.
3609 * @param[in] arg_count Count of elements in @p args.
3610 * @param[in,out] set Context and result set at the same time.
3611 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003612 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003613 */
3614static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003615xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003616{
3617 struct lysc_node *scnode = NULL;
3618 LY_ERR rc = LY_SUCCESS;
3619
3620 if (options & LYXP_SCNODE_ALL) {
3621 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3622 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003623 }
3624 set_scnode_clear_ctx(set);
3625 return rc;
3626 }
3627
Michal Vasko03ff5a72019-09-11 13:49:33 +02003628 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003629 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003630 return LY_EVALID;
3631 }
3632
3633 set_fill_number(set, args[0]->used);
3634 return LY_SUCCESS;
3635}
3636
3637/**
3638 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3639 * with the context with the intial node.
3640 *
3641 * @param[in] args Array of arguments.
3642 * @param[in] arg_count Count of elements in @p args.
3643 * @param[in,out] set Context and result set at the same time.
3644 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003645 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003646 */
3647static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003648xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003649{
3650 if (arg_count || args) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003651 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003652 return LY_EVALID;
3653 }
3654
3655 if (options & LYXP_SCNODE_ALL) {
3656 set_scnode_clear_ctx(set);
3657
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003658 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003659 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003660 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003661
3662 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003663 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003664 }
3665
3666 return LY_SUCCESS;
3667}
3668
3669/**
3670 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3671 * leafref or instance-identifier target node(s).
3672 *
3673 * @param[in] args Array of arguments.
3674 * @param[in] arg_count Count of elements in @p args.
3675 * @param[in,out] set Context and result set at the same time.
3676 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003677 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003678 */
3679static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003680xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003681{
3682 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003683 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003684 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003685 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003686 struct ly_path *p;
3687 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003688 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003689 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003690 LY_ERR rc = LY_SUCCESS;
3691
3692 if (options & LYXP_SCNODE_ALL) {
3693 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3694 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003695 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3696 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 +02003697 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3698 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
Michal Vasko69730152020-10-09 16:30:07 +02003699 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003700 }
3701 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003702 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003703 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003704 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003705
3706 /* it was already evaluated on schema, it must succeed */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003707 rc = ly_path_compile(set->ctx, lref->cur_mod, &sleaf->node, lref->path, LY_PATH_LREF_TRUE, oper,
3708 LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, NULL, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003709 assert(!rc);
3710
3711 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003712 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003713 ly_path_free(set->ctx, p);
3714
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003715 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003716 }
3717
Michal Vasko03ff5a72019-09-11 13:49:33 +02003718 return rc;
3719 }
3720
Michal Vaskod3678892020-05-21 10:06:58 +02003721 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003722 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003723 return LY_EVALID;
3724 }
3725
Michal Vaskod3678892020-05-21 10:06:58 +02003726 lyxp_set_free_content(set);
3727 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003728 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3729 sleaf = (struct lysc_node_leaf *)leaf->schema;
3730 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3731 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3732 /* find leafref target */
Michal Vasko9e685082021-01-29 14:49:09 +01003733 if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
3734 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003735 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003736 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003737 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003738 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003739 } else {
3740 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003741 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003742 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Michal Vasko69730152020-10-09 16:30:07 +02003743 LYD_CANON_VALUE(leaf));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003744 return LY_EVALID;
3745 }
3746 }
Michal Vasko004d3152020-06-11 19:59:22 +02003747
3748 /* insert it */
3749 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003750 }
3751 }
3752
3753 return LY_SUCCESS;
3754}
3755
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003756static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003757xpath_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 +02003758{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01003759 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003760 LY_ARRAY_COUNT_TYPE u;
3761 struct lyd_node_term *leaf;
3762 struct lysc_node_leaf *sleaf;
3763 struct lyd_meta *meta;
3764 struct lyd_value data = {0}, *val;
3765 struct ly_err_item *err = NULL;
3766 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003767 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003768
3769 if (options & LYXP_SCNODE_ALL) {
3770 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3771 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
3772 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3773 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003774 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003775 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3776 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3777 }
3778
3779 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3780 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3781 LOGWRN(set->ctx, "Argument #2 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_string_type(sleaf->type)) {
3784 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3785 }
3786 }
3787 set_scnode_clear_ctx(set);
3788 return rc;
3789 }
3790
3791 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003792 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 +02003793 return LY_EVALID;
3794 }
3795 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3796 LY_CHECK_RET(rc);
3797
3798 set_fill_boolean(set, 0);
3799 found = 0;
3800 for (i = 0; i < args[0]->used; ++i) {
3801 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3802 continue;
3803 }
3804
3805 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3806 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3807 sleaf = (struct lysc_node_leaf *)leaf->schema;
3808 val = &leaf->value;
3809 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3810 /* uninteresting */
3811 continue;
3812 }
3813
3814 /* store args[1] as ident */
3815 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 +01003816 0, set->format, set->prefix_data, LYD_HINT_DATA, leaf->schema, &data, NULL, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003817 } else {
3818 meta = args[0]->val.meta[i].meta;
3819 val = &meta->value;
3820 if (val->realtype->basetype != LY_TYPE_IDENT) {
3821 /* uninteresting */
3822 continue;
3823 }
3824
3825 /* store args[1] as ident */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003826 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 +01003827 set->format, set->prefix_data, LYD_HINT_DATA, meta->parent->schema, &data, NULL, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003828 }
3829
3830 if (err) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01003831 ly_err_print(set->ctx, err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003832 ly_err_free(err);
3833 }
3834 LY_CHECK_RET(rc);
3835
3836 /* finally check the identity itself */
3837 if (self_match && (data.ident == val->ident)) {
3838 set_fill_boolean(set, 1);
3839 found = 1;
3840 }
3841 if (!found) {
3842 LY_ARRAY_FOR(data.ident->derived, u) {
3843 if (data.ident->derived[u] == val->ident) {
3844 set_fill_boolean(set, 1);
3845 found = 1;
3846 break;
3847 }
3848 }
3849 }
3850
3851 /* free temporary value */
3852 val->realtype->plugin->free(set->ctx, &data);
3853 if (found) {
3854 break;
3855 }
3856 }
3857
3858 return LY_SUCCESS;
3859}
3860
Michal Vasko03ff5a72019-09-11 13:49:33 +02003861/**
3862 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3863 * on whether the first argument nodes contain a node of an identity derived from the second
3864 * argument identity.
3865 *
3866 * @param[in] args Array of arguments.
3867 * @param[in] arg_count Count of elements in @p args.
3868 * @param[in,out] set Context and result set at the same time.
3869 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003870 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003871 */
3872static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003873xpath_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 +02003874{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003875 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003876}
3877
3878/**
3879 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3880 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3881 * the second argument identity.
3882 *
3883 * @param[in] args Array of arguments.
3884 * @param[in] arg_count Count of elements in @p args.
3885 * @param[in,out] set Context and result set at the same time.
3886 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003887 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003888 */
3889static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003890xpath_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 +02003891{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003892 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003893}
3894
3895/**
3896 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3897 * with the integer value of the first node's enum value, otherwise NaN.
3898 *
3899 * @param[in] args Array of arguments.
3900 * @param[in] arg_count Count of elements in @p args.
3901 * @param[in,out] set Context and result set at the same time.
3902 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003903 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003904 */
3905static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003906xpath_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 +02003907{
3908 struct lyd_node_term *leaf;
3909 struct lysc_node_leaf *sleaf;
3910 LY_ERR rc = LY_SUCCESS;
3911
3912 if (options & LYXP_SCNODE_ALL) {
3913 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3914 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003915 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3916 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 +02003917 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3918 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003919 }
3920 set_scnode_clear_ctx(set);
3921 return rc;
3922 }
3923
Michal Vaskod3678892020-05-21 10:06:58 +02003924 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003925 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003926 return LY_EVALID;
3927 }
3928
3929 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003930 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003931 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3932 sleaf = (struct lysc_node_leaf *)leaf->schema;
3933 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3934 set_fill_number(set, leaf->value.enum_item->value);
3935 }
3936 }
3937
3938 return LY_SUCCESS;
3939}
3940
3941/**
3942 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3943 * with false value.
3944 *
3945 * @param[in] args Array of arguments.
3946 * @param[in] arg_count Count of elements in @p args.
3947 * @param[in,out] set Context and result set at the same time.
3948 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003949 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003950 */
3951static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003952xpath_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 +02003953{
3954 if (options & LYXP_SCNODE_ALL) {
3955 set_scnode_clear_ctx(set);
3956 return LY_SUCCESS;
3957 }
3958
3959 set_fill_boolean(set, 0);
3960 return LY_SUCCESS;
3961}
3962
3963/**
3964 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3965 * with the first argument floored (truncated).
3966 *
3967 * @param[in] args Array of arguments.
3968 * @param[in] arg_count Count of elements in @p args.
3969 * @param[in,out] set Context and result set at the same time.
3970 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003971 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003972 */
3973static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003974xpath_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 +02003975{
3976 LY_ERR rc;
3977
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003978 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003979 LY_CHECK_RET(rc);
3980 if (isfinite(args[0]->val.num)) {
3981 set_fill_number(set, (long long)args[0]->val.num);
3982 }
3983
3984 return LY_SUCCESS;
3985}
3986
3987/**
3988 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3989 * whether the language of the text matches the one from the argument.
3990 *
3991 * @param[in] args Array of arguments.
3992 * @param[in] arg_count Count of elements in @p args.
3993 * @param[in,out] set Context and result set at the same time.
3994 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003995 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003996 */
3997static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003998xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003999{
4000 const struct lyd_node *node;
4001 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004002 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004003 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004004 LY_ERR rc = LY_SUCCESS;
4005
4006 if (options & LYXP_SCNODE_ALL) {
4007 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4008 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4009 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 +02004010 } else if (!warn_is_string_type(sleaf->type)) {
4011 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004012 }
4013 }
4014 set_scnode_clear_ctx(set);
4015 return rc;
4016 }
4017
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004018 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004019 LY_CHECK_RET(rc);
4020
Michal Vasko03ff5a72019-09-11 13:49:33 +02004021 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004022 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004023 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004024 } else if (!set->used) {
4025 set_fill_boolean(set, 0);
4026 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004027 }
4028
4029 switch (set->val.nodes[0].type) {
4030 case LYXP_NODE_ELEM:
4031 case LYXP_NODE_TEXT:
4032 node = set->val.nodes[0].node;
4033 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004034 case LYXP_NODE_META:
4035 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004036 break;
4037 default:
4038 /* nothing to do with roots */
4039 set_fill_boolean(set, 0);
4040 return LY_SUCCESS;
4041 }
4042
Michal Vasko9f96a052020-03-10 09:41:45 +01004043 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004044 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004045 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004046 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004047 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004048 break;
4049 }
4050 }
4051
Michal Vasko9f96a052020-03-10 09:41:45 +01004052 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004053 break;
4054 }
4055 }
4056
4057 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004058 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004059 set_fill_boolean(set, 0);
4060 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004061 uint64_t i;
4062
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004063 val = meta->value.canonical;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004064 for (i = 0; args[0]->val.str[i]; ++i) {
4065 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4066 set_fill_boolean(set, 0);
4067 break;
4068 }
4069 }
4070 if (!args[0]->val.str[i]) {
4071 if (!val[i] || (val[i] == '-')) {
4072 set_fill_boolean(set, 1);
4073 } else {
4074 set_fill_boolean(set, 0);
4075 }
4076 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004077 }
4078
4079 return LY_SUCCESS;
4080}
4081
4082/**
4083 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4084 * with the context size.
4085 *
4086 * @param[in] args Array of arguments.
4087 * @param[in] arg_count Count of elements in @p args.
4088 * @param[in,out] set Context and result set at the same time.
4089 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004090 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004091 */
4092static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004093xpath_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 +02004094{
4095 if (options & LYXP_SCNODE_ALL) {
4096 set_scnode_clear_ctx(set);
4097 return LY_SUCCESS;
4098 }
4099
Michal Vasko03ff5a72019-09-11 13:49:33 +02004100 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004101 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004102 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004103 } else if (!set->used) {
4104 set_fill_number(set, 0);
4105 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004106 }
4107
4108 set_fill_number(set, set->ctx_size);
4109 return LY_SUCCESS;
4110}
4111
4112/**
4113 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4114 * with the node name without namespace from the argument or the context.
4115 *
4116 * @param[in] args Array of arguments.
4117 * @param[in] arg_count Count of elements in @p args.
4118 * @param[in,out] set Context and result set at the same time.
4119 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004120 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004121 */
4122static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004123xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004124{
4125 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004126
Michal Vasko03ff5a72019-09-11 13:49:33 +02004127 /* suppress unused variable warning */
4128 (void)options;
4129
4130 if (options & LYXP_SCNODE_ALL) {
4131 set_scnode_clear_ctx(set);
4132 return LY_SUCCESS;
4133 }
4134
4135 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004136 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004137 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004138 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004139 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004140 } else if (!args[0]->used) {
4141 set_fill_string(set, "", 0);
4142 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004143 }
4144
4145 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004146 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004147
4148 item = &args[0]->val.nodes[0];
4149 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004150 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004151 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004152 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004153 } else if (!set->used) {
4154 set_fill_string(set, "", 0);
4155 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004156 }
4157
4158 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004159 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004160
4161 item = &set->val.nodes[0];
4162 }
4163
4164 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004165 case LYXP_NODE_NONE:
4166 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004167 case LYXP_NODE_ROOT:
4168 case LYXP_NODE_ROOT_CONFIG:
4169 case LYXP_NODE_TEXT:
4170 set_fill_string(set, "", 0);
4171 break;
4172 case LYXP_NODE_ELEM:
4173 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4174 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004175 case LYXP_NODE_META:
4176 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004177 break;
4178 }
4179
4180 return LY_SUCCESS;
4181}
4182
4183/**
4184 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4185 * with the node name fully qualified (with namespace) from the argument or the context.
4186 *
4187 * @param[in] args Array of arguments.
4188 * @param[in] arg_count Count of elements in @p args.
4189 * @param[in,out] set Context and result set at the same time.
4190 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004191 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004192 */
4193static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004194xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004195{
4196 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004197 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004198 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004199 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004200
4201 if (options & LYXP_SCNODE_ALL) {
4202 set_scnode_clear_ctx(set);
4203 return LY_SUCCESS;
4204 }
4205
4206 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004207 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004208 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004209 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004210 } else if (!args[0]->used) {
4211 set_fill_string(set, "", 0);
4212 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004213 }
4214
4215 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004216 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004217
4218 item = &args[0]->val.nodes[0];
4219 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004220 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004221 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004222 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004223 } else if (!set->used) {
4224 set_fill_string(set, "", 0);
4225 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004226 }
4227
4228 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004229 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004230
4231 item = &set->val.nodes[0];
4232 }
4233
4234 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004235 case LYXP_NODE_NONE:
4236 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004237 case LYXP_NODE_ROOT:
4238 case LYXP_NODE_ROOT_CONFIG:
4239 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004240 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004241 break;
4242 case LYXP_NODE_ELEM:
4243 mod = item->node->schema->module;
4244 name = item->node->schema->name;
4245 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004246 case LYXP_NODE_META:
4247 mod = ((struct lyd_meta *)item->node)->annotation->module;
4248 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004249 break;
4250 }
4251
4252 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004253 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004254 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4255 set_fill_string(set, str, strlen(str));
4256 free(str);
4257 } else {
4258 set_fill_string(set, "", 0);
4259 }
4260
4261 return LY_SUCCESS;
4262}
4263
4264/**
4265 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4266 * with the namespace of the node from the argument or the context.
4267 *
4268 * @param[in] args Array of arguments.
4269 * @param[in] arg_count Count of elements in @p args.
4270 * @param[in,out] set Context and result set at the same time.
4271 * @param[in] options XPath options.
4272 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4273 */
4274static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004275xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004276{
4277 struct lyxp_set_node *item;
4278 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004279
Michal Vasko03ff5a72019-09-11 13:49:33 +02004280 /* suppress unused variable warning */
4281 (void)options;
4282
4283 if (options & LYXP_SCNODE_ALL) {
4284 set_scnode_clear_ctx(set);
4285 return LY_SUCCESS;
4286 }
4287
4288 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004289 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004290 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004291 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004292 return LY_EVALID;
4293 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004294 set_fill_string(set, "", 0);
4295 return LY_SUCCESS;
4296 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004297
4298 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004299 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004300
4301 item = &args[0]->val.nodes[0];
4302 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004303 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004304 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004305 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004306 } else if (!set->used) {
4307 set_fill_string(set, "", 0);
4308 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004309 }
4310
4311 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004312 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004313
4314 item = &set->val.nodes[0];
4315 }
4316
4317 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004318 case LYXP_NODE_NONE:
4319 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004320 case LYXP_NODE_ROOT:
4321 case LYXP_NODE_ROOT_CONFIG:
4322 case LYXP_NODE_TEXT:
4323 set_fill_string(set, "", 0);
4324 break;
4325 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004326 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004327 if (item->type == LYXP_NODE_ELEM) {
4328 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004329 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004330 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004331 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004332 }
4333
4334 set_fill_string(set, mod->ns, strlen(mod->ns));
4335 break;
4336 }
4337
4338 return LY_SUCCESS;
4339}
4340
4341/**
4342 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4343 * with only nodes from the context. In practice it either leaves the context
4344 * as it is or returns an empty node set.
4345 *
4346 * @param[in] args Array of arguments.
4347 * @param[in] arg_count Count of elements in @p args.
4348 * @param[in,out] set Context and result set at the same time.
4349 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004350 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004351 */
4352static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004353xpath_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 +02004354{
4355 if (options & LYXP_SCNODE_ALL) {
4356 set_scnode_clear_ctx(set);
4357 return LY_SUCCESS;
4358 }
4359
4360 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004361 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004362 }
4363 return LY_SUCCESS;
4364}
4365
4366/**
4367 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4368 * with normalized value (no leading, trailing, double white spaces) of the node
4369 * from the argument or the context.
4370 *
4371 * @param[in] args Array of arguments.
4372 * @param[in] arg_count Count of elements in @p args.
4373 * @param[in,out] set Context and result set at the same time.
4374 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004375 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004376 */
4377static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004378xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004379{
4380 uint16_t i, new_used;
4381 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004382 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004383 struct lysc_node_leaf *sleaf;
4384 LY_ERR rc = LY_SUCCESS;
4385
4386 if (options & LYXP_SCNODE_ALL) {
4387 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4388 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4389 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 +02004390 } else if (!warn_is_string_type(sleaf->type)) {
4391 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004392 }
4393 }
4394 set_scnode_clear_ctx(set);
4395 return rc;
4396 }
4397
4398 if (arg_count) {
4399 set_fill_set(set, args[0]);
4400 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004401 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004402 LY_CHECK_RET(rc);
4403
4404 /* is there any normalization necessary? */
4405 for (i = 0; set->val.str[i]; ++i) {
4406 if (is_xmlws(set->val.str[i])) {
4407 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4408 have_spaces = 1;
4409 break;
4410 }
4411 space_before = 1;
4412 } else {
4413 space_before = 0;
4414 }
4415 }
4416
4417 /* yep, there is */
4418 if (have_spaces) {
4419 /* it's enough, at least one character will go, makes space for ending '\0' */
4420 new = malloc(strlen(set->val.str) * sizeof(char));
4421 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4422 new_used = 0;
4423
4424 space_before = 0;
4425 for (i = 0; set->val.str[i]; ++i) {
4426 if (is_xmlws(set->val.str[i])) {
4427 if ((i == 0) || space_before) {
4428 space_before = 1;
4429 continue;
4430 } else {
4431 space_before = 1;
4432 }
4433 } else {
4434 space_before = 0;
4435 }
4436
4437 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4438 ++new_used;
4439 }
4440
4441 /* at worst there is one trailing space now */
4442 if (new_used && is_xmlws(new[new_used - 1])) {
4443 --new_used;
4444 }
4445
4446 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4447 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4448 new[new_used] = '\0';
4449
4450 free(set->val.str);
4451 set->val.str = new;
4452 }
4453
4454 return LY_SUCCESS;
4455}
4456
4457/**
4458 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4459 * with the argument converted to boolean and logically inverted.
4460 *
4461 * @param[in] args Array of arguments.
4462 * @param[in] arg_count Count of elements in @p args.
4463 * @param[in,out] set Context and result set at the same time.
4464 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004465 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004466 */
4467static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004468xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004469{
4470 if (options & LYXP_SCNODE_ALL) {
4471 set_scnode_clear_ctx(set);
4472 return LY_SUCCESS;
4473 }
4474
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004475 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004476 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004477 set_fill_boolean(set, 0);
4478 } else {
4479 set_fill_boolean(set, 1);
4480 }
4481
4482 return LY_SUCCESS;
4483}
4484
4485/**
4486 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4487 * with the number representation of either the argument or the context.
4488 *
4489 * @param[in] args Array of arguments.
4490 * @param[in] arg_count Count of elements in @p args.
4491 * @param[in,out] set Context and result set at the same time.
4492 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004493 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004494 */
4495static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004496xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004497{
4498 LY_ERR rc;
4499
4500 if (options & LYXP_SCNODE_ALL) {
4501 set_scnode_clear_ctx(set);
4502 return LY_SUCCESS;
4503 }
4504
4505 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004506 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004507 LY_CHECK_RET(rc);
4508 set_fill_set(set, args[0]);
4509 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004510 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004511 LY_CHECK_RET(rc);
4512 }
4513
4514 return LY_SUCCESS;
4515}
4516
4517/**
4518 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4519 * with the context position.
4520 *
4521 * @param[in] args Array of arguments.
4522 * @param[in] arg_count Count of elements in @p args.
4523 * @param[in,out] set Context and result set at the same time.
4524 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004525 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004526 */
4527static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004528xpath_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 +02004529{
4530 if (options & LYXP_SCNODE_ALL) {
4531 set_scnode_clear_ctx(set);
4532 return LY_SUCCESS;
4533 }
4534
Michal Vasko03ff5a72019-09-11 13:49:33 +02004535 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004536 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004537 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004538 } else if (!set->used) {
4539 set_fill_number(set, 0);
4540 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004541 }
4542
4543 set_fill_number(set, set->ctx_pos);
4544
4545 /* UNUSED in 'Release' build type */
4546 (void)options;
4547 return LY_SUCCESS;
4548}
4549
4550/**
4551 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4552 * depending on whether the second argument regex matches the first argument string. For details refer to
4553 * YANG 1.1 RFC section 10.2.1.
4554 *
4555 * @param[in] args Array of arguments.
4556 * @param[in] arg_count Count of elements in @p args.
4557 * @param[in,out] set Context and result set at the same time.
4558 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004559 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004560 */
4561static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004562xpath_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 +02004563{
4564 struct lysc_pattern **patterns = NULL, **pattern;
4565 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004566 LY_ERR rc = LY_SUCCESS;
4567 struct ly_err_item *err;
4568
4569 if (options & LYXP_SCNODE_ALL) {
4570 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4571 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4572 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 +02004573 } else if (!warn_is_string_type(sleaf->type)) {
4574 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004575 }
4576 }
4577
4578 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4579 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4580 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 +02004581 } else if (!warn_is_string_type(sleaf->type)) {
4582 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004583 }
4584 }
4585 set_scnode_clear_ctx(set);
4586 return rc;
4587 }
4588
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004589 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004590 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004591 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004592 LY_CHECK_RET(rc);
4593
4594 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek IÅ¡a45802b52021-02-09 09:21:58 +01004595 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004596 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004597 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Radek Krejciddace2c2021-01-08 11:30:56 +01004598 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004599 if (rc != LY_SUCCESS) {
4600 LY_ARRAY_FREE(patterns);
4601 return rc;
4602 }
4603
4604 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4605 pcre2_code_free((*pattern)->code);
4606 free(*pattern);
4607 LY_ARRAY_FREE(patterns);
4608 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004609 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004610 ly_err_free(err);
4611 return rc;
4612 }
4613
4614 if (rc == LY_EVALID) {
4615 ly_err_free(err);
4616 set_fill_boolean(set, 0);
4617 } else {
4618 set_fill_boolean(set, 1);
4619 }
4620
4621 return LY_SUCCESS;
4622}
4623
4624/**
4625 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4626 * with the rounded first argument. For details refer to
4627 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4628 *
4629 * @param[in] args Array of arguments.
4630 * @param[in] arg_count Count of elements in @p args.
4631 * @param[in,out] set Context and result set at the same time.
4632 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004633 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004634 */
4635static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004636xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004637{
4638 struct lysc_node_leaf *sleaf;
4639 LY_ERR rc = LY_SUCCESS;
4640
4641 if (options & LYXP_SCNODE_ALL) {
4642 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4643 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004644 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4645 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 +02004646 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4647 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004648 }
4649 set_scnode_clear_ctx(set);
4650 return rc;
4651 }
4652
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004653 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004654 LY_CHECK_RET(rc);
4655
4656 /* cover only the cases where floor can't be used */
4657 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4658 set_fill_number(set, -0.0f);
4659 } else {
4660 args[0]->val.num += 0.5;
4661 rc = xpath_floor(args, 1, args[0], options);
4662 LY_CHECK_RET(rc);
4663 set_fill_number(set, args[0]->val.num);
4664 }
4665
4666 return LY_SUCCESS;
4667}
4668
4669/**
4670 * @brief Execute the XPath starts-with(string, string) function.
4671 * Returns LYXP_SET_BOOLEAN whether the second argument is
4672 * the prefix of the first or not.
4673 *
4674 * @param[in] args Array of arguments.
4675 * @param[in] arg_count Count of elements in @p args.
4676 * @param[in,out] set Context and result set at the same time.
4677 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004678 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004679 */
4680static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004681xpath_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 +02004682{
4683 struct lysc_node_leaf *sleaf;
4684 LY_ERR rc = LY_SUCCESS;
4685
4686 if (options & LYXP_SCNODE_ALL) {
4687 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4688 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4689 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 +02004690 } else if (!warn_is_string_type(sleaf->type)) {
4691 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004692 }
4693 }
4694
4695 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4696 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4697 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 +02004698 } else if (!warn_is_string_type(sleaf->type)) {
4699 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004700 }
4701 }
4702 set_scnode_clear_ctx(set);
4703 return rc;
4704 }
4705
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004706 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004707 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004708 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004709 LY_CHECK_RET(rc);
4710
4711 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4712 set_fill_boolean(set, 0);
4713 } else {
4714 set_fill_boolean(set, 1);
4715 }
4716
4717 return LY_SUCCESS;
4718}
4719
4720/**
4721 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4722 * with the string representation of either the argument or the context.
4723 *
4724 * @param[in] args Array of arguments.
4725 * @param[in] arg_count Count of elements in @p args.
4726 * @param[in,out] set Context and result set at the same time.
4727 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004728 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004729 */
4730static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004731xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004732{
4733 LY_ERR rc;
4734
4735 if (options & LYXP_SCNODE_ALL) {
4736 set_scnode_clear_ctx(set);
4737 return LY_SUCCESS;
4738 }
4739
4740 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004741 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004742 LY_CHECK_RET(rc);
4743 set_fill_set(set, args[0]);
4744 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004745 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004746 LY_CHECK_RET(rc);
4747 }
4748
4749 return LY_SUCCESS;
4750}
4751
4752/**
4753 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4754 * with the length of the string in either the argument or the context.
4755 *
4756 * @param[in] args Array of arguments.
4757 * @param[in] arg_count Count of elements in @p args.
4758 * @param[in,out] set Context and result set at the same time.
4759 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004760 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004761 */
4762static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004763xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004764{
4765 struct lysc_node_leaf *sleaf;
4766 LY_ERR rc = LY_SUCCESS;
4767
4768 if (options & LYXP_SCNODE_ALL) {
4769 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4770 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4771 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 +02004772 } else if (!warn_is_string_type(sleaf->type)) {
4773 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004774 }
4775 }
4776 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4777 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4778 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 +02004779 } else if (!warn_is_string_type(sleaf->type)) {
4780 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004781 }
4782 }
4783 set_scnode_clear_ctx(set);
4784 return rc;
4785 }
4786
4787 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004788 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004789 LY_CHECK_RET(rc);
4790 set_fill_number(set, strlen(args[0]->val.str));
4791 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004792 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004793 LY_CHECK_RET(rc);
4794 set_fill_number(set, strlen(set->val.str));
4795 }
4796
4797 return LY_SUCCESS;
4798}
4799
4800/**
4801 * @brief Execute the XPath substring(string, number, number?) function.
4802 * Returns LYXP_SET_STRING substring of the first argument starting
4803 * on the second argument index ending on the third argument index,
4804 * indexed from 1. For exact definition refer to
4805 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4806 *
4807 * @param[in] args Array of arguments.
4808 * @param[in] arg_count Count of elements in @p args.
4809 * @param[in,out] set Context and result set at the same time.
4810 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004811 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004812 */
4813static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004814xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004815{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004816 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004817 uint16_t str_start, str_len, pos;
4818 struct lysc_node_leaf *sleaf;
4819 LY_ERR rc = LY_SUCCESS;
4820
4821 if (options & LYXP_SCNODE_ALL) {
4822 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4823 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4824 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 +02004825 } else if (!warn_is_string_type(sleaf->type)) {
4826 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004827 }
4828 }
4829
4830 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4831 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4832 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 +02004833 } else if (!warn_is_numeric_type(sleaf->type)) {
4834 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004835 }
4836 }
4837
Michal Vasko69730152020-10-09 16:30:07 +02004838 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4839 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004840 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4841 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 +02004842 } else if (!warn_is_numeric_type(sleaf->type)) {
4843 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004844 }
4845 }
4846 set_scnode_clear_ctx(set);
4847 return rc;
4848 }
4849
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004850 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004851 LY_CHECK_RET(rc);
4852
4853 /* start */
4854 if (xpath_round(&args[1], 1, args[1], options)) {
4855 return -1;
4856 }
4857 if (isfinite(args[1]->val.num)) {
4858 start = args[1]->val.num - 1;
4859 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004860 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004861 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004862 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004863 }
4864
4865 /* len */
4866 if (arg_count == 3) {
4867 rc = xpath_round(&args[2], 1, args[2], options);
4868 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004869 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004870 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004871 } else if (isfinite(args[2]->val.num)) {
4872 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004873 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004874 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004875 }
4876 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004877 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004878 }
4879
4880 /* find matching character positions */
4881 str_start = 0;
4882 str_len = 0;
4883 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4884 if (pos < start) {
4885 ++str_start;
4886 } else if (pos < start + len) {
4887 ++str_len;
4888 } else {
4889 break;
4890 }
4891 }
4892
4893 set_fill_string(set, args[0]->val.str + str_start, str_len);
4894 return LY_SUCCESS;
4895}
4896
4897/**
4898 * @brief Execute the XPath substring-after(string, string) function.
4899 * Returns LYXP_SET_STRING with the string succeeding the occurance
4900 * of the second argument in the first or an empty string.
4901 *
4902 * @param[in] args Array of arguments.
4903 * @param[in] arg_count Count of elements in @p args.
4904 * @param[in,out] set Context and result set at the same time.
4905 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004906 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004907 */
4908static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004909xpath_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 +02004910{
4911 char *ptr;
4912 struct lysc_node_leaf *sleaf;
4913 LY_ERR rc = LY_SUCCESS;
4914
4915 if (options & LYXP_SCNODE_ALL) {
4916 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4917 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4918 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 +02004919 } else if (!warn_is_string_type(sleaf->type)) {
4920 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004921 }
4922 }
4923
4924 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4925 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4926 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 +02004927 } else if (!warn_is_string_type(sleaf->type)) {
4928 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004929 }
4930 }
4931 set_scnode_clear_ctx(set);
4932 return rc;
4933 }
4934
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004935 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004936 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004937 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004938 LY_CHECK_RET(rc);
4939
4940 ptr = strstr(args[0]->val.str, args[1]->val.str);
4941 if (ptr) {
4942 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4943 } else {
4944 set_fill_string(set, "", 0);
4945 }
4946
4947 return LY_SUCCESS;
4948}
4949
4950/**
4951 * @brief Execute the XPath substring-before(string, string) function.
4952 * Returns LYXP_SET_STRING with the string preceding the occurance
4953 * of the second argument in the first or an empty string.
4954 *
4955 * @param[in] args Array of arguments.
4956 * @param[in] arg_count Count of elements in @p args.
4957 * @param[in,out] set Context and result set at the same time.
4958 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004959 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004960 */
4961static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004962xpath_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 +02004963{
4964 char *ptr;
4965 struct lysc_node_leaf *sleaf;
4966 LY_ERR rc = LY_SUCCESS;
4967
4968 if (options & LYXP_SCNODE_ALL) {
4969 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4970 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4971 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 +02004972 } else if (!warn_is_string_type(sleaf->type)) {
4973 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004974 }
4975 }
4976
4977 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4978 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4979 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 +02004980 } else if (!warn_is_string_type(sleaf->type)) {
4981 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004982 }
4983 }
4984 set_scnode_clear_ctx(set);
4985 return rc;
4986 }
4987
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004988 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004989 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004990 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004991 LY_CHECK_RET(rc);
4992
4993 ptr = strstr(args[0]->val.str, args[1]->val.str);
4994 if (ptr) {
4995 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4996 } else {
4997 set_fill_string(set, "", 0);
4998 }
4999
5000 return LY_SUCCESS;
5001}
5002
5003/**
5004 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5005 * with the sum of all the nodes in the context.
5006 *
5007 * @param[in] args Array of arguments.
5008 * @param[in] arg_count Count of elements in @p args.
5009 * @param[in,out] set Context and result set at the same time.
5010 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005011 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005012 */
5013static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005014xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005015{
5016 long double num;
5017 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005018 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005019 struct lyxp_set set_item;
5020 struct lysc_node_leaf *sleaf;
5021 LY_ERR rc = LY_SUCCESS;
5022
5023 if (options & LYXP_SCNODE_ALL) {
5024 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5025 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005026 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005027 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5028 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5029 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005030 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005031 } else if (!warn_is_numeric_type(sleaf->type)) {
5032 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005033 }
5034 }
5035 }
5036 }
5037 set_scnode_clear_ctx(set);
5038 return rc;
5039 }
5040
5041 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005042
5043 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005044 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005045 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005046 } else if (!args[0]->used) {
5047 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005048 }
5049
Michal Vasko5c4e5892019-11-14 12:31:38 +01005050 set_init(&set_item, set);
5051
Michal Vasko03ff5a72019-09-11 13:49:33 +02005052 set_item.type = LYXP_SET_NODE_SET;
5053 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5054 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5055
5056 set_item.used = 1;
5057 set_item.size = 1;
5058
5059 for (i = 0; i < args[0]->used; ++i) {
5060 set_item.val.nodes[0] = args[0]->val.nodes[i];
5061
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005062 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005063 LY_CHECK_RET(rc);
5064 num = cast_string_to_number(str);
5065 free(str);
5066 set->val.num += num;
5067 }
5068
5069 free(set_item.val.nodes);
5070
5071 return LY_SUCCESS;
5072}
5073
5074/**
5075 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5076 * with the text content of the nodes in the context.
5077 *
5078 * @param[in] args Array of arguments.
5079 * @param[in] arg_count Count of elements in @p args.
5080 * @param[in,out] set Context and result set at the same time.
5081 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005082 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005083 */
5084static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005085xpath_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 +02005086{
5087 uint32_t i;
5088
5089 if (options & LYXP_SCNODE_ALL) {
5090 set_scnode_clear_ctx(set);
5091 return LY_SUCCESS;
5092 }
5093
Michal Vasko03ff5a72019-09-11 13:49:33 +02005094 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005095 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005096 return LY_EVALID;
5097 }
5098
Michal Vaskod989ba02020-08-24 10:59:24 +02005099 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005100 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005101 case LYXP_NODE_NONE:
5102 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005103 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005104 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5105 set->val.nodes[i].type = LYXP_NODE_TEXT;
5106 ++i;
5107 break;
5108 }
Radek Krejci0f969882020-08-21 16:56:47 +02005109 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005110 case LYXP_NODE_ROOT:
5111 case LYXP_NODE_ROOT_CONFIG:
5112 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005113 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005114 set_remove_node(set, i);
5115 break;
5116 }
5117 }
5118
5119 return LY_SUCCESS;
5120}
5121
5122/**
5123 * @brief Execute the XPath translate(string, string, string) function.
5124 * Returns LYXP_SET_STRING with the first argument with the characters
5125 * from the second argument replaced by those on the corresponding
5126 * positions in the third argument.
5127 *
5128 * @param[in] args Array of arguments.
5129 * @param[in] arg_count Count of elements in @p args.
5130 * @param[in,out] set Context and result set at the same time.
5131 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005132 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005133 */
5134static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005135xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005136{
5137 uint16_t i, j, new_used;
5138 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005139 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005140 struct lysc_node_leaf *sleaf;
5141 LY_ERR rc = LY_SUCCESS;
5142
5143 if (options & LYXP_SCNODE_ALL) {
5144 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5145 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5146 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 +02005147 } else if (!warn_is_string_type(sleaf->type)) {
5148 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005149 }
5150 }
5151
5152 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5153 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5154 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 +02005155 } else if (!warn_is_string_type(sleaf->type)) {
5156 LOGWRN(set->ctx, "Argument #2 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[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5161 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5162 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 +02005163 } else if (!warn_is_string_type(sleaf->type)) {
5164 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005165 }
5166 }
5167 set_scnode_clear_ctx(set);
5168 return rc;
5169 }
5170
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005171 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005172 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005173 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005174 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005175 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005176 LY_CHECK_RET(rc);
5177
5178 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5179 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5180 new_used = 0;
5181
5182 have_removed = 0;
5183 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005184 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005185
5186 for (j = 0; args[1]->val.str[j]; ++j) {
5187 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5188 /* removing this char */
5189 if (j >= strlen(args[2]->val.str)) {
5190 have_removed = 1;
5191 found = 1;
5192 break;
5193 }
5194 /* replacing this char */
5195 new[new_used] = args[2]->val.str[j];
5196 ++new_used;
5197 found = 1;
5198 break;
5199 }
5200 }
5201
5202 /* copying this char */
5203 if (!found) {
5204 new[new_used] = args[0]->val.str[i];
5205 ++new_used;
5206 }
5207 }
5208
5209 if (have_removed) {
5210 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5211 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5212 }
5213 new[new_used] = '\0';
5214
Michal Vaskod3678892020-05-21 10:06:58 +02005215 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005216 set->type = LYXP_SET_STRING;
5217 set->val.str = new;
5218
5219 return LY_SUCCESS;
5220}
5221
5222/**
5223 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5224 * with true value.
5225 *
5226 * @param[in] args Array of arguments.
5227 * @param[in] arg_count Count of elements in @p args.
5228 * @param[in,out] set Context and result set at the same time.
5229 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005230 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005231 */
5232static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005233xpath_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 +02005234{
5235 if (options & LYXP_SCNODE_ALL) {
5236 set_scnode_clear_ctx(set);
5237 return LY_SUCCESS;
5238 }
5239
5240 set_fill_boolean(set, 1);
5241 return LY_SUCCESS;
5242}
5243
5244/*
5245 * moveto functions
5246 *
5247 * They and only they actually change the context (set).
5248 */
5249
5250/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005251 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005252 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005253 * XPath @p set is expected to be a (sc)node set!
5254 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005255 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5256 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005257 * @param[in] set Set with general XPath context.
5258 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005259 * @param[out] moveto_mod Expected module of a matching node.
5260 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005261 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005262static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005263moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5264 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005265{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005266 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005267 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005268 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005269
Michal Vasko2104e9f2020-03-06 08:23:25 +01005270 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5271
Michal Vasko6346ece2019-09-24 13:12:53 +02005272 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5273 /* specific module */
5274 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005275 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005276
Michal Vasko004d3152020-06-11 19:59:22 +02005277 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005278 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005279 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005280 return LY_EVALID;
5281 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005282
Michal Vasko6346ece2019-09-24 13:12:53 +02005283 *qname += pref_len + 1;
5284 *qname_len -= pref_len + 1;
5285 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5286 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005287 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005288 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005289 switch (set->format) {
5290 case LY_PREF_SCHEMA:
5291 case LY_PREF_SCHEMA_RESOLVED:
5292 /* current module */
5293 mod = set->cur_mod;
5294 break;
5295 case LY_PREF_JSON:
5296 /* inherit parent (context node) module */
5297 if (ctx_scnode) {
5298 mod = ctx_scnode->module;
5299 } else {
5300 mod = NULL;
5301 }
5302 break;
5303 case LY_PREF_XML:
5304 /* not defined */
5305 LOGINT_RET(set->ctx);
5306 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005307 }
5308
Michal Vasko6346ece2019-09-24 13:12:53 +02005309 *moveto_mod = mod;
5310 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005311}
5312
5313/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005314 * @brief Move context @p set to the root. Handles absolute path.
5315 * Result is LYXP_SET_NODE_SET.
5316 *
5317 * @param[in,out] set Set to use.
5318 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005319 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005320 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005321static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005322moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005323{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005324 if (!set) {
Michal Vaskob0099a92020-08-31 14:55:23 +02005325 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005326 }
5327
5328 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005329 set_scnode_clear_ctx(set);
Michal Vaskob0099a92020-08-31 14:55:23 +02005330 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005331 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005332 set->type = LYXP_SET_NODE_SET;
5333 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005334 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005335 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005336
5337 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005338}
5339
5340/**
5341 * @brief Check @p node as a part of NameTest processing.
5342 *
5343 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005344 * @param[in] ctx_node Context node.
5345 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005346 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005347 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005348 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005349 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5350 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005351 */
5352static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005353moveto_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 +01005354 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005355{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005356 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5357 switch (set->format) {
5358 case LY_PREF_SCHEMA:
5359 case LY_PREF_SCHEMA_RESOLVED:
5360 /* use current module */
5361 moveto_mod = set->cur_mod;
5362 break;
5363 case LY_PREF_JSON:
5364 /* inherit module of the context node, if any */
5365 if (ctx_node) {
5366 moveto_mod = ctx_node->schema->module;
5367 }
5368 break;
5369 case LY_PREF_XML:
5370 /* not defined */
5371 LOGINT(set->ctx);
5372 return LY_EINVAL;
5373 }
5374 }
5375
Michal Vasko03ff5a72019-09-11 13:49:33 +02005376 /* module check */
5377 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005378 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005379 }
5380
Michal Vasko5c4e5892019-11-14 12:31:38 +01005381 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005382 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005383 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005384 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5385 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005386 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005387 }
5388
5389 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005390 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005391 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005392 }
5393
Michal Vaskoa1424542019-11-14 16:08:52 +01005394 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005395 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005396 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005397 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005398
5399 /* match */
5400 return LY_SUCCESS;
5401}
5402
5403/**
5404 * @brief Check @p node as a part of schema NameTest processing.
5405 *
5406 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005407 * @param[in] ctx_scnode Context node.
5408 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005409 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005410 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005411 * @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 +02005412 */
5413static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005414moveto_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 +02005415 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005416{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005417 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5418 switch (set->format) {
5419 case LY_PREF_SCHEMA:
5420 case LY_PREF_SCHEMA_RESOLVED:
5421 /* use current module */
5422 moveto_mod = set->cur_mod;
5423 break;
5424 case LY_PREF_JSON:
5425 /* inherit module of the context node, if any */
5426 if (ctx_scnode) {
5427 moveto_mod = ctx_scnode->module;
5428 }
5429 break;
5430 case LY_PREF_XML:
5431 /* not defined */
5432 LOGINT(set->ctx);
5433 return LY_EINVAL;
5434 }
5435 }
5436
Michal Vasko03ff5a72019-09-11 13:49:33 +02005437 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005438 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005439 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005440 }
5441
5442 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005443 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005444 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005445 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005446 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005447 }
5448
5449 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005450 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005451 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005452 }
5453
5454 /* match */
5455 return LY_SUCCESS;
5456}
5457
5458/**
Michal Vaskod3678892020-05-21 10:06:58 +02005459 * @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 +02005460 *
5461 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005462 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005463 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005464 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005465 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5466 */
5467static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005468moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005469{
Michal Vaskof03ed032020-03-04 13:31:44 +01005470 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005471 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005472 LY_ERR rc;
5473
Michal Vaskod3678892020-05-21 10:06:58 +02005474 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005475 return LY_SUCCESS;
5476 }
5477
5478 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005479 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005480 return LY_EVALID;
5481 }
5482
Michal Vasko03ff5a72019-09-11 13:49:33 +02005483 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005484 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005485
5486 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 +01005487 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005488
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005489 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005490 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005491 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005492 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005493 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005494 ctx_node = set->val.nodes[i].node;
5495 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005496 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005497
Michal Vaskod3678892020-05-21 10:06:58 +02005498 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005499 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005500 if (rc == LY_SUCCESS) {
5501 if (!replaced) {
5502 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5503 replaced = 1;
5504 } else {
5505 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005506 }
Michal Vaskod3678892020-05-21 10:06:58 +02005507 ++i;
5508 } else if (rc == LY_EINCOMPLETE) {
5509 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005510 }
5511 }
5512
5513 if (!replaced) {
5514 /* no match */
5515 set_remove_node(set, i);
5516 }
5517 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005518
5519 return LY_SUCCESS;
5520}
5521
5522/**
Michal Vaskod3678892020-05-21 10:06:58 +02005523 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5524 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005525 *
5526 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005527 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005528 * @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 +01005529 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005530 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5531 */
5532static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005533moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5534 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005535{
Michal Vasko004d3152020-06-11 19:59:22 +02005536 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005537 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005538 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005539 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005540
Michal Vasko004d3152020-06-11 19:59:22 +02005541 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005542
5543 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005544 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005545 }
5546
5547 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005548 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005549 ret = LY_EVALID;
5550 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005551 }
5552
5553 /* context check for all the nodes since we have the schema node */
5554 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5555 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005556 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005557 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5558 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005559 lyxp_set_free_content(set);
5560 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005561 }
5562
5563 /* create specific data instance if needed */
5564 if (scnode->nodetype == LYS_LIST) {
5565 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5566 } else if (scnode->nodetype == LYS_LEAFLIST) {
5567 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005568 }
5569
5570 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005571 siblings = NULL;
5572
5573 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5574 assert(!set->val.nodes[i].node);
5575
5576 /* search in all the trees */
5577 siblings = set->tree;
5578 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5579 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005580 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005581 }
5582
5583 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005584 if (inst) {
5585 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005586 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005587 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005588 }
5589
5590 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005591 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005592 ret = LY_EINCOMPLETE;
5593 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005594 }
5595
5596 if (sub) {
5597 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005598 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005599 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005600 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005601 /* no match */
5602 set_remove_node(set, i);
5603 }
5604 }
5605
Michal Vasko004d3152020-06-11 19:59:22 +02005606cleanup:
5607 lyd_free_tree(inst);
5608 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005609}
5610
5611/**
5612 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5613 *
5614 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005615 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005616 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005617 * @param[in] options XPath options.
5618 * @return LY_ERR
5619 */
5620static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005621moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005622{
Radek Krejci857189e2020-09-01 13:26:36 +02005623 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005624 uint32_t getnext_opts;
5625 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005626 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005627 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005628 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005629
Michal Vaskod3678892020-05-21 10:06:58 +02005630 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005631 return LY_SUCCESS;
5632 }
5633
5634 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005635 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005636 return LY_EVALID;
5637 }
5638
Michal Vaskocafad9d2019-11-07 15:20:03 +01005639 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005640 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005641 if (options & LYXP_SCNODE_OUTPUT) {
5642 getnext_opts |= LYS_GETNEXT_OUTPUT;
5643 }
5644
Michal Vasko03ff5a72019-09-11 13:49:33 +02005645 orig_used = set->used;
5646 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005647 uint32_t idx;
5648
Radek Krejcif13b87b2020-12-01 22:02:17 +01005649 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5650 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005651 continue;
5652 }
5653
5654 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005655 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005656 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005657 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005658 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005659
5660 start_parent = set->val.scnodes[i].scnode;
5661
5662 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 +02005663 /* 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 +02005664 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005665 mod_idx = 0;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005666 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005667 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005668 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005669 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005670 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005671 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5672
Michal Vasko03ff5a72019-09-11 13:49:33 +02005673 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005674 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005675 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005676 temp_ctx = 1;
5677 }
5678 }
5679 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005680 }
5681
Michal Vasko519fd602020-05-26 12:17:39 +02005682 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5683 iter = NULL;
5684 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005685 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005686 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5687
5688 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005689 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005690 temp_ctx = 1;
5691 }
5692 }
5693 }
5694 }
5695 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005696
5697 /* correct temporary in_ctx values */
5698 if (temp_ctx) {
5699 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005700 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5701 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005702 }
5703 }
5704 }
5705
5706 return LY_SUCCESS;
5707}
5708
5709/**
Michal Vaskod3678892020-05-21 10:06:58 +02005710 * @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 +02005711 * Context position aware.
5712 *
5713 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005714 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005715 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005716 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005717 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5718 */
5719static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005720moveto_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 +02005721{
5722 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005723 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005724 struct lyxp_set ret_set;
5725 LY_ERR rc;
5726
Michal Vaskod3678892020-05-21 10:06:58 +02005727 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005728 return LY_SUCCESS;
5729 }
5730
5731 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005732 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005733 return LY_EVALID;
5734 }
5735
Michal Vasko9f96a052020-03-10 09:41:45 +01005736 /* 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 +01005737 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005738 LY_CHECK_RET(rc);
5739
Michal Vasko6346ece2019-09-24 13:12:53 +02005740 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005741 set_init(&ret_set, set);
5742 for (i = 0; i < set->used; ++i) {
5743
5744 /* TREE DFS */
5745 start = set->val.nodes[i].node;
5746 for (elem = next = start; elem; elem = next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005747 rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005748 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005749 /* add matching node into result set */
5750 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5751 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5752 /* the node is a duplicate, we'll process it later in the set */
5753 goto skip_children;
5754 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005755 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005756 return rc;
5757 } else if (rc == LY_EINVAL) {
5758 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005759 }
5760
5761 /* TREE DFS NEXT ELEM */
5762 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005763 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005764 if (!next) {
5765skip_children:
5766 /* no children, so try siblings, but only if it's not the start,
5767 * that is considered to be the root and it's siblings are not traversed */
5768 if (elem != start) {
5769 next = elem->next;
5770 } else {
5771 break;
5772 }
5773 }
5774 while (!next) {
5775 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01005776 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005777 /* we are done, no next element to process */
5778 break;
5779 }
5780 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01005781 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005782 next = elem->next;
5783 }
5784 }
5785 }
5786
5787 /* make the temporary set the current one */
5788 ret_set.ctx_pos = set->ctx_pos;
5789 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005790 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005791 memcpy(set, &ret_set, sizeof *set);
5792
5793 return LY_SUCCESS;
5794}
5795
5796/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005797 * @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 +02005798 *
5799 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005800 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005801 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005802 * @param[in] options XPath options.
5803 * @return LY_ERR
5804 */
5805static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005806moveto_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 +02005807{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005808 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005809 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005810 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005811
Michal Vaskod3678892020-05-21 10:06:58 +02005812 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005813 return LY_SUCCESS;
5814 }
5815
5816 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005817 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005818 return LY_EVALID;
5819 }
5820
Michal Vasko03ff5a72019-09-11 13:49:33 +02005821 orig_used = set->used;
5822 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005823 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5824 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005825 continue;
5826 }
5827
5828 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005829 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005830 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005831 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005832 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005833
5834 /* TREE DFS */
5835 start = set->val.scnodes[i].scnode;
5836 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005837 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5838 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005839 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005840 }
5841
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005842 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005843 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005844 uint32_t idx;
5845
5846 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005847 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005848 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005849 /* we will process it later in the set */
5850 goto skip_children;
5851 }
5852 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005853 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005854 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005855 } else if (rc == LY_EINVAL) {
5856 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005857 }
5858
5859next_iter:
5860 /* TREE DFS NEXT ELEM */
5861 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005862 next = lysc_node_child(elem);
5863 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5864 next = next->next;
5865 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5866 next = next->next;
5867 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005868 if (!next) {
5869skip_children:
5870 /* no children, so try siblings, but only if it's not the start,
5871 * that is considered to be the root and it's siblings are not traversed */
5872 if (elem != start) {
5873 next = elem->next;
5874 } else {
5875 break;
5876 }
5877 }
5878 while (!next) {
5879 /* no siblings, go back through the parents */
5880 if (elem->parent == start) {
5881 /* we are done, no next element to process */
5882 break;
5883 }
5884 /* parent is already processed, go to its sibling */
5885 elem = elem->parent;
5886 next = elem->next;
5887 }
5888 }
5889 }
5890
5891 return LY_SUCCESS;
5892}
5893
5894/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005895 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005896 * Indirectly context position aware.
5897 *
5898 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005899 * @param[in] mod Matching metadata module, NULL for any.
5900 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005901 * @return LY_ERR
5902 */
5903static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005904moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005905{
Michal Vasko9f96a052020-03-10 09:41:45 +01005906 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005907
Michal Vaskod3678892020-05-21 10:06:58 +02005908 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005909 return LY_SUCCESS;
5910 }
5911
5912 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005913 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005914 return LY_EVALID;
5915 }
5916
Radek Krejci1deb5be2020-08-26 16:43:36 +02005917 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005918 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005919
5920 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5921 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005922 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005923 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005924
5925 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005926 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005927 continue;
5928 }
5929
Michal Vaskod3678892020-05-21 10:06:58 +02005930 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005931 /* match */
5932 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005933 set->val.meta[i].meta = sub;
5934 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005935 /* pos does not change */
5936 replaced = 1;
5937 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005938 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 +02005939 }
5940 ++i;
5941 }
5942 }
5943 }
5944
5945 if (!replaced) {
5946 /* no match */
5947 set_remove_node(set, i);
5948 }
5949 }
5950
5951 return LY_SUCCESS;
5952}
5953
5954/**
5955 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005956 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005957 *
5958 * @param[in,out] set1 Set to use for the result.
5959 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005960 * @return LY_ERR
5961 */
5962static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005963moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005964{
5965 LY_ERR rc;
5966
Michal Vaskod3678892020-05-21 10:06:58 +02005967 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005968 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005969 return LY_EVALID;
5970 }
5971
5972 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005973 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005974 return LY_SUCCESS;
5975 }
5976
Michal Vaskod3678892020-05-21 10:06:58 +02005977 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005978 memcpy(set1, set2, sizeof *set1);
5979 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005980 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005981 return LY_SUCCESS;
5982 }
5983
5984 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005985 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005986
5987 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005988 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005989 LY_CHECK_RET(rc);
5990
5991 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005992 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005993
5994 return LY_SUCCESS;
5995}
5996
5997/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005998 * @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 +02005999 * Context position aware.
6000 *
6001 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006002 * @param[in] mod Matching metadata module, NULL for any.
6003 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006004 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006005 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6006 */
6007static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006008moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006009{
Michal Vasko9f96a052020-03-10 09:41:45 +01006010 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006011 struct lyxp_set *set_all_desc = NULL;
6012 LY_ERR rc;
6013
Michal Vaskod3678892020-05-21 10:06:58 +02006014 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006015 return LY_SUCCESS;
6016 }
6017
6018 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006019 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006020 return LY_EVALID;
6021 }
6022
Michal Vasko03ff5a72019-09-11 13:49:33 +02006023 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6024 * but it likely won't be used much, so it's a waste of time */
6025 /* copy the context */
6026 set_all_desc = set_copy(set);
6027 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006028 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006029 if (rc != LY_SUCCESS) {
6030 lyxp_set_free(set_all_desc);
6031 return rc;
6032 }
6033 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006034 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006035 if (rc != LY_SUCCESS) {
6036 lyxp_set_free(set_all_desc);
6037 return rc;
6038 }
6039 lyxp_set_free(set_all_desc);
6040
Radek Krejci1deb5be2020-08-26 16:43:36 +02006041 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006042 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006043
6044 /* only attributes of an elem can be in the result, skip all the rest,
6045 * we have all attributes qualified in lyd tree */
6046 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006047 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006048 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006049 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006050 continue;
6051 }
6052
Michal Vaskod3678892020-05-21 10:06:58 +02006053 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006054 /* match */
6055 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006056 set->val.meta[i].meta = sub;
6057 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006058 /* pos does not change */
6059 replaced = 1;
6060 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006061 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 +02006062 }
6063 ++i;
6064 }
6065 }
6066 }
6067
6068 if (!replaced) {
6069 /* no match */
6070 set_remove_node(set, i);
6071 }
6072 }
6073
6074 return LY_SUCCESS;
6075}
6076
6077/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006078 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6079 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006080 *
6081 * @param[in] parent Current parent.
6082 * @param[in] parent_pos Position of @p parent.
6083 * @param[in] parent_type Node type of @p parent.
6084 * @param[in,out] to_set Set to use.
6085 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006086 * @param[in] options XPath options.
6087 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6088 */
6089static LY_ERR
6090moveto_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 +02006091 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006092{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006093 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006094 LY_ERR rc;
6095
6096 switch (parent_type) {
6097 case LYXP_NODE_ROOT:
6098 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006099 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006100
Michal Vasko61ac2f62020-05-25 12:39:51 +02006101 /* add all top-level nodes as elements */
6102 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006103 break;
6104 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006105 /* add just the text node of this term element node */
6106 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006107 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6108 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6109 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006110 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006111 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006112
6113 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006114 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006115 break;
6116 default:
6117 LOGINT_RET(parent->schema->module->ctx);
6118 }
6119
Michal Vasko61ac2f62020-05-25 12:39:51 +02006120 /* add all top-level nodes as elements */
6121 LY_LIST_FOR(first, iter) {
6122 /* context check */
6123 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6124 continue;
6125 }
6126
6127 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006128 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006129 return LY_EINCOMPLETE;
6130 }
6131
6132 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6133 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6134
6135 /* also add all the children of this node, recursively */
6136 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6137 LY_CHECK_RET(rc);
6138 }
6139 }
6140
Michal Vasko03ff5a72019-09-11 13:49:33 +02006141 return LY_SUCCESS;
6142}
6143
6144/**
6145 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6146 * (or LYXP_SET_EMPTY). Context position aware.
6147 *
6148 * @param[in,out] set Set to use.
6149 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6150 * @param[in] options XPath options.
6151 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6152 */
6153static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006154moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006155{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006156 struct lyxp_set ret_set;
6157 LY_ERR rc;
6158
Michal Vaskod3678892020-05-21 10:06:58 +02006159 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006160 return LY_SUCCESS;
6161 }
6162
6163 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006164 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006165 return LY_EVALID;
6166 }
6167
6168 /* nothing to do */
6169 if (!all_desc) {
6170 return LY_SUCCESS;
6171 }
6172
Michal Vasko03ff5a72019-09-11 13:49:33 +02006173 /* add all the children, they get added recursively */
6174 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006175 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006176 /* copy the current node to tmp */
6177 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6178
6179 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006180 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006181 continue;
6182 }
6183
Michal Vasko03ff5a72019-09-11 13:49:33 +02006184 /* add all the children */
6185 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 +02006186 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006187 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006188 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006189 return rc;
6190 }
6191 }
6192
6193 /* use the temporary set as the current one */
6194 ret_set.ctx_pos = set->ctx_pos;
6195 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006196 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006197 memcpy(set, &ret_set, sizeof *set);
6198
6199 return LY_SUCCESS;
6200}
6201
6202/**
6203 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6204 * (or LYXP_SET_EMPTY).
6205 *
6206 * @param[in,out] set Set to use.
6207 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6208 * @param[in] options XPath options.
6209 * @return LY_ERR
6210 */
6211static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006212moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006213{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006214 uint32_t getnext_opts;
6215 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006216 const struct lysc_node *iter, *start_parent;
6217 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006218
Michal Vaskod3678892020-05-21 10:06:58 +02006219 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006220 return LY_SUCCESS;
6221 }
6222
6223 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006224 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006225 return LY_EVALID;
6226 }
6227
Michal Vasko519fd602020-05-26 12:17:39 +02006228 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006229 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006230 if (options & LYXP_SCNODE_OUTPUT) {
6231 getnext_opts |= LYS_GETNEXT_OUTPUT;
6232 }
6233
6234 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006235 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006236 if (!all_desc) {
6237 /* traverse the start node */
6238 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6239 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6240 }
6241 continue;
6242 }
6243
Radek Krejcif13b87b2020-12-01 22:02:17 +01006244 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6245 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006246 continue;
6247 }
6248
Michal Vasko519fd602020-05-26 12:17:39 +02006249 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006250 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006251 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006252 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006253 }
6254
Michal Vasko519fd602020-05-26 12:17:39 +02006255 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006256
Michal Vasko519fd602020-05-26 12:17:39 +02006257 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6258 /* it can actually be in any module, it's all <running> */
6259 mod_idx = 0;
6260 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6261 iter = NULL;
6262 /* module may not be implemented */
6263 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6264 /* context check */
6265 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6266 continue;
6267 }
6268
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006269 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006270 /* throw away the insert index, we want to consider that node again, recursively */
6271 }
6272 }
6273
6274 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6275 iter = NULL;
6276 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006277 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006278 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006279 continue;
6280 }
6281
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006282 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006283 }
6284 }
6285 }
6286
6287 return LY_SUCCESS;
6288}
6289
6290/**
6291 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6292 * (or LYXP_SET_EMPTY). Context position aware.
6293 *
6294 * @param[in] set Set to use.
6295 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6296 * @param[in] options XPath options.
6297 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6298 */
6299static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006300moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006301{
6302 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006303 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006304 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006305
Michal Vaskod3678892020-05-21 10:06:58 +02006306 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006307 return LY_SUCCESS;
6308 }
6309
6310 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006311 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006312 return LY_EVALID;
6313 }
6314
6315 if (all_desc) {
6316 /* <path>//.. == <path>//./.. */
6317 rc = moveto_self(set, 1, options);
6318 LY_CHECK_RET(rc);
6319 }
6320
Radek Krejci1deb5be2020-08-26 16:43:36 +02006321 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006322 node = set->val.nodes[i].node;
6323
6324 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9e685082021-01-29 14:49:09 +01006325 new_node = lyd_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006326 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6327 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006328 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6329 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006330 if (!new_node) {
6331 LOGINT_RET(set->ctx);
6332 }
6333 } else {
6334 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006335 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006336 continue;
6337 }
6338
Michal Vaskoa1424542019-11-14 16:08:52 +01006339 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006340 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 +02006341 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006342 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006343
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006344 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006345 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006346 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006347
Michal Vasko03ff5a72019-09-11 13:49:33 +02006348 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006349 /* 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 +02006350 new_type = LYXP_NODE_ELEM;
6351 }
6352
Michal Vasko03ff5a72019-09-11 13:49:33 +02006353 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006354 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006355 } else {
6356 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006357 }
6358 }
6359
Michal Vasko2caefc12019-11-14 16:07:56 +01006360 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006361 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006362
6363 return LY_SUCCESS;
6364}
6365
6366/**
6367 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6368 * (or LYXP_SET_EMPTY).
6369 *
6370 * @param[in] set Set to use.
6371 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6372 * @param[in] options XPath options.
6373 * @return LY_ERR
6374 */
6375static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006376moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006377{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006378 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006379 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006380 const struct lysc_node *node, *new_node;
6381 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006382
Michal Vaskod3678892020-05-21 10:06:58 +02006383 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006384 return LY_SUCCESS;
6385 }
6386
6387 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006388 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006389 return LY_EVALID;
6390 }
6391
6392 if (all_desc) {
6393 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006394 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006395 }
6396
Michal Vasko03ff5a72019-09-11 13:49:33 +02006397 orig_used = set->used;
6398 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006399 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6400 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006401 continue;
6402 }
6403
6404 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006405 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006406 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006407 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006408 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006409
6410 node = set->val.scnodes[i].scnode;
6411
6412 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006413 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006414 } else {
6415 /* root does not have a parent */
6416 continue;
6417 }
6418
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006419 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006420 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006421 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006422
Michal Vasko03ff5a72019-09-11 13:49:33 +02006423 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006424 /* 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 +02006425 new_type = LYXP_NODE_ELEM;
6426 }
6427
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006428 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6429 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006430 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006431 temp_ctx = 1;
6432 }
6433 }
6434
6435 if (temp_ctx) {
6436 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006437 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6438 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006439 }
6440 }
6441 }
6442
6443 return LY_SUCCESS;
6444}
6445
6446/**
6447 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6448 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6449 *
6450 * @param[in,out] set1 Set to use for the result.
6451 * @param[in] set2 Set acting as the second operand for @p op.
6452 * @param[in] op Comparison operator to process.
6453 * @param[in] options XPath options.
6454 * @return LY_ERR
6455 */
6456static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006457moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006458{
6459 /*
6460 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6461 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6462 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6463 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6464 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6465 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6466 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6467 *
6468 * '=' or '!='
6469 * BOOLEAN + BOOLEAN
6470 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6471 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6472 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6473 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6474 * NUMBER + NUMBER
6475 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6476 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6477 * STRING + STRING
6478 *
6479 * '<=', '<', '>=', '>'
6480 * NUMBER + NUMBER
6481 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6482 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6483 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6484 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6485 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6486 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6487 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6488 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6489 */
6490 struct lyxp_set iter1, iter2;
6491 int result;
6492 int64_t i;
6493 LY_ERR rc;
6494
Michal Vasko004d3152020-06-11 19:59:22 +02006495 memset(&iter1, 0, sizeof iter1);
6496 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006497
6498 /* iterative evaluation with node-sets */
6499 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6500 if (set1->type == LYXP_SET_NODE_SET) {
6501 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006502 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006503 switch (set2->type) {
6504 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006505 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006506 break;
6507 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006508 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006509 break;
6510 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006511 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006512 break;
6513 }
6514 LY_CHECK_RET(rc);
6515
Michal Vasko4c7763f2020-07-27 17:40:37 +02006516 /* canonize set2 */
6517 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6518
6519 /* compare recursively */
6520 rc = moveto_op_comp(&iter1, &iter2, op, options);
6521 lyxp_set_free_content(&iter2);
6522 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006523
6524 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006525 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006526 set_fill_boolean(set1, 1);
6527 return LY_SUCCESS;
6528 }
6529 }
6530 } else {
6531 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006532 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006533 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006534 case LYXP_SET_NUMBER:
6535 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6536 break;
6537 case LYXP_SET_BOOLEAN:
6538 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6539 break;
6540 default:
6541 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6542 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006543 }
6544 LY_CHECK_RET(rc);
6545
Michal Vasko4c7763f2020-07-27 17:40:37 +02006546 /* canonize set1 */
6547 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 +02006548
Michal Vasko4c7763f2020-07-27 17:40:37 +02006549 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006550 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006551 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006552 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006553
6554 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006555 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006556 set_fill_boolean(set1, 1);
6557 return LY_SUCCESS;
6558 }
6559 }
6560 }
6561
6562 /* false for all nodes */
6563 set_fill_boolean(set1, 0);
6564 return LY_SUCCESS;
6565 }
6566
6567 /* first convert properly */
6568 if ((op[0] == '=') || (op[0] == '!')) {
6569 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006570 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6571 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006572 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006573 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006574 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006575 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006576 LY_CHECK_RET(rc);
6577 } /* else we have 2 strings */
6578 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006579 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006580 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006581 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006582 LY_CHECK_RET(rc);
6583 }
6584
6585 assert(set1->type == set2->type);
6586
6587 /* compute result */
6588 if (op[0] == '=') {
6589 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006590 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006591 } else if (set1->type == LYXP_SET_NUMBER) {
6592 result = (set1->val.num == set2->val.num);
6593 } else {
6594 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006595 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006596 }
6597 } else if (op[0] == '!') {
6598 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006599 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006600 } else if (set1->type == LYXP_SET_NUMBER) {
6601 result = (set1->val.num != set2->val.num);
6602 } else {
6603 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006604 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006605 }
6606 } else {
6607 assert(set1->type == LYXP_SET_NUMBER);
6608 if (op[0] == '<') {
6609 if (op[1] == '=') {
6610 result = (set1->val.num <= set2->val.num);
6611 } else {
6612 result = (set1->val.num < set2->val.num);
6613 }
6614 } else {
6615 if (op[1] == '=') {
6616 result = (set1->val.num >= set2->val.num);
6617 } else {
6618 result = (set1->val.num > set2->val.num);
6619 }
6620 }
6621 }
6622
6623 /* assign result */
6624 if (result) {
6625 set_fill_boolean(set1, 1);
6626 } else {
6627 set_fill_boolean(set1, 0);
6628 }
6629
6630 return LY_SUCCESS;
6631}
6632
6633/**
6634 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6635 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6636 *
6637 * @param[in,out] set1 Set to use for the result.
6638 * @param[in] set2 Set acting as the second operand for @p op.
6639 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006640 * @return LY_ERR
6641 */
6642static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006643moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006644{
6645 LY_ERR rc;
6646
6647 /* unary '-' */
6648 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006649 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006650 LY_CHECK_RET(rc);
6651 set1->val.num *= -1;
6652 lyxp_set_free(set2);
6653 return LY_SUCCESS;
6654 }
6655
6656 assert(set1 && set2);
6657
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006658 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006659 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006660 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006661 LY_CHECK_RET(rc);
6662
6663 switch (op[0]) {
6664 /* '+' */
6665 case '+':
6666 set1->val.num += set2->val.num;
6667 break;
6668
6669 /* '-' */
6670 case '-':
6671 set1->val.num -= set2->val.num;
6672 break;
6673
6674 /* '*' */
6675 case '*':
6676 set1->val.num *= set2->val.num;
6677 break;
6678
6679 /* 'div' */
6680 case 'd':
6681 set1->val.num /= set2->val.num;
6682 break;
6683
6684 /* 'mod' */
6685 case 'm':
6686 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6687 break;
6688
6689 default:
6690 LOGINT_RET(set1->ctx);
6691 }
6692
6693 return LY_SUCCESS;
6694}
6695
6696/*
6697 * eval functions
6698 *
6699 * They execute a parsed XPath expression on some data subtree.
6700 */
6701
6702/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006703 * @brief Evaluate Predicate. Logs directly on error.
6704 *
Michal Vaskod3678892020-05-21 10:06:58 +02006705 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006706 *
6707 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006708 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006709 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6710 * @param[in] options XPath options.
6711 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6712 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6713 */
6714static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006715eval_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 +02006716{
6717 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006718 uint16_t orig_exp;
6719 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006720 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006721 struct lyxp_set set2;
6722 struct lyd_node *orig_parent;
6723
6724 /* '[' */
6725 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006726 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006727 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006728
6729 if (!set) {
6730only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006731 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006732 LY_CHECK_RET(rc);
6733 } else if (set->type == LYXP_SET_NODE_SET) {
6734 /* 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 +01006735 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006736
6737 /* empty set, nothing to evaluate */
6738 if (!set->used) {
6739 goto only_parse;
6740 }
6741
Michal Vasko004d3152020-06-11 19:59:22 +02006742 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006743 orig_pos = 0;
6744 orig_size = set->used;
6745 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006746 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006747 set_init(&set2, set);
6748 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6749 /* remember the node context position for position() and context size for last(),
6750 * predicates should always be evaluated with respect to the child axis (since we do
6751 * not support explicit axes) so we assign positions based on their parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006752 if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) {
6753 orig_parent = lyd_parent(set->val.nodes[i].node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006754 orig_pos = 1;
6755 } else {
6756 ++orig_pos;
6757 }
6758
6759 set2.ctx_pos = orig_pos;
6760 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006761 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006762
Michal Vasko004d3152020-06-11 19:59:22 +02006763 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006764 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006765 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006766 return rc;
6767 }
6768
6769 /* number is a position */
6770 if (set2.type == LYXP_SET_NUMBER) {
6771 if ((long long)set2.val.num == orig_pos) {
6772 set2.val.num = 1;
6773 } else {
6774 set2.val.num = 0;
6775 }
6776 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006777 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006778
6779 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006780 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006781 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006782 }
6783 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006784 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006785
6786 } else if (set->type == LYXP_SET_SCNODE_SET) {
6787 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006788 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006789 /* there is a currently-valid node */
6790 break;
6791 }
6792 }
6793 /* empty set, nothing to evaluate */
6794 if (i == set->used) {
6795 goto only_parse;
6796 }
6797
Michal Vasko004d3152020-06-11 19:59:22 +02006798 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006799
Michal Vasko03ff5a72019-09-11 13:49:33 +02006800 /* set special in_ctx to all the valid snodes */
6801 pred_in_ctx = set_scnode_new_in_ctx(set);
6802
6803 /* use the valid snodes one-by-one */
6804 for (i = 0; i < set->used; ++i) {
6805 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6806 continue;
6807 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006808 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006809
Michal Vasko004d3152020-06-11 19:59:22 +02006810 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006811
Michal Vasko004d3152020-06-11 19:59:22 +02006812 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006813 LY_CHECK_RET(rc);
6814
6815 set->val.scnodes[i].in_ctx = pred_in_ctx;
6816 }
6817
6818 /* restore the state as it was before the predicate */
6819 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006820 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
6821 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006822 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006823 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006824 }
6825 }
6826
6827 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006828 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006829 set_fill_set(&set2, set);
6830
Michal Vasko004d3152020-06-11 19:59:22 +02006831 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006832 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006833 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006834 return rc;
6835 }
6836
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006837 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006838 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006839 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006840 }
Michal Vaskod3678892020-05-21 10:06:58 +02006841 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006842 }
6843
6844 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006845 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006846 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006847 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006848 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006849
6850 return LY_SUCCESS;
6851}
6852
6853/**
Michal Vaskod3678892020-05-21 10:06:58 +02006854 * @brief Evaluate Literal. Logs directly on error.
6855 *
6856 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006857 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006858 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6859 */
6860static void
Michal Vasko40308e72020-10-20 16:38:40 +02006861eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006862{
6863 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006864 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006865 set_fill_string(set, "", 0);
6866 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006867 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 +02006868 }
6869 }
6870 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006871 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006872 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006873}
6874
6875/**
Michal Vasko004d3152020-06-11 19:59:22 +02006876 * @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 +02006877 *
Michal Vasko004d3152020-06-11 19:59:22 +02006878 * @param[in] exp Full parsed XPath expression.
6879 * @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 +02006880 * @param[in] ctx_node Found schema node as the context for the predicate.
6881 * @param[in] cur_mod Current module for the expression.
6882 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006883 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006884 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006885 * @param[out] predicates Parsed predicates.
6886 * @param[out] pred_type Type of @p predicates.
6887 * @return LY_SUCCESS on success,
6888 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006889 */
6890static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006891eval_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 +02006892 const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_PREFIX_FORMAT format, void *prefix_data,
6893 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006894{
Michal Vasko004d3152020-06-11 19:59:22 +02006895 LY_ERR ret = LY_SUCCESS;
6896 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006897 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006898 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006899 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006900 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006901
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006902 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006903
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006904 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006905 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006906 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006907 return LY_EINVAL;
6908 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006909 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 +02006910 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006911
Michal Vasko004d3152020-06-11 19:59:22 +02006912 /* learn where the predicates end */
6913 e_idx = *tok_idx;
6914 while (key_count) {
6915 /* '[' */
6916 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6917 return LY_EINVAL;
6918 }
6919 ++e_idx;
6920
6921 /* ']' */
6922 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6923 ++e_idx;
6924 }
6925 ++e_idx;
6926
6927 /* another presumably key predicate parsed */
6928 --key_count;
6929 }
Michal Vasko004d3152020-06-11 19:59:22 +02006930 } else {
6931 /* learn just where this single predicate ends */
6932 e_idx = *tok_idx;
6933
Michal Vaskod3678892020-05-21 10:06:58 +02006934 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006935 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6936 return LY_EINVAL;
6937 }
6938 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006939
6940 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006941 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6942 ++e_idx;
6943 }
6944 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006945 }
6946
Michal Vasko004d3152020-06-11 19:59:22 +02006947 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6948 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6949
6950 /* turn logging off */
6951 prev_lo = ly_log_options(0);
6952
6953 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006954 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
6955 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02006956
6957 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006958 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
6959 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006960 LY_CHECK_GOTO(ret, cleanup);
6961
6962 /* success, the predicate must include all the needed information for hash-based search */
6963 *tok_idx = e_idx;
6964
6965cleanup:
6966 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006967 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02006968 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006969}
6970
6971/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006972 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
6973 * data node(s) could be found using a single hash-based search.
6974 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01006975 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006976 * @param[in] node Next context node to check.
6977 * @param[in] name Expected node name.
6978 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01006979 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006980 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006981 * @param[in] format Prefix format.
6982 * @param[in,out] found Previously found node, is updated.
6983 * @return LY_SUCCESS on success,
6984 * @return LY_ENOT if the whole check failed and hashes cannot be used.
6985 */
6986static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01006987eval_name_test_with_predicate_get_scnode(const struct ly_ctx *ctx, const struct lyd_node *node, const char *name,
6988 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 +02006989 const struct lysc_node **found)
6990{
6991 const struct lysc_node *scnode;
6992 const struct lys_module *mod;
6993 uint32_t idx = 0;
6994
Michal Vaskoc71c37b2021-01-11 13:40:02 +01006995 assert((format == LY_PREF_JSON) || moveto_mod);
6996
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006997continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02006998 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006999 if (!node) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007000 if ((format == LY_PREF_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007001 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007002 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007003 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7004 if (scnode) {
7005 /* we have found a match */
7006 break;
7007 }
7008 }
7009
Michal Vasko7d1d0912020-10-16 08:38:30 +02007010 if (!scnode) {
7011 /* all modules searched */
7012 idx = 0;
7013 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007014 } else {
7015 /* search in top-level */
7016 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7017 }
7018 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007019 if ((format == LY_PREF_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007020 /* we must adjust the module to inherit the one from the context node */
7021 moveto_mod = node->schema->module;
7022 }
7023
7024 /* search in children, do not repeat the same search */
7025 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007026 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007027
7028 /* additional context check */
7029 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7030 scnode = NULL;
7031 }
7032
7033 if (scnode) {
7034 if (*found) {
7035 /* we found a schema node with the same name but at different level, give up, too complicated
7036 * (more hash-based searches would be required, not supported) */
7037 return LY_ENOT;
7038 } else {
7039 /* remember the found schema node and continue to make sure it can be used */
7040 *found = scnode;
7041 }
7042 }
7043
7044 if (idx) {
7045 /* continue searching all the following models */
7046 goto continue_search;
7047 }
7048
7049 return LY_SUCCESS;
7050}
7051
7052/**
Michal Vaskod3678892020-05-21 10:06:58 +02007053 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7054 *
7055 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7056 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7057 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7058 *
7059 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007060 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007061 * @param[in] attr_axis Whether to search attributes or standard nodes.
7062 * @param[in] all_desc Whether to search all the descendants or children only.
7063 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7064 * @param[in] options XPath options.
7065 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7066 */
7067static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007068eval_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 +02007069 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007070{
Michal Vaskod3678892020-05-21 10:06:58 +02007071 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007072 const char *ncname, *ncname_dict = NULL;
7073 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007074 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007075 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007076 struct ly_path_predicate *predicates = NULL;
7077 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007078 LY_ERR rc = LY_SUCCESS;
7079
7080 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007081 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007082 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007083
7084 if (!set) {
7085 goto moveto;
7086 }
7087
Michal Vasko004d3152020-06-11 19:59:22 +02007088 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7089 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007090
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007091 if ((ncname[0] == '*') && (ncname_len == 1)) {
7092 /* all nodes will match */
7093 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007094 }
7095
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007096 /* parse (and skip) module name */
7097 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007098 LY_CHECK_GOTO(rc, cleanup);
7099
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007100 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 +02007101 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007102 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007103 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7104 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007105 /* check failed */
7106 scnode = NULL;
7107 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007108 }
7109 }
7110
Michal Vasko004d3152020-06-11 19:59:22 +02007111 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7112 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007113 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7114 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007115 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007116 scnode = NULL;
7117 }
7118 }
7119 }
7120
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007121 if (!scnode) {
7122 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007123 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007124 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007125 }
7126
7127moveto:
7128 /* move to the attribute(s), data node(s), or schema node(s) */
7129 if (attr_axis) {
7130 if (set && (options & LYXP_SCNODE_ALL)) {
7131 set_scnode_clear_ctx(set);
7132 } else {
7133 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007134 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007135 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007136 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007137 }
7138 LY_CHECK_GOTO(rc, cleanup);
7139 }
7140 } else {
7141 if (set && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007142 int64_t i;
7143
Michal Vaskod3678892020-05-21 10:06:58 +02007144 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007145 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007146 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007147 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007148 }
7149 LY_CHECK_GOTO(rc, cleanup);
7150
7151 for (i = set->used - 1; i > -1; --i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007152 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM) {
Michal Vaskod3678892020-05-21 10:06:58 +02007153 break;
7154 }
7155 }
7156 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007157 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vasko90f12dc2020-12-03 14:20:42 +01007158 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".",
7159 ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007160 free(path);
7161 }
7162 } else {
7163 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007164 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007165 } else {
7166 if (scnode) {
7167 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007168 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007169 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007170 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007171 }
7172 }
7173 LY_CHECK_GOTO(rc, cleanup);
7174 }
7175 }
7176
7177 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007178 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7179 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007180 LY_CHECK_RET(rc);
7181 }
7182
7183cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007184 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007185 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007186 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007187 }
Michal Vaskod3678892020-05-21 10:06:58 +02007188 return rc;
7189}
7190
7191/**
7192 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7193 *
7194 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7195 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7196 * [8] NodeType ::= 'text' | 'node'
7197 *
7198 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007199 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007200 * @param[in] attr_axis Whether to search attributes or standard nodes.
7201 * @param[in] all_desc Whether to search all the descendants or children only.
7202 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7203 * @param[in] options XPath options.
7204 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7205 */
7206static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007207eval_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 +02007208 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007209{
7210 LY_ERR rc;
7211
7212 /* TODO */
7213 (void)attr_axis;
7214 (void)all_desc;
7215
7216 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007217 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007218 if (set->type == LYXP_SET_SCNODE_SET) {
7219 set_scnode_clear_ctx(set);
7220 /* just for the debug message below */
7221 set = NULL;
7222 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007223 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007224 rc = xpath_node(NULL, 0, set, options);
7225 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007226 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007227 rc = xpath_text(NULL, 0, set, options);
7228 }
7229 LY_CHECK_RET(rc);
7230 }
7231 }
7232 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007233 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007234 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007235
7236 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007237 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007238 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007239 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007240 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007241
7242 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007243 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007244 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007245 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007246 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007247
7248 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007249 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7250 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007251 LY_CHECK_RET(rc);
7252 }
7253
7254 return LY_SUCCESS;
7255}
7256
7257/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007258 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7259 *
7260 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7261 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007262 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007263 *
7264 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007265 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007266 * @param[in] all_desc Whether to search all the descendants or children only.
7267 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7268 * @param[in] options XPath options.
7269 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7270 */
7271static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007272eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7273 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007274{
Radek Krejci857189e2020-09-01 13:26:36 +02007275 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007276 LY_ERR rc;
7277
7278 goto step;
7279 do {
7280 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007281 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007282 all_desc = 0;
7283 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007284 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007285 all_desc = 1;
7286 }
7287 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007288 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007289 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007290
7291step:
Michal Vaskod3678892020-05-21 10:06:58 +02007292 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007293 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007294 attr_axis = 1;
7295
7296 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007297 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007298 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007299 } else {
7300 attr_axis = 0;
7301 }
7302
Michal Vasko03ff5a72019-09-11 13:49:33 +02007303 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007304 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007305 case LYXP_TOKEN_DOT:
7306 /* evaluate '.' */
7307 if (set && (options & LYXP_SCNODE_ALL)) {
7308 rc = moveto_scnode_self(set, all_desc, options);
7309 } else {
7310 rc = moveto_self(set, all_desc, options);
7311 }
7312 LY_CHECK_RET(rc);
7313 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007314 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007315 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007316 break;
7317
7318 case LYXP_TOKEN_DDOT:
7319 /* evaluate '..' */
7320 if (set && (options & LYXP_SCNODE_ALL)) {
7321 rc = moveto_scnode_parent(set, all_desc, options);
7322 } else {
7323 rc = moveto_parent(set, all_desc, options);
7324 }
7325 LY_CHECK_RET(rc);
7326 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007327 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007328 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007329 break;
7330
Michal Vasko03ff5a72019-09-11 13:49:33 +02007331 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007332 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007333 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007334 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007335 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007336
Michal Vaskod3678892020-05-21 10:06:58 +02007337 case LYXP_TOKEN_NODETYPE:
7338 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007339 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007340 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007341 break;
7342
7343 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007344 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007345 }
Michal Vasko004d3152020-06-11 19:59:22 +02007346 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007347
7348 return LY_SUCCESS;
7349}
7350
7351/**
7352 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7353 *
7354 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7355 *
7356 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007357 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007358 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7359 * @param[in] options XPath options.
7360 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7361 */
7362static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007363eval_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 +02007364{
Radek Krejci857189e2020-09-01 13:26:36 +02007365 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007366
7367 if (set) {
7368 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007369 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007370 }
7371
7372 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007373 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007374 /* evaluate '/' - deferred */
7375 all_desc = 0;
7376 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007377 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007378 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007379
Michal Vasko004d3152020-06-11 19:59:22 +02007380 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007381 return LY_SUCCESS;
7382 }
Michal Vasko004d3152020-06-11 19:59:22 +02007383 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007384 case LYXP_TOKEN_DOT:
7385 case LYXP_TOKEN_DDOT:
7386 case LYXP_TOKEN_AT:
7387 case LYXP_TOKEN_NAMETEST:
7388 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007389 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007390 break;
7391 default:
7392 break;
7393 }
7394
Michal Vasko03ff5a72019-09-11 13:49:33 +02007395 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007396 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007397 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7398 all_desc = 1;
7399 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007400 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007401 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007402
Michal Vaskob0099a92020-08-31 14:55:23 +02007403 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007404 }
7405
7406 return LY_SUCCESS;
7407}
7408
7409/**
7410 * @brief Evaluate FunctionCall. Logs directly on error.
7411 *
Michal Vaskod3678892020-05-21 10:06:58 +02007412 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007413 *
7414 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007415 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007416 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7417 * @param[in] options XPath options.
7418 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7419 */
7420static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007421eval_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 +02007422{
7423 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007424
Radek Krejci1deb5be2020-08-26 16:43:36 +02007425 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007426 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007427 struct lyxp_set **args = NULL, **args_aux;
7428
7429 if (set) {
7430 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007431 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007432 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007433 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007434 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007435 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007436 xpath_func = &xpath_sum;
7437 }
7438 break;
7439 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007440 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007441 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007442 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007443 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007444 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007445 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007446 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007447 xpath_func = &xpath_true;
7448 }
7449 break;
7450 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007451 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007452 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007453 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007454 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007455 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007456 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007457 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007458 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007459 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007460 xpath_func = &xpath_deref;
7461 }
7462 break;
7463 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007464 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007465 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007466 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007467 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007468 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007469 xpath_func = &xpath_string;
7470 }
7471 break;
7472 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007473 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007474 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007475 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007476 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007477 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007478 xpath_func = &xpath_current;
7479 }
7480 break;
7481 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007482 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007483 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007484 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007485 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007486 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007487 xpath_func = &xpath_re_match;
7488 }
7489 break;
7490 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007491 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007492 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007493 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007494 xpath_func = &xpath_translate;
7495 }
7496 break;
7497 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007498 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007499 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007500 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007501 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007502 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007503 xpath_func = &xpath_bit_is_set;
7504 }
7505 break;
7506 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007507 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007508 xpath_func = &xpath_starts_with;
7509 }
7510 break;
7511 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007512 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007513 xpath_func = &xpath_derived_from;
7514 }
7515 break;
7516 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007517 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007518 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007519 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007520 xpath_func = &xpath_string_length;
7521 }
7522 break;
7523 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007524 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007525 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007526 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007527 xpath_func = &xpath_substring_after;
7528 }
7529 break;
7530 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007531 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007532 xpath_func = &xpath_substring_before;
7533 }
7534 break;
7535 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007536 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007537 xpath_func = &xpath_derived_from_or_self;
7538 }
7539 break;
7540 }
7541
7542 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007543 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 +02007544 return LY_EVALID;
7545 }
7546 }
7547
7548 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007549 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007550 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007551
7552 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007553 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007554 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007555 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007556 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007557
7558 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007559 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007560 if (set) {
7561 args = malloc(sizeof *args);
7562 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7563 arg_count = 1;
7564 args[0] = set_copy(set);
7565 if (!args[0]) {
7566 rc = LY_EMEM;
7567 goto cleanup;
7568 }
7569
Michal Vasko004d3152020-06-11 19:59:22 +02007570 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007571 LY_CHECK_GOTO(rc, cleanup);
7572 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007573 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007574 LY_CHECK_GOTO(rc, cleanup);
7575 }
7576 }
Michal Vasko004d3152020-06-11 19:59:22 +02007577 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007578 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007579 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007580 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007581
7582 if (set) {
7583 ++arg_count;
7584 args_aux = realloc(args, arg_count * sizeof *args);
7585 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7586 args = args_aux;
7587 args[arg_count - 1] = set_copy(set);
7588 if (!args[arg_count - 1]) {
7589 rc = LY_EMEM;
7590 goto cleanup;
7591 }
7592
Michal Vasko004d3152020-06-11 19:59:22 +02007593 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007594 LY_CHECK_GOTO(rc, cleanup);
7595 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007596 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007597 LY_CHECK_GOTO(rc, cleanup);
7598 }
7599 }
7600
7601 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007602 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007603 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007604 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007605 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007606
7607 if (set) {
7608 /* evaluate function */
7609 rc = xpath_func(args, arg_count, set, options);
7610
7611 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007612 /* merge all nodes from arg evaluations */
7613 for (i = 0; i < arg_count; ++i) {
7614 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007615 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007616 }
7617 }
7618 } else {
7619 rc = LY_SUCCESS;
7620 }
7621
7622cleanup:
7623 for (i = 0; i < arg_count; ++i) {
7624 lyxp_set_free(args[i]);
7625 }
7626 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007627 return rc;
7628}
7629
7630/**
7631 * @brief Evaluate Number. Logs directly on error.
7632 *
7633 * @param[in] ctx Context for errors.
7634 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007635 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007636 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7637 * @return LY_ERR
7638 */
7639static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007640eval_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 +02007641{
7642 long double num;
7643 char *endptr;
7644
7645 if (set) {
7646 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007647 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007648 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007649 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7650 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007651 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007652 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007653 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007654 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7655 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007656 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007657 return LY_EVALID;
7658 }
7659
7660 set_fill_number(set, num);
7661 }
7662
7663 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007664 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007665 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007666 return LY_SUCCESS;
7667}
7668
7669/**
7670 * @brief Evaluate PathExpr. Logs directly on error.
7671 *
Michal Vaskod3678892020-05-21 10:06:58 +02007672 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007673 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7674 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7675 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007676 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007677 *
7678 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007679 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007680 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7681 * @param[in] options XPath options.
7682 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7683 */
7684static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007685eval_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 +02007686{
Radek Krejci857189e2020-09-01 13:26:36 +02007687 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007688 LY_ERR rc;
7689
Michal Vasko004d3152020-06-11 19:59:22 +02007690 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007691 case LYXP_TOKEN_PAR1:
7692 /* '(' Expr ')' */
7693
7694 /* '(' */
7695 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007696 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007697 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007698
7699 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007700 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007701 LY_CHECK_RET(rc);
7702
7703 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007704 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007705 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007706 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007707 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007708
7709 parent_pos_pred = 0;
7710 goto predicate;
7711
7712 case LYXP_TOKEN_DOT:
7713 case LYXP_TOKEN_DDOT:
7714 case LYXP_TOKEN_AT:
7715 case LYXP_TOKEN_NAMETEST:
7716 case LYXP_TOKEN_NODETYPE:
7717 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007718 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007719 LY_CHECK_RET(rc);
7720 break;
7721
7722 case LYXP_TOKEN_FUNCNAME:
7723 /* FunctionCall */
7724 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007725 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007726 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007727 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007728 }
7729 LY_CHECK_RET(rc);
7730
7731 parent_pos_pred = 1;
7732 goto predicate;
7733
Michal Vasko3e48bf32020-06-01 08:39:07 +02007734 case LYXP_TOKEN_OPER_PATH:
7735 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007736 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007737 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007738 LY_CHECK_RET(rc);
7739 break;
7740
7741 case LYXP_TOKEN_LITERAL:
7742 /* Literal */
7743 if (!set || (options & LYXP_SCNODE_ALL)) {
7744 if (set) {
7745 set_scnode_clear_ctx(set);
7746 }
Michal Vasko004d3152020-06-11 19:59:22 +02007747 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007748 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007749 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007750 }
7751
7752 parent_pos_pred = 1;
7753 goto predicate;
7754
7755 case LYXP_TOKEN_NUMBER:
7756 /* Number */
7757 if (!set || (options & LYXP_SCNODE_ALL)) {
7758 if (set) {
7759 set_scnode_clear_ctx(set);
7760 }
Michal Vasko004d3152020-06-11 19:59:22 +02007761 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007762 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007763 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007764 }
7765 LY_CHECK_RET(rc);
7766
7767 parent_pos_pred = 1;
7768 goto predicate;
7769
7770 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01007771 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 +02007772 return LY_EVALID;
7773 }
7774
7775 return LY_SUCCESS;
7776
7777predicate:
7778 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007779 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7780 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007781 LY_CHECK_RET(rc);
7782 }
7783
7784 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007785 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007786
7787 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007788 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007789 all_desc = 0;
7790 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007791 all_desc = 1;
7792 }
7793
7794 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007795 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007796 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007797
Michal Vasko004d3152020-06-11 19:59:22 +02007798 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007799 LY_CHECK_RET(rc);
7800 }
7801
7802 return LY_SUCCESS;
7803}
7804
7805/**
7806 * @brief Evaluate UnionExpr. Logs directly on error.
7807 *
Michal Vaskod3678892020-05-21 10:06:58 +02007808 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007809 *
7810 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007811 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007812 * @param[in] repeat How many times this expression is repeated.
7813 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7814 * @param[in] options XPath options.
7815 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7816 */
7817static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007818eval_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 +02007819{
7820 LY_ERR rc = LY_SUCCESS;
7821 struct lyxp_set orig_set, set2;
7822 uint16_t i;
7823
7824 assert(repeat);
7825
7826 set_init(&orig_set, set);
7827 set_init(&set2, set);
7828
7829 set_fill_set(&orig_set, set);
7830
Michal Vasko004d3152020-06-11 19:59:22 +02007831 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007832 LY_CHECK_GOTO(rc, cleanup);
7833
7834 /* ('|' PathExpr)* */
7835 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007836 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007837 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007838 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007839 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007840
7841 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007842 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007843 LY_CHECK_GOTO(rc, cleanup);
7844 continue;
7845 }
7846
7847 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007848 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007849 LY_CHECK_GOTO(rc, cleanup);
7850
7851 /* eval */
7852 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007853 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007854 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007855 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007856 LY_CHECK_GOTO(rc, cleanup);
7857 }
7858 }
7859
7860cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007861 lyxp_set_free_content(&orig_set);
7862 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007863 return rc;
7864}
7865
7866/**
7867 * @brief Evaluate UnaryExpr. Logs directly on error.
7868 *
Michal Vaskod3678892020-05-21 10:06:58 +02007869 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007870 *
7871 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007872 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007873 * @param[in] repeat How many times this expression is repeated.
7874 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7875 * @param[in] options XPath options.
7876 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7877 */
7878static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007879eval_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 +02007880{
7881 LY_ERR rc;
7882 uint16_t this_op, i;
7883
7884 assert(repeat);
7885
7886 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007887 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007888 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007889 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 +02007890
7891 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007892 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007893 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007894 }
7895
Michal Vasko004d3152020-06-11 19:59:22 +02007896 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007897 LY_CHECK_RET(rc);
7898
7899 if (set && (repeat % 2)) {
7900 if (options & LYXP_SCNODE_ALL) {
7901 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7902 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007903 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007904 LY_CHECK_RET(rc);
7905 }
7906 }
7907
7908 return LY_SUCCESS;
7909}
7910
7911/**
7912 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7913 *
Michal Vaskod3678892020-05-21 10:06:58 +02007914 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007915 * | MultiplicativeExpr '*' UnaryExpr
7916 * | MultiplicativeExpr 'div' UnaryExpr
7917 * | MultiplicativeExpr 'mod' UnaryExpr
7918 *
7919 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007920 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007921 * @param[in] repeat How many times this expression is repeated.
7922 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7923 * @param[in] options XPath options.
7924 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7925 */
7926static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007927eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
7928 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007929{
7930 LY_ERR rc;
7931 uint16_t this_op;
7932 struct lyxp_set orig_set, set2;
7933 uint16_t i;
7934
7935 assert(repeat);
7936
7937 set_init(&orig_set, set);
7938 set_init(&set2, set);
7939
7940 set_fill_set(&orig_set, set);
7941
Michal Vasko004d3152020-06-11 19:59:22 +02007942 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007943 LY_CHECK_GOTO(rc, cleanup);
7944
7945 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7946 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007947 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007948
Michal Vasko004d3152020-06-11 19:59:22 +02007949 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007950 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007951 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007952 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007953
7954 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007955 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007956 LY_CHECK_GOTO(rc, cleanup);
7957 continue;
7958 }
7959
7960 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007961 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007962 LY_CHECK_GOTO(rc, cleanup);
7963
7964 /* eval */
7965 if (options & LYXP_SCNODE_ALL) {
7966 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007967 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007968 set_scnode_clear_ctx(set);
7969 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007970 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007971 LY_CHECK_GOTO(rc, cleanup);
7972 }
7973 }
7974
7975cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007976 lyxp_set_free_content(&orig_set);
7977 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007978 return rc;
7979}
7980
7981/**
7982 * @brief Evaluate AdditiveExpr. Logs directly on error.
7983 *
Michal Vaskod3678892020-05-21 10:06:58 +02007984 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007985 * | AdditiveExpr '+' MultiplicativeExpr
7986 * | AdditiveExpr '-' MultiplicativeExpr
7987 *
7988 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007989 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007990 * @param[in] repeat How many times this expression is repeated.
7991 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7992 * @param[in] options XPath options.
7993 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7994 */
7995static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007996eval_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 +02007997{
7998 LY_ERR rc;
7999 uint16_t this_op;
8000 struct lyxp_set orig_set, set2;
8001 uint16_t i;
8002
8003 assert(repeat);
8004
8005 set_init(&orig_set, set);
8006 set_init(&set2, set);
8007
8008 set_fill_set(&orig_set, set);
8009
Michal Vasko004d3152020-06-11 19:59:22 +02008010 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008011 LY_CHECK_GOTO(rc, cleanup);
8012
8013 /* ('+' / '-' MultiplicativeExpr)* */
8014 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008015 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008016
Michal Vasko004d3152020-06-11 19:59:22 +02008017 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008018 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008019 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008020 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008021
8022 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008023 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008024 LY_CHECK_GOTO(rc, cleanup);
8025 continue;
8026 }
8027
8028 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008029 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008030 LY_CHECK_GOTO(rc, cleanup);
8031
8032 /* eval */
8033 if (options & LYXP_SCNODE_ALL) {
8034 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008035 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008036 set_scnode_clear_ctx(set);
8037 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008038 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008039 LY_CHECK_GOTO(rc, cleanup);
8040 }
8041 }
8042
8043cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008044 lyxp_set_free_content(&orig_set);
8045 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008046 return rc;
8047}
8048
8049/**
8050 * @brief Evaluate RelationalExpr. Logs directly on error.
8051 *
Michal Vaskod3678892020-05-21 10:06:58 +02008052 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008053 * | RelationalExpr '<' AdditiveExpr
8054 * | RelationalExpr '>' AdditiveExpr
8055 * | RelationalExpr '<=' AdditiveExpr
8056 * | RelationalExpr '>=' AdditiveExpr
8057 *
8058 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008059 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008060 * @param[in] repeat How many times this expression is repeated.
8061 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8062 * @param[in] options XPath options.
8063 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8064 */
8065static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008066eval_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 +02008067{
8068 LY_ERR rc;
8069 uint16_t this_op;
8070 struct lyxp_set orig_set, set2;
8071 uint16_t i;
8072
8073 assert(repeat);
8074
8075 set_init(&orig_set, set);
8076 set_init(&set2, set);
8077
8078 set_fill_set(&orig_set, set);
8079
Michal Vasko004d3152020-06-11 19:59:22 +02008080 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008081 LY_CHECK_GOTO(rc, cleanup);
8082
8083 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8084 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008085 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008086
Michal Vasko004d3152020-06-11 19:59:22 +02008087 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008088 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008089 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008090 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008091
8092 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008093 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008094 LY_CHECK_GOTO(rc, cleanup);
8095 continue;
8096 }
8097
8098 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008099 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008100 LY_CHECK_GOTO(rc, cleanup);
8101
8102 /* eval */
8103 if (options & LYXP_SCNODE_ALL) {
8104 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008105 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008106 set_scnode_clear_ctx(set);
8107 } else {
8108 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8109 LY_CHECK_GOTO(rc, cleanup);
8110 }
8111 }
8112
8113cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008114 lyxp_set_free_content(&orig_set);
8115 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008116 return rc;
8117}
8118
8119/**
8120 * @brief Evaluate EqualityExpr. Logs directly on error.
8121 *
Michal Vaskod3678892020-05-21 10:06:58 +02008122 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008123 * | EqualityExpr '!=' RelationalExpr
8124 *
8125 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008126 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008127 * @param[in] repeat How many times this expression is repeated.
8128 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8129 * @param[in] options XPath options.
8130 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8131 */
8132static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008133eval_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 +02008134{
8135 LY_ERR rc;
8136 uint16_t this_op;
8137 struct lyxp_set orig_set, set2;
8138 uint16_t i;
8139
8140 assert(repeat);
8141
8142 set_init(&orig_set, set);
8143 set_init(&set2, set);
8144
8145 set_fill_set(&orig_set, set);
8146
Michal Vasko004d3152020-06-11 19:59:22 +02008147 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008148 LY_CHECK_GOTO(rc, cleanup);
8149
8150 /* ('=' / '!=' RelationalExpr)* */
8151 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008152 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008153
Michal Vasko004d3152020-06-11 19:59:22 +02008154 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008155 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008156 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008157 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008158
8159 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008160 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008161 LY_CHECK_GOTO(rc, cleanup);
8162 continue;
8163 }
8164
8165 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008166 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008167 LY_CHECK_GOTO(rc, cleanup);
8168
8169 /* eval */
8170 if (options & LYXP_SCNODE_ALL) {
8171 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008172 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8173 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008174 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008175 set_scnode_clear_ctx(set);
8176 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008177 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8178 LY_CHECK_GOTO(rc, cleanup);
8179 }
8180 }
8181
8182cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008183 lyxp_set_free_content(&orig_set);
8184 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008185 return rc;
8186}
8187
8188/**
8189 * @brief Evaluate AndExpr. Logs directly on error.
8190 *
Michal Vaskod3678892020-05-21 10:06:58 +02008191 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008192 *
8193 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008194 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008195 * @param[in] repeat How many times this expression is repeated.
8196 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8197 * @param[in] options XPath options.
8198 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8199 */
8200static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008201eval_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 +02008202{
8203 LY_ERR rc;
8204 struct lyxp_set orig_set, set2;
8205 uint16_t i;
8206
8207 assert(repeat);
8208
8209 set_init(&orig_set, set);
8210 set_init(&set2, set);
8211
8212 set_fill_set(&orig_set, set);
8213
Michal Vasko004d3152020-06-11 19:59:22 +02008214 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008215 LY_CHECK_GOTO(rc, cleanup);
8216
8217 /* cast to boolean, we know that will be the final result */
8218 if (set && (options & LYXP_SCNODE_ALL)) {
8219 set_scnode_clear_ctx(set);
8220 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008221 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008222 }
8223
8224 /* ('and' EqualityExpr)* */
8225 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008226 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8227 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008228 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008229 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008230
8231 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008232 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8233 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008234 LY_CHECK_GOTO(rc, cleanup);
8235 continue;
8236 }
8237
8238 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008239 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008240 LY_CHECK_GOTO(rc, cleanup);
8241
8242 /* eval - just get boolean value actually */
8243 if (set->type == LYXP_SET_SCNODE_SET) {
8244 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008245 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008246 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008247 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008248 set_fill_set(set, &set2);
8249 }
8250 }
8251
8252cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008253 lyxp_set_free_content(&orig_set);
8254 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008255 return rc;
8256}
8257
8258/**
8259 * @brief Evaluate OrExpr. Logs directly on error.
8260 *
Michal Vaskod3678892020-05-21 10:06:58 +02008261 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008262 *
8263 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008264 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008265 * @param[in] repeat How many times this expression is repeated.
8266 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8267 * @param[in] options XPath options.
8268 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8269 */
8270static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008271eval_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 +02008272{
8273 LY_ERR rc;
8274 struct lyxp_set orig_set, set2;
8275 uint16_t i;
8276
8277 assert(repeat);
8278
8279 set_init(&orig_set, set);
8280 set_init(&set2, set);
8281
8282 set_fill_set(&orig_set, set);
8283
Michal Vasko004d3152020-06-11 19:59:22 +02008284 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008285 LY_CHECK_GOTO(rc, cleanup);
8286
8287 /* cast to boolean, we know that will be the final result */
8288 if (set && (options & LYXP_SCNODE_ALL)) {
8289 set_scnode_clear_ctx(set);
8290 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008291 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008292 }
8293
8294 /* ('or' AndExpr)* */
8295 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008296 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8297 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008298 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008299 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008300
8301 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008302 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8303 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008304 LY_CHECK_GOTO(rc, cleanup);
8305 continue;
8306 }
8307
8308 set_fill_set(&set2, &orig_set);
8309 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8310 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008311 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008312 LY_CHECK_GOTO(rc, cleanup);
8313
8314 /* eval - just get boolean value actually */
8315 if (set->type == LYXP_SET_SCNODE_SET) {
8316 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008317 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008318 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008319 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008320 set_fill_set(set, &set2);
8321 }
8322 }
8323
8324cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008325 lyxp_set_free_content(&orig_set);
8326 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008327 return rc;
8328}
8329
8330/**
Michal Vasko004d3152020-06-11 19:59:22 +02008331 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008332 *
8333 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008334 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008335 * @param[in] etype Expression type to evaluate.
8336 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8337 * @param[in] options XPath options.
8338 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8339 */
8340static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008341eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8342 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008343{
8344 uint16_t i, count;
8345 enum lyxp_expr_type next_etype;
8346 LY_ERR rc;
8347
8348 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008349 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008350 next_etype = LYXP_EXPR_NONE;
8351 } else {
8352 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008353 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008354
8355 /* select one-priority lower because etype expression called us */
8356 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008357 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008358 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008359 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008360 } else {
8361 next_etype = LYXP_EXPR_NONE;
8362 }
8363 }
8364
8365 /* decide what expression are we parsing based on the repeat */
8366 switch (next_etype) {
8367 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008368 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008369 break;
8370 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008371 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008372 break;
8373 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008374 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008375 break;
8376 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008377 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008378 break;
8379 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008380 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008381 break;
8382 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008383 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008384 break;
8385 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008386 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008387 break;
8388 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008389 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008390 break;
8391 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008392 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008393 break;
8394 default:
8395 LOGINT_RET(set->ctx);
8396 }
8397
8398 return rc;
8399}
8400
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008401/**
8402 * @brief Get root type.
8403 *
8404 * @param[in] ctx_node Context node.
8405 * @param[in] ctx_scnode Schema context node.
8406 * @param[in] options XPath options.
8407 * @return Root type.
8408 */
8409static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008410lyxp_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 +01008411{
Michal Vasko6b26e742020-07-17 15:02:10 +02008412 const struct lysc_node *op;
8413
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008414 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008415 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008416 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008417
8418 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008419 /* general root that can access everything */
8420 return LYXP_NODE_ROOT;
8421 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8422 /* root context node can access only config data (because we said so, it is unspecified) */
8423 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008424 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008425 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008426 }
8427
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008428 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008429 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008430 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008431
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008432 if (op || !(options & LYXP_SCHEMA)) {
8433 /* general root that can access everything */
8434 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05008435 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008436 /* root context node can access only config data (because we said so, it is unspecified) */
8437 return LYXP_NODE_ROOT_CONFIG;
8438 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008439 return LYXP_NODE_ROOT;
8440}
8441
Michal Vasko03ff5a72019-09-11 13:49:33 +02008442LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008443lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
8444 LY_PREFIX_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node, const struct lyd_node *tree,
8445 struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008446{
Michal Vasko004d3152020-06-11 19:59:22 +02008447 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008448 LY_ERR rc;
8449
Michal Vasko400e9672021-01-11 13:39:17 +01008450 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008451 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8452 LOGARG(NULL, "Current module must be set if schema format is used.");
8453 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008454 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008455
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008456 if (tree) {
8457 /* adjust the pointer to be the first top-level sibling */
8458 while (tree->parent) {
8459 tree = lyd_parent(tree);
8460 }
8461 tree = lyd_first_sibling(tree);
8462 }
8463
Michal Vasko03ff5a72019-09-11 13:49:33 +02008464 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008465 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008466 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008467 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8468 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8469
Michal Vasko400e9672021-01-11 13:39:17 +01008470 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008471 set->cur_node = ctx_node;
8472 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008473 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8474 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008475 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008476 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008477 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008478 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008479
Radek Krejciddace2c2021-01-08 11:30:56 +01008480 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008481
Michal Vasko03ff5a72019-09-11 13:49:33 +02008482 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008483 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008484 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008485 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008486 }
8487
Radek Krejciddace2c2021-01-08 11:30:56 +01008488 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008489 return rc;
8490}
8491
8492#if 0
8493
8494/* full xml printing of set elements, not used currently */
8495
8496void
8497lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8498{
8499 uint32_t i;
8500 char *str_num;
8501 struct lyout out;
8502
8503 memset(&out, 0, sizeof out);
8504
8505 out.type = LYOUT_STREAM;
8506 out.method.f = f;
8507
8508 switch (set->type) {
8509 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008510 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008511 break;
8512 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008513 ly_print_(&out, "Boolean XPath set:\n");
8514 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008515 break;
8516 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008517 ly_print_(&out, "String XPath set:\n");
8518 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008519 break;
8520 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008521 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008522
8523 if (isnan(set->value.num)) {
8524 str_num = strdup("NaN");
8525 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8526 str_num = strdup("0");
8527 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8528 str_num = strdup("Infinity");
8529 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8530 str_num = strdup("-Infinity");
8531 } else if ((long long)set->value.num == set->value.num) {
8532 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8533 str_num = NULL;
8534 }
8535 } else {
8536 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8537 str_num = NULL;
8538 }
8539 }
8540 if (!str_num) {
8541 LOGMEM;
8542 return;
8543 }
Michal Vasko5233e962020-08-14 14:26:20 +02008544 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008545 free(str_num);
8546 break;
8547 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008548 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008549
8550 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008551 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008552 switch (set->node_type[i]) {
8553 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008554 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008555 break;
8556 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008557 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008558 break;
8559 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008560 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008561 break;
8562 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008563 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008564 break;
8565 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008566 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008567 break;
8568 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008569 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008570 break;
8571 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008572 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008573 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008574 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008575 break;
8576 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008577 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 +02008578 break;
8579 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008580 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 +02008581 break;
8582 }
8583 }
8584 break;
8585 }
8586}
8587
8588#endif
8589
8590LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008591lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008592{
8593 long double num;
8594 char *str;
8595 LY_ERR rc;
8596
8597 if (!set || (set->type == target)) {
8598 return LY_SUCCESS;
8599 }
8600
8601 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008602 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008603
8604 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008605 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008606 return LY_EINVAL;
8607 }
8608
8609 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008610 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008611 switch (set->type) {
8612 case LYXP_SET_NUMBER:
8613 if (isnan(set->val.num)) {
8614 set->val.str = strdup("NaN");
8615 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8616 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8617 set->val.str = strdup("0");
8618 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8619 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8620 set->val.str = strdup("Infinity");
8621 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8622 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8623 set->val.str = strdup("-Infinity");
8624 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8625 } else if ((long long)set->val.num == set->val.num) {
8626 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8627 LOGMEM_RET(set->ctx);
8628 }
8629 set->val.str = str;
8630 } else {
8631 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8632 LOGMEM_RET(set->ctx);
8633 }
8634 set->val.str = str;
8635 }
8636 break;
8637 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008638 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008639 set->val.str = strdup("true");
8640 } else {
8641 set->val.str = strdup("false");
8642 }
8643 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8644 break;
8645 case LYXP_SET_NODE_SET:
8646 assert(set->used);
8647
8648 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008649 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008650
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008651 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008652 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008653 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008654 set->val.str = str;
8655 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008656 default:
8657 LOGINT_RET(set->ctx);
8658 }
8659 set->type = LYXP_SET_STRING;
8660 }
8661
8662 /* to NUMBER */
8663 if (target == LYXP_SET_NUMBER) {
8664 switch (set->type) {
8665 case LYXP_SET_STRING:
8666 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008667 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008668 set->val.num = num;
8669 break;
8670 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008671 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008672 set->val.num = 1;
8673 } else {
8674 set->val.num = 0;
8675 }
8676 break;
8677 default:
8678 LOGINT_RET(set->ctx);
8679 }
8680 set->type = LYXP_SET_NUMBER;
8681 }
8682
8683 /* to BOOLEAN */
8684 if (target == LYXP_SET_BOOLEAN) {
8685 switch (set->type) {
8686 case LYXP_SET_NUMBER:
8687 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008688 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008689 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008690 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008691 }
8692 break;
8693 case LYXP_SET_STRING:
8694 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008695 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008696 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008697 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008698 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008699 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008700 }
8701 break;
8702 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008703 if (set->used) {
8704 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008705 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008706 } else {
8707 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008708 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008709 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008710 break;
8711 default:
8712 LOGINT_RET(set->ctx);
8713 }
8714 set->type = LYXP_SET_BOOLEAN;
8715 }
8716
Michal Vasko03ff5a72019-09-11 13:49:33 +02008717 return LY_SUCCESS;
8718}
8719
8720LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008721lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
8722 LY_PREFIX_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
8723 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008724{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008725 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008726 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008727
Michal Vasko400e9672021-01-11 13:39:17 +01008728 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008729 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8730 LOGARG(NULL, "Current module must be set if schema format is used.");
8731 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008732 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008733
8734 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008735 memset(set, 0, sizeof *set);
8736 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008737 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8738 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 +01008739 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008740
Michal Vasko400e9672021-01-11 13:39:17 +01008741 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008742 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008743 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008744 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8745 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008746 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008747 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008748 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008749
Radek Krejciddace2c2021-01-08 11:30:56 +01008750 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008751
Michal Vasko03ff5a72019-09-11 13:49:33 +02008752 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01008753 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
8754
Radek Krejciddace2c2021-01-08 11:30:56 +01008755 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008756 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008757}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008758
8759API const char *
8760lyxp_get_expr(const struct lyxp_expr *path)
8761{
8762 if (!path) {
8763 return NULL;
8764 }
8765
8766 return path->expr;
8767}