blob: e54198a54a1b783546ac246e7c61acd5cf1f1d5e [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"
Radek Krejci859a15a2021-03-05 20:56:59 +010044#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020045#include "tree_schema_internal.h"
46#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020047
Michal Vasko004d3152020-06-11 19:59:22 +020048static 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 +020049static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype,
50 struct lyxp_set *set, uint32_t options);
Michal Vasko03ff5a72019-09-11 13:49:33 +020051
52/**
53 * @brief Print the type of an XPath \p set.
54 *
55 * @param[in] set Set to use.
56 * @return Set type string.
57 */
58static const char *
59print_set_type(struct lyxp_set *set)
60{
61 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020062 case LYXP_SET_NODE_SET:
63 return "node set";
64 case LYXP_SET_SCNODE_SET:
65 return "schema node set";
66 case LYXP_SET_BOOLEAN:
67 return "boolean";
68 case LYXP_SET_NUMBER:
69 return "number";
70 case LYXP_SET_STRING:
71 return "string";
72 }
73
74 return NULL;
75}
76
Michal Vasko24cddf82020-06-01 08:17:01 +020077const char *
78lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020079{
80 switch (tok) {
81 case LYXP_TOKEN_PAR1:
82 return "(";
83 case LYXP_TOKEN_PAR2:
84 return ")";
85 case LYXP_TOKEN_BRACK1:
86 return "[";
87 case LYXP_TOKEN_BRACK2:
88 return "]";
89 case LYXP_TOKEN_DOT:
90 return ".";
91 case LYXP_TOKEN_DDOT:
92 return "..";
93 case LYXP_TOKEN_AT:
94 return "@";
95 case LYXP_TOKEN_COMMA:
96 return ",";
97 case LYXP_TOKEN_NAMETEST:
98 return "NameTest";
99 case LYXP_TOKEN_NODETYPE:
100 return "NodeType";
101 case LYXP_TOKEN_FUNCNAME:
102 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200103 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200104 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200105 case LYXP_TOKEN_OPER_EQUAL:
106 return "Operator(Equal)";
107 case LYXP_TOKEN_OPER_NEQUAL:
108 return "Operator(Non-equal)";
109 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200110 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200111 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200112 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200113 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200114 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200115 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200116 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200117 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200118 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200119 case LYXP_TOKEN_LITERAL:
120 return "Literal";
121 case LYXP_TOKEN_NUMBER:
122 return "Number";
123 default:
124 LOGINT(NULL);
125 return "";
126 }
127}
128
129/**
130 * @brief Print the whole expression \p exp to debug output.
131 *
132 * @param[in] exp Expression to use.
133 */
134static void
Michal Vasko40308e72020-10-20 16:38:40 +0200135print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200136{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100137#define MSG_BUFFER_SIZE 128
138 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100139 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200140
Radek Krejci52b6d512020-10-12 12:33:17 +0200141 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200142 return;
143 }
144
145 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
146 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200147 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200148 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100149 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200150 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
151 for (j = 1; exp->repeat[i][j]; ++j) {
152 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
153 }
154 strcat(tmp, ")");
155 }
156 LOGDBG(LY_LDGXPATH, tmp);
157 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100158#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200159}
160
161#ifndef NDEBUG
162
163/**
164 * @brief Print XPath set content to debug output.
165 *
166 * @param[in] set Set to print.
167 */
168static void
169print_set_debug(struct lyxp_set *set)
170{
171 uint32_t i;
172 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200173 struct lyxp_set_node *item;
174 struct lyxp_set_scnode *sitem;
175
Radek Krejci52b6d512020-10-12 12:33:17 +0200176 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200177 return;
178 }
179
180 switch (set->type) {
181 case LYXP_SET_NODE_SET:
182 LOGDBG(LY_LDGXPATH, "set NODE SET:");
183 for (i = 0; i < set->used; ++i) {
184 item = &set->val.nodes[i];
185
186 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100187 case LYXP_NODE_NONE:
188 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
189 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200190 case LYXP_NODE_ROOT:
191 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
192 break;
193 case LYXP_NODE_ROOT_CONFIG:
194 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
195 break;
196 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100197 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200198 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200199 item->node->schema->name, LYD_CANON_VALUE(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100200 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200201 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200202 item->node->schema->name, LYD_CANON_VALUE(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200203 } else {
204 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
205 }
206 break;
207 case LYXP_NODE_TEXT:
208 if (item->node->schema->nodetype & LYS_ANYDATA) {
209 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200210 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200211 } else {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200212 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 +0200213 }
214 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100215 case LYXP_NODE_META:
216 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 +0200217 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200218 break;
219 }
220 }
221 break;
222
223 case LYXP_SET_SCNODE_SET:
224 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
225 for (i = 0; i < set->used; ++i) {
226 sitem = &set->val.scnodes[i];
227
228 switch (sitem->type) {
229 case LYXP_NODE_ROOT:
230 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
231 break;
232 case LYXP_NODE_ROOT_CONFIG:
233 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
234 break;
235 case LYXP_NODE_ELEM:
236 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
237 break;
238 default:
239 LOGINT(NULL);
240 break;
241 }
242 }
243 break;
244
Michal Vasko03ff5a72019-09-11 13:49:33 +0200245 case LYXP_SET_BOOLEAN:
246 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200247 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200248 break;
249
250 case LYXP_SET_STRING:
251 LOGDBG(LY_LDGXPATH, "set STRING");
252 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
253 break;
254
255 case LYXP_SET_NUMBER:
256 LOGDBG(LY_LDGXPATH, "set NUMBER");
257
258 if (isnan(set->val.num)) {
259 str = strdup("NaN");
260 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
261 str = strdup("0");
262 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
263 str = strdup("Infinity");
264 } else if (isinf(set->val.num) && signbit(set->val.num)) {
265 str = strdup("-Infinity");
266 } else if ((long long)set->val.num == set->val.num) {
267 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
268 str = NULL;
269 }
270 } else {
271 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
272 str = NULL;
273 }
274 }
275 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
276
277 LOGDBG(LY_LDGXPATH, "\t%s", str);
278 free(str);
279 }
280}
281
282#endif
283
284/**
285 * @brief Realloc the string \p str.
286 *
287 * @param[in] ctx libyang context for logging.
288 * @param[in] needed How much free space is required.
289 * @param[in,out] str Pointer to the string to use.
290 * @param[in,out] used Used bytes in \p str.
291 * @param[in,out] size Allocated bytes in \p str.
292 * @return LY_ERR
293 */
294static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100295cast_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 +0200296{
297 if (*size - *used < needed) {
298 do {
299 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
300 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
301 return LY_EINVAL;
302 }
303 *size += LYXP_STRING_CAST_SIZE_STEP;
304 } while (*size - *used < needed);
305 *str = ly_realloc(*str, *size * sizeof(char));
306 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
307 }
308
309 return LY_SUCCESS;
310}
311
312/**
313 * @brief Cast nodes recursively to one string @p str.
314 *
315 * @param[in] node Node to cast.
316 * @param[in] fake_cont Whether to put the data into a "fake" container.
317 * @param[in] root_type Type of the XPath root.
318 * @param[in] indent Current indent.
319 * @param[in,out] str Resulting string.
320 * @param[in,out] used Used bytes in @p str.
321 * @param[in,out] size Allocated bytes in @p str.
322 * @return LY_ERR
323 */
324static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200325cast_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 +0200326 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200327{
Radek Krejci7f769d72020-07-11 23:13:56 +0200328 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200329 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200330 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200331 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200332 struct lyd_node_any *any;
333 LY_ERR rc;
334
335 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
336 return LY_SUCCESS;
337 }
338
339 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200340 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200341 LY_CHECK_RET(rc);
342 strcpy(*str + (*used - 1), "\n");
343 ++(*used);
344
345 ++indent;
346 }
347
348 switch (node->schema->nodetype) {
349 case LYS_CONTAINER:
350 case LYS_LIST:
351 case LYS_RPC:
352 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200353 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200354 LY_CHECK_RET(rc);
355 strcpy(*str + (*used - 1), "\n");
356 ++(*used);
357
Radek Krejcia1c1e542020-09-29 16:06:52 +0200358 for (child = lyd_child(node); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200359 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
360 LY_CHECK_RET(rc);
361 }
362
363 break;
364
365 case LYS_LEAF:
366 case LYS_LEAFLIST:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200367 value_str = LYD_CANON_VALUE(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200368
369 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200370 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 +0200371 memset(*str + (*used - 1), ' ', indent * 2);
372 *used += indent * 2;
373
374 /* print value */
375 if (*used == 1) {
376 sprintf(*str + (*used - 1), "%s", value_str);
377 *used += strlen(value_str);
378 } else {
379 sprintf(*str + (*used - 1), "%s\n", value_str);
380 *used += strlen(value_str) + 1;
381 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200382
383 break;
384
385 case LYS_ANYXML:
386 case LYS_ANYDATA:
387 any = (struct lyd_node_any *)node;
388 if (!(void *)any->value.tree) {
389 /* no content */
390 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200391 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200392 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200393 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100394
Michal Vasko60ea6352020-06-29 13:39:39 +0200395 if (any->value_type == LYD_ANYDATA_LYB) {
396 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200397 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 +0200398 /* successfully parsed */
399 free(any->value.mem);
400 any->value.tree = tree;
401 any->value_type = LYD_ANYDATA_DATATREE;
402 }
Radek Krejci7931b192020-06-25 17:05:03 +0200403 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200404 }
405
Michal Vasko03ff5a72019-09-11 13:49:33 +0200406 switch (any->value_type) {
407 case LYD_ANYDATA_STRING:
408 case LYD_ANYDATA_XML:
409 case LYD_ANYDATA_JSON:
410 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200411 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200412 break;
413 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200414 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200415 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200416 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100417 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200418 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200419 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200420 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200421 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200422 }
423 }
424
425 line = strtok_r(buf, "\n", &ptr);
426 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200427 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200428 if (rc != LY_SUCCESS) {
429 free(buf);
430 return rc;
431 }
432 memset(*str + (*used - 1), ' ', indent * 2);
433 *used += indent * 2;
434
435 strcpy(*str + (*used - 1), line);
436 *used += strlen(line);
437
438 strcpy(*str + (*used - 1), "\n");
439 *used += 1;
440 } while ((line = strtok_r(NULL, "\n", &ptr)));
441
442 free(buf);
443 break;
444
445 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200446 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200447 }
448
449 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200450 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200451 LY_CHECK_RET(rc);
452 strcpy(*str + (*used - 1), "\n");
453 ++(*used);
454
455 --indent;
456 }
457
458 return LY_SUCCESS;
459}
460
461/**
462 * @brief Cast an element into a string.
463 *
464 * @param[in] node Node to cast.
465 * @param[in] fake_cont Whether to put the data into a "fake" container.
466 * @param[in] root_type Type of the XPath root.
467 * @param[out] str Element cast to dynamically-allocated string.
468 * @return LY_ERR
469 */
470static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200471cast_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 +0200472{
473 uint16_t used, size;
474 LY_ERR rc;
475
476 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200477 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200478 (*str)[0] = '\0';
479 used = 1;
480 size = LYXP_STRING_CAST_SIZE_START;
481
482 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
483 if (rc != LY_SUCCESS) {
484 free(*str);
485 return rc;
486 }
487
488 if (size > used) {
489 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200490 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200491 }
492 return LY_SUCCESS;
493}
494
495/**
496 * @brief Cast a LYXP_SET_NODE_SET set into a string.
497 * Context position aware.
498 *
499 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200500 * @param[out] str Cast dynamically-allocated string.
501 * @return LY_ERR
502 */
503static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100504cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200505{
Michal Vaskoaa677062021-03-09 13:52:53 +0100506 if (!set->used) {
507 *str = strdup("");
508 if (!*str) {
509 LOGMEM_RET(set->ctx);
510 }
511 return LY_SUCCESS;
512 }
513
Michal Vasko03ff5a72019-09-11 13:49:33 +0200514 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100515 case LYXP_NODE_NONE:
516 /* invalid */
517 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200518 case LYXP_NODE_ROOT:
519 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100520 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200521 case LYXP_NODE_ELEM:
522 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100523 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100524 case LYXP_NODE_META:
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200525 *str = strdup(set->val.meta[0].meta->value.canonical);
526 if (!*str) {
527 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200528 }
529 return LY_SUCCESS;
530 }
531
532 LOGINT_RET(set->ctx);
533}
534
535/**
536 * @brief Cast a string into an XPath number.
537 *
538 * @param[in] str String to use.
539 * @return Cast number.
540 */
541static long double
542cast_string_to_number(const char *str)
543{
544 long double num;
545 char *ptr;
546
547 errno = 0;
548 num = strtold(str, &ptr);
549 if (errno || *ptr) {
550 num = NAN;
551 }
552 return num;
553}
554
555/**
556 * @brief Callback for checking value equality.
557 *
Michal Vasko62524a92021-02-26 10:08:50 +0100558 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200559 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200560 * @param[in] val1_p First value.
561 * @param[in] val2_p Second value.
562 * @param[in] mod Whether hash table is being modified.
563 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200564 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200565 */
Radek Krejci857189e2020-09-01 13:26:36 +0200566static ly_bool
567set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200568{
569 struct lyxp_set_hash_node *val1, *val2;
570
571 val1 = (struct lyxp_set_hash_node *)val1_p;
572 val2 = (struct lyxp_set_hash_node *)val2_p;
573
574 if ((val1->node == val2->node) && (val1->type == val2->type)) {
575 return 1;
576 }
577
578 return 0;
579}
580
581/**
582 * @brief Insert node and its hash into set.
583 *
584 * @param[in] set et to insert to.
585 * @param[in] node Node with hash.
586 * @param[in] type Node type.
587 */
588static void
589set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
590{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200591 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200592 uint32_t i, hash;
593 struct lyxp_set_hash_node hnode;
594
595 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
596 /* create hash table and add all the nodes */
597 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
598 for (i = 0; i < set->used; ++i) {
599 hnode.node = set->val.nodes[i].node;
600 hnode.type = set->val.nodes[i].type;
601
602 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
603 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
604 hash = dict_hash_multi(hash, NULL, 0);
605
606 r = lyht_insert(set->ht, &hnode, hash, NULL);
607 assert(!r);
608 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200609
Michal Vasko9d6befd2019-12-11 14:56:56 +0100610 if (hnode.node == node) {
611 /* it was just added, do not add it twice */
612 node = NULL;
613 }
614 }
615 }
616
617 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200618 /* add the new node into hash table */
619 hnode.node = node;
620 hnode.type = type;
621
622 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
623 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
624 hash = dict_hash_multi(hash, NULL, 0);
625
626 r = lyht_insert(set->ht, &hnode, hash, NULL);
627 assert(!r);
628 (void)r;
629 }
630}
631
632/**
633 * @brief Remove node and its hash from set.
634 *
635 * @param[in] set Set to remove from.
636 * @param[in] node Node to remove.
637 * @param[in] type Node type.
638 */
639static void
640set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
641{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200642 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200643 struct lyxp_set_hash_node hnode;
644 uint32_t hash;
645
646 if (set->ht) {
647 hnode.node = node;
648 hnode.type = type;
649
650 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
651 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
652 hash = dict_hash_multi(hash, NULL, 0);
653
654 r = lyht_remove(set->ht, &hnode, hash);
655 assert(!r);
656 (void)r;
657
658 if (!set->ht->used) {
659 lyht_free(set->ht);
660 set->ht = NULL;
661 }
662 }
663}
664
665/**
666 * @brief Check whether node is in set based on its hash.
667 *
668 * @param[in] set Set to search in.
669 * @param[in] node Node to search for.
670 * @param[in] type Node type.
671 * @param[in] skip_idx Index in @p set to skip.
672 * @return LY_ERR
673 */
674static LY_ERR
675set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
676{
677 struct lyxp_set_hash_node hnode, *match_p;
678 uint32_t hash;
679
680 hnode.node = node;
681 hnode.type = type;
682
683 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
684 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
685 hash = dict_hash_multi(hash, NULL, 0);
686
687 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
688 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
689 /* we found it on the index that should be skipped, find another */
690 hnode = *match_p;
691 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
692 /* none other found */
693 return LY_SUCCESS;
694 }
695 }
696
697 return LY_EEXIST;
698 }
699
700 /* not found */
701 return LY_SUCCESS;
702}
703
Michal Vaskod3678892020-05-21 10:06:58 +0200704void
705lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200706{
707 if (!set) {
708 return;
709 }
710
711 if (set->type == LYXP_SET_NODE_SET) {
712 free(set->val.nodes);
713 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200714 } else if (set->type == LYXP_SET_SCNODE_SET) {
715 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200716 lyht_free(set->ht);
717 } else {
718 if (set->type == LYXP_SET_STRING) {
719 free(set->val.str);
720 }
721 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200722 }
Michal Vaskod3678892020-05-21 10:06:58 +0200723
724 set->val.nodes = NULL;
725 set->used = 0;
726 set->size = 0;
727 set->ht = NULL;
728 set->ctx_pos = 0;
729 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200730}
731
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100732/**
733 * @brief Free dynamically-allocated set.
734 *
735 * @param[in] set Set to free.
736 */
737static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200738lyxp_set_free(struct lyxp_set *set)
739{
740 if (!set) {
741 return;
742 }
743
Michal Vaskod3678892020-05-21 10:06:58 +0200744 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200745 free(set);
746}
747
748/**
749 * @brief Initialize set context.
750 *
751 * @param[in] new Set to initialize.
752 * @param[in] set Arbitrary initialized set.
753 */
754static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200755set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200756{
757 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200758 if (set) {
759 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200760 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100761 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200762 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100763 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200764 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200765 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200766 new->prefix_data = set->prefix_data;
Michal Vasko02a77382019-09-12 11:47:35 +0200767 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200768}
769
770/**
771 * @brief Create a deep copy of a set.
772 *
773 * @param[in] set Set to copy.
774 * @return Copy of @p set.
775 */
776static struct lyxp_set *
777set_copy(struct lyxp_set *set)
778{
779 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100780 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200781
782 if (!set) {
783 return NULL;
784 }
785
786 ret = malloc(sizeof *ret);
787 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
788 set_init(ret, set);
789
790 if (set->type == LYXP_SET_SCNODE_SET) {
791 ret->type = set->type;
792
793 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100794 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
795 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200796 uint32_t idx;
797 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
798 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100799 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200800 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200801 lyxp_set_free(ret);
802 return NULL;
803 }
Michal Vaskoba716542019-12-16 10:01:58 +0100804 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200805 }
806 }
807 } else if (set->type == LYXP_SET_NODE_SET) {
808 ret->type = set->type;
809 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
810 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
811 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
812
813 ret->used = ret->size = set->used;
814 ret->ctx_pos = set->ctx_pos;
815 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100816 if (set->ht) {
817 ret->ht = lyht_dup(set->ht);
818 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200819 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200820 memcpy(ret, set, sizeof *ret);
821 if (set->type == LYXP_SET_STRING) {
822 ret->val.str = strdup(set->val.str);
823 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
824 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200825 }
826
827 return ret;
828}
829
830/**
831 * @brief Fill XPath set with a string. Any current data are disposed of.
832 *
833 * @param[in] set Set to fill.
834 * @param[in] string String to fill into \p set.
835 * @param[in] str_len Length of \p string. 0 is a valid value!
836 */
837static void
838set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
839{
Michal Vaskod3678892020-05-21 10:06:58 +0200840 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200841
842 set->type = LYXP_SET_STRING;
843 if ((str_len == 0) && (string[0] != '\0')) {
844 string = "";
845 }
846 set->val.str = strndup(string, str_len);
847}
848
849/**
850 * @brief Fill XPath set with a number. Any current data are disposed of.
851 *
852 * @param[in] set Set to fill.
853 * @param[in] number Number to fill into \p set.
854 */
855static void
856set_fill_number(struct lyxp_set *set, long double number)
857{
Michal Vaskod3678892020-05-21 10:06:58 +0200858 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200859
860 set->type = LYXP_SET_NUMBER;
861 set->val.num = number;
862}
863
864/**
865 * @brief Fill XPath set with a boolean. Any current data are disposed of.
866 *
867 * @param[in] set Set to fill.
868 * @param[in] boolean Boolean to fill into \p set.
869 */
870static void
Radek Krejci857189e2020-09-01 13:26:36 +0200871set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200872{
Michal Vaskod3678892020-05-21 10:06:58 +0200873 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200874
875 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200876 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200877}
878
879/**
880 * @brief Fill XPath set with the value from another set (deep assign).
881 * Any current data are disposed of.
882 *
883 * @param[in] trg Set to fill.
884 * @param[in] src Source set to copy into \p trg.
885 */
886static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200887set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200888{
889 if (!trg || !src) {
890 return;
891 }
892
893 if (trg->type == LYXP_SET_NODE_SET) {
894 free(trg->val.nodes);
895 } else if (trg->type == LYXP_SET_STRING) {
896 free(trg->val.str);
897 }
898 set_init(trg, src);
899
900 if (src->type == LYXP_SET_SCNODE_SET) {
901 trg->type = LYXP_SET_SCNODE_SET;
902 trg->used = src->used;
903 trg->size = src->used;
904
905 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
906 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
907 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
908 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200909 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200910 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200911 set_fill_number(trg, src->val.num);
912 } else if (src->type == LYXP_SET_STRING) {
913 set_fill_string(trg, src->val.str, strlen(src->val.str));
914 } else {
915 if (trg->type == LYXP_SET_NODE_SET) {
916 free(trg->val.nodes);
917 } else if (trg->type == LYXP_SET_STRING) {
918 free(trg->val.str);
919 }
920
Michal Vaskod3678892020-05-21 10:06:58 +0200921 assert(src->type == LYXP_SET_NODE_SET);
922
923 trg->type = LYXP_SET_NODE_SET;
924 trg->used = src->used;
925 trg->size = src->used;
926 trg->ctx_pos = src->ctx_pos;
927 trg->ctx_size = src->ctx_size;
928
929 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
930 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
931 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
932 if (src->ht) {
933 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200934 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200935 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200936 }
937 }
938}
939
940/**
941 * @brief Clear context of all schema nodes.
942 *
943 * @param[in] set Set to clear.
944 */
945static void
946set_scnode_clear_ctx(struct lyxp_set *set)
947{
948 uint32_t i;
949
950 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100951 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
952 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
953 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
954 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200955 }
956 }
957}
958
959/**
960 * @brief Remove a node from a set. Removing last node changes
961 * set into LYXP_SET_EMPTY. Context position aware.
962 *
963 * @param[in] set Set to use.
964 * @param[in] idx Index from @p set of the node to be removed.
965 */
966static void
967set_remove_node(struct lyxp_set *set, uint32_t idx)
968{
969 assert(set && (set->type == LYXP_SET_NODE_SET));
970 assert(idx < set->used);
971
972 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
973
974 --set->used;
975 if (set->used) {
976 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
977 (set->used - idx) * sizeof *set->val.nodes);
978 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200979 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200980 }
981}
982
983/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100984 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200985 *
986 * @param[in] set Set to use.
987 * @param[in] idx Index from @p set of the node to be removed.
988 */
989static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100990set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200991{
992 assert(set && (set->type == LYXP_SET_NODE_SET));
993 assert(idx < set->used);
994
Michal Vasko2caefc12019-11-14 16:07:56 +0100995 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
996 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
997 }
998 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +0200999}
1000
1001/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001002 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001003 * set into LYXP_SET_EMPTY. Context position aware.
1004 *
1005 * @param[in] set Set to consolidate.
1006 */
1007static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001008set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001009{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001010 uint32_t i, orig_used, end = 0;
1011 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001012
Michal Vaskod3678892020-05-21 10:06:58 +02001013 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001014
1015 orig_used = set->used;
1016 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001017 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001018 start = -1;
1019 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001020 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001021 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001022 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001023 end = i;
1024 ++i;
1025 break;
1026 }
1027
1028 ++i;
1029 if (i == orig_used) {
1030 end = i;
1031 }
1032 } while (i < orig_used);
1033
1034 if (start > -1) {
1035 /* move the whole chunk of valid nodes together */
1036 if (set->used != (unsigned)start) {
1037 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1038 }
1039 set->used += end - start;
1040 }
1041 }
Michal Vasko57eab132019-09-24 11:46:26 +02001042}
1043
1044/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001045 * @brief Check for duplicates in a node set.
1046 *
1047 * @param[in] set Set to check.
1048 * @param[in] node Node to look for in @p set.
1049 * @param[in] node_type Type of @p node.
1050 * @param[in] skip_idx Index from @p set to skip.
1051 * @return LY_ERR
1052 */
1053static LY_ERR
1054set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1055{
1056 uint32_t i;
1057
Michal Vasko2caefc12019-11-14 16:07:56 +01001058 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001059 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1060 }
1061
1062 for (i = 0; i < set->used; ++i) {
1063 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1064 continue;
1065 }
1066
1067 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1068 return LY_EEXIST;
1069 }
1070 }
1071
1072 return LY_SUCCESS;
1073}
1074
Radek Krejci857189e2020-09-01 13:26:36 +02001075ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001076lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1077 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001078{
1079 uint32_t i;
1080
1081 for (i = 0; i < set->used; ++i) {
1082 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1083 continue;
1084 }
1085
1086 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001087 if (index_p) {
1088 *index_p = i;
1089 }
1090 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001091 }
1092 }
1093
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001094 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001095}
1096
Michal Vaskoecd62de2019-11-13 12:35:11 +01001097void
1098lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001099{
1100 uint32_t orig_used, i, j;
1101
Michal Vaskod3678892020-05-21 10:06:58 +02001102 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001103
Michal Vaskod3678892020-05-21 10:06:58 +02001104 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001105 return;
1106 }
1107
Michal Vaskod3678892020-05-21 10:06:58 +02001108 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001109 memcpy(set1, set2, sizeof *set1);
1110 return;
1111 }
1112
1113 if (set1->used + set2->used > set1->size) {
1114 set1->size = set1->used + set2->used;
1115 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1116 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1117 }
1118
1119 orig_used = set1->used;
1120
1121 for (i = 0; i < set2->used; ++i) {
1122 for (j = 0; j < orig_used; ++j) {
1123 /* detect duplicities */
1124 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1125 break;
1126 }
1127 }
1128
Michal Vasko0587bce2020-12-10 12:19:21 +01001129 if (j < orig_used) {
1130 /* node is there, but update its status if needed */
1131 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1132 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
1133 }
1134 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001135 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1136 ++set1->used;
1137 }
1138 }
1139
Michal Vaskod3678892020-05-21 10:06:58 +02001140 lyxp_set_free_content(set2);
1141 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001142}
1143
1144/**
1145 * @brief Insert a node into a set. Context position aware.
1146 *
1147 * @param[in] set Set to use.
1148 * @param[in] node Node to insert to @p set.
1149 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1150 * @param[in] node_type Node type of @p node.
1151 * @param[in] idx Index in @p set to insert into.
1152 */
1153static void
1154set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1155{
Michal Vaskod3678892020-05-21 10:06:58 +02001156 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001157
Michal Vaskod3678892020-05-21 10:06:58 +02001158 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001159 /* first item */
1160 if (idx) {
1161 /* no real harm done, but it is a bug */
1162 LOGINT(set->ctx);
1163 idx = 0;
1164 }
1165 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1166 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1167 set->type = LYXP_SET_NODE_SET;
1168 set->used = 0;
1169 set->size = LYXP_SET_SIZE_START;
1170 set->ctx_pos = 1;
1171 set->ctx_size = 1;
1172 set->ht = NULL;
1173 } else {
1174 /* not an empty set */
1175 if (set->used == set->size) {
1176
1177 /* set is full */
1178 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1179 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1180 set->size += LYXP_SET_SIZE_STEP;
1181 }
1182
1183 if (idx > set->used) {
1184 LOGINT(set->ctx);
1185 idx = set->used;
1186 }
1187
1188 /* make space for the new node */
1189 if (idx < set->used) {
1190 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1191 }
1192 }
1193
1194 /* finally assign the value */
1195 set->val.nodes[idx].node = (struct lyd_node *)node;
1196 set->val.nodes[idx].type = node_type;
1197 set->val.nodes[idx].pos = pos;
1198 ++set->used;
1199
Michal Vasko2caefc12019-11-14 16:07:56 +01001200 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1201 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1202 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001203}
1204
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001205LY_ERR
1206lyxp_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 +02001207{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001208 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001209
1210 assert(set->type == LYXP_SET_SCNODE_SET);
1211
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001212 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001213 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001214 } else {
1215 if (set->used == set->size) {
1216 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes);
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001217 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001218 set->size += LYXP_SET_SIZE_STEP;
1219 }
1220
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001221 index = set->used;
1222 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1223 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001224 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001225 ++set->used;
1226 }
1227
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001228 if (index_p) {
1229 *index_p = index;
1230 }
1231
1232 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001233}
1234
1235/**
1236 * @brief Replace a node in a set with another. Context position aware.
1237 *
1238 * @param[in] set Set to use.
1239 * @param[in] node Node to insert to @p set.
1240 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1241 * @param[in] node_type Node type of @p node.
1242 * @param[in] idx Index in @p set of the node to replace.
1243 */
1244static void
1245set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1246{
1247 assert(set && (idx < set->used));
1248
Michal Vasko2caefc12019-11-14 16:07:56 +01001249 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1250 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1251 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001252 set->val.nodes[idx].node = (struct lyd_node *)node;
1253 set->val.nodes[idx].type = node_type;
1254 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001255 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1256 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1257 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001258}
1259
1260/**
1261 * @brief Set all nodes with ctx 1 to a new unique context value.
1262 *
1263 * @param[in] set Set to modify.
1264 * @return New context value.
1265 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001266static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001267set_scnode_new_in_ctx(struct lyxp_set *set)
1268{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001269 uint32_t i;
1270 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001271
1272 assert(set->type == LYXP_SET_SCNODE_SET);
1273
Radek Krejcif13b87b2020-12-01 22:02:17 +01001274 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001275retry:
1276 for (i = 0; i < set->used; ++i) {
1277 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1278 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1279 goto retry;
1280 }
1281 }
1282 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001283 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001284 set->val.scnodes[i].in_ctx = ret_ctx;
1285 }
1286 }
1287
1288 return ret_ctx;
1289}
1290
1291/**
1292 * @brief Get unique @p node position in the data.
1293 *
1294 * @param[in] node Node to find.
1295 * @param[in] node_type Node type of @p node.
1296 * @param[in] root Root node.
1297 * @param[in] root_type Type of the XPath @p root node.
1298 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1299 * be used to increase efficiency and start the DFS from this node.
1300 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1301 * @return Node position.
1302 */
1303static uint32_t
1304get_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 +02001305 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001306{
Michal Vasko56daf732020-08-10 10:57:18 +02001307 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001308 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001309 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001310
1311 assert(prev && prev_pos && !root->prev->next);
1312
1313 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1314 return 0;
1315 }
1316
1317 if (*prev) {
1318 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001319 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001320 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001321 goto dfs_search;
1322 }
1323
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001324 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001325 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001326dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001327 if (*prev && !elem) {
1328 /* resume previous DFS */
1329 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1330 LYD_TREE_DFS_continue = 0;
1331 }
1332
Michal Vasko03ff5a72019-09-11 13:49:33 +02001333 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001334 /* skip */
1335 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001336 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001337 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001338 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001339 break;
1340 }
Michal Vasko56daf732020-08-10 10:57:18 +02001341 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001342 }
Michal Vasko56daf732020-08-10 10:57:18 +02001343
1344 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001345 }
1346
1347 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001348 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001349 break;
1350 }
1351 }
1352
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001353 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001354 if (!(*prev)) {
1355 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001356 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001357 return 0;
1358 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001359 /* start the search again from the beginning */
1360 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001361
Michal Vasko56daf732020-08-10 10:57:18 +02001362 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001363 pos = 1;
1364 goto dfs_search;
1365 }
1366 }
1367
1368 /* remember the last found node for next time */
1369 *prev = node;
1370 *prev_pos = pos;
1371
1372 return pos;
1373}
1374
1375/**
1376 * @brief Assign (fill) missing node positions.
1377 *
1378 * @param[in] set Set to fill positions in.
1379 * @param[in] root Context root node.
1380 * @param[in] root_type Context root type.
1381 * @return LY_ERR
1382 */
1383static LY_ERR
1384set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1385{
1386 const struct lyd_node *prev = NULL, *tmp_node;
1387 uint32_t i, tmp_pos = 0;
1388
1389 for (i = 0; i < set->used; ++i) {
1390 if (!set->val.nodes[i].pos) {
1391 tmp_node = NULL;
1392 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001393 case LYXP_NODE_META:
1394 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001395 if (!tmp_node) {
1396 LOGINT_RET(root->schema->module->ctx);
1397 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001398 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001399 case LYXP_NODE_ELEM:
1400 case LYXP_NODE_TEXT:
1401 if (!tmp_node) {
1402 tmp_node = set->val.nodes[i].node;
1403 }
1404 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1405 break;
1406 default:
1407 /* all roots have position 0 */
1408 break;
1409 }
1410 }
1411 }
1412
1413 return LY_SUCCESS;
1414}
1415
1416/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001417 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001418 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001419 * @param[in] meta Metadata to use.
1420 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001421 */
1422static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001423get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001424{
1425 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001426 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001427
Michal Vasko9f96a052020-03-10 09:41:45 +01001428 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001429 ++pos;
1430 }
1431
Michal Vasko9f96a052020-03-10 09:41:45 +01001432 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001433 return pos;
1434}
1435
1436/**
1437 * @brief Compare 2 nodes in respect to XPath document order.
1438 *
1439 * @param[in] item1 1st node.
1440 * @param[in] item2 2nd node.
1441 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1442 */
1443static int
1444set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1445{
Michal Vasko9f96a052020-03-10 09:41:45 +01001446 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001447
1448 if (item1->pos < item2->pos) {
1449 return -1;
1450 }
1451
1452 if (item1->pos > item2->pos) {
1453 return 1;
1454 }
1455
1456 /* node positions are equal, the fun case */
1457
1458 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1459 /* special case since text nodes are actually saved as their parents */
1460 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1461 if (item1->type == LYXP_NODE_ELEM) {
1462 assert(item2->type == LYXP_NODE_TEXT);
1463 return -1;
1464 } else {
1465 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1466 return 1;
1467 }
1468 }
1469
Michal Vasko9f96a052020-03-10 09:41:45 +01001470 /* we need meta positions now */
1471 if (item1->type == LYXP_NODE_META) {
1472 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001473 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001474 if (item2->type == LYXP_NODE_META) {
1475 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001476 }
1477
Michal Vasko9f96a052020-03-10 09:41:45 +01001478 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001479 /* check for duplicates */
1480 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001481 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001482 return 0;
1483 }
1484
Michal Vasko9f96a052020-03-10 09:41:45 +01001485 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001486 /* elem is always first, 2nd node is after it */
1487 if (item1->type == LYXP_NODE_ELEM) {
1488 assert(item2->type != LYXP_NODE_ELEM);
1489 return -1;
1490 }
1491
Michal Vasko9f96a052020-03-10 09:41:45 +01001492 /* 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 +02001493 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001494 if (((item1->type == LYXP_NODE_TEXT) &&
1495 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1496 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1497 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1498 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001499 return 1;
1500 }
1501
Michal Vasko9f96a052020-03-10 09:41:45 +01001502 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001503 /* 2nd is after 1st */
1504 return -1;
1505}
1506
1507/**
1508 * @brief Set cast for comparisons.
1509 *
1510 * @param[in] trg Target set to cast source into.
1511 * @param[in] src Source set.
1512 * @param[in] type Target set type.
1513 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001514 * @return LY_ERR
1515 */
1516static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001517set_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 +02001518{
1519 assert(src->type == LYXP_SET_NODE_SET);
1520
1521 set_init(trg, src);
1522
1523 /* insert node into target set */
1524 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1525
1526 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001527 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001528}
1529
Michal Vasko4c7763f2020-07-27 17:40:37 +02001530/**
1531 * @brief Set content canonization for comparisons.
1532 *
1533 * @param[in] trg Target set to put the canononized source into.
1534 * @param[in] src Source set.
1535 * @param[in] xp_node Source XPath node/meta to use for canonization.
1536 * @return LY_ERR
1537 */
1538static LY_ERR
1539set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1540{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001541 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001542 struct lyd_value val;
1543 struct ly_err_item *err = NULL;
1544 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001545 LY_ERR rc;
1546
1547 /* is there anything to canonize even? */
1548 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1549 /* do we have a type to use for canonization? */
1550 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1551 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1552 } else if (xp_node->type == LYXP_NODE_META) {
1553 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1554 }
1555 }
1556 if (!type) {
1557 goto fill;
1558 }
1559
1560 if (src->type == LYXP_SET_NUMBER) {
1561 /* canonize number */
1562 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1563 LOGMEM(src->ctx);
1564 return LY_EMEM;
1565 }
1566 } else {
1567 /* canonize string */
1568 str = strdup(src->val.str);
1569 }
1570
1571 /* ignore errors, the value may not satisfy schema constraints */
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001572 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 +01001573 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001574 ly_err_free(err);
1575 if (rc) {
1576 /* invalid value */
Radek IÅ¡a48460222021-03-02 15:18:32 +01001577 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001578 goto fill;
1579 }
1580
Michal Vasko4c7763f2020-07-27 17:40:37 +02001581 /* use the canonized value */
1582 set_init(trg, src);
1583 trg->type = src->type;
1584 if (src->type == LYXP_SET_NUMBER) {
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001585 trg->val.num = strtold(val.canonical, &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001586 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1587 } else {
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001588 trg->val.str = strdup(val.canonical);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001589 }
1590 type->plugin->free(src->ctx, &val);
1591 return LY_SUCCESS;
1592
1593fill:
1594 /* no canonization needed/possible */
1595 set_fill_set(trg, src);
1596 return LY_SUCCESS;
1597}
1598
Michal Vasko03ff5a72019-09-11 13:49:33 +02001599#ifndef NDEBUG
1600
1601/**
1602 * @brief Bubble sort @p set into XPath document order.
1603 * Context position aware. Unused in the 'Release' build target.
1604 *
1605 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001606 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1607 */
1608static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001609set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001610{
1611 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001612 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001613 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001614 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001615 struct lyxp_set_node item;
1616 struct lyxp_set_hash_node hnode;
1617 uint64_t hash;
1618
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001619 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001620 return 0;
1621 }
1622
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001623 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001624 for (root = set->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001625 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001626
1627 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001628 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001629 return -1;
1630 }
1631
1632 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1633 print_set_debug(set);
1634
1635 for (i = 0; i < set->used; ++i) {
1636 inverted = 0;
1637 change = 0;
1638
1639 for (j = 1; j < set->used - i; ++j) {
1640 /* compare node positions */
1641 if (inverted) {
1642 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1643 } else {
1644 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1645 }
1646
1647 /* swap if needed */
1648 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1649 change = 1;
1650
1651 item = set->val.nodes[j - 1];
1652 set->val.nodes[j - 1] = set->val.nodes[j];
1653 set->val.nodes[j] = item;
1654 } else {
1655 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1656 inverted = !inverted;
1657 }
1658 }
1659
1660 ++ret;
1661
1662 if (!change) {
1663 break;
1664 }
1665 }
1666
1667 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1668 print_set_debug(set);
1669
1670 /* check node hashes */
1671 if (set->used >= LYD_HT_MIN_ITEMS) {
1672 assert(set->ht);
1673 for (i = 0; i < set->used; ++i) {
1674 hnode.node = set->val.nodes[i].node;
1675 hnode.type = set->val.nodes[i].type;
1676
1677 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1678 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1679 hash = dict_hash_multi(hash, NULL, 0);
1680
1681 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1682 }
1683 }
1684
1685 return ret - 1;
1686}
1687
1688/**
1689 * @brief Remove duplicate entries in a sorted node set.
1690 *
1691 * @param[in] set Sorted set to check.
1692 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1693 */
1694static LY_ERR
1695set_sorted_dup_node_clean(struct lyxp_set *set)
1696{
1697 uint32_t i = 0;
1698 LY_ERR ret = LY_SUCCESS;
1699
1700 if (set->used > 1) {
1701 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001702 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1703 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001704 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001705 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001706 }
Michal Vasko57eab132019-09-24 11:46:26 +02001707 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001708 }
1709 }
1710
Michal Vasko2caefc12019-11-14 16:07:56 +01001711 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001712 return ret;
1713}
1714
1715#endif
1716
1717/**
1718 * @brief Merge 2 sorted sets into one.
1719 *
1720 * @param[in,out] trg Set to merge into. Duplicates are removed.
1721 * @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 +02001722 * @return LY_ERR
1723 */
1724static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001725set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001726{
1727 uint32_t i, j, k, count, dup_count;
1728 int cmp;
1729 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001730
Michal Vaskod3678892020-05-21 10:06:58 +02001731 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001732 return LY_EINVAL;
1733 }
1734
Michal Vaskod3678892020-05-21 10:06:58 +02001735 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001736 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001737 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001738 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001739 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001740 return LY_SUCCESS;
1741 }
1742
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001743 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001744 for (root = trg->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001745 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001746
1747 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001748 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001749 return LY_EINT;
1750 }
1751
1752#ifndef NDEBUG
1753 LOGDBG(LY_LDGXPATH, "MERGE target");
1754 print_set_debug(trg);
1755 LOGDBG(LY_LDGXPATH, "MERGE source");
1756 print_set_debug(src);
1757#endif
1758
1759 /* make memory for the merge (duplicates are not detected yet, so space
1760 * will likely be wasted on them, too bad) */
1761 if (trg->size - trg->used < src->used) {
1762 trg->size = trg->used + src->used;
1763
1764 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1765 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1766 }
1767
1768 i = 0;
1769 j = 0;
1770 count = 0;
1771 dup_count = 0;
1772 do {
1773 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1774 if (!cmp) {
1775 if (!count) {
1776 /* duplicate, just skip it */
1777 ++i;
1778 ++j;
1779 } else {
1780 /* we are copying something already, so let's copy the duplicate too,
1781 * we are hoping that afterwards there are some more nodes to
1782 * copy and this way we can copy them all together */
1783 ++count;
1784 ++dup_count;
1785 ++i;
1786 ++j;
1787 }
1788 } else if (cmp < 0) {
1789 /* inserting src node into trg, just remember it for now */
1790 ++count;
1791 ++i;
1792
1793 /* insert the hash now */
1794 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1795 } else if (count) {
1796copy_nodes:
1797 /* time to actually copy the nodes, we have found the largest block of nodes */
1798 memmove(&trg->val.nodes[j + (count - dup_count)],
1799 &trg->val.nodes[j],
1800 (trg->used - j) * sizeof *trg->val.nodes);
1801 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1802
1803 trg->used += count - dup_count;
1804 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1805 j += count - dup_count;
1806
1807 count = 0;
1808 dup_count = 0;
1809 } else {
1810 ++j;
1811 }
1812 } while ((i < src->used) && (j < trg->used));
1813
1814 if ((i < src->used) || count) {
1815 /* insert all the hashes first */
1816 for (k = i; k < src->used; ++k) {
1817 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1818 }
1819
1820 /* loop ended, but we need to copy something at trg end */
1821 count += src->used - i;
1822 i = src->used;
1823 goto copy_nodes;
1824 }
1825
1826 /* we are inserting hashes before the actual node insert, which causes
1827 * situations when there were initially not enough items for a hash table,
1828 * but even after some were inserted, hash table was not created (during
1829 * insertion the number of items is not updated yet) */
1830 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1831 set_insert_node_hash(trg, NULL, 0);
1832 }
1833
1834#ifndef NDEBUG
1835 LOGDBG(LY_LDGXPATH, "MERGE result");
1836 print_set_debug(trg);
1837#endif
1838
Michal Vaskod3678892020-05-21 10:06:58 +02001839 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001840 return LY_SUCCESS;
1841}
1842
1843/*
1844 * (re)parse functions
1845 *
1846 * Parse functions parse the expression into
1847 * tokens (syntactic analysis).
1848 *
1849 * Reparse functions perform semantic analysis
1850 * (do not save the result, just a check) of
1851 * the expression and fill repeat indices.
1852 */
1853
Michal Vasko14676352020-05-29 11:35:55 +02001854LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001855lyxp_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 +02001856{
Michal Vasko004d3152020-06-11 19:59:22 +02001857 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001858 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001859 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001860 }
Michal Vasko14676352020-05-29 11:35:55 +02001861 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001862 }
1863
Michal Vasko004d3152020-06-11 19:59:22 +02001864 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001865 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001866 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001867 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001868 }
Michal Vasko14676352020-05-29 11:35:55 +02001869 return LY_ENOT;
1870 }
1871
1872 return LY_SUCCESS;
1873}
1874
Michal Vasko004d3152020-06-11 19:59:22 +02001875LY_ERR
1876lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1877{
1878 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1879
1880 /* skip the token */
1881 ++(*tok_idx);
1882
1883 return LY_SUCCESS;
1884}
1885
Michal Vasko14676352020-05-29 11:35:55 +02001886/* just like lyxp_check_token() but tests for 2 tokens */
1887static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001888exp_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 +02001889 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001890{
Michal Vasko004d3152020-06-11 19:59:22 +02001891 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001892 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001893 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001894 }
1895 return LY_EINCOMPLETE;
1896 }
1897
Michal Vasko004d3152020-06-11 19:59:22 +02001898 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001899 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001900 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001901 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001902 }
1903 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001904 }
1905
1906 return LY_SUCCESS;
1907}
1908
1909/**
1910 * @brief Stack operation push on the repeat array.
1911 *
1912 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001913 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001914 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1915 */
1916static void
Michal Vasko004d3152020-06-11 19:59:22 +02001917exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001918{
1919 uint16_t i;
1920
Michal Vasko004d3152020-06-11 19:59:22 +02001921 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001922 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001923 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1924 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1925 exp->repeat[tok_idx][i] = repeat_op_idx;
1926 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001927 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001928 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1929 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1930 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001931 }
1932}
1933
1934/**
1935 * @brief Reparse Predicate. Logs directly on error.
1936 *
1937 * [7] Predicate ::= '[' Expr ']'
1938 *
1939 * @param[in] ctx Context for logging.
1940 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001941 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001942 * @return LY_ERR
1943 */
1944static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001945reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001946{
1947 LY_ERR rc;
1948
Michal Vasko004d3152020-06-11 19:59:22 +02001949 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001950 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001951 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001952
Michal Vasko004d3152020-06-11 19:59:22 +02001953 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001954 LY_CHECK_RET(rc);
1955
Michal Vasko004d3152020-06-11 19:59:22 +02001956 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001957 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001958 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001959
1960 return LY_SUCCESS;
1961}
1962
1963/**
1964 * @brief Reparse RelativeLocationPath. Logs directly on error.
1965 *
1966 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1967 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1968 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1969 *
1970 * @param[in] ctx Context for logging.
1971 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001972 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001973 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1974 */
1975static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001976reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001977{
1978 LY_ERR rc;
1979
Michal Vasko004d3152020-06-11 19:59:22 +02001980 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001981 LY_CHECK_RET(rc);
1982
1983 goto step;
1984 do {
1985 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001986 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001987
Michal Vasko004d3152020-06-11 19:59:22 +02001988 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001989 LY_CHECK_RET(rc);
1990step:
1991 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001992 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001993 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001994 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001995 break;
1996
1997 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001998 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001999 break;
2000
2001 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002002 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002003
Michal Vasko004d3152020-06-11 19:59:22 +02002004 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002005 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002006 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002007 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 +02002008 return LY_EVALID;
2009 }
Radek Krejci0f969882020-08-21 16:56:47 +02002010 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002011 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002012 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002013 goto reparse_predicate;
2014 break;
2015
2016 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002017 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002018
2019 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002020 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002021 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002022 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002023
2024 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002025 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002026 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002027 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002028
2029reparse_predicate:
2030 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002031 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2032 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002033 LY_CHECK_RET(rc);
2034 }
2035 break;
2036 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002037 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 +02002038 return LY_EVALID;
2039 }
Michal Vasko004d3152020-06-11 19:59:22 +02002040 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002041
2042 return LY_SUCCESS;
2043}
2044
2045/**
2046 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2047 *
2048 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2049 *
2050 * @param[in] ctx Context for logging.
2051 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002052 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002053 * @return LY_ERR
2054 */
2055static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002056reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002057{
2058 LY_ERR rc;
2059
Michal Vasko004d3152020-06-11 19:59:22 +02002060 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 +02002061
2062 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002063 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002064 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002065 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002066
Michal Vasko004d3152020-06-11 19:59:22 +02002067 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002068 return LY_SUCCESS;
2069 }
Michal Vasko004d3152020-06-11 19:59:22 +02002070 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002071 case LYXP_TOKEN_DOT:
2072 case LYXP_TOKEN_DDOT:
2073 case LYXP_TOKEN_AT:
2074 case LYXP_TOKEN_NAMETEST:
2075 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002076 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002077 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002078 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002079 default:
2080 break;
2081 }
2082
Michal Vasko03ff5a72019-09-11 13:49:33 +02002083 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002084 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002085 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002086
Michal Vasko004d3152020-06-11 19:59:22 +02002087 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002088 LY_CHECK_RET(rc);
2089 }
2090
2091 return LY_SUCCESS;
2092}
2093
2094/**
2095 * @brief Reparse FunctionCall. Logs directly on error.
2096 *
2097 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2098 *
2099 * @param[in] ctx Context for logging.
2100 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002101 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002102 * @return LY_ERR
2103 */
2104static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002105reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002106{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002107 int8_t min_arg_count = -1;
2108 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002109 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002110 LY_ERR rc;
2111
Michal Vasko004d3152020-06-11 19:59:22 +02002112 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002113 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002114 func_tok_idx = *tok_idx;
2115 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002116 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002117 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
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]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002121 min_arg_count = 1;
2122 max_arg_count = 1;
2123 }
2124 break;
2125 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002126 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002127 min_arg_count = 1;
2128 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002129 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002130 min_arg_count = 0;
2131 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002132 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002133 min_arg_count = 0;
2134 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002135 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002136 min_arg_count = 0;
2137 max_arg_count = 0;
2138 }
2139 break;
2140 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002141 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 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]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002145 min_arg_count = 0;
2146 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002147 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002148 min_arg_count = 1;
2149 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002150 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002151 min_arg_count = 1;
2152 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002153 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002154 min_arg_count = 1;
2155 max_arg_count = 1;
2156 }
2157 break;
2158 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002159 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002160 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002161 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002162 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002163 min_arg_count = 0;
2164 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002165 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002166 min_arg_count = 0;
2167 max_arg_count = 1;
2168 }
2169 break;
2170 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002171 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002172 min_arg_count = 1;
2173 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002174 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002175 min_arg_count = 1;
2176 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002177 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002178 min_arg_count = 0;
2179 max_arg_count = 0;
2180 }
2181 break;
2182 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002183 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002184 min_arg_count = 2;
2185 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002186 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002187 min_arg_count = 0;
2188 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002189 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002190 min_arg_count = 2;
2191 max_arg_count = 2;
2192 }
2193 break;
2194 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002195 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002196 min_arg_count = 2;
2197 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002198 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002199 min_arg_count = 3;
2200 max_arg_count = 3;
2201 }
2202 break;
2203 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002204 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002205 min_arg_count = 0;
2206 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002207 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002208 min_arg_count = 1;
2209 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002210 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002211 min_arg_count = 2;
2212 max_arg_count = 2;
2213 }
2214 break;
2215 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002216 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002217 min_arg_count = 2;
2218 max_arg_count = 2;
2219 }
2220 break;
2221 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002222 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002223 min_arg_count = 2;
2224 max_arg_count = 2;
2225 }
2226 break;
2227 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002228 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
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]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002232 min_arg_count = 0;
2233 max_arg_count = 1;
2234 }
2235 break;
2236 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002237 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002238 min_arg_count = 0;
2239 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002240 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002241 min_arg_count = 2;
2242 max_arg_count = 2;
2243 }
2244 break;
2245 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002246 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002247 min_arg_count = 2;
2248 max_arg_count = 2;
2249 }
2250 break;
2251 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002252 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002253 min_arg_count = 2;
2254 max_arg_count = 2;
2255 }
2256 break;
2257 }
2258 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002259 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 +02002260 return LY_EINVAL;
2261 }
Michal Vasko004d3152020-06-11 19:59:22 +02002262 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002263
2264 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002265 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002266 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002267 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002268
2269 /* ( Expr ( ',' Expr )* )? */
2270 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002271 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002272 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002273 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002274 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002275 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002276 LY_CHECK_RET(rc);
2277 }
Michal Vasko004d3152020-06-11 19:59:22 +02002278 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2279 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002280
2281 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002282 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002283 LY_CHECK_RET(rc);
2284 }
2285
2286 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002287 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002288 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002289 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002290
Radek Krejci857189e2020-09-01 13:26:36 +02002291 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002292 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 +02002293 return LY_EVALID;
2294 }
2295
2296 return LY_SUCCESS;
2297}
2298
2299/**
2300 * @brief Reparse PathExpr. Logs directly on error.
2301 *
2302 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2303 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2304 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2305 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2306 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2307 *
2308 * @param[in] ctx Context for logging.
2309 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002310 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002311 * @return LY_ERR
2312 */
2313static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002314reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002315{
2316 LY_ERR rc;
2317
Michal Vasko004d3152020-06-11 19:59:22 +02002318 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002319 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002320 }
2321
Michal Vasko004d3152020-06-11 19:59:22 +02002322 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002323 case LYXP_TOKEN_PAR1:
2324 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002325 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002326
Michal Vasko004d3152020-06-11 19:59:22 +02002327 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002328 LY_CHECK_RET(rc);
2329
Michal Vasko004d3152020-06-11 19:59:22 +02002330 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002331 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002332 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002333 goto predicate;
2334 break;
2335 case LYXP_TOKEN_DOT:
2336 case LYXP_TOKEN_DDOT:
2337 case LYXP_TOKEN_AT:
2338 case LYXP_TOKEN_NAMETEST:
2339 case LYXP_TOKEN_NODETYPE:
2340 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002341 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002342 LY_CHECK_RET(rc);
2343 break;
2344 case LYXP_TOKEN_FUNCNAME:
2345 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002346 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002347 LY_CHECK_RET(rc);
2348 goto predicate;
2349 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002350 case LYXP_TOKEN_OPER_PATH:
2351 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002352 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002353 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002354 LY_CHECK_RET(rc);
2355 break;
2356 case LYXP_TOKEN_LITERAL:
2357 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002358 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002359 goto predicate;
2360 break;
2361 case LYXP_TOKEN_NUMBER:
2362 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002363 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002364 goto predicate;
2365 break;
2366 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002367 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 +02002368 return LY_EVALID;
2369 }
2370
2371 return LY_SUCCESS;
2372
2373predicate:
2374 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002375 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2376 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002377 LY_CHECK_RET(rc);
2378 }
2379
2380 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002381 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002382
2383 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002384 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002385
Michal Vasko004d3152020-06-11 19:59:22 +02002386 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002387 LY_CHECK_RET(rc);
2388 }
2389
2390 return LY_SUCCESS;
2391}
2392
2393/**
2394 * @brief Reparse UnaryExpr. Logs directly on error.
2395 *
2396 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2397 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2398 *
2399 * @param[in] ctx Context for logging.
2400 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002401 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002402 * @return LY_ERR
2403 */
2404static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002405reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002406{
2407 uint16_t prev_exp;
2408 LY_ERR rc;
2409
2410 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002411 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002412 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2413 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002414 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002415 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002416 }
2417
2418 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002419 prev_exp = *tok_idx;
2420 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002421 LY_CHECK_RET(rc);
2422
2423 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002424 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002425 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002426 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002427
Michal Vasko004d3152020-06-11 19:59:22 +02002428 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002429 LY_CHECK_RET(rc);
2430 }
2431
2432 return LY_SUCCESS;
2433}
2434
2435/**
2436 * @brief Reparse AdditiveExpr. Logs directly on error.
2437 *
2438 * [15] AdditiveExpr ::= MultiplicativeExpr
2439 * | AdditiveExpr '+' MultiplicativeExpr
2440 * | AdditiveExpr '-' MultiplicativeExpr
2441 * [16] MultiplicativeExpr ::= UnaryExpr
2442 * | MultiplicativeExpr '*' UnaryExpr
2443 * | MultiplicativeExpr 'div' UnaryExpr
2444 * | MultiplicativeExpr 'mod' UnaryExpr
2445 *
2446 * @param[in] ctx Context for logging.
2447 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002448 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002449 * @return LY_ERR
2450 */
2451static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002452reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002453{
2454 uint16_t prev_add_exp, prev_mul_exp;
2455 LY_ERR rc;
2456
Michal Vasko004d3152020-06-11 19:59:22 +02002457 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002458 goto reparse_multiplicative_expr;
2459
2460 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002461 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2462 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002463 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002464 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002465
2466reparse_multiplicative_expr:
2467 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002468 prev_mul_exp = *tok_idx;
2469 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002470 LY_CHECK_RET(rc);
2471
2472 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002473 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2474 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002475 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002476 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002477
Michal Vasko004d3152020-06-11 19:59:22 +02002478 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002479 LY_CHECK_RET(rc);
2480 }
2481 }
2482
2483 return LY_SUCCESS;
2484}
2485
2486/**
2487 * @brief Reparse EqualityExpr. Logs directly on error.
2488 *
2489 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2490 * | EqualityExpr '!=' RelationalExpr
2491 * [14] RelationalExpr ::= AdditiveExpr
2492 * | RelationalExpr '<' AdditiveExpr
2493 * | RelationalExpr '>' AdditiveExpr
2494 * | RelationalExpr '<=' AdditiveExpr
2495 * | RelationalExpr '>=' AdditiveExpr
2496 *
2497 * @param[in] ctx Context for logging.
2498 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002499 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002500 * @return LY_ERR
2501 */
2502static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002503reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002504{
2505 uint16_t prev_eq_exp, prev_rel_exp;
2506 LY_ERR rc;
2507
Michal Vasko004d3152020-06-11 19:59:22 +02002508 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002509 goto reparse_additive_expr;
2510
2511 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002512 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002513 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002514 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002515
2516reparse_additive_expr:
2517 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002518 prev_rel_exp = *tok_idx;
2519 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002520 LY_CHECK_RET(rc);
2521
2522 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002523 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002524 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002525 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002526
Michal Vasko004d3152020-06-11 19:59:22 +02002527 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002528 LY_CHECK_RET(rc);
2529 }
2530 }
2531
2532 return LY_SUCCESS;
2533}
2534
2535/**
2536 * @brief Reparse OrExpr. Logs directly on error.
2537 *
2538 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2539 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2540 *
2541 * @param[in] ctx Context for logging.
2542 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002543 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002544 * @return LY_ERR
2545 */
2546static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002547reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002548{
2549 uint16_t prev_or_exp, prev_and_exp;
2550 LY_ERR rc;
2551
Michal Vasko004d3152020-06-11 19:59:22 +02002552 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002553 goto reparse_equality_expr;
2554
2555 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002556 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 +02002557 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002558 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002559
2560reparse_equality_expr:
2561 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002562 prev_and_exp = *tok_idx;
2563 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002564 LY_CHECK_RET(rc);
2565
2566 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002567 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 +02002568 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002569 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002570
Michal Vasko004d3152020-06-11 19:59:22 +02002571 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002572 LY_CHECK_RET(rc);
2573 }
2574 }
2575
2576 return LY_SUCCESS;
2577}
Radek Krejcib1646a92018-11-02 16:08:26 +01002578
2579/**
2580 * @brief Parse NCName.
2581 *
2582 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002583 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002584 */
Radek Krejcid4270262019-01-07 15:07:25 +01002585static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002586parse_ncname(const char *ncname)
2587{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002588 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002589 size_t size;
2590 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002591
2592 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2593 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2594 return len;
2595 }
2596
2597 do {
2598 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002599 if (!*ncname) {
2600 break;
2601 }
Radek Krejcid4270262019-01-07 15:07:25 +01002602 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002603 } while (is_xmlqnamechar(uc) && (uc != ':'));
2604
2605 return len;
2606}
2607
2608/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002609 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002610 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002611 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002612 * @param[in] exp Expression to use.
2613 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002614 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002615 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002616 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002617 */
2618static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002619exp_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 +01002620{
2621 uint32_t prev;
2622
2623 if (exp->used == exp->size) {
2624 prev = exp->size;
2625 exp->size += LYXP_EXPR_SIZE_STEP;
2626 if (prev > exp->size) {
2627 LOGINT(ctx);
2628 return LY_EINT;
2629 }
2630
2631 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2632 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2633 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2634 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2635 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2636 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2637 }
2638
2639 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002640 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002641 exp->tok_len[exp->used] = tok_len;
2642 ++exp->used;
2643 return LY_SUCCESS;
2644}
2645
2646void
Michal Vasko14676352020-05-29 11:35:55 +02002647lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002648{
2649 uint16_t i;
2650
2651 if (!expr) {
2652 return;
2653 }
2654
2655 lydict_remove(ctx, expr->expr);
2656 free(expr->tokens);
2657 free(expr->tok_pos);
2658 free(expr->tok_len);
2659 if (expr->repeat) {
2660 for (i = 0; i < expr->used; ++i) {
2661 free(expr->repeat[i]);
2662 }
2663 }
2664 free(expr->repeat);
2665 free(expr);
2666}
2667
Radek Krejcif03a9e22020-09-18 20:09:31 +02002668LY_ERR
2669lyxp_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 +01002670{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002671 LY_ERR ret = LY_SUCCESS;
2672 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002673 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002674 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002675 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002676 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002677
Radek Krejcif03a9e22020-09-18 20:09:31 +02002678 assert(expr_p);
2679
2680 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002681 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002682 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002683 }
2684
2685 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002686 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002687 }
2688 if (expr_len > UINT16_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002689 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002690 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002691 }
2692
2693 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002694 expr = calloc(1, sizeof *expr);
2695 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2696 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2697 expr->used = 0;
2698 expr->size = LYXP_EXPR_SIZE_START;
2699 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2700 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002701
Radek Krejcif03a9e22020-09-18 20:09:31 +02002702 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2703 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002704
Radek Krejcif03a9e22020-09-18 20:09:31 +02002705 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2706 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002707
Michal Vasko004d3152020-06-11 19:59:22 +02002708 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002709 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002710
Radek Krejcif03a9e22020-09-18 20:09:31 +02002711 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002712 ++parsed;
2713 }
2714
2715 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002716 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002717
2718 /* '(' */
2719 tok_len = 1;
2720 tok_type = LYXP_TOKEN_PAR1;
2721
Radek Krejcif03a9e22020-09-18 20:09:31 +02002722 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002723 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002724 if (((expr->tok_len[expr->used - 1] == 4) &&
2725 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2726 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2727 ((expr->tok_len[expr->used - 1] == 7) &&
2728 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002729 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002730 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002731 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002732 }
2733 prev_function_check = 0;
2734 }
2735
Radek Krejcif03a9e22020-09-18 20:09:31 +02002736 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002737
2738 /* ')' */
2739 tok_len = 1;
2740 tok_type = LYXP_TOKEN_PAR2;
2741
Radek Krejcif03a9e22020-09-18 20:09:31 +02002742 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002743
2744 /* '[' */
2745 tok_len = 1;
2746 tok_type = LYXP_TOKEN_BRACK1;
2747
Radek Krejcif03a9e22020-09-18 20:09:31 +02002748 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002749
2750 /* ']' */
2751 tok_len = 1;
2752 tok_type = LYXP_TOKEN_BRACK2;
2753
Radek Krejcif03a9e22020-09-18 20:09:31 +02002754 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002755
2756 /* '..' */
2757 tok_len = 2;
2758 tok_type = LYXP_TOKEN_DDOT;
2759
Radek Krejcif03a9e22020-09-18 20:09:31 +02002760 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002761
2762 /* '.' */
2763 tok_len = 1;
2764 tok_type = LYXP_TOKEN_DOT;
2765
Radek Krejcif03a9e22020-09-18 20:09:31 +02002766 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002767
2768 /* '@' */
2769 tok_len = 1;
2770 tok_type = LYXP_TOKEN_AT;
2771
Radek Krejcif03a9e22020-09-18 20:09:31 +02002772 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002773
2774 /* ',' */
2775 tok_len = 1;
2776 tok_type = LYXP_TOKEN_COMMA;
2777
Radek Krejcif03a9e22020-09-18 20:09:31 +02002778 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002779
2780 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002781 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2782 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002783 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002784 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002785 ++tok_len;
2786 tok_type = LYXP_TOKEN_LITERAL;
2787
Radek Krejcif03a9e22020-09-18 20:09:31 +02002788 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002789
2790 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002791 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2792 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002793 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002794 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002795 ++tok_len;
2796 tok_type = LYXP_TOKEN_LITERAL;
2797
Radek Krejcif03a9e22020-09-18 20:09:31 +02002798 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002799
2800 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002801 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2802 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002803 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002804 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002805 }
2806 tok_type = LYXP_TOKEN_NUMBER;
2807
Radek Krejcif03a9e22020-09-18 20:09:31 +02002808 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002809
2810 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002811 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002812 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002813 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002814 } else {
2815 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002816 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002817 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002818
Radek Krejcif03a9e22020-09-18 20:09:31 +02002819 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002820
Michal Vasko3e48bf32020-06-01 08:39:07 +02002821 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002822 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002823 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2824
Radek Krejcif03a9e22020-09-18 20:09:31 +02002825 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002826
2827 /* Operator '<=', '>=' */
2828 tok_len = 2;
2829 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002830
Radek Krejcif03a9e22020-09-18 20:09:31 +02002831 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002832
2833 /* Operator '|' */
2834 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002835 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002836
Radek Krejcif03a9e22020-09-18 20:09:31 +02002837 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002838
2839 /* Operator '+', '-' */
2840 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002841 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002842
Radek Krejcif03a9e22020-09-18 20:09:31 +02002843 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002844
Michal Vasko3e48bf32020-06-01 08:39:07 +02002845 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002846 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002847 tok_type = LYXP_TOKEN_OPER_EQUAL;
2848
Radek Krejcif03a9e22020-09-18 20:09:31 +02002849 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002850
2851 /* Operator '<', '>' */
2852 tok_len = 1;
2853 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002854
Michal Vasko69730152020-10-09 16:30:07 +02002855 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2856 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2857 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2858 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2859 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2860 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2861 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2862 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2863 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2864 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2865 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2866 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002867
2868 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002869 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002870 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002871 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002872
Radek Krejcif03a9e22020-09-18 20:09:31 +02002873 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002874 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002875 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002876
Radek Krejcif03a9e22020-09-18 20:09:31 +02002877 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002878 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002879 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002880
Radek Krejcif03a9e22020-09-18 20:09:31 +02002881 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002882 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002883 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002884
2885 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002886 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 +02002887 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 +02002888 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002889 goto error;
2890 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002891 LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002892 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002893 goto error;
2894 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002895 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002896
2897 /* NameTest '*' */
2898 tok_len = 1;
2899 tok_type = LYXP_TOKEN_NAMETEST;
2900
2901 } else {
2902
2903 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002904 long int ncname_len = parse_ncname(&expr_str[parsed]);
2905 LY_CHECK_ERR_GOTO(ncname_len < 0,
Radek Krejci2efc45b2020-12-22 16:25:44 +01002906 LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002907 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002908 tok_len = ncname_len;
2909
Radek Krejcif03a9e22020-09-18 20:09:31 +02002910 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002911 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002912 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002913 ++tok_len;
2914 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002915 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
2916 LY_CHECK_ERR_GOTO(ncname_len < 0,
Radek Krejci2efc45b2020-12-22 16:25:44 +01002917 LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002918 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002919 tok_len += ncname_len;
2920 }
2921 /* remove old flag to prevent ambiguities */
2922 prev_function_check = 0;
2923 tok_type = LYXP_TOKEN_NAMETEST;
2924 } else {
2925 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2926 prev_function_check = 1;
2927 tok_type = LYXP_TOKEN_NAMETEST;
2928 }
2929 }
2930
2931 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002932 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002933 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002934 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002935 ++parsed;
2936 }
2937
Radek Krejcif03a9e22020-09-18 20:09:31 +02002938 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002939
Michal Vasko004d3152020-06-11 19:59:22 +02002940 if (reparse) {
2941 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002942 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2943 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002944
Michal Vasko004d3152020-06-11 19:59:22 +02002945 /* fill repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002946 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error);
2947 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002948 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002949 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002950 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002951 goto error;
2952 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002953 }
2954
Radek Krejcif03a9e22020-09-18 20:09:31 +02002955 print_expr_struct_debug(expr);
2956 *expr_p = expr;
2957 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002958
2959error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002960 lyxp_expr_free(ctx, expr);
2961 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002962}
2963
Michal Vasko1734be92020-09-22 08:55:10 +02002964LY_ERR
2965lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02002966{
Michal Vasko1734be92020-09-22 08:55:10 +02002967 LY_ERR ret = LY_SUCCESS;
2968 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02002969 uint32_t i, j;
2970
Michal Vasko7f45cf22020-10-01 12:49:44 +02002971 if (!exp) {
2972 goto cleanup;
2973 }
2974
Michal Vasko004d3152020-06-11 19:59:22 +02002975 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02002976 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002977
2978 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
Michal Vasko1734be92020-09-22 08:55:10 +02002979 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002980 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2981
2982 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
Michal Vasko1734be92020-09-22 08:55:10 +02002983 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002984 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2985
2986 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
Michal Vasko1734be92020-09-22 08:55:10 +02002987 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002988 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2989
2990 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002991 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002992 for (i = 0; i < exp->used; ++i) {
2993 if (!exp->repeat[i]) {
2994 dup->repeat[i] = NULL;
2995 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02002996 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002997 /* the ending 0 as well */
2998 ++j;
2999
Michal Vasko99c71642020-07-03 13:33:36 +02003000 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02003001 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003002 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3003 dup->repeat[i][j - 1] = 0;
3004 }
3005 }
3006
3007 dup->used = exp->used;
3008 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003009 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003010
Michal Vasko1734be92020-09-22 08:55:10 +02003011cleanup:
3012 if (ret) {
3013 lyxp_expr_free(ctx, dup);
3014 } else {
3015 *dup_p = dup;
3016 }
3017 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003018}
3019
Michal Vasko03ff5a72019-09-11 13:49:33 +02003020/*
3021 * warn functions
3022 *
3023 * Warn functions check specific reasonable conditions for schema XPath
3024 * and print a warning if they are not satisfied.
3025 */
3026
3027/**
3028 * @brief Get the last-added schema node that is currently in the context.
3029 *
3030 * @param[in] set Set to search in.
3031 * @return Last-added schema context node, NULL if no node is in context.
3032 */
3033static struct lysc_node *
3034warn_get_scnode_in_ctx(struct lyxp_set *set)
3035{
3036 uint32_t i;
3037
3038 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3039 return NULL;
3040 }
3041
3042 i = set->used;
3043 do {
3044 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003045 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003046 /* if there are more, simply return the first found (last added) */
3047 return set->val.scnodes[i].scnode;
3048 }
3049 } while (i);
3050
3051 return NULL;
3052}
3053
3054/**
3055 * @brief Test whether a type is numeric - integer type or decimal64.
3056 *
3057 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003058 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003059 */
Radek Krejci857189e2020-09-01 13:26:36 +02003060static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003061warn_is_numeric_type(struct lysc_type *type)
3062{
3063 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003064 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003065 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003066
3067 switch (type->basetype) {
3068 case LY_TYPE_DEC64:
3069 case LY_TYPE_INT8:
3070 case LY_TYPE_UINT8:
3071 case LY_TYPE_INT16:
3072 case LY_TYPE_UINT16:
3073 case LY_TYPE_INT32:
3074 case LY_TYPE_UINT32:
3075 case LY_TYPE_INT64:
3076 case LY_TYPE_UINT64:
3077 return 1;
3078 case LY_TYPE_UNION:
3079 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003080 LY_ARRAY_FOR(uni->types, u) {
3081 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003082 if (ret) {
3083 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003084 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003085 }
3086 }
3087 /* did not find any suitable type */
3088 return 0;
3089 case LY_TYPE_LEAFREF:
3090 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3091 default:
3092 return 0;
3093 }
3094}
3095
3096/**
3097 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3098 *
3099 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003100 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003101 */
Radek Krejci857189e2020-09-01 13:26:36 +02003102static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003103warn_is_string_type(struct lysc_type *type)
3104{
3105 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003106 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003107 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003108
3109 switch (type->basetype) {
3110 case LY_TYPE_BITS:
3111 case LY_TYPE_ENUM:
3112 case LY_TYPE_IDENT:
3113 case LY_TYPE_INST:
3114 case LY_TYPE_STRING:
3115 return 1;
3116 case LY_TYPE_UNION:
3117 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003118 LY_ARRAY_FOR(uni->types, u) {
3119 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003120 if (ret) {
3121 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003122 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003123 }
3124 }
3125 /* did not find any suitable type */
3126 return 0;
3127 case LY_TYPE_LEAFREF:
3128 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3129 default:
3130 return 0;
3131 }
3132}
3133
3134/**
3135 * @brief Test whether a type is one specific type.
3136 *
3137 * @param[in] type Type to test.
3138 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003139 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003140 */
Radek Krejci857189e2020-09-01 13:26:36 +02003141static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003142warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3143{
3144 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003145 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003146 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003147
3148 if (type->basetype == base) {
3149 return 1;
3150 } else if (type->basetype == LY_TYPE_UNION) {
3151 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003152 LY_ARRAY_FOR(uni->types, u) {
3153 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003154 if (ret) {
3155 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003156 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003157 }
3158 }
3159 /* did not find any suitable type */
3160 return 0;
3161 } else if (type->basetype == LY_TYPE_LEAFREF) {
3162 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3163 }
3164
3165 return 0;
3166}
3167
3168/**
3169 * @brief Get next type of a (union) type.
3170 *
3171 * @param[in] type Base type.
3172 * @param[in] prev_type Previously returned type.
3173 * @return Next type or NULL.
3174 */
3175static struct lysc_type *
3176warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3177{
3178 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003179 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003180 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003181
3182 switch (type->basetype) {
3183 case LY_TYPE_UNION:
3184 uni = (struct lysc_type_union *)type;
3185 if (!prev_type) {
3186 return uni->types[0];
3187 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003188 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003189 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003190 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003191 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003192 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003193 found = 1;
3194 }
3195 }
3196 return NULL;
3197 default:
3198 if (prev_type) {
3199 assert(type == prev_type);
3200 return NULL;
3201 } else {
3202 return type;
3203 }
3204 }
3205}
3206
3207/**
3208 * @brief Test whether 2 types have a common type.
3209 *
3210 * @param[in] type1 First type.
3211 * @param[in] type2 Second type.
3212 * @return 1 if they do, 0 otherwise.
3213 */
3214static int
3215warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3216{
3217 struct lysc_type *t1, *rt1, *t2, *rt2;
3218
3219 t1 = NULL;
3220 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3221 if (t1->basetype == LY_TYPE_LEAFREF) {
3222 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3223 } else {
3224 rt1 = t1;
3225 }
3226
3227 t2 = NULL;
3228 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3229 if (t2->basetype == LY_TYPE_LEAFREF) {
3230 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3231 } else {
3232 rt2 = t2;
3233 }
3234
3235 if (rt2->basetype == rt1->basetype) {
3236 /* match found */
3237 return 1;
3238 }
3239 }
3240 }
3241
3242 return 0;
3243}
3244
3245/**
3246 * @brief Check both operands of comparison operators.
3247 *
3248 * @param[in] ctx Context for errors.
3249 * @param[in] set1 First operand set.
3250 * @param[in] set2 Second operand set.
3251 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3252 * @param[in] expr Start of the expression to print with the warning.
3253 * @param[in] tok_pos Token position.
3254 */
3255static void
Radek Krejci857189e2020-09-01 13:26:36 +02003256warn_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 +02003257{
3258 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003259 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003260
3261 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3262 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3263
3264 if (!node1 && !node2) {
3265 /* no node-sets involved, nothing to do */
3266 return;
3267 }
3268
3269 if (node1) {
3270 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3271 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3272 warning = 1;
3273 leaves = 0;
3274 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3275 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3276 warning = 1;
3277 }
3278 }
3279
3280 if (node2) {
3281 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3282 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3283 warning = 1;
3284 leaves = 0;
3285 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3286 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3287 warning = 1;
3288 }
3289 }
3290
3291 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003292 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3293 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3294 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3295 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003296 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3297 warning = 1;
3298 }
3299 }
3300
3301 if (warning) {
3302 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3303 }
3304}
3305
3306/**
3307 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3308 *
3309 * @param[in] exp Parsed XPath expression.
3310 * @param[in] set Set with the leaf/leaf-list.
3311 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3312 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3313 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3314 */
3315static void
Michal Vasko40308e72020-10-20 16:38:40 +02003316warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3317 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003318{
3319 struct lysc_node *scnode;
3320 struct lysc_type *type;
3321 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003322 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003323 LY_ERR rc;
3324 struct ly_err_item *err = NULL;
3325
Michal Vasko69730152020-10-09 16:30:07 +02003326 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3327 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003328 /* check that the node can have the specified value */
3329 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3330 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3331 } else {
3332 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3333 }
3334 if (!value) {
3335 LOGMEM(set->ctx);
3336 return;
3337 }
3338
3339 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3340 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003341 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003342 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003343 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003344 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003345 }
3346
3347 type = ((struct lysc_node_leaf *)scnode)->type;
3348 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003349 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003350 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003351 if (rc == LY_EINCOMPLETE) {
3352 rc = LY_SUCCESS;
3353 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003354
3355 if (err) {
3356 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3357 ly_err_free(err);
3358 } else if (rc != LY_SUCCESS) {
3359 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3360 }
3361 if (rc != LY_SUCCESS) {
3362 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003363 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003364 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003365 } else {
3366 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003367 }
3368 }
3369 free(value);
3370 }
3371}
3372
3373/*
3374 * XPath functions
3375 */
3376
3377/**
3378 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3379 * depending on whether the first node bit value from the second argument is set.
3380 *
3381 * @param[in] args Array of arguments.
3382 * @param[in] arg_count Count of elements in @p args.
3383 * @param[in,out] set Context and result set at the same time.
3384 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003385 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003386 */
3387static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003388xpath_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 +02003389{
3390 struct lyd_node_term *leaf;
3391 struct lysc_node_leaf *sleaf;
3392 struct lysc_type_bits *bits;
3393 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003394 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003395
3396 if (options & LYXP_SCNODE_ALL) {
3397 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3398 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003399 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3400 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 +02003401 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3402 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003403 }
3404
3405 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3406 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3407 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 +02003408 } else if (!warn_is_string_type(sleaf->type)) {
3409 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003410 }
3411 }
3412 set_scnode_clear_ctx(set);
3413 return rc;
3414 }
3415
Michal Vaskod3678892020-05-21 10:06:58 +02003416 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003417 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 +02003418 return LY_EVALID;
3419 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003420 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003421 LY_CHECK_RET(rc);
3422
3423 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003424 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003425 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003426 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3427 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003428 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003429 LY_ARRAY_FOR(bits->bits, u) {
3430 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003431 set_fill_boolean(set, 1);
3432 break;
3433 }
3434 }
3435 }
3436 }
3437
3438 return LY_SUCCESS;
3439}
3440
3441/**
3442 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3443 * with the argument converted to boolean.
3444 *
3445 * @param[in] args Array of arguments.
3446 * @param[in] arg_count Count of elements in @p args.
3447 * @param[in,out] set Context and result set at the same time.
3448 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003449 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003450 */
3451static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003452xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003453{
3454 LY_ERR rc;
3455
3456 if (options & LYXP_SCNODE_ALL) {
3457 set_scnode_clear_ctx(set);
3458 return LY_SUCCESS;
3459 }
3460
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003461 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003462 LY_CHECK_RET(rc);
3463 set_fill_set(set, args[0]);
3464
3465 return LY_SUCCESS;
3466}
3467
3468/**
3469 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3470 * with the first argument rounded up to the nearest integer.
3471 *
3472 * @param[in] args Array of arguments.
3473 * @param[in] arg_count Count of elements in @p args.
3474 * @param[in,out] set Context and result set at the same time.
3475 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003476 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003477 */
3478static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003479xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003480{
3481 struct lysc_node_leaf *sleaf;
3482 LY_ERR rc = LY_SUCCESS;
3483
3484 if (options & LYXP_SCNODE_ALL) {
3485 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3486 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003487 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3488 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 +02003489 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3490 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003491 }
3492 set_scnode_clear_ctx(set);
3493 return rc;
3494 }
3495
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003496 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003497 LY_CHECK_RET(rc);
3498 if ((long long)args[0]->val.num != args[0]->val.num) {
3499 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3500 } else {
3501 set_fill_number(set, args[0]->val.num);
3502 }
3503
3504 return LY_SUCCESS;
3505}
3506
3507/**
3508 * @brief Execute the XPath concat(string, string, string*) function.
3509 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3510 *
3511 * @param[in] args Array of arguments.
3512 * @param[in] arg_count Count of elements in @p args.
3513 * @param[in,out] set Context and result set at the same time.
3514 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003515 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003516 */
3517static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003518xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003519{
3520 uint16_t i;
3521 char *str = NULL;
3522 size_t used = 1;
3523 LY_ERR rc = LY_SUCCESS;
3524 struct lysc_node_leaf *sleaf;
3525
3526 if (options & LYXP_SCNODE_ALL) {
3527 for (i = 0; i < arg_count; ++i) {
3528 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3529 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3530 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003531 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003532 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003533 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 +02003534 }
3535 }
3536 }
3537 set_scnode_clear_ctx(set);
3538 return rc;
3539 }
3540
3541 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003542 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003543 if (rc != LY_SUCCESS) {
3544 free(str);
3545 return rc;
3546 }
3547
3548 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3549 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3550 strcpy(str + used - 1, args[i]->val.str);
3551 used += strlen(args[i]->val.str);
3552 }
3553
3554 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003555 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003556 set->type = LYXP_SET_STRING;
3557 set->val.str = str;
3558
3559 return LY_SUCCESS;
3560}
3561
3562/**
3563 * @brief Execute the XPath contains(string, string) function.
3564 * Returns LYXP_SET_BOOLEAN whether the second argument can
3565 * be found in the first or not.
3566 *
3567 * @param[in] args Array of arguments.
3568 * @param[in] arg_count Count of elements in @p args.
3569 * @param[in,out] set Context and result set at the same time.
3570 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003571 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003572 */
3573static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003574xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003575{
3576 struct lysc_node_leaf *sleaf;
3577 LY_ERR rc = LY_SUCCESS;
3578
3579 if (options & LYXP_SCNODE_ALL) {
3580 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3581 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3582 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 +02003583 } else if (!warn_is_string_type(sleaf->type)) {
3584 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003585 }
3586 }
3587
3588 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3589 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3590 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 +02003591 } else if (!warn_is_string_type(sleaf->type)) {
3592 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003593 }
3594 }
3595 set_scnode_clear_ctx(set);
3596 return rc;
3597 }
3598
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003599 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003600 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003601 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003602 LY_CHECK_RET(rc);
3603
3604 if (strstr(args[0]->val.str, args[1]->val.str)) {
3605 set_fill_boolean(set, 1);
3606 } else {
3607 set_fill_boolean(set, 0);
3608 }
3609
3610 return LY_SUCCESS;
3611}
3612
3613/**
3614 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3615 * with the size of the node-set from the argument.
3616 *
3617 * @param[in] args Array of arguments.
3618 * @param[in] arg_count Count of elements in @p args.
3619 * @param[in,out] set Context and result set at the same time.
3620 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003621 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003622 */
3623static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003624xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003625{
3626 struct lysc_node *scnode = NULL;
3627 LY_ERR rc = LY_SUCCESS;
3628
3629 if (options & LYXP_SCNODE_ALL) {
3630 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3631 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003632 }
3633 set_scnode_clear_ctx(set);
3634 return rc;
3635 }
3636
Michal Vasko03ff5a72019-09-11 13:49:33 +02003637 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003638 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003639 return LY_EVALID;
3640 }
3641
3642 set_fill_number(set, args[0]->used);
3643 return LY_SUCCESS;
3644}
3645
3646/**
3647 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3648 * with the context with the intial node.
3649 *
3650 * @param[in] args Array of arguments.
3651 * @param[in] arg_count Count of elements in @p args.
3652 * @param[in,out] set Context and result set at the same time.
3653 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003654 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003655 */
3656static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003657xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003658{
3659 if (arg_count || args) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003660 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003661 return LY_EVALID;
3662 }
3663
3664 if (options & LYXP_SCNODE_ALL) {
3665 set_scnode_clear_ctx(set);
3666
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003667 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003668 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003669 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003670
3671 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003672 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003673 }
3674
3675 return LY_SUCCESS;
3676}
3677
3678/**
3679 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3680 * leafref or instance-identifier target node(s).
3681 *
3682 * @param[in] args Array of arguments.
3683 * @param[in] arg_count Count of elements in @p args.
3684 * @param[in,out] set Context and result set at the same time.
3685 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003686 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003687 */
3688static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003689xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003690{
3691 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003692 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003693 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003694 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003695 struct ly_path *p;
3696 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003697 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003698 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003699 LY_ERR rc = LY_SUCCESS;
3700
3701 if (options & LYXP_SCNODE_ALL) {
3702 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3703 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003704 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3705 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 +02003706 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3707 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
Michal Vasko69730152020-10-09 16:30:07 +02003708 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003709 }
3710 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003711 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003712 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003713 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003714
3715 /* it was already evaluated on schema, it must succeed */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003716 rc = ly_path_compile(set->ctx, lref->cur_mod, &sleaf->node, lref->path, LY_PATH_LREF_TRUE, oper,
3717 LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, NULL, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003718 assert(!rc);
3719
3720 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003721 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003722 ly_path_free(set->ctx, p);
3723
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003724 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003725 }
3726
Michal Vasko03ff5a72019-09-11 13:49:33 +02003727 return rc;
3728 }
3729
Michal Vaskod3678892020-05-21 10:06:58 +02003730 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003731 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003732 return LY_EVALID;
3733 }
3734
Michal Vaskod3678892020-05-21 10:06:58 +02003735 lyxp_set_free_content(set);
3736 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003737 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3738 sleaf = (struct lysc_node_leaf *)leaf->schema;
3739 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3740 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3741 /* find leafref target */
Michal Vasko9e685082021-01-29 14:49:09 +01003742 if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
3743 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003744 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003745 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003746 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003747 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003748 } else {
3749 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003750 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003751 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Michal Vasko69730152020-10-09 16:30:07 +02003752 LYD_CANON_VALUE(leaf));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003753 return LY_EVALID;
3754 }
3755 }
Michal Vasko004d3152020-06-11 19:59:22 +02003756
3757 /* insert it */
3758 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003759 }
3760 }
3761
3762 return LY_SUCCESS;
3763}
3764
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003765static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003766xpath_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 +02003767{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01003768 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003769 LY_ARRAY_COUNT_TYPE u;
3770 struct lyd_node_term *leaf;
3771 struct lysc_node_leaf *sleaf;
3772 struct lyd_meta *meta;
3773 struct lyd_value data = {0}, *val;
3774 struct ly_err_item *err = NULL;
3775 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003776 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003777
3778 if (options & LYXP_SCNODE_ALL) {
3779 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3780 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
3781 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3782 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003783 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003784 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3785 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3786 }
3787
3788 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3789 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3790 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003791 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003792 } else if (!warn_is_string_type(sleaf->type)) {
3793 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3794 }
3795 }
3796 set_scnode_clear_ctx(set);
3797 return rc;
3798 }
3799
3800 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003801 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 +02003802 return LY_EVALID;
3803 }
3804 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3805 LY_CHECK_RET(rc);
3806
3807 set_fill_boolean(set, 0);
3808 found = 0;
3809 for (i = 0; i < args[0]->used; ++i) {
3810 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3811 continue;
3812 }
3813
3814 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3815 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3816 sleaf = (struct lysc_node_leaf *)leaf->schema;
3817 val = &leaf->value;
3818 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3819 /* uninteresting */
3820 continue;
3821 }
3822
3823 /* store args[1] as ident */
3824 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 +01003825 0, set->format, set->prefix_data, LYD_HINT_DATA, leaf->schema, &data, NULL, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003826 } else {
3827 meta = args[0]->val.meta[i].meta;
3828 val = &meta->value;
3829 if (val->realtype->basetype != LY_TYPE_IDENT) {
3830 /* uninteresting */
3831 continue;
3832 }
3833
3834 /* store args[1] as ident */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003835 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 +01003836 set->format, set->prefix_data, LYD_HINT_DATA, meta->parent->schema, &data, NULL, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003837 }
3838
3839 if (err) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01003840 ly_err_print(set->ctx, err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003841 ly_err_free(err);
3842 }
3843 LY_CHECK_RET(rc);
3844
3845 /* finally check the identity itself */
3846 if (self_match && (data.ident == val->ident)) {
3847 set_fill_boolean(set, 1);
3848 found = 1;
3849 }
3850 if (!found) {
3851 LY_ARRAY_FOR(data.ident->derived, u) {
3852 if (data.ident->derived[u] == val->ident) {
3853 set_fill_boolean(set, 1);
3854 found = 1;
3855 break;
3856 }
3857 }
3858 }
3859
3860 /* free temporary value */
3861 val->realtype->plugin->free(set->ctx, &data);
3862 if (found) {
3863 break;
3864 }
3865 }
3866
3867 return LY_SUCCESS;
3868}
3869
Michal Vasko03ff5a72019-09-11 13:49:33 +02003870/**
3871 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3872 * on whether the first argument nodes contain a node of an identity derived from the second
3873 * argument identity.
3874 *
3875 * @param[in] args Array of arguments.
3876 * @param[in] arg_count Count of elements in @p args.
3877 * @param[in,out] set Context and result set at the same time.
3878 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003879 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003880 */
3881static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003882xpath_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 +02003883{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003884 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003885}
3886
3887/**
3888 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3889 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3890 * the second argument identity.
3891 *
3892 * @param[in] args Array of arguments.
3893 * @param[in] arg_count Count of elements in @p args.
3894 * @param[in,out] set Context and result set at the same time.
3895 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003896 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003897 */
3898static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003899xpath_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 +02003900{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003901 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003902}
3903
3904/**
3905 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3906 * with the integer value of the first node's enum value, otherwise NaN.
3907 *
3908 * @param[in] args Array of arguments.
3909 * @param[in] arg_count Count of elements in @p args.
3910 * @param[in,out] set Context and result set at the same time.
3911 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003912 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003913 */
3914static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003915xpath_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 +02003916{
3917 struct lyd_node_term *leaf;
3918 struct lysc_node_leaf *sleaf;
3919 LY_ERR rc = LY_SUCCESS;
3920
3921 if (options & LYXP_SCNODE_ALL) {
3922 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3923 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003924 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3925 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 +02003926 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3927 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003928 }
3929 set_scnode_clear_ctx(set);
3930 return rc;
3931 }
3932
Michal Vaskod3678892020-05-21 10:06:58 +02003933 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003934 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003935 return LY_EVALID;
3936 }
3937
3938 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003939 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003940 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3941 sleaf = (struct lysc_node_leaf *)leaf->schema;
3942 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3943 set_fill_number(set, leaf->value.enum_item->value);
3944 }
3945 }
3946
3947 return LY_SUCCESS;
3948}
3949
3950/**
3951 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3952 * with false value.
3953 *
3954 * @param[in] args Array of arguments.
3955 * @param[in] arg_count Count of elements in @p args.
3956 * @param[in,out] set Context and result set at the same time.
3957 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003958 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003959 */
3960static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003961xpath_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 +02003962{
3963 if (options & LYXP_SCNODE_ALL) {
3964 set_scnode_clear_ctx(set);
3965 return LY_SUCCESS;
3966 }
3967
3968 set_fill_boolean(set, 0);
3969 return LY_SUCCESS;
3970}
3971
3972/**
3973 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3974 * with the first argument floored (truncated).
3975 *
3976 * @param[in] args Array of arguments.
3977 * @param[in] arg_count Count of elements in @p args.
3978 * @param[in,out] set Context and result set at the same time.
3979 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003980 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003981 */
3982static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003983xpath_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 +02003984{
3985 LY_ERR rc;
3986
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003987 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003988 LY_CHECK_RET(rc);
3989 if (isfinite(args[0]->val.num)) {
3990 set_fill_number(set, (long long)args[0]->val.num);
3991 }
3992
3993 return LY_SUCCESS;
3994}
3995
3996/**
3997 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3998 * whether the language of the text matches the one from the argument.
3999 *
4000 * @param[in] args Array of arguments.
4001 * @param[in] arg_count Count of elements in @p args.
4002 * @param[in,out] set Context and result set at the same time.
4003 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004004 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004005 */
4006static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004007xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004008{
4009 const struct lyd_node *node;
4010 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004011 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004012 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004013 LY_ERR rc = LY_SUCCESS;
4014
4015 if (options & LYXP_SCNODE_ALL) {
4016 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4017 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4018 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 +02004019 } else if (!warn_is_string_type(sleaf->type)) {
4020 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004021 }
4022 }
4023 set_scnode_clear_ctx(set);
4024 return rc;
4025 }
4026
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004027 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004028 LY_CHECK_RET(rc);
4029
Michal Vasko03ff5a72019-09-11 13:49:33 +02004030 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004031 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004032 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004033 } else if (!set->used) {
4034 set_fill_boolean(set, 0);
4035 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004036 }
4037
4038 switch (set->val.nodes[0].type) {
4039 case LYXP_NODE_ELEM:
4040 case LYXP_NODE_TEXT:
4041 node = set->val.nodes[0].node;
4042 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004043 case LYXP_NODE_META:
4044 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004045 break;
4046 default:
4047 /* nothing to do with roots */
4048 set_fill_boolean(set, 0);
4049 return LY_SUCCESS;
4050 }
4051
Michal Vasko9f96a052020-03-10 09:41:45 +01004052 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004053 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004054 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004055 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004056 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004057 break;
4058 }
4059 }
4060
Michal Vasko9f96a052020-03-10 09:41:45 +01004061 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004062 break;
4063 }
4064 }
4065
4066 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004067 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004068 set_fill_boolean(set, 0);
4069 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004070 uint64_t i;
4071
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004072 val = meta->value.canonical;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004073 for (i = 0; args[0]->val.str[i]; ++i) {
4074 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4075 set_fill_boolean(set, 0);
4076 break;
4077 }
4078 }
4079 if (!args[0]->val.str[i]) {
4080 if (!val[i] || (val[i] == '-')) {
4081 set_fill_boolean(set, 1);
4082 } else {
4083 set_fill_boolean(set, 0);
4084 }
4085 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004086 }
4087
4088 return LY_SUCCESS;
4089}
4090
4091/**
4092 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4093 * with the context size.
4094 *
4095 * @param[in] args Array of arguments.
4096 * @param[in] arg_count Count of elements in @p args.
4097 * @param[in,out] set Context and result set at the same time.
4098 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004099 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004100 */
4101static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004102xpath_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 +02004103{
4104 if (options & LYXP_SCNODE_ALL) {
4105 set_scnode_clear_ctx(set);
4106 return LY_SUCCESS;
4107 }
4108
Michal Vasko03ff5a72019-09-11 13:49:33 +02004109 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004110 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004111 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004112 } else if (!set->used) {
4113 set_fill_number(set, 0);
4114 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004115 }
4116
4117 set_fill_number(set, set->ctx_size);
4118 return LY_SUCCESS;
4119}
4120
4121/**
4122 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4123 * with the node name without namespace from the argument or the context.
4124 *
4125 * @param[in] args Array of arguments.
4126 * @param[in] arg_count Count of elements in @p args.
4127 * @param[in,out] set Context and result set at the same time.
4128 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004129 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004130 */
4131static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004132xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004133{
4134 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004135
Michal Vasko03ff5a72019-09-11 13:49:33 +02004136 /* suppress unused variable warning */
4137 (void)options;
4138
4139 if (options & LYXP_SCNODE_ALL) {
4140 set_scnode_clear_ctx(set);
4141 return LY_SUCCESS;
4142 }
4143
4144 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004145 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004146 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004147 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004148 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004149 } else if (!args[0]->used) {
4150 set_fill_string(set, "", 0);
4151 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004152 }
4153
4154 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004155 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004156
4157 item = &args[0]->val.nodes[0];
4158 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004159 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004160 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004161 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004162 } else if (!set->used) {
4163 set_fill_string(set, "", 0);
4164 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004165 }
4166
4167 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004168 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004169
4170 item = &set->val.nodes[0];
4171 }
4172
4173 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004174 case LYXP_NODE_NONE:
4175 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004176 case LYXP_NODE_ROOT:
4177 case LYXP_NODE_ROOT_CONFIG:
4178 case LYXP_NODE_TEXT:
4179 set_fill_string(set, "", 0);
4180 break;
4181 case LYXP_NODE_ELEM:
4182 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4183 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004184 case LYXP_NODE_META:
4185 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004186 break;
4187 }
4188
4189 return LY_SUCCESS;
4190}
4191
4192/**
4193 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4194 * with the node name fully qualified (with namespace) from the argument or the context.
4195 *
4196 * @param[in] args Array of arguments.
4197 * @param[in] arg_count Count of elements in @p args.
4198 * @param[in,out] set Context and result set at the same time.
4199 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004200 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004201 */
4202static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004203xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004204{
4205 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004206 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004207 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004208 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004209
4210 if (options & LYXP_SCNODE_ALL) {
4211 set_scnode_clear_ctx(set);
4212 return LY_SUCCESS;
4213 }
4214
4215 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004216 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004217 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004218 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004219 } else if (!args[0]->used) {
4220 set_fill_string(set, "", 0);
4221 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004222 }
4223
4224 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004225 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004226
4227 item = &args[0]->val.nodes[0];
4228 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004229 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004230 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004231 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004232 } else if (!set->used) {
4233 set_fill_string(set, "", 0);
4234 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004235 }
4236
4237 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004238 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004239
4240 item = &set->val.nodes[0];
4241 }
4242
4243 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004244 case LYXP_NODE_NONE:
4245 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004246 case LYXP_NODE_ROOT:
4247 case LYXP_NODE_ROOT_CONFIG:
4248 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004249 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004250 break;
4251 case LYXP_NODE_ELEM:
4252 mod = item->node->schema->module;
4253 name = item->node->schema->name;
4254 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004255 case LYXP_NODE_META:
4256 mod = ((struct lyd_meta *)item->node)->annotation->module;
4257 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004258 break;
4259 }
4260
4261 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004262 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004263 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4264 set_fill_string(set, str, strlen(str));
4265 free(str);
4266 } else {
4267 set_fill_string(set, "", 0);
4268 }
4269
4270 return LY_SUCCESS;
4271}
4272
4273/**
4274 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4275 * with the namespace of the node from the argument or the context.
4276 *
4277 * @param[in] args Array of arguments.
4278 * @param[in] arg_count Count of elements in @p args.
4279 * @param[in,out] set Context and result set at the same time.
4280 * @param[in] options XPath options.
4281 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4282 */
4283static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004284xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004285{
4286 struct lyxp_set_node *item;
4287 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004288
Michal Vasko03ff5a72019-09-11 13:49:33 +02004289 /* suppress unused variable warning */
4290 (void)options;
4291
4292 if (options & LYXP_SCNODE_ALL) {
4293 set_scnode_clear_ctx(set);
4294 return LY_SUCCESS;
4295 }
4296
4297 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004298 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004299 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004300 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004301 return LY_EVALID;
4302 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004303 set_fill_string(set, "", 0);
4304 return LY_SUCCESS;
4305 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004306
4307 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004308 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004309
4310 item = &args[0]->val.nodes[0];
4311 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004312 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004313 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004314 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004315 } else if (!set->used) {
4316 set_fill_string(set, "", 0);
4317 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004318 }
4319
4320 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004321 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004322
4323 item = &set->val.nodes[0];
4324 }
4325
4326 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004327 case LYXP_NODE_NONE:
4328 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004329 case LYXP_NODE_ROOT:
4330 case LYXP_NODE_ROOT_CONFIG:
4331 case LYXP_NODE_TEXT:
4332 set_fill_string(set, "", 0);
4333 break;
4334 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004335 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004336 if (item->type == LYXP_NODE_ELEM) {
4337 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004338 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004339 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004340 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004341 }
4342
4343 set_fill_string(set, mod->ns, strlen(mod->ns));
4344 break;
4345 }
4346
4347 return LY_SUCCESS;
4348}
4349
4350/**
4351 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4352 * with only nodes from the context. In practice it either leaves the context
4353 * as it is or returns an empty node set.
4354 *
4355 * @param[in] args Array of arguments.
4356 * @param[in] arg_count Count of elements in @p args.
4357 * @param[in,out] set Context and result set at the same time.
4358 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004359 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004360 */
4361static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004362xpath_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 +02004363{
4364 if (options & LYXP_SCNODE_ALL) {
4365 set_scnode_clear_ctx(set);
4366 return LY_SUCCESS;
4367 }
4368
4369 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004370 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004371 }
4372 return LY_SUCCESS;
4373}
4374
4375/**
4376 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4377 * with normalized value (no leading, trailing, double white spaces) of the node
4378 * from the argument or the context.
4379 *
4380 * @param[in] args Array of arguments.
4381 * @param[in] arg_count Count of elements in @p args.
4382 * @param[in,out] set Context and result set at the same time.
4383 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004384 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004385 */
4386static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004387xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004388{
4389 uint16_t i, new_used;
4390 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004391 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004392 struct lysc_node_leaf *sleaf;
4393 LY_ERR rc = LY_SUCCESS;
4394
4395 if (options & LYXP_SCNODE_ALL) {
4396 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4397 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4398 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 +02004399 } else if (!warn_is_string_type(sleaf->type)) {
4400 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004401 }
4402 }
4403 set_scnode_clear_ctx(set);
4404 return rc;
4405 }
4406
4407 if (arg_count) {
4408 set_fill_set(set, args[0]);
4409 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004410 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004411 LY_CHECK_RET(rc);
4412
4413 /* is there any normalization necessary? */
4414 for (i = 0; set->val.str[i]; ++i) {
4415 if (is_xmlws(set->val.str[i])) {
4416 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4417 have_spaces = 1;
4418 break;
4419 }
4420 space_before = 1;
4421 } else {
4422 space_before = 0;
4423 }
4424 }
4425
4426 /* yep, there is */
4427 if (have_spaces) {
4428 /* it's enough, at least one character will go, makes space for ending '\0' */
4429 new = malloc(strlen(set->val.str) * sizeof(char));
4430 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4431 new_used = 0;
4432
4433 space_before = 0;
4434 for (i = 0; set->val.str[i]; ++i) {
4435 if (is_xmlws(set->val.str[i])) {
4436 if ((i == 0) || space_before) {
4437 space_before = 1;
4438 continue;
4439 } else {
4440 space_before = 1;
4441 }
4442 } else {
4443 space_before = 0;
4444 }
4445
4446 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4447 ++new_used;
4448 }
4449
4450 /* at worst there is one trailing space now */
4451 if (new_used && is_xmlws(new[new_used - 1])) {
4452 --new_used;
4453 }
4454
4455 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4456 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4457 new[new_used] = '\0';
4458
4459 free(set->val.str);
4460 set->val.str = new;
4461 }
4462
4463 return LY_SUCCESS;
4464}
4465
4466/**
4467 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4468 * with the argument converted to boolean and logically inverted.
4469 *
4470 * @param[in] args Array of arguments.
4471 * @param[in] arg_count Count of elements in @p args.
4472 * @param[in,out] set Context and result set at the same time.
4473 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004474 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004475 */
4476static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004477xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004478{
4479 if (options & LYXP_SCNODE_ALL) {
4480 set_scnode_clear_ctx(set);
4481 return LY_SUCCESS;
4482 }
4483
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004484 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004485 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004486 set_fill_boolean(set, 0);
4487 } else {
4488 set_fill_boolean(set, 1);
4489 }
4490
4491 return LY_SUCCESS;
4492}
4493
4494/**
4495 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4496 * with the number representation of either the argument or the context.
4497 *
4498 * @param[in] args Array of arguments.
4499 * @param[in] arg_count Count of elements in @p args.
4500 * @param[in,out] set Context and result set at the same time.
4501 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004502 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004503 */
4504static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004505xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004506{
4507 LY_ERR rc;
4508
4509 if (options & LYXP_SCNODE_ALL) {
4510 set_scnode_clear_ctx(set);
4511 return LY_SUCCESS;
4512 }
4513
4514 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004515 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004516 LY_CHECK_RET(rc);
4517 set_fill_set(set, args[0]);
4518 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004519 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004520 LY_CHECK_RET(rc);
4521 }
4522
4523 return LY_SUCCESS;
4524}
4525
4526/**
4527 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4528 * with the context position.
4529 *
4530 * @param[in] args Array of arguments.
4531 * @param[in] arg_count Count of elements in @p args.
4532 * @param[in,out] set Context and result set at the same time.
4533 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004534 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004535 */
4536static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004537xpath_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 +02004538{
4539 if (options & LYXP_SCNODE_ALL) {
4540 set_scnode_clear_ctx(set);
4541 return LY_SUCCESS;
4542 }
4543
Michal Vasko03ff5a72019-09-11 13:49:33 +02004544 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004545 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004546 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004547 } else if (!set->used) {
4548 set_fill_number(set, 0);
4549 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004550 }
4551
4552 set_fill_number(set, set->ctx_pos);
4553
4554 /* UNUSED in 'Release' build type */
4555 (void)options;
4556 return LY_SUCCESS;
4557}
4558
4559/**
4560 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4561 * depending on whether the second argument regex matches the first argument string. For details refer to
4562 * YANG 1.1 RFC section 10.2.1.
4563 *
4564 * @param[in] args Array of arguments.
4565 * @param[in] arg_count Count of elements in @p args.
4566 * @param[in,out] set Context and result set at the same time.
4567 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004568 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004569 */
4570static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004571xpath_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 +02004572{
4573 struct lysc_pattern **patterns = NULL, **pattern;
4574 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004575 LY_ERR rc = LY_SUCCESS;
4576 struct ly_err_item *err;
4577
4578 if (options & LYXP_SCNODE_ALL) {
4579 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4580 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4581 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 +02004582 } else if (!warn_is_string_type(sleaf->type)) {
4583 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004584 }
4585 }
4586
4587 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4588 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4589 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 +02004590 } else if (!warn_is_string_type(sleaf->type)) {
4591 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004592 }
4593 }
4594 set_scnode_clear_ctx(set);
4595 return rc;
4596 }
4597
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004598 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004599 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004600 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004601 LY_CHECK_RET(rc);
4602
4603 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek IÅ¡a45802b52021-02-09 09:21:58 +01004604 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004605 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004606 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Radek Krejciddace2c2021-01-08 11:30:56 +01004607 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004608 if (rc != LY_SUCCESS) {
4609 LY_ARRAY_FREE(patterns);
4610 return rc;
4611 }
4612
4613 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4614 pcre2_code_free((*pattern)->code);
4615 free(*pattern);
4616 LY_ARRAY_FREE(patterns);
4617 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004618 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004619 ly_err_free(err);
4620 return rc;
4621 }
4622
4623 if (rc == LY_EVALID) {
4624 ly_err_free(err);
4625 set_fill_boolean(set, 0);
4626 } else {
4627 set_fill_boolean(set, 1);
4628 }
4629
4630 return LY_SUCCESS;
4631}
4632
4633/**
4634 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4635 * with the rounded first argument. For details refer to
4636 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4637 *
4638 * @param[in] args Array of arguments.
4639 * @param[in] arg_count Count of elements in @p args.
4640 * @param[in,out] set Context and result set at the same time.
4641 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004642 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004643 */
4644static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004645xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004646{
4647 struct lysc_node_leaf *sleaf;
4648 LY_ERR rc = LY_SUCCESS;
4649
4650 if (options & LYXP_SCNODE_ALL) {
4651 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4652 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004653 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4654 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 +02004655 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4656 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004657 }
4658 set_scnode_clear_ctx(set);
4659 return rc;
4660 }
4661
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004662 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004663 LY_CHECK_RET(rc);
4664
4665 /* cover only the cases where floor can't be used */
4666 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4667 set_fill_number(set, -0.0f);
4668 } else {
4669 args[0]->val.num += 0.5;
4670 rc = xpath_floor(args, 1, args[0], options);
4671 LY_CHECK_RET(rc);
4672 set_fill_number(set, args[0]->val.num);
4673 }
4674
4675 return LY_SUCCESS;
4676}
4677
4678/**
4679 * @brief Execute the XPath starts-with(string, string) function.
4680 * Returns LYXP_SET_BOOLEAN whether the second argument is
4681 * the prefix of the first or not.
4682 *
4683 * @param[in] args Array of arguments.
4684 * @param[in] arg_count Count of elements in @p args.
4685 * @param[in,out] set Context and result set at the same time.
4686 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004687 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004688 */
4689static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004690xpath_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 +02004691{
4692 struct lysc_node_leaf *sleaf;
4693 LY_ERR rc = LY_SUCCESS;
4694
4695 if (options & LYXP_SCNODE_ALL) {
4696 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4697 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4698 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 +02004699 } else if (!warn_is_string_type(sleaf->type)) {
4700 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004701 }
4702 }
4703
4704 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4705 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4706 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 +02004707 } else if (!warn_is_string_type(sleaf->type)) {
4708 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004709 }
4710 }
4711 set_scnode_clear_ctx(set);
4712 return rc;
4713 }
4714
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004715 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004716 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004717 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004718 LY_CHECK_RET(rc);
4719
4720 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4721 set_fill_boolean(set, 0);
4722 } else {
4723 set_fill_boolean(set, 1);
4724 }
4725
4726 return LY_SUCCESS;
4727}
4728
4729/**
4730 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4731 * with the string representation of either the argument or the context.
4732 *
4733 * @param[in] args Array of arguments.
4734 * @param[in] arg_count Count of elements in @p args.
4735 * @param[in,out] set Context and result set at the same time.
4736 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004737 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004738 */
4739static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004740xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004741{
4742 LY_ERR rc;
4743
4744 if (options & LYXP_SCNODE_ALL) {
4745 set_scnode_clear_ctx(set);
4746 return LY_SUCCESS;
4747 }
4748
4749 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004750 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004751 LY_CHECK_RET(rc);
4752 set_fill_set(set, args[0]);
4753 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004754 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004755 LY_CHECK_RET(rc);
4756 }
4757
4758 return LY_SUCCESS;
4759}
4760
4761/**
4762 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4763 * with the length of the string in either the argument or the context.
4764 *
4765 * @param[in] args Array of arguments.
4766 * @param[in] arg_count Count of elements in @p args.
4767 * @param[in,out] set Context and result set at the same time.
4768 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004769 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004770 */
4771static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004772xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004773{
4774 struct lysc_node_leaf *sleaf;
4775 LY_ERR rc = LY_SUCCESS;
4776
4777 if (options & LYXP_SCNODE_ALL) {
4778 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4779 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4780 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 +02004781 } else if (!warn_is_string_type(sleaf->type)) {
4782 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004783 }
4784 }
4785 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4786 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4787 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 +02004788 } else if (!warn_is_string_type(sleaf->type)) {
4789 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004790 }
4791 }
4792 set_scnode_clear_ctx(set);
4793 return rc;
4794 }
4795
4796 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004797 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004798 LY_CHECK_RET(rc);
4799 set_fill_number(set, strlen(args[0]->val.str));
4800 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004801 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004802 LY_CHECK_RET(rc);
4803 set_fill_number(set, strlen(set->val.str));
4804 }
4805
4806 return LY_SUCCESS;
4807}
4808
4809/**
4810 * @brief Execute the XPath substring(string, number, number?) function.
4811 * Returns LYXP_SET_STRING substring of the first argument starting
4812 * on the second argument index ending on the third argument index,
4813 * indexed from 1. For exact definition refer to
4814 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4815 *
4816 * @param[in] args Array of arguments.
4817 * @param[in] arg_count Count of elements in @p args.
4818 * @param[in,out] set Context and result set at the same time.
4819 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004820 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004821 */
4822static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004823xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004824{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004825 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004826 uint16_t str_start, str_len, pos;
4827 struct lysc_node_leaf *sleaf;
4828 LY_ERR rc = LY_SUCCESS;
4829
4830 if (options & LYXP_SCNODE_ALL) {
4831 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4832 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4833 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 +02004834 } else if (!warn_is_string_type(sleaf->type)) {
4835 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004836 }
4837 }
4838
4839 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4840 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4841 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 +02004842 } else if (!warn_is_numeric_type(sleaf->type)) {
4843 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004844 }
4845 }
4846
Michal Vasko69730152020-10-09 16:30:07 +02004847 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4848 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004849 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4850 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 +02004851 } else if (!warn_is_numeric_type(sleaf->type)) {
4852 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004853 }
4854 }
4855 set_scnode_clear_ctx(set);
4856 return rc;
4857 }
4858
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004859 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004860 LY_CHECK_RET(rc);
4861
4862 /* start */
4863 if (xpath_round(&args[1], 1, args[1], options)) {
4864 return -1;
4865 }
4866 if (isfinite(args[1]->val.num)) {
4867 start = args[1]->val.num - 1;
4868 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004869 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004870 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004871 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004872 }
4873
4874 /* len */
4875 if (arg_count == 3) {
4876 rc = xpath_round(&args[2], 1, args[2], options);
4877 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004878 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004879 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004880 } else if (isfinite(args[2]->val.num)) {
4881 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004882 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004883 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004884 }
4885 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004886 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004887 }
4888
4889 /* find matching character positions */
4890 str_start = 0;
4891 str_len = 0;
4892 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4893 if (pos < start) {
4894 ++str_start;
4895 } else if (pos < start + len) {
4896 ++str_len;
4897 } else {
4898 break;
4899 }
4900 }
4901
4902 set_fill_string(set, args[0]->val.str + str_start, str_len);
4903 return LY_SUCCESS;
4904}
4905
4906/**
4907 * @brief Execute the XPath substring-after(string, string) function.
4908 * Returns LYXP_SET_STRING with the string succeeding the occurance
4909 * of the second argument in the first or an empty string.
4910 *
4911 * @param[in] args Array of arguments.
4912 * @param[in] arg_count Count of elements in @p args.
4913 * @param[in,out] set Context and result set at the same time.
4914 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004915 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004916 */
4917static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004918xpath_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 +02004919{
4920 char *ptr;
4921 struct lysc_node_leaf *sleaf;
4922 LY_ERR rc = LY_SUCCESS;
4923
4924 if (options & LYXP_SCNODE_ALL) {
4925 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4926 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4927 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 +02004928 } else if (!warn_is_string_type(sleaf->type)) {
4929 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004930 }
4931 }
4932
4933 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4934 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4935 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 +02004936 } else if (!warn_is_string_type(sleaf->type)) {
4937 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004938 }
4939 }
4940 set_scnode_clear_ctx(set);
4941 return rc;
4942 }
4943
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004944 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004945 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004946 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004947 LY_CHECK_RET(rc);
4948
4949 ptr = strstr(args[0]->val.str, args[1]->val.str);
4950 if (ptr) {
4951 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4952 } else {
4953 set_fill_string(set, "", 0);
4954 }
4955
4956 return LY_SUCCESS;
4957}
4958
4959/**
4960 * @brief Execute the XPath substring-before(string, string) function.
4961 * Returns LYXP_SET_STRING with the string preceding the occurance
4962 * of the second argument in the first or an empty string.
4963 *
4964 * @param[in] args Array of arguments.
4965 * @param[in] arg_count Count of elements in @p args.
4966 * @param[in,out] set Context and result set at the same time.
4967 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004968 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004969 */
4970static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004971xpath_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 +02004972{
4973 char *ptr;
4974 struct lysc_node_leaf *sleaf;
4975 LY_ERR rc = LY_SUCCESS;
4976
4977 if (options & LYXP_SCNODE_ALL) {
4978 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4979 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4980 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 +02004981 } else if (!warn_is_string_type(sleaf->type)) {
4982 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004983 }
4984 }
4985
4986 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4987 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4988 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 +02004989 } else if (!warn_is_string_type(sleaf->type)) {
4990 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004991 }
4992 }
4993 set_scnode_clear_ctx(set);
4994 return rc;
4995 }
4996
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004997 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004998 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004999 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005000 LY_CHECK_RET(rc);
5001
5002 ptr = strstr(args[0]->val.str, args[1]->val.str);
5003 if (ptr) {
5004 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5005 } else {
5006 set_fill_string(set, "", 0);
5007 }
5008
5009 return LY_SUCCESS;
5010}
5011
5012/**
5013 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5014 * with the sum of all the nodes in the context.
5015 *
5016 * @param[in] args Array of arguments.
5017 * @param[in] arg_count Count of elements in @p args.
5018 * @param[in,out] set Context and result set at the same time.
5019 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005020 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005021 */
5022static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005023xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005024{
5025 long double num;
5026 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005027 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005028 struct lyxp_set set_item;
5029 struct lysc_node_leaf *sleaf;
5030 LY_ERR rc = LY_SUCCESS;
5031
5032 if (options & LYXP_SCNODE_ALL) {
5033 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5034 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005035 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005036 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5037 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5038 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005039 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005040 } else if (!warn_is_numeric_type(sleaf->type)) {
5041 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005042 }
5043 }
5044 }
5045 }
5046 set_scnode_clear_ctx(set);
5047 return rc;
5048 }
5049
5050 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005051
5052 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005053 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005054 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005055 } else if (!args[0]->used) {
5056 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005057 }
5058
Michal Vasko5c4e5892019-11-14 12:31:38 +01005059 set_init(&set_item, set);
5060
Michal Vasko03ff5a72019-09-11 13:49:33 +02005061 set_item.type = LYXP_SET_NODE_SET;
5062 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5063 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5064
5065 set_item.used = 1;
5066 set_item.size = 1;
5067
5068 for (i = 0; i < args[0]->used; ++i) {
5069 set_item.val.nodes[0] = args[0]->val.nodes[i];
5070
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005071 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005072 LY_CHECK_RET(rc);
5073 num = cast_string_to_number(str);
5074 free(str);
5075 set->val.num += num;
5076 }
5077
5078 free(set_item.val.nodes);
5079
5080 return LY_SUCCESS;
5081}
5082
5083/**
5084 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5085 * with the text content of the nodes in the context.
5086 *
5087 * @param[in] args Array of arguments.
5088 * @param[in] arg_count Count of elements in @p args.
5089 * @param[in,out] set Context and result set at the same time.
5090 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005091 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005092 */
5093static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005094xpath_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 +02005095{
5096 uint32_t i;
5097
5098 if (options & LYXP_SCNODE_ALL) {
5099 set_scnode_clear_ctx(set);
5100 return LY_SUCCESS;
5101 }
5102
Michal Vasko03ff5a72019-09-11 13:49:33 +02005103 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005104 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005105 return LY_EVALID;
5106 }
5107
Michal Vaskod989ba02020-08-24 10:59:24 +02005108 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005109 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005110 case LYXP_NODE_NONE:
5111 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005112 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005113 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5114 set->val.nodes[i].type = LYXP_NODE_TEXT;
5115 ++i;
5116 break;
5117 }
Radek Krejci0f969882020-08-21 16:56:47 +02005118 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005119 case LYXP_NODE_ROOT:
5120 case LYXP_NODE_ROOT_CONFIG:
5121 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005122 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005123 set_remove_node(set, i);
5124 break;
5125 }
5126 }
5127
5128 return LY_SUCCESS;
5129}
5130
5131/**
5132 * @brief Execute the XPath translate(string, string, string) function.
5133 * Returns LYXP_SET_STRING with the first argument with the characters
5134 * from the second argument replaced by those on the corresponding
5135 * positions in the third argument.
5136 *
5137 * @param[in] args Array of arguments.
5138 * @param[in] arg_count Count of elements in @p args.
5139 * @param[in,out] set Context and result set at the same time.
5140 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005141 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005142 */
5143static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005144xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005145{
5146 uint16_t i, j, new_used;
5147 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005148 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005149 struct lysc_node_leaf *sleaf;
5150 LY_ERR rc = LY_SUCCESS;
5151
5152 if (options & LYXP_SCNODE_ALL) {
5153 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5154 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5155 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 +02005156 } else if (!warn_is_string_type(sleaf->type)) {
5157 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005158 }
5159 }
5160
5161 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5162 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5163 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 +02005164 } else if (!warn_is_string_type(sleaf->type)) {
5165 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005166 }
5167 }
5168
5169 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5170 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5171 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 +02005172 } else if (!warn_is_string_type(sleaf->type)) {
5173 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005174 }
5175 }
5176 set_scnode_clear_ctx(set);
5177 return rc;
5178 }
5179
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005180 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005181 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005182 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005183 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005184 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005185 LY_CHECK_RET(rc);
5186
5187 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5188 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5189 new_used = 0;
5190
5191 have_removed = 0;
5192 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005193 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005194
5195 for (j = 0; args[1]->val.str[j]; ++j) {
5196 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5197 /* removing this char */
5198 if (j >= strlen(args[2]->val.str)) {
5199 have_removed = 1;
5200 found = 1;
5201 break;
5202 }
5203 /* replacing this char */
5204 new[new_used] = args[2]->val.str[j];
5205 ++new_used;
5206 found = 1;
5207 break;
5208 }
5209 }
5210
5211 /* copying this char */
5212 if (!found) {
5213 new[new_used] = args[0]->val.str[i];
5214 ++new_used;
5215 }
5216 }
5217
5218 if (have_removed) {
5219 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5220 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5221 }
5222 new[new_used] = '\0';
5223
Michal Vaskod3678892020-05-21 10:06:58 +02005224 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005225 set->type = LYXP_SET_STRING;
5226 set->val.str = new;
5227
5228 return LY_SUCCESS;
5229}
5230
5231/**
5232 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5233 * with true value.
5234 *
5235 * @param[in] args Array of arguments.
5236 * @param[in] arg_count Count of elements in @p args.
5237 * @param[in,out] set Context and result set at the same time.
5238 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005239 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005240 */
5241static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005242xpath_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 +02005243{
5244 if (options & LYXP_SCNODE_ALL) {
5245 set_scnode_clear_ctx(set);
5246 return LY_SUCCESS;
5247 }
5248
5249 set_fill_boolean(set, 1);
5250 return LY_SUCCESS;
5251}
5252
5253/*
5254 * moveto functions
5255 *
5256 * They and only they actually change the context (set).
5257 */
5258
5259/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005260 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005261 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005262 * XPath @p set is expected to be a (sc)node set!
5263 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005264 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5265 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005266 * @param[in] set Set with general XPath context.
5267 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005268 * @param[out] moveto_mod Expected module of a matching node.
5269 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005270 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005271static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005272moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5273 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005274{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005275 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005276 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005277 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005278
Michal Vasko2104e9f2020-03-06 08:23:25 +01005279 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5280
Michal Vasko6346ece2019-09-24 13:12:53 +02005281 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5282 /* specific module */
5283 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005284 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005285
Michal Vasko004d3152020-06-11 19:59:22 +02005286 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005287 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005288 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005289 return LY_EVALID;
5290 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005291
Michal Vasko6346ece2019-09-24 13:12:53 +02005292 *qname += pref_len + 1;
5293 *qname_len -= pref_len + 1;
5294 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5295 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005296 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005297 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005298 switch (set->format) {
5299 case LY_PREF_SCHEMA:
5300 case LY_PREF_SCHEMA_RESOLVED:
5301 /* current module */
5302 mod = set->cur_mod;
5303 break;
5304 case LY_PREF_JSON:
5305 /* inherit parent (context node) module */
5306 if (ctx_scnode) {
5307 mod = ctx_scnode->module;
5308 } else {
5309 mod = NULL;
5310 }
5311 break;
5312 case LY_PREF_XML:
5313 /* not defined */
5314 LOGINT_RET(set->ctx);
5315 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005316 }
5317
Michal Vasko6346ece2019-09-24 13:12:53 +02005318 *moveto_mod = mod;
5319 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005320}
5321
5322/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005323 * @brief Move context @p set to the root. Handles absolute path.
5324 * Result is LYXP_SET_NODE_SET.
5325 *
5326 * @param[in,out] set Set to use.
5327 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005328 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005329 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005330static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005331moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005332{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005333 if (!set) {
Michal Vaskob0099a92020-08-31 14:55:23 +02005334 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005335 }
5336
5337 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005338 set_scnode_clear_ctx(set);
Michal Vaskob0099a92020-08-31 14:55:23 +02005339 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005340 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005341 set->type = LYXP_SET_NODE_SET;
5342 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005343 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005344 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005345
5346 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005347}
5348
5349/**
5350 * @brief Check @p node as a part of NameTest processing.
5351 *
5352 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005353 * @param[in] ctx_node Context node.
5354 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005355 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005356 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005357 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005358 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5359 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005360 */
5361static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005362moveto_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 +01005363 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005364{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005365 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5366 switch (set->format) {
5367 case LY_PREF_SCHEMA:
5368 case LY_PREF_SCHEMA_RESOLVED:
5369 /* use current module */
5370 moveto_mod = set->cur_mod;
5371 break;
5372 case LY_PREF_JSON:
5373 /* inherit module of the context node, if any */
5374 if (ctx_node) {
5375 moveto_mod = ctx_node->schema->module;
5376 }
5377 break;
5378 case LY_PREF_XML:
5379 /* not defined */
5380 LOGINT(set->ctx);
5381 return LY_EINVAL;
5382 }
5383 }
5384
Michal Vasko03ff5a72019-09-11 13:49:33 +02005385 /* module check */
5386 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005387 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005388 }
5389
Michal Vasko5c4e5892019-11-14 12:31:38 +01005390 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005391 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005392 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005393 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5394 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005395 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005396 }
5397
5398 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005399 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005400 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005401 }
5402
Michal Vaskoa1424542019-11-14 16:08:52 +01005403 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005404 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005405 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005406 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005407
5408 /* match */
5409 return LY_SUCCESS;
5410}
5411
5412/**
5413 * @brief Check @p node as a part of schema NameTest processing.
5414 *
5415 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005416 * @param[in] ctx_scnode Context node.
5417 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005418 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005419 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005420 * @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 +02005421 */
5422static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005423moveto_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 +02005424 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005425{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005426 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5427 switch (set->format) {
5428 case LY_PREF_SCHEMA:
5429 case LY_PREF_SCHEMA_RESOLVED:
5430 /* use current module */
5431 moveto_mod = set->cur_mod;
5432 break;
5433 case LY_PREF_JSON:
5434 /* inherit module of the context node, if any */
5435 if (ctx_scnode) {
5436 moveto_mod = ctx_scnode->module;
5437 }
5438 break;
5439 case LY_PREF_XML:
5440 /* not defined */
5441 LOGINT(set->ctx);
5442 return LY_EINVAL;
5443 }
5444 }
5445
Michal Vasko03ff5a72019-09-11 13:49:33 +02005446 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005447 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005448 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005449 }
5450
5451 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005452 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005453 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005454 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005455 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005456 }
5457
5458 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005459 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005460 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005461 }
5462
5463 /* match */
5464 return LY_SUCCESS;
5465}
5466
5467/**
Michal Vaskod3678892020-05-21 10:06:58 +02005468 * @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 +02005469 *
5470 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005471 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005472 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005473 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005474 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5475 */
5476static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005477moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005478{
Michal Vaskof03ed032020-03-04 13:31:44 +01005479 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005480 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005481 LY_ERR rc;
5482
Michal Vaskod3678892020-05-21 10:06:58 +02005483 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005484 return LY_SUCCESS;
5485 }
5486
5487 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005488 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005489 return LY_EVALID;
5490 }
5491
Michal Vasko03ff5a72019-09-11 13:49:33 +02005492 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005493 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005494
5495 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 +01005496 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005497
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005498 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005499 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005500 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005501 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005502 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005503 ctx_node = set->val.nodes[i].node;
5504 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005505 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005506
Michal Vaskod3678892020-05-21 10:06:58 +02005507 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005508 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005509 if (rc == LY_SUCCESS) {
5510 if (!replaced) {
5511 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5512 replaced = 1;
5513 } else {
5514 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005515 }
Michal Vaskod3678892020-05-21 10:06:58 +02005516 ++i;
5517 } else if (rc == LY_EINCOMPLETE) {
5518 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005519 }
5520 }
5521
5522 if (!replaced) {
5523 /* no match */
5524 set_remove_node(set, i);
5525 }
5526 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005527
5528 return LY_SUCCESS;
5529}
5530
5531/**
Michal Vaskod3678892020-05-21 10:06:58 +02005532 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5533 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005534 *
5535 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005536 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005537 * @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 +01005538 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005539 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5540 */
5541static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005542moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5543 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005544{
Michal Vasko004d3152020-06-11 19:59:22 +02005545 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005546 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005547 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005548 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005549
Michal Vasko004d3152020-06-11 19:59:22 +02005550 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005551
5552 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005553 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005554 }
5555
5556 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005557 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005558 ret = LY_EVALID;
5559 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005560 }
5561
5562 /* context check for all the nodes since we have the schema node */
5563 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5564 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005565 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005566 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5567 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005568 lyxp_set_free_content(set);
5569 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005570 }
5571
5572 /* create specific data instance if needed */
5573 if (scnode->nodetype == LYS_LIST) {
5574 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5575 } else if (scnode->nodetype == LYS_LEAFLIST) {
5576 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005577 }
5578
5579 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005580 siblings = NULL;
5581
5582 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5583 assert(!set->val.nodes[i].node);
5584
5585 /* search in all the trees */
5586 siblings = set->tree;
5587 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5588 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005589 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005590 }
5591
5592 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005593 if (inst) {
5594 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005595 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005596 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005597 }
5598
5599 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005600 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005601 ret = LY_EINCOMPLETE;
5602 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005603 }
5604
5605 if (sub) {
5606 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005607 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005608 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005609 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005610 /* no match */
5611 set_remove_node(set, i);
5612 }
5613 }
5614
Michal Vasko004d3152020-06-11 19:59:22 +02005615cleanup:
5616 lyd_free_tree(inst);
5617 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005618}
5619
5620/**
5621 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5622 *
5623 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005624 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005625 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005626 * @param[in] options XPath options.
5627 * @return LY_ERR
5628 */
5629static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005630moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005631{
Radek Krejci857189e2020-09-01 13:26:36 +02005632 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005633 uint32_t getnext_opts;
5634 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005635 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005636 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005637 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005638
Michal Vaskod3678892020-05-21 10:06:58 +02005639 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005640 return LY_SUCCESS;
5641 }
5642
5643 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005644 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005645 return LY_EVALID;
5646 }
5647
Michal Vaskocafad9d2019-11-07 15:20:03 +01005648 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005649 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005650 if (options & LYXP_SCNODE_OUTPUT) {
5651 getnext_opts |= LYS_GETNEXT_OUTPUT;
5652 }
5653
Michal Vasko03ff5a72019-09-11 13:49:33 +02005654 orig_used = set->used;
5655 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005656 uint32_t idx;
5657
Radek Krejcif13b87b2020-12-01 22:02:17 +01005658 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5659 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005660 continue;
5661 }
5662
5663 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005664 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005665 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005666 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005667 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005668
5669 start_parent = set->val.scnodes[i].scnode;
5670
5671 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 +02005672 /* 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 +02005673 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005674 mod_idx = 0;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005675 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005676 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005677 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005678 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005679 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005680 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5681
Michal Vasko03ff5a72019-09-11 13:49:33 +02005682 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005683 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005684 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005685 temp_ctx = 1;
5686 }
5687 }
5688 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005689 }
5690
Michal Vasko519fd602020-05-26 12:17:39 +02005691 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5692 iter = NULL;
5693 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005694 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005695 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5696
5697 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005698 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005699 temp_ctx = 1;
5700 }
5701 }
5702 }
5703 }
5704 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005705
5706 /* correct temporary in_ctx values */
5707 if (temp_ctx) {
5708 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005709 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5710 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005711 }
5712 }
5713 }
5714
5715 return LY_SUCCESS;
5716}
5717
5718/**
Michal Vaskod3678892020-05-21 10:06:58 +02005719 * @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 +02005720 * Context position aware.
5721 *
5722 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005723 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005724 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005725 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005726 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5727 */
5728static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005729moveto_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 +02005730{
5731 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005732 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005733 struct lyxp_set ret_set;
5734 LY_ERR rc;
5735
Michal Vaskod3678892020-05-21 10:06:58 +02005736 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005737 return LY_SUCCESS;
5738 }
5739
5740 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005741 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005742 return LY_EVALID;
5743 }
5744
Michal Vasko9f96a052020-03-10 09:41:45 +01005745 /* 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 +01005746 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005747 LY_CHECK_RET(rc);
5748
Michal Vasko6346ece2019-09-24 13:12:53 +02005749 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005750 set_init(&ret_set, set);
5751 for (i = 0; i < set->used; ++i) {
5752
5753 /* TREE DFS */
5754 start = set->val.nodes[i].node;
5755 for (elem = next = start; elem; elem = next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005756 rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005757 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005758 /* add matching node into result set */
5759 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5760 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5761 /* the node is a duplicate, we'll process it later in the set */
5762 goto skip_children;
5763 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005764 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005765 return rc;
5766 } else if (rc == LY_EINVAL) {
5767 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005768 }
5769
5770 /* TREE DFS NEXT ELEM */
5771 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005772 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005773 if (!next) {
5774skip_children:
5775 /* no children, so try siblings, but only if it's not the start,
5776 * that is considered to be the root and it's siblings are not traversed */
5777 if (elem != start) {
5778 next = elem->next;
5779 } else {
5780 break;
5781 }
5782 }
5783 while (!next) {
5784 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01005785 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005786 /* we are done, no next element to process */
5787 break;
5788 }
5789 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01005790 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005791 next = elem->next;
5792 }
5793 }
5794 }
5795
5796 /* make the temporary set the current one */
5797 ret_set.ctx_pos = set->ctx_pos;
5798 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005799 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005800 memcpy(set, &ret_set, sizeof *set);
5801
5802 return LY_SUCCESS;
5803}
5804
5805/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005806 * @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 +02005807 *
5808 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005809 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005810 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005811 * @param[in] options XPath options.
5812 * @return LY_ERR
5813 */
5814static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005815moveto_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 +02005816{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005817 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005818 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005819 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005820
Michal Vaskod3678892020-05-21 10:06:58 +02005821 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005822 return LY_SUCCESS;
5823 }
5824
5825 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005826 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005827 return LY_EVALID;
5828 }
5829
Michal Vasko03ff5a72019-09-11 13:49:33 +02005830 orig_used = set->used;
5831 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005832 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5833 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005834 continue;
5835 }
5836
5837 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005838 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005839 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005840 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005841 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005842
5843 /* TREE DFS */
5844 start = set->val.scnodes[i].scnode;
5845 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005846 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5847 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005848 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005849 }
5850
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005851 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005852 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005853 uint32_t idx;
5854
5855 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005856 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005857 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005858 /* we will process it later in the set */
5859 goto skip_children;
5860 }
5861 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005862 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005863 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005864 } else if (rc == LY_EINVAL) {
5865 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005866 }
5867
5868next_iter:
5869 /* TREE DFS NEXT ELEM */
5870 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005871 next = lysc_node_child(elem);
5872 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5873 next = next->next;
5874 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5875 next = next->next;
5876 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005877 if (!next) {
5878skip_children:
5879 /* no children, so try siblings, but only if it's not the start,
5880 * that is considered to be the root and it's siblings are not traversed */
5881 if (elem != start) {
5882 next = elem->next;
5883 } else {
5884 break;
5885 }
5886 }
5887 while (!next) {
5888 /* no siblings, go back through the parents */
5889 if (elem->parent == start) {
5890 /* we are done, no next element to process */
5891 break;
5892 }
5893 /* parent is already processed, go to its sibling */
5894 elem = elem->parent;
5895 next = elem->next;
5896 }
5897 }
5898 }
5899
5900 return LY_SUCCESS;
5901}
5902
5903/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005904 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005905 * Indirectly context position aware.
5906 *
5907 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005908 * @param[in] mod Matching metadata module, NULL for any.
5909 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005910 * @return LY_ERR
5911 */
5912static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005913moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005914{
Michal Vasko9f96a052020-03-10 09:41:45 +01005915 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005916
Michal Vaskod3678892020-05-21 10:06:58 +02005917 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005918 return LY_SUCCESS;
5919 }
5920
5921 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005922 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005923 return LY_EVALID;
5924 }
5925
Radek Krejci1deb5be2020-08-26 16:43:36 +02005926 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005927 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005928
5929 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5930 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005931 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005932 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005933
5934 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005935 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005936 continue;
5937 }
5938
Michal Vaskod3678892020-05-21 10:06:58 +02005939 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005940 /* match */
5941 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005942 set->val.meta[i].meta = sub;
5943 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005944 /* pos does not change */
5945 replaced = 1;
5946 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005947 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 +02005948 }
5949 ++i;
5950 }
5951 }
5952 }
5953
5954 if (!replaced) {
5955 /* no match */
5956 set_remove_node(set, i);
5957 }
5958 }
5959
5960 return LY_SUCCESS;
5961}
5962
5963/**
5964 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005965 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005966 *
5967 * @param[in,out] set1 Set to use for the result.
5968 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005969 * @return LY_ERR
5970 */
5971static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005972moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005973{
5974 LY_ERR rc;
5975
Michal Vaskod3678892020-05-21 10:06:58 +02005976 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005977 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005978 return LY_EVALID;
5979 }
5980
5981 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005982 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005983 return LY_SUCCESS;
5984 }
5985
Michal Vaskod3678892020-05-21 10:06:58 +02005986 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005987 memcpy(set1, set2, sizeof *set1);
5988 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005989 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005990 return LY_SUCCESS;
5991 }
5992
5993 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005994 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005995
5996 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005997 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005998 LY_CHECK_RET(rc);
5999
6000 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006001 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006002
6003 return LY_SUCCESS;
6004}
6005
6006/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006007 * @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 +02006008 * Context position aware.
6009 *
6010 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006011 * @param[in] mod Matching metadata module, NULL for any.
6012 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006013 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006014 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6015 */
6016static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006017moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006018{
Michal Vasko9f96a052020-03-10 09:41:45 +01006019 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006020 struct lyxp_set *set_all_desc = NULL;
6021 LY_ERR rc;
6022
Michal Vaskod3678892020-05-21 10:06:58 +02006023 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006024 return LY_SUCCESS;
6025 }
6026
6027 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006028 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006029 return LY_EVALID;
6030 }
6031
Michal Vasko03ff5a72019-09-11 13:49:33 +02006032 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6033 * but it likely won't be used much, so it's a waste of time */
6034 /* copy the context */
6035 set_all_desc = set_copy(set);
6036 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006037 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006038 if (rc != LY_SUCCESS) {
6039 lyxp_set_free(set_all_desc);
6040 return rc;
6041 }
6042 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006043 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006044 if (rc != LY_SUCCESS) {
6045 lyxp_set_free(set_all_desc);
6046 return rc;
6047 }
6048 lyxp_set_free(set_all_desc);
6049
Radek Krejci1deb5be2020-08-26 16:43:36 +02006050 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006051 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006052
6053 /* only attributes of an elem can be in the result, skip all the rest,
6054 * we have all attributes qualified in lyd tree */
6055 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006056 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006057 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006058 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006059 continue;
6060 }
6061
Michal Vaskod3678892020-05-21 10:06:58 +02006062 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006063 /* match */
6064 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006065 set->val.meta[i].meta = sub;
6066 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006067 /* pos does not change */
6068 replaced = 1;
6069 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006070 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 +02006071 }
6072 ++i;
6073 }
6074 }
6075 }
6076
6077 if (!replaced) {
6078 /* no match */
6079 set_remove_node(set, i);
6080 }
6081 }
6082
6083 return LY_SUCCESS;
6084}
6085
6086/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006087 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6088 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006089 *
6090 * @param[in] parent Current parent.
6091 * @param[in] parent_pos Position of @p parent.
6092 * @param[in] parent_type Node type of @p parent.
6093 * @param[in,out] to_set Set to use.
6094 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006095 * @param[in] options XPath options.
6096 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6097 */
6098static LY_ERR
6099moveto_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 +02006100 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006101{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006102 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006103 LY_ERR rc;
6104
6105 switch (parent_type) {
6106 case LYXP_NODE_ROOT:
6107 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006108 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006109
Michal Vasko61ac2f62020-05-25 12:39:51 +02006110 /* add all top-level nodes as elements */
6111 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006112 break;
6113 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006114 /* add just the text node of this term element node */
6115 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006116 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6117 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6118 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006119 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006120 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006121
6122 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006123 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006124 break;
6125 default:
6126 LOGINT_RET(parent->schema->module->ctx);
6127 }
6128
Michal Vasko61ac2f62020-05-25 12:39:51 +02006129 /* add all top-level nodes as elements */
6130 LY_LIST_FOR(first, iter) {
6131 /* context check */
6132 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6133 continue;
6134 }
6135
6136 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006137 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006138 return LY_EINCOMPLETE;
6139 }
6140
6141 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6142 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6143
6144 /* also add all the children of this node, recursively */
6145 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6146 LY_CHECK_RET(rc);
6147 }
6148 }
6149
Michal Vasko03ff5a72019-09-11 13:49:33 +02006150 return LY_SUCCESS;
6151}
6152
6153/**
6154 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6155 * (or LYXP_SET_EMPTY). Context position aware.
6156 *
6157 * @param[in,out] set Set to use.
6158 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6159 * @param[in] options XPath options.
6160 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6161 */
6162static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006163moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006164{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006165 struct lyxp_set ret_set;
6166 LY_ERR rc;
6167
Michal Vaskod3678892020-05-21 10:06:58 +02006168 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006169 return LY_SUCCESS;
6170 }
6171
6172 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006173 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006174 return LY_EVALID;
6175 }
6176
6177 /* nothing to do */
6178 if (!all_desc) {
6179 return LY_SUCCESS;
6180 }
6181
Michal Vasko03ff5a72019-09-11 13:49:33 +02006182 /* add all the children, they get added recursively */
6183 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006184 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006185 /* copy the current node to tmp */
6186 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6187
6188 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006189 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006190 continue;
6191 }
6192
Michal Vasko03ff5a72019-09-11 13:49:33 +02006193 /* add all the children */
6194 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 +02006195 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006196 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006197 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006198 return rc;
6199 }
6200 }
6201
6202 /* use the temporary set as the current one */
6203 ret_set.ctx_pos = set->ctx_pos;
6204 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006205 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006206 memcpy(set, &ret_set, sizeof *set);
6207
6208 return LY_SUCCESS;
6209}
6210
6211/**
6212 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6213 * (or LYXP_SET_EMPTY).
6214 *
6215 * @param[in,out] set Set to use.
6216 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6217 * @param[in] options XPath options.
6218 * @return LY_ERR
6219 */
6220static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006221moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006222{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006223 uint32_t getnext_opts;
6224 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006225 const struct lysc_node *iter, *start_parent;
6226 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006227
Michal Vaskod3678892020-05-21 10:06:58 +02006228 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006229 return LY_SUCCESS;
6230 }
6231
6232 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006233 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006234 return LY_EVALID;
6235 }
6236
Michal Vasko519fd602020-05-26 12:17:39 +02006237 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006238 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006239 if (options & LYXP_SCNODE_OUTPUT) {
6240 getnext_opts |= LYS_GETNEXT_OUTPUT;
6241 }
6242
6243 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006244 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006245 if (!all_desc) {
6246 /* traverse the start node */
6247 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6248 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6249 }
6250 continue;
6251 }
6252
Radek Krejcif13b87b2020-12-01 22:02:17 +01006253 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6254 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006255 continue;
6256 }
6257
Michal Vasko519fd602020-05-26 12:17:39 +02006258 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006259 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006260 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006261 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006262 }
6263
Michal Vasko519fd602020-05-26 12:17:39 +02006264 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006265
Michal Vasko519fd602020-05-26 12:17:39 +02006266 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6267 /* it can actually be in any module, it's all <running> */
6268 mod_idx = 0;
6269 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6270 iter = NULL;
6271 /* module may not be implemented */
6272 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6273 /* context check */
6274 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6275 continue;
6276 }
6277
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006278 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006279 /* throw away the insert index, we want to consider that node again, recursively */
6280 }
6281 }
6282
6283 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6284 iter = NULL;
6285 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006286 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006287 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006288 continue;
6289 }
6290
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006291 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006292 }
6293 }
6294 }
6295
6296 return LY_SUCCESS;
6297}
6298
6299/**
6300 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6301 * (or LYXP_SET_EMPTY). Context position aware.
6302 *
6303 * @param[in] set Set to use.
6304 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6305 * @param[in] options XPath options.
6306 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6307 */
6308static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006309moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006310{
6311 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006312 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006313 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006314
Michal Vaskod3678892020-05-21 10:06:58 +02006315 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006316 return LY_SUCCESS;
6317 }
6318
6319 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006320 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006321 return LY_EVALID;
6322 }
6323
6324 if (all_desc) {
6325 /* <path>//.. == <path>//./.. */
6326 rc = moveto_self(set, 1, options);
6327 LY_CHECK_RET(rc);
6328 }
6329
Radek Krejci1deb5be2020-08-26 16:43:36 +02006330 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006331 node = set->val.nodes[i].node;
6332
6333 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9e685082021-01-29 14:49:09 +01006334 new_node = lyd_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006335 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6336 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006337 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6338 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006339 if (!new_node) {
6340 LOGINT_RET(set->ctx);
6341 }
6342 } else {
6343 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006344 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006345 continue;
6346 }
6347
Michal Vaskoa1424542019-11-14 16:08:52 +01006348 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006349 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 +02006350 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006351 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006352
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006353 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006354 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006355 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006356
Michal Vasko03ff5a72019-09-11 13:49:33 +02006357 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006358 /* 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 +02006359 new_type = LYXP_NODE_ELEM;
6360 }
6361
Michal Vasko03ff5a72019-09-11 13:49:33 +02006362 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006363 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006364 } else {
6365 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006366 }
6367 }
6368
Michal Vasko2caefc12019-11-14 16:07:56 +01006369 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006370 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006371
6372 return LY_SUCCESS;
6373}
6374
6375/**
6376 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6377 * (or LYXP_SET_EMPTY).
6378 *
6379 * @param[in] set Set to use.
6380 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6381 * @param[in] options XPath options.
6382 * @return LY_ERR
6383 */
6384static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006385moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006386{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006387 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006388 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006389 const struct lysc_node *node, *new_node;
6390 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006391
Michal Vaskod3678892020-05-21 10:06:58 +02006392 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006393 return LY_SUCCESS;
6394 }
6395
6396 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006397 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006398 return LY_EVALID;
6399 }
6400
6401 if (all_desc) {
6402 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006403 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006404 }
6405
Michal Vasko03ff5a72019-09-11 13:49:33 +02006406 orig_used = set->used;
6407 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006408 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6409 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006410 continue;
6411 }
6412
6413 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006414 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006415 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006416 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006417 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006418
6419 node = set->val.scnodes[i].scnode;
6420
6421 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006422 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006423 } else {
6424 /* root does not have a parent */
6425 continue;
6426 }
6427
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006428 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006429 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006430 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006431
Michal Vasko03ff5a72019-09-11 13:49:33 +02006432 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006433 /* 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 +02006434 new_type = LYXP_NODE_ELEM;
6435 }
6436
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006437 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6438 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006439 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006440 temp_ctx = 1;
6441 }
6442 }
6443
6444 if (temp_ctx) {
6445 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006446 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6447 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006448 }
6449 }
6450 }
6451
6452 return LY_SUCCESS;
6453}
6454
6455/**
6456 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6457 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6458 *
6459 * @param[in,out] set1 Set to use for the result.
6460 * @param[in] set2 Set acting as the second operand for @p op.
6461 * @param[in] op Comparison operator to process.
6462 * @param[in] options XPath options.
6463 * @return LY_ERR
6464 */
6465static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006466moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006467{
6468 /*
6469 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6470 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6471 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6472 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6473 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6474 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6475 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6476 *
6477 * '=' or '!='
6478 * BOOLEAN + BOOLEAN
6479 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6480 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6481 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6482 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6483 * NUMBER + NUMBER
6484 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6485 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6486 * STRING + STRING
6487 *
6488 * '<=', '<', '>=', '>'
6489 * NUMBER + NUMBER
6490 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6491 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6492 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6493 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6494 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6495 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6496 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6497 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6498 */
6499 struct lyxp_set iter1, iter2;
6500 int result;
6501 int64_t i;
6502 LY_ERR rc;
6503
Michal Vasko004d3152020-06-11 19:59:22 +02006504 memset(&iter1, 0, sizeof iter1);
6505 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006506
6507 /* iterative evaluation with node-sets */
6508 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6509 if (set1->type == LYXP_SET_NODE_SET) {
6510 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006511 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006512 switch (set2->type) {
6513 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006514 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006515 break;
6516 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006517 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006518 break;
6519 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006520 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006521 break;
6522 }
6523 LY_CHECK_RET(rc);
6524
Michal Vasko4c7763f2020-07-27 17:40:37 +02006525 /* canonize set2 */
6526 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6527
6528 /* compare recursively */
6529 rc = moveto_op_comp(&iter1, &iter2, op, options);
6530 lyxp_set_free_content(&iter2);
6531 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006532
6533 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006534 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006535 set_fill_boolean(set1, 1);
6536 return LY_SUCCESS;
6537 }
6538 }
6539 } else {
6540 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006541 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006542 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006543 case LYXP_SET_NUMBER:
6544 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6545 break;
6546 case LYXP_SET_BOOLEAN:
6547 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6548 break;
6549 default:
6550 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6551 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006552 }
6553 LY_CHECK_RET(rc);
6554
Michal Vasko4c7763f2020-07-27 17:40:37 +02006555 /* canonize set1 */
6556 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 +02006557
Michal Vasko4c7763f2020-07-27 17:40:37 +02006558 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006559 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006560 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006561 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006562
6563 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006564 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006565 set_fill_boolean(set1, 1);
6566 return LY_SUCCESS;
6567 }
6568 }
6569 }
6570
6571 /* false for all nodes */
6572 set_fill_boolean(set1, 0);
6573 return LY_SUCCESS;
6574 }
6575
6576 /* first convert properly */
6577 if ((op[0] == '=') || (op[0] == '!')) {
6578 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006579 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6580 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006581 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006582 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006583 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006584 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006585 LY_CHECK_RET(rc);
6586 } /* else we have 2 strings */
6587 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006588 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006589 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006590 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006591 LY_CHECK_RET(rc);
6592 }
6593
6594 assert(set1->type == set2->type);
6595
6596 /* compute result */
6597 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 Vaskoac6c72f2019-11-14 16:09:34 +01006604 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006605 }
6606 } else if (op[0] == '!') {
6607 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006608 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006609 } else if (set1->type == LYXP_SET_NUMBER) {
6610 result = (set1->val.num != set2->val.num);
6611 } else {
6612 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006613 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006614 }
6615 } else {
6616 assert(set1->type == LYXP_SET_NUMBER);
6617 if (op[0] == '<') {
6618 if (op[1] == '=') {
6619 result = (set1->val.num <= set2->val.num);
6620 } else {
6621 result = (set1->val.num < set2->val.num);
6622 }
6623 } else {
6624 if (op[1] == '=') {
6625 result = (set1->val.num >= set2->val.num);
6626 } else {
6627 result = (set1->val.num > set2->val.num);
6628 }
6629 }
6630 }
6631
6632 /* assign result */
6633 if (result) {
6634 set_fill_boolean(set1, 1);
6635 } else {
6636 set_fill_boolean(set1, 0);
6637 }
6638
6639 return LY_SUCCESS;
6640}
6641
6642/**
6643 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6644 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6645 *
6646 * @param[in,out] set1 Set to use for the result.
6647 * @param[in] set2 Set acting as the second operand for @p op.
6648 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006649 * @return LY_ERR
6650 */
6651static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006652moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006653{
6654 LY_ERR rc;
6655
6656 /* unary '-' */
6657 if (!set2 && (op[0] == '-')) {
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);
6660 set1->val.num *= -1;
6661 lyxp_set_free(set2);
6662 return LY_SUCCESS;
6663 }
6664
6665 assert(set1 && set2);
6666
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006667 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006668 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006669 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006670 LY_CHECK_RET(rc);
6671
6672 switch (op[0]) {
6673 /* '+' */
6674 case '+':
6675 set1->val.num += set2->val.num;
6676 break;
6677
6678 /* '-' */
6679 case '-':
6680 set1->val.num -= set2->val.num;
6681 break;
6682
6683 /* '*' */
6684 case '*':
6685 set1->val.num *= set2->val.num;
6686 break;
6687
6688 /* 'div' */
6689 case 'd':
6690 set1->val.num /= set2->val.num;
6691 break;
6692
6693 /* 'mod' */
6694 case 'm':
6695 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6696 break;
6697
6698 default:
6699 LOGINT_RET(set1->ctx);
6700 }
6701
6702 return LY_SUCCESS;
6703}
6704
6705/*
6706 * eval functions
6707 *
6708 * They execute a parsed XPath expression on some data subtree.
6709 */
6710
6711/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006712 * @brief Evaluate Predicate. Logs directly on error.
6713 *
Michal Vaskod3678892020-05-21 10:06:58 +02006714 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006715 *
6716 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006717 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006718 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6719 * @param[in] options XPath options.
6720 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6721 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6722 */
6723static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006724eval_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 +02006725{
6726 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006727 uint16_t orig_exp;
6728 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006729 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006730 struct lyxp_set set2;
6731 struct lyd_node *orig_parent;
6732
6733 /* '[' */
6734 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006735 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006736 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006737
6738 if (!set) {
6739only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006740 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006741 LY_CHECK_RET(rc);
6742 } else if (set->type == LYXP_SET_NODE_SET) {
6743 /* 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 +01006744 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006745
6746 /* empty set, nothing to evaluate */
6747 if (!set->used) {
6748 goto only_parse;
6749 }
6750
Michal Vasko004d3152020-06-11 19:59:22 +02006751 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006752 orig_pos = 0;
6753 orig_size = set->used;
6754 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006755 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006756 set_init(&set2, set);
6757 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6758 /* remember the node context position for position() and context size for last(),
6759 * predicates should always be evaluated with respect to the child axis (since we do
6760 * not support explicit axes) so we assign positions based on their parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006761 if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) {
6762 orig_parent = lyd_parent(set->val.nodes[i].node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006763 orig_pos = 1;
6764 } else {
6765 ++orig_pos;
6766 }
6767
6768 set2.ctx_pos = orig_pos;
6769 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006770 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006771
Michal Vasko004d3152020-06-11 19:59:22 +02006772 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006773 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006774 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006775 return rc;
6776 }
6777
6778 /* number is a position */
6779 if (set2.type == LYXP_SET_NUMBER) {
6780 if ((long long)set2.val.num == orig_pos) {
6781 set2.val.num = 1;
6782 } else {
6783 set2.val.num = 0;
6784 }
6785 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006786 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006787
6788 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006789 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006790 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006791 }
6792 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006793 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006794
6795 } else if (set->type == LYXP_SET_SCNODE_SET) {
6796 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006797 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006798 /* there is a currently-valid node */
6799 break;
6800 }
6801 }
6802 /* empty set, nothing to evaluate */
6803 if (i == set->used) {
6804 goto only_parse;
6805 }
6806
Michal Vasko004d3152020-06-11 19:59:22 +02006807 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006808
Michal Vasko03ff5a72019-09-11 13:49:33 +02006809 /* set special in_ctx to all the valid snodes */
6810 pred_in_ctx = set_scnode_new_in_ctx(set);
6811
6812 /* use the valid snodes one-by-one */
6813 for (i = 0; i < set->used; ++i) {
6814 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6815 continue;
6816 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006817 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006818
Michal Vasko004d3152020-06-11 19:59:22 +02006819 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006820
Michal Vasko004d3152020-06-11 19:59:22 +02006821 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006822 LY_CHECK_RET(rc);
6823
6824 set->val.scnodes[i].in_ctx = pred_in_ctx;
6825 }
6826
6827 /* restore the state as it was before the predicate */
6828 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006829 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
6830 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006831 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006832 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006833 }
6834 }
6835
6836 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006837 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006838 set_fill_set(&set2, set);
6839
Michal Vasko004d3152020-06-11 19:59:22 +02006840 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006841 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006842 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006843 return rc;
6844 }
6845
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006846 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006847 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006848 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006849 }
Michal Vaskod3678892020-05-21 10:06:58 +02006850 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006851 }
6852
6853 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006854 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006855 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006856 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006857 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006858
6859 return LY_SUCCESS;
6860}
6861
6862/**
Michal Vaskod3678892020-05-21 10:06:58 +02006863 * @brief Evaluate Literal. Logs directly on error.
6864 *
6865 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006866 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006867 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6868 */
6869static void
Michal Vasko40308e72020-10-20 16:38:40 +02006870eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006871{
6872 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006873 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006874 set_fill_string(set, "", 0);
6875 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006876 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 +02006877 }
6878 }
6879 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006880 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006881 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006882}
6883
6884/**
Michal Vasko004d3152020-06-11 19:59:22 +02006885 * @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 +02006886 *
Michal Vasko004d3152020-06-11 19:59:22 +02006887 * @param[in] exp Full parsed XPath expression.
6888 * @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 +02006889 * @param[in] ctx_node Found schema node as the context for the predicate.
6890 * @param[in] cur_mod Current module for the expression.
6891 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006892 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006893 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006894 * @param[out] predicates Parsed predicates.
6895 * @param[out] pred_type Type of @p predicates.
6896 * @return LY_SUCCESS on success,
6897 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006898 */
6899static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006900eval_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 +02006901 const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_PREFIX_FORMAT format, void *prefix_data,
6902 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006903{
Michal Vasko004d3152020-06-11 19:59:22 +02006904 LY_ERR ret = LY_SUCCESS;
6905 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006906 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006907 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006908 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006909 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006910
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006911 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006912
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006913 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006914 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006915 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006916 return LY_EINVAL;
6917 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006918 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 +02006919 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006920
Michal Vasko004d3152020-06-11 19:59:22 +02006921 /* learn where the predicates end */
6922 e_idx = *tok_idx;
6923 while (key_count) {
6924 /* '[' */
6925 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6926 return LY_EINVAL;
6927 }
6928 ++e_idx;
6929
6930 /* ']' */
6931 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6932 ++e_idx;
6933 }
6934 ++e_idx;
6935
6936 /* another presumably key predicate parsed */
6937 --key_count;
6938 }
Michal Vasko004d3152020-06-11 19:59:22 +02006939 } else {
6940 /* learn just where this single predicate ends */
6941 e_idx = *tok_idx;
6942
Michal Vaskod3678892020-05-21 10:06:58 +02006943 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006944 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6945 return LY_EINVAL;
6946 }
6947 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006948
6949 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006950 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6951 ++e_idx;
6952 }
6953 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006954 }
6955
Michal Vasko004d3152020-06-11 19:59:22 +02006956 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6957 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6958
6959 /* turn logging off */
6960 prev_lo = ly_log_options(0);
6961
6962 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006963 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
6964 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02006965
6966 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006967 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
6968 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006969 LY_CHECK_GOTO(ret, cleanup);
6970
6971 /* success, the predicate must include all the needed information for hash-based search */
6972 *tok_idx = e_idx;
6973
6974cleanup:
6975 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006976 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02006977 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006978}
6979
6980/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006981 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
6982 * data node(s) could be found using a single hash-based search.
6983 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01006984 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006985 * @param[in] node Next context node to check.
6986 * @param[in] name Expected node name.
6987 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01006988 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006989 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006990 * @param[in] format Prefix format.
6991 * @param[in,out] found Previously found node, is updated.
6992 * @return LY_SUCCESS on success,
6993 * @return LY_ENOT if the whole check failed and hashes cannot be used.
6994 */
6995static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01006996eval_name_test_with_predicate_get_scnode(const struct ly_ctx *ctx, const struct lyd_node *node, const char *name,
6997 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 +02006998 const struct lysc_node **found)
6999{
7000 const struct lysc_node *scnode;
7001 const struct lys_module *mod;
7002 uint32_t idx = 0;
7003
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007004 assert((format == LY_PREF_JSON) || moveto_mod);
7005
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007006continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007007 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007008 if (!node) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007009 if ((format == LY_PREF_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007010 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007011 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007012 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7013 if (scnode) {
7014 /* we have found a match */
7015 break;
7016 }
7017 }
7018
Michal Vasko7d1d0912020-10-16 08:38:30 +02007019 if (!scnode) {
7020 /* all modules searched */
7021 idx = 0;
7022 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007023 } else {
7024 /* search in top-level */
7025 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7026 }
7027 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007028 if ((format == LY_PREF_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007029 /* we must adjust the module to inherit the one from the context node */
7030 moveto_mod = node->schema->module;
7031 }
7032
7033 /* search in children, do not repeat the same search */
7034 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007035 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007036
7037 /* additional context check */
7038 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7039 scnode = NULL;
7040 }
7041
7042 if (scnode) {
7043 if (*found) {
7044 /* we found a schema node with the same name but at different level, give up, too complicated
7045 * (more hash-based searches would be required, not supported) */
7046 return LY_ENOT;
7047 } else {
7048 /* remember the found schema node and continue to make sure it can be used */
7049 *found = scnode;
7050 }
7051 }
7052
7053 if (idx) {
7054 /* continue searching all the following models */
7055 goto continue_search;
7056 }
7057
7058 return LY_SUCCESS;
7059}
7060
7061/**
Michal Vaskod3678892020-05-21 10:06:58 +02007062 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7063 *
7064 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7065 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7066 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7067 *
7068 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007069 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007070 * @param[in] attr_axis Whether to search attributes or standard nodes.
7071 * @param[in] all_desc Whether to search all the descendants or children only.
7072 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7073 * @param[in] options XPath options.
7074 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7075 */
7076static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007077eval_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 +02007078 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007079{
Michal Vaskod3678892020-05-21 10:06:58 +02007080 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007081 const char *ncname, *ncname_dict = NULL;
7082 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007083 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007084 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007085 struct ly_path_predicate *predicates = NULL;
7086 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007087 LY_ERR rc = LY_SUCCESS;
7088
7089 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007090 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007091 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007092
7093 if (!set) {
7094 goto moveto;
7095 }
7096
Michal Vasko004d3152020-06-11 19:59:22 +02007097 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7098 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007099
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007100 if ((ncname[0] == '*') && (ncname_len == 1)) {
7101 /* all nodes will match */
7102 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007103 }
7104
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007105 /* parse (and skip) module name */
7106 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007107 LY_CHECK_GOTO(rc, cleanup);
7108
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007109 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 +02007110 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007111 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007112 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7113 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007114 /* check failed */
7115 scnode = NULL;
7116 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007117 }
7118 }
7119
Michal Vasko004d3152020-06-11 19:59:22 +02007120 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7121 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007122 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7123 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007124 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007125 scnode = NULL;
7126 }
7127 }
7128 }
7129
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007130 if (!scnode) {
7131 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007132 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007133 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007134 }
7135
7136moveto:
7137 /* move to the attribute(s), data node(s), or schema node(s) */
7138 if (attr_axis) {
7139 if (set && (options & LYXP_SCNODE_ALL)) {
7140 set_scnode_clear_ctx(set);
7141 } else {
7142 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007143 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007144 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007145 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007146 }
7147 LY_CHECK_GOTO(rc, cleanup);
7148 }
7149 } else {
7150 if (set && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007151 int64_t i;
7152
Michal Vaskod3678892020-05-21 10:06:58 +02007153 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007154 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007155 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007156 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007157 }
7158 LY_CHECK_GOTO(rc, cleanup);
7159
7160 for (i = set->used - 1; i > -1; --i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007161 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM) {
Michal Vaskod3678892020-05-21 10:06:58 +02007162 break;
7163 }
7164 }
7165 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007166 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vasko90f12dc2020-12-03 14:20:42 +01007167 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".",
7168 ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007169 free(path);
7170 }
7171 } else {
7172 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007173 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007174 } else {
7175 if (scnode) {
7176 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007177 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007178 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007179 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007180 }
7181 }
7182 LY_CHECK_GOTO(rc, cleanup);
7183 }
7184 }
7185
7186 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007187 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7188 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007189 LY_CHECK_RET(rc);
7190 }
7191
7192cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007193 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007194 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007195 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007196 }
Michal Vaskod3678892020-05-21 10:06:58 +02007197 return rc;
7198}
7199
7200/**
7201 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7202 *
7203 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7204 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7205 * [8] NodeType ::= 'text' | 'node'
7206 *
7207 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007208 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007209 * @param[in] attr_axis Whether to search attributes or standard nodes.
7210 * @param[in] all_desc Whether to search all the descendants or children only.
7211 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7212 * @param[in] options XPath options.
7213 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7214 */
7215static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007216eval_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 +02007217 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007218{
7219 LY_ERR rc;
7220
7221 /* TODO */
7222 (void)attr_axis;
7223 (void)all_desc;
7224
7225 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007226 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007227 if (set->type == LYXP_SET_SCNODE_SET) {
7228 set_scnode_clear_ctx(set);
7229 /* just for the debug message below */
7230 set = NULL;
7231 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007232 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007233 rc = xpath_node(NULL, 0, set, options);
7234 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007235 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007236 rc = xpath_text(NULL, 0, set, options);
7237 }
7238 LY_CHECK_RET(rc);
7239 }
7240 }
7241 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007242 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007243 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007244
7245 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007246 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007247 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007248 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007249 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007250
7251 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007252 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007253 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007254 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007255 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007256
7257 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007258 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7259 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007260 LY_CHECK_RET(rc);
7261 }
7262
7263 return LY_SUCCESS;
7264}
7265
7266/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007267 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7268 *
7269 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7270 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007271 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007272 *
7273 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007274 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007275 * @param[in] all_desc Whether to search all the descendants or children only.
7276 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7277 * @param[in] options XPath options.
7278 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7279 */
7280static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007281eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7282 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007283{
Radek Krejci857189e2020-09-01 13:26:36 +02007284 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007285 LY_ERR rc;
7286
7287 goto step;
7288 do {
7289 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007290 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007291 all_desc = 0;
7292 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007293 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007294 all_desc = 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 Vasko03ff5a72019-09-11 13:49:33 +02007299
7300step:
Michal Vaskod3678892020-05-21 10:06:58 +02007301 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007302 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007303 attr_axis = 1;
7304
7305 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007306 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007307 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007308 } else {
7309 attr_axis = 0;
7310 }
7311
Michal Vasko03ff5a72019-09-11 13:49:33 +02007312 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007313 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007314 case LYXP_TOKEN_DOT:
7315 /* evaluate '.' */
7316 if (set && (options & LYXP_SCNODE_ALL)) {
7317 rc = moveto_scnode_self(set, all_desc, options);
7318 } else {
7319 rc = moveto_self(set, all_desc, options);
7320 }
7321 LY_CHECK_RET(rc);
7322 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007323 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007324 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007325 break;
7326
7327 case LYXP_TOKEN_DDOT:
7328 /* evaluate '..' */
7329 if (set && (options & LYXP_SCNODE_ALL)) {
7330 rc = moveto_scnode_parent(set, all_desc, options);
7331 } else {
7332 rc = moveto_parent(set, all_desc, options);
7333 }
7334 LY_CHECK_RET(rc);
7335 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007336 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007337 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007338 break;
7339
Michal Vasko03ff5a72019-09-11 13:49:33 +02007340 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007341 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007342 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007343 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007344 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007345
Michal Vaskod3678892020-05-21 10:06:58 +02007346 case LYXP_TOKEN_NODETYPE:
7347 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007348 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007349 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007350 break;
7351
7352 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007353 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007354 }
Michal Vasko004d3152020-06-11 19:59:22 +02007355 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007356
7357 return LY_SUCCESS;
7358}
7359
7360/**
7361 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7362 *
7363 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7364 *
7365 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007366 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007367 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7368 * @param[in] options XPath options.
7369 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7370 */
7371static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007372eval_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 +02007373{
Radek Krejci857189e2020-09-01 13:26:36 +02007374 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007375
7376 if (set) {
7377 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007378 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007379 }
7380
7381 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007382 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007383 /* evaluate '/' - deferred */
7384 all_desc = 0;
7385 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007386 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007387 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007388
Michal Vasko004d3152020-06-11 19:59:22 +02007389 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007390 return LY_SUCCESS;
7391 }
Michal Vasko004d3152020-06-11 19:59:22 +02007392 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007393 case LYXP_TOKEN_DOT:
7394 case LYXP_TOKEN_DDOT:
7395 case LYXP_TOKEN_AT:
7396 case LYXP_TOKEN_NAMETEST:
7397 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007398 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007399 break;
7400 default:
7401 break;
7402 }
7403
Michal Vasko03ff5a72019-09-11 13:49:33 +02007404 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007405 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007406 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7407 all_desc = 1;
7408 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007409 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007410 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007411
Michal Vaskob0099a92020-08-31 14:55:23 +02007412 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007413 }
7414
7415 return LY_SUCCESS;
7416}
7417
7418/**
7419 * @brief Evaluate FunctionCall. Logs directly on error.
7420 *
Michal Vaskod3678892020-05-21 10:06:58 +02007421 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007422 *
7423 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007424 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007425 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7426 * @param[in] options XPath options.
7427 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7428 */
7429static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007430eval_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 +02007431{
7432 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007433
Radek Krejci1deb5be2020-08-26 16:43:36 +02007434 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007435 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007436 struct lyxp_set **args = NULL, **args_aux;
7437
7438 if (set) {
7439 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007440 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007441 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007442 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007443 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007444 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007445 xpath_func = &xpath_sum;
7446 }
7447 break;
7448 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007449 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007450 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007451 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007452 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007453 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007454 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007455 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007456 xpath_func = &xpath_true;
7457 }
7458 break;
7459 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007460 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007461 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007462 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007463 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007464 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007465 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007466 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007467 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007468 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007469 xpath_func = &xpath_deref;
7470 }
7471 break;
7472 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007473 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007474 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007475 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007476 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007477 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007478 xpath_func = &xpath_string;
7479 }
7480 break;
7481 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007482 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007483 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007484 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007485 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007486 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007487 xpath_func = &xpath_current;
7488 }
7489 break;
7490 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007491 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007492 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007493 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007494 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007495 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007496 xpath_func = &xpath_re_match;
7497 }
7498 break;
7499 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007500 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007501 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007502 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007503 xpath_func = &xpath_translate;
7504 }
7505 break;
7506 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007507 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007508 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007509 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007510 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007511 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007512 xpath_func = &xpath_bit_is_set;
7513 }
7514 break;
7515 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007516 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007517 xpath_func = &xpath_starts_with;
7518 }
7519 break;
7520 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007521 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007522 xpath_func = &xpath_derived_from;
7523 }
7524 break;
7525 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007526 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007527 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007528 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007529 xpath_func = &xpath_string_length;
7530 }
7531 break;
7532 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007533 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007534 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007535 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007536 xpath_func = &xpath_substring_after;
7537 }
7538 break;
7539 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007540 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007541 xpath_func = &xpath_substring_before;
7542 }
7543 break;
7544 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007545 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007546 xpath_func = &xpath_derived_from_or_self;
7547 }
7548 break;
7549 }
7550
7551 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007552 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 +02007553 return LY_EVALID;
7554 }
7555 }
7556
7557 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007558 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007559 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007560
7561 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007562 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007563 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007564 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007565 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007566
7567 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007568 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007569 if (set) {
7570 args = malloc(sizeof *args);
7571 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7572 arg_count = 1;
7573 args[0] = set_copy(set);
7574 if (!args[0]) {
7575 rc = LY_EMEM;
7576 goto cleanup;
7577 }
7578
Michal Vasko004d3152020-06-11 19:59:22 +02007579 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007580 LY_CHECK_GOTO(rc, cleanup);
7581 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007582 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007583 LY_CHECK_GOTO(rc, cleanup);
7584 }
7585 }
Michal Vasko004d3152020-06-11 19:59:22 +02007586 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007587 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007588 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007589 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007590
7591 if (set) {
7592 ++arg_count;
7593 args_aux = realloc(args, arg_count * sizeof *args);
7594 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7595 args = args_aux;
7596 args[arg_count - 1] = set_copy(set);
7597 if (!args[arg_count - 1]) {
7598 rc = LY_EMEM;
7599 goto cleanup;
7600 }
7601
Michal Vasko004d3152020-06-11 19:59:22 +02007602 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007603 LY_CHECK_GOTO(rc, cleanup);
7604 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007605 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007606 LY_CHECK_GOTO(rc, cleanup);
7607 }
7608 }
7609
7610 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007611 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007612 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007613 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007614 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007615
7616 if (set) {
7617 /* evaluate function */
7618 rc = xpath_func(args, arg_count, set, options);
7619
7620 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007621 /* merge all nodes from arg evaluations */
7622 for (i = 0; i < arg_count; ++i) {
7623 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007624 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007625 }
7626 }
7627 } else {
7628 rc = LY_SUCCESS;
7629 }
7630
7631cleanup:
7632 for (i = 0; i < arg_count; ++i) {
7633 lyxp_set_free(args[i]);
7634 }
7635 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007636 return rc;
7637}
7638
7639/**
7640 * @brief Evaluate Number. Logs directly on error.
7641 *
7642 * @param[in] ctx Context for errors.
7643 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007644 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007645 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7646 * @return LY_ERR
7647 */
7648static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007649eval_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 +02007650{
7651 long double num;
7652 char *endptr;
7653
7654 if (set) {
7655 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007656 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007657 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007658 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7659 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007660 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007661 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007662 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007663 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7664 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007665 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007666 return LY_EVALID;
7667 }
7668
7669 set_fill_number(set, num);
7670 }
7671
7672 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007673 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007674 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007675 return LY_SUCCESS;
7676}
7677
7678/**
7679 * @brief Evaluate PathExpr. Logs directly on error.
7680 *
Michal Vaskod3678892020-05-21 10:06:58 +02007681 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007682 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7683 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7684 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007685 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007686 *
7687 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007688 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007689 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7690 * @param[in] options XPath options.
7691 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7692 */
7693static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007694eval_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 +02007695{
Radek Krejci857189e2020-09-01 13:26:36 +02007696 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007697 LY_ERR rc;
7698
Michal Vasko004d3152020-06-11 19:59:22 +02007699 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007700 case LYXP_TOKEN_PAR1:
7701 /* '(' Expr ')' */
7702
7703 /* '(' */
7704 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007705 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007706 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007707
7708 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007709 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007710 LY_CHECK_RET(rc);
7711
7712 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007713 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007714 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007715 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007716 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007717
7718 parent_pos_pred = 0;
7719 goto predicate;
7720
7721 case LYXP_TOKEN_DOT:
7722 case LYXP_TOKEN_DDOT:
7723 case LYXP_TOKEN_AT:
7724 case LYXP_TOKEN_NAMETEST:
7725 case LYXP_TOKEN_NODETYPE:
7726 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007727 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007728 LY_CHECK_RET(rc);
7729 break;
7730
7731 case LYXP_TOKEN_FUNCNAME:
7732 /* FunctionCall */
7733 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007734 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007735 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007736 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007737 }
7738 LY_CHECK_RET(rc);
7739
7740 parent_pos_pred = 1;
7741 goto predicate;
7742
Michal Vasko3e48bf32020-06-01 08:39:07 +02007743 case LYXP_TOKEN_OPER_PATH:
7744 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007745 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007746 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007747 LY_CHECK_RET(rc);
7748 break;
7749
7750 case LYXP_TOKEN_LITERAL:
7751 /* Literal */
7752 if (!set || (options & LYXP_SCNODE_ALL)) {
7753 if (set) {
7754 set_scnode_clear_ctx(set);
7755 }
Michal Vasko004d3152020-06-11 19:59:22 +02007756 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007757 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007758 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007759 }
7760
7761 parent_pos_pred = 1;
7762 goto predicate;
7763
7764 case LYXP_TOKEN_NUMBER:
7765 /* Number */
7766 if (!set || (options & LYXP_SCNODE_ALL)) {
7767 if (set) {
7768 set_scnode_clear_ctx(set);
7769 }
Michal Vasko004d3152020-06-11 19:59:22 +02007770 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007771 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007772 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007773 }
7774 LY_CHECK_RET(rc);
7775
7776 parent_pos_pred = 1;
7777 goto predicate;
7778
7779 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01007780 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 +02007781 return LY_EVALID;
7782 }
7783
7784 return LY_SUCCESS;
7785
7786predicate:
7787 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007788 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7789 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007790 LY_CHECK_RET(rc);
7791 }
7792
7793 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007794 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007795
7796 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007797 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007798 all_desc = 0;
7799 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007800 all_desc = 1;
7801 }
7802
7803 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007804 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007805 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007806
Michal Vasko004d3152020-06-11 19:59:22 +02007807 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007808 LY_CHECK_RET(rc);
7809 }
7810
7811 return LY_SUCCESS;
7812}
7813
7814/**
7815 * @brief Evaluate UnionExpr. Logs directly on error.
7816 *
Michal Vaskod3678892020-05-21 10:06:58 +02007817 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007818 *
7819 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007820 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007821 * @param[in] repeat How many times this expression is repeated.
7822 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7823 * @param[in] options XPath options.
7824 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7825 */
7826static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007827eval_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 +02007828{
7829 LY_ERR rc = LY_SUCCESS;
7830 struct lyxp_set orig_set, set2;
7831 uint16_t i;
7832
7833 assert(repeat);
7834
7835 set_init(&orig_set, set);
7836 set_init(&set2, set);
7837
7838 set_fill_set(&orig_set, set);
7839
Michal Vasko004d3152020-06-11 19:59:22 +02007840 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007841 LY_CHECK_GOTO(rc, cleanup);
7842
7843 /* ('|' PathExpr)* */
7844 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007845 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007846 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007847 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007848 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007849
7850 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007851 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007852 LY_CHECK_GOTO(rc, cleanup);
7853 continue;
7854 }
7855
7856 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007857 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007858 LY_CHECK_GOTO(rc, cleanup);
7859
7860 /* eval */
7861 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007862 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007863 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007864 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007865 LY_CHECK_GOTO(rc, cleanup);
7866 }
7867 }
7868
7869cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007870 lyxp_set_free_content(&orig_set);
7871 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007872 return rc;
7873}
7874
7875/**
7876 * @brief Evaluate UnaryExpr. Logs directly on error.
7877 *
Michal Vaskod3678892020-05-21 10:06:58 +02007878 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007879 *
7880 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007881 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007882 * @param[in] repeat How many times this expression is repeated.
7883 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7884 * @param[in] options XPath options.
7885 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7886 */
7887static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007888eval_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 +02007889{
7890 LY_ERR rc;
7891 uint16_t this_op, i;
7892
7893 assert(repeat);
7894
7895 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007896 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007897 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007898 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 +02007899
7900 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007901 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007902 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007903 }
7904
Michal Vasko004d3152020-06-11 19:59:22 +02007905 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007906 LY_CHECK_RET(rc);
7907
7908 if (set && (repeat % 2)) {
7909 if (options & LYXP_SCNODE_ALL) {
7910 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7911 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007912 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007913 LY_CHECK_RET(rc);
7914 }
7915 }
7916
7917 return LY_SUCCESS;
7918}
7919
7920/**
7921 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7922 *
Michal Vaskod3678892020-05-21 10:06:58 +02007923 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007924 * | MultiplicativeExpr '*' UnaryExpr
7925 * | MultiplicativeExpr 'div' UnaryExpr
7926 * | MultiplicativeExpr 'mod' UnaryExpr
7927 *
7928 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007929 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007930 * @param[in] repeat How many times this expression is repeated.
7931 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7932 * @param[in] options XPath options.
7933 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7934 */
7935static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007936eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
7937 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007938{
7939 LY_ERR rc;
7940 uint16_t this_op;
7941 struct lyxp_set orig_set, set2;
7942 uint16_t i;
7943
7944 assert(repeat);
7945
7946 set_init(&orig_set, set);
7947 set_init(&set2, set);
7948
7949 set_fill_set(&orig_set, set);
7950
Michal Vasko004d3152020-06-11 19:59:22 +02007951 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007952 LY_CHECK_GOTO(rc, cleanup);
7953
7954 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7955 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007956 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007957
Michal Vasko004d3152020-06-11 19:59:22 +02007958 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007959 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007960 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007961 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007962
7963 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007964 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007965 LY_CHECK_GOTO(rc, cleanup);
7966 continue;
7967 }
7968
7969 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007970 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007971 LY_CHECK_GOTO(rc, cleanup);
7972
7973 /* eval */
7974 if (options & LYXP_SCNODE_ALL) {
7975 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007976 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007977 set_scnode_clear_ctx(set);
7978 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007979 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007980 LY_CHECK_GOTO(rc, cleanup);
7981 }
7982 }
7983
7984cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007985 lyxp_set_free_content(&orig_set);
7986 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007987 return rc;
7988}
7989
7990/**
7991 * @brief Evaluate AdditiveExpr. Logs directly on error.
7992 *
Michal Vaskod3678892020-05-21 10:06:58 +02007993 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007994 * | AdditiveExpr '+' MultiplicativeExpr
7995 * | AdditiveExpr '-' MultiplicativeExpr
7996 *
7997 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007998 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007999 * @param[in] repeat How many times this expression is repeated.
8000 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8001 * @param[in] options XPath options.
8002 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8003 */
8004static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008005eval_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 +02008006{
8007 LY_ERR rc;
8008 uint16_t this_op;
8009 struct lyxp_set orig_set, set2;
8010 uint16_t i;
8011
8012 assert(repeat);
8013
8014 set_init(&orig_set, set);
8015 set_init(&set2, set);
8016
8017 set_fill_set(&orig_set, set);
8018
Michal Vasko004d3152020-06-11 19:59:22 +02008019 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008020 LY_CHECK_GOTO(rc, cleanup);
8021
8022 /* ('+' / '-' MultiplicativeExpr)* */
8023 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008024 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008025
Michal Vasko004d3152020-06-11 19:59:22 +02008026 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008027 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008028 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008029 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008030
8031 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008032 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008033 LY_CHECK_GOTO(rc, cleanup);
8034 continue;
8035 }
8036
8037 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008038 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008039 LY_CHECK_GOTO(rc, cleanup);
8040
8041 /* eval */
8042 if (options & LYXP_SCNODE_ALL) {
8043 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008044 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008045 set_scnode_clear_ctx(set);
8046 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008047 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008048 LY_CHECK_GOTO(rc, cleanup);
8049 }
8050 }
8051
8052cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008053 lyxp_set_free_content(&orig_set);
8054 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008055 return rc;
8056}
8057
8058/**
8059 * @brief Evaluate RelationalExpr. Logs directly on error.
8060 *
Michal Vaskod3678892020-05-21 10:06:58 +02008061 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008062 * | RelationalExpr '<' AdditiveExpr
8063 * | RelationalExpr '>' AdditiveExpr
8064 * | RelationalExpr '<=' AdditiveExpr
8065 * | RelationalExpr '>=' AdditiveExpr
8066 *
8067 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008068 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008069 * @param[in] repeat How many times this expression is repeated.
8070 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8071 * @param[in] options XPath options.
8072 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8073 */
8074static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008075eval_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 +02008076{
8077 LY_ERR rc;
8078 uint16_t this_op;
8079 struct lyxp_set orig_set, set2;
8080 uint16_t i;
8081
8082 assert(repeat);
8083
8084 set_init(&orig_set, set);
8085 set_init(&set2, set);
8086
8087 set_fill_set(&orig_set, set);
8088
Michal Vasko004d3152020-06-11 19:59:22 +02008089 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008090 LY_CHECK_GOTO(rc, cleanup);
8091
8092 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8093 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008094 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008095
Michal Vasko004d3152020-06-11 19:59:22 +02008096 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008097 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008098 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008099 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008100
8101 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008102 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008103 LY_CHECK_GOTO(rc, cleanup);
8104 continue;
8105 }
8106
8107 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008108 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008109 LY_CHECK_GOTO(rc, cleanup);
8110
8111 /* eval */
8112 if (options & LYXP_SCNODE_ALL) {
8113 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008114 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008115 set_scnode_clear_ctx(set);
8116 } else {
8117 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8118 LY_CHECK_GOTO(rc, cleanup);
8119 }
8120 }
8121
8122cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008123 lyxp_set_free_content(&orig_set);
8124 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008125 return rc;
8126}
8127
8128/**
8129 * @brief Evaluate EqualityExpr. Logs directly on error.
8130 *
Michal Vaskod3678892020-05-21 10:06:58 +02008131 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008132 * | EqualityExpr '!=' RelationalExpr
8133 *
8134 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008135 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008136 * @param[in] repeat How many times this expression is repeated.
8137 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8138 * @param[in] options XPath options.
8139 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8140 */
8141static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008142eval_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 +02008143{
8144 LY_ERR rc;
8145 uint16_t this_op;
8146 struct lyxp_set orig_set, set2;
8147 uint16_t i;
8148
8149 assert(repeat);
8150
8151 set_init(&orig_set, set);
8152 set_init(&set2, set);
8153
8154 set_fill_set(&orig_set, set);
8155
Michal Vasko004d3152020-06-11 19:59:22 +02008156 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008157 LY_CHECK_GOTO(rc, cleanup);
8158
8159 /* ('=' / '!=' RelationalExpr)* */
8160 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008161 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008162
Michal Vasko004d3152020-06-11 19:59:22 +02008163 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008164 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008165 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008166 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008167
8168 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008169 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008170 LY_CHECK_GOTO(rc, cleanup);
8171 continue;
8172 }
8173
8174 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008175 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008176 LY_CHECK_GOTO(rc, cleanup);
8177
8178 /* eval */
8179 if (options & LYXP_SCNODE_ALL) {
8180 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008181 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8182 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008183 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008184 set_scnode_clear_ctx(set);
8185 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008186 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8187 LY_CHECK_GOTO(rc, cleanup);
8188 }
8189 }
8190
8191cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008192 lyxp_set_free_content(&orig_set);
8193 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008194 return rc;
8195}
8196
8197/**
8198 * @brief Evaluate AndExpr. Logs directly on error.
8199 *
Michal Vaskod3678892020-05-21 10:06:58 +02008200 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008201 *
8202 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008203 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008204 * @param[in] repeat How many times this expression is repeated.
8205 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8206 * @param[in] options XPath options.
8207 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8208 */
8209static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008210eval_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 +02008211{
8212 LY_ERR rc;
8213 struct lyxp_set orig_set, set2;
8214 uint16_t i;
8215
8216 assert(repeat);
8217
8218 set_init(&orig_set, set);
8219 set_init(&set2, set);
8220
8221 set_fill_set(&orig_set, set);
8222
Michal Vasko004d3152020-06-11 19:59:22 +02008223 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008224 LY_CHECK_GOTO(rc, cleanup);
8225
8226 /* cast to boolean, we know that will be the final result */
8227 if (set && (options & LYXP_SCNODE_ALL)) {
8228 set_scnode_clear_ctx(set);
8229 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008230 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008231 }
8232
8233 /* ('and' EqualityExpr)* */
8234 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008235 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8236 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008237 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008238 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008239
8240 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008241 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8242 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008243 LY_CHECK_GOTO(rc, cleanup);
8244 continue;
8245 }
8246
8247 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008248 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008249 LY_CHECK_GOTO(rc, cleanup);
8250
8251 /* eval - just get boolean value actually */
8252 if (set->type == LYXP_SET_SCNODE_SET) {
8253 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008254 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008255 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008256 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008257 set_fill_set(set, &set2);
8258 }
8259 }
8260
8261cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008262 lyxp_set_free_content(&orig_set);
8263 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008264 return rc;
8265}
8266
8267/**
8268 * @brief Evaluate OrExpr. Logs directly on error.
8269 *
Michal Vaskod3678892020-05-21 10:06:58 +02008270 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008271 *
8272 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008273 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008274 * @param[in] repeat How many times this expression is repeated.
8275 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8276 * @param[in] options XPath options.
8277 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8278 */
8279static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008280eval_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 +02008281{
8282 LY_ERR rc;
8283 struct lyxp_set orig_set, set2;
8284 uint16_t i;
8285
8286 assert(repeat);
8287
8288 set_init(&orig_set, set);
8289 set_init(&set2, set);
8290
8291 set_fill_set(&orig_set, set);
8292
Michal Vasko004d3152020-06-11 19:59:22 +02008293 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008294 LY_CHECK_GOTO(rc, cleanup);
8295
8296 /* cast to boolean, we know that will be the final result */
8297 if (set && (options & LYXP_SCNODE_ALL)) {
8298 set_scnode_clear_ctx(set);
8299 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008300 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008301 }
8302
8303 /* ('or' AndExpr)* */
8304 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008305 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8306 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008307 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008308 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008309
8310 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008311 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8312 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008313 LY_CHECK_GOTO(rc, cleanup);
8314 continue;
8315 }
8316
8317 set_fill_set(&set2, &orig_set);
8318 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8319 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008320 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008321 LY_CHECK_GOTO(rc, cleanup);
8322
8323 /* eval - just get boolean value actually */
8324 if (set->type == LYXP_SET_SCNODE_SET) {
8325 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008326 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008327 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008328 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008329 set_fill_set(set, &set2);
8330 }
8331 }
8332
8333cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008334 lyxp_set_free_content(&orig_set);
8335 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008336 return rc;
8337}
8338
8339/**
Michal Vasko004d3152020-06-11 19:59:22 +02008340 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008341 *
8342 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008343 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008344 * @param[in] etype Expression type to evaluate.
8345 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8346 * @param[in] options XPath options.
8347 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8348 */
8349static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008350eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8351 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008352{
8353 uint16_t i, count;
8354 enum lyxp_expr_type next_etype;
8355 LY_ERR rc;
8356
8357 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008358 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008359 next_etype = LYXP_EXPR_NONE;
8360 } else {
8361 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008362 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008363
8364 /* select one-priority lower because etype expression called us */
8365 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008366 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008367 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008368 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008369 } else {
8370 next_etype = LYXP_EXPR_NONE;
8371 }
8372 }
8373
8374 /* decide what expression are we parsing based on the repeat */
8375 switch (next_etype) {
8376 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008377 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008378 break;
8379 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008380 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008381 break;
8382 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008383 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008384 break;
8385 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008386 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008387 break;
8388 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008389 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008390 break;
8391 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008392 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008393 break;
8394 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008395 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008396 break;
8397 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008398 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008399 break;
8400 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008401 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008402 break;
8403 default:
8404 LOGINT_RET(set->ctx);
8405 }
8406
8407 return rc;
8408}
8409
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008410/**
8411 * @brief Get root type.
8412 *
8413 * @param[in] ctx_node Context node.
8414 * @param[in] ctx_scnode Schema context node.
8415 * @param[in] options XPath options.
8416 * @return Root type.
8417 */
8418static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008419lyxp_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 +01008420{
Michal Vasko6b26e742020-07-17 15:02:10 +02008421 const struct lysc_node *op;
8422
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008423 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008424 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008425 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008426
8427 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008428 /* general root that can access everything */
8429 return LYXP_NODE_ROOT;
8430 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8431 /* root context node can access only config data (because we said so, it is unspecified) */
8432 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008433 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008434 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008435 }
8436
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008437 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008438 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008439 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008440
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008441 if (op || !(options & LYXP_SCHEMA)) {
8442 /* general root that can access everything */
8443 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05008444 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008445 /* root context node can access only config data (because we said so, it is unspecified) */
8446 return LYXP_NODE_ROOT_CONFIG;
8447 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008448 return LYXP_NODE_ROOT;
8449}
8450
Michal Vasko03ff5a72019-09-11 13:49:33 +02008451LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008452lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
8453 LY_PREFIX_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node, const struct lyd_node *tree,
8454 struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008455{
Michal Vasko004d3152020-06-11 19:59:22 +02008456 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008457 LY_ERR rc;
8458
Michal Vasko400e9672021-01-11 13:39:17 +01008459 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008460 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8461 LOGARG(NULL, "Current module must be set if schema format is used.");
8462 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008463 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008464
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008465 if (tree) {
8466 /* adjust the pointer to be the first top-level sibling */
8467 while (tree->parent) {
8468 tree = lyd_parent(tree);
8469 }
8470 tree = lyd_first_sibling(tree);
8471 }
8472
Michal Vasko03ff5a72019-09-11 13:49:33 +02008473 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008474 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008475 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008476 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8477 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8478
Michal Vasko400e9672021-01-11 13:39:17 +01008479 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008480 set->cur_node = ctx_node;
8481 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008482 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8483 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008484 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008485 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008486 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008487 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008488
Radek Krejciddace2c2021-01-08 11:30:56 +01008489 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008490
Michal Vasko03ff5a72019-09-11 13:49:33 +02008491 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008492 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008493 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008494 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008495 }
8496
Radek Krejciddace2c2021-01-08 11:30:56 +01008497 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008498 return rc;
8499}
8500
8501#if 0
8502
8503/* full xml printing of set elements, not used currently */
8504
8505void
8506lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8507{
8508 uint32_t i;
8509 char *str_num;
8510 struct lyout out;
8511
8512 memset(&out, 0, sizeof out);
8513
8514 out.type = LYOUT_STREAM;
8515 out.method.f = f;
8516
8517 switch (set->type) {
8518 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008519 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008520 break;
8521 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008522 ly_print_(&out, "Boolean XPath set:\n");
8523 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008524 break;
8525 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008526 ly_print_(&out, "String XPath set:\n");
8527 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008528 break;
8529 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008530 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008531
8532 if (isnan(set->value.num)) {
8533 str_num = strdup("NaN");
8534 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8535 str_num = strdup("0");
8536 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8537 str_num = strdup("Infinity");
8538 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8539 str_num = strdup("-Infinity");
8540 } else if ((long long)set->value.num == set->value.num) {
8541 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8542 str_num = NULL;
8543 }
8544 } else {
8545 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8546 str_num = NULL;
8547 }
8548 }
8549 if (!str_num) {
8550 LOGMEM;
8551 return;
8552 }
Michal Vasko5233e962020-08-14 14:26:20 +02008553 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008554 free(str_num);
8555 break;
8556 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008557 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008558
8559 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008560 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008561 switch (set->node_type[i]) {
8562 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008563 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008564 break;
8565 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008566 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008567 break;
8568 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008569 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008570 break;
8571 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008572 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008573 break;
8574 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008575 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008576 break;
8577 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008578 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008579 break;
8580 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008581 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008582 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008583 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008584 break;
8585 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008586 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 +02008587 break;
8588 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008589 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 +02008590 break;
8591 }
8592 }
8593 break;
8594 }
8595}
8596
8597#endif
8598
8599LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008600lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008601{
8602 long double num;
8603 char *str;
8604 LY_ERR rc;
8605
8606 if (!set || (set->type == target)) {
8607 return LY_SUCCESS;
8608 }
8609
8610 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008611 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008612
8613 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008614 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008615 return LY_EINVAL;
8616 }
8617
8618 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008619 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008620 switch (set->type) {
8621 case LYXP_SET_NUMBER:
8622 if (isnan(set->val.num)) {
8623 set->val.str = strdup("NaN");
8624 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8625 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8626 set->val.str = strdup("0");
8627 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8628 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8629 set->val.str = strdup("Infinity");
8630 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8631 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8632 set->val.str = strdup("-Infinity");
8633 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8634 } else if ((long long)set->val.num == set->val.num) {
8635 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8636 LOGMEM_RET(set->ctx);
8637 }
8638 set->val.str = str;
8639 } else {
8640 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8641 LOGMEM_RET(set->ctx);
8642 }
8643 set->val.str = str;
8644 }
8645 break;
8646 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008647 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008648 set->val.str = strdup("true");
8649 } else {
8650 set->val.str = strdup("false");
8651 }
8652 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8653 break;
8654 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008655 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008656 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008657
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008658 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008659 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008660 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008661 set->val.str = str;
8662 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008663 default:
8664 LOGINT_RET(set->ctx);
8665 }
8666 set->type = LYXP_SET_STRING;
8667 }
8668
8669 /* to NUMBER */
8670 if (target == LYXP_SET_NUMBER) {
8671 switch (set->type) {
8672 case LYXP_SET_STRING:
8673 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008674 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008675 set->val.num = num;
8676 break;
8677 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008678 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008679 set->val.num = 1;
8680 } else {
8681 set->val.num = 0;
8682 }
8683 break;
8684 default:
8685 LOGINT_RET(set->ctx);
8686 }
8687 set->type = LYXP_SET_NUMBER;
8688 }
8689
8690 /* to BOOLEAN */
8691 if (target == LYXP_SET_BOOLEAN) {
8692 switch (set->type) {
8693 case LYXP_SET_NUMBER:
8694 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008695 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008696 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008697 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008698 }
8699 break;
8700 case LYXP_SET_STRING:
8701 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008702 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008703 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008704 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008705 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008706 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008707 }
8708 break;
8709 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008710 if (set->used) {
8711 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008712 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008713 } else {
8714 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008715 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008716 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008717 break;
8718 default:
8719 LOGINT_RET(set->ctx);
8720 }
8721 set->type = LYXP_SET_BOOLEAN;
8722 }
8723
Michal Vasko03ff5a72019-09-11 13:49:33 +02008724 return LY_SUCCESS;
8725}
8726
8727LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008728lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
8729 LY_PREFIX_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
8730 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008731{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008732 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008733 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008734
Michal Vasko400e9672021-01-11 13:39:17 +01008735 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008736 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8737 LOGARG(NULL, "Current module must be set if schema format is used.");
8738 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008739 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008740
8741 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008742 memset(set, 0, sizeof *set);
8743 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008744 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8745 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 +01008746 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008747
Michal Vasko400e9672021-01-11 13:39:17 +01008748 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008749 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008750 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008751 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8752 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008753 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008754 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008755 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008756
Radek Krejciddace2c2021-01-08 11:30:56 +01008757 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008758
Michal Vasko03ff5a72019-09-11 13:49:33 +02008759 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01008760 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
8761
Radek Krejciddace2c2021-01-08 11:30:56 +01008762 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008763 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008764}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008765
8766API const char *
8767lyxp_get_expr(const struct lyxp_expr *path)
8768{
8769 if (!path) {
8770 return NULL;
8771 }
8772
8773 return path->expr;
8774}