blob: f2e7fe69e02092d1852f9302fe87884ca6a5b6d2 [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 */
Christian Hopps32874e12021-05-01 09:43:54 -040014#define _GNU_SOURCE /* asprintf, strdup */
15#include <sys/cdefs.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010016
Radek Krejci535ea9f2020-05-29 16:01:05 +020017#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010018
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010020#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010024#include <stdio.h>
25#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010026#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010027
Radek Krejci535ea9f2020-05-29 16:01:05 +020028#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020029#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020032#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010033#include "out.h"
Radek Krejci7931b192020-06-25 17:05:03 +020034#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020035#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020036#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020038#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020039#include "tree.h"
Radek Krejci77114102021-03-10 15:21:57 +010040#include "tree_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020041#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010042#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020043#include "tree_schema_internal.h"
44#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020045
aPiecekbf968d92021-05-27 14:35:05 +020046static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth);
Michal Vasko40308e72020-10-20 16:38:40 +020047static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype,
48 struct lyxp_set *set, uint32_t options);
Michal Vasko93923692021-05-07 15:28:02 +020049static LY_ERR moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
50 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod);
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,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200199 item->node->schema->name, lyd_get_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,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200202 item->node->schema->name, lyd_get_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 {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200212 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, lyd_get_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:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200367 value_str = lyd_get_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:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200525 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200526 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.
Michal Vasko1a09b212021-05-06 13:00:10 +0200944 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200945 */
946static void
Michal Vasko1a09b212021-05-06 13:00:10 +0200947set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200948{
949 uint32_t i;
950
951 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100952 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +0200953 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100954 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
955 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200956 }
957 }
958}
959
960/**
961 * @brief Remove a node from a set. Removing last node changes
962 * set into LYXP_SET_EMPTY. Context position aware.
963 *
964 * @param[in] set Set to use.
965 * @param[in] idx Index from @p set of the node to be removed.
966 */
967static void
968set_remove_node(struct lyxp_set *set, uint32_t idx)
969{
970 assert(set && (set->type == LYXP_SET_NODE_SET));
971 assert(idx < set->used);
972
973 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
974
975 --set->used;
976 if (set->used) {
977 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
978 (set->used - idx) * sizeof *set->val.nodes);
979 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200980 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200981 }
982}
983
984/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100985 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200986 *
987 * @param[in] set Set to use.
988 * @param[in] idx Index from @p set of the node to be removed.
989 */
990static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100991set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200992{
993 assert(set && (set->type == LYXP_SET_NODE_SET));
994 assert(idx < set->used);
995
Michal Vasko2caefc12019-11-14 16:07:56 +0100996 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
997 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
998 }
999 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001000}
1001
1002/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001003 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001004 * set into LYXP_SET_EMPTY. Context position aware.
1005 *
1006 * @param[in] set Set to consolidate.
1007 */
1008static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001009set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001010{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001011 uint32_t i, orig_used, end = 0;
1012 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001013
Michal Vaskod3678892020-05-21 10:06:58 +02001014 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001015
1016 orig_used = set->used;
1017 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001018 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001019 start = -1;
1020 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001021 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001022 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001023 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001024 end = i;
1025 ++i;
1026 break;
1027 }
1028
1029 ++i;
1030 if (i == orig_used) {
1031 end = i;
1032 }
1033 } while (i < orig_used);
1034
1035 if (start > -1) {
1036 /* move the whole chunk of valid nodes together */
1037 if (set->used != (unsigned)start) {
1038 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1039 }
1040 set->used += end - start;
1041 }
1042 }
Michal Vasko57eab132019-09-24 11:46:26 +02001043}
1044
1045/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001046 * @brief Check for duplicates in a node set.
1047 *
1048 * @param[in] set Set to check.
1049 * @param[in] node Node to look for in @p set.
1050 * @param[in] node_type Type of @p node.
1051 * @param[in] skip_idx Index from @p set to skip.
1052 * @return LY_ERR
1053 */
1054static LY_ERR
1055set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1056{
1057 uint32_t i;
1058
Michal Vasko2caefc12019-11-14 16:07:56 +01001059 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001060 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1061 }
1062
1063 for (i = 0; i < set->used; ++i) {
1064 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1065 continue;
1066 }
1067
1068 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1069 return LY_EEXIST;
1070 }
1071 }
1072
1073 return LY_SUCCESS;
1074}
1075
Radek Krejci857189e2020-09-01 13:26:36 +02001076ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001077lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1078 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001079{
1080 uint32_t i;
1081
1082 for (i = 0; i < set->used; ++i) {
1083 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1084 continue;
1085 }
1086
1087 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001088 if (index_p) {
1089 *index_p = i;
1090 }
1091 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001092 }
1093 }
1094
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001095 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001096}
1097
Michal Vaskoecd62de2019-11-13 12:35:11 +01001098void
1099lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001100{
1101 uint32_t orig_used, i, j;
1102
Michal Vaskod3678892020-05-21 10:06:58 +02001103 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001104
Michal Vaskod3678892020-05-21 10:06:58 +02001105 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001106 return;
1107 }
1108
Michal Vaskod3678892020-05-21 10:06:58 +02001109 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001110 memcpy(set1, set2, sizeof *set1);
1111 return;
1112 }
1113
1114 if (set1->used + set2->used > set1->size) {
1115 set1->size = set1->used + set2->used;
1116 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1117 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1118 }
1119
1120 orig_used = set1->used;
1121
1122 for (i = 0; i < set2->used; ++i) {
1123 for (j = 0; j < orig_used; ++j) {
1124 /* detect duplicities */
1125 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1126 break;
1127 }
1128 }
1129
Michal Vasko0587bce2020-12-10 12:19:21 +01001130 if (j < orig_used) {
1131 /* node is there, but update its status if needed */
1132 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1133 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001134 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1135 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1136 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001137 }
1138 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001139 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1140 ++set1->used;
1141 }
1142 }
1143
Michal Vaskod3678892020-05-21 10:06:58 +02001144 lyxp_set_free_content(set2);
1145 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001146}
1147
1148/**
1149 * @brief Insert a node into a set. Context position aware.
1150 *
1151 * @param[in] set Set to use.
1152 * @param[in] node Node to insert to @p set.
1153 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1154 * @param[in] node_type Node type of @p node.
1155 * @param[in] idx Index in @p set to insert into.
1156 */
1157static void
1158set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1159{
Michal Vaskod3678892020-05-21 10:06:58 +02001160 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001161
Michal Vaskod3678892020-05-21 10:06:58 +02001162 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001163 /* first item */
1164 if (idx) {
1165 /* no real harm done, but it is a bug */
1166 LOGINT(set->ctx);
1167 idx = 0;
1168 }
1169 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1170 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1171 set->type = LYXP_SET_NODE_SET;
1172 set->used = 0;
1173 set->size = LYXP_SET_SIZE_START;
1174 set->ctx_pos = 1;
1175 set->ctx_size = 1;
1176 set->ht = NULL;
1177 } else {
1178 /* not an empty set */
1179 if (set->used == set->size) {
1180
1181 /* set is full */
1182 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1183 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1184 set->size += LYXP_SET_SIZE_STEP;
1185 }
1186
1187 if (idx > set->used) {
1188 LOGINT(set->ctx);
1189 idx = set->used;
1190 }
1191
1192 /* make space for the new node */
1193 if (idx < set->used) {
1194 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1195 }
1196 }
1197
1198 /* finally assign the value */
1199 set->val.nodes[idx].node = (struct lyd_node *)node;
1200 set->val.nodes[idx].type = node_type;
1201 set->val.nodes[idx].pos = pos;
1202 ++set->used;
1203
Michal Vasko2caefc12019-11-14 16:07:56 +01001204 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1205 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1206 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001207}
1208
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001209LY_ERR
1210lyxp_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 +02001211{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001212 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001213
1214 assert(set->type == LYXP_SET_SCNODE_SET);
1215
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001216 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001217 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001218 } else {
1219 if (set->used == set->size) {
1220 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 +02001221 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001222 set->size += LYXP_SET_SIZE_STEP;
1223 }
1224
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001225 index = set->used;
1226 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1227 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001228 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001229 ++set->used;
1230 }
1231
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001232 if (index_p) {
1233 *index_p = index;
1234 }
1235
1236 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001237}
1238
1239/**
1240 * @brief Replace a node in a set with another. Context position aware.
1241 *
1242 * @param[in] set Set to use.
1243 * @param[in] node Node to insert to @p set.
1244 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1245 * @param[in] node_type Node type of @p node.
1246 * @param[in] idx Index in @p set of the node to replace.
1247 */
1248static void
1249set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1250{
1251 assert(set && (idx < set->used));
1252
Michal Vasko2caefc12019-11-14 16:07:56 +01001253 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1254 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1255 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001256 set->val.nodes[idx].node = (struct lyd_node *)node;
1257 set->val.nodes[idx].type = node_type;
1258 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001259 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1260 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1261 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001262}
1263
1264/**
1265 * @brief Set all nodes with ctx 1 to a new unique context value.
1266 *
1267 * @param[in] set Set to modify.
1268 * @return New context value.
1269 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001270static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001271set_scnode_new_in_ctx(struct lyxp_set *set)
1272{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001273 uint32_t i;
1274 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001275
1276 assert(set->type == LYXP_SET_SCNODE_SET);
1277
Radek Krejcif13b87b2020-12-01 22:02:17 +01001278 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001279retry:
1280 for (i = 0; i < set->used; ++i) {
1281 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1282 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1283 goto retry;
1284 }
1285 }
1286 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001287 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001288 set->val.scnodes[i].in_ctx = ret_ctx;
1289 }
1290 }
1291
1292 return ret_ctx;
1293}
1294
1295/**
1296 * @brief Get unique @p node position in the data.
1297 *
1298 * @param[in] node Node to find.
1299 * @param[in] node_type Node type of @p node.
1300 * @param[in] root Root node.
1301 * @param[in] root_type Type of the XPath @p root node.
1302 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1303 * be used to increase efficiency and start the DFS from this node.
1304 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1305 * @return Node position.
1306 */
1307static uint32_t
1308get_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 +02001309 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001310{
Michal Vasko56daf732020-08-10 10:57:18 +02001311 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001312 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001313 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001314
1315 assert(prev && prev_pos && !root->prev->next);
1316
1317 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1318 return 0;
1319 }
1320
1321 if (*prev) {
1322 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001323 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001324 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001325 goto dfs_search;
1326 }
1327
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001328 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001329 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001330dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001331 if (*prev && !elem) {
1332 /* resume previous DFS */
1333 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1334 LYD_TREE_DFS_continue = 0;
1335 }
1336
Michal Vasko03ff5a72019-09-11 13:49:33 +02001337 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001338 /* skip */
1339 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001340 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001341 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001342 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001343 break;
1344 }
Michal Vasko56daf732020-08-10 10:57:18 +02001345 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001346 }
Michal Vasko56daf732020-08-10 10:57:18 +02001347
1348 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001349 }
1350
1351 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001352 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001353 break;
1354 }
1355 }
1356
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001357 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001358 if (!(*prev)) {
1359 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001360 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001361 return 0;
1362 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001363 /* start the search again from the beginning */
1364 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001365
Michal Vasko56daf732020-08-10 10:57:18 +02001366 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001367 pos = 1;
1368 goto dfs_search;
1369 }
1370 }
1371
1372 /* remember the last found node for next time */
1373 *prev = node;
1374 *prev_pos = pos;
1375
1376 return pos;
1377}
1378
1379/**
1380 * @brief Assign (fill) missing node positions.
1381 *
1382 * @param[in] set Set to fill positions in.
1383 * @param[in] root Context root node.
1384 * @param[in] root_type Context root type.
1385 * @return LY_ERR
1386 */
1387static LY_ERR
1388set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1389{
1390 const struct lyd_node *prev = NULL, *tmp_node;
1391 uint32_t i, tmp_pos = 0;
1392
1393 for (i = 0; i < set->used; ++i) {
1394 if (!set->val.nodes[i].pos) {
1395 tmp_node = NULL;
1396 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001397 case LYXP_NODE_META:
1398 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001399 if (!tmp_node) {
1400 LOGINT_RET(root->schema->module->ctx);
1401 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001402 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001403 case LYXP_NODE_ELEM:
1404 case LYXP_NODE_TEXT:
1405 if (!tmp_node) {
1406 tmp_node = set->val.nodes[i].node;
1407 }
1408 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1409 break;
1410 default:
1411 /* all roots have position 0 */
1412 break;
1413 }
1414 }
1415 }
1416
1417 return LY_SUCCESS;
1418}
1419
1420/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001421 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001422 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001423 * @param[in] meta Metadata to use.
1424 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001425 */
1426static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001427get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001428{
1429 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001430 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001431
Michal Vasko9f96a052020-03-10 09:41:45 +01001432 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001433 ++pos;
1434 }
1435
Michal Vasko9f96a052020-03-10 09:41:45 +01001436 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001437 return pos;
1438}
1439
1440/**
1441 * @brief Compare 2 nodes in respect to XPath document order.
1442 *
1443 * @param[in] item1 1st node.
1444 * @param[in] item2 2nd node.
1445 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1446 */
1447static int
1448set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1449{
Michal Vasko9f96a052020-03-10 09:41:45 +01001450 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001451
1452 if (item1->pos < item2->pos) {
1453 return -1;
1454 }
1455
1456 if (item1->pos > item2->pos) {
1457 return 1;
1458 }
1459
1460 /* node positions are equal, the fun case */
1461
1462 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1463 /* special case since text nodes are actually saved as their parents */
1464 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1465 if (item1->type == LYXP_NODE_ELEM) {
1466 assert(item2->type == LYXP_NODE_TEXT);
1467 return -1;
1468 } else {
1469 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1470 return 1;
1471 }
1472 }
1473
Michal Vasko9f96a052020-03-10 09:41:45 +01001474 /* we need meta positions now */
1475 if (item1->type == LYXP_NODE_META) {
1476 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001477 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001478 if (item2->type == LYXP_NODE_META) {
1479 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001480 }
1481
Michal Vasko9f96a052020-03-10 09:41:45 +01001482 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001483 /* check for duplicates */
1484 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001485 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001486 return 0;
1487 }
1488
Michal Vasko9f96a052020-03-10 09:41:45 +01001489 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001490 /* elem is always first, 2nd node is after it */
1491 if (item1->type == LYXP_NODE_ELEM) {
1492 assert(item2->type != LYXP_NODE_ELEM);
1493 return -1;
1494 }
1495
Michal Vasko9f96a052020-03-10 09:41:45 +01001496 /* 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 +02001497 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001498 if (((item1->type == LYXP_NODE_TEXT) &&
1499 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1500 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1501 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1502 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001503 return 1;
1504 }
1505
Michal Vasko9f96a052020-03-10 09:41:45 +01001506 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001507 /* 2nd is after 1st */
1508 return -1;
1509}
1510
1511/**
1512 * @brief Set cast for comparisons.
1513 *
1514 * @param[in] trg Target set to cast source into.
1515 * @param[in] src Source set.
1516 * @param[in] type Target set type.
1517 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001518 * @return LY_ERR
1519 */
1520static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001521set_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 +02001522{
1523 assert(src->type == LYXP_SET_NODE_SET);
1524
1525 set_init(trg, src);
1526
1527 /* insert node into target set */
1528 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1529
1530 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001531 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001532}
1533
Michal Vasko4c7763f2020-07-27 17:40:37 +02001534/**
1535 * @brief Set content canonization for comparisons.
1536 *
1537 * @param[in] trg Target set to put the canononized source into.
1538 * @param[in] src Source set.
1539 * @param[in] xp_node Source XPath node/meta to use for canonization.
1540 * @return LY_ERR
1541 */
1542static LY_ERR
1543set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1544{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001545 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001546 struct lyd_value val;
1547 struct ly_err_item *err = NULL;
1548 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001549 LY_ERR rc;
1550
1551 /* is there anything to canonize even? */
1552 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1553 /* do we have a type to use for canonization? */
1554 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1555 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1556 } else if (xp_node->type == LYXP_NODE_META) {
1557 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1558 }
1559 }
1560 if (!type) {
1561 goto fill;
1562 }
1563
1564 if (src->type == LYXP_SET_NUMBER) {
1565 /* canonize number */
1566 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1567 LOGMEM(src->ctx);
1568 return LY_EMEM;
1569 }
1570 } else {
1571 /* canonize string */
1572 str = strdup(src->val.str);
1573 }
1574
1575 /* ignore errors, the value may not satisfy schema constraints */
Radek Krejci0b013302021-03-29 15:22:32 +02001576 rc = type->plugin->store(src->ctx, type, str, strlen(str), LYPLG_TYPE_STORE_DYNAMIC, src->format, src->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001577 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001578 ly_err_free(err);
1579 if (rc) {
1580 /* invalid value */
Radek IÅ¡a48460222021-03-02 15:18:32 +01001581 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001582 goto fill;
1583 }
1584
Michal Vasko4c7763f2020-07-27 17:40:37 +02001585 /* use the canonized value */
1586 set_init(trg, src);
1587 trg->type = src->type;
1588 if (src->type == LYXP_SET_NUMBER) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001589 trg->val.num = strtold(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001590 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1591 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001592 trg->val.str = strdup(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko4c7763f2020-07-27 17:40:37 +02001593 }
1594 type->plugin->free(src->ctx, &val);
1595 return LY_SUCCESS;
1596
1597fill:
1598 /* no canonization needed/possible */
1599 set_fill_set(trg, src);
1600 return LY_SUCCESS;
1601}
1602
Michal Vasko03ff5a72019-09-11 13:49:33 +02001603#ifndef NDEBUG
1604
1605/**
1606 * @brief Bubble sort @p set into XPath document order.
1607 * Context position aware. Unused in the 'Release' build target.
1608 *
1609 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001610 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1611 */
1612static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001613set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001614{
1615 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001616 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001617 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001618 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001619 struct lyxp_set_node item;
1620 struct lyxp_set_hash_node hnode;
1621 uint64_t hash;
1622
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001623 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001624 return 0;
1625 }
1626
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001627 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001628 for (root = set->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001629 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001630
1631 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001632 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001633 return -1;
1634 }
1635
1636 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1637 print_set_debug(set);
1638
1639 for (i = 0; i < set->used; ++i) {
1640 inverted = 0;
1641 change = 0;
1642
1643 for (j = 1; j < set->used - i; ++j) {
1644 /* compare node positions */
1645 if (inverted) {
1646 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1647 } else {
1648 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1649 }
1650
1651 /* swap if needed */
1652 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1653 change = 1;
1654
1655 item = set->val.nodes[j - 1];
1656 set->val.nodes[j - 1] = set->val.nodes[j];
1657 set->val.nodes[j] = item;
1658 } else {
1659 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1660 inverted = !inverted;
1661 }
1662 }
1663
1664 ++ret;
1665
1666 if (!change) {
1667 break;
1668 }
1669 }
1670
1671 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1672 print_set_debug(set);
1673
1674 /* check node hashes */
1675 if (set->used >= LYD_HT_MIN_ITEMS) {
1676 assert(set->ht);
1677 for (i = 0; i < set->used; ++i) {
1678 hnode.node = set->val.nodes[i].node;
1679 hnode.type = set->val.nodes[i].type;
1680
1681 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1682 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1683 hash = dict_hash_multi(hash, NULL, 0);
1684
1685 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1686 }
1687 }
1688
1689 return ret - 1;
1690}
1691
1692/**
1693 * @brief Remove duplicate entries in a sorted node set.
1694 *
1695 * @param[in] set Sorted set to check.
1696 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1697 */
1698static LY_ERR
1699set_sorted_dup_node_clean(struct lyxp_set *set)
1700{
1701 uint32_t i = 0;
1702 LY_ERR ret = LY_SUCCESS;
1703
1704 if (set->used > 1) {
1705 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001706 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1707 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001708 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001709 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001710 }
Michal Vasko57eab132019-09-24 11:46:26 +02001711 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001712 }
1713 }
1714
Michal Vasko2caefc12019-11-14 16:07:56 +01001715 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001716 return ret;
1717}
1718
1719#endif
1720
1721/**
1722 * @brief Merge 2 sorted sets into one.
1723 *
1724 * @param[in,out] trg Set to merge into. Duplicates are removed.
1725 * @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 +02001726 * @return LY_ERR
1727 */
1728static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001729set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001730{
1731 uint32_t i, j, k, count, dup_count;
1732 int cmp;
1733 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001734
Michal Vaskod3678892020-05-21 10:06:58 +02001735 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001736 return LY_EINVAL;
1737 }
1738
Michal Vaskod3678892020-05-21 10:06:58 +02001739 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001740 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001741 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001742 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001743 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001744 return LY_SUCCESS;
1745 }
1746
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001747 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001748 for (root = trg->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001749 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001750
1751 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001752 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001753 return LY_EINT;
1754 }
1755
1756#ifndef NDEBUG
1757 LOGDBG(LY_LDGXPATH, "MERGE target");
1758 print_set_debug(trg);
1759 LOGDBG(LY_LDGXPATH, "MERGE source");
1760 print_set_debug(src);
1761#endif
1762
1763 /* make memory for the merge (duplicates are not detected yet, so space
1764 * will likely be wasted on them, too bad) */
1765 if (trg->size - trg->used < src->used) {
1766 trg->size = trg->used + src->used;
1767
1768 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1769 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1770 }
1771
1772 i = 0;
1773 j = 0;
1774 count = 0;
1775 dup_count = 0;
1776 do {
1777 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1778 if (!cmp) {
1779 if (!count) {
1780 /* duplicate, just skip it */
1781 ++i;
1782 ++j;
1783 } else {
1784 /* we are copying something already, so let's copy the duplicate too,
1785 * we are hoping that afterwards there are some more nodes to
1786 * copy and this way we can copy them all together */
1787 ++count;
1788 ++dup_count;
1789 ++i;
1790 ++j;
1791 }
1792 } else if (cmp < 0) {
1793 /* inserting src node into trg, just remember it for now */
1794 ++count;
1795 ++i;
1796
1797 /* insert the hash now */
1798 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1799 } else if (count) {
1800copy_nodes:
1801 /* time to actually copy the nodes, we have found the largest block of nodes */
1802 memmove(&trg->val.nodes[j + (count - dup_count)],
1803 &trg->val.nodes[j],
1804 (trg->used - j) * sizeof *trg->val.nodes);
1805 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1806
1807 trg->used += count - dup_count;
1808 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1809 j += count - dup_count;
1810
1811 count = 0;
1812 dup_count = 0;
1813 } else {
1814 ++j;
1815 }
1816 } while ((i < src->used) && (j < trg->used));
1817
1818 if ((i < src->used) || count) {
1819 /* insert all the hashes first */
1820 for (k = i; k < src->used; ++k) {
1821 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1822 }
1823
1824 /* loop ended, but we need to copy something at trg end */
1825 count += src->used - i;
1826 i = src->used;
1827 goto copy_nodes;
1828 }
1829
1830 /* we are inserting hashes before the actual node insert, which causes
1831 * situations when there were initially not enough items for a hash table,
1832 * but even after some were inserted, hash table was not created (during
1833 * insertion the number of items is not updated yet) */
1834 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1835 set_insert_node_hash(trg, NULL, 0);
1836 }
1837
1838#ifndef NDEBUG
1839 LOGDBG(LY_LDGXPATH, "MERGE result");
1840 print_set_debug(trg);
1841#endif
1842
Michal Vaskod3678892020-05-21 10:06:58 +02001843 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001844 return LY_SUCCESS;
1845}
1846
1847/*
1848 * (re)parse functions
1849 *
1850 * Parse functions parse the expression into
1851 * tokens (syntactic analysis).
1852 *
1853 * Reparse functions perform semantic analysis
1854 * (do not save the result, just a check) of
1855 * the expression and fill repeat indices.
1856 */
1857
Michal Vasko14676352020-05-29 11:35:55 +02001858LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001859lyxp_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 +02001860{
Michal Vasko004d3152020-06-11 19:59:22 +02001861 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001862 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001863 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001864 }
Michal Vasko14676352020-05-29 11:35:55 +02001865 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001866 }
1867
Michal Vasko004d3152020-06-11 19:59:22 +02001868 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001869 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001870 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001871 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001872 }
Michal Vasko14676352020-05-29 11:35:55 +02001873 return LY_ENOT;
1874 }
1875
1876 return LY_SUCCESS;
1877}
1878
Michal Vasko004d3152020-06-11 19:59:22 +02001879LY_ERR
1880lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1881{
1882 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1883
1884 /* skip the token */
1885 ++(*tok_idx);
1886
1887 return LY_SUCCESS;
1888}
1889
Michal Vasko14676352020-05-29 11:35:55 +02001890/* just like lyxp_check_token() but tests for 2 tokens */
1891static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001892exp_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 +02001893 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001894{
Michal Vasko004d3152020-06-11 19:59:22 +02001895 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001896 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001897 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001898 }
1899 return LY_EINCOMPLETE;
1900 }
1901
Michal Vasko004d3152020-06-11 19:59:22 +02001902 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001903 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001904 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001905 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001906 }
1907 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001908 }
1909
1910 return LY_SUCCESS;
1911}
1912
1913/**
1914 * @brief Stack operation push on the repeat array.
1915 *
1916 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001917 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001918 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1919 */
1920static void
Michal Vasko004d3152020-06-11 19:59:22 +02001921exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001922{
1923 uint16_t i;
1924
Michal Vasko004d3152020-06-11 19:59:22 +02001925 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001926 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001927 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1928 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1929 exp->repeat[tok_idx][i] = repeat_op_idx;
1930 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001931 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001932 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1933 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1934 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001935 }
1936}
1937
1938/**
1939 * @brief Reparse Predicate. Logs directly on error.
1940 *
1941 * [7] Predicate ::= '[' Expr ']'
1942 *
1943 * @param[in] ctx Context for logging.
1944 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001945 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02001946 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001947 * @return LY_ERR
1948 */
1949static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02001950reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001951{
1952 LY_ERR rc;
1953
Michal Vasko004d3152020-06-11 19:59:22 +02001954 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001955 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001956 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001957
aPiecekbf968d92021-05-27 14:35:05 +02001958 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001959 LY_CHECK_RET(rc);
1960
Michal Vasko004d3152020-06-11 19:59:22 +02001961 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001962 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001963 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001964
1965 return LY_SUCCESS;
1966}
1967
1968/**
1969 * @brief Reparse RelativeLocationPath. Logs directly on error.
1970 *
1971 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1972 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1973 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1974 *
1975 * @param[in] ctx Context for logging.
1976 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001977 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02001978 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001979 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1980 */
1981static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02001982reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001983{
1984 LY_ERR rc;
1985
Michal Vasko004d3152020-06-11 19:59:22 +02001986 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001987 LY_CHECK_RET(rc);
1988
1989 goto step;
1990 do {
1991 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001992 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001993
Michal Vasko004d3152020-06-11 19:59:22 +02001994 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001995 LY_CHECK_RET(rc);
1996step:
1997 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001998 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001999 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002000 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002001 break;
2002
2003 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002004 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002005 break;
2006
2007 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002008 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002009
Michal Vasko004d3152020-06-11 19:59:22 +02002010 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002011 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002012 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002013 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 +02002014 return LY_EVALID;
2015 }
Radek Krejci0f969882020-08-21 16:56:47 +02002016 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002017 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002018 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002019 goto reparse_predicate;
2020 break;
2021
2022 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002023 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002024
2025 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002026 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002027 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002028 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002029
2030 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002031 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002032 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002033 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002034
2035reparse_predicate:
2036 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002037 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002038 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002039 LY_CHECK_RET(rc);
2040 }
2041 break;
2042 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002043 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 +02002044 return LY_EVALID;
2045 }
Michal Vasko004d3152020-06-11 19:59:22 +02002046 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002047
2048 return LY_SUCCESS;
2049}
2050
2051/**
2052 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2053 *
2054 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2055 *
2056 * @param[in] ctx Context for logging.
2057 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002058 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002059 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002060 * @return LY_ERR
2061 */
2062static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002063reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002064{
2065 LY_ERR rc;
2066
Michal Vasko004d3152020-06-11 19:59:22 +02002067 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 +02002068
2069 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002070 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002071 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002072 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002073
Michal Vasko004d3152020-06-11 19:59:22 +02002074 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002075 return LY_SUCCESS;
2076 }
Michal Vasko004d3152020-06-11 19:59:22 +02002077 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002078 case LYXP_TOKEN_DOT:
2079 case LYXP_TOKEN_DDOT:
2080 case LYXP_TOKEN_AT:
2081 case LYXP_TOKEN_NAMETEST:
2082 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002083 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002084 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002085 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002086 default:
2087 break;
2088 }
2089
Michal Vasko03ff5a72019-09-11 13:49:33 +02002090 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002091 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002092 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002093
aPiecekbf968d92021-05-27 14:35:05 +02002094 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002095 LY_CHECK_RET(rc);
2096 }
2097
2098 return LY_SUCCESS;
2099}
2100
2101/**
2102 * @brief Reparse FunctionCall. Logs directly on error.
2103 *
2104 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2105 *
2106 * @param[in] ctx Context for logging.
2107 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002108 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002109 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002110 * @return LY_ERR
2111 */
2112static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002113reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002114{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002115 int8_t min_arg_count = -1;
2116 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002117 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002118 LY_ERR rc;
2119
Michal Vasko004d3152020-06-11 19:59:22 +02002120 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002121 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002122 func_tok_idx = *tok_idx;
2123 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002124 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002125 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002126 min_arg_count = 1;
2127 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002128 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002129 min_arg_count = 1;
2130 max_arg_count = 1;
2131 }
2132 break;
2133 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002134 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002135 min_arg_count = 1;
2136 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002137 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002138 min_arg_count = 0;
2139 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002140 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002141 min_arg_count = 0;
2142 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002143 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002144 min_arg_count = 0;
2145 max_arg_count = 0;
2146 }
2147 break;
2148 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002149 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002150 min_arg_count = 1;
2151 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002152 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002153 min_arg_count = 0;
2154 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002155 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002156 min_arg_count = 1;
2157 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002158 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002159 min_arg_count = 1;
2160 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002161 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002162 min_arg_count = 1;
2163 max_arg_count = 1;
2164 }
2165 break;
2166 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002167 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002168 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002169 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002170 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002171 min_arg_count = 0;
2172 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002173 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002174 min_arg_count = 0;
2175 max_arg_count = 1;
2176 }
2177 break;
2178 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002179 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002180 min_arg_count = 1;
2181 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002182 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002183 min_arg_count = 1;
2184 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002185 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002186 min_arg_count = 0;
2187 max_arg_count = 0;
2188 }
2189 break;
2190 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002191 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002192 min_arg_count = 2;
2193 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002194 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002195 min_arg_count = 0;
2196 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002197 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002198 min_arg_count = 2;
2199 max_arg_count = 2;
2200 }
2201 break;
2202 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002203 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002204 min_arg_count = 2;
2205 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002206 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002207 min_arg_count = 3;
2208 max_arg_count = 3;
2209 }
2210 break;
2211 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002212 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002213 min_arg_count = 0;
2214 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002215 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002216 min_arg_count = 1;
2217 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002218 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002219 min_arg_count = 2;
2220 max_arg_count = 2;
2221 }
2222 break;
2223 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002224 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002225 min_arg_count = 2;
2226 max_arg_count = 2;
2227 }
2228 break;
2229 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002230 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002231 min_arg_count = 2;
2232 max_arg_count = 2;
2233 }
2234 break;
2235 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002236 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002237 min_arg_count = 0;
2238 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002239 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002240 min_arg_count = 0;
2241 max_arg_count = 1;
2242 }
2243 break;
2244 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002245 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002246 min_arg_count = 0;
2247 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002248 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002249 min_arg_count = 2;
2250 max_arg_count = 2;
2251 }
2252 break;
2253 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002254 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002255 min_arg_count = 2;
2256 max_arg_count = 2;
2257 }
2258 break;
2259 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002260 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002261 min_arg_count = 2;
2262 max_arg_count = 2;
2263 }
2264 break;
2265 }
2266 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002267 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 +02002268 return LY_EINVAL;
2269 }
Michal Vasko004d3152020-06-11 19:59:22 +02002270 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002271
2272 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002273 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002274 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002275 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002276
2277 /* ( Expr ( ',' Expr )* )? */
2278 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002279 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002280 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002281 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002282 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002283 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002284 LY_CHECK_RET(rc);
2285 }
Michal Vasko004d3152020-06-11 19:59:22 +02002286 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2287 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002288
2289 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002290 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002291 LY_CHECK_RET(rc);
2292 }
2293
2294 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002295 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002296 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002297 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002298
Radek Krejci857189e2020-09-01 13:26:36 +02002299 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002300 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 +02002301 return LY_EVALID;
2302 }
2303
2304 return LY_SUCCESS;
2305}
2306
2307/**
2308 * @brief Reparse PathExpr. Logs directly on error.
2309 *
2310 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2311 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2312 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2313 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2314 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2315 *
2316 * @param[in] ctx Context for logging.
2317 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002318 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002319 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002320 * @return LY_ERR
2321 */
2322static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002323reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002324{
2325 LY_ERR rc;
2326
Michal Vasko004d3152020-06-11 19:59:22 +02002327 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002328 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002329 }
2330
Michal Vasko004d3152020-06-11 19:59:22 +02002331 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002332 case LYXP_TOKEN_PAR1:
2333 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002334 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002335
aPiecekbf968d92021-05-27 14:35:05 +02002336 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002337 LY_CHECK_RET(rc);
2338
Michal Vasko004d3152020-06-11 19:59:22 +02002339 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002340 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002341 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002342 goto predicate;
2343 break;
2344 case LYXP_TOKEN_DOT:
2345 case LYXP_TOKEN_DDOT:
2346 case LYXP_TOKEN_AT:
2347 case LYXP_TOKEN_NAMETEST:
2348 case LYXP_TOKEN_NODETYPE:
2349 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002350 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002351 LY_CHECK_RET(rc);
2352 break;
2353 case LYXP_TOKEN_FUNCNAME:
2354 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002355 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002356 LY_CHECK_RET(rc);
2357 goto predicate;
2358 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002359 case LYXP_TOKEN_OPER_PATH:
2360 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002361 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002362 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002363 LY_CHECK_RET(rc);
2364 break;
2365 case LYXP_TOKEN_LITERAL:
2366 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002367 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002368 goto predicate;
2369 break;
2370 case LYXP_TOKEN_NUMBER:
2371 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002372 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002373 goto predicate;
2374 break;
2375 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002376 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 +02002377 return LY_EVALID;
2378 }
2379
2380 return LY_SUCCESS;
2381
2382predicate:
2383 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002384 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002385 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002386 LY_CHECK_RET(rc);
2387 }
2388
2389 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002390 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002391
2392 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002393 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002394
aPiecekbf968d92021-05-27 14:35:05 +02002395 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002396 LY_CHECK_RET(rc);
2397 }
2398
2399 return LY_SUCCESS;
2400}
2401
2402/**
2403 * @brief Reparse UnaryExpr. Logs directly on error.
2404 *
2405 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2406 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2407 *
2408 * @param[in] ctx Context for logging.
2409 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002410 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002411 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002412 * @return LY_ERR
2413 */
2414static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002415reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002416{
2417 uint16_t prev_exp;
2418 LY_ERR rc;
2419
2420 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002421 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002422 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2423 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002424 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002425 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002426 }
2427
2428 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002429 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002430 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002431 LY_CHECK_RET(rc);
2432
2433 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002434 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002435 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002436 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002437
aPiecekbf968d92021-05-27 14:35:05 +02002438 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002439 LY_CHECK_RET(rc);
2440 }
2441
2442 return LY_SUCCESS;
2443}
2444
2445/**
2446 * @brief Reparse AdditiveExpr. Logs directly on error.
2447 *
2448 * [15] AdditiveExpr ::= MultiplicativeExpr
2449 * | AdditiveExpr '+' MultiplicativeExpr
2450 * | AdditiveExpr '-' MultiplicativeExpr
2451 * [16] MultiplicativeExpr ::= UnaryExpr
2452 * | MultiplicativeExpr '*' UnaryExpr
2453 * | MultiplicativeExpr 'div' UnaryExpr
2454 * | MultiplicativeExpr 'mod' UnaryExpr
2455 *
2456 * @param[in] ctx Context for logging.
2457 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002458 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002459 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002460 * @return LY_ERR
2461 */
2462static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002463reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002464{
2465 uint16_t prev_add_exp, prev_mul_exp;
2466 LY_ERR rc;
2467
Michal Vasko004d3152020-06-11 19:59:22 +02002468 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002469 goto reparse_multiplicative_expr;
2470
2471 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002472 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2473 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002474 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002475 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002476
2477reparse_multiplicative_expr:
2478 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002479 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002480 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002481 LY_CHECK_RET(rc);
2482
2483 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002484 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2485 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002486 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002487 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002488
aPiecekbf968d92021-05-27 14:35:05 +02002489 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002490 LY_CHECK_RET(rc);
2491 }
2492 }
2493
2494 return LY_SUCCESS;
2495}
2496
2497/**
2498 * @brief Reparse EqualityExpr. Logs directly on error.
2499 *
2500 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2501 * | EqualityExpr '!=' RelationalExpr
2502 * [14] RelationalExpr ::= AdditiveExpr
2503 * | RelationalExpr '<' AdditiveExpr
2504 * | RelationalExpr '>' AdditiveExpr
2505 * | RelationalExpr '<=' AdditiveExpr
2506 * | RelationalExpr '>=' AdditiveExpr
2507 *
2508 * @param[in] ctx Context for logging.
2509 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002510 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002511 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002512 * @return LY_ERR
2513 */
2514static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002515reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002516{
2517 uint16_t prev_eq_exp, prev_rel_exp;
2518 LY_ERR rc;
2519
Michal Vasko004d3152020-06-11 19:59:22 +02002520 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002521 goto reparse_additive_expr;
2522
2523 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002524 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002525 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002526 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002527
2528reparse_additive_expr:
2529 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002530 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002531 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002532 LY_CHECK_RET(rc);
2533
2534 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002535 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002536 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002537 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002538
aPiecekbf968d92021-05-27 14:35:05 +02002539 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002540 LY_CHECK_RET(rc);
2541 }
2542 }
2543
2544 return LY_SUCCESS;
2545}
2546
2547/**
2548 * @brief Reparse OrExpr. Logs directly on error.
2549 *
2550 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2551 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2552 *
2553 * @param[in] ctx Context for logging.
2554 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002555 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002556 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002557 * @return LY_ERR
2558 */
2559static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002560reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002561{
2562 uint16_t prev_or_exp, prev_and_exp;
2563 LY_ERR rc;
2564
aPiecekbf968d92021-05-27 14:35:05 +02002565 ++depth;
2566 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2567
Michal Vasko004d3152020-06-11 19:59:22 +02002568 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002569 goto reparse_equality_expr;
2570
2571 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002572 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 +02002573 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002574 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002575
2576reparse_equality_expr:
2577 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002578 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002579 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002580 LY_CHECK_RET(rc);
2581
2582 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002583 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 +02002584 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002585 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002586
aPiecekbf968d92021-05-27 14:35:05 +02002587 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002588 LY_CHECK_RET(rc);
2589 }
2590 }
2591
2592 return LY_SUCCESS;
2593}
Radek Krejcib1646a92018-11-02 16:08:26 +01002594
2595/**
2596 * @brief Parse NCName.
2597 *
2598 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002599 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002600 */
Radek Krejcid4270262019-01-07 15:07:25 +01002601static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002602parse_ncname(const char *ncname)
2603{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002604 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002605 size_t size;
2606 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002607
2608 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2609 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2610 return len;
2611 }
2612
2613 do {
2614 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002615 if (!*ncname) {
2616 break;
2617 }
Radek Krejcid4270262019-01-07 15:07:25 +01002618 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002619 } while (is_xmlqnamechar(uc) && (uc != ':'));
2620
2621 return len;
2622}
2623
2624/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002625 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002626 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002627 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002628 * @param[in] exp Expression to use.
2629 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002630 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002631 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002632 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002633 */
2634static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002635exp_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 +01002636{
2637 uint32_t prev;
2638
2639 if (exp->used == exp->size) {
2640 prev = exp->size;
2641 exp->size += LYXP_EXPR_SIZE_STEP;
2642 if (prev > exp->size) {
2643 LOGINT(ctx);
2644 return LY_EINT;
2645 }
2646
2647 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2648 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2649 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2650 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2651 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2652 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2653 }
2654
2655 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002656 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002657 exp->tok_len[exp->used] = tok_len;
2658 ++exp->used;
2659 return LY_SUCCESS;
2660}
2661
2662void
Michal Vasko14676352020-05-29 11:35:55 +02002663lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002664{
2665 uint16_t i;
2666
2667 if (!expr) {
2668 return;
2669 }
2670
2671 lydict_remove(ctx, expr->expr);
2672 free(expr->tokens);
2673 free(expr->tok_pos);
2674 free(expr->tok_len);
2675 if (expr->repeat) {
2676 for (i = 0; i < expr->used; ++i) {
2677 free(expr->repeat[i]);
2678 }
2679 }
2680 free(expr->repeat);
2681 free(expr);
2682}
2683
Radek Krejcif03a9e22020-09-18 20:09:31 +02002684LY_ERR
2685lyxp_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 +01002686{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002687 LY_ERR ret = LY_SUCCESS;
2688 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002689 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002690 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002691 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002692 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002693
Radek Krejcif03a9e22020-09-18 20:09:31 +02002694 assert(expr_p);
2695
2696 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002697 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002698 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002699 }
2700
2701 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002702 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002703 }
2704 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002705 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002706 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002707 }
2708
2709 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002710 expr = calloc(1, sizeof *expr);
2711 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2712 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2713 expr->used = 0;
2714 expr->size = LYXP_EXPR_SIZE_START;
2715 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2716 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002717
Radek Krejcif03a9e22020-09-18 20:09:31 +02002718 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2719 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002720
Radek Krejcif03a9e22020-09-18 20:09:31 +02002721 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2722 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002723
Michal Vasko004d3152020-06-11 19:59:22 +02002724 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002725 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002726
Radek Krejcif03a9e22020-09-18 20:09:31 +02002727 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002728 ++parsed;
2729 }
2730
2731 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002732 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002733
2734 /* '(' */
2735 tok_len = 1;
2736 tok_type = LYXP_TOKEN_PAR1;
2737
Radek Krejcif03a9e22020-09-18 20:09:31 +02002738 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002739 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002740 if (((expr->tok_len[expr->used - 1] == 4) &&
2741 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2742 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2743 ((expr->tok_len[expr->used - 1] == 7) &&
2744 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002745 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002746 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002747 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002748 }
2749 prev_function_check = 0;
2750 }
2751
Radek Krejcif03a9e22020-09-18 20:09:31 +02002752 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002753
2754 /* ')' */
2755 tok_len = 1;
2756 tok_type = LYXP_TOKEN_PAR2;
2757
Radek Krejcif03a9e22020-09-18 20:09:31 +02002758 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002759
2760 /* '[' */
2761 tok_len = 1;
2762 tok_type = LYXP_TOKEN_BRACK1;
2763
Radek Krejcif03a9e22020-09-18 20:09:31 +02002764 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002765
2766 /* ']' */
2767 tok_len = 1;
2768 tok_type = LYXP_TOKEN_BRACK2;
2769
Radek Krejcif03a9e22020-09-18 20:09:31 +02002770 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002771
2772 /* '..' */
2773 tok_len = 2;
2774 tok_type = LYXP_TOKEN_DDOT;
2775
Radek Krejcif03a9e22020-09-18 20:09:31 +02002776 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002777
2778 /* '.' */
2779 tok_len = 1;
2780 tok_type = LYXP_TOKEN_DOT;
2781
Radek Krejcif03a9e22020-09-18 20:09:31 +02002782 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002783
2784 /* '@' */
2785 tok_len = 1;
2786 tok_type = LYXP_TOKEN_AT;
2787
Radek Krejcif03a9e22020-09-18 20:09:31 +02002788 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002789
2790 /* ',' */
2791 tok_len = 1;
2792 tok_type = LYXP_TOKEN_COMMA;
2793
Radek Krejcif03a9e22020-09-18 20:09:31 +02002794 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002795
2796 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002797 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2798 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002799 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002800 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002801 ++tok_len;
2802 tok_type = LYXP_TOKEN_LITERAL;
2803
Radek Krejcif03a9e22020-09-18 20:09:31 +02002804 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002805
2806 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002807 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2808 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002809 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002810 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002811 ++tok_len;
2812 tok_type = LYXP_TOKEN_LITERAL;
2813
Radek Krejcif03a9e22020-09-18 20:09:31 +02002814 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002815
2816 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002817 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2818 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002819 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002820 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002821 }
2822 tok_type = LYXP_TOKEN_NUMBER;
2823
Radek Krejcif03a9e22020-09-18 20:09:31 +02002824 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002825
2826 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002827 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002828 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002829 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002830 } else {
2831 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002832 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002833 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002834
Radek Krejcif03a9e22020-09-18 20:09:31 +02002835 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002836
Michal Vasko3e48bf32020-06-01 08:39:07 +02002837 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002838 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002839 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2840
Radek Krejcif03a9e22020-09-18 20:09:31 +02002841 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002842
2843 /* Operator '<=', '>=' */
2844 tok_len = 2;
2845 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002846
Radek Krejcif03a9e22020-09-18 20:09:31 +02002847 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002848
2849 /* Operator '|' */
2850 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002851 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002852
Radek Krejcif03a9e22020-09-18 20:09:31 +02002853 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002854
2855 /* Operator '+', '-' */
2856 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002857 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002858
Radek Krejcif03a9e22020-09-18 20:09:31 +02002859 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002860
Michal Vasko3e48bf32020-06-01 08:39:07 +02002861 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002862 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002863 tok_type = LYXP_TOKEN_OPER_EQUAL;
2864
Radek Krejcif03a9e22020-09-18 20:09:31 +02002865 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002866
2867 /* Operator '<', '>' */
2868 tok_len = 1;
2869 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002870
Michal Vasko69730152020-10-09 16:30:07 +02002871 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2872 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2873 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2874 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2875 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2876 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2877 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2878 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2879 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2880 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2881 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2882 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002883
2884 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002885 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002886 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002887 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002888
Radek Krejcif03a9e22020-09-18 20:09:31 +02002889 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002890 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002891 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002892
Radek Krejcif03a9e22020-09-18 20:09:31 +02002893 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002894 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002895 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002896
Radek Krejcif03a9e22020-09-18 20:09:31 +02002897 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002898 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002899 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002900
2901 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002902 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 +02002903 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 +02002904 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002905 goto error;
2906 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02002907 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002908 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002909 goto error;
2910 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002911 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002912
2913 /* NameTest '*' */
2914 tok_len = 1;
2915 tok_type = LYXP_TOKEN_NAMETEST;
2916
2917 } else {
2918
2919 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002920 long int ncname_len = parse_ncname(&expr_str[parsed]);
Michal Vasko774ce402021-04-14 15:35:06 +02002921 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2922 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002923 tok_len = ncname_len;
2924
Radek Krejcif03a9e22020-09-18 20:09:31 +02002925 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002926 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002927 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002928 ++tok_len;
2929 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002930 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vasko774ce402021-04-14 15:35:06 +02002931 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2932 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002933 tok_len += ncname_len;
2934 }
2935 /* remove old flag to prevent ambiguities */
2936 prev_function_check = 0;
2937 tok_type = LYXP_TOKEN_NAMETEST;
2938 } else {
2939 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2940 prev_function_check = 1;
2941 tok_type = LYXP_TOKEN_NAMETEST;
2942 }
2943 }
2944
2945 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002946 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002947 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002948 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002949 ++parsed;
2950 }
2951
Radek Krejcif03a9e22020-09-18 20:09:31 +02002952 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002953
Michal Vasko004d3152020-06-11 19:59:22 +02002954 if (reparse) {
2955 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002956 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2957 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002958
Michal Vasko004d3152020-06-11 19:59:22 +02002959 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02002960 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002961 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002962 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002963 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002964 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002965 goto error;
2966 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002967 }
2968
Radek Krejcif03a9e22020-09-18 20:09:31 +02002969 print_expr_struct_debug(expr);
2970 *expr_p = expr;
2971 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002972
2973error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002974 lyxp_expr_free(ctx, expr);
2975 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002976}
2977
Michal Vasko1734be92020-09-22 08:55:10 +02002978LY_ERR
2979lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02002980{
Michal Vasko1734be92020-09-22 08:55:10 +02002981 LY_ERR ret = LY_SUCCESS;
2982 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02002983 uint32_t i, j;
2984
Michal Vasko7f45cf22020-10-01 12:49:44 +02002985 if (!exp) {
2986 goto cleanup;
2987 }
2988
Michal Vasko004d3152020-06-11 19:59:22 +02002989 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02002990 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002991
2992 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
Michal Vasko1734be92020-09-22 08:55:10 +02002993 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002994 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2995
2996 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
Michal Vasko1734be92020-09-22 08:55:10 +02002997 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002998 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2999
3000 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
Michal Vasko1734be92020-09-22 08:55:10 +02003001 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003002 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
3003
3004 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02003005 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003006 for (i = 0; i < exp->used; ++i) {
3007 if (!exp->repeat[i]) {
3008 dup->repeat[i] = NULL;
3009 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02003010 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02003011 /* the ending 0 as well */
3012 ++j;
3013
Michal Vasko99c71642020-07-03 13:33:36 +02003014 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02003015 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003016 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3017 dup->repeat[i][j - 1] = 0;
3018 }
3019 }
3020
3021 dup->used = exp->used;
3022 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003023 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003024
Michal Vasko1734be92020-09-22 08:55:10 +02003025cleanup:
3026 if (ret) {
3027 lyxp_expr_free(ctx, dup);
3028 } else {
3029 *dup_p = dup;
3030 }
3031 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003032}
3033
Michal Vasko03ff5a72019-09-11 13:49:33 +02003034/*
3035 * warn functions
3036 *
3037 * Warn functions check specific reasonable conditions for schema XPath
3038 * and print a warning if they are not satisfied.
3039 */
3040
3041/**
3042 * @brief Get the last-added schema node that is currently in the context.
3043 *
3044 * @param[in] set Set to search in.
3045 * @return Last-added schema context node, NULL if no node is in context.
3046 */
3047static struct lysc_node *
3048warn_get_scnode_in_ctx(struct lyxp_set *set)
3049{
3050 uint32_t i;
3051
3052 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3053 return NULL;
3054 }
3055
3056 i = set->used;
3057 do {
3058 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003059 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003060 /* if there are more, simply return the first found (last added) */
3061 return set->val.scnodes[i].scnode;
3062 }
3063 } while (i);
3064
3065 return NULL;
3066}
3067
3068/**
3069 * @brief Test whether a type is numeric - integer type or decimal64.
3070 *
3071 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003072 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003073 */
Radek Krejci857189e2020-09-01 13:26:36 +02003074static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003075warn_is_numeric_type(struct lysc_type *type)
3076{
3077 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003078 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003079 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003080
3081 switch (type->basetype) {
3082 case LY_TYPE_DEC64:
3083 case LY_TYPE_INT8:
3084 case LY_TYPE_UINT8:
3085 case LY_TYPE_INT16:
3086 case LY_TYPE_UINT16:
3087 case LY_TYPE_INT32:
3088 case LY_TYPE_UINT32:
3089 case LY_TYPE_INT64:
3090 case LY_TYPE_UINT64:
3091 return 1;
3092 case LY_TYPE_UNION:
3093 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003094 LY_ARRAY_FOR(uni->types, u) {
3095 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003096 if (ret) {
3097 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003098 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003099 }
3100 }
3101 /* did not find any suitable type */
3102 return 0;
3103 case LY_TYPE_LEAFREF:
3104 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3105 default:
3106 return 0;
3107 }
3108}
3109
3110/**
3111 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3112 *
3113 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003114 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003115 */
Radek Krejci857189e2020-09-01 13:26:36 +02003116static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003117warn_is_string_type(struct lysc_type *type)
3118{
3119 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003120 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003121 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003122
3123 switch (type->basetype) {
3124 case LY_TYPE_BITS:
3125 case LY_TYPE_ENUM:
3126 case LY_TYPE_IDENT:
3127 case LY_TYPE_INST:
3128 case LY_TYPE_STRING:
3129 return 1;
3130 case LY_TYPE_UNION:
3131 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003132 LY_ARRAY_FOR(uni->types, u) {
3133 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003134 if (ret) {
3135 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003136 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003137 }
3138 }
3139 /* did not find any suitable type */
3140 return 0;
3141 case LY_TYPE_LEAFREF:
3142 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3143 default:
3144 return 0;
3145 }
3146}
3147
3148/**
3149 * @brief Test whether a type is one specific type.
3150 *
3151 * @param[in] type Type to test.
3152 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003153 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003154 */
Radek Krejci857189e2020-09-01 13:26:36 +02003155static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003156warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3157{
3158 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003159 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003160 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003161
3162 if (type->basetype == base) {
3163 return 1;
3164 } else if (type->basetype == LY_TYPE_UNION) {
3165 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003166 LY_ARRAY_FOR(uni->types, u) {
3167 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003168 if (ret) {
3169 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003170 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003171 }
3172 }
3173 /* did not find any suitable type */
3174 return 0;
3175 } else if (type->basetype == LY_TYPE_LEAFREF) {
3176 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3177 }
3178
3179 return 0;
3180}
3181
3182/**
3183 * @brief Get next type of a (union) type.
3184 *
3185 * @param[in] type Base type.
3186 * @param[in] prev_type Previously returned type.
3187 * @return Next type or NULL.
3188 */
3189static struct lysc_type *
3190warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3191{
3192 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003193 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003194 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003195
3196 switch (type->basetype) {
3197 case LY_TYPE_UNION:
3198 uni = (struct lysc_type_union *)type;
3199 if (!prev_type) {
3200 return uni->types[0];
3201 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003202 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003203 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003204 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003205 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003206 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003207 found = 1;
3208 }
3209 }
3210 return NULL;
3211 default:
3212 if (prev_type) {
3213 assert(type == prev_type);
3214 return NULL;
3215 } else {
3216 return type;
3217 }
3218 }
3219}
3220
3221/**
3222 * @brief Test whether 2 types have a common type.
3223 *
3224 * @param[in] type1 First type.
3225 * @param[in] type2 Second type.
3226 * @return 1 if they do, 0 otherwise.
3227 */
3228static int
3229warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3230{
3231 struct lysc_type *t1, *rt1, *t2, *rt2;
3232
3233 t1 = NULL;
3234 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3235 if (t1->basetype == LY_TYPE_LEAFREF) {
3236 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3237 } else {
3238 rt1 = t1;
3239 }
3240
3241 t2 = NULL;
3242 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3243 if (t2->basetype == LY_TYPE_LEAFREF) {
3244 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3245 } else {
3246 rt2 = t2;
3247 }
3248
3249 if (rt2->basetype == rt1->basetype) {
3250 /* match found */
3251 return 1;
3252 }
3253 }
3254 }
3255
3256 return 0;
3257}
3258
3259/**
3260 * @brief Check both operands of comparison operators.
3261 *
3262 * @param[in] ctx Context for errors.
3263 * @param[in] set1 First operand set.
3264 * @param[in] set2 Second operand set.
3265 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3266 * @param[in] expr Start of the expression to print with the warning.
3267 * @param[in] tok_pos Token position.
3268 */
3269static void
Radek Krejci857189e2020-09-01 13:26:36 +02003270warn_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 +02003271{
3272 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003273 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003274
3275 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3276 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3277
3278 if (!node1 && !node2) {
3279 /* no node-sets involved, nothing to do */
3280 return;
3281 }
3282
3283 if (node1) {
3284 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3285 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3286 warning = 1;
3287 leaves = 0;
3288 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3289 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3290 warning = 1;
3291 }
3292 }
3293
3294 if (node2) {
3295 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3296 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3297 warning = 1;
3298 leaves = 0;
3299 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3300 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3301 warning = 1;
3302 }
3303 }
3304
3305 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003306 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3307 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3308 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3309 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003310 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3311 warning = 1;
3312 }
3313 }
3314
3315 if (warning) {
3316 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3317 }
3318}
3319
3320/**
3321 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3322 *
3323 * @param[in] exp Parsed XPath expression.
3324 * @param[in] set Set with the leaf/leaf-list.
3325 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3326 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3327 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3328 */
3329static void
Michal Vasko40308e72020-10-20 16:38:40 +02003330warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3331 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003332{
3333 struct lysc_node *scnode;
3334 struct lysc_type *type;
3335 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003336 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003337 LY_ERR rc;
3338 struct ly_err_item *err = NULL;
3339
Michal Vasko69730152020-10-09 16:30:07 +02003340 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3341 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003342 /* check that the node can have the specified value */
3343 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3344 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3345 } else {
3346 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3347 }
3348 if (!value) {
3349 LOGMEM(set->ctx);
3350 return;
3351 }
3352
3353 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3354 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003355 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003356 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003357 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003358 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003359 }
3360
3361 type = ((struct lysc_node_leaf *)scnode)->type;
3362 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003363 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003364 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003365 if (rc == LY_EINCOMPLETE) {
3366 rc = LY_SUCCESS;
3367 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003368
3369 if (err) {
3370 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3371 ly_err_free(err);
3372 } else if (rc != LY_SUCCESS) {
3373 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3374 }
3375 if (rc != LY_SUCCESS) {
3376 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003377 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003378 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003379 } else {
3380 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003381 }
3382 }
3383 free(value);
3384 }
3385}
3386
3387/*
3388 * XPath functions
3389 */
3390
3391/**
3392 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3393 * depending on whether the first node bit value from the second argument is set.
3394 *
3395 * @param[in] args Array of arguments.
3396 * @param[in] arg_count Count of elements in @p args.
3397 * @param[in,out] set Context and result set at the same time.
3398 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003399 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003400 */
3401static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003402xpath_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 +02003403{
3404 struct lyd_node_term *leaf;
3405 struct lysc_node_leaf *sleaf;
3406 struct lysc_type_bits *bits;
3407 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003408 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003409
3410 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003411 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003412 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003413 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3414 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3415 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3416 sleaf->name);
3417 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3418 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3419 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003420 }
3421
3422 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3423 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003424 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3425 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003426 } else if (!warn_is_string_type(sleaf->type)) {
3427 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003428 }
3429 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003430 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003431 return rc;
3432 }
3433
Michal Vaskod3678892020-05-21 10:06:58 +02003434 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003435 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 +02003436 return LY_EVALID;
3437 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003438 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003439 LY_CHECK_RET(rc);
3440
3441 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003442 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003443 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003444 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3445 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003446 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003447 LY_ARRAY_FOR(bits->bits, u) {
3448 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003449 set_fill_boolean(set, 1);
3450 break;
3451 }
3452 }
3453 }
3454 }
3455
3456 return LY_SUCCESS;
3457}
3458
3459/**
3460 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3461 * with the argument converted to boolean.
3462 *
3463 * @param[in] args Array of arguments.
3464 * @param[in] arg_count Count of elements in @p args.
3465 * @param[in,out] set Context and result set at the same time.
3466 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003467 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003468 */
3469static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003470xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003471{
3472 LY_ERR rc;
3473
3474 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003475 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003476 return LY_SUCCESS;
3477 }
3478
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003479 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003480 LY_CHECK_RET(rc);
3481 set_fill_set(set, args[0]);
3482
3483 return LY_SUCCESS;
3484}
3485
3486/**
3487 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3488 * with the first argument rounded up to the nearest integer.
3489 *
3490 * @param[in] args Array of arguments.
3491 * @param[in] arg_count Count of elements in @p args.
3492 * @param[in,out] set Context and result set at the same time.
3493 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003494 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003495 */
3496static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003497xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003498{
3499 struct lysc_node_leaf *sleaf;
3500 LY_ERR rc = LY_SUCCESS;
3501
3502 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003503 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003504 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003505 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3506 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3507 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3508 sleaf->name);
3509 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3510 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3511 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003512 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003513 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003514 return rc;
3515 }
3516
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003517 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003518 LY_CHECK_RET(rc);
3519 if ((long long)args[0]->val.num != args[0]->val.num) {
3520 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3521 } else {
3522 set_fill_number(set, args[0]->val.num);
3523 }
3524
3525 return LY_SUCCESS;
3526}
3527
3528/**
3529 * @brief Execute the XPath concat(string, string, string*) function.
3530 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3531 *
3532 * @param[in] args Array of arguments.
3533 * @param[in] arg_count Count of elements in @p args.
3534 * @param[in,out] set Context and result set at the same time.
3535 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003536 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003537 */
3538static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003539xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003540{
3541 uint16_t i;
3542 char *str = NULL;
3543 size_t used = 1;
3544 LY_ERR rc = LY_SUCCESS;
3545 struct lysc_node_leaf *sleaf;
3546
3547 if (options & LYXP_SCNODE_ALL) {
3548 for (i = 0; i < arg_count; ++i) {
3549 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3550 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3551 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003552 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003553 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003554 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 +02003555 }
3556 }
3557 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003558 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003559 return rc;
3560 }
3561
3562 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003563 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003564 if (rc != LY_SUCCESS) {
3565 free(str);
3566 return rc;
3567 }
3568
3569 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3570 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3571 strcpy(str + used - 1, args[i]->val.str);
3572 used += strlen(args[i]->val.str);
3573 }
3574
3575 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003576 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003577 set->type = LYXP_SET_STRING;
3578 set->val.str = str;
3579
3580 return LY_SUCCESS;
3581}
3582
3583/**
3584 * @brief Execute the XPath contains(string, string) function.
3585 * Returns LYXP_SET_BOOLEAN whether the second argument can
3586 * be found in the first or not.
3587 *
3588 * @param[in] args Array of arguments.
3589 * @param[in] arg_count Count of elements in @p args.
3590 * @param[in,out] set Context and result set at the same time.
3591 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003592 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003593 */
3594static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003595xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003596{
3597 struct lysc_node_leaf *sleaf;
3598 LY_ERR rc = LY_SUCCESS;
3599
3600 if (options & LYXP_SCNODE_ALL) {
3601 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3602 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003603 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3604 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003605 } else if (!warn_is_string_type(sleaf->type)) {
3606 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003607 }
3608 }
3609
3610 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3611 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003612 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3613 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003614 } else if (!warn_is_string_type(sleaf->type)) {
3615 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003616 }
3617 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003618 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003619 return rc;
3620 }
3621
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003622 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003623 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003624 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003625 LY_CHECK_RET(rc);
3626
3627 if (strstr(args[0]->val.str, args[1]->val.str)) {
3628 set_fill_boolean(set, 1);
3629 } else {
3630 set_fill_boolean(set, 0);
3631 }
3632
3633 return LY_SUCCESS;
3634}
3635
3636/**
3637 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3638 * with the size of the node-set from the argument.
3639 *
3640 * @param[in] args Array of arguments.
3641 * @param[in] arg_count Count of elements in @p args.
3642 * @param[in,out] set Context and result set at the same time.
3643 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003644 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003645 */
3646static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003647xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003648{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003649 LY_ERR rc = LY_SUCCESS;
3650
3651 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003652 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003653 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003654 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003655 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003656 return rc;
3657 }
3658
Michal Vasko03ff5a72019-09-11 13:49:33 +02003659 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003660 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003661 return LY_EVALID;
3662 }
3663
3664 set_fill_number(set, args[0]->used);
3665 return LY_SUCCESS;
3666}
3667
3668/**
3669 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3670 * with the context with the intial node.
3671 *
3672 * @param[in] args Array of arguments.
3673 * @param[in] arg_count Count of elements in @p args.
3674 * @param[in,out] set Context and result set at the same time.
3675 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003676 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003677 */
3678static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003679xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003680{
3681 if (arg_count || args) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003682 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003683 return LY_EVALID;
3684 }
3685
3686 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003687 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003688
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003689 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003690 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003691 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003692
3693 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003694 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003695 }
3696
3697 return LY_SUCCESS;
3698}
3699
3700/**
3701 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3702 * leafref or instance-identifier target node(s).
3703 *
3704 * @param[in] args Array of arguments.
3705 * @param[in] arg_count Count of elements in @p args.
3706 * @param[in,out] set Context and result set at the same time.
3707 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003708 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003709 */
3710static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003711xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003712{
3713 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003714 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003715 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003716 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003717 struct ly_path *p;
3718 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003719 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003720 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003721 LY_ERR rc = LY_SUCCESS;
3722
3723 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003724 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003725 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003726 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3727 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3728 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3729 sleaf->name);
3730 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3731 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3732 __func__, sleaf->name);
3733 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003734 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003735 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko42e497c2020-01-06 08:38:25 +01003736 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003737 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003738 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003739
3740 /* it was already evaluated on schema, it must succeed */
Radek Krejcid5d37432021-03-12 13:46:40 +01003741 rc = ly_path_compile(set->ctx, lref->cur_mod, &sleaf->node, NULL, lref->path, LY_PATH_LREF_TRUE, oper,
Radek Krejci8df109d2021-04-23 12:19:08 +02003742 LY_PATH_TARGET_MANY, LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, NULL, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003743 assert(!rc);
3744
3745 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003746 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003747 ly_path_free(set->ctx, p);
3748
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003749 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003750 }
3751
Michal Vasko03ff5a72019-09-11 13:49:33 +02003752 return rc;
3753 }
3754
Michal Vaskod3678892020-05-21 10:06:58 +02003755 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003756 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003757 return LY_EVALID;
3758 }
3759
Michal Vaskod3678892020-05-21 10:06:58 +02003760 lyxp_set_free_content(set);
3761 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003762 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3763 sleaf = (struct lysc_node_leaf *)leaf->schema;
3764 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3765 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3766 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02003767 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01003768 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003769 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003770 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003771 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003772 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003773 } else {
3774 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003775 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003776 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003777 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003778 return LY_EVALID;
3779 }
3780 }
Michal Vasko004d3152020-06-11 19:59:22 +02003781
3782 /* insert it */
3783 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003784 }
3785 }
3786
3787 return LY_SUCCESS;
3788}
3789
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003790static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003791xpath_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 +02003792{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01003793 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003794 LY_ARRAY_COUNT_TYPE u;
3795 struct lyd_node_term *leaf;
3796 struct lysc_node_leaf *sleaf;
3797 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02003798 struct lyd_value *val;
3799 const struct lys_module *mod;
3800 const char *id_name;
3801 uint16_t id_len;
3802 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003803 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003804 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003805
3806 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003807 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003808 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003809 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3810 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3811 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3812 sleaf->name);
3813 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3814 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3815 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003816 }
3817
3818 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3819 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3820 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003821 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003822 } else if (!warn_is_string_type(sleaf->type)) {
3823 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3824 }
3825 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003826 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003827 return rc;
3828 }
3829
3830 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003831 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 +02003832 return LY_EVALID;
3833 }
3834 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3835 LY_CHECK_RET(rc);
3836
Michal Vasko93923692021-05-07 15:28:02 +02003837 /* parse the identity */
3838 id_name = args[1]->val.str;
3839 id_len = strlen(id_name);
3840 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
3841 LY_CHECK_RET(rc);
3842 if (!mod) {
3843 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
3844 return LY_EVALID;
3845 }
3846
3847 /* find the identity */
3848 found = 0;
3849 LY_ARRAY_FOR(mod->identities, u) {
3850 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
3851 /* we have match */
3852 found = 1;
3853 break;
3854 }
3855 }
3856 if (!found) {
3857 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
3858 return LY_EVALID;
3859 }
3860 id = &mod->identities[u];
3861
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003862 set_fill_boolean(set, 0);
3863 found = 0;
3864 for (i = 0; i < args[0]->used; ++i) {
3865 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3866 continue;
3867 }
3868
3869 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3870 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3871 sleaf = (struct lysc_node_leaf *)leaf->schema;
3872 val = &leaf->value;
3873 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3874 /* uninteresting */
3875 continue;
3876 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003877 } else {
3878 meta = args[0]->val.meta[i].meta;
3879 val = &meta->value;
3880 if (val->realtype->basetype != LY_TYPE_IDENT) {
3881 /* uninteresting */
3882 continue;
3883 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003884 }
3885
Michal Vasko93923692021-05-07 15:28:02 +02003886 /* check the identity itself */
3887 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003888 set_fill_boolean(set, 1);
3889 found = 1;
3890 }
Michal Vasko93923692021-05-07 15:28:02 +02003891 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
3892 set_fill_boolean(set, 1);
3893 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003894 }
3895
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003896 if (found) {
3897 break;
3898 }
3899 }
3900
3901 return LY_SUCCESS;
3902}
3903
Michal Vasko03ff5a72019-09-11 13:49:33 +02003904/**
3905 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3906 * on whether the first argument nodes contain a node of an identity derived from the second
3907 * argument identity.
3908 *
3909 * @param[in] args Array of arguments.
3910 * @param[in] arg_count Count of elements in @p args.
3911 * @param[in,out] set Context and result set at the same time.
3912 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003913 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003914 */
3915static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003916xpath_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 +02003917{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003918 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003919}
3920
3921/**
3922 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3923 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3924 * the second argument identity.
3925 *
3926 * @param[in] args Array of arguments.
3927 * @param[in] arg_count Count of elements in @p args.
3928 * @param[in,out] set Context and result set at the same time.
3929 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003930 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003931 */
3932static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003933xpath_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 +02003934{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003935 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003936}
3937
3938/**
3939 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3940 * with the integer value of the first node's enum value, otherwise NaN.
3941 *
3942 * @param[in] args Array of arguments.
3943 * @param[in] arg_count Count of elements in @p args.
3944 * @param[in,out] set Context and result set at the same time.
3945 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003946 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003947 */
3948static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003949xpath_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 +02003950{
3951 struct lyd_node_term *leaf;
3952 struct lysc_node_leaf *sleaf;
3953 LY_ERR rc = LY_SUCCESS;
3954
3955 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003956 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003957 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003958 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3959 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3960 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3961 sleaf->name);
3962 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3963 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
3964 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003965 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003966 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003967 return rc;
3968 }
3969
Michal Vaskod3678892020-05-21 10:06:58 +02003970 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003971 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003972 return LY_EVALID;
3973 }
3974
3975 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003976 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003977 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3978 sleaf = (struct lysc_node_leaf *)leaf->schema;
3979 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3980 set_fill_number(set, leaf->value.enum_item->value);
3981 }
3982 }
3983
3984 return LY_SUCCESS;
3985}
3986
3987/**
3988 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3989 * with false value.
3990 *
3991 * @param[in] args Array of arguments.
3992 * @param[in] arg_count Count of elements in @p args.
3993 * @param[in,out] set Context and result set at the same time.
3994 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003995 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003996 */
3997static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003998xpath_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 +02003999{
4000 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004001 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004002 return LY_SUCCESS;
4003 }
4004
4005 set_fill_boolean(set, 0);
4006 return LY_SUCCESS;
4007}
4008
4009/**
4010 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4011 * with the first argument floored (truncated).
4012 *
4013 * @param[in] args Array of arguments.
4014 * @param[in] arg_count Count of elements in @p args.
4015 * @param[in,out] set Context and result set at the same time.
4016 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004017 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004018 */
4019static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004020xpath_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 +02004021{
4022 LY_ERR rc;
4023
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004024 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004025 LY_CHECK_RET(rc);
4026 if (isfinite(args[0]->val.num)) {
4027 set_fill_number(set, (long long)args[0]->val.num);
4028 }
4029
4030 return LY_SUCCESS;
4031}
4032
4033/**
4034 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4035 * whether the language of the text matches the one from the argument.
4036 *
4037 * @param[in] args Array of arguments.
4038 * @param[in] arg_count Count of elements in @p args.
4039 * @param[in,out] set Context and result set at the same time.
4040 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004041 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004042 */
4043static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004044xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004045{
4046 const struct lyd_node *node;
4047 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004048 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004049 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004050 LY_ERR rc = LY_SUCCESS;
4051
4052 if (options & LYXP_SCNODE_ALL) {
4053 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4054 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004055 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4056 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004057 } else if (!warn_is_string_type(sleaf->type)) {
4058 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004059 }
4060 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004061 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004062 return rc;
4063 }
4064
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004065 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004066 LY_CHECK_RET(rc);
4067
Michal Vasko03ff5a72019-09-11 13:49:33 +02004068 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004069 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004070 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004071 } else if (!set->used) {
4072 set_fill_boolean(set, 0);
4073 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004074 }
4075
4076 switch (set->val.nodes[0].type) {
4077 case LYXP_NODE_ELEM:
4078 case LYXP_NODE_TEXT:
4079 node = set->val.nodes[0].node;
4080 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004081 case LYXP_NODE_META:
4082 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004083 break;
4084 default:
4085 /* nothing to do with roots */
4086 set_fill_boolean(set, 0);
4087 return LY_SUCCESS;
4088 }
4089
Michal Vasko9f96a052020-03-10 09:41:45 +01004090 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004091 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004092 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004093 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004094 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004095 break;
4096 }
4097 }
4098
Michal Vasko9f96a052020-03-10 09:41:45 +01004099 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004100 break;
4101 }
4102 }
4103
4104 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004105 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004106 set_fill_boolean(set, 0);
4107 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004108 uint64_t i;
4109
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004110 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004111 for (i = 0; args[0]->val.str[i]; ++i) {
4112 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4113 set_fill_boolean(set, 0);
4114 break;
4115 }
4116 }
4117 if (!args[0]->val.str[i]) {
4118 if (!val[i] || (val[i] == '-')) {
4119 set_fill_boolean(set, 1);
4120 } else {
4121 set_fill_boolean(set, 0);
4122 }
4123 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004124 }
4125
4126 return LY_SUCCESS;
4127}
4128
4129/**
4130 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4131 * with the context size.
4132 *
4133 * @param[in] args Array of arguments.
4134 * @param[in] arg_count Count of elements in @p args.
4135 * @param[in,out] set Context and result set at the same time.
4136 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004137 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004138 */
4139static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004140xpath_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 +02004141{
4142 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004143 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004144 return LY_SUCCESS;
4145 }
4146
Michal Vasko03ff5a72019-09-11 13:49:33 +02004147 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004148 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004149 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004150 } else if (!set->used) {
4151 set_fill_number(set, 0);
4152 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004153 }
4154
4155 set_fill_number(set, set->ctx_size);
4156 return LY_SUCCESS;
4157}
4158
4159/**
4160 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4161 * with the node name without namespace from the argument or the context.
4162 *
4163 * @param[in] args Array of arguments.
4164 * @param[in] arg_count Count of elements in @p args.
4165 * @param[in,out] set Context and result set at the same time.
4166 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004167 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004168 */
4169static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004170xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004171{
4172 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004173
Michal Vasko03ff5a72019-09-11 13:49:33 +02004174 /* suppress unused variable warning */
4175 (void)options;
4176
4177 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004178 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004179 return LY_SUCCESS;
4180 }
4181
4182 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004183 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004184 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004185 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004186 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004187 } else if (!args[0]->used) {
4188 set_fill_string(set, "", 0);
4189 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004190 }
4191
4192 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004193 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004194
4195 item = &args[0]->val.nodes[0];
4196 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004197 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004198 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004199 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004200 } else if (!set->used) {
4201 set_fill_string(set, "", 0);
4202 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004203 }
4204
4205 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004206 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004207
4208 item = &set->val.nodes[0];
4209 }
4210
4211 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004212 case LYXP_NODE_NONE:
4213 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004214 case LYXP_NODE_ROOT:
4215 case LYXP_NODE_ROOT_CONFIG:
4216 case LYXP_NODE_TEXT:
4217 set_fill_string(set, "", 0);
4218 break;
4219 case LYXP_NODE_ELEM:
4220 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4221 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004222 case LYXP_NODE_META:
4223 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004224 break;
4225 }
4226
4227 return LY_SUCCESS;
4228}
4229
4230/**
4231 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4232 * with the node name fully qualified (with namespace) from the argument or the context.
4233 *
4234 * @param[in] args Array of arguments.
4235 * @param[in] arg_count Count of elements in @p args.
4236 * @param[in,out] set Context and result set at the same time.
4237 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004238 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004239 */
4240static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004241xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004242{
4243 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004244 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004245 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004246 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004247
4248 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004249 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004250 return LY_SUCCESS;
4251 }
4252
4253 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004254 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004255 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004256 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004257 } else if (!args[0]->used) {
4258 set_fill_string(set, "", 0);
4259 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004260 }
4261
4262 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004263 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004264
4265 item = &args[0]->val.nodes[0];
4266 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004267 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004268 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004269 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004270 } else if (!set->used) {
4271 set_fill_string(set, "", 0);
4272 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004273 }
4274
4275 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004276 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004277
4278 item = &set->val.nodes[0];
4279 }
4280
4281 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004282 case LYXP_NODE_NONE:
4283 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004284 case LYXP_NODE_ROOT:
4285 case LYXP_NODE_ROOT_CONFIG:
4286 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004287 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004288 break;
4289 case LYXP_NODE_ELEM:
4290 mod = item->node->schema->module;
4291 name = item->node->schema->name;
4292 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004293 case LYXP_NODE_META:
4294 mod = ((struct lyd_meta *)item->node)->annotation->module;
4295 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004296 break;
4297 }
4298
4299 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004300 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004301 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4302 set_fill_string(set, str, strlen(str));
4303 free(str);
4304 } else {
4305 set_fill_string(set, "", 0);
4306 }
4307
4308 return LY_SUCCESS;
4309}
4310
4311/**
4312 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4313 * with the namespace of the node from the argument or the context.
4314 *
4315 * @param[in] args Array of arguments.
4316 * @param[in] arg_count Count of elements in @p args.
4317 * @param[in,out] set Context and result set at the same time.
4318 * @param[in] options XPath options.
4319 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4320 */
4321static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004322xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004323{
4324 struct lyxp_set_node *item;
4325 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004326
Michal Vasko03ff5a72019-09-11 13:49:33 +02004327 /* suppress unused variable warning */
4328 (void)options;
4329
4330 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004331 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004332 return LY_SUCCESS;
4333 }
4334
4335 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004336 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004337 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004338 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004339 return LY_EVALID;
4340 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004341 set_fill_string(set, "", 0);
4342 return LY_SUCCESS;
4343 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004344
4345 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004346 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004347
4348 item = &args[0]->val.nodes[0];
4349 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004350 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004351 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004352 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004353 } else if (!set->used) {
4354 set_fill_string(set, "", 0);
4355 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004356 }
4357
4358 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004359 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004360
4361 item = &set->val.nodes[0];
4362 }
4363
4364 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004365 case LYXP_NODE_NONE:
4366 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004367 case LYXP_NODE_ROOT:
4368 case LYXP_NODE_ROOT_CONFIG:
4369 case LYXP_NODE_TEXT:
4370 set_fill_string(set, "", 0);
4371 break;
4372 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004373 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004374 if (item->type == LYXP_NODE_ELEM) {
4375 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004376 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004377 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004378 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004379 }
4380
4381 set_fill_string(set, mod->ns, strlen(mod->ns));
4382 break;
4383 }
4384
4385 return LY_SUCCESS;
4386}
4387
4388/**
4389 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4390 * with only nodes from the context. In practice it either leaves the context
4391 * as it is or returns an empty node set.
4392 *
4393 * @param[in] args Array of arguments.
4394 * @param[in] arg_count Count of elements in @p args.
4395 * @param[in,out] set Context and result set at the same time.
4396 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004397 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004398 */
4399static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004400xpath_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 +02004401{
4402 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004403 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004404 return LY_SUCCESS;
4405 }
4406
4407 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004408 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004409 }
4410 return LY_SUCCESS;
4411}
4412
4413/**
4414 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4415 * with normalized value (no leading, trailing, double white spaces) of the node
4416 * from the argument or the context.
4417 *
4418 * @param[in] args Array of arguments.
4419 * @param[in] arg_count Count of elements in @p args.
4420 * @param[in,out] set Context and result set at the same time.
4421 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004422 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004423 */
4424static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004425xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004426{
4427 uint16_t i, new_used;
4428 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004429 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004430 struct lysc_node_leaf *sleaf;
4431 LY_ERR rc = LY_SUCCESS;
4432
4433 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004434 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4435 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004436 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004437 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4438 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004439 } else if (!warn_is_string_type(sleaf->type)) {
4440 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004441 }
4442 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004443 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004444 return rc;
4445 }
4446
4447 if (arg_count) {
4448 set_fill_set(set, args[0]);
4449 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004450 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004451 LY_CHECK_RET(rc);
4452
4453 /* is there any normalization necessary? */
4454 for (i = 0; set->val.str[i]; ++i) {
4455 if (is_xmlws(set->val.str[i])) {
4456 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4457 have_spaces = 1;
4458 break;
4459 }
4460 space_before = 1;
4461 } else {
4462 space_before = 0;
4463 }
4464 }
4465
4466 /* yep, there is */
4467 if (have_spaces) {
4468 /* it's enough, at least one character will go, makes space for ending '\0' */
4469 new = malloc(strlen(set->val.str) * sizeof(char));
4470 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4471 new_used = 0;
4472
4473 space_before = 0;
4474 for (i = 0; set->val.str[i]; ++i) {
4475 if (is_xmlws(set->val.str[i])) {
4476 if ((i == 0) || space_before) {
4477 space_before = 1;
4478 continue;
4479 } else {
4480 space_before = 1;
4481 }
4482 } else {
4483 space_before = 0;
4484 }
4485
4486 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4487 ++new_used;
4488 }
4489
4490 /* at worst there is one trailing space now */
4491 if (new_used && is_xmlws(new[new_used - 1])) {
4492 --new_used;
4493 }
4494
4495 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4496 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4497 new[new_used] = '\0';
4498
4499 free(set->val.str);
4500 set->val.str = new;
4501 }
4502
4503 return LY_SUCCESS;
4504}
4505
4506/**
4507 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4508 * with the argument converted to boolean and logically inverted.
4509 *
4510 * @param[in] args Array of arguments.
4511 * @param[in] arg_count Count of elements in @p args.
4512 * @param[in,out] set Context and result set at the same time.
4513 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004514 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004515 */
4516static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004517xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004518{
4519 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004520 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004521 return LY_SUCCESS;
4522 }
4523
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004524 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004525 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004526 set_fill_boolean(set, 0);
4527 } else {
4528 set_fill_boolean(set, 1);
4529 }
4530
4531 return LY_SUCCESS;
4532}
4533
4534/**
4535 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4536 * with the number representation of either the argument or the context.
4537 *
4538 * @param[in] args Array of arguments.
4539 * @param[in] arg_count Count of elements in @p args.
4540 * @param[in,out] set Context and result set at the same time.
4541 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004542 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004543 */
4544static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004545xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004546{
4547 LY_ERR rc;
4548
4549 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004550 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004551 return LY_SUCCESS;
4552 }
4553
4554 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004555 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004556 LY_CHECK_RET(rc);
4557 set_fill_set(set, args[0]);
4558 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004559 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004560 LY_CHECK_RET(rc);
4561 }
4562
4563 return LY_SUCCESS;
4564}
4565
4566/**
4567 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4568 * with the context position.
4569 *
4570 * @param[in] args Array of arguments.
4571 * @param[in] arg_count Count of elements in @p args.
4572 * @param[in,out] set Context and result set at the same time.
4573 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004574 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004575 */
4576static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004577xpath_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 +02004578{
4579 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004580 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004581 return LY_SUCCESS;
4582 }
4583
Michal Vasko03ff5a72019-09-11 13:49:33 +02004584 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004585 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004586 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004587 } else if (!set->used) {
4588 set_fill_number(set, 0);
4589 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004590 }
4591
4592 set_fill_number(set, set->ctx_pos);
4593
4594 /* UNUSED in 'Release' build type */
4595 (void)options;
4596 return LY_SUCCESS;
4597}
4598
4599/**
4600 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4601 * depending on whether the second argument regex matches the first argument string. For details refer to
4602 * YANG 1.1 RFC section 10.2.1.
4603 *
4604 * @param[in] args Array of arguments.
4605 * @param[in] arg_count Count of elements in @p args.
4606 * @param[in,out] set Context and result set at the same time.
4607 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004608 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004609 */
4610static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004611xpath_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 +02004612{
4613 struct lysc_pattern **patterns = NULL, **pattern;
4614 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004615 LY_ERR rc = LY_SUCCESS;
4616 struct ly_err_item *err;
4617
4618 if (options & LYXP_SCNODE_ALL) {
4619 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4620 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4621 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004622 } else if (!warn_is_string_type(sleaf->type)) {
4623 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004624 }
4625 }
4626
4627 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4628 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4629 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 +02004630 } else if (!warn_is_string_type(sleaf->type)) {
4631 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004632 }
4633 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004634 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004635 return rc;
4636 }
4637
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004638 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004639 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004640 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004641 LY_CHECK_RET(rc);
4642
4643 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek IÅ¡a45802b52021-02-09 09:21:58 +01004644 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004645 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004646 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Radek Krejciddace2c2021-01-08 11:30:56 +01004647 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004648 if (rc != LY_SUCCESS) {
4649 LY_ARRAY_FREE(patterns);
4650 return rc;
4651 }
4652
Radek Krejci0b013302021-03-29 15:22:32 +02004653 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004654 pcre2_code_free((*pattern)->code);
4655 free(*pattern);
4656 LY_ARRAY_FREE(patterns);
4657 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004658 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004659 ly_err_free(err);
4660 return rc;
4661 }
4662
4663 if (rc == LY_EVALID) {
4664 ly_err_free(err);
4665 set_fill_boolean(set, 0);
4666 } else {
4667 set_fill_boolean(set, 1);
4668 }
4669
4670 return LY_SUCCESS;
4671}
4672
4673/**
4674 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4675 * with the rounded first argument. For details refer to
4676 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4677 *
4678 * @param[in] args Array of arguments.
4679 * @param[in] arg_count Count of elements in @p args.
4680 * @param[in,out] set Context and result set at the same time.
4681 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004682 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004683 */
4684static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004685xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004686{
4687 struct lysc_node_leaf *sleaf;
4688 LY_ERR rc = LY_SUCCESS;
4689
4690 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004691 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004692 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004693 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4694 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4695 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4696 sleaf->name);
4697 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4698 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4699 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004700 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004701 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004702 return rc;
4703 }
4704
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004705 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004706 LY_CHECK_RET(rc);
4707
4708 /* cover only the cases where floor can't be used */
4709 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4710 set_fill_number(set, -0.0f);
4711 } else {
4712 args[0]->val.num += 0.5;
4713 rc = xpath_floor(args, 1, args[0], options);
4714 LY_CHECK_RET(rc);
4715 set_fill_number(set, args[0]->val.num);
4716 }
4717
4718 return LY_SUCCESS;
4719}
4720
4721/**
4722 * @brief Execute the XPath starts-with(string, string) function.
4723 * Returns LYXP_SET_BOOLEAN whether the second argument is
4724 * the prefix of the first or not.
4725 *
4726 * @param[in] args Array of arguments.
4727 * @param[in] arg_count Count of elements in @p args.
4728 * @param[in,out] set Context and result set at the same time.
4729 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004730 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004731 */
4732static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004733xpath_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 +02004734{
4735 struct lysc_node_leaf *sleaf;
4736 LY_ERR rc = LY_SUCCESS;
4737
4738 if (options & LYXP_SCNODE_ALL) {
4739 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4740 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4741 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 +02004742 } else if (!warn_is_string_type(sleaf->type)) {
4743 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004744 }
4745 }
4746
4747 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4748 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4749 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 +02004750 } else if (!warn_is_string_type(sleaf->type)) {
4751 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004752 }
4753 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004754 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004755 return rc;
4756 }
4757
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004758 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004759 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004760 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004761 LY_CHECK_RET(rc);
4762
4763 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4764 set_fill_boolean(set, 0);
4765 } else {
4766 set_fill_boolean(set, 1);
4767 }
4768
4769 return LY_SUCCESS;
4770}
4771
4772/**
4773 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4774 * with the string representation of either the argument or the context.
4775 *
4776 * @param[in] args Array of arguments.
4777 * @param[in] arg_count Count of elements in @p args.
4778 * @param[in,out] set Context and result set at the same time.
4779 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004780 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004781 */
4782static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004783xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004784{
4785 LY_ERR rc;
4786
4787 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004788 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004789 return LY_SUCCESS;
4790 }
4791
4792 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004793 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004794 LY_CHECK_RET(rc);
4795 set_fill_set(set, args[0]);
4796 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004797 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004798 LY_CHECK_RET(rc);
4799 }
4800
4801 return LY_SUCCESS;
4802}
4803
4804/**
4805 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4806 * with the length of the string in either the argument or the context.
4807 *
4808 * @param[in] args Array of arguments.
4809 * @param[in] arg_count Count of elements in @p args.
4810 * @param[in,out] set Context and result set at the same time.
4811 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004812 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004813 */
4814static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004815xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004816{
4817 struct lysc_node_leaf *sleaf;
4818 LY_ERR rc = LY_SUCCESS;
4819
4820 if (options & LYXP_SCNODE_ALL) {
4821 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4822 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4823 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 +02004824 } else if (!warn_is_string_type(sleaf->type)) {
4825 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004826 }
4827 }
4828 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4829 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4830 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 +02004831 } else if (!warn_is_string_type(sleaf->type)) {
4832 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004833 }
4834 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004835 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004836 return rc;
4837 }
4838
4839 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004840 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004841 LY_CHECK_RET(rc);
4842 set_fill_number(set, strlen(args[0]->val.str));
4843 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004844 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004845 LY_CHECK_RET(rc);
4846 set_fill_number(set, strlen(set->val.str));
4847 }
4848
4849 return LY_SUCCESS;
4850}
4851
4852/**
4853 * @brief Execute the XPath substring(string, number, number?) function.
4854 * Returns LYXP_SET_STRING substring of the first argument starting
4855 * on the second argument index ending on the third argument index,
4856 * indexed from 1. For exact definition refer to
4857 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4858 *
4859 * @param[in] args Array of arguments.
4860 * @param[in] arg_count Count of elements in @p args.
4861 * @param[in,out] set Context and result set at the same time.
4862 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004863 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004864 */
4865static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004866xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004867{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004868 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004869 uint16_t str_start, str_len, pos;
4870 struct lysc_node_leaf *sleaf;
4871 LY_ERR rc = LY_SUCCESS;
4872
4873 if (options & LYXP_SCNODE_ALL) {
4874 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4875 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4876 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 +02004877 } else if (!warn_is_string_type(sleaf->type)) {
4878 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004879 }
4880 }
4881
4882 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4883 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4884 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 +02004885 } else if (!warn_is_numeric_type(sleaf->type)) {
4886 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004887 }
4888 }
4889
Michal Vasko69730152020-10-09 16:30:07 +02004890 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4891 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004892 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4893 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 +02004894 } else if (!warn_is_numeric_type(sleaf->type)) {
4895 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004896 }
4897 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004898 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004899 return rc;
4900 }
4901
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004902 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004903 LY_CHECK_RET(rc);
4904
4905 /* start */
4906 if (xpath_round(&args[1], 1, args[1], options)) {
4907 return -1;
4908 }
4909 if (isfinite(args[1]->val.num)) {
4910 start = args[1]->val.num - 1;
4911 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004912 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004913 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004914 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004915 }
4916
4917 /* len */
4918 if (arg_count == 3) {
4919 rc = xpath_round(&args[2], 1, args[2], options);
4920 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004921 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004922 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004923 } else if (isfinite(args[2]->val.num)) {
4924 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004925 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004926 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004927 }
4928 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004929 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004930 }
4931
4932 /* find matching character positions */
4933 str_start = 0;
4934 str_len = 0;
4935 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4936 if (pos < start) {
4937 ++str_start;
4938 } else if (pos < start + len) {
4939 ++str_len;
4940 } else {
4941 break;
4942 }
4943 }
4944
4945 set_fill_string(set, args[0]->val.str + str_start, str_len);
4946 return LY_SUCCESS;
4947}
4948
4949/**
4950 * @brief Execute the XPath substring-after(string, string) function.
4951 * Returns LYXP_SET_STRING with the string succeeding the occurance
4952 * of the second argument in the first or an empty string.
4953 *
4954 * @param[in] args Array of arguments.
4955 * @param[in] arg_count Count of elements in @p args.
4956 * @param[in,out] set Context and result set at the same time.
4957 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004958 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004959 */
4960static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004961xpath_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 +02004962{
4963 char *ptr;
4964 struct lysc_node_leaf *sleaf;
4965 LY_ERR rc = LY_SUCCESS;
4966
4967 if (options & LYXP_SCNODE_ALL) {
4968 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4969 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4970 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 +02004971 } else if (!warn_is_string_type(sleaf->type)) {
4972 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004973 }
4974 }
4975
4976 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4977 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4978 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 +02004979 } else if (!warn_is_string_type(sleaf->type)) {
4980 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004981 }
4982 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004983 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004984 return rc;
4985 }
4986
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004987 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004988 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004989 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004990 LY_CHECK_RET(rc);
4991
4992 ptr = strstr(args[0]->val.str, args[1]->val.str);
4993 if (ptr) {
4994 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4995 } else {
4996 set_fill_string(set, "", 0);
4997 }
4998
4999 return LY_SUCCESS;
5000}
5001
5002/**
5003 * @brief Execute the XPath substring-before(string, string) function.
5004 * Returns LYXP_SET_STRING with the string preceding the occurance
5005 * of the second argument in the first or an empty string.
5006 *
5007 * @param[in] args Array of arguments.
5008 * @param[in] arg_count Count of elements in @p args.
5009 * @param[in,out] set Context and result set at the same time.
5010 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005011 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005012 */
5013static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005014xpath_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 +02005015{
5016 char *ptr;
5017 struct lysc_node_leaf *sleaf;
5018 LY_ERR rc = LY_SUCCESS;
5019
5020 if (options & LYXP_SCNODE_ALL) {
5021 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5022 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5023 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 +02005024 } else if (!warn_is_string_type(sleaf->type)) {
5025 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005026 }
5027 }
5028
5029 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5030 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5031 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 +02005032 } else if (!warn_is_string_type(sleaf->type)) {
5033 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005034 }
5035 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005036 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005037 return rc;
5038 }
5039
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005040 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005041 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005042 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005043 LY_CHECK_RET(rc);
5044
5045 ptr = strstr(args[0]->val.str, args[1]->val.str);
5046 if (ptr) {
5047 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5048 } else {
5049 set_fill_string(set, "", 0);
5050 }
5051
5052 return LY_SUCCESS;
5053}
5054
5055/**
5056 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5057 * with the sum of all the nodes in the context.
5058 *
5059 * @param[in] args Array of arguments.
5060 * @param[in] arg_count Count of elements in @p args.
5061 * @param[in,out] set Context and result set at the same time.
5062 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005063 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005064 */
5065static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005066xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005067{
5068 long double num;
5069 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005070 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005071 struct lyxp_set set_item;
5072 struct lysc_node_leaf *sleaf;
5073 LY_ERR rc = LY_SUCCESS;
5074
5075 if (options & LYXP_SCNODE_ALL) {
5076 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5077 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005078 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005079 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5080 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5081 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005082 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005083 } else if (!warn_is_numeric_type(sleaf->type)) {
5084 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005085 }
5086 }
5087 }
5088 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005089 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005090 return rc;
5091 }
5092
5093 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005094
5095 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005096 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005097 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005098 } else if (!args[0]->used) {
5099 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005100 }
5101
Michal Vasko5c4e5892019-11-14 12:31:38 +01005102 set_init(&set_item, set);
5103
Michal Vasko03ff5a72019-09-11 13:49:33 +02005104 set_item.type = LYXP_SET_NODE_SET;
5105 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5106 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5107
5108 set_item.used = 1;
5109 set_item.size = 1;
5110
5111 for (i = 0; i < args[0]->used; ++i) {
5112 set_item.val.nodes[0] = args[0]->val.nodes[i];
5113
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005114 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005115 LY_CHECK_RET(rc);
5116 num = cast_string_to_number(str);
5117 free(str);
5118 set->val.num += num;
5119 }
5120
5121 free(set_item.val.nodes);
5122
5123 return LY_SUCCESS;
5124}
5125
5126/**
5127 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5128 * with the text content of the nodes in the context.
5129 *
5130 * @param[in] args Array of arguments.
5131 * @param[in] arg_count Count of elements in @p args.
5132 * @param[in,out] set Context and result set at the same time.
5133 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005134 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005135 */
5136static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005137xpath_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 +02005138{
5139 uint32_t i;
5140
5141 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005142 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005143 return LY_SUCCESS;
5144 }
5145
Michal Vasko03ff5a72019-09-11 13:49:33 +02005146 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005147 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005148 return LY_EVALID;
5149 }
5150
Michal Vaskod989ba02020-08-24 10:59:24 +02005151 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005152 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005153 case LYXP_NODE_NONE:
5154 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005155 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005156 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5157 set->val.nodes[i].type = LYXP_NODE_TEXT;
5158 ++i;
5159 break;
5160 }
Radek Krejci0f969882020-08-21 16:56:47 +02005161 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005162 case LYXP_NODE_ROOT:
5163 case LYXP_NODE_ROOT_CONFIG:
5164 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005165 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005166 set_remove_node(set, i);
5167 break;
5168 }
5169 }
5170
5171 return LY_SUCCESS;
5172}
5173
5174/**
5175 * @brief Execute the XPath translate(string, string, string) function.
5176 * Returns LYXP_SET_STRING with the first argument with the characters
5177 * from the second argument replaced by those on the corresponding
5178 * positions in the third argument.
5179 *
5180 * @param[in] args Array of arguments.
5181 * @param[in] arg_count Count of elements in @p args.
5182 * @param[in,out] set Context and result set at the same time.
5183 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005184 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005185 */
5186static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005187xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005188{
5189 uint16_t i, j, new_used;
5190 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005191 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005192 struct lysc_node_leaf *sleaf;
5193 LY_ERR rc = LY_SUCCESS;
5194
5195 if (options & LYXP_SCNODE_ALL) {
5196 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5197 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5198 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 +02005199 } else if (!warn_is_string_type(sleaf->type)) {
5200 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005201 }
5202 }
5203
5204 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5205 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5206 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 +02005207 } else if (!warn_is_string_type(sleaf->type)) {
5208 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005209 }
5210 }
5211
5212 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5213 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5214 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 +02005215 } else if (!warn_is_string_type(sleaf->type)) {
5216 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005217 }
5218 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005219 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005220 return rc;
5221 }
5222
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005223 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005224 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005225 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005226 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005227 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005228 LY_CHECK_RET(rc);
5229
5230 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5231 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5232 new_used = 0;
5233
5234 have_removed = 0;
5235 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005236 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005237
5238 for (j = 0; args[1]->val.str[j]; ++j) {
5239 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5240 /* removing this char */
5241 if (j >= strlen(args[2]->val.str)) {
5242 have_removed = 1;
5243 found = 1;
5244 break;
5245 }
5246 /* replacing this char */
5247 new[new_used] = args[2]->val.str[j];
5248 ++new_used;
5249 found = 1;
5250 break;
5251 }
5252 }
5253
5254 /* copying this char */
5255 if (!found) {
5256 new[new_used] = args[0]->val.str[i];
5257 ++new_used;
5258 }
5259 }
5260
5261 if (have_removed) {
5262 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5263 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5264 }
5265 new[new_used] = '\0';
5266
Michal Vaskod3678892020-05-21 10:06:58 +02005267 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005268 set->type = LYXP_SET_STRING;
5269 set->val.str = new;
5270
5271 return LY_SUCCESS;
5272}
5273
5274/**
5275 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5276 * with true value.
5277 *
5278 * @param[in] args Array of arguments.
5279 * @param[in] arg_count Count of elements in @p args.
5280 * @param[in,out] set Context and result set at the same time.
5281 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005282 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005283 */
5284static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005285xpath_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 +02005286{
5287 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005288 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005289 return LY_SUCCESS;
5290 }
5291
5292 set_fill_boolean(set, 1);
5293 return LY_SUCCESS;
5294}
5295
5296/*
5297 * moveto functions
5298 *
5299 * They and only they actually change the context (set).
5300 */
5301
5302/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005303 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005304 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005305 * XPath @p set is expected to be a (sc)node set!
5306 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005307 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5308 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005309 * @param[in] set Set with general XPath context.
5310 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005311 * @param[out] moveto_mod Expected module of a matching node.
5312 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005313 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005314static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005315moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5316 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005317{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005318 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005319 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005320 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005321
Michal Vasko2104e9f2020-03-06 08:23:25 +01005322 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5323
Michal Vasko6346ece2019-09-24 13:12:53 +02005324 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5325 /* specific module */
5326 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005327 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005328
Michal Vasko004d3152020-06-11 19:59:22 +02005329 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005330 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005331 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005332 return LY_EVALID;
5333 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005334
Michal Vasko6346ece2019-09-24 13:12:53 +02005335 *qname += pref_len + 1;
5336 *qname_len -= pref_len + 1;
5337 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5338 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005339 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005340 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005341 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005342 case LY_VALUE_SCHEMA:
5343 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005344 /* current module */
5345 mod = set->cur_mod;
5346 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005347 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005348 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005349 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005350 /* inherit parent (context node) module */
5351 if (ctx_scnode) {
5352 mod = ctx_scnode->module;
5353 } else {
5354 mod = NULL;
5355 }
5356 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005357 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005358 /* all nodes need to be prefixed */
5359 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5360 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005361 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005362 }
5363
Michal Vasko6346ece2019-09-24 13:12:53 +02005364 *moveto_mod = mod;
5365 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005366}
5367
5368/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005369 * @brief Move context @p set to the root. Handles absolute path.
5370 * Result is LYXP_SET_NODE_SET.
5371 *
5372 * @param[in,out] set Set to use.
5373 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005374 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005375 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005376static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005377moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005378{
aPiecek8b0cc152021-05-31 16:40:31 +02005379 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005380
5381 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005382 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskob0099a92020-08-31 14:55:23 +02005383 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005384 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005385 set->type = LYXP_SET_NODE_SET;
5386 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005387 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005388 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005389
5390 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005391}
5392
5393/**
5394 * @brief Check @p node as a part of NameTest processing.
5395 *
5396 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005397 * @param[in] ctx_node Context node.
5398 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005399 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005400 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005401 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005402 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5403 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005404 */
5405static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005406moveto_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 +01005407 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005408{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005409 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5410 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005411 case LY_VALUE_SCHEMA:
5412 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005413 /* use current module */
5414 moveto_mod = set->cur_mod;
5415 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005416 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005417 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005418 /* inherit module of the context node, if any */
5419 if (ctx_node) {
5420 moveto_mod = ctx_node->schema->module;
5421 }
5422 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005423 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005424 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005425 /* not defined */
5426 LOGINT(set->ctx);
5427 return LY_EINVAL;
5428 }
5429 }
5430
Michal Vasko03ff5a72019-09-11 13:49:33 +02005431 /* module check */
5432 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005433 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005434 }
5435
Michal Vasko5c4e5892019-11-14 12:31:38 +01005436 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005437 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005438 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005439 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5440 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005441 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005442 }
5443
5444 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005445 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005446 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005447 }
5448
Michal Vaskoa1424542019-11-14 16:08:52 +01005449 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005450 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005451 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005452 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005453
5454 /* match */
5455 return LY_SUCCESS;
5456}
5457
5458/**
5459 * @brief Check @p node as a part of schema NameTest processing.
5460 *
5461 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005462 * @param[in] ctx_scnode Context node.
5463 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005464 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005465 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005466 * @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 +02005467 */
5468static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005469moveto_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 +02005470 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005471{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005472 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5473 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005474 case LY_VALUE_SCHEMA:
5475 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005476 /* use current module */
5477 moveto_mod = set->cur_mod;
5478 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005479 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005480 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005481 /* inherit module of the context node, if any */
5482 if (ctx_scnode) {
5483 moveto_mod = ctx_scnode->module;
5484 }
5485 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005486 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005487 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005488 /* not defined */
5489 LOGINT(set->ctx);
5490 return LY_EINVAL;
5491 }
5492 }
5493
Michal Vasko03ff5a72019-09-11 13:49:33 +02005494 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005495 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005496 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005497 }
5498
5499 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005500 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005501 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005502 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005503 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005504 }
5505
5506 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005507 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005508 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005509 }
5510
5511 /* match */
5512 return LY_SUCCESS;
5513}
5514
5515/**
Michal Vaskod3678892020-05-21 10:06:58 +02005516 * @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 +02005517 *
5518 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005519 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005520 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005521 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005522 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5523 */
5524static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005525moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005526{
Michal Vaskof03ed032020-03-04 13:31:44 +01005527 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005528 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005529 LY_ERR rc;
5530
aPiecek8b0cc152021-05-31 16:40:31 +02005531 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005532 return LY_SUCCESS;
5533 }
5534
5535 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005536 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005537 return LY_EVALID;
5538 }
5539
Michal Vasko03ff5a72019-09-11 13:49:33 +02005540 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005541 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005542
5543 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 +01005544 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005545
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005546 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005547 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005548 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005549 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005550 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005551 ctx_node = set->val.nodes[i].node;
5552 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005553 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005554
Michal Vaskod3678892020-05-21 10:06:58 +02005555 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005556 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005557 if (rc == LY_SUCCESS) {
5558 if (!replaced) {
5559 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5560 replaced = 1;
5561 } else {
5562 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005563 }
Michal Vaskod3678892020-05-21 10:06:58 +02005564 ++i;
5565 } else if (rc == LY_EINCOMPLETE) {
5566 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005567 }
5568 }
5569
5570 if (!replaced) {
5571 /* no match */
5572 set_remove_node(set, i);
5573 }
5574 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005575
5576 return LY_SUCCESS;
5577}
5578
5579/**
Michal Vaskod3678892020-05-21 10:06:58 +02005580 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5581 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005582 *
5583 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005584 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005585 * @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 +01005586 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005587 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5588 */
5589static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005590moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5591 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005592{
Michal Vasko004d3152020-06-11 19:59:22 +02005593 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005594 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005595 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005596 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005597
Michal Vasko004d3152020-06-11 19:59:22 +02005598 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005599
aPiecek8b0cc152021-05-31 16:40:31 +02005600 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02005601 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005602 }
5603
5604 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005605 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005606 ret = LY_EVALID;
5607 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005608 }
5609
5610 /* context check for all the nodes since we have the schema node */
5611 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5612 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005613 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005614 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5615 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005616 lyxp_set_free_content(set);
5617 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005618 }
5619
5620 /* create specific data instance if needed */
5621 if (scnode->nodetype == LYS_LIST) {
5622 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5623 } else if (scnode->nodetype == LYS_LEAFLIST) {
5624 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005625 }
5626
5627 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005628 siblings = NULL;
5629
5630 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5631 assert(!set->val.nodes[i].node);
5632
5633 /* search in all the trees */
5634 siblings = set->tree;
5635 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5636 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005637 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005638 }
5639
5640 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005641 if (inst) {
5642 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005643 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005644 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005645 }
5646
5647 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005648 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005649 ret = LY_EINCOMPLETE;
5650 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005651 }
5652
5653 if (sub) {
5654 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005655 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005656 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005657 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005658 /* no match */
5659 set_remove_node(set, i);
5660 }
5661 }
5662
Michal Vasko004d3152020-06-11 19:59:22 +02005663cleanup:
5664 lyd_free_tree(inst);
5665 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005666}
5667
5668/**
5669 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5670 *
5671 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005672 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005673 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005674 * @param[in] options XPath options.
5675 * @return LY_ERR
5676 */
5677static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005678moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005679{
Radek Krejci857189e2020-09-01 13:26:36 +02005680 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005681 uint32_t getnext_opts;
5682 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005683 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005684 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005685 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005686
aPiecek8b0cc152021-05-31 16:40:31 +02005687 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005688 return LY_SUCCESS;
5689 }
5690
5691 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005692 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005693 return LY_EVALID;
5694 }
5695
Michal Vaskocafad9d2019-11-07 15:20:03 +01005696 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005697 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005698 if (options & LYXP_SCNODE_OUTPUT) {
5699 getnext_opts |= LYS_GETNEXT_OUTPUT;
5700 }
5701
Michal Vasko03ff5a72019-09-11 13:49:33 +02005702 orig_used = set->used;
5703 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005704 uint32_t idx;
5705
Radek Krejcif13b87b2020-12-01 22:02:17 +01005706 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5707 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005708 continue;
5709 }
5710
5711 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005712 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005713 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005714 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005715 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005716
5717 start_parent = set->val.scnodes[i].scnode;
5718
5719 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 +02005720 /* 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 +02005721 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005722 mod_idx = 0;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005723 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005724 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005725 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005726 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005727 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005728 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5729
Michal Vasko03ff5a72019-09-11 13:49:33 +02005730 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005731 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005732 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005733 temp_ctx = 1;
5734 }
5735 }
5736 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005737 }
5738
Michal Vasko519fd602020-05-26 12:17:39 +02005739 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5740 iter = NULL;
5741 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005742 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005743 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5744
5745 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005746 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005747 temp_ctx = 1;
5748 }
5749 }
5750 }
5751 }
5752 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005753
5754 /* correct temporary in_ctx values */
5755 if (temp_ctx) {
5756 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005757 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5758 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005759 }
5760 }
5761 }
5762
5763 return LY_SUCCESS;
5764}
5765
5766/**
Michal Vaskod3678892020-05-21 10:06:58 +02005767 * @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 +02005768 * Context position aware.
5769 *
5770 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005771 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005772 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005773 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005774 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5775 */
5776static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005777moveto_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 +02005778{
5779 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005780 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005781 struct lyxp_set ret_set;
5782 LY_ERR rc;
5783
aPiecek8b0cc152021-05-31 16:40:31 +02005784 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005785 return LY_SUCCESS;
5786 }
5787
5788 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005789 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005790 return LY_EVALID;
5791 }
5792
Michal Vasko9f96a052020-03-10 09:41:45 +01005793 /* 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 +01005794 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005795 LY_CHECK_RET(rc);
5796
Michal Vasko6346ece2019-09-24 13:12:53 +02005797 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005798 set_init(&ret_set, set);
5799 for (i = 0; i < set->used; ++i) {
5800
5801 /* TREE DFS */
5802 start = set->val.nodes[i].node;
5803 for (elem = next = start; elem; elem = next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005804 rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005805 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005806 /* add matching node into result set */
5807 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5808 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5809 /* the node is a duplicate, we'll process it later in the set */
5810 goto skip_children;
5811 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005812 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005813 return rc;
5814 } else if (rc == LY_EINVAL) {
5815 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005816 }
5817
5818 /* TREE DFS NEXT ELEM */
5819 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005820 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005821 if (!next) {
5822skip_children:
5823 /* no children, so try siblings, but only if it's not the start,
5824 * that is considered to be the root and it's siblings are not traversed */
5825 if (elem != start) {
5826 next = elem->next;
5827 } else {
5828 break;
5829 }
5830 }
5831 while (!next) {
5832 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01005833 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005834 /* we are done, no next element to process */
5835 break;
5836 }
5837 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01005838 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005839 next = elem->next;
5840 }
5841 }
5842 }
5843
5844 /* make the temporary set the current one */
5845 ret_set.ctx_pos = set->ctx_pos;
5846 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005847 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005848 memcpy(set, &ret_set, sizeof *set);
5849
5850 return LY_SUCCESS;
5851}
5852
5853/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005854 * @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 +02005855 *
5856 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005857 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005858 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005859 * @param[in] options XPath options.
5860 * @return LY_ERR
5861 */
5862static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005863moveto_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 +02005864{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005865 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005866 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005867 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005868
aPiecek8b0cc152021-05-31 16:40:31 +02005869 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005870 return LY_SUCCESS;
5871 }
5872
5873 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005874 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005875 return LY_EVALID;
5876 }
5877
Michal Vasko03ff5a72019-09-11 13:49:33 +02005878 orig_used = set->used;
5879 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005880 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5881 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005882 continue;
5883 }
5884
5885 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005886 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005887 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005888 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005889 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005890
5891 /* TREE DFS */
5892 start = set->val.scnodes[i].scnode;
5893 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005894 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5895 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005896 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005897 }
5898
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005899 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005900 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005901 uint32_t idx;
5902
5903 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005904 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005905 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005906 /* we will process it later in the set */
5907 goto skip_children;
5908 }
5909 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005910 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005911 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005912 } else if (rc == LY_EINVAL) {
5913 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005914 }
5915
5916next_iter:
5917 /* TREE DFS NEXT ELEM */
5918 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005919 next = lysc_node_child(elem);
5920 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5921 next = next->next;
5922 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5923 next = next->next;
5924 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005925 if (!next) {
5926skip_children:
5927 /* no children, so try siblings, but only if it's not the start,
5928 * that is considered to be the root and it's siblings are not traversed */
5929 if (elem != start) {
5930 next = elem->next;
5931 } else {
5932 break;
5933 }
5934 }
5935 while (!next) {
5936 /* no siblings, go back through the parents */
5937 if (elem->parent == start) {
5938 /* we are done, no next element to process */
5939 break;
5940 }
5941 /* parent is already processed, go to its sibling */
5942 elem = elem->parent;
5943 next = elem->next;
5944 }
5945 }
5946 }
5947
5948 return LY_SUCCESS;
5949}
5950
5951/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005952 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005953 * Indirectly context position aware.
5954 *
5955 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005956 * @param[in] mod Matching metadata module, NULL for any.
5957 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02005958 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005959 * @return LY_ERR
5960 */
5961static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02005962moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005963{
Michal Vasko9f96a052020-03-10 09:41:45 +01005964 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005965
aPiecek8b0cc152021-05-31 16:40:31 +02005966 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005967 return LY_SUCCESS;
5968 }
5969
5970 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005971 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005972 return LY_EVALID;
5973 }
5974
Radek Krejci1deb5be2020-08-26 16:43:36 +02005975 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005976 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005977
5978 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5979 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005980 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005981 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005982
5983 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005984 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005985 continue;
5986 }
5987
Michal Vaskod3678892020-05-21 10:06:58 +02005988 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005989 /* match */
5990 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005991 set->val.meta[i].meta = sub;
5992 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005993 /* pos does not change */
5994 replaced = 1;
5995 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005996 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 +02005997 }
5998 ++i;
5999 }
6000 }
6001 }
6002
6003 if (!replaced) {
6004 /* no match */
6005 set_remove_node(set, i);
6006 }
6007 }
6008
6009 return LY_SUCCESS;
6010}
6011
6012/**
6013 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02006014 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006015 *
6016 * @param[in,out] set1 Set to use for the result.
6017 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006018 * @return LY_ERR
6019 */
6020static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006021moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006022{
6023 LY_ERR rc;
6024
Michal Vaskod3678892020-05-21 10:06:58 +02006025 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006026 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006027 return LY_EVALID;
6028 }
6029
6030 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02006031 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006032 return LY_SUCCESS;
6033 }
6034
Michal Vaskod3678892020-05-21 10:06:58 +02006035 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006036 memcpy(set1, set2, sizeof *set1);
6037 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02006038 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006039 return LY_SUCCESS;
6040 }
6041
6042 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006043 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006044
6045 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006046 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006047 LY_CHECK_RET(rc);
6048
6049 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006050 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006051
6052 return LY_SUCCESS;
6053}
6054
6055/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006056 * @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 +02006057 * Context position aware.
6058 *
6059 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006060 * @param[in] mod Matching metadata module, NULL for any.
6061 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006062 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006063 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6064 */
6065static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006066moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006067{
Michal Vasko9f96a052020-03-10 09:41:45 +01006068 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006069 struct lyxp_set *set_all_desc = NULL;
6070 LY_ERR rc;
6071
aPiecek8b0cc152021-05-31 16:40:31 +02006072 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006073 return LY_SUCCESS;
6074 }
6075
6076 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006077 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006078 return LY_EVALID;
6079 }
6080
Michal Vasko03ff5a72019-09-11 13:49:33 +02006081 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6082 * but it likely won't be used much, so it's a waste of time */
6083 /* copy the context */
6084 set_all_desc = set_copy(set);
6085 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006086 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006087 if (rc != LY_SUCCESS) {
6088 lyxp_set_free(set_all_desc);
6089 return rc;
6090 }
6091 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006092 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006093 if (rc != LY_SUCCESS) {
6094 lyxp_set_free(set_all_desc);
6095 return rc;
6096 }
6097 lyxp_set_free(set_all_desc);
6098
Radek Krejci1deb5be2020-08-26 16:43:36 +02006099 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006100 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006101
6102 /* only attributes of an elem can be in the result, skip all the rest,
6103 * we have all attributes qualified in lyd tree */
6104 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006105 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006106 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006107 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006108 continue;
6109 }
6110
Michal Vaskod3678892020-05-21 10:06:58 +02006111 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006112 /* match */
6113 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006114 set->val.meta[i].meta = sub;
6115 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006116 /* pos does not change */
6117 replaced = 1;
6118 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006119 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 +02006120 }
6121 ++i;
6122 }
6123 }
6124 }
6125
6126 if (!replaced) {
6127 /* no match */
6128 set_remove_node(set, i);
6129 }
6130 }
6131
6132 return LY_SUCCESS;
6133}
6134
6135/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006136 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6137 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006138 *
6139 * @param[in] parent Current parent.
6140 * @param[in] parent_pos Position of @p parent.
6141 * @param[in] parent_type Node type of @p parent.
6142 * @param[in,out] to_set Set to use.
6143 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006144 * @param[in] options XPath options.
6145 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6146 */
6147static LY_ERR
6148moveto_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 +02006149 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006150{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006151 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006152 LY_ERR rc;
6153
6154 switch (parent_type) {
6155 case LYXP_NODE_ROOT:
6156 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006157 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006158
Michal Vasko61ac2f62020-05-25 12:39:51 +02006159 /* add all top-level nodes as elements */
6160 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006161 break;
6162 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006163 /* add just the text node of this term element node */
6164 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006165 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6166 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6167 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006168 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006169 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006170
6171 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006172 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006173 break;
6174 default:
6175 LOGINT_RET(parent->schema->module->ctx);
6176 }
6177
Michal Vasko61ac2f62020-05-25 12:39:51 +02006178 /* add all top-level nodes as elements */
6179 LY_LIST_FOR(first, iter) {
6180 /* context check */
6181 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6182 continue;
6183 }
6184
6185 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006186 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006187 return LY_EINCOMPLETE;
6188 }
6189
6190 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6191 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6192
6193 /* also add all the children of this node, recursively */
6194 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6195 LY_CHECK_RET(rc);
6196 }
6197 }
6198
Michal Vasko03ff5a72019-09-11 13:49:33 +02006199 return LY_SUCCESS;
6200}
6201
6202/**
6203 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6204 * (or LYXP_SET_EMPTY). Context position aware.
6205 *
6206 * @param[in,out] set Set to use.
6207 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6208 * @param[in] options XPath options.
6209 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6210 */
6211static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006212moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006213{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006214 struct lyxp_set ret_set;
6215 LY_ERR rc;
6216
aPiecek8b0cc152021-05-31 16:40:31 +02006217 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006218 return LY_SUCCESS;
6219 }
6220
6221 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006222 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006223 return LY_EVALID;
6224 }
6225
6226 /* nothing to do */
6227 if (!all_desc) {
6228 return LY_SUCCESS;
6229 }
6230
Michal Vasko03ff5a72019-09-11 13:49:33 +02006231 /* add all the children, they get added recursively */
6232 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006233 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006234 /* copy the current node to tmp */
6235 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6236
6237 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006238 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006239 continue;
6240 }
6241
Michal Vasko03ff5a72019-09-11 13:49:33 +02006242 /* add all the children */
6243 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 +02006244 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006245 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006246 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006247 return rc;
6248 }
6249 }
6250
6251 /* use the temporary set as the current one */
6252 ret_set.ctx_pos = set->ctx_pos;
6253 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006254 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006255 memcpy(set, &ret_set, sizeof *set);
6256
6257 return LY_SUCCESS;
6258}
6259
6260/**
6261 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6262 * (or LYXP_SET_EMPTY).
6263 *
6264 * @param[in,out] set Set to use.
6265 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6266 * @param[in] options XPath options.
6267 * @return LY_ERR
6268 */
6269static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006270moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006271{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006272 uint32_t getnext_opts;
6273 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006274 const struct lysc_node *iter, *start_parent;
6275 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006276
aPiecek8b0cc152021-05-31 16:40:31 +02006277 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006278 return LY_SUCCESS;
6279 }
6280
6281 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006282 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006283 return LY_EVALID;
6284 }
6285
Michal Vasko519fd602020-05-26 12:17:39 +02006286 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006287 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006288 if (options & LYXP_SCNODE_OUTPUT) {
6289 getnext_opts |= LYS_GETNEXT_OUTPUT;
6290 }
6291
6292 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006293 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006294 if (!all_desc) {
6295 /* traverse the start node */
6296 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6297 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6298 }
6299 continue;
6300 }
6301
Radek Krejcif13b87b2020-12-01 22:02:17 +01006302 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6303 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006304 continue;
6305 }
6306
Michal Vasko519fd602020-05-26 12:17:39 +02006307 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006308 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006309 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006310 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006311 }
6312
Michal Vasko519fd602020-05-26 12:17:39 +02006313 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006314
Michal Vasko519fd602020-05-26 12:17:39 +02006315 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6316 /* it can actually be in any module, it's all <running> */
6317 mod_idx = 0;
6318 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6319 iter = NULL;
6320 /* module may not be implemented */
6321 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6322 /* context check */
6323 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6324 continue;
6325 }
6326
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006327 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006328 /* throw away the insert index, we want to consider that node again, recursively */
6329 }
6330 }
6331
6332 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6333 iter = NULL;
6334 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006335 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006336 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006337 continue;
6338 }
6339
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006340 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006341 }
6342 }
6343 }
6344
6345 return LY_SUCCESS;
6346}
6347
6348/**
6349 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6350 * (or LYXP_SET_EMPTY). Context position aware.
6351 *
6352 * @param[in] set Set to use.
6353 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6354 * @param[in] options XPath options.
6355 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6356 */
6357static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006358moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006359{
6360 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006361 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006362 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006363
aPiecek8b0cc152021-05-31 16:40:31 +02006364 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006365 return LY_SUCCESS;
6366 }
6367
6368 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006369 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006370 return LY_EVALID;
6371 }
6372
6373 if (all_desc) {
6374 /* <path>//.. == <path>//./.. */
6375 rc = moveto_self(set, 1, options);
6376 LY_CHECK_RET(rc);
6377 }
6378
Radek Krejci1deb5be2020-08-26 16:43:36 +02006379 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006380 node = set->val.nodes[i].node;
6381
6382 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9e685082021-01-29 14:49:09 +01006383 new_node = lyd_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006384 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6385 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006386 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6387 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006388 if (!new_node) {
6389 LOGINT_RET(set->ctx);
6390 }
6391 } else {
6392 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006393 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006394 continue;
6395 }
6396
Michal Vaskoa1424542019-11-14 16:08:52 +01006397 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006398 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 +02006399 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006400 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006401
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006402 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006403 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006404 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006405
Michal Vasko03ff5a72019-09-11 13:49:33 +02006406 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006407 /* 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 +02006408 new_type = LYXP_NODE_ELEM;
6409 }
6410
Michal Vasko03ff5a72019-09-11 13:49:33 +02006411 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006412 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006413 } else {
6414 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006415 }
6416 }
6417
Michal Vasko2caefc12019-11-14 16:07:56 +01006418 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006419 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006420
6421 return LY_SUCCESS;
6422}
6423
6424/**
6425 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6426 * (or LYXP_SET_EMPTY).
6427 *
6428 * @param[in] set Set to use.
6429 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6430 * @param[in] options XPath options.
6431 * @return LY_ERR
6432 */
6433static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006434moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006435{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006436 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006437 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006438 const struct lysc_node *node, *new_node;
6439 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006440
aPiecek8b0cc152021-05-31 16:40:31 +02006441 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006442 return LY_SUCCESS;
6443 }
6444
6445 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006446 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006447 return LY_EVALID;
6448 }
6449
6450 if (all_desc) {
6451 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006452 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006453 }
6454
Michal Vasko03ff5a72019-09-11 13:49:33 +02006455 orig_used = set->used;
6456 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006457 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6458 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006459 continue;
6460 }
6461
6462 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006463 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006464 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006465 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006466 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006467
6468 node = set->val.scnodes[i].scnode;
6469
6470 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006471 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006472 } else {
6473 /* root does not have a parent */
6474 continue;
6475 }
6476
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006477 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006478 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006479 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006480
Michal Vasko03ff5a72019-09-11 13:49:33 +02006481 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006482 /* 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 +02006483 new_type = LYXP_NODE_ELEM;
6484 }
6485
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006486 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6487 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006488 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006489 temp_ctx = 1;
6490 }
6491 }
6492
6493 if (temp_ctx) {
6494 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006495 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6496 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006497 }
6498 }
6499 }
6500
6501 return LY_SUCCESS;
6502}
6503
6504/**
6505 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6506 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6507 *
6508 * @param[in,out] set1 Set to use for the result.
6509 * @param[in] set2 Set acting as the second operand for @p op.
6510 * @param[in] op Comparison operator to process.
6511 * @param[in] options XPath options.
6512 * @return LY_ERR
6513 */
6514static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006515moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006516{
6517 /*
6518 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6519 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6520 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6521 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6522 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6523 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6524 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6525 *
6526 * '=' or '!='
6527 * BOOLEAN + BOOLEAN
6528 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6529 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6530 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6531 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6532 * NUMBER + NUMBER
6533 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6534 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6535 * STRING + STRING
6536 *
6537 * '<=', '<', '>=', '>'
6538 * NUMBER + NUMBER
6539 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6540 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6541 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6542 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6543 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6544 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6545 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6546 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6547 */
6548 struct lyxp_set iter1, iter2;
6549 int result;
6550 int64_t i;
6551 LY_ERR rc;
6552
Michal Vasko004d3152020-06-11 19:59:22 +02006553 memset(&iter1, 0, sizeof iter1);
6554 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006555
6556 /* iterative evaluation with node-sets */
6557 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6558 if (set1->type == LYXP_SET_NODE_SET) {
6559 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006560 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006561 switch (set2->type) {
6562 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006563 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006564 break;
6565 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006566 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006567 break;
6568 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006569 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006570 break;
6571 }
6572 LY_CHECK_RET(rc);
6573
Michal Vasko4c7763f2020-07-27 17:40:37 +02006574 /* canonize set2 */
6575 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6576
6577 /* compare recursively */
6578 rc = moveto_op_comp(&iter1, &iter2, op, options);
6579 lyxp_set_free_content(&iter2);
6580 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006581
6582 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006583 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006584 set_fill_boolean(set1, 1);
6585 return LY_SUCCESS;
6586 }
6587 }
6588 } else {
6589 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006590 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006591 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006592 case LYXP_SET_NUMBER:
6593 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6594 break;
6595 case LYXP_SET_BOOLEAN:
6596 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6597 break;
6598 default:
6599 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6600 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006601 }
6602 LY_CHECK_RET(rc);
6603
Michal Vasko4c7763f2020-07-27 17:40:37 +02006604 /* canonize set1 */
6605 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 +02006606
Michal Vasko4c7763f2020-07-27 17:40:37 +02006607 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006608 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006609 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006610 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006611
6612 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006613 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006614 set_fill_boolean(set1, 1);
6615 return LY_SUCCESS;
6616 }
6617 }
6618 }
6619
6620 /* false for all nodes */
6621 set_fill_boolean(set1, 0);
6622 return LY_SUCCESS;
6623 }
6624
6625 /* first convert properly */
6626 if ((op[0] == '=') || (op[0] == '!')) {
6627 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006628 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6629 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006630 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006631 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006632 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006633 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006634 LY_CHECK_RET(rc);
6635 } /* else we have 2 strings */
6636 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006637 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006638 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006639 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006640 LY_CHECK_RET(rc);
6641 }
6642
6643 assert(set1->type == set2->type);
6644
6645 /* compute result */
6646 if (op[0] == '=') {
6647 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006648 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006649 } else if (set1->type == LYXP_SET_NUMBER) {
6650 result = (set1->val.num == set2->val.num);
6651 } else {
6652 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006653 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006654 }
6655 } else if (op[0] == '!') {
6656 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006657 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006658 } else if (set1->type == LYXP_SET_NUMBER) {
6659 result = (set1->val.num != set2->val.num);
6660 } else {
6661 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006662 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006663 }
6664 } else {
6665 assert(set1->type == LYXP_SET_NUMBER);
6666 if (op[0] == '<') {
6667 if (op[1] == '=') {
6668 result = (set1->val.num <= set2->val.num);
6669 } else {
6670 result = (set1->val.num < set2->val.num);
6671 }
6672 } else {
6673 if (op[1] == '=') {
6674 result = (set1->val.num >= set2->val.num);
6675 } else {
6676 result = (set1->val.num > set2->val.num);
6677 }
6678 }
6679 }
6680
6681 /* assign result */
6682 if (result) {
6683 set_fill_boolean(set1, 1);
6684 } else {
6685 set_fill_boolean(set1, 0);
6686 }
6687
6688 return LY_SUCCESS;
6689}
6690
6691/**
6692 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6693 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6694 *
6695 * @param[in,out] set1 Set to use for the result.
6696 * @param[in] set2 Set acting as the second operand for @p op.
6697 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006698 * @return LY_ERR
6699 */
6700static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006701moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006702{
6703 LY_ERR rc;
6704
6705 /* unary '-' */
6706 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006707 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006708 LY_CHECK_RET(rc);
6709 set1->val.num *= -1;
6710 lyxp_set_free(set2);
6711 return LY_SUCCESS;
6712 }
6713
6714 assert(set1 && set2);
6715
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006716 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006717 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006718 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006719 LY_CHECK_RET(rc);
6720
6721 switch (op[0]) {
6722 /* '+' */
6723 case '+':
6724 set1->val.num += set2->val.num;
6725 break;
6726
6727 /* '-' */
6728 case '-':
6729 set1->val.num -= set2->val.num;
6730 break;
6731
6732 /* '*' */
6733 case '*':
6734 set1->val.num *= set2->val.num;
6735 break;
6736
6737 /* 'div' */
6738 case 'd':
6739 set1->val.num /= set2->val.num;
6740 break;
6741
6742 /* 'mod' */
6743 case 'm':
6744 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6745 break;
6746
6747 default:
6748 LOGINT_RET(set1->ctx);
6749 }
6750
6751 return LY_SUCCESS;
6752}
6753
6754/*
6755 * eval functions
6756 *
6757 * They execute a parsed XPath expression on some data subtree.
6758 */
6759
6760/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006761 * @brief Evaluate Predicate. Logs directly on error.
6762 *
Michal Vaskod3678892020-05-21 10:06:58 +02006763 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006764 *
6765 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006766 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02006767 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006768 * @param[in] options XPath options.
6769 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6770 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6771 */
6772static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006773eval_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 +02006774{
6775 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006776 uint16_t orig_exp;
6777 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006778 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006779 struct lyxp_set set2;
6780 struct lyd_node *orig_parent;
6781
6782 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02006783 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006784 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006785 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006786
aPiecek8b0cc152021-05-31 16:40:31 +02006787 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006788only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02006789 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006790 LY_CHECK_RET(rc);
6791 } else if (set->type == LYXP_SET_NODE_SET) {
6792 /* 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 +01006793 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006794
6795 /* empty set, nothing to evaluate */
6796 if (!set->used) {
6797 goto only_parse;
6798 }
6799
Michal Vasko004d3152020-06-11 19:59:22 +02006800 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006801 orig_pos = 0;
6802 orig_size = set->used;
6803 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006804 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006805 set_init(&set2, set);
6806 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6807 /* remember the node context position for position() and context size for last(),
6808 * predicates should always be evaluated with respect to the child axis (since we do
6809 * not support explicit axes) so we assign positions based on their parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006810 if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) {
6811 orig_parent = lyd_parent(set->val.nodes[i].node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006812 orig_pos = 1;
6813 } else {
6814 ++orig_pos;
6815 }
6816
6817 set2.ctx_pos = orig_pos;
6818 set2.ctx_size = orig_size;
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, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006822 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006823 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006824 return rc;
6825 }
6826
6827 /* number is a position */
6828 if (set2.type == LYXP_SET_NUMBER) {
6829 if ((long long)set2.val.num == orig_pos) {
6830 set2.val.num = 1;
6831 } else {
6832 set2.val.num = 0;
6833 }
6834 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006835 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006836
6837 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006838 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006839 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006840 }
6841 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006842 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006843
6844 } else if (set->type == LYXP_SET_SCNODE_SET) {
6845 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006846 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006847 /* there is a currently-valid node */
6848 break;
6849 }
6850 }
6851 /* empty set, nothing to evaluate */
6852 if (i == set->used) {
6853 goto only_parse;
6854 }
6855
Michal Vasko004d3152020-06-11 19:59:22 +02006856 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006857
Michal Vasko03ff5a72019-09-11 13:49:33 +02006858 /* set special in_ctx to all the valid snodes */
6859 pred_in_ctx = set_scnode_new_in_ctx(set);
6860
6861 /* use the valid snodes one-by-one */
6862 for (i = 0; i < set->used; ++i) {
6863 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6864 continue;
6865 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006866 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006867
Michal Vasko004d3152020-06-11 19:59:22 +02006868 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006869
Michal Vasko004d3152020-06-11 19:59:22 +02006870 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006871 LY_CHECK_RET(rc);
6872
6873 set->val.scnodes[i].in_ctx = pred_in_ctx;
6874 }
6875
6876 /* restore the state as it was before the predicate */
6877 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006878 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02006879 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006880 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006881 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006882 }
6883 }
6884
6885 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006886 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006887 set_fill_set(&set2, set);
6888
Michal Vasko004d3152020-06-11 19:59:22 +02006889 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006890 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006891 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006892 return rc;
6893 }
6894
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006895 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006896 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006897 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006898 }
Michal Vaskod3678892020-05-21 10:06:58 +02006899 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006900 }
6901
6902 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006903 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02006904 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006905 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006906 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006907
6908 return LY_SUCCESS;
6909}
6910
6911/**
Michal Vaskod3678892020-05-21 10:06:58 +02006912 * @brief Evaluate Literal. Logs directly on error.
6913 *
6914 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006915 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006916 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6917 */
6918static void
Michal Vasko40308e72020-10-20 16:38:40 +02006919eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006920{
6921 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006922 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006923 set_fill_string(set, "", 0);
6924 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006925 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 +02006926 }
6927 }
6928 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006929 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006930 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006931}
6932
6933/**
Michal Vasko004d3152020-06-11 19:59:22 +02006934 * @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 +02006935 *
Michal Vasko004d3152020-06-11 19:59:22 +02006936 * @param[in] exp Full parsed XPath expression.
6937 * @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 +02006938 * @param[in] ctx_node Found schema node as the context for the predicate.
6939 * @param[in] cur_mod Current module for the expression.
6940 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006941 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006942 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006943 * @param[out] predicates Parsed predicates.
6944 * @param[out] pred_type Type of @p predicates.
6945 * @return LY_SUCCESS on success,
6946 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006947 */
6948static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006949eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *ctx_node,
Radek Krejci8df109d2021-04-23 12:19:08 +02006950 const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_VALUE_FORMAT format, void *prefix_data,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006951 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006952{
Michal Vasko004d3152020-06-11 19:59:22 +02006953 LY_ERR ret = LY_SUCCESS;
6954 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006955 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006956 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006957 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006958 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006959
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006960 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006961
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006962 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006963 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006964 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006965 return LY_EINVAL;
6966 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006967 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 +02006968 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006969
Michal Vasko004d3152020-06-11 19:59:22 +02006970 /* learn where the predicates end */
6971 e_idx = *tok_idx;
6972 while (key_count) {
6973 /* '[' */
6974 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6975 return LY_EINVAL;
6976 }
6977 ++e_idx;
6978
Michal Vasko3354d272021-04-06 09:40:06 +02006979 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
6980 /* definitely not a key */
6981 return LY_EINVAL;
6982 }
6983
Michal Vasko004d3152020-06-11 19:59:22 +02006984 /* ']' */
6985 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6986 ++e_idx;
6987 }
6988 ++e_idx;
6989
6990 /* another presumably key predicate parsed */
6991 --key_count;
6992 }
Michal Vasko004d3152020-06-11 19:59:22 +02006993 } else {
6994 /* learn just where this single predicate ends */
6995 e_idx = *tok_idx;
6996
Michal Vaskod3678892020-05-21 10:06:58 +02006997 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006998 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6999 return LY_EINVAL;
7000 }
7001 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007002
Michal Vasko3354d272021-04-06 09:40:06 +02007003 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
7004 /* definitely not the value */
7005 return LY_EINVAL;
7006 }
7007
Michal Vaskod3678892020-05-21 10:06:58 +02007008 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007009 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7010 ++e_idx;
7011 }
7012 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007013 }
7014
Michal Vasko004d3152020-06-11 19:59:22 +02007015 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
7016 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
7017
7018 /* turn logging off */
7019 prev_lo = ly_log_options(0);
7020
7021 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007022 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
7023 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007024
7025 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007026 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
7027 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02007028 LY_CHECK_GOTO(ret, cleanup);
7029
7030 /* success, the predicate must include all the needed information for hash-based search */
7031 *tok_idx = e_idx;
7032
7033cleanup:
7034 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007035 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02007036 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02007037}
7038
7039/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007040 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7041 * data node(s) could be found using a single hash-based search.
7042 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007043 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007044 * @param[in] node Next context node to check.
7045 * @param[in] name Expected node name.
7046 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007047 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007048 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007049 * @param[in] format Prefix format.
7050 * @param[in,out] found Previously found node, is updated.
7051 * @return LY_SUCCESS on success,
7052 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7053 */
7054static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007055eval_name_test_with_predicate_get_scnode(const struct ly_ctx *ctx, const struct lyd_node *node, const char *name,
Radek Krejci8df109d2021-04-23 12:19:08 +02007056 uint16_t name_len, const struct lys_module *moveto_mod, enum lyxp_node_type root_type, LY_VALUE_FORMAT format,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007057 const struct lysc_node **found)
7058{
7059 const struct lysc_node *scnode;
7060 const struct lys_module *mod;
7061 uint32_t idx = 0;
7062
Radek Krejci8df109d2021-04-23 12:19:08 +02007063 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007064
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007065continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007066 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007067 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007068 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007069 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007070 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007071 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7072 if (scnode) {
7073 /* we have found a match */
7074 break;
7075 }
7076 }
7077
Michal Vasko7d1d0912020-10-16 08:38:30 +02007078 if (!scnode) {
7079 /* all modules searched */
7080 idx = 0;
7081 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007082 } else {
7083 /* search in top-level */
7084 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7085 }
7086 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007087 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007088 /* we must adjust the module to inherit the one from the context node */
7089 moveto_mod = node->schema->module;
7090 }
7091
7092 /* search in children, do not repeat the same search */
7093 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007094 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007095
7096 /* additional context check */
7097 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7098 scnode = NULL;
7099 }
7100
7101 if (scnode) {
7102 if (*found) {
7103 /* we found a schema node with the same name but at different level, give up, too complicated
7104 * (more hash-based searches would be required, not supported) */
7105 return LY_ENOT;
7106 } else {
7107 /* remember the found schema node and continue to make sure it can be used */
7108 *found = scnode;
7109 }
7110 }
7111
7112 if (idx) {
7113 /* continue searching all the following models */
7114 goto continue_search;
7115 }
7116
7117 return LY_SUCCESS;
7118}
7119
7120/**
Michal Vaskod3678892020-05-21 10:06:58 +02007121 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7122 *
7123 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7124 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7125 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7126 *
7127 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007128 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007129 * @param[in] attr_axis Whether to search attributes or standard nodes.
7130 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007131 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007132 * @param[in] options XPath options.
7133 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7134 */
7135static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007136eval_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 +02007137 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007138{
Michal Vaskod3678892020-05-21 10:06:58 +02007139 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007140 const char *ncname, *ncname_dict = NULL;
7141 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007142 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007143 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007144 struct ly_path_predicate *predicates = NULL;
7145 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007146 LY_ERR rc = LY_SUCCESS;
7147
aPiecek8b0cc152021-05-31 16:40:31 +02007148 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007149 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007150 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007151
aPiecek8b0cc152021-05-31 16:40:31 +02007152 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02007153 goto moveto;
7154 }
7155
Michal Vasko004d3152020-06-11 19:59:22 +02007156 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7157 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007158
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007159 if ((ncname[0] == '*') && (ncname_len == 1)) {
7160 /* all nodes will match */
7161 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007162 }
7163
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007164 /* parse (and skip) module name */
7165 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007166 LY_CHECK_GOTO(rc, cleanup);
7167
Radek Krejci8df109d2021-04-23 12:19:08 +02007168 if (((set->format == LY_VALUE_JSON) || moveto_mod) && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007169 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007170 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007171 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7172 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007173 /* check failed */
7174 scnode = NULL;
7175 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007176 }
7177 }
7178
Michal Vasko004d3152020-06-11 19:59:22 +02007179 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7180 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007181 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7182 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007183 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007184 scnode = NULL;
7185 }
7186 }
7187 }
7188
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007189 if (!scnode) {
7190 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007191 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007192 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007193 }
7194
7195moveto:
7196 /* move to the attribute(s), data node(s), or schema node(s) */
7197 if (attr_axis) {
aPiecek8b0cc152021-05-31 16:40:31 +02007198 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007199 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007200 } else {
7201 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007202 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007203 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007204 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007205 }
7206 LY_CHECK_GOTO(rc, cleanup);
7207 }
7208 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007209 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007210 int64_t i;
7211
Michal Vaskod3678892020-05-21 10:06:58 +02007212 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007213 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007214 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007215 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007216 }
7217 LY_CHECK_GOTO(rc, cleanup);
7218
7219 for (i = set->used - 1; i > -1; --i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007220 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vaskod3678892020-05-21 10:06:58 +02007221 break;
7222 }
7223 }
7224 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007225 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vasko90f12dc2020-12-03 14:20:42 +01007226 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".",
7227 ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007228 free(path);
7229 }
7230 } else {
7231 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007232 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007233 } else {
7234 if (scnode) {
7235 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007236 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007237 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007238 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007239 }
7240 }
7241 LY_CHECK_GOTO(rc, cleanup);
7242 }
7243 }
7244
7245 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007246 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7247 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007248 LY_CHECK_RET(rc);
7249 }
7250
7251cleanup:
aPiecek8b0cc152021-05-31 16:40:31 +02007252 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007253 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007254 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007255 }
Michal Vaskod3678892020-05-21 10:06:58 +02007256 return rc;
7257}
7258
7259/**
7260 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7261 *
7262 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7263 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7264 * [8] NodeType ::= 'text' | 'node'
7265 *
7266 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007267 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007268 * @param[in] attr_axis Whether to search attributes or standard nodes.
7269 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007270 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007271 * @param[in] options XPath options.
7272 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7273 */
7274static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007275eval_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 +02007276 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007277{
7278 LY_ERR rc;
7279
7280 /* TODO */
7281 (void)attr_axis;
7282 (void)all_desc;
7283
aPiecek8b0cc152021-05-31 16:40:31 +02007284 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007285 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007286 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007287 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
aPiecek8b0cc152021-05-31 16:40:31 +02007288 options |= LYXP_SKIP_EXPR;
Michal Vaskod3678892020-05-21 10:06:58 +02007289 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007290 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007291 rc = xpath_node(NULL, 0, set, options);
7292 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007293 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007294 rc = xpath_text(NULL, 0, set, options);
7295 }
7296 LY_CHECK_RET(rc);
7297 }
7298 }
aPiecek8b0cc152021-05-31 16:40:31 +02007299 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007300 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007301 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007302
7303 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007304 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007305 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
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
7309 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007310 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007311 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007312 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007313 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007314
7315 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007316 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7317 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007318 LY_CHECK_RET(rc);
7319 }
7320
7321 return LY_SUCCESS;
7322}
7323
7324/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007325 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7326 *
7327 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7328 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007329 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007330 *
7331 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007332 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007333 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007334 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007335 * @param[in] options XPath options.
7336 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7337 */
7338static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007339eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7340 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007341{
Radek Krejci857189e2020-09-01 13:26:36 +02007342 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007343 LY_ERR rc;
7344
7345 goto step;
7346 do {
7347 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007348 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007349 all_desc = 0;
7350 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007351 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007352 all_desc = 1;
7353 }
aPiecek8b0cc152021-05-31 16:40:31 +02007354 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007355 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007356 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007357
7358step:
Michal Vaskod3678892020-05-21 10:06:58 +02007359 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007360 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007361 attr_axis = 1;
7362
aPiecek8b0cc152021-05-31 16:40:31 +02007363 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007364 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007365 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007366 } else {
7367 attr_axis = 0;
7368 }
7369
Michal Vasko03ff5a72019-09-11 13:49:33 +02007370 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007371 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007372 case LYXP_TOKEN_DOT:
7373 /* evaluate '.' */
aPiecek8b0cc152021-05-31 16:40:31 +02007374 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007375 rc = moveto_scnode_self(set, all_desc, options);
7376 } else {
7377 rc = moveto_self(set, all_desc, options);
7378 }
7379 LY_CHECK_RET(rc);
7380 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007381 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007382 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007383 break;
7384
7385 case LYXP_TOKEN_DDOT:
7386 /* evaluate '..' */
aPiecek8b0cc152021-05-31 16:40:31 +02007387 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007388 rc = moveto_scnode_parent(set, all_desc, options);
7389 } else {
7390 rc = moveto_parent(set, all_desc, options);
7391 }
7392 LY_CHECK_RET(rc);
aPiecek8b0cc152021-05-31 16:40:31 +02007393 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007394 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007395 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007396 break;
7397
Michal Vasko03ff5a72019-09-11 13:49:33 +02007398 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007399 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007400 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007401 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007402 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007403
Michal Vaskod3678892020-05-21 10:06:58 +02007404 case LYXP_TOKEN_NODETYPE:
7405 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007406 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007407 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007408 break;
7409
7410 default:
aPiecek8b0cc152021-05-31 16:40:31 +02007411 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007412 }
Michal Vasko004d3152020-06-11 19:59:22 +02007413 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007414
7415 return LY_SUCCESS;
7416}
7417
7418/**
7419 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7420 *
7421 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7422 *
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.
aPiecek8b0cc152021-05-31 16:40:31 +02007425 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007426 * @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_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 +02007431{
Radek Krejci857189e2020-09-01 13:26:36 +02007432 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007433
aPiecek8b0cc152021-05-31 16:40:31 +02007434 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007435 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007436 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007437 }
7438
7439 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007440 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007441 /* evaluate '/' - deferred */
7442 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02007443 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007444 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007445 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007446
Michal Vasko004d3152020-06-11 19:59:22 +02007447 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007448 return LY_SUCCESS;
7449 }
Michal Vasko004d3152020-06-11 19:59:22 +02007450 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007451 case LYXP_TOKEN_DOT:
7452 case LYXP_TOKEN_DDOT:
7453 case LYXP_TOKEN_AT:
7454 case LYXP_TOKEN_NAMETEST:
7455 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007456 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007457 break;
7458 default:
7459 break;
7460 }
7461
Michal Vasko03ff5a72019-09-11 13:49:33 +02007462 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007463 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007464 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7465 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02007466 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007467 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007468 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007469
Michal Vaskob0099a92020-08-31 14:55:23 +02007470 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007471 }
7472
7473 return LY_SUCCESS;
7474}
7475
7476/**
7477 * @brief Evaluate FunctionCall. Logs directly on error.
7478 *
Michal Vaskod3678892020-05-21 10:06:58 +02007479 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007480 *
7481 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007482 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007483 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007484 * @param[in] options XPath options.
7485 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7486 */
7487static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007488eval_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 +02007489{
7490 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007491
Radek Krejci1deb5be2020-08-26 16:43:36 +02007492 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007493 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007494 struct lyxp_set **args = NULL, **args_aux;
7495
aPiecek8b0cc152021-05-31 16:40:31 +02007496 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007497 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007498 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007499 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007500 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007501 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007502 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007503 xpath_func = &xpath_sum;
7504 }
7505 break;
7506 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007507 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007508 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007509 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007510 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007511 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007512 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007513 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007514 xpath_func = &xpath_true;
7515 }
7516 break;
7517 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007518 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007519 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007520 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007521 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007522 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007523 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007524 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007525 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007526 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007527 xpath_func = &xpath_deref;
7528 }
7529 break;
7530 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007531 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007532 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007533 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007534 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007535 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007536 xpath_func = &xpath_string;
7537 }
7538 break;
7539 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007540 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007541 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007542 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007543 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007544 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545 xpath_func = &xpath_current;
7546 }
7547 break;
7548 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007549 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007550 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007551 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007552 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007553 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007554 xpath_func = &xpath_re_match;
7555 }
7556 break;
7557 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007558 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007559 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007560 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007561 xpath_func = &xpath_translate;
7562 }
7563 break;
7564 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007565 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007566 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007567 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007568 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007569 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570 xpath_func = &xpath_bit_is_set;
7571 }
7572 break;
7573 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007574 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007575 xpath_func = &xpath_starts_with;
7576 }
7577 break;
7578 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007579 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007580 xpath_func = &xpath_derived_from;
7581 }
7582 break;
7583 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007584 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007585 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007586 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007587 xpath_func = &xpath_string_length;
7588 }
7589 break;
7590 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007591 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007592 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007593 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007594 xpath_func = &xpath_substring_after;
7595 }
7596 break;
7597 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007598 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007599 xpath_func = &xpath_substring_before;
7600 }
7601 break;
7602 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007603 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007604 xpath_func = &xpath_derived_from_or_self;
7605 }
7606 break;
7607 }
7608
7609 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007610 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 +02007611 return LY_EVALID;
7612 }
7613 }
7614
aPiecek8b0cc152021-05-31 16:40:31 +02007615 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007616 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007617 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007618
7619 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007620 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007621 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007622 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007623 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007624
7625 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007626 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02007627 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007628 args = malloc(sizeof *args);
7629 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7630 arg_count = 1;
7631 args[0] = set_copy(set);
7632 if (!args[0]) {
7633 rc = LY_EMEM;
7634 goto cleanup;
7635 }
7636
Michal Vasko004d3152020-06-11 19:59:22 +02007637 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007638 LY_CHECK_GOTO(rc, cleanup);
7639 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007640 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007641 LY_CHECK_GOTO(rc, cleanup);
7642 }
7643 }
Michal Vasko004d3152020-06-11 19:59:22 +02007644 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02007645 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007646 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007647 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007648
aPiecek8b0cc152021-05-31 16:40:31 +02007649 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007650 ++arg_count;
7651 args_aux = realloc(args, arg_count * sizeof *args);
7652 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7653 args = args_aux;
7654 args[arg_count - 1] = set_copy(set);
7655 if (!args[arg_count - 1]) {
7656 rc = LY_EMEM;
7657 goto cleanup;
7658 }
7659
Michal Vasko004d3152020-06-11 19:59:22 +02007660 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007661 LY_CHECK_GOTO(rc, cleanup);
7662 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007663 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007664 LY_CHECK_GOTO(rc, cleanup);
7665 }
7666 }
7667
7668 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007669 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007670 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007671 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007672 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007673
aPiecek8b0cc152021-05-31 16:40:31 +02007674 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007675 /* evaluate function */
7676 rc = xpath_func(args, arg_count, set, options);
7677
7678 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007679 /* merge all nodes from arg evaluations */
7680 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007681 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007682 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007683 }
7684 }
7685 } else {
7686 rc = LY_SUCCESS;
7687 }
7688
7689cleanup:
7690 for (i = 0; i < arg_count; ++i) {
7691 lyxp_set_free(args[i]);
7692 }
7693 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007694 return rc;
7695}
7696
7697/**
7698 * @brief Evaluate Number. Logs directly on error.
7699 *
7700 * @param[in] ctx Context for errors.
7701 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007702 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007703 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7704 * @return LY_ERR
7705 */
7706static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007707eval_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 +02007708{
7709 long double num;
7710 char *endptr;
7711
7712 if (set) {
7713 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007714 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007715 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007716 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7717 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007718 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007719 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007720 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007721 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7722 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007723 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007724 return LY_EVALID;
7725 }
7726
7727 set_fill_number(set, num);
7728 }
7729
7730 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007731 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007732 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007733 return LY_SUCCESS;
7734}
7735
7736/**
7737 * @brief Evaluate PathExpr. Logs directly on error.
7738 *
Michal Vaskod3678892020-05-21 10:06:58 +02007739 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007740 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7741 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7742 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007743 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007744 *
7745 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007746 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007747 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007748 * @param[in] options XPath options.
7749 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7750 */
7751static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007752eval_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 +02007753{
Radek Krejci857189e2020-09-01 13:26:36 +02007754 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007755 LY_ERR rc;
7756
Michal Vasko004d3152020-06-11 19:59:22 +02007757 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007758 case LYXP_TOKEN_PAR1:
7759 /* '(' Expr ')' */
7760
7761 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02007762 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007763 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007764 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007765
7766 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007767 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007768 LY_CHECK_RET(rc);
7769
7770 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007771 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007772 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007773 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007774 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007775
7776 parent_pos_pred = 0;
7777 goto predicate;
7778
7779 case LYXP_TOKEN_DOT:
7780 case LYXP_TOKEN_DDOT:
7781 case LYXP_TOKEN_AT:
7782 case LYXP_TOKEN_NAMETEST:
7783 case LYXP_TOKEN_NODETYPE:
7784 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007785 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007786 LY_CHECK_RET(rc);
7787 break;
7788
7789 case LYXP_TOKEN_FUNCNAME:
7790 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02007791 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007792 LY_CHECK_RET(rc);
7793
7794 parent_pos_pred = 1;
7795 goto predicate;
7796
Michal Vasko3e48bf32020-06-01 08:39:07 +02007797 case LYXP_TOKEN_OPER_PATH:
7798 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007799 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007800 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007801 LY_CHECK_RET(rc);
7802 break;
7803
7804 case LYXP_TOKEN_LITERAL:
7805 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02007806 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7807 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007808 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007809 }
Michal Vasko004d3152020-06-11 19:59:22 +02007810 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007811 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007812 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007813 }
7814
7815 parent_pos_pred = 1;
7816 goto predicate;
7817
7818 case LYXP_TOKEN_NUMBER:
7819 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02007820 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7821 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007822 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007823 }
Michal Vasko004d3152020-06-11 19:59:22 +02007824 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007825 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007826 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007827 }
7828 LY_CHECK_RET(rc);
7829
7830 parent_pos_pred = 1;
7831 goto predicate;
7832
7833 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01007834 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 +02007835 return LY_EVALID;
7836 }
7837
7838 return LY_SUCCESS;
7839
7840predicate:
7841 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007842 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7843 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007844 LY_CHECK_RET(rc);
7845 }
7846
7847 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007848 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007849
7850 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007851 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007852 all_desc = 0;
7853 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007854 all_desc = 1;
7855 }
7856
aPiecek8b0cc152021-05-31 16:40:31 +02007857 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007858 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007859 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007860
Michal Vasko004d3152020-06-11 19:59:22 +02007861 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007862 LY_CHECK_RET(rc);
7863 }
7864
7865 return LY_SUCCESS;
7866}
7867
7868/**
7869 * @brief Evaluate UnionExpr. Logs directly on error.
7870 *
Michal Vaskod3678892020-05-21 10:06:58 +02007871 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007872 *
7873 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007874 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007875 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007876 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007877 * @param[in] options XPath options.
7878 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7879 */
7880static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007881eval_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 +02007882{
7883 LY_ERR rc = LY_SUCCESS;
7884 struct lyxp_set orig_set, set2;
7885 uint16_t i;
7886
7887 assert(repeat);
7888
7889 set_init(&orig_set, set);
7890 set_init(&set2, set);
7891
7892 set_fill_set(&orig_set, set);
7893
Michal Vasko004d3152020-06-11 19:59:22 +02007894 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007895 LY_CHECK_GOTO(rc, cleanup);
7896
7897 /* ('|' PathExpr)* */
7898 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007899 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02007900 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
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
aPiecek8b0cc152021-05-31 16:40:31 +02007904 if (options & LYXP_SKIP_EXPR) {
7905 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007906 LY_CHECK_GOTO(rc, cleanup);
7907 continue;
7908 }
7909
7910 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007911 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007912 LY_CHECK_GOTO(rc, cleanup);
7913
7914 /* eval */
7915 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007916 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007917 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007918 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007919 LY_CHECK_GOTO(rc, cleanup);
7920 }
7921 }
7922
7923cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007924 lyxp_set_free_content(&orig_set);
7925 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007926 return rc;
7927}
7928
7929/**
7930 * @brief Evaluate UnaryExpr. Logs directly on error.
7931 *
Michal Vaskod3678892020-05-21 10:06:58 +02007932 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007933 *
7934 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007935 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007936 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007937 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007938 * @param[in] options XPath options.
7939 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7940 */
7941static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007942eval_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 +02007943{
7944 LY_ERR rc;
7945 uint16_t this_op, i;
7946
7947 assert(repeat);
7948
7949 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007950 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007951 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007952 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 +02007953
aPiecek8b0cc152021-05-31 16:40:31 +02007954 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007955 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007956 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007957 }
7958
Michal Vasko004d3152020-06-11 19:59:22 +02007959 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007960 LY_CHECK_RET(rc);
7961
aPiecek8b0cc152021-05-31 16:40:31 +02007962 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007963 if (options & LYXP_SCNODE_ALL) {
7964 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7965 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007966 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007967 LY_CHECK_RET(rc);
7968 }
7969 }
7970
7971 return LY_SUCCESS;
7972}
7973
7974/**
7975 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7976 *
Michal Vaskod3678892020-05-21 10:06:58 +02007977 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007978 * | MultiplicativeExpr '*' UnaryExpr
7979 * | MultiplicativeExpr 'div' UnaryExpr
7980 * | MultiplicativeExpr 'mod' UnaryExpr
7981 *
7982 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007983 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007984 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007985 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007986 * @param[in] options XPath options.
7987 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7988 */
7989static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007990eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
7991 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007992{
7993 LY_ERR rc;
7994 uint16_t this_op;
7995 struct lyxp_set orig_set, set2;
7996 uint16_t i;
7997
7998 assert(repeat);
7999
8000 set_init(&orig_set, set);
8001 set_init(&set2, set);
8002
8003 set_fill_set(&orig_set, set);
8004
Michal Vasko004d3152020-06-11 19:59:22 +02008005 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008006 LY_CHECK_GOTO(rc, cleanup);
8007
8008 /* ('*' / 'div' / 'mod' UnaryExpr)* */
8009 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008010 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008011
Michal Vasko004d3152020-06-11 19:59:22 +02008012 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02008013 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008014 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008015 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008016
aPiecek8b0cc152021-05-31 16:40:31 +02008017 if (options & LYXP_SKIP_EXPR) {
8018 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008019 LY_CHECK_GOTO(rc, cleanup);
8020 continue;
8021 }
8022
8023 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008024 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008025 LY_CHECK_GOTO(rc, cleanup);
8026
8027 /* eval */
8028 if (options & LYXP_SCNODE_ALL) {
8029 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008030 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008031 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008032 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008033 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008034 LY_CHECK_GOTO(rc, cleanup);
8035 }
8036 }
8037
8038cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008039 lyxp_set_free_content(&orig_set);
8040 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008041 return rc;
8042}
8043
8044/**
8045 * @brief Evaluate AdditiveExpr. Logs directly on error.
8046 *
Michal Vaskod3678892020-05-21 10:06:58 +02008047 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008048 * | AdditiveExpr '+' MultiplicativeExpr
8049 * | AdditiveExpr '-' MultiplicativeExpr
8050 *
8051 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008052 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008053 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008054 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008055 * @param[in] options XPath options.
8056 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8057 */
8058static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008059eval_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 +02008060{
8061 LY_ERR rc;
8062 uint16_t this_op;
8063 struct lyxp_set orig_set, set2;
8064 uint16_t i;
8065
8066 assert(repeat);
8067
8068 set_init(&orig_set, set);
8069 set_init(&set2, set);
8070
8071 set_fill_set(&orig_set, set);
8072
Michal Vasko004d3152020-06-11 19:59:22 +02008073 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008074 LY_CHECK_GOTO(rc, cleanup);
8075
8076 /* ('+' / '-' MultiplicativeExpr)* */
8077 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008078 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008079
Michal Vasko004d3152020-06-11 19:59:22 +02008080 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008081 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008082 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008083 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008084
aPiecek8b0cc152021-05-31 16:40:31 +02008085 if (options & LYXP_SKIP_EXPR) {
8086 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008087 LY_CHECK_GOTO(rc, cleanup);
8088 continue;
8089 }
8090
8091 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008092 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008093 LY_CHECK_GOTO(rc, cleanup);
8094
8095 /* eval */
8096 if (options & LYXP_SCNODE_ALL) {
8097 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008098 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008099 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008100 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008101 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008102 LY_CHECK_GOTO(rc, cleanup);
8103 }
8104 }
8105
8106cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008107 lyxp_set_free_content(&orig_set);
8108 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008109 return rc;
8110}
8111
8112/**
8113 * @brief Evaluate RelationalExpr. Logs directly on error.
8114 *
Michal Vaskod3678892020-05-21 10:06:58 +02008115 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008116 * | RelationalExpr '<' AdditiveExpr
8117 * | RelationalExpr '>' AdditiveExpr
8118 * | RelationalExpr '<=' AdditiveExpr
8119 * | RelationalExpr '>=' AdditiveExpr
8120 *
8121 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008122 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008123 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008124 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008125 * @param[in] options XPath options.
8126 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8127 */
8128static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008129eval_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 +02008130{
8131 LY_ERR rc;
8132 uint16_t this_op;
8133 struct lyxp_set orig_set, set2;
8134 uint16_t i;
8135
8136 assert(repeat);
8137
8138 set_init(&orig_set, set);
8139 set_init(&set2, set);
8140
8141 set_fill_set(&orig_set, set);
8142
Michal Vasko004d3152020-06-11 19:59:22 +02008143 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008144 LY_CHECK_GOTO(rc, cleanup);
8145
8146 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8147 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008148 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008149
Michal Vasko004d3152020-06-11 19:59:22 +02008150 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02008151 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008152 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008153 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008154
aPiecek8b0cc152021-05-31 16:40:31 +02008155 if (options & LYXP_SKIP_EXPR) {
8156 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008157 LY_CHECK_GOTO(rc, cleanup);
8158 continue;
8159 }
8160
8161 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008162 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008163 LY_CHECK_GOTO(rc, cleanup);
8164
8165 /* eval */
8166 if (options & LYXP_SCNODE_ALL) {
8167 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008168 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008169 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008170 } else {
8171 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8172 LY_CHECK_GOTO(rc, cleanup);
8173 }
8174 }
8175
8176cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008177 lyxp_set_free_content(&orig_set);
8178 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008179 return rc;
8180}
8181
8182/**
8183 * @brief Evaluate EqualityExpr. Logs directly on error.
8184 *
Michal Vaskod3678892020-05-21 10:06:58 +02008185 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008186 * | EqualityExpr '!=' RelationalExpr
8187 *
8188 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008189 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008190 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008191 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008192 * @param[in] options XPath options.
8193 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8194 */
8195static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008196eval_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 +02008197{
8198 LY_ERR rc;
8199 uint16_t this_op;
8200 struct lyxp_set orig_set, set2;
8201 uint16_t i;
8202
8203 assert(repeat);
8204
8205 set_init(&orig_set, set);
8206 set_init(&set2, set);
8207
8208 set_fill_set(&orig_set, set);
8209
Michal Vasko004d3152020-06-11 19:59:22 +02008210 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008211 LY_CHECK_GOTO(rc, cleanup);
8212
8213 /* ('=' / '!=' RelationalExpr)* */
8214 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008215 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008216
Michal Vasko004d3152020-06-11 19:59:22 +02008217 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02008218 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008219 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008220 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008221
aPiecek8b0cc152021-05-31 16:40:31 +02008222 if (options & LYXP_SKIP_EXPR) {
8223 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008224 LY_CHECK_GOTO(rc, cleanup);
8225 continue;
8226 }
8227
8228 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008229 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008230 LY_CHECK_GOTO(rc, cleanup);
8231
8232 /* eval */
8233 if (options & LYXP_SCNODE_ALL) {
8234 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008235 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8236 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008237 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008238 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008239 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008240 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8241 LY_CHECK_GOTO(rc, cleanup);
8242 }
8243 }
8244
8245cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008246 lyxp_set_free_content(&orig_set);
8247 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008248 return rc;
8249}
8250
8251/**
8252 * @brief Evaluate AndExpr. Logs directly on error.
8253 *
Michal Vaskod3678892020-05-21 10:06:58 +02008254 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008255 *
8256 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008257 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008258 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008259 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008260 * @param[in] options XPath options.
8261 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8262 */
8263static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008264eval_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 +02008265{
8266 LY_ERR rc;
8267 struct lyxp_set orig_set, set2;
8268 uint16_t i;
8269
8270 assert(repeat);
8271
8272 set_init(&orig_set, set);
8273 set_init(&set2, set);
8274
8275 set_fill_set(&orig_set, set);
8276
Michal Vasko004d3152020-06-11 19:59:22 +02008277 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008278 LY_CHECK_GOTO(rc, cleanup);
8279
8280 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008281 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008282 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008283 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008284 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008285 }
8286
8287 /* ('and' EqualityExpr)* */
8288 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008289 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008290 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008291 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008292 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008293
8294 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008295 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8296 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008297 LY_CHECK_GOTO(rc, cleanup);
8298 continue;
8299 }
8300
8301 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008302 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008303 LY_CHECK_GOTO(rc, cleanup);
8304
8305 /* eval - just get boolean value actually */
8306 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008307 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008308 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008309 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008310 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008311 set_fill_set(set, &set2);
8312 }
8313 }
8314
8315cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008316 lyxp_set_free_content(&orig_set);
8317 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008318 return rc;
8319}
8320
8321/**
8322 * @brief Evaluate OrExpr. Logs directly on error.
8323 *
Michal Vaskod3678892020-05-21 10:06:58 +02008324 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008325 *
8326 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008327 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008328 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008329 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008330 * @param[in] options XPath options.
8331 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8332 */
8333static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008334eval_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 +02008335{
8336 LY_ERR rc;
8337 struct lyxp_set orig_set, set2;
8338 uint16_t i;
8339
8340 assert(repeat);
8341
8342 set_init(&orig_set, set);
8343 set_init(&set2, set);
8344
8345 set_fill_set(&orig_set, set);
8346
Michal Vasko004d3152020-06-11 19:59:22 +02008347 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008348 LY_CHECK_GOTO(rc, cleanup);
8349
8350 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008351 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008352 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008353 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008354 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008355 }
8356
8357 /* ('or' AndExpr)* */
8358 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008359 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008360 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008361 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008362 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008363
8364 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008365 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8366 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008367 LY_CHECK_GOTO(rc, cleanup);
8368 continue;
8369 }
8370
8371 set_fill_set(&set2, &orig_set);
8372 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8373 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008374 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008375 LY_CHECK_GOTO(rc, cleanup);
8376
8377 /* eval - just get boolean value actually */
8378 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008379 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008380 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008381 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008382 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008383 set_fill_set(set, &set2);
8384 }
8385 }
8386
8387cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008388 lyxp_set_free_content(&orig_set);
8389 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008390 return rc;
8391}
8392
8393/**
Michal Vasko004d3152020-06-11 19:59:22 +02008394 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008395 *
8396 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008397 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008398 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02008399 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008400 * @param[in] options XPath options.
8401 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8402 */
8403static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008404eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8405 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008406{
8407 uint16_t i, count;
8408 enum lyxp_expr_type next_etype;
8409 LY_ERR rc;
8410
8411 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008412 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008413 next_etype = LYXP_EXPR_NONE;
8414 } else {
8415 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008416 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008417
8418 /* select one-priority lower because etype expression called us */
8419 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008420 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008421 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008422 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008423 } else {
8424 next_etype = LYXP_EXPR_NONE;
8425 }
8426 }
8427
8428 /* decide what expression are we parsing based on the repeat */
8429 switch (next_etype) {
8430 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008431 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008432 break;
8433 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008434 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008435 break;
8436 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008437 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008438 break;
8439 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008440 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008441 break;
8442 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008443 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008444 break;
8445 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008446 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008447 break;
8448 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008449 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008450 break;
8451 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008452 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008453 break;
8454 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008455 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008456 break;
8457 default:
8458 LOGINT_RET(set->ctx);
8459 }
8460
8461 return rc;
8462}
8463
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008464/**
8465 * @brief Get root type.
8466 *
8467 * @param[in] ctx_node Context node.
8468 * @param[in] ctx_scnode Schema context node.
8469 * @param[in] options XPath options.
8470 * @return Root type.
8471 */
8472static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008473lyxp_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 +01008474{
Michal Vasko6b26e742020-07-17 15:02:10 +02008475 const struct lysc_node *op;
8476
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008477 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008478 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008479 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008480
8481 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008482 /* general root that can access everything */
8483 return LYXP_NODE_ROOT;
8484 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8485 /* root context node can access only config data (because we said so, it is unspecified) */
8486 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008487 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008488 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008489 }
8490
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008491 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008492 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008493 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008494
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008495 if (op || !(options & LYXP_SCHEMA)) {
8496 /* general root that can access everything */
8497 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05008498 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008499 /* root context node can access only config data (because we said so, it is unspecified) */
8500 return LYXP_NODE_ROOT_CONFIG;
8501 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008502 return LYXP_NODE_ROOT;
8503}
8504
Michal Vasko03ff5a72019-09-11 13:49:33 +02008505LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008506lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008507 LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node, const struct lyd_node *tree,
Michal Vasko400e9672021-01-11 13:39:17 +01008508 struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008509{
Michal Vasko004d3152020-06-11 19:59:22 +02008510 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008511 LY_ERR rc;
8512
Michal Vasko400e9672021-01-11 13:39:17 +01008513 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008514 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008515 LOGARG(NULL, "Current module must be set if schema format is used.");
8516 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008517 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008518
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008519 if (tree) {
8520 /* adjust the pointer to be the first top-level sibling */
8521 while (tree->parent) {
8522 tree = lyd_parent(tree);
8523 }
8524 tree = lyd_first_sibling(tree);
8525 }
8526
Michal Vasko03ff5a72019-09-11 13:49:33 +02008527 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008528 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008529 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008530 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8531 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8532
Michal Vasko400e9672021-01-11 13:39:17 +01008533 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008534 set->cur_node = ctx_node;
8535 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008536 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8537 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008538 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008539 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008540 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008541 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008542
Radek Krejciddace2c2021-01-08 11:30:56 +01008543 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008544
Michal Vasko03ff5a72019-09-11 13:49:33 +02008545 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008546 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008547 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008548 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008549 }
8550
Radek Krejciddace2c2021-01-08 11:30:56 +01008551 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008552 return rc;
8553}
8554
8555#if 0
8556
8557/* full xml printing of set elements, not used currently */
8558
8559void
8560lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8561{
8562 uint32_t i;
8563 char *str_num;
8564 struct lyout out;
8565
8566 memset(&out, 0, sizeof out);
8567
8568 out.type = LYOUT_STREAM;
8569 out.method.f = f;
8570
8571 switch (set->type) {
8572 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008573 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008574 break;
8575 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008576 ly_print_(&out, "Boolean XPath set:\n");
8577 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008578 break;
8579 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008580 ly_print_(&out, "String XPath set:\n");
8581 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008582 break;
8583 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008584 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008585
8586 if (isnan(set->value.num)) {
8587 str_num = strdup("NaN");
8588 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8589 str_num = strdup("0");
8590 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8591 str_num = strdup("Infinity");
8592 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8593 str_num = strdup("-Infinity");
8594 } else if ((long long)set->value.num == set->value.num) {
8595 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8596 str_num = NULL;
8597 }
8598 } else {
8599 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8600 str_num = NULL;
8601 }
8602 }
8603 if (!str_num) {
8604 LOGMEM;
8605 return;
8606 }
Michal Vasko5233e962020-08-14 14:26:20 +02008607 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008608 free(str_num);
8609 break;
8610 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008611 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008612
8613 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008614 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008615 switch (set->node_type[i]) {
8616 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008617 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008618 break;
8619 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008620 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008621 break;
8622 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008623 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008624 break;
8625 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008626 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008627 break;
8628 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008629 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008630 break;
8631 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008632 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008633 break;
8634 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008635 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008636 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008637 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008638 break;
8639 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008640 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 +02008641 break;
8642 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008643 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 +02008644 break;
8645 }
8646 }
8647 break;
8648 }
8649}
8650
8651#endif
8652
8653LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008654lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008655{
8656 long double num;
8657 char *str;
8658 LY_ERR rc;
8659
8660 if (!set || (set->type == target)) {
8661 return LY_SUCCESS;
8662 }
8663
8664 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008665 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008666
8667 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008668 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008669 return LY_EINVAL;
8670 }
8671
8672 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008673 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008674 switch (set->type) {
8675 case LYXP_SET_NUMBER:
8676 if (isnan(set->val.num)) {
8677 set->val.str = strdup("NaN");
8678 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8679 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8680 set->val.str = strdup("0");
8681 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8682 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8683 set->val.str = strdup("Infinity");
8684 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8685 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8686 set->val.str = strdup("-Infinity");
8687 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8688 } else if ((long long)set->val.num == set->val.num) {
8689 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8690 LOGMEM_RET(set->ctx);
8691 }
8692 set->val.str = str;
8693 } else {
8694 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8695 LOGMEM_RET(set->ctx);
8696 }
8697 set->val.str = str;
8698 }
8699 break;
8700 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008701 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008702 set->val.str = strdup("true");
8703 } else {
8704 set->val.str = strdup("false");
8705 }
8706 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8707 break;
8708 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008709 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008710 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008711
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008712 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008713 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008714 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008715 set->val.str = str;
8716 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008717 default:
8718 LOGINT_RET(set->ctx);
8719 }
8720 set->type = LYXP_SET_STRING;
8721 }
8722
8723 /* to NUMBER */
8724 if (target == LYXP_SET_NUMBER) {
8725 switch (set->type) {
8726 case LYXP_SET_STRING:
8727 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008728 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008729 set->val.num = num;
8730 break;
8731 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008732 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008733 set->val.num = 1;
8734 } else {
8735 set->val.num = 0;
8736 }
8737 break;
8738 default:
8739 LOGINT_RET(set->ctx);
8740 }
8741 set->type = LYXP_SET_NUMBER;
8742 }
8743
8744 /* to BOOLEAN */
8745 if (target == LYXP_SET_BOOLEAN) {
8746 switch (set->type) {
8747 case LYXP_SET_NUMBER:
8748 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008749 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008750 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008751 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008752 }
8753 break;
8754 case LYXP_SET_STRING:
8755 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008756 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008757 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008758 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008759 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008760 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008761 }
8762 break;
8763 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008764 if (set->used) {
8765 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008766 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008767 } else {
8768 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008769 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008770 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008771 break;
8772 default:
8773 LOGINT_RET(set->ctx);
8774 }
8775 set->type = LYXP_SET_BOOLEAN;
8776 }
8777
Michal Vasko03ff5a72019-09-11 13:49:33 +02008778 return LY_SUCCESS;
8779}
8780
8781LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008782lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008783 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
Michal Vasko400e9672021-01-11 13:39:17 +01008784 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008785{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008786 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008787 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008788
Michal Vasko400e9672021-01-11 13:39:17 +01008789 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008790 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008791 LOGARG(NULL, "Current module must be set if schema format is used.");
8792 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008793 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008794
8795 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008796 memset(set, 0, sizeof *set);
8797 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008798 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8799 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 +01008800 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008801
Michal Vasko400e9672021-01-11 13:39:17 +01008802 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008803 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008804 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008805 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8806 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008807 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008808 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008809 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008810
Radek Krejciddace2c2021-01-08 11:30:56 +01008811 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008812
Michal Vasko03ff5a72019-09-11 13:49:33 +02008813 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01008814 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
8815
Radek Krejciddace2c2021-01-08 11:30:56 +01008816 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008817 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008818}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008819
8820API const char *
8821lyxp_get_expr(const struct lyxp_expr *path)
8822{
8823 if (!path) {
8824 return NULL;
8825 }
8826
8827 return path->expr;
8828}