blob: 5683ed44d111dabac27b96ab61f4568c22e15474 [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 */
Radek Krejcib1646a92018-11-02 16:08:26 +010015
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010017
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010019#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010023#include <stdio.h>
24#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010025#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010026
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020028#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010032#include "out.h"
Radek Krejci7931b192020-06-25 17:05:03 +020033#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020034#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020035#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020037#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "tree.h"
Radek Krejci77114102021-03-10 15:21:57 +010039#include "tree_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020040#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010041#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020042#include "tree_schema_internal.h"
43#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020044
aPiecekbf968d92021-05-27 14:35:05 +020045static 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 +020046static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype,
47 struct lyxp_set *set, uint32_t options);
Michal Vasko93923692021-05-07 15:28:02 +020048static LY_ERR moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
49 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod);
Michal Vasko03ff5a72019-09-11 13:49:33 +020050
51/**
52 * @brief Print the type of an XPath \p set.
53 *
54 * @param[in] set Set to use.
55 * @return Set type string.
56 */
57static const char *
58print_set_type(struct lyxp_set *set)
59{
60 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020061 case LYXP_SET_NODE_SET:
62 return "node set";
63 case LYXP_SET_SCNODE_SET:
64 return "schema node set";
65 case LYXP_SET_BOOLEAN:
66 return "boolean";
67 case LYXP_SET_NUMBER:
68 return "number";
69 case LYXP_SET_STRING:
70 return "string";
71 }
72
73 return NULL;
74}
75
Michal Vasko24cddf82020-06-01 08:17:01 +020076const char *
77lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020078{
79 switch (tok) {
80 case LYXP_TOKEN_PAR1:
81 return "(";
82 case LYXP_TOKEN_PAR2:
83 return ")";
84 case LYXP_TOKEN_BRACK1:
85 return "[";
86 case LYXP_TOKEN_BRACK2:
87 return "]";
88 case LYXP_TOKEN_DOT:
89 return ".";
90 case LYXP_TOKEN_DDOT:
91 return "..";
92 case LYXP_TOKEN_AT:
93 return "@";
94 case LYXP_TOKEN_COMMA:
95 return ",";
96 case LYXP_TOKEN_NAMETEST:
97 return "NameTest";
98 case LYXP_TOKEN_NODETYPE:
99 return "NodeType";
100 case LYXP_TOKEN_FUNCNAME:
101 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200102 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200103 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200104 case LYXP_TOKEN_OPER_EQUAL:
105 return "Operator(Equal)";
106 case LYXP_TOKEN_OPER_NEQUAL:
107 return "Operator(Non-equal)";
108 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200109 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200110 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200111 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200112 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200113 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200114 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200115 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200116 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200117 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200118 case LYXP_TOKEN_LITERAL:
119 return "Literal";
120 case LYXP_TOKEN_NUMBER:
121 return "Number";
122 default:
123 LOGINT(NULL);
124 return "";
125 }
126}
127
128/**
129 * @brief Print the whole expression \p exp to debug output.
130 *
131 * @param[in] exp Expression to use.
132 */
133static void
Michal Vasko40308e72020-10-20 16:38:40 +0200134print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200135{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100136#define MSG_BUFFER_SIZE 128
137 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100138 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200139
Radek Krejci52b6d512020-10-12 12:33:17 +0200140 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200141 return;
142 }
143
144 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
145 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200146 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200147 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100148 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200149 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
150 for (j = 1; exp->repeat[i][j]; ++j) {
151 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
152 }
153 strcat(tmp, ")");
154 }
155 LOGDBG(LY_LDGXPATH, tmp);
156 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100157#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200158}
159
160#ifndef NDEBUG
161
162/**
163 * @brief Print XPath set content to debug output.
164 *
165 * @param[in] set Set to print.
166 */
167static void
168print_set_debug(struct lyxp_set *set)
169{
170 uint32_t i;
171 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200172 struct lyxp_set_node *item;
173 struct lyxp_set_scnode *sitem;
174
Radek Krejci52b6d512020-10-12 12:33:17 +0200175 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200176 return;
177 }
178
179 switch (set->type) {
180 case LYXP_SET_NODE_SET:
181 LOGDBG(LY_LDGXPATH, "set NODE SET:");
182 for (i = 0; i < set->used; ++i) {
183 item = &set->val.nodes[i];
184
185 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100186 case LYXP_NODE_NONE:
187 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
188 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200189 case LYXP_NODE_ROOT:
190 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
191 break;
192 case LYXP_NODE_ROOT_CONFIG:
193 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
194 break;
195 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100196 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200197 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200198 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100199 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200200 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200201 item->node->schema->name, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200202 } else {
203 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
204 }
205 break;
206 case LYXP_NODE_TEXT:
207 if (item->node->schema->nodetype & LYS_ANYDATA) {
208 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200209 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200210 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200211 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 +0200212 }
213 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100214 case LYXP_NODE_META:
215 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
Michal Vasko69730152020-10-09 16:30:07 +0200216 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200217 break;
218 }
219 }
220 break;
221
222 case LYXP_SET_SCNODE_SET:
223 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
224 for (i = 0; i < set->used; ++i) {
225 sitem = &set->val.scnodes[i];
226
227 switch (sitem->type) {
228 case LYXP_NODE_ROOT:
229 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
230 break;
231 case LYXP_NODE_ROOT_CONFIG:
232 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
233 break;
234 case LYXP_NODE_ELEM:
235 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
236 break;
237 default:
238 LOGINT(NULL);
239 break;
240 }
241 }
242 break;
243
Michal Vasko03ff5a72019-09-11 13:49:33 +0200244 case LYXP_SET_BOOLEAN:
245 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200246 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200247 break;
248
249 case LYXP_SET_STRING:
250 LOGDBG(LY_LDGXPATH, "set STRING");
251 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
252 break;
253
254 case LYXP_SET_NUMBER:
255 LOGDBG(LY_LDGXPATH, "set NUMBER");
256
257 if (isnan(set->val.num)) {
258 str = strdup("NaN");
259 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
260 str = strdup("0");
261 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
262 str = strdup("Infinity");
263 } else if (isinf(set->val.num) && signbit(set->val.num)) {
264 str = strdup("-Infinity");
265 } else if ((long long)set->val.num == set->val.num) {
266 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
267 str = NULL;
268 }
269 } else {
270 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
271 str = NULL;
272 }
273 }
274 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
275
276 LOGDBG(LY_LDGXPATH, "\t%s", str);
277 free(str);
278 }
279}
280
281#endif
282
283/**
284 * @brief Realloc the string \p str.
285 *
286 * @param[in] ctx libyang context for logging.
287 * @param[in] needed How much free space is required.
288 * @param[in,out] str Pointer to the string to use.
289 * @param[in,out] used Used bytes in \p str.
290 * @param[in,out] size Allocated bytes in \p str.
291 * @return LY_ERR
292 */
293static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100294cast_string_realloc(const struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200295{
296 if (*size - *used < needed) {
297 do {
298 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
299 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
300 return LY_EINVAL;
301 }
302 *size += LYXP_STRING_CAST_SIZE_STEP;
303 } while (*size - *used < needed);
304 *str = ly_realloc(*str, *size * sizeof(char));
305 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
306 }
307
308 return LY_SUCCESS;
309}
310
311/**
312 * @brief Cast nodes recursively to one string @p str.
313 *
314 * @param[in] node Node to cast.
315 * @param[in] fake_cont Whether to put the data into a "fake" container.
316 * @param[in] root_type Type of the XPath root.
317 * @param[in] indent Current indent.
318 * @param[in,out] str Resulting string.
319 * @param[in,out] used Used bytes in @p str.
320 * @param[in,out] size Allocated bytes in @p str.
321 * @return LY_ERR
322 */
323static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200324cast_string_recursive(const struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
Radek Krejci0f969882020-08-21 16:56:47 +0200325 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200326{
Radek Krejci7f769d72020-07-11 23:13:56 +0200327 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200328 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200329 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200330 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200331 struct lyd_node_any *any;
332 LY_ERR rc;
333
334 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
335 return LY_SUCCESS;
336 }
337
338 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200339 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200340 LY_CHECK_RET(rc);
341 strcpy(*str + (*used - 1), "\n");
342 ++(*used);
343
344 ++indent;
345 }
346
347 switch (node->schema->nodetype) {
348 case LYS_CONTAINER:
349 case LYS_LIST:
350 case LYS_RPC:
351 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200352 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200353 LY_CHECK_RET(rc);
354 strcpy(*str + (*used - 1), "\n");
355 ++(*used);
356
Radek Krejcia1c1e542020-09-29 16:06:52 +0200357 for (child = lyd_child(node); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200358 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
359 LY_CHECK_RET(rc);
360 }
361
362 break;
363
364 case LYS_LEAF:
365 case LYS_LEAFLIST:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200366 value_str = lyd_get_value(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200367
368 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200369 LY_CHECK_RET(cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200370 memset(*str + (*used - 1), ' ', indent * 2);
371 *used += indent * 2;
372
373 /* print value */
374 if (*used == 1) {
375 sprintf(*str + (*used - 1), "%s", value_str);
376 *used += strlen(value_str);
377 } else {
378 sprintf(*str + (*used - 1), "%s\n", value_str);
379 *used += strlen(value_str) + 1;
380 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200381
382 break;
383
384 case LYS_ANYXML:
385 case LYS_ANYDATA:
386 any = (struct lyd_node_any *)node;
387 if (!(void *)any->value.tree) {
388 /* no content */
389 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200390 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200391 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200392 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100393
Michal Vasko60ea6352020-06-29 13:39:39 +0200394 if (any->value_type == LYD_ANYDATA_LYB) {
395 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200396 if (lyd_parse_data_mem((struct ly_ctx *)LYD_CTX(node), any->value.mem, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200397 /* successfully parsed */
398 free(any->value.mem);
399 any->value.tree = tree;
400 any->value_type = LYD_ANYDATA_DATATREE;
401 }
Radek Krejci7931b192020-06-25 17:05:03 +0200402 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200403 }
404
Michal Vasko03ff5a72019-09-11 13:49:33 +0200405 switch (any->value_type) {
406 case LYD_ANYDATA_STRING:
407 case LYD_ANYDATA_XML:
408 case LYD_ANYDATA_JSON:
409 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200410 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200411 break;
412 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200413 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200414 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200415 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100416 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200417 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200418 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200419 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200420 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200421 }
422 }
423
424 line = strtok_r(buf, "\n", &ptr);
425 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200426 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200427 if (rc != LY_SUCCESS) {
428 free(buf);
429 return rc;
430 }
431 memset(*str + (*used - 1), ' ', indent * 2);
432 *used += indent * 2;
433
434 strcpy(*str + (*used - 1), line);
435 *used += strlen(line);
436
437 strcpy(*str + (*used - 1), "\n");
438 *used += 1;
439 } while ((line = strtok_r(NULL, "\n", &ptr)));
440
441 free(buf);
442 break;
443
444 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200445 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200446 }
447
448 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200449 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200450 LY_CHECK_RET(rc);
451 strcpy(*str + (*used - 1), "\n");
452 ++(*used);
453
454 --indent;
455 }
456
457 return LY_SUCCESS;
458}
459
460/**
461 * @brief Cast an element into a string.
462 *
463 * @param[in] node Node to cast.
464 * @param[in] fake_cont Whether to put the data into a "fake" container.
465 * @param[in] root_type Type of the XPath root.
466 * @param[out] str Element cast to dynamically-allocated string.
467 * @return LY_ERR
468 */
469static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200470cast_string_elem(struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200471{
472 uint16_t used, size;
473 LY_ERR rc;
474
475 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200476 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200477 (*str)[0] = '\0';
478 used = 1;
479 size = LYXP_STRING_CAST_SIZE_START;
480
481 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
482 if (rc != LY_SUCCESS) {
483 free(*str);
484 return rc;
485 }
486
487 if (size > used) {
488 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200489 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200490 }
491 return LY_SUCCESS;
492}
493
494/**
495 * @brief Cast a LYXP_SET_NODE_SET set into a string.
496 * Context position aware.
497 *
498 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200499 * @param[out] str Cast dynamically-allocated string.
500 * @return LY_ERR
501 */
502static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100503cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200504{
Michal Vaskoaa677062021-03-09 13:52:53 +0100505 if (!set->used) {
506 *str = strdup("");
507 if (!*str) {
508 LOGMEM_RET(set->ctx);
509 }
510 return LY_SUCCESS;
511 }
512
Michal Vasko03ff5a72019-09-11 13:49:33 +0200513 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100514 case LYXP_NODE_NONE:
515 /* invalid */
516 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200517 case LYXP_NODE_ROOT:
518 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100519 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200520 case LYXP_NODE_ELEM:
521 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100522 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100523 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200524 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200525 if (!*str) {
526 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200527 }
528 return LY_SUCCESS;
529 }
530
531 LOGINT_RET(set->ctx);
532}
533
534/**
535 * @brief Cast a string into an XPath number.
536 *
537 * @param[in] str String to use.
538 * @return Cast number.
539 */
540static long double
541cast_string_to_number(const char *str)
542{
543 long double num;
544 char *ptr;
545
546 errno = 0;
547 num = strtold(str, &ptr);
548 if (errno || *ptr) {
549 num = NAN;
550 }
551 return num;
552}
553
554/**
555 * @brief Callback for checking value equality.
556 *
Michal Vasko62524a92021-02-26 10:08:50 +0100557 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200558 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200559 * @param[in] val1_p First value.
560 * @param[in] val2_p Second value.
561 * @param[in] mod Whether hash table is being modified.
562 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200563 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200564 */
Radek Krejci857189e2020-09-01 13:26:36 +0200565static ly_bool
566set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200567{
568 struct lyxp_set_hash_node *val1, *val2;
569
570 val1 = (struct lyxp_set_hash_node *)val1_p;
571 val2 = (struct lyxp_set_hash_node *)val2_p;
572
573 if ((val1->node == val2->node) && (val1->type == val2->type)) {
574 return 1;
575 }
576
577 return 0;
578}
579
580/**
581 * @brief Insert node and its hash into set.
582 *
583 * @param[in] set et to insert to.
584 * @param[in] node Node with hash.
585 * @param[in] type Node type.
586 */
587static void
588set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
589{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200590 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200591 uint32_t i, hash;
592 struct lyxp_set_hash_node hnode;
593
594 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
595 /* create hash table and add all the nodes */
596 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
597 for (i = 0; i < set->used; ++i) {
598 hnode.node = set->val.nodes[i].node;
599 hnode.type = set->val.nodes[i].type;
600
601 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
602 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
603 hash = dict_hash_multi(hash, NULL, 0);
604
605 r = lyht_insert(set->ht, &hnode, hash, NULL);
606 assert(!r);
607 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200608
Michal Vasko9d6befd2019-12-11 14:56:56 +0100609 if (hnode.node == node) {
610 /* it was just added, do not add it twice */
611 node = NULL;
612 }
613 }
614 }
615
616 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200617 /* add the new node into hash table */
618 hnode.node = node;
619 hnode.type = type;
620
621 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
622 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
623 hash = dict_hash_multi(hash, NULL, 0);
624
625 r = lyht_insert(set->ht, &hnode, hash, NULL);
626 assert(!r);
627 (void)r;
628 }
629}
630
631/**
632 * @brief Remove node and its hash from set.
633 *
634 * @param[in] set Set to remove from.
635 * @param[in] node Node to remove.
636 * @param[in] type Node type.
637 */
638static void
639set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
640{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200641 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200642 struct lyxp_set_hash_node hnode;
643 uint32_t hash;
644
645 if (set->ht) {
646 hnode.node = node;
647 hnode.type = type;
648
649 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
650 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
651 hash = dict_hash_multi(hash, NULL, 0);
652
653 r = lyht_remove(set->ht, &hnode, hash);
654 assert(!r);
655 (void)r;
656
657 if (!set->ht->used) {
658 lyht_free(set->ht);
659 set->ht = NULL;
660 }
661 }
662}
663
664/**
665 * @brief Check whether node is in set based on its hash.
666 *
667 * @param[in] set Set to search in.
668 * @param[in] node Node to search for.
669 * @param[in] type Node type.
670 * @param[in] skip_idx Index in @p set to skip.
671 * @return LY_ERR
672 */
673static LY_ERR
674set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
675{
676 struct lyxp_set_hash_node hnode, *match_p;
677 uint32_t hash;
678
679 hnode.node = node;
680 hnode.type = type;
681
682 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
683 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
684 hash = dict_hash_multi(hash, NULL, 0);
685
686 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
687 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
688 /* we found it on the index that should be skipped, find another */
689 hnode = *match_p;
690 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
691 /* none other found */
692 return LY_SUCCESS;
693 }
694 }
695
696 return LY_EEXIST;
697 }
698
699 /* not found */
700 return LY_SUCCESS;
701}
702
Michal Vaskod3678892020-05-21 10:06:58 +0200703void
704lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200705{
706 if (!set) {
707 return;
708 }
709
710 if (set->type == LYXP_SET_NODE_SET) {
711 free(set->val.nodes);
712 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200713 } else if (set->type == LYXP_SET_SCNODE_SET) {
714 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200715 lyht_free(set->ht);
716 } else {
717 if (set->type == LYXP_SET_STRING) {
718 free(set->val.str);
719 }
720 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200721 }
Michal Vaskod3678892020-05-21 10:06:58 +0200722
723 set->val.nodes = NULL;
724 set->used = 0;
725 set->size = 0;
726 set->ht = NULL;
727 set->ctx_pos = 0;
aPiecek748da732021-06-01 09:56:43 +0200728 set->ctx_size = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200729}
730
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100731/**
732 * @brief Free dynamically-allocated set.
733 *
734 * @param[in] set Set to free.
735 */
736static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200737lyxp_set_free(struct lyxp_set *set)
738{
739 if (!set) {
740 return;
741 }
742
Michal Vaskod3678892020-05-21 10:06:58 +0200743 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200744 free(set);
745}
746
747/**
748 * @brief Initialize set context.
749 *
750 * @param[in] new Set to initialize.
751 * @param[in] set Arbitrary initialized set.
752 */
753static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200754set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200755{
756 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200757 if (set) {
758 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200759 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100760 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200761 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100762 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200763 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200764 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200765 new->prefix_data = set->prefix_data;
Michal Vasko02a77382019-09-12 11:47:35 +0200766 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200767}
768
769/**
770 * @brief Create a deep copy of a set.
771 *
772 * @param[in] set Set to copy.
773 * @return Copy of @p set.
774 */
775static struct lyxp_set *
776set_copy(struct lyxp_set *set)
777{
778 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100779 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200780
781 if (!set) {
782 return NULL;
783 }
784
785 ret = malloc(sizeof *ret);
786 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
787 set_init(ret, set);
788
789 if (set->type == LYXP_SET_SCNODE_SET) {
790 ret->type = set->type;
791
792 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100793 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
794 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200795 uint32_t idx;
796 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
797 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100798 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200799 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200800 lyxp_set_free(ret);
801 return NULL;
802 }
Michal Vaskoba716542019-12-16 10:01:58 +0100803 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200804 }
805 }
806 } else if (set->type == LYXP_SET_NODE_SET) {
807 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200808 if (set->used) {
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 } else {
813 ret->val.nodes = NULL;
814 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200815
816 ret->used = ret->size = set->used;
817 ret->ctx_pos = set->ctx_pos;
818 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100819 if (set->ht) {
820 ret->ht = lyht_dup(set->ht);
821 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200822 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200823 memcpy(ret, set, sizeof *ret);
824 if (set->type == LYXP_SET_STRING) {
825 ret->val.str = strdup(set->val.str);
826 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
827 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200828 }
829
830 return ret;
831}
832
833/**
834 * @brief Fill XPath set with a string. Any current data are disposed of.
835 *
836 * @param[in] set Set to fill.
837 * @param[in] string String to fill into \p set.
838 * @param[in] str_len Length of \p string. 0 is a valid value!
839 */
840static void
841set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
842{
Michal Vaskod3678892020-05-21 10:06:58 +0200843 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200844
845 set->type = LYXP_SET_STRING;
846 if ((str_len == 0) && (string[0] != '\0')) {
847 string = "";
848 }
849 set->val.str = strndup(string, str_len);
850}
851
852/**
853 * @brief Fill XPath set with a number. Any current data are disposed of.
854 *
855 * @param[in] set Set to fill.
856 * @param[in] number Number to fill into \p set.
857 */
858static void
859set_fill_number(struct lyxp_set *set, long double number)
860{
Michal Vaskod3678892020-05-21 10:06:58 +0200861 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200862
863 set->type = LYXP_SET_NUMBER;
864 set->val.num = number;
865}
866
867/**
868 * @brief Fill XPath set with a boolean. Any current data are disposed of.
869 *
870 * @param[in] set Set to fill.
871 * @param[in] boolean Boolean to fill into \p set.
872 */
873static void
Radek Krejci857189e2020-09-01 13:26:36 +0200874set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200875{
Michal Vaskod3678892020-05-21 10:06:58 +0200876 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200877
878 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200879 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200880}
881
882/**
883 * @brief Fill XPath set with the value from another set (deep assign).
884 * Any current data are disposed of.
885 *
886 * @param[in] trg Set to fill.
887 * @param[in] src Source set to copy into \p trg.
888 */
889static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200890set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200891{
892 if (!trg || !src) {
893 return;
894 }
895
896 if (trg->type == LYXP_SET_NODE_SET) {
897 free(trg->val.nodes);
898 } else if (trg->type == LYXP_SET_STRING) {
899 free(trg->val.str);
900 }
901 set_init(trg, src);
902
903 if (src->type == LYXP_SET_SCNODE_SET) {
904 trg->type = LYXP_SET_SCNODE_SET;
905 trg->used = src->used;
906 trg->size = src->used;
907
Michal Vasko08e9b112021-06-11 15:41:17 +0200908 if (trg->size) {
909 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
910 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
911 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
912 } else {
913 trg->val.scnodes = NULL;
914 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200915 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200916 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200917 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200918 set_fill_number(trg, src->val.num);
919 } else if (src->type == LYXP_SET_STRING) {
920 set_fill_string(trg, src->val.str, strlen(src->val.str));
921 } else {
922 if (trg->type == LYXP_SET_NODE_SET) {
923 free(trg->val.nodes);
924 } else if (trg->type == LYXP_SET_STRING) {
925 free(trg->val.str);
926 }
927
Michal Vaskod3678892020-05-21 10:06:58 +0200928 assert(src->type == LYXP_SET_NODE_SET);
929
930 trg->type = LYXP_SET_NODE_SET;
931 trg->used = src->used;
932 trg->size = src->used;
933 trg->ctx_pos = src->ctx_pos;
934 trg->ctx_size = src->ctx_size;
935
Michal Vasko08e9b112021-06-11 15:41:17 +0200936 if (trg->size) {
937 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
938 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
939 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
940 } else {
941 trg->val.nodes = NULL;
942 }
Michal Vaskod3678892020-05-21 10:06:58 +0200943 if (src->ht) {
944 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200945 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200946 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200947 }
948 }
949}
950
951/**
952 * @brief Clear context of all schema nodes.
953 *
954 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +0200955 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200956 */
957static void
Michal Vasko1a09b212021-05-06 13:00:10 +0200958set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200959{
960 uint32_t i;
961
962 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100963 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +0200964 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100965 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
966 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200967 }
968 }
969}
970
971/**
972 * @brief Remove a node from a set. Removing last node changes
973 * set into LYXP_SET_EMPTY. Context position aware.
974 *
975 * @param[in] set Set to use.
976 * @param[in] idx Index from @p set of the node to be removed.
977 */
978static void
979set_remove_node(struct lyxp_set *set, uint32_t idx)
980{
981 assert(set && (set->type == LYXP_SET_NODE_SET));
982 assert(idx < set->used);
983
984 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
985
986 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +0200987 if (idx < set->used) {
988 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
989 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +0200990 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200991 }
992}
993
994/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100995 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200996 *
997 * @param[in] set Set to use.
998 * @param[in] idx Index from @p set of the node to be removed.
999 */
1000static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001001set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001002{
1003 assert(set && (set->type == LYXP_SET_NODE_SET));
1004 assert(idx < set->used);
1005
Michal Vasko2caefc12019-11-14 16:07:56 +01001006 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1007 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1008 }
1009 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001010}
1011
1012/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001013 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001014 * set into LYXP_SET_EMPTY. Context position aware.
1015 *
1016 * @param[in] set Set to consolidate.
1017 */
1018static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001019set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001020{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001021 uint32_t i, orig_used, end = 0;
1022 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001023
Michal Vaskod3678892020-05-21 10:06:58 +02001024 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001025
1026 orig_used = set->used;
1027 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001028 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001029 start = -1;
1030 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001031 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001032 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001033 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001034 end = i;
1035 ++i;
1036 break;
1037 }
1038
1039 ++i;
1040 if (i == orig_used) {
1041 end = i;
1042 }
1043 } while (i < orig_used);
1044
1045 if (start > -1) {
1046 /* move the whole chunk of valid nodes together */
1047 if (set->used != (unsigned)start) {
1048 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1049 }
1050 set->used += end - start;
1051 }
1052 }
Michal Vasko57eab132019-09-24 11:46:26 +02001053}
1054
1055/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001056 * @brief Check for duplicates in a node set.
1057 *
1058 * @param[in] set Set to check.
1059 * @param[in] node Node to look for in @p set.
1060 * @param[in] node_type Type of @p node.
1061 * @param[in] skip_idx Index from @p set to skip.
1062 * @return LY_ERR
1063 */
1064static LY_ERR
1065set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1066{
1067 uint32_t i;
1068
Michal Vasko2caefc12019-11-14 16:07:56 +01001069 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001070 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1071 }
1072
1073 for (i = 0; i < set->used; ++i) {
1074 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1075 continue;
1076 }
1077
1078 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1079 return LY_EEXIST;
1080 }
1081 }
1082
1083 return LY_SUCCESS;
1084}
1085
Radek Krejci857189e2020-09-01 13:26:36 +02001086ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001087lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1088 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001089{
1090 uint32_t i;
1091
1092 for (i = 0; i < set->used; ++i) {
1093 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1094 continue;
1095 }
1096
1097 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001098 if (index_p) {
1099 *index_p = i;
1100 }
1101 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001102 }
1103 }
1104
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001105 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001106}
1107
Michal Vaskoecd62de2019-11-13 12:35:11 +01001108void
1109lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001110{
1111 uint32_t orig_used, i, j;
1112
Michal Vaskod3678892020-05-21 10:06:58 +02001113 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001114
Michal Vaskod3678892020-05-21 10:06:58 +02001115 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001116 return;
1117 }
1118
Michal Vaskod3678892020-05-21 10:06:58 +02001119 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001120 memcpy(set1, set2, sizeof *set1);
1121 return;
1122 }
1123
1124 if (set1->used + set2->used > set1->size) {
1125 set1->size = set1->used + set2->used;
1126 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1127 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1128 }
1129
1130 orig_used = set1->used;
1131
1132 for (i = 0; i < set2->used; ++i) {
1133 for (j = 0; j < orig_used; ++j) {
1134 /* detect duplicities */
1135 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1136 break;
1137 }
1138 }
1139
Michal Vasko0587bce2020-12-10 12:19:21 +01001140 if (j < orig_used) {
1141 /* node is there, but update its status if needed */
1142 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1143 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001144 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1145 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1146 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001147 }
1148 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001149 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1150 ++set1->used;
1151 }
1152 }
1153
Michal Vaskod3678892020-05-21 10:06:58 +02001154 lyxp_set_free_content(set2);
1155 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001156}
1157
1158/**
1159 * @brief Insert a node into a set. Context position aware.
1160 *
1161 * @param[in] set Set to use.
1162 * @param[in] node Node to insert to @p set.
1163 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1164 * @param[in] node_type Node type of @p node.
1165 * @param[in] idx Index in @p set to insert into.
1166 */
1167static void
1168set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1169{
Michal Vaskod3678892020-05-21 10:06:58 +02001170 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001171
Michal Vaskod3678892020-05-21 10:06:58 +02001172 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001173 /* first item */
1174 if (idx) {
1175 /* no real harm done, but it is a bug */
1176 LOGINT(set->ctx);
1177 idx = 0;
1178 }
1179 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1180 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1181 set->type = LYXP_SET_NODE_SET;
1182 set->used = 0;
1183 set->size = LYXP_SET_SIZE_START;
1184 set->ctx_pos = 1;
1185 set->ctx_size = 1;
1186 set->ht = NULL;
1187 } else {
1188 /* not an empty set */
1189 if (set->used == set->size) {
1190
1191 /* set is full */
1192 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1193 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1194 set->size += LYXP_SET_SIZE_STEP;
1195 }
1196
1197 if (idx > set->used) {
1198 LOGINT(set->ctx);
1199 idx = set->used;
1200 }
1201
1202 /* make space for the new node */
1203 if (idx < set->used) {
1204 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1205 }
1206 }
1207
1208 /* finally assign the value */
1209 set->val.nodes[idx].node = (struct lyd_node *)node;
1210 set->val.nodes[idx].type = node_type;
1211 set->val.nodes[idx].pos = pos;
1212 ++set->used;
1213
Michal Vasko2caefc12019-11-14 16:07:56 +01001214 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1215 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1216 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001217}
1218
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001219LY_ERR
1220lyxp_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 +02001221{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001222 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001223
1224 assert(set->type == LYXP_SET_SCNODE_SET);
1225
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001226 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001227 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001228 } else {
1229 if (set->used == set->size) {
1230 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 +02001231 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001232 set->size += LYXP_SET_SIZE_STEP;
1233 }
1234
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001235 index = set->used;
1236 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1237 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001238 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001239 ++set->used;
1240 }
1241
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001242 if (index_p) {
1243 *index_p = index;
1244 }
1245
1246 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001247}
1248
1249/**
1250 * @brief Replace a node in a set with another. Context position aware.
1251 *
1252 * @param[in] set Set to use.
1253 * @param[in] node Node to insert to @p set.
1254 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1255 * @param[in] node_type Node type of @p node.
1256 * @param[in] idx Index in @p set of the node to replace.
1257 */
1258static void
1259set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1260{
1261 assert(set && (idx < set->used));
1262
Michal Vasko2caefc12019-11-14 16:07:56 +01001263 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1264 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1265 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001266 set->val.nodes[idx].node = (struct lyd_node *)node;
1267 set->val.nodes[idx].type = node_type;
1268 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001269 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1270 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1271 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001272}
1273
1274/**
1275 * @brief Set all nodes with ctx 1 to a new unique context value.
1276 *
1277 * @param[in] set Set to modify.
1278 * @return New context value.
1279 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001280static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001281set_scnode_new_in_ctx(struct lyxp_set *set)
1282{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001283 uint32_t i;
1284 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001285
1286 assert(set->type == LYXP_SET_SCNODE_SET);
1287
Radek Krejcif13b87b2020-12-01 22:02:17 +01001288 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001289retry:
1290 for (i = 0; i < set->used; ++i) {
1291 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1292 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1293 goto retry;
1294 }
1295 }
1296 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001297 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001298 set->val.scnodes[i].in_ctx = ret_ctx;
1299 }
1300 }
1301
1302 return ret_ctx;
1303}
1304
1305/**
1306 * @brief Get unique @p node position in the data.
1307 *
1308 * @param[in] node Node to find.
1309 * @param[in] node_type Node type of @p node.
1310 * @param[in] root Root node.
1311 * @param[in] root_type Type of the XPath @p root node.
1312 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1313 * be used to increase efficiency and start the DFS from this node.
1314 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1315 * @return Node position.
1316 */
1317static uint32_t
1318get_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 +02001319 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001320{
Michal Vasko56daf732020-08-10 10:57:18 +02001321 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001322 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001323 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001324
1325 assert(prev && prev_pos && !root->prev->next);
1326
1327 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1328 return 0;
1329 }
1330
1331 if (*prev) {
1332 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001333 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001334 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001335 goto dfs_search;
1336 }
1337
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001338 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001339 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001340dfs_search:
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001341 LYD_TREE_DFS_continue = 0;
1342
Michal Vasko56daf732020-08-10 10:57:18 +02001343 if (*prev && !elem) {
1344 /* resume previous DFS */
1345 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1346 LYD_TREE_DFS_continue = 0;
1347 }
1348
Michal Vasko03ff5a72019-09-11 13:49:33 +02001349 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001350 /* skip */
1351 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001352 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001353 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001354 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001355 break;
1356 }
Michal Vasko56daf732020-08-10 10:57:18 +02001357 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001358 }
Michal Vasko56daf732020-08-10 10:57:18 +02001359
1360 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001361 }
1362
1363 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001364 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001365 break;
1366 }
1367 }
1368
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001369 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001370 if (!(*prev)) {
1371 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001372 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001373 return 0;
1374 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001375 /* start the search again from the beginning */
1376 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001377
Michal Vasko56daf732020-08-10 10:57:18 +02001378 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001379 pos = 1;
1380 goto dfs_search;
1381 }
1382 }
1383
1384 /* remember the last found node for next time */
1385 *prev = node;
1386 *prev_pos = pos;
1387
1388 return pos;
1389}
1390
1391/**
1392 * @brief Assign (fill) missing node positions.
1393 *
1394 * @param[in] set Set to fill positions in.
1395 * @param[in] root Context root node.
1396 * @param[in] root_type Context root type.
1397 * @return LY_ERR
1398 */
1399static LY_ERR
1400set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1401{
1402 const struct lyd_node *prev = NULL, *tmp_node;
1403 uint32_t i, tmp_pos = 0;
1404
1405 for (i = 0; i < set->used; ++i) {
1406 if (!set->val.nodes[i].pos) {
1407 tmp_node = NULL;
1408 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001409 case LYXP_NODE_META:
1410 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001411 if (!tmp_node) {
1412 LOGINT_RET(root->schema->module->ctx);
1413 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001414 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001415 case LYXP_NODE_ELEM:
1416 case LYXP_NODE_TEXT:
1417 if (!tmp_node) {
1418 tmp_node = set->val.nodes[i].node;
1419 }
1420 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1421 break;
1422 default:
1423 /* all roots have position 0 */
1424 break;
1425 }
1426 }
1427 }
1428
1429 return LY_SUCCESS;
1430}
1431
1432/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001433 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001434 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001435 * @param[in] meta Metadata to use.
1436 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001437 */
1438static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001439get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001440{
1441 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001442 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001443
Michal Vasko9f96a052020-03-10 09:41:45 +01001444 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001445 ++pos;
1446 }
1447
Michal Vasko9f96a052020-03-10 09:41:45 +01001448 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001449 return pos;
1450}
1451
1452/**
1453 * @brief Compare 2 nodes in respect to XPath document order.
1454 *
1455 * @param[in] item1 1st node.
1456 * @param[in] item2 2nd node.
1457 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1458 */
1459static int
1460set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1461{
Michal Vasko9f96a052020-03-10 09:41:45 +01001462 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001463
1464 if (item1->pos < item2->pos) {
1465 return -1;
1466 }
1467
1468 if (item1->pos > item2->pos) {
1469 return 1;
1470 }
1471
1472 /* node positions are equal, the fun case */
1473
1474 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1475 /* special case since text nodes are actually saved as their parents */
1476 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1477 if (item1->type == LYXP_NODE_ELEM) {
1478 assert(item2->type == LYXP_NODE_TEXT);
1479 return -1;
1480 } else {
1481 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1482 return 1;
1483 }
1484 }
1485
Michal Vasko9f96a052020-03-10 09:41:45 +01001486 /* we need meta positions now */
1487 if (item1->type == LYXP_NODE_META) {
1488 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001489 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001490 if (item2->type == LYXP_NODE_META) {
1491 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001492 }
1493
Michal Vasko9f96a052020-03-10 09:41:45 +01001494 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001495 /* check for duplicates */
1496 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001497 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001498 return 0;
1499 }
1500
Michal Vasko9f96a052020-03-10 09:41:45 +01001501 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001502 /* elem is always first, 2nd node is after it */
1503 if (item1->type == LYXP_NODE_ELEM) {
1504 assert(item2->type != LYXP_NODE_ELEM);
1505 return -1;
1506 }
1507
Michal Vasko9f96a052020-03-10 09:41:45 +01001508 /* 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 +02001509 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001510 if (((item1->type == LYXP_NODE_TEXT) &&
1511 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1512 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1513 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1514 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001515 return 1;
1516 }
1517
Michal Vasko9f96a052020-03-10 09:41:45 +01001518 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001519 /* 2nd is after 1st */
1520 return -1;
1521}
1522
1523/**
1524 * @brief Set cast for comparisons.
1525 *
1526 * @param[in] trg Target set to cast source into.
1527 * @param[in] src Source set.
1528 * @param[in] type Target set type.
1529 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001530 * @return LY_ERR
1531 */
1532static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001533set_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 +02001534{
1535 assert(src->type == LYXP_SET_NODE_SET);
1536
1537 set_init(trg, src);
1538
1539 /* insert node into target set */
1540 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1541
1542 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001543 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001544}
1545
Michal Vasko4c7763f2020-07-27 17:40:37 +02001546/**
1547 * @brief Set content canonization for comparisons.
1548 *
1549 * @param[in] trg Target set to put the canononized source into.
1550 * @param[in] src Source set.
1551 * @param[in] xp_node Source XPath node/meta to use for canonization.
1552 * @return LY_ERR
1553 */
1554static LY_ERR
1555set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1556{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001557 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001558 struct lyd_value val;
1559 struct ly_err_item *err = NULL;
1560 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001561 LY_ERR rc;
1562
1563 /* is there anything to canonize even? */
1564 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1565 /* do we have a type to use for canonization? */
1566 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1567 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1568 } else if (xp_node->type == LYXP_NODE_META) {
1569 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1570 }
1571 }
1572 if (!type) {
1573 goto fill;
1574 }
1575
1576 if (src->type == LYXP_SET_NUMBER) {
1577 /* canonize number */
1578 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1579 LOGMEM(src->ctx);
1580 return LY_EMEM;
1581 }
1582 } else {
1583 /* canonize string */
1584 str = strdup(src->val.str);
1585 }
1586
1587 /* ignore errors, the value may not satisfy schema constraints */
Radek Krejci0b013302021-03-29 15:22:32 +02001588 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 +01001589 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001590 ly_err_free(err);
1591 if (rc) {
1592 /* invalid value */
Radek IÅ¡a48460222021-03-02 15:18:32 +01001593 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001594 goto fill;
1595 }
1596
Michal Vasko4c7763f2020-07-27 17:40:37 +02001597 /* use the canonized value */
1598 set_init(trg, src);
1599 trg->type = src->type;
1600 if (src->type == LYXP_SET_NUMBER) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001601 trg->val.num = strtold(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001602 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1603 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001604 trg->val.str = strdup(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko4c7763f2020-07-27 17:40:37 +02001605 }
1606 type->plugin->free(src->ctx, &val);
1607 return LY_SUCCESS;
1608
1609fill:
1610 /* no canonization needed/possible */
1611 set_fill_set(trg, src);
1612 return LY_SUCCESS;
1613}
1614
Michal Vasko03ff5a72019-09-11 13:49:33 +02001615#ifndef NDEBUG
1616
1617/**
1618 * @brief Bubble sort @p set into XPath document order.
1619 * Context position aware. Unused in the 'Release' build target.
1620 *
1621 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001622 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1623 */
1624static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001625set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001626{
1627 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001628 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001629 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001630 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001631 struct lyxp_set_node item;
1632 struct lyxp_set_hash_node hnode;
1633 uint64_t hash;
1634
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001635 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001636 return 0;
1637 }
1638
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001639 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001640 for (root = set->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001641 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001642
1643 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001644 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001645 return -1;
1646 }
1647
1648 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1649 print_set_debug(set);
1650
1651 for (i = 0; i < set->used; ++i) {
1652 inverted = 0;
1653 change = 0;
1654
1655 for (j = 1; j < set->used - i; ++j) {
1656 /* compare node positions */
1657 if (inverted) {
1658 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1659 } else {
1660 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1661 }
1662
1663 /* swap if needed */
1664 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1665 change = 1;
1666
1667 item = set->val.nodes[j - 1];
1668 set->val.nodes[j - 1] = set->val.nodes[j];
1669 set->val.nodes[j] = item;
1670 } else {
1671 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1672 inverted = !inverted;
1673 }
1674 }
1675
1676 ++ret;
1677
1678 if (!change) {
1679 break;
1680 }
1681 }
1682
1683 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1684 print_set_debug(set);
1685
1686 /* check node hashes */
1687 if (set->used >= LYD_HT_MIN_ITEMS) {
1688 assert(set->ht);
1689 for (i = 0; i < set->used; ++i) {
1690 hnode.node = set->val.nodes[i].node;
1691 hnode.type = set->val.nodes[i].type;
1692
1693 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1694 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1695 hash = dict_hash_multi(hash, NULL, 0);
1696
1697 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1698 }
1699 }
1700
1701 return ret - 1;
1702}
1703
1704/**
1705 * @brief Remove duplicate entries in a sorted node set.
1706 *
1707 * @param[in] set Sorted set to check.
1708 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1709 */
1710static LY_ERR
1711set_sorted_dup_node_clean(struct lyxp_set *set)
1712{
1713 uint32_t i = 0;
1714 LY_ERR ret = LY_SUCCESS;
1715
1716 if (set->used > 1) {
1717 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001718 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1719 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001720 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001721 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001722 }
Michal Vasko57eab132019-09-24 11:46:26 +02001723 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001724 }
1725 }
1726
Michal Vasko2caefc12019-11-14 16:07:56 +01001727 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001728 return ret;
1729}
1730
1731#endif
1732
1733/**
1734 * @brief Merge 2 sorted sets into one.
1735 *
1736 * @param[in,out] trg Set to merge into. Duplicates are removed.
1737 * @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 +02001738 * @return LY_ERR
1739 */
1740static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001741set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001742{
1743 uint32_t i, j, k, count, dup_count;
1744 int cmp;
1745 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001746
Michal Vaskod3678892020-05-21 10:06:58 +02001747 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001748 return LY_EINVAL;
1749 }
1750
Michal Vaskod3678892020-05-21 10:06:58 +02001751 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001752 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001753 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001754 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001755 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001756 return LY_SUCCESS;
1757 }
1758
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001759 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001760 for (root = trg->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001761 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001762
1763 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001764 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001765 return LY_EINT;
1766 }
1767
1768#ifndef NDEBUG
1769 LOGDBG(LY_LDGXPATH, "MERGE target");
1770 print_set_debug(trg);
1771 LOGDBG(LY_LDGXPATH, "MERGE source");
1772 print_set_debug(src);
1773#endif
1774
1775 /* make memory for the merge (duplicates are not detected yet, so space
1776 * will likely be wasted on them, too bad) */
1777 if (trg->size - trg->used < src->used) {
1778 trg->size = trg->used + src->used;
1779
1780 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1781 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1782 }
1783
1784 i = 0;
1785 j = 0;
1786 count = 0;
1787 dup_count = 0;
1788 do {
1789 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1790 if (!cmp) {
1791 if (!count) {
1792 /* duplicate, just skip it */
1793 ++i;
1794 ++j;
1795 } else {
1796 /* we are copying something already, so let's copy the duplicate too,
1797 * we are hoping that afterwards there are some more nodes to
1798 * copy and this way we can copy them all together */
1799 ++count;
1800 ++dup_count;
1801 ++i;
1802 ++j;
1803 }
1804 } else if (cmp < 0) {
1805 /* inserting src node into trg, just remember it for now */
1806 ++count;
1807 ++i;
1808
1809 /* insert the hash now */
1810 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1811 } else if (count) {
1812copy_nodes:
1813 /* time to actually copy the nodes, we have found the largest block of nodes */
1814 memmove(&trg->val.nodes[j + (count - dup_count)],
1815 &trg->val.nodes[j],
1816 (trg->used - j) * sizeof *trg->val.nodes);
1817 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1818
1819 trg->used += count - dup_count;
1820 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1821 j += count - dup_count;
1822
1823 count = 0;
1824 dup_count = 0;
1825 } else {
1826 ++j;
1827 }
1828 } while ((i < src->used) && (j < trg->used));
1829
1830 if ((i < src->used) || count) {
1831 /* insert all the hashes first */
1832 for (k = i; k < src->used; ++k) {
1833 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1834 }
1835
1836 /* loop ended, but we need to copy something at trg end */
1837 count += src->used - i;
1838 i = src->used;
1839 goto copy_nodes;
1840 }
1841
1842 /* we are inserting hashes before the actual node insert, which causes
1843 * situations when there were initially not enough items for a hash table,
1844 * but even after some were inserted, hash table was not created (during
1845 * insertion the number of items is not updated yet) */
1846 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1847 set_insert_node_hash(trg, NULL, 0);
1848 }
1849
1850#ifndef NDEBUG
1851 LOGDBG(LY_LDGXPATH, "MERGE result");
1852 print_set_debug(trg);
1853#endif
1854
Michal Vaskod3678892020-05-21 10:06:58 +02001855 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001856 return LY_SUCCESS;
1857}
1858
1859/*
1860 * (re)parse functions
1861 *
1862 * Parse functions parse the expression into
1863 * tokens (syntactic analysis).
1864 *
1865 * Reparse functions perform semantic analysis
1866 * (do not save the result, just a check) of
1867 * the expression and fill repeat indices.
1868 */
1869
Michal Vasko14676352020-05-29 11:35:55 +02001870LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001871lyxp_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 +02001872{
Michal Vasko004d3152020-06-11 19:59:22 +02001873 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001874 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001875 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001876 }
Michal Vasko14676352020-05-29 11:35:55 +02001877 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001878 }
1879
Michal Vasko004d3152020-06-11 19:59:22 +02001880 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001881 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001882 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001883 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001884 }
Michal Vasko14676352020-05-29 11:35:55 +02001885 return LY_ENOT;
1886 }
1887
1888 return LY_SUCCESS;
1889}
1890
Michal Vasko004d3152020-06-11 19:59:22 +02001891LY_ERR
1892lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1893{
1894 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1895
1896 /* skip the token */
1897 ++(*tok_idx);
1898
1899 return LY_SUCCESS;
1900}
1901
Michal Vasko14676352020-05-29 11:35:55 +02001902/* just like lyxp_check_token() but tests for 2 tokens */
1903static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001904exp_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 +02001905 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001906{
Michal Vasko004d3152020-06-11 19:59:22 +02001907 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001908 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001909 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001910 }
1911 return LY_EINCOMPLETE;
1912 }
1913
Michal Vasko004d3152020-06-11 19:59:22 +02001914 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001915 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001916 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001917 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001918 }
1919 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001920 }
1921
1922 return LY_SUCCESS;
1923}
1924
Michal Vasko4911eeb2021-06-28 11:23:05 +02001925LY_ERR
1926lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok1,
1927 enum lyxp_token want_tok2)
1928{
1929 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
1930
1931 /* skip the token */
1932 ++(*tok_idx);
1933
1934 return LY_SUCCESS;
1935}
1936
Michal Vasko03ff5a72019-09-11 13:49:33 +02001937/**
1938 * @brief Stack operation push on the repeat array.
1939 *
1940 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001941 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001942 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1943 */
1944static void
Michal Vasko004d3152020-06-11 19:59:22 +02001945exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001946{
1947 uint16_t i;
1948
Michal Vasko004d3152020-06-11 19:59:22 +02001949 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001950 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001951 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1952 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1953 exp->repeat[tok_idx][i] = repeat_op_idx;
1954 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001955 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001956 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1957 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1958 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001959 }
1960}
1961
1962/**
1963 * @brief Reparse Predicate. Logs directly on error.
1964 *
1965 * [7] Predicate ::= '[' Expr ']'
1966 *
1967 * @param[in] ctx Context for logging.
1968 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001969 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02001970 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001971 * @return LY_ERR
1972 */
1973static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02001974reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001975{
1976 LY_ERR rc;
1977
Michal Vasko004d3152020-06-11 19:59:22 +02001978 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001979 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001980 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001981
aPiecekbf968d92021-05-27 14:35:05 +02001982 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001983 LY_CHECK_RET(rc);
1984
Michal Vasko004d3152020-06-11 19:59:22 +02001985 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001986 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001987 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001988
1989 return LY_SUCCESS;
1990}
1991
1992/**
1993 * @brief Reparse RelativeLocationPath. Logs directly on error.
1994 *
1995 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1996 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1997 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1998 *
1999 * @param[in] ctx Context for logging.
2000 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002001 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002002 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002003 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
2004 */
2005static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002006reparse_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 +02002007{
2008 LY_ERR rc;
2009
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);
2012
2013 goto step;
2014 do {
2015 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002016 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002017
Michal Vasko004d3152020-06-11 19:59:22 +02002018 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002019 LY_CHECK_RET(rc);
2020step:
2021 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002022 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002023 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002024 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002025 break;
2026
2027 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002028 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002029 break;
2030
2031 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002032 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002033
Michal Vasko004d3152020-06-11 19:59:22 +02002034 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002035 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002036 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002037 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002038 return LY_EVALID;
2039 }
Radek Krejci0f969882020-08-21 16:56:47 +02002040 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002041 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002042 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002043 goto reparse_predicate;
2044 break;
2045
2046 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002047 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002048
2049 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002050 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002051 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002052 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002053
2054 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002055 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002056 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002057 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002058
2059reparse_predicate:
2060 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002061 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002062 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002063 LY_CHECK_RET(rc);
2064 }
2065 break;
2066 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002067 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 +02002068 return LY_EVALID;
2069 }
Michal Vasko004d3152020-06-11 19:59:22 +02002070 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002071
2072 return LY_SUCCESS;
2073}
2074
2075/**
2076 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2077 *
2078 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2079 *
2080 * @param[in] ctx Context for logging.
2081 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002082 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002083 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002084 * @return LY_ERR
2085 */
2086static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002087reparse_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 +02002088{
2089 LY_ERR rc;
2090
Michal Vasko004d3152020-06-11 19:59:22 +02002091 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 +02002092
2093 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002094 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002095 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002096 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002097
Michal Vasko004d3152020-06-11 19:59:22 +02002098 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002099 return LY_SUCCESS;
2100 }
Michal Vasko004d3152020-06-11 19:59:22 +02002101 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002102 case LYXP_TOKEN_DOT:
2103 case LYXP_TOKEN_DDOT:
2104 case LYXP_TOKEN_AT:
2105 case LYXP_TOKEN_NAMETEST:
2106 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002107 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002108 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002109 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002110 default:
2111 break;
2112 }
2113
Michal Vasko03ff5a72019-09-11 13:49:33 +02002114 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002115 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002116 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002117
aPiecekbf968d92021-05-27 14:35:05 +02002118 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002119 LY_CHECK_RET(rc);
2120 }
2121
2122 return LY_SUCCESS;
2123}
2124
2125/**
2126 * @brief Reparse FunctionCall. Logs directly on error.
2127 *
2128 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2129 *
2130 * @param[in] ctx Context for logging.
2131 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002132 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002133 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002134 * @return LY_ERR
2135 */
2136static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002137reparse_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 +02002138{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002139 int8_t min_arg_count = -1;
2140 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002141 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002142 LY_ERR rc;
2143
Michal Vasko004d3152020-06-11 19:59:22 +02002144 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002145 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002146 func_tok_idx = *tok_idx;
2147 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002148 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002149 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
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]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002153 min_arg_count = 1;
2154 max_arg_count = 1;
2155 }
2156 break;
2157 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002158 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
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]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002162 min_arg_count = 0;
2163 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002164 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002165 min_arg_count = 0;
2166 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002167 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002168 min_arg_count = 0;
2169 max_arg_count = 0;
2170 }
2171 break;
2172 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002173 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002174 min_arg_count = 1;
2175 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002176 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002177 min_arg_count = 0;
2178 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002179 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
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]], "round", 5)) {
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]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002186 min_arg_count = 1;
2187 max_arg_count = 1;
2188 }
2189 break;
2190 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002191 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002192 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002193 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002194 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002195 min_arg_count = 0;
2196 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002197 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002198 min_arg_count = 0;
2199 max_arg_count = 1;
2200 }
2201 break;
2202 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002203 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002204 min_arg_count = 1;
2205 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002206 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002207 min_arg_count = 1;
2208 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002209 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002210 min_arg_count = 0;
2211 max_arg_count = 0;
2212 }
2213 break;
2214 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002215 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002216 min_arg_count = 2;
2217 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002218 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002219 min_arg_count = 0;
2220 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002221 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002222 min_arg_count = 2;
2223 max_arg_count = 2;
2224 }
2225 break;
2226 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002227 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002228 min_arg_count = 2;
2229 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002230 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002231 min_arg_count = 3;
2232 max_arg_count = 3;
2233 }
2234 break;
2235 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002236 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
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]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002240 min_arg_count = 1;
2241 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002242 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002243 min_arg_count = 2;
2244 max_arg_count = 2;
2245 }
2246 break;
2247 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002248 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002249 min_arg_count = 2;
2250 max_arg_count = 2;
2251 }
2252 break;
2253 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002254 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002255 min_arg_count = 2;
2256 max_arg_count = 2;
2257 }
2258 break;
2259 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002260 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002261 min_arg_count = 0;
2262 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002263 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002264 min_arg_count = 0;
2265 max_arg_count = 1;
2266 }
2267 break;
2268 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002269 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002270 min_arg_count = 0;
2271 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002272 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002273 min_arg_count = 2;
2274 max_arg_count = 2;
2275 }
2276 break;
2277 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002278 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002279 min_arg_count = 2;
2280 max_arg_count = 2;
2281 }
2282 break;
2283 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002284 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002285 min_arg_count = 2;
2286 max_arg_count = 2;
2287 }
2288 break;
2289 }
2290 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002291 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 +02002292 return LY_EINVAL;
2293 }
Michal Vasko004d3152020-06-11 19:59:22 +02002294 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002295
2296 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002297 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002298 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002299 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002300
2301 /* ( Expr ( ',' Expr )* )? */
2302 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002303 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002304 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002305 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002306 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002307 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002308 LY_CHECK_RET(rc);
2309 }
Michal Vasko004d3152020-06-11 19:59:22 +02002310 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2311 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002312
2313 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002314 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002315 LY_CHECK_RET(rc);
2316 }
2317
2318 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002319 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002320 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002321 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002322
Radek Krejci857189e2020-09-01 13:26:36 +02002323 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002324 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 +02002325 return LY_EVALID;
2326 }
2327
2328 return LY_SUCCESS;
2329}
2330
2331/**
2332 * @brief Reparse PathExpr. Logs directly on error.
2333 *
2334 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2335 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2336 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2337 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2338 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2339 *
2340 * @param[in] ctx Context for logging.
2341 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002342 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002343 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002344 * @return LY_ERR
2345 */
2346static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002347reparse_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 +02002348{
2349 LY_ERR rc;
2350
Michal Vasko004d3152020-06-11 19:59:22 +02002351 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002352 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002353 }
2354
Michal Vasko004d3152020-06-11 19:59:22 +02002355 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002356 case LYXP_TOKEN_PAR1:
2357 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002358 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002359
aPiecekbf968d92021-05-27 14:35:05 +02002360 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002361 LY_CHECK_RET(rc);
2362
Michal Vasko004d3152020-06-11 19:59:22 +02002363 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002364 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002365 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002366 goto predicate;
2367 break;
2368 case LYXP_TOKEN_DOT:
2369 case LYXP_TOKEN_DDOT:
2370 case LYXP_TOKEN_AT:
2371 case LYXP_TOKEN_NAMETEST:
2372 case LYXP_TOKEN_NODETYPE:
2373 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002374 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002375 LY_CHECK_RET(rc);
2376 break;
2377 case LYXP_TOKEN_FUNCNAME:
2378 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002379 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002380 LY_CHECK_RET(rc);
2381 goto predicate;
2382 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002383 case LYXP_TOKEN_OPER_PATH:
2384 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002385 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002386 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002387 LY_CHECK_RET(rc);
2388 break;
2389 case LYXP_TOKEN_LITERAL:
2390 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002391 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002392 goto predicate;
2393 break;
2394 case LYXP_TOKEN_NUMBER:
2395 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002396 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002397 goto predicate;
2398 break;
2399 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002400 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 +02002401 return LY_EVALID;
2402 }
2403
2404 return LY_SUCCESS;
2405
2406predicate:
2407 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002408 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002409 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002410 LY_CHECK_RET(rc);
2411 }
2412
2413 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002414 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002415
2416 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002417 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002418
aPiecekbf968d92021-05-27 14:35:05 +02002419 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002420 LY_CHECK_RET(rc);
2421 }
2422
2423 return LY_SUCCESS;
2424}
2425
2426/**
2427 * @brief Reparse UnaryExpr. Logs directly on error.
2428 *
2429 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2430 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2431 *
2432 * @param[in] ctx Context for logging.
2433 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002434 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002435 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002436 * @return LY_ERR
2437 */
2438static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002439reparse_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 +02002440{
2441 uint16_t prev_exp;
2442 LY_ERR rc;
2443
2444 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002445 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002446 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2447 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002448 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002449 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002450 }
2451
2452 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002453 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002454 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002455 LY_CHECK_RET(rc);
2456
2457 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002458 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002459 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002460 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002461
aPiecekbf968d92021-05-27 14:35:05 +02002462 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002463 LY_CHECK_RET(rc);
2464 }
2465
2466 return LY_SUCCESS;
2467}
2468
2469/**
2470 * @brief Reparse AdditiveExpr. Logs directly on error.
2471 *
2472 * [15] AdditiveExpr ::= MultiplicativeExpr
2473 * | AdditiveExpr '+' MultiplicativeExpr
2474 * | AdditiveExpr '-' MultiplicativeExpr
2475 * [16] MultiplicativeExpr ::= UnaryExpr
2476 * | MultiplicativeExpr '*' UnaryExpr
2477 * | MultiplicativeExpr 'div' UnaryExpr
2478 * | MultiplicativeExpr 'mod' UnaryExpr
2479 *
2480 * @param[in] ctx Context for logging.
2481 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002482 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002483 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002484 * @return LY_ERR
2485 */
2486static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002487reparse_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 +02002488{
2489 uint16_t prev_add_exp, prev_mul_exp;
2490 LY_ERR rc;
2491
Michal Vasko004d3152020-06-11 19:59:22 +02002492 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002493 goto reparse_multiplicative_expr;
2494
2495 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002496 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2497 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002498 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002499 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002500
2501reparse_multiplicative_expr:
2502 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002503 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002504 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002505 LY_CHECK_RET(rc);
2506
2507 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002508 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2509 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002510 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002511 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002512
aPiecekbf968d92021-05-27 14:35:05 +02002513 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002514 LY_CHECK_RET(rc);
2515 }
2516 }
2517
2518 return LY_SUCCESS;
2519}
2520
2521/**
2522 * @brief Reparse EqualityExpr. Logs directly on error.
2523 *
2524 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2525 * | EqualityExpr '!=' RelationalExpr
2526 * [14] RelationalExpr ::= AdditiveExpr
2527 * | RelationalExpr '<' AdditiveExpr
2528 * | RelationalExpr '>' AdditiveExpr
2529 * | RelationalExpr '<=' AdditiveExpr
2530 * | RelationalExpr '>=' AdditiveExpr
2531 *
2532 * @param[in] ctx Context for logging.
2533 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002534 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002535 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002536 * @return LY_ERR
2537 */
2538static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002539reparse_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 +02002540{
2541 uint16_t prev_eq_exp, prev_rel_exp;
2542 LY_ERR rc;
2543
Michal Vasko004d3152020-06-11 19:59:22 +02002544 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002545 goto reparse_additive_expr;
2546
2547 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002548 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002549 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002550 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002551
2552reparse_additive_expr:
2553 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002554 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002555 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002556 LY_CHECK_RET(rc);
2557
2558 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002559 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002560 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002561 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002562
aPiecekbf968d92021-05-27 14:35:05 +02002563 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002564 LY_CHECK_RET(rc);
2565 }
2566 }
2567
2568 return LY_SUCCESS;
2569}
2570
2571/**
2572 * @brief Reparse OrExpr. Logs directly on error.
2573 *
2574 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2575 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2576 *
2577 * @param[in] ctx Context for logging.
2578 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002579 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002580 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002581 * @return LY_ERR
2582 */
2583static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002584reparse_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 +02002585{
2586 uint16_t prev_or_exp, prev_and_exp;
2587 LY_ERR rc;
2588
aPiecekbf968d92021-05-27 14:35:05 +02002589 ++depth;
2590 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2591
Michal Vasko004d3152020-06-11 19:59:22 +02002592 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002593 goto reparse_equality_expr;
2594
2595 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002596 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 +02002597 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002598 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002599
2600reparse_equality_expr:
2601 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002602 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002603 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002604 LY_CHECK_RET(rc);
2605
2606 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002607 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 +02002608 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002609 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002610
aPiecekbf968d92021-05-27 14:35:05 +02002611 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002612 LY_CHECK_RET(rc);
2613 }
2614 }
2615
2616 return LY_SUCCESS;
2617}
Radek Krejcib1646a92018-11-02 16:08:26 +01002618
2619/**
2620 * @brief Parse NCName.
2621 *
2622 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002623 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002624 */
Radek Krejcid4270262019-01-07 15:07:25 +01002625static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002626parse_ncname(const char *ncname)
2627{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002628 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002629 size_t size;
2630 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002631
2632 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2633 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2634 return len;
2635 }
2636
2637 do {
2638 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002639 if (!*ncname) {
2640 break;
2641 }
Radek Krejcid4270262019-01-07 15:07:25 +01002642 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002643 } while (is_xmlqnamechar(uc) && (uc != ':'));
2644
2645 return len;
2646}
2647
2648/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002649 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002650 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002651 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002652 * @param[in] exp Expression to use.
2653 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002654 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002655 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002656 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002657 */
2658static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002659exp_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 +01002660{
2661 uint32_t prev;
2662
2663 if (exp->used == exp->size) {
2664 prev = exp->size;
2665 exp->size += LYXP_EXPR_SIZE_STEP;
2666 if (prev > exp->size) {
2667 LOGINT(ctx);
2668 return LY_EINT;
2669 }
2670
2671 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2672 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2673 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2674 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2675 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2676 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2677 }
2678
2679 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002680 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002681 exp->tok_len[exp->used] = tok_len;
2682 ++exp->used;
2683 return LY_SUCCESS;
2684}
2685
2686void
Michal Vasko14676352020-05-29 11:35:55 +02002687lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002688{
2689 uint16_t i;
2690
2691 if (!expr) {
2692 return;
2693 }
2694
2695 lydict_remove(ctx, expr->expr);
2696 free(expr->tokens);
2697 free(expr->tok_pos);
2698 free(expr->tok_len);
2699 if (expr->repeat) {
2700 for (i = 0; i < expr->used; ++i) {
2701 free(expr->repeat[i]);
2702 }
2703 }
2704 free(expr->repeat);
2705 free(expr);
2706}
2707
Radek Krejcif03a9e22020-09-18 20:09:31 +02002708LY_ERR
2709lyxp_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 +01002710{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002711 LY_ERR ret = LY_SUCCESS;
2712 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002713 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002714 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002715 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002716 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002717
Radek Krejcif03a9e22020-09-18 20:09:31 +02002718 assert(expr_p);
2719
2720 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002721 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002722 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002723 }
2724
2725 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002726 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002727 }
2728 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002729 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002730 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002731 }
2732
2733 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002734 expr = calloc(1, sizeof *expr);
2735 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2736 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2737 expr->used = 0;
2738 expr->size = LYXP_EXPR_SIZE_START;
2739 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2740 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002741
Radek Krejcif03a9e22020-09-18 20:09:31 +02002742 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2743 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002744
Radek Krejcif03a9e22020-09-18 20:09:31 +02002745 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2746 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002747
Michal Vasko004d3152020-06-11 19:59:22 +02002748 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002749 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002750
Radek Krejcif03a9e22020-09-18 20:09:31 +02002751 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002752 ++parsed;
2753 }
2754
2755 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002756 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002757
2758 /* '(' */
2759 tok_len = 1;
2760 tok_type = LYXP_TOKEN_PAR1;
2761
Radek Krejcif03a9e22020-09-18 20:09:31 +02002762 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002763 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002764 if (((expr->tok_len[expr->used - 1] == 4) &&
2765 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2766 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2767 ((expr->tok_len[expr->used - 1] == 7) &&
2768 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002769 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002770 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002771 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002772 }
2773 prev_function_check = 0;
2774 }
2775
Radek Krejcif03a9e22020-09-18 20:09:31 +02002776 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002777
2778 /* ')' */
2779 tok_len = 1;
2780 tok_type = LYXP_TOKEN_PAR2;
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_BRACK1;
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_BRACK2;
2793
Radek Krejcif03a9e22020-09-18 20:09:31 +02002794 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002795
2796 /* '..' */
2797 tok_len = 2;
2798 tok_type = LYXP_TOKEN_DDOT;
2799
Radek Krejcif03a9e22020-09-18 20:09:31 +02002800 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002801
2802 /* '.' */
2803 tok_len = 1;
2804 tok_type = LYXP_TOKEN_DOT;
2805
Radek Krejcif03a9e22020-09-18 20:09:31 +02002806 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002807
2808 /* '@' */
2809 tok_len = 1;
2810 tok_type = LYXP_TOKEN_AT;
2811
Radek Krejcif03a9e22020-09-18 20:09:31 +02002812 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002813
2814 /* ',' */
2815 tok_len = 1;
2816 tok_type = LYXP_TOKEN_COMMA;
2817
Radek Krejcif03a9e22020-09-18 20:09:31 +02002818 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002819
2820 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002821 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2822 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002823 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002824 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002825 ++tok_len;
2826 tok_type = LYXP_TOKEN_LITERAL;
2827
Radek Krejcif03a9e22020-09-18 20:09:31 +02002828 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002829
2830 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002831 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2832 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002833 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002834 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002835 ++tok_len;
2836 tok_type = LYXP_TOKEN_LITERAL;
2837
Radek Krejcif03a9e22020-09-18 20:09:31 +02002838 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002839
2840 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002841 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2842 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002843 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002844 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002845 }
2846 tok_type = LYXP_TOKEN_NUMBER;
2847
Radek Krejcif03a9e22020-09-18 20:09:31 +02002848 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002849
2850 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002851 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002852 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002853 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002854 } else {
2855 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002856 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002857 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002858
Radek Krejcif03a9e22020-09-18 20:09:31 +02002859 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
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 = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002863 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2864
Radek Krejcif03a9e22020-09-18 20:09:31 +02002865 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002866
2867 /* Operator '<=', '>=' */
2868 tok_len = 2;
2869 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002870
Radek Krejcif03a9e22020-09-18 20:09:31 +02002871 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002872
2873 /* Operator '|' */
2874 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002875 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002876
Radek Krejcif03a9e22020-09-18 20:09:31 +02002877 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002878
2879 /* Operator '+', '-' */
2880 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002881 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002882
Radek Krejcif03a9e22020-09-18 20:09:31 +02002883 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002884
Michal Vasko3e48bf32020-06-01 08:39:07 +02002885 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002886 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002887 tok_type = LYXP_TOKEN_OPER_EQUAL;
2888
Radek Krejcif03a9e22020-09-18 20:09:31 +02002889 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002890
2891 /* Operator '<', '>' */
2892 tok_len = 1;
2893 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002894
Michal Vasko69730152020-10-09 16:30:07 +02002895 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2896 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2897 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2898 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2899 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2900 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2901 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2902 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2903 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2904 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2905 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2906 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002907
2908 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002909 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002910 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002911 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002912
Radek Krejcif03a9e22020-09-18 20:09:31 +02002913 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002914 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002915 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002916
Radek Krejcif03a9e22020-09-18 20:09:31 +02002917 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002918 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002919 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002920
Radek Krejcif03a9e22020-09-18 20:09:31 +02002921 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002922 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002923 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002924
2925 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002926 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 +02002927 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 +02002928 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002929 goto error;
2930 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02002931 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002932 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002933 goto error;
2934 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002935 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002936
2937 /* NameTest '*' */
2938 tok_len = 1;
2939 tok_type = LYXP_TOKEN_NAMETEST;
2940
2941 } else {
2942
2943 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002944 long int ncname_len = parse_ncname(&expr_str[parsed]);
Michal Vasko774ce402021-04-14 15:35:06 +02002945 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2946 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002947 tok_len = ncname_len;
2948
Radek Krejcif03a9e22020-09-18 20:09:31 +02002949 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002950 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002951 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002952 ++tok_len;
2953 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002954 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vasko774ce402021-04-14 15:35:06 +02002955 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2956 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002957 tok_len += ncname_len;
2958 }
2959 /* remove old flag to prevent ambiguities */
2960 prev_function_check = 0;
2961 tok_type = LYXP_TOKEN_NAMETEST;
2962 } else {
2963 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2964 prev_function_check = 1;
2965 tok_type = LYXP_TOKEN_NAMETEST;
2966 }
2967 }
2968
2969 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002970 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002971 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002972 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002973 ++parsed;
2974 }
2975
Radek Krejcif03a9e22020-09-18 20:09:31 +02002976 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002977
Michal Vasko004d3152020-06-11 19:59:22 +02002978 if (reparse) {
2979 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002980 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2981 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002982
Michal Vasko004d3152020-06-11 19:59:22 +02002983 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02002984 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002985 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002986 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002987 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002988 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002989 goto error;
2990 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002991 }
2992
Radek Krejcif03a9e22020-09-18 20:09:31 +02002993 print_expr_struct_debug(expr);
2994 *expr_p = expr;
2995 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002996
2997error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002998 lyxp_expr_free(ctx, expr);
2999 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01003000}
3001
Michal Vasko1734be92020-09-22 08:55:10 +02003002LY_ERR
3003lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003004{
Michal Vasko1734be92020-09-22 08:55:10 +02003005 LY_ERR ret = LY_SUCCESS;
3006 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003007 uint32_t i, j;
3008
Michal Vasko7f45cf22020-10-01 12:49:44 +02003009 if (!exp) {
3010 goto cleanup;
3011 }
3012
Michal Vasko004d3152020-06-11 19:59:22 +02003013 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003014 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003015
Michal Vasko08e9b112021-06-11 15:41:17 +02003016 if (exp->used) {
3017 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
3018 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3019 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003020
Michal Vasko08e9b112021-06-11 15:41:17 +02003021 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
3022 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3023 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003024
Michal Vasko08e9b112021-06-11 15:41:17 +02003025 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
3026 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3027 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003028
Michal Vasko08e9b112021-06-11 15:41:17 +02003029 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
3030 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3031 for (i = 0; i < exp->used; ++i) {
3032 if (!exp->repeat[i]) {
3033 dup->repeat[i] = NULL;
3034 } else {
3035 for (j = 0; exp->repeat[i][j]; ++j) {}
3036 /* the ending 0 as well */
3037 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003038
Michal Vasko08e9b112021-06-11 15:41:17 +02003039 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
3040 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3041 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3042 dup->repeat[i][j - 1] = 0;
3043 }
Michal Vasko004d3152020-06-11 19:59:22 +02003044 }
3045 }
3046
3047 dup->used = exp->used;
3048 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003049 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003050
Michal Vasko1734be92020-09-22 08:55:10 +02003051cleanup:
3052 if (ret) {
3053 lyxp_expr_free(ctx, dup);
3054 } else {
3055 *dup_p = dup;
3056 }
3057 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003058}
3059
Michal Vasko03ff5a72019-09-11 13:49:33 +02003060/*
3061 * warn functions
3062 *
3063 * Warn functions check specific reasonable conditions for schema XPath
3064 * and print a warning if they are not satisfied.
3065 */
3066
3067/**
3068 * @brief Get the last-added schema node that is currently in the context.
3069 *
3070 * @param[in] set Set to search in.
3071 * @return Last-added schema context node, NULL if no node is in context.
3072 */
3073static struct lysc_node *
3074warn_get_scnode_in_ctx(struct lyxp_set *set)
3075{
3076 uint32_t i;
3077
3078 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3079 return NULL;
3080 }
3081
3082 i = set->used;
3083 do {
3084 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003085 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003086 /* if there are more, simply return the first found (last added) */
3087 return set->val.scnodes[i].scnode;
3088 }
3089 } while (i);
3090
3091 return NULL;
3092}
3093
3094/**
3095 * @brief Test whether a type is numeric - integer type or decimal64.
3096 *
3097 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003098 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003099 */
Radek Krejci857189e2020-09-01 13:26:36 +02003100static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003101warn_is_numeric_type(struct lysc_type *type)
3102{
3103 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003104 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003105 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003106
3107 switch (type->basetype) {
3108 case LY_TYPE_DEC64:
3109 case LY_TYPE_INT8:
3110 case LY_TYPE_UINT8:
3111 case LY_TYPE_INT16:
3112 case LY_TYPE_UINT16:
3113 case LY_TYPE_INT32:
3114 case LY_TYPE_UINT32:
3115 case LY_TYPE_INT64:
3116 case LY_TYPE_UINT64:
3117 return 1;
3118 case LY_TYPE_UNION:
3119 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003120 LY_ARRAY_FOR(uni->types, u) {
3121 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003122 if (ret) {
3123 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003124 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003125 }
3126 }
3127 /* did not find any suitable type */
3128 return 0;
3129 case LY_TYPE_LEAFREF:
3130 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3131 default:
3132 return 0;
3133 }
3134}
3135
3136/**
3137 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3138 *
3139 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003140 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003141 */
Radek Krejci857189e2020-09-01 13:26:36 +02003142static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003143warn_is_string_type(struct lysc_type *type)
3144{
3145 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003146 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003147 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003148
3149 switch (type->basetype) {
3150 case LY_TYPE_BITS:
3151 case LY_TYPE_ENUM:
3152 case LY_TYPE_IDENT:
3153 case LY_TYPE_INST:
3154 case LY_TYPE_STRING:
3155 return 1;
3156 case LY_TYPE_UNION:
3157 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003158 LY_ARRAY_FOR(uni->types, u) {
3159 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003160 if (ret) {
3161 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003162 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003163 }
3164 }
3165 /* did not find any suitable type */
3166 return 0;
3167 case LY_TYPE_LEAFREF:
3168 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3169 default:
3170 return 0;
3171 }
3172}
3173
3174/**
3175 * @brief Test whether a type is one specific type.
3176 *
3177 * @param[in] type Type to test.
3178 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003179 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003180 */
Radek Krejci857189e2020-09-01 13:26:36 +02003181static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003182warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3183{
3184 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003185 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003186 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003187
3188 if (type->basetype == base) {
3189 return 1;
3190 } else if (type->basetype == LY_TYPE_UNION) {
3191 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003192 LY_ARRAY_FOR(uni->types, u) {
3193 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003194 if (ret) {
3195 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003196 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003197 }
3198 }
3199 /* did not find any suitable type */
3200 return 0;
3201 } else if (type->basetype == LY_TYPE_LEAFREF) {
3202 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3203 }
3204
3205 return 0;
3206}
3207
3208/**
3209 * @brief Get next type of a (union) type.
3210 *
3211 * @param[in] type Base type.
3212 * @param[in] prev_type Previously returned type.
3213 * @return Next type or NULL.
3214 */
3215static struct lysc_type *
3216warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3217{
3218 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003219 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003220 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003221
3222 switch (type->basetype) {
3223 case LY_TYPE_UNION:
3224 uni = (struct lysc_type_union *)type;
3225 if (!prev_type) {
3226 return uni->types[0];
3227 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003228 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003229 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003230 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003231 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003232 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003233 found = 1;
3234 }
3235 }
3236 return NULL;
3237 default:
3238 if (prev_type) {
3239 assert(type == prev_type);
3240 return NULL;
3241 } else {
3242 return type;
3243 }
3244 }
3245}
3246
3247/**
3248 * @brief Test whether 2 types have a common type.
3249 *
3250 * @param[in] type1 First type.
3251 * @param[in] type2 Second type.
3252 * @return 1 if they do, 0 otherwise.
3253 */
3254static int
3255warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3256{
3257 struct lysc_type *t1, *rt1, *t2, *rt2;
3258
3259 t1 = NULL;
3260 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3261 if (t1->basetype == LY_TYPE_LEAFREF) {
3262 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3263 } else {
3264 rt1 = t1;
3265 }
3266
3267 t2 = NULL;
3268 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3269 if (t2->basetype == LY_TYPE_LEAFREF) {
3270 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3271 } else {
3272 rt2 = t2;
3273 }
3274
3275 if (rt2->basetype == rt1->basetype) {
3276 /* match found */
3277 return 1;
3278 }
3279 }
3280 }
3281
3282 return 0;
3283}
3284
3285/**
3286 * @brief Check both operands of comparison operators.
3287 *
3288 * @param[in] ctx Context for errors.
3289 * @param[in] set1 First operand set.
3290 * @param[in] set2 Second operand set.
3291 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3292 * @param[in] expr Start of the expression to print with the warning.
3293 * @param[in] tok_pos Token position.
3294 */
3295static void
Radek Krejci857189e2020-09-01 13:26:36 +02003296warn_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 +02003297{
3298 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003299 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003300
3301 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3302 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3303
3304 if (!node1 && !node2) {
3305 /* no node-sets involved, nothing to do */
3306 return;
3307 }
3308
3309 if (node1) {
3310 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3311 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3312 warning = 1;
3313 leaves = 0;
3314 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3315 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3316 warning = 1;
3317 }
3318 }
3319
3320 if (node2) {
3321 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3322 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3323 warning = 1;
3324 leaves = 0;
3325 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3326 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3327 warning = 1;
3328 }
3329 }
3330
3331 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003332 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3333 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3334 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3335 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003336 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3337 warning = 1;
3338 }
3339 }
3340
3341 if (warning) {
3342 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3343 }
3344}
3345
3346/**
3347 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3348 *
3349 * @param[in] exp Parsed XPath expression.
3350 * @param[in] set Set with the leaf/leaf-list.
3351 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3352 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3353 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3354 */
3355static void
Michal Vasko40308e72020-10-20 16:38:40 +02003356warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3357 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003358{
3359 struct lysc_node *scnode;
3360 struct lysc_type *type;
3361 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003362 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003363 LY_ERR rc;
3364 struct ly_err_item *err = NULL;
3365
Michal Vasko69730152020-10-09 16:30:07 +02003366 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3367 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003368 /* check that the node can have the specified value */
3369 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3370 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3371 } else {
3372 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3373 }
3374 if (!value) {
3375 LOGMEM(set->ctx);
3376 return;
3377 }
3378
3379 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3380 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003381 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003382 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003383 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003384 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003385 }
3386
3387 type = ((struct lysc_node_leaf *)scnode)->type;
3388 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003389 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003390 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003391 if (rc == LY_EINCOMPLETE) {
3392 rc = LY_SUCCESS;
3393 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003394
3395 if (err) {
3396 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3397 ly_err_free(err);
3398 } else if (rc != LY_SUCCESS) {
3399 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3400 }
3401 if (rc != LY_SUCCESS) {
3402 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003403 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003404 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003405 } else {
3406 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003407 }
3408 }
3409 free(value);
3410 }
3411}
3412
3413/*
3414 * XPath functions
3415 */
3416
3417/**
3418 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3419 * depending on whether the first node bit value from the second argument is set.
3420 *
3421 * @param[in] args Array of arguments.
3422 * @param[in] arg_count Count of elements in @p args.
3423 * @param[in,out] set Context and result set at the same time.
3424 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003425 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003426 */
3427static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003428xpath_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 +02003429{
3430 struct lyd_node_term *leaf;
3431 struct lysc_node_leaf *sleaf;
3432 struct lysc_type_bits *bits;
3433 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003434 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003435
3436 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003437 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003438 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003439 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3440 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3441 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3442 sleaf->name);
3443 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3444 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3445 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003446 }
3447
3448 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3449 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003450 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3451 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003452 } else if (!warn_is_string_type(sleaf->type)) {
3453 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003454 }
3455 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003456 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003457 return rc;
3458 }
3459
Michal Vaskod3678892020-05-21 10:06:58 +02003460 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003461 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 +02003462 return LY_EVALID;
3463 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003464 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003465 LY_CHECK_RET(rc);
3466
3467 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003468 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003469 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003470 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3471 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003472 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003473 LY_ARRAY_FOR(bits->bits, u) {
3474 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003475 set_fill_boolean(set, 1);
3476 break;
3477 }
3478 }
3479 }
3480 }
3481
3482 return LY_SUCCESS;
3483}
3484
3485/**
3486 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3487 * with the argument converted to boolean.
3488 *
3489 * @param[in] args Array of arguments.
3490 * @param[in] arg_count Count of elements in @p args.
3491 * @param[in,out] set Context and result set at the same time.
3492 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003493 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003494 */
3495static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003496xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003497{
3498 LY_ERR rc;
3499
3500 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003501 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003502 return LY_SUCCESS;
3503 }
3504
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003505 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003506 LY_CHECK_RET(rc);
3507 set_fill_set(set, args[0]);
3508
3509 return LY_SUCCESS;
3510}
3511
3512/**
3513 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3514 * with the first argument rounded up to the nearest integer.
3515 *
3516 * @param[in] args Array of arguments.
3517 * @param[in] arg_count Count of elements in @p args.
3518 * @param[in,out] set Context and result set at the same time.
3519 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003520 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003521 */
3522static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003523xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003524{
3525 struct lysc_node_leaf *sleaf;
3526 LY_ERR rc = LY_SUCCESS;
3527
3528 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003529 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003530 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003531 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3532 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3533 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3534 sleaf->name);
3535 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3536 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3537 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003538 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003539 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003540 return rc;
3541 }
3542
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003543 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003544 LY_CHECK_RET(rc);
3545 if ((long long)args[0]->val.num != args[0]->val.num) {
3546 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3547 } else {
3548 set_fill_number(set, args[0]->val.num);
3549 }
3550
3551 return LY_SUCCESS;
3552}
3553
3554/**
3555 * @brief Execute the XPath concat(string, string, string*) function.
3556 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3557 *
3558 * @param[in] args Array of arguments.
3559 * @param[in] arg_count Count of elements in @p args.
3560 * @param[in,out] set Context and result set at the same time.
3561 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003562 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003563 */
3564static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003565xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003566{
3567 uint16_t i;
3568 char *str = NULL;
3569 size_t used = 1;
3570 LY_ERR rc = LY_SUCCESS;
3571 struct lysc_node_leaf *sleaf;
3572
3573 if (options & LYXP_SCNODE_ALL) {
3574 for (i = 0; i < arg_count; ++i) {
3575 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3576 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3577 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003578 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003579 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003580 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 +02003581 }
3582 }
3583 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003584 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003585 return rc;
3586 }
3587
3588 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003589 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003590 if (rc != LY_SUCCESS) {
3591 free(str);
3592 return rc;
3593 }
3594
3595 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3596 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3597 strcpy(str + used - 1, args[i]->val.str);
3598 used += strlen(args[i]->val.str);
3599 }
3600
3601 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003602 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003603 set->type = LYXP_SET_STRING;
3604 set->val.str = str;
3605
3606 return LY_SUCCESS;
3607}
3608
3609/**
3610 * @brief Execute the XPath contains(string, string) function.
3611 * Returns LYXP_SET_BOOLEAN whether the second argument can
3612 * be found in the first or not.
3613 *
3614 * @param[in] args Array of arguments.
3615 * @param[in] arg_count Count of elements in @p args.
3616 * @param[in,out] set Context and result set at the same time.
3617 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003618 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003619 */
3620static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003621xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003622{
3623 struct lysc_node_leaf *sleaf;
3624 LY_ERR rc = LY_SUCCESS;
3625
3626 if (options & LYXP_SCNODE_ALL) {
3627 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3628 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003629 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3630 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003631 } else if (!warn_is_string_type(sleaf->type)) {
3632 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003633 }
3634 }
3635
3636 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3637 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003638 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3639 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003640 } else if (!warn_is_string_type(sleaf->type)) {
3641 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003642 }
3643 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003644 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003645 return rc;
3646 }
3647
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003648 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003649 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003650 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003651 LY_CHECK_RET(rc);
3652
3653 if (strstr(args[0]->val.str, args[1]->val.str)) {
3654 set_fill_boolean(set, 1);
3655 } else {
3656 set_fill_boolean(set, 0);
3657 }
3658
3659 return LY_SUCCESS;
3660}
3661
3662/**
3663 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3664 * with the size of the node-set from the argument.
3665 *
3666 * @param[in] args Array of arguments.
3667 * @param[in] arg_count Count of elements in @p args.
3668 * @param[in,out] set Context and result set at the same time.
3669 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003670 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003671 */
3672static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003673xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003674{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003675 LY_ERR rc = LY_SUCCESS;
3676
3677 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003678 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003679 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003680 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003681 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003682 return rc;
3683 }
3684
Michal Vasko03ff5a72019-09-11 13:49:33 +02003685 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003686 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003687 return LY_EVALID;
3688 }
3689
3690 set_fill_number(set, args[0]->used);
3691 return LY_SUCCESS;
3692}
3693
3694/**
3695 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3696 * with the context with the intial node.
3697 *
3698 * @param[in] args Array of arguments.
3699 * @param[in] arg_count Count of elements in @p args.
3700 * @param[in,out] set Context and result set at the same time.
3701 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003702 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003703 */
3704static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003705xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003706{
3707 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003708 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003709 return LY_EVALID;
3710 }
3711
3712 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003713 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003714
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003715 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003716 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003717 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003718
3719 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003720 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003721 }
3722
3723 return LY_SUCCESS;
3724}
3725
3726/**
3727 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3728 * leafref or instance-identifier target node(s).
3729 *
3730 * @param[in] args Array of arguments.
3731 * @param[in] arg_count Count of elements in @p args.
3732 * @param[in,out] set Context and result set at the same time.
3733 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003734 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003735 */
3736static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003737xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003738{
3739 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003740 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003741 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003742 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003743 struct ly_path *p;
3744 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003745 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003746 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02003747 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003748
3749 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003750 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003751 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003752 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko423ba3f2021-07-19 13:08:50 +02003753 if (!(sleaf->nodetype & LYD_NODE_TERM)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003754 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3755 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02003756 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
3757 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003758 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3759 __func__, sleaf->name);
3760 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003761 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003762 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko423ba3f2021-07-19 13:08:50 +02003763 if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003764 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003765 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003766
3767 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02003768 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02003769 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02003770 if (!r) {
3771 /* get the target node */
3772 target = p[LY_ARRAY_COUNT(p) - 1].node;
3773 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02003774
Michal Vasko741bb562021-06-24 11:59:50 +02003775 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
3776 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003777 }
3778
Michal Vasko741bb562021-06-24 11:59:50 +02003779 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003780 }
3781
Michal Vaskod3678892020-05-21 10:06:58 +02003782 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003783 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003784 return LY_EVALID;
3785 }
3786
Michal Vaskod3678892020-05-21 10:06:58 +02003787 lyxp_set_free_content(set);
3788 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003789 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3790 sleaf = (struct lysc_node_leaf *)leaf->schema;
3791 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3792 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3793 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02003794 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01003795 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003796 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003797 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003798 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003799 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003800 } else {
3801 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003802 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003803 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003804 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003805 return LY_EVALID;
3806 }
3807 }
Michal Vasko004d3152020-06-11 19:59:22 +02003808
3809 /* insert it */
3810 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003811 }
3812 }
3813
3814 return LY_SUCCESS;
3815}
3816
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003817static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003818xpath_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 +02003819{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01003820 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003821 LY_ARRAY_COUNT_TYPE u;
3822 struct lyd_node_term *leaf;
3823 struct lysc_node_leaf *sleaf;
3824 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02003825 struct lyd_value *val;
3826 const struct lys_module *mod;
3827 const char *id_name;
3828 uint16_t id_len;
3829 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003830 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003831 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003832
3833 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003834 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003835 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003836 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3837 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3838 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3839 sleaf->name);
3840 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3841 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3842 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003843 }
3844
3845 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3846 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3847 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003848 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003849 } else if (!warn_is_string_type(sleaf->type)) {
3850 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3851 }
3852 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003853 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003854 return rc;
3855 }
3856
3857 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003858 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 +02003859 return LY_EVALID;
3860 }
3861 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3862 LY_CHECK_RET(rc);
3863
Michal Vasko93923692021-05-07 15:28:02 +02003864 /* parse the identity */
3865 id_name = args[1]->val.str;
3866 id_len = strlen(id_name);
3867 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
3868 LY_CHECK_RET(rc);
3869 if (!mod) {
3870 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
3871 return LY_EVALID;
3872 }
3873
3874 /* find the identity */
3875 found = 0;
3876 LY_ARRAY_FOR(mod->identities, u) {
3877 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
3878 /* we have match */
3879 found = 1;
3880 break;
3881 }
3882 }
3883 if (!found) {
3884 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
3885 return LY_EVALID;
3886 }
3887 id = &mod->identities[u];
3888
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003889 set_fill_boolean(set, 0);
3890 found = 0;
3891 for (i = 0; i < args[0]->used; ++i) {
3892 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3893 continue;
3894 }
3895
3896 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3897 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3898 sleaf = (struct lysc_node_leaf *)leaf->schema;
3899 val = &leaf->value;
3900 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3901 /* uninteresting */
3902 continue;
3903 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003904 } else {
3905 meta = args[0]->val.meta[i].meta;
3906 val = &meta->value;
3907 if (val->realtype->basetype != LY_TYPE_IDENT) {
3908 /* uninteresting */
3909 continue;
3910 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003911 }
3912
Michal Vasko93923692021-05-07 15:28:02 +02003913 /* check the identity itself */
3914 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003915 set_fill_boolean(set, 1);
3916 found = 1;
3917 }
Michal Vasko93923692021-05-07 15:28:02 +02003918 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
3919 set_fill_boolean(set, 1);
3920 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003921 }
3922
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003923 if (found) {
3924 break;
3925 }
3926 }
3927
3928 return LY_SUCCESS;
3929}
3930
Michal Vasko03ff5a72019-09-11 13:49:33 +02003931/**
3932 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3933 * on whether the first argument nodes contain a node of an identity derived from the second
3934 * argument identity.
3935 *
3936 * @param[in] args Array of arguments.
3937 * @param[in] arg_count Count of elements in @p args.
3938 * @param[in,out] set Context and result set at the same time.
3939 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003940 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003941 */
3942static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003943xpath_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 +02003944{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003945 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003946}
3947
3948/**
3949 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3950 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3951 * the second argument identity.
3952 *
3953 * @param[in] args Array of arguments.
3954 * @param[in] arg_count Count of elements in @p args.
3955 * @param[in,out] set Context and result set at the same time.
3956 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003957 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003958 */
3959static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003960xpath_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 +02003961{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003962 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003963}
3964
3965/**
3966 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3967 * with the integer value of the first node's enum value, otherwise NaN.
3968 *
3969 * @param[in] args Array of arguments.
3970 * @param[in] arg_count Count of elements in @p args.
3971 * @param[in,out] set Context and result set at the same time.
3972 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003973 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003974 */
3975static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003976xpath_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 +02003977{
3978 struct lyd_node_term *leaf;
3979 struct lysc_node_leaf *sleaf;
3980 LY_ERR rc = LY_SUCCESS;
3981
3982 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003983 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003984 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003985 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3986 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3987 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3988 sleaf->name);
3989 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3990 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
3991 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003992 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003993 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003994 return rc;
3995 }
3996
Michal Vaskod3678892020-05-21 10:06:58 +02003997 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003998 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003999 return LY_EVALID;
4000 }
4001
4002 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004003 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004004 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4005 sleaf = (struct lysc_node_leaf *)leaf->schema;
4006 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4007 set_fill_number(set, leaf->value.enum_item->value);
4008 }
4009 }
4010
4011 return LY_SUCCESS;
4012}
4013
4014/**
4015 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4016 * with false value.
4017 *
4018 * @param[in] args Array of arguments.
4019 * @param[in] arg_count Count of elements in @p args.
4020 * @param[in,out] set Context and result set at the same time.
4021 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004022 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004023 */
4024static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004025xpath_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 +02004026{
4027 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004028 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004029 return LY_SUCCESS;
4030 }
4031
4032 set_fill_boolean(set, 0);
4033 return LY_SUCCESS;
4034}
4035
4036/**
4037 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4038 * with the first argument floored (truncated).
4039 *
4040 * @param[in] args Array of arguments.
4041 * @param[in] arg_count Count of elements in @p args.
4042 * @param[in,out] set Context and result set at the same time.
4043 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004044 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004045 */
4046static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004047xpath_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 +02004048{
4049 LY_ERR rc;
4050
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004051 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004052 LY_CHECK_RET(rc);
4053 if (isfinite(args[0]->val.num)) {
4054 set_fill_number(set, (long long)args[0]->val.num);
4055 }
4056
4057 return LY_SUCCESS;
4058}
4059
4060/**
4061 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4062 * whether the language of the text matches the one from the argument.
4063 *
4064 * @param[in] args Array of arguments.
4065 * @param[in] arg_count Count of elements in @p args.
4066 * @param[in,out] set Context and result set at the same time.
4067 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004068 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004069 */
4070static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004071xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004072{
4073 const struct lyd_node *node;
4074 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004075 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004076 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004077 LY_ERR rc = LY_SUCCESS;
4078
4079 if (options & LYXP_SCNODE_ALL) {
4080 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4081 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004082 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4083 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004084 } else if (!warn_is_string_type(sleaf->type)) {
4085 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004086 }
4087 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004088 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004089 return rc;
4090 }
4091
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004092 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004093 LY_CHECK_RET(rc);
4094
Michal Vasko03ff5a72019-09-11 13:49:33 +02004095 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004096 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004097 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004098 } else if (!set->used) {
4099 set_fill_boolean(set, 0);
4100 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004101 }
4102
4103 switch (set->val.nodes[0].type) {
4104 case LYXP_NODE_ELEM:
4105 case LYXP_NODE_TEXT:
4106 node = set->val.nodes[0].node;
4107 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004108 case LYXP_NODE_META:
4109 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004110 break;
4111 default:
4112 /* nothing to do with roots */
4113 set_fill_boolean(set, 0);
4114 return LY_SUCCESS;
4115 }
4116
Michal Vasko9f96a052020-03-10 09:41:45 +01004117 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004118 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004119 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004120 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004121 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004122 break;
4123 }
4124 }
4125
Michal Vasko9f96a052020-03-10 09:41:45 +01004126 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004127 break;
4128 }
4129 }
4130
4131 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004132 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004133 set_fill_boolean(set, 0);
4134 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004135 uint64_t i;
4136
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004137 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004138 for (i = 0; args[0]->val.str[i]; ++i) {
4139 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4140 set_fill_boolean(set, 0);
4141 break;
4142 }
4143 }
4144 if (!args[0]->val.str[i]) {
4145 if (!val[i] || (val[i] == '-')) {
4146 set_fill_boolean(set, 1);
4147 } else {
4148 set_fill_boolean(set, 0);
4149 }
4150 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004151 }
4152
4153 return LY_SUCCESS;
4154}
4155
4156/**
4157 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4158 * with the context size.
4159 *
4160 * @param[in] args Array of arguments.
4161 * @param[in] arg_count Count of elements in @p args.
4162 * @param[in,out] set Context and result set at the same time.
4163 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004164 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004165 */
4166static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004167xpath_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 +02004168{
4169 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004170 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004171 return LY_SUCCESS;
4172 }
4173
Michal Vasko03ff5a72019-09-11 13:49:33 +02004174 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004175 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004176 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004177 } else if (!set->used) {
4178 set_fill_number(set, 0);
4179 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004180 }
4181
4182 set_fill_number(set, set->ctx_size);
4183 return LY_SUCCESS;
4184}
4185
4186/**
4187 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4188 * with the node name without namespace from the argument or the context.
4189 *
4190 * @param[in] args Array of arguments.
4191 * @param[in] arg_count Count of elements in @p args.
4192 * @param[in,out] set Context and result set at the same time.
4193 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004194 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004195 */
4196static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004197xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004198{
4199 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004200
Michal Vasko03ff5a72019-09-11 13:49:33 +02004201 /* suppress unused variable warning */
4202 (void)options;
4203
4204 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004205 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004206 return LY_SUCCESS;
4207 }
4208
4209 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004210 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004211 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004212 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004213 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004214 } else if (!args[0]->used) {
4215 set_fill_string(set, "", 0);
4216 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004217 }
4218
4219 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004220 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004221
4222 item = &args[0]->val.nodes[0];
4223 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004224 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004225 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004226 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004227 } else if (!set->used) {
4228 set_fill_string(set, "", 0);
4229 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004230 }
4231
4232 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004233 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004234
4235 item = &set->val.nodes[0];
4236 }
4237
4238 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004239 case LYXP_NODE_NONE:
4240 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004241 case LYXP_NODE_ROOT:
4242 case LYXP_NODE_ROOT_CONFIG:
4243 case LYXP_NODE_TEXT:
4244 set_fill_string(set, "", 0);
4245 break;
4246 case LYXP_NODE_ELEM:
4247 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4248 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004249 case LYXP_NODE_META:
4250 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004251 break;
4252 }
4253
4254 return LY_SUCCESS;
4255}
4256
4257/**
4258 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4259 * with the node name fully qualified (with namespace) from the argument or the context.
4260 *
4261 * @param[in] args Array of arguments.
4262 * @param[in] arg_count Count of elements in @p args.
4263 * @param[in,out] set Context and result set at the same time.
4264 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004265 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004266 */
4267static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004268xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004269{
4270 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004271 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004272 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004273 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004274
4275 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004276 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004277 return LY_SUCCESS;
4278 }
4279
4280 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004281 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004282 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004283 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004284 } else if (!args[0]->used) {
4285 set_fill_string(set, "", 0);
4286 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004287 }
4288
4289 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004290 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004291
4292 item = &args[0]->val.nodes[0];
4293 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004294 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004295 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004296 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004297 } else if (!set->used) {
4298 set_fill_string(set, "", 0);
4299 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004300 }
4301
4302 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004303 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004304
4305 item = &set->val.nodes[0];
4306 }
4307
4308 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004309 case LYXP_NODE_NONE:
4310 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004311 case LYXP_NODE_ROOT:
4312 case LYXP_NODE_ROOT_CONFIG:
4313 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004314 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004315 break;
4316 case LYXP_NODE_ELEM:
4317 mod = item->node->schema->module;
4318 name = item->node->schema->name;
4319 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004320 case LYXP_NODE_META:
4321 mod = ((struct lyd_meta *)item->node)->annotation->module;
4322 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004323 break;
4324 }
4325
4326 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004327 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004328 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4329 set_fill_string(set, str, strlen(str));
4330 free(str);
4331 } else {
4332 set_fill_string(set, "", 0);
4333 }
4334
4335 return LY_SUCCESS;
4336}
4337
4338/**
4339 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4340 * with the namespace of the node from the argument or the context.
4341 *
4342 * @param[in] args Array of arguments.
4343 * @param[in] arg_count Count of elements in @p args.
4344 * @param[in,out] set Context and result set at the same time.
4345 * @param[in] options XPath options.
4346 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4347 */
4348static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004349xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004350{
4351 struct lyxp_set_node *item;
4352 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004353
Michal Vasko03ff5a72019-09-11 13:49:33 +02004354 /* suppress unused variable warning */
4355 (void)options;
4356
4357 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004358 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004359 return LY_SUCCESS;
4360 }
4361
4362 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004363 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004364 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004365 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004366 return LY_EVALID;
4367 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004368 set_fill_string(set, "", 0);
4369 return LY_SUCCESS;
4370 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004371
4372 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004373 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004374
4375 item = &args[0]->val.nodes[0];
4376 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004377 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004378 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004379 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004380 } else if (!set->used) {
4381 set_fill_string(set, "", 0);
4382 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004383 }
4384
4385 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004386 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004387
4388 item = &set->val.nodes[0];
4389 }
4390
4391 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004392 case LYXP_NODE_NONE:
4393 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004394 case LYXP_NODE_ROOT:
4395 case LYXP_NODE_ROOT_CONFIG:
4396 case LYXP_NODE_TEXT:
4397 set_fill_string(set, "", 0);
4398 break;
4399 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004400 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004401 if (item->type == LYXP_NODE_ELEM) {
4402 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004403 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004404 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004405 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004406 }
4407
4408 set_fill_string(set, mod->ns, strlen(mod->ns));
4409 break;
4410 }
4411
4412 return LY_SUCCESS;
4413}
4414
4415/**
4416 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4417 * with only nodes from the context. In practice it either leaves the context
4418 * as it is or returns an empty node set.
4419 *
4420 * @param[in] args Array of arguments.
4421 * @param[in] arg_count Count of elements in @p args.
4422 * @param[in,out] set Context and result set at the same time.
4423 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004424 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004425 */
4426static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004427xpath_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 +02004428{
4429 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004430 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004431 return LY_SUCCESS;
4432 }
4433
4434 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004435 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004436 }
4437 return LY_SUCCESS;
4438}
4439
4440/**
4441 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4442 * with normalized value (no leading, trailing, double white spaces) of the node
4443 * from the argument or the context.
4444 *
4445 * @param[in] args Array of arguments.
4446 * @param[in] arg_count Count of elements in @p args.
4447 * @param[in,out] set Context and result set at the same time.
4448 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004449 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004450 */
4451static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004452xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004453{
4454 uint16_t i, new_used;
4455 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004456 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004457 struct lysc_node_leaf *sleaf;
4458 LY_ERR rc = LY_SUCCESS;
4459
4460 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004461 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4462 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004463 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004464 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4465 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004466 } else if (!warn_is_string_type(sleaf->type)) {
4467 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004468 }
4469 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004470 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004471 return rc;
4472 }
4473
4474 if (arg_count) {
4475 set_fill_set(set, args[0]);
4476 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004477 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004478 LY_CHECK_RET(rc);
4479
4480 /* is there any normalization necessary? */
4481 for (i = 0; set->val.str[i]; ++i) {
4482 if (is_xmlws(set->val.str[i])) {
4483 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4484 have_spaces = 1;
4485 break;
4486 }
4487 space_before = 1;
4488 } else {
4489 space_before = 0;
4490 }
4491 }
4492
4493 /* yep, there is */
4494 if (have_spaces) {
4495 /* it's enough, at least one character will go, makes space for ending '\0' */
4496 new = malloc(strlen(set->val.str) * sizeof(char));
4497 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4498 new_used = 0;
4499
4500 space_before = 0;
4501 for (i = 0; set->val.str[i]; ++i) {
4502 if (is_xmlws(set->val.str[i])) {
4503 if ((i == 0) || space_before) {
4504 space_before = 1;
4505 continue;
4506 } else {
4507 space_before = 1;
4508 }
4509 } else {
4510 space_before = 0;
4511 }
4512
4513 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4514 ++new_used;
4515 }
4516
4517 /* at worst there is one trailing space now */
4518 if (new_used && is_xmlws(new[new_used - 1])) {
4519 --new_used;
4520 }
4521
4522 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4523 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4524 new[new_used] = '\0';
4525
4526 free(set->val.str);
4527 set->val.str = new;
4528 }
4529
4530 return LY_SUCCESS;
4531}
4532
4533/**
4534 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4535 * with the argument converted to boolean and logically inverted.
4536 *
4537 * @param[in] args Array of arguments.
4538 * @param[in] arg_count Count of elements in @p args.
4539 * @param[in,out] set Context and result set at the same time.
4540 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004541 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004542 */
4543static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004544xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004545{
4546 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004547 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004548 return LY_SUCCESS;
4549 }
4550
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004551 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004552 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004553 set_fill_boolean(set, 0);
4554 } else {
4555 set_fill_boolean(set, 1);
4556 }
4557
4558 return LY_SUCCESS;
4559}
4560
4561/**
4562 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4563 * with the number representation of either the argument or the context.
4564 *
4565 * @param[in] args Array of arguments.
4566 * @param[in] arg_count Count of elements in @p args.
4567 * @param[in,out] set Context and result set at the same time.
4568 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004569 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004570 */
4571static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004572xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004573{
4574 LY_ERR rc;
4575
4576 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004577 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004578 return LY_SUCCESS;
4579 }
4580
4581 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004582 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004583 LY_CHECK_RET(rc);
4584 set_fill_set(set, args[0]);
4585 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004586 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004587 LY_CHECK_RET(rc);
4588 }
4589
4590 return LY_SUCCESS;
4591}
4592
4593/**
4594 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4595 * with the context position.
4596 *
4597 * @param[in] args Array of arguments.
4598 * @param[in] arg_count Count of elements in @p args.
4599 * @param[in,out] set Context and result set at the same time.
4600 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004601 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004602 */
4603static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004604xpath_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 +02004605{
4606 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004607 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004608 return LY_SUCCESS;
4609 }
4610
Michal Vasko03ff5a72019-09-11 13:49:33 +02004611 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004612 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004613 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004614 } else if (!set->used) {
4615 set_fill_number(set, 0);
4616 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004617 }
4618
4619 set_fill_number(set, set->ctx_pos);
4620
4621 /* UNUSED in 'Release' build type */
4622 (void)options;
4623 return LY_SUCCESS;
4624}
4625
4626/**
4627 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4628 * depending on whether the second argument regex matches the first argument string. For details refer to
4629 * YANG 1.1 RFC section 10.2.1.
4630 *
4631 * @param[in] args Array of arguments.
4632 * @param[in] arg_count Count of elements in @p args.
4633 * @param[in,out] set Context and result set at the same time.
4634 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004635 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004636 */
4637static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004638xpath_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 +02004639{
4640 struct lysc_pattern **patterns = NULL, **pattern;
4641 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004642 LY_ERR rc = LY_SUCCESS;
4643 struct ly_err_item *err;
4644
4645 if (options & LYXP_SCNODE_ALL) {
4646 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4647 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4648 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 +02004649 } else if (!warn_is_string_type(sleaf->type)) {
4650 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004651 }
4652 }
4653
4654 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4655 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4656 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 +02004657 } else if (!warn_is_string_type(sleaf->type)) {
4658 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004659 }
4660 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004661 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004662 return rc;
4663 }
4664
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004665 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004666 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004667 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004668 LY_CHECK_RET(rc);
4669
4670 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek IÅ¡a45802b52021-02-09 09:21:58 +01004671 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004672 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004673 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Radek Krejciddace2c2021-01-08 11:30:56 +01004674 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004675 if (rc != LY_SUCCESS) {
4676 LY_ARRAY_FREE(patterns);
4677 return rc;
4678 }
4679
Radek Krejci0b013302021-03-29 15:22:32 +02004680 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004681 pcre2_code_free((*pattern)->code);
4682 free(*pattern);
4683 LY_ARRAY_FREE(patterns);
4684 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004685 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004686 ly_err_free(err);
4687 return rc;
4688 }
4689
4690 if (rc == LY_EVALID) {
4691 ly_err_free(err);
4692 set_fill_boolean(set, 0);
4693 } else {
4694 set_fill_boolean(set, 1);
4695 }
4696
4697 return LY_SUCCESS;
4698}
4699
4700/**
4701 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4702 * with the rounded first argument. For details refer to
4703 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4704 *
4705 * @param[in] args Array of arguments.
4706 * @param[in] arg_count Count of elements in @p args.
4707 * @param[in,out] set Context and result set at the same time.
4708 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004709 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004710 */
4711static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004712xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004713{
4714 struct lysc_node_leaf *sleaf;
4715 LY_ERR rc = LY_SUCCESS;
4716
4717 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004718 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004719 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004720 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4721 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4722 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4723 sleaf->name);
4724 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4725 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4726 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004727 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004728 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004729 return rc;
4730 }
4731
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004732 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004733 LY_CHECK_RET(rc);
4734
4735 /* cover only the cases where floor can't be used */
4736 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4737 set_fill_number(set, -0.0f);
4738 } else {
4739 args[0]->val.num += 0.5;
4740 rc = xpath_floor(args, 1, args[0], options);
4741 LY_CHECK_RET(rc);
4742 set_fill_number(set, args[0]->val.num);
4743 }
4744
4745 return LY_SUCCESS;
4746}
4747
4748/**
4749 * @brief Execute the XPath starts-with(string, string) function.
4750 * Returns LYXP_SET_BOOLEAN whether the second argument is
4751 * the prefix of the first or not.
4752 *
4753 * @param[in] args Array of arguments.
4754 * @param[in] arg_count Count of elements in @p args.
4755 * @param[in,out] set Context and result set at the same time.
4756 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004757 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004758 */
4759static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004760xpath_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 +02004761{
4762 struct lysc_node_leaf *sleaf;
4763 LY_ERR rc = LY_SUCCESS;
4764
4765 if (options & LYXP_SCNODE_ALL) {
4766 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4767 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4768 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 +02004769 } else if (!warn_is_string_type(sleaf->type)) {
4770 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004771 }
4772 }
4773
4774 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4775 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4776 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 +02004777 } else if (!warn_is_string_type(sleaf->type)) {
4778 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004779 }
4780 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004781 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004782 return rc;
4783 }
4784
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004785 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004786 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004787 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004788 LY_CHECK_RET(rc);
4789
4790 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4791 set_fill_boolean(set, 0);
4792 } else {
4793 set_fill_boolean(set, 1);
4794 }
4795
4796 return LY_SUCCESS;
4797}
4798
4799/**
4800 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4801 * with the string representation of either the argument or the context.
4802 *
4803 * @param[in] args Array of arguments.
4804 * @param[in] arg_count Count of elements in @p args.
4805 * @param[in,out] set Context and result set at the same time.
4806 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004807 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004808 */
4809static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004810xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004811{
4812 LY_ERR rc;
4813
4814 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004815 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004816 return LY_SUCCESS;
4817 }
4818
4819 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004820 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004821 LY_CHECK_RET(rc);
4822 set_fill_set(set, args[0]);
4823 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004824 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004825 LY_CHECK_RET(rc);
4826 }
4827
4828 return LY_SUCCESS;
4829}
4830
4831/**
4832 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4833 * with the length of the string in either the argument or the context.
4834 *
4835 * @param[in] args Array of arguments.
4836 * @param[in] arg_count Count of elements in @p args.
4837 * @param[in,out] set Context and result set at the same time.
4838 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004839 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004840 */
4841static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004842xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004843{
4844 struct lysc_node_leaf *sleaf;
4845 LY_ERR rc = LY_SUCCESS;
4846
4847 if (options & LYXP_SCNODE_ALL) {
4848 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4849 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4850 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 +02004851 } else if (!warn_is_string_type(sleaf->type)) {
4852 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004853 }
4854 }
4855 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4856 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4857 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 +02004858 } else if (!warn_is_string_type(sleaf->type)) {
4859 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004860 }
4861 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004862 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004863 return rc;
4864 }
4865
4866 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004867 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004868 LY_CHECK_RET(rc);
4869 set_fill_number(set, strlen(args[0]->val.str));
4870 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004871 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004872 LY_CHECK_RET(rc);
4873 set_fill_number(set, strlen(set->val.str));
4874 }
4875
4876 return LY_SUCCESS;
4877}
4878
4879/**
4880 * @brief Execute the XPath substring(string, number, number?) function.
4881 * Returns LYXP_SET_STRING substring of the first argument starting
4882 * on the second argument index ending on the third argument index,
4883 * indexed from 1. For exact definition refer to
4884 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4885 *
4886 * @param[in] args Array of arguments.
4887 * @param[in] arg_count Count of elements in @p args.
4888 * @param[in,out] set Context and result set at the same time.
4889 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004890 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004891 */
4892static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004893xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004894{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004895 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004896 uint16_t str_start, str_len, pos;
4897 struct lysc_node_leaf *sleaf;
4898 LY_ERR rc = LY_SUCCESS;
4899
4900 if (options & LYXP_SCNODE_ALL) {
4901 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4902 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4903 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 +02004904 } else if (!warn_is_string_type(sleaf->type)) {
4905 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004906 }
4907 }
4908
4909 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4910 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4911 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 +02004912 } else if (!warn_is_numeric_type(sleaf->type)) {
4913 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004914 }
4915 }
4916
Michal Vasko69730152020-10-09 16:30:07 +02004917 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4918 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004919 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4920 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 +02004921 } else if (!warn_is_numeric_type(sleaf->type)) {
4922 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004923 }
4924 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004925 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004926 return rc;
4927 }
4928
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004929 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004930 LY_CHECK_RET(rc);
4931
4932 /* start */
4933 if (xpath_round(&args[1], 1, args[1], options)) {
4934 return -1;
4935 }
4936 if (isfinite(args[1]->val.num)) {
4937 start = args[1]->val.num - 1;
4938 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004939 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004940 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004941 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004942 }
4943
4944 /* len */
4945 if (arg_count == 3) {
4946 rc = xpath_round(&args[2], 1, args[2], options);
4947 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004948 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004949 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004950 } else if (isfinite(args[2]->val.num)) {
4951 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004952 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004953 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004954 }
4955 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004956 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004957 }
4958
4959 /* find matching character positions */
4960 str_start = 0;
4961 str_len = 0;
4962 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4963 if (pos < start) {
4964 ++str_start;
4965 } else if (pos < start + len) {
4966 ++str_len;
4967 } else {
4968 break;
4969 }
4970 }
4971
4972 set_fill_string(set, args[0]->val.str + str_start, str_len);
4973 return LY_SUCCESS;
4974}
4975
4976/**
4977 * @brief Execute the XPath substring-after(string, string) function.
4978 * Returns LYXP_SET_STRING with the string succeeding the occurance
4979 * of the second argument in the first or an empty string.
4980 *
4981 * @param[in] args Array of arguments.
4982 * @param[in] arg_count Count of elements in @p args.
4983 * @param[in,out] set Context and result set at the same time.
4984 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004985 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004986 */
4987static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004988xpath_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 +02004989{
4990 char *ptr;
4991 struct lysc_node_leaf *sleaf;
4992 LY_ERR rc = LY_SUCCESS;
4993
4994 if (options & LYXP_SCNODE_ALL) {
4995 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4996 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4997 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 +02004998 } else if (!warn_is_string_type(sleaf->type)) {
4999 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005000 }
5001 }
5002
5003 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5004 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5005 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 +02005006 } else if (!warn_is_string_type(sleaf->type)) {
5007 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005008 }
5009 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005010 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005011 return rc;
5012 }
5013
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005014 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005015 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005016 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005017 LY_CHECK_RET(rc);
5018
5019 ptr = strstr(args[0]->val.str, args[1]->val.str);
5020 if (ptr) {
5021 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5022 } else {
5023 set_fill_string(set, "", 0);
5024 }
5025
5026 return LY_SUCCESS;
5027}
5028
5029/**
5030 * @brief Execute the XPath substring-before(string, string) function.
5031 * Returns LYXP_SET_STRING with the string preceding the occurance
5032 * of the second argument in the first or an empty string.
5033 *
5034 * @param[in] args Array of arguments.
5035 * @param[in] arg_count Count of elements in @p args.
5036 * @param[in,out] set Context and result set at the same time.
5037 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005038 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005039 */
5040static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005041xpath_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 +02005042{
5043 char *ptr;
5044 struct lysc_node_leaf *sleaf;
5045 LY_ERR rc = LY_SUCCESS;
5046
5047 if (options & LYXP_SCNODE_ALL) {
5048 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5049 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5050 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 +02005051 } else if (!warn_is_string_type(sleaf->type)) {
5052 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005053 }
5054 }
5055
5056 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5057 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5058 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 +02005059 } else if (!warn_is_string_type(sleaf->type)) {
5060 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005061 }
5062 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005063 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005064 return rc;
5065 }
5066
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005067 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005068 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005069 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005070 LY_CHECK_RET(rc);
5071
5072 ptr = strstr(args[0]->val.str, args[1]->val.str);
5073 if (ptr) {
5074 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5075 } else {
5076 set_fill_string(set, "", 0);
5077 }
5078
5079 return LY_SUCCESS;
5080}
5081
5082/**
5083 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5084 * with the sum of all the nodes in the context.
5085 *
5086 * @param[in] args Array of arguments.
5087 * @param[in] arg_count Count of elements in @p args.
5088 * @param[in,out] set Context and result set at the same time.
5089 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005090 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005091 */
5092static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005093xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005094{
5095 long double num;
5096 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005097 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005098 struct lyxp_set set_item;
5099 struct lysc_node_leaf *sleaf;
5100 LY_ERR rc = LY_SUCCESS;
5101
5102 if (options & LYXP_SCNODE_ALL) {
5103 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5104 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005105 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005106 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5107 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5108 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005109 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005110 } else if (!warn_is_numeric_type(sleaf->type)) {
5111 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005112 }
5113 }
5114 }
5115 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005116 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005117 return rc;
5118 }
5119
5120 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005121
5122 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005123 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005124 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005125 } else if (!args[0]->used) {
5126 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005127 }
5128
Michal Vasko5c4e5892019-11-14 12:31:38 +01005129 set_init(&set_item, set);
5130
Michal Vasko03ff5a72019-09-11 13:49:33 +02005131 set_item.type = LYXP_SET_NODE_SET;
5132 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5133 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5134
5135 set_item.used = 1;
5136 set_item.size = 1;
5137
5138 for (i = 0; i < args[0]->used; ++i) {
5139 set_item.val.nodes[0] = args[0]->val.nodes[i];
5140
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005141 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005142 LY_CHECK_RET(rc);
5143 num = cast_string_to_number(str);
5144 free(str);
5145 set->val.num += num;
5146 }
5147
5148 free(set_item.val.nodes);
5149
5150 return LY_SUCCESS;
5151}
5152
5153/**
5154 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5155 * with the text content of the nodes in the context.
5156 *
5157 * @param[in] args Array of arguments.
5158 * @param[in] arg_count Count of elements in @p args.
5159 * @param[in,out] set Context and result set at the same time.
5160 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005161 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005162 */
5163static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005164xpath_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 +02005165{
5166 uint32_t i;
5167
5168 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005169 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005170 return LY_SUCCESS;
5171 }
5172
Michal Vasko03ff5a72019-09-11 13:49:33 +02005173 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005174 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005175 return LY_EVALID;
5176 }
5177
Michal Vaskod989ba02020-08-24 10:59:24 +02005178 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005179 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005180 case LYXP_NODE_NONE:
5181 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005182 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005183 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5184 set->val.nodes[i].type = LYXP_NODE_TEXT;
5185 ++i;
5186 break;
5187 }
Radek Krejci0f969882020-08-21 16:56:47 +02005188 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005189 case LYXP_NODE_ROOT:
5190 case LYXP_NODE_ROOT_CONFIG:
5191 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005192 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005193 set_remove_node(set, i);
5194 break;
5195 }
5196 }
5197
5198 return LY_SUCCESS;
5199}
5200
5201/**
5202 * @brief Execute the XPath translate(string, string, string) function.
5203 * Returns LYXP_SET_STRING with the first argument with the characters
5204 * from the second argument replaced by those on the corresponding
5205 * positions in the third argument.
5206 *
5207 * @param[in] args Array of arguments.
5208 * @param[in] arg_count Count of elements in @p args.
5209 * @param[in,out] set Context and result set at the same time.
5210 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005211 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005212 */
5213static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005214xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005215{
5216 uint16_t i, j, new_used;
5217 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005218 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005219 struct lysc_node_leaf *sleaf;
5220 LY_ERR rc = LY_SUCCESS;
5221
5222 if (options & LYXP_SCNODE_ALL) {
5223 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5224 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5225 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 +02005226 } else if (!warn_is_string_type(sleaf->type)) {
5227 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005228 }
5229 }
5230
5231 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5232 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5233 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 +02005234 } else if (!warn_is_string_type(sleaf->type)) {
5235 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005236 }
5237 }
5238
5239 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5240 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5241 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 +02005242 } else if (!warn_is_string_type(sleaf->type)) {
5243 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005244 }
5245 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005246 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005247 return rc;
5248 }
5249
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005250 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005251 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005252 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005253 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005254 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005255 LY_CHECK_RET(rc);
5256
5257 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5258 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5259 new_used = 0;
5260
5261 have_removed = 0;
5262 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005263 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005264
5265 for (j = 0; args[1]->val.str[j]; ++j) {
5266 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5267 /* removing this char */
5268 if (j >= strlen(args[2]->val.str)) {
5269 have_removed = 1;
5270 found = 1;
5271 break;
5272 }
5273 /* replacing this char */
5274 new[new_used] = args[2]->val.str[j];
5275 ++new_used;
5276 found = 1;
5277 break;
5278 }
5279 }
5280
5281 /* copying this char */
5282 if (!found) {
5283 new[new_used] = args[0]->val.str[i];
5284 ++new_used;
5285 }
5286 }
5287
5288 if (have_removed) {
5289 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5290 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5291 }
5292 new[new_used] = '\0';
5293
Michal Vaskod3678892020-05-21 10:06:58 +02005294 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005295 set->type = LYXP_SET_STRING;
5296 set->val.str = new;
5297
5298 return LY_SUCCESS;
5299}
5300
5301/**
5302 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5303 * with true value.
5304 *
5305 * @param[in] args Array of arguments.
5306 * @param[in] arg_count Count of elements in @p args.
5307 * @param[in,out] set Context and result set at the same time.
5308 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005309 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005310 */
5311static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005312xpath_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 +02005313{
5314 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005315 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005316 return LY_SUCCESS;
5317 }
5318
5319 set_fill_boolean(set, 1);
5320 return LY_SUCCESS;
5321}
5322
5323/*
5324 * moveto functions
5325 *
5326 * They and only they actually change the context (set).
5327 */
5328
5329/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005330 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005331 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005332 * XPath @p set is expected to be a (sc)node set!
5333 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005334 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5335 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005336 * @param[in] set Set with general XPath context.
5337 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005338 * @param[out] moveto_mod Expected module of a matching node.
5339 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005340 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005341static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005342moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5343 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005344{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005345 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005346 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005347 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005348
Michal Vasko2104e9f2020-03-06 08:23:25 +01005349 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5350
Michal Vasko6346ece2019-09-24 13:12:53 +02005351 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5352 /* specific module */
5353 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005354 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005355
Michal Vasko004d3152020-06-11 19:59:22 +02005356 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005357 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005358 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005359 return LY_EVALID;
5360 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005361
Michal Vasko6346ece2019-09-24 13:12:53 +02005362 *qname += pref_len + 1;
5363 *qname_len -= pref_len + 1;
5364 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5365 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005366 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005367 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005368 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005369 case LY_VALUE_SCHEMA:
5370 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005371 /* current module */
5372 mod = set->cur_mod;
5373 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005374 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005375 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005376 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005377 /* inherit parent (context node) module */
5378 if (ctx_scnode) {
5379 mod = ctx_scnode->module;
5380 } else {
5381 mod = NULL;
5382 }
5383 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005384 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005385 /* all nodes need to be prefixed */
5386 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5387 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005388 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005389 }
5390
Michal Vasko6346ece2019-09-24 13:12:53 +02005391 *moveto_mod = mod;
5392 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005393}
5394
5395/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005396 * @brief Move context @p set to the root. Handles absolute path.
5397 * Result is LYXP_SET_NODE_SET.
5398 *
5399 * @param[in,out] set Set to use.
5400 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005401 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005402 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005403static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005404moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005405{
aPiecek8b0cc152021-05-31 16:40:31 +02005406 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005407
5408 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005409 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskob0099a92020-08-31 14:55:23 +02005410 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005411 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005412 set->type = LYXP_SET_NODE_SET;
5413 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005414 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005415 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005416
5417 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005418}
5419
5420/**
5421 * @brief Check @p node as a part of NameTest processing.
5422 *
5423 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005424 * @param[in] ctx_node Context node.
5425 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005426 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005427 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005428 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005429 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5430 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005431 */
5432static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005433moveto_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 +01005434 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005435{
Michal Vaskodca9f122021-07-16 13:56:22 +02005436 if (!node->schema) {
5437 /* opaque node never matches */
5438 return LY_ENOT;
5439 }
5440
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005441 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5442 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005443 case LY_VALUE_SCHEMA:
5444 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005445 /* use current module */
5446 moveto_mod = set->cur_mod;
5447 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005448 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005449 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005450 /* inherit module of the context node, if any */
5451 if (ctx_node) {
5452 moveto_mod = ctx_node->schema->module;
5453 }
5454 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005455 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005456 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005457 /* not defined */
5458 LOGINT(set->ctx);
5459 return LY_EINVAL;
5460 }
5461 }
5462
Michal Vasko03ff5a72019-09-11 13:49:33 +02005463 /* module check */
5464 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005465 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005466 }
5467
Michal Vasko5c4e5892019-11-14 12:31:38 +01005468 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005469 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005470 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005471 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5472 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005473 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005474 }
5475
5476 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005477 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005478 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005479 }
5480
Michal Vaskoa1424542019-11-14 16:08:52 +01005481 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005482 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005483 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005484 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005485
5486 /* match */
5487 return LY_SUCCESS;
5488}
5489
5490/**
5491 * @brief Check @p node as a part of schema NameTest processing.
5492 *
5493 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005494 * @param[in] ctx_scnode Context node.
5495 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005496 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005497 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005498 * @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 +02005499 */
5500static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005501moveto_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 +02005502 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005503{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005504 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5505 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005506 case LY_VALUE_SCHEMA:
5507 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005508 /* use current module */
5509 moveto_mod = set->cur_mod;
5510 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005511 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005512 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005513 /* inherit module of the context node, if any */
5514 if (ctx_scnode) {
5515 moveto_mod = ctx_scnode->module;
5516 }
5517 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005518 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005519 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005520 /* not defined */
5521 LOGINT(set->ctx);
5522 return LY_EINVAL;
5523 }
5524 }
5525
Michal Vasko03ff5a72019-09-11 13:49:33 +02005526 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005527 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005528 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005529 }
5530
5531 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005532 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005533 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005534 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005535 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005536 }
5537
5538 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005539 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005540 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005541 }
5542
5543 /* match */
5544 return LY_SUCCESS;
5545}
5546
5547/**
Michal Vaskod3678892020-05-21 10:06:58 +02005548 * @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 +02005549 *
5550 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005551 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005552 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005553 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005554 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5555 */
5556static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005557moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005558{
Michal Vaskof03ed032020-03-04 13:31:44 +01005559 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005560 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005561 LY_ERR rc;
5562
aPiecek8b0cc152021-05-31 16:40:31 +02005563 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005564 return LY_SUCCESS;
5565 }
5566
5567 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005568 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005569 return LY_EVALID;
5570 }
5571
Michal Vasko03ff5a72019-09-11 13:49:33 +02005572 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005573 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005574
5575 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 +01005576 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005577
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005578 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005579 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005580 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005581 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005582 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005583 ctx_node = set->val.nodes[i].node;
5584 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005585 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005586
Michal Vaskod3678892020-05-21 10:06:58 +02005587 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005588 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005589 if (rc == LY_SUCCESS) {
5590 if (!replaced) {
5591 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5592 replaced = 1;
5593 } else {
5594 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005595 }
Michal Vaskod3678892020-05-21 10:06:58 +02005596 ++i;
5597 } else if (rc == LY_EINCOMPLETE) {
5598 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005599 }
5600 }
5601
5602 if (!replaced) {
5603 /* no match */
5604 set_remove_node(set, i);
5605 }
5606 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005607
5608 return LY_SUCCESS;
5609}
5610
5611/**
Michal Vaskod3678892020-05-21 10:06:58 +02005612 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5613 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005614 *
5615 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005616 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005617 * @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 +01005618 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005619 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5620 */
5621static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005622moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5623 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005624{
Michal Vasko004d3152020-06-11 19:59:22 +02005625 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005626 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005627 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005628 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005629
Michal Vasko004d3152020-06-11 19:59:22 +02005630 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005631
aPiecek8b0cc152021-05-31 16:40:31 +02005632 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02005633 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005634 }
5635
5636 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005637 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005638 ret = LY_EVALID;
5639 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005640 }
5641
5642 /* context check for all the nodes since we have the schema node */
5643 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5644 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005645 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005646 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5647 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005648 lyxp_set_free_content(set);
5649 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005650 }
5651
5652 /* create specific data instance if needed */
5653 if (scnode->nodetype == LYS_LIST) {
5654 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5655 } else if (scnode->nodetype == LYS_LEAFLIST) {
5656 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005657 }
5658
5659 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005660 siblings = NULL;
5661
5662 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5663 assert(!set->val.nodes[i].node);
5664
5665 /* search in all the trees */
5666 siblings = set->tree;
5667 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5668 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005669 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005670 }
5671
5672 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005673 if (inst) {
5674 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005675 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005676 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005677 }
5678
5679 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005680 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005681 ret = LY_EINCOMPLETE;
5682 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005683 }
5684
5685 if (sub) {
5686 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005687 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005688 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005689 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005690 /* no match */
5691 set_remove_node(set, i);
5692 }
5693 }
5694
Michal Vasko004d3152020-06-11 19:59:22 +02005695cleanup:
5696 lyd_free_tree(inst);
5697 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005698}
5699
5700/**
5701 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5702 *
5703 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005704 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005705 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005706 * @param[in] options XPath options.
5707 * @return LY_ERR
5708 */
5709static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005710moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005711{
Radek Krejci857189e2020-09-01 13:26:36 +02005712 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005713 uint32_t getnext_opts;
5714 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005715 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005716 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005717 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005718
aPiecek8b0cc152021-05-31 16:40:31 +02005719 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005720 return LY_SUCCESS;
5721 }
5722
5723 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005724 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005725 return LY_EVALID;
5726 }
5727
Michal Vaskocafad9d2019-11-07 15:20:03 +01005728 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005729 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005730 if (options & LYXP_SCNODE_OUTPUT) {
5731 getnext_opts |= LYS_GETNEXT_OUTPUT;
5732 }
5733
Michal Vasko03ff5a72019-09-11 13:49:33 +02005734 orig_used = set->used;
5735 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005736 uint32_t idx;
5737
Radek Krejcif13b87b2020-12-01 22:02:17 +01005738 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5739 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005740 continue;
5741 }
5742
5743 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005744 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005745 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005746 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005747 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005748
5749 start_parent = set->val.scnodes[i].scnode;
5750
5751 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 +02005752 /* 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 +02005753 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005754 mod_idx = 0;
Michal Vaskoa51ef072021-07-02 10:40:30 +02005755 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005756 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005757 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005758 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005759 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005760 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5761
Michal Vasko03ff5a72019-09-11 13:49:33 +02005762 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005763 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005764 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005765 temp_ctx = 1;
5766 }
5767 }
5768 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005769 }
5770
Michal Vasko519fd602020-05-26 12:17:39 +02005771 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5772 iter = NULL;
5773 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005774 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005775 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5776
5777 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005778 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005779 temp_ctx = 1;
5780 }
5781 }
5782 }
5783 }
5784 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005785
5786 /* correct temporary in_ctx values */
5787 if (temp_ctx) {
5788 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005789 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5790 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005791 }
5792 }
5793 }
5794
5795 return LY_SUCCESS;
5796}
5797
5798/**
Michal Vaskod3678892020-05-21 10:06:58 +02005799 * @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 +02005800 * Context position aware.
5801 *
5802 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005803 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005804 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005805 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005806 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5807 */
5808static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005809moveto_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 +02005810{
5811 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005812 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005813 struct lyxp_set ret_set;
5814 LY_ERR rc;
5815
aPiecek8b0cc152021-05-31 16:40:31 +02005816 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005817 return LY_SUCCESS;
5818 }
5819
5820 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005821 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005822 return LY_EVALID;
5823 }
5824
Michal Vasko9f96a052020-03-10 09:41:45 +01005825 /* 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 +01005826 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005827 LY_CHECK_RET(rc);
5828
Michal Vasko6346ece2019-09-24 13:12:53 +02005829 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005830 set_init(&ret_set, set);
5831 for (i = 0; i < set->used; ++i) {
5832
5833 /* TREE DFS */
5834 start = set->val.nodes[i].node;
5835 for (elem = next = start; elem; elem = next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005836 rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005837 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005838 /* add matching node into result set */
5839 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5840 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5841 /* the node is a duplicate, we'll process it later in the set */
5842 goto skip_children;
5843 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005844 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005845 return rc;
5846 } else if (rc == LY_EINVAL) {
5847 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005848 }
5849
5850 /* TREE DFS NEXT ELEM */
5851 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005852 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005853 if (!next) {
5854skip_children:
5855 /* no children, so try siblings, but only if it's not the start,
5856 * that is considered to be the root and it's siblings are not traversed */
5857 if (elem != start) {
5858 next = elem->next;
5859 } else {
5860 break;
5861 }
5862 }
5863 while (!next) {
5864 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01005865 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005866 /* we are done, no next element to process */
5867 break;
5868 }
5869 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01005870 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005871 next = elem->next;
5872 }
5873 }
5874 }
5875
5876 /* make the temporary set the current one */
5877 ret_set.ctx_pos = set->ctx_pos;
5878 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005879 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005880 memcpy(set, &ret_set, sizeof *set);
5881
5882 return LY_SUCCESS;
5883}
5884
5885/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005886 * @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 +02005887 *
5888 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005889 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005890 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005891 * @param[in] options XPath options.
5892 * @return LY_ERR
5893 */
5894static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005895moveto_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 +02005896{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005897 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005898 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005899 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005900
aPiecek8b0cc152021-05-31 16:40:31 +02005901 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005902 return LY_SUCCESS;
5903 }
5904
5905 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005906 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005907 return LY_EVALID;
5908 }
5909
Michal Vasko03ff5a72019-09-11 13:49:33 +02005910 orig_used = set->used;
5911 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005912 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5913 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005914 continue;
5915 }
5916
5917 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005918 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005919 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005920 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005921 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005922
5923 /* TREE DFS */
5924 start = set->val.scnodes[i].scnode;
5925 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005926 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5927 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005928 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005929 }
5930
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005931 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005932 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005933 uint32_t idx;
5934
5935 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005936 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005937 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005938 /* we will process it later in the set */
5939 goto skip_children;
5940 }
5941 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005942 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005943 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005944 } else if (rc == LY_EINVAL) {
5945 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005946 }
5947
5948next_iter:
5949 /* TREE DFS NEXT ELEM */
5950 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005951 next = lysc_node_child(elem);
5952 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5953 next = next->next;
5954 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5955 next = next->next;
5956 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005957 if (!next) {
5958skip_children:
5959 /* no children, so try siblings, but only if it's not the start,
5960 * that is considered to be the root and it's siblings are not traversed */
5961 if (elem != start) {
5962 next = elem->next;
5963 } else {
5964 break;
5965 }
5966 }
5967 while (!next) {
5968 /* no siblings, go back through the parents */
5969 if (elem->parent == start) {
5970 /* we are done, no next element to process */
5971 break;
5972 }
5973 /* parent is already processed, go to its sibling */
5974 elem = elem->parent;
5975 next = elem->next;
5976 }
5977 }
5978 }
5979
5980 return LY_SUCCESS;
5981}
5982
5983/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005984 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005985 * Indirectly context position aware.
5986 *
5987 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005988 * @param[in] mod Matching metadata module, NULL for any.
5989 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02005990 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005991 * @return LY_ERR
5992 */
5993static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02005994moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005995{
Michal Vasko9f96a052020-03-10 09:41:45 +01005996 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005997
aPiecek8b0cc152021-05-31 16:40:31 +02005998 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005999 return LY_SUCCESS;
6000 }
6001
6002 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006003 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006004 return LY_EVALID;
6005 }
6006
Radek Krejci1deb5be2020-08-26 16:43:36 +02006007 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006008 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006009
6010 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
6011 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01006012 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006013 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006014
6015 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006016 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006017 continue;
6018 }
6019
Michal Vaskod3678892020-05-21 10:06:58 +02006020 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006021 /* match */
6022 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006023 set->val.meta[i].meta = sub;
6024 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006025 /* pos does not change */
6026 replaced = 1;
6027 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006028 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 +02006029 }
6030 ++i;
6031 }
6032 }
6033 }
6034
6035 if (!replaced) {
6036 /* no match */
6037 set_remove_node(set, i);
6038 }
6039 }
6040
6041 return LY_SUCCESS;
6042}
6043
6044/**
6045 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02006046 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006047 *
6048 * @param[in,out] set1 Set to use for the result.
6049 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006050 * @return LY_ERR
6051 */
6052static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006053moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006054{
6055 LY_ERR rc;
6056
Michal Vaskod3678892020-05-21 10:06:58 +02006057 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006058 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006059 return LY_EVALID;
6060 }
6061
6062 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02006063 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006064 return LY_SUCCESS;
6065 }
6066
Michal Vaskod3678892020-05-21 10:06:58 +02006067 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006068 memcpy(set1, set2, sizeof *set1);
6069 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02006070 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006071 return LY_SUCCESS;
6072 }
6073
6074 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006075 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006076
6077 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006078 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006079 LY_CHECK_RET(rc);
6080
6081 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006082 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006083
6084 return LY_SUCCESS;
6085}
6086
6087/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006088 * @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 +02006089 * Context position aware.
6090 *
6091 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006092 * @param[in] mod Matching metadata module, NULL for any.
6093 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006094 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006095 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6096 */
6097static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006098moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006099{
Michal Vasko9f96a052020-03-10 09:41:45 +01006100 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006101 struct lyxp_set *set_all_desc = NULL;
6102 LY_ERR rc;
6103
aPiecek8b0cc152021-05-31 16:40:31 +02006104 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006105 return LY_SUCCESS;
6106 }
6107
6108 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006109 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006110 return LY_EVALID;
6111 }
6112
Michal Vasko03ff5a72019-09-11 13:49:33 +02006113 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6114 * but it likely won't be used much, so it's a waste of time */
6115 /* copy the context */
6116 set_all_desc = set_copy(set);
6117 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006118 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006119 if (rc != LY_SUCCESS) {
6120 lyxp_set_free(set_all_desc);
6121 return rc;
6122 }
6123 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006124 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006125 if (rc != LY_SUCCESS) {
6126 lyxp_set_free(set_all_desc);
6127 return rc;
6128 }
6129 lyxp_set_free(set_all_desc);
6130
Radek Krejci1deb5be2020-08-26 16:43:36 +02006131 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006132 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006133
6134 /* only attributes of an elem can be in the result, skip all the rest,
6135 * we have all attributes qualified in lyd tree */
6136 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006137 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006138 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006139 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006140 continue;
6141 }
6142
Michal Vaskod3678892020-05-21 10:06:58 +02006143 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006144 /* match */
6145 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006146 set->val.meta[i].meta = sub;
6147 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006148 /* pos does not change */
6149 replaced = 1;
6150 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006151 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 +02006152 }
6153 ++i;
6154 }
6155 }
6156 }
6157
6158 if (!replaced) {
6159 /* no match */
6160 set_remove_node(set, i);
6161 }
6162 }
6163
6164 return LY_SUCCESS;
6165}
6166
6167/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006168 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6169 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006170 *
6171 * @param[in] parent Current parent.
6172 * @param[in] parent_pos Position of @p parent.
6173 * @param[in] parent_type Node type of @p parent.
6174 * @param[in,out] to_set Set to use.
6175 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006176 * @param[in] options XPath options.
6177 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6178 */
6179static LY_ERR
6180moveto_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 +02006181 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006182{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006183 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006184 LY_ERR rc;
6185
6186 switch (parent_type) {
6187 case LYXP_NODE_ROOT:
6188 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006189 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006190
Michal Vasko61ac2f62020-05-25 12:39:51 +02006191 /* add all top-level nodes as elements */
6192 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006193 break;
6194 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006195 /* add just the text node of this term element node */
6196 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006197 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6198 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6199 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006200 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006201 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006202
6203 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006204 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006205 break;
6206 default:
6207 LOGINT_RET(parent->schema->module->ctx);
6208 }
6209
Michal Vasko61ac2f62020-05-25 12:39:51 +02006210 /* add all top-level nodes as elements */
6211 LY_LIST_FOR(first, iter) {
6212 /* context check */
6213 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6214 continue;
6215 }
6216
6217 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006218 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006219 return LY_EINCOMPLETE;
6220 }
6221
6222 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6223 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6224
6225 /* also add all the children of this node, recursively */
6226 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6227 LY_CHECK_RET(rc);
6228 }
6229 }
6230
Michal Vasko03ff5a72019-09-11 13:49:33 +02006231 return LY_SUCCESS;
6232}
6233
6234/**
6235 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6236 * (or LYXP_SET_EMPTY). Context position aware.
6237 *
6238 * @param[in,out] set Set to use.
6239 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6240 * @param[in] options XPath options.
6241 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6242 */
6243static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006244moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006245{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006246 struct lyxp_set ret_set;
6247 LY_ERR rc;
6248
aPiecek8b0cc152021-05-31 16:40:31 +02006249 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006250 return LY_SUCCESS;
6251 }
6252
6253 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006254 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006255 return LY_EVALID;
6256 }
6257
6258 /* nothing to do */
6259 if (!all_desc) {
6260 return LY_SUCCESS;
6261 }
6262
Michal Vasko03ff5a72019-09-11 13:49:33 +02006263 /* add all the children, they get added recursively */
6264 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006265 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006266 /* copy the current node to tmp */
6267 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6268
6269 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006270 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006271 continue;
6272 }
6273
Michal Vasko03ff5a72019-09-11 13:49:33 +02006274 /* add all the children */
6275 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 +02006276 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006277 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006278 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006279 return rc;
6280 }
6281 }
6282
6283 /* use the temporary set as the current one */
6284 ret_set.ctx_pos = set->ctx_pos;
6285 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006286 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006287 memcpy(set, &ret_set, sizeof *set);
6288
6289 return LY_SUCCESS;
6290}
6291
6292/**
6293 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6294 * (or LYXP_SET_EMPTY).
6295 *
6296 * @param[in,out] set Set to use.
6297 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6298 * @param[in] options XPath options.
6299 * @return LY_ERR
6300 */
6301static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006302moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006303{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006304 uint32_t getnext_opts;
6305 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006306 const struct lysc_node *iter, *start_parent;
6307 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006308
aPiecek8b0cc152021-05-31 16:40:31 +02006309 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006310 return LY_SUCCESS;
6311 }
6312
6313 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006314 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006315 return LY_EVALID;
6316 }
6317
Michal Vasko519fd602020-05-26 12:17:39 +02006318 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006319 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006320 if (options & LYXP_SCNODE_OUTPUT) {
6321 getnext_opts |= LYS_GETNEXT_OUTPUT;
6322 }
6323
6324 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006325 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006326 if (!all_desc) {
6327 /* traverse the start node */
6328 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6329 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6330 }
6331 continue;
6332 }
6333
Radek Krejcif13b87b2020-12-01 22:02:17 +01006334 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6335 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006336 continue;
6337 }
6338
Michal Vasko519fd602020-05-26 12:17:39 +02006339 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006340 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006341 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006342 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006343 }
6344
Michal Vasko519fd602020-05-26 12:17:39 +02006345 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006346
Michal Vasko519fd602020-05-26 12:17:39 +02006347 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6348 /* it can actually be in any module, it's all <running> */
6349 mod_idx = 0;
Michal Vaskoa51ef072021-07-02 10:40:30 +02006350 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02006351 iter = NULL;
6352 /* module may not be implemented */
6353 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6354 /* context check */
6355 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6356 continue;
6357 }
6358
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006359 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006360 /* throw away the insert index, we want to consider that node again, recursively */
6361 }
6362 }
6363
6364 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6365 iter = NULL;
6366 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006367 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006368 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006369 continue;
6370 }
6371
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006372 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006373 }
6374 }
6375 }
6376
6377 return LY_SUCCESS;
6378}
6379
6380/**
6381 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6382 * (or LYXP_SET_EMPTY). Context position aware.
6383 *
6384 * @param[in] set Set to use.
6385 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6386 * @param[in] options XPath options.
6387 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6388 */
6389static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006390moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006391{
6392 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006393 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006394 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006395
aPiecek8b0cc152021-05-31 16:40:31 +02006396 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006397 return LY_SUCCESS;
6398 }
6399
6400 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006401 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006402 return LY_EVALID;
6403 }
6404
6405 if (all_desc) {
6406 /* <path>//.. == <path>//./.. */
6407 rc = moveto_self(set, 1, options);
6408 LY_CHECK_RET(rc);
6409 }
6410
Radek Krejci1deb5be2020-08-26 16:43:36 +02006411 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006412 node = set->val.nodes[i].node;
6413
6414 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9e685082021-01-29 14:49:09 +01006415 new_node = lyd_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006416 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6417 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006418 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6419 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006420 if (!new_node) {
6421 LOGINT_RET(set->ctx);
6422 }
6423 } else {
6424 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006425 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006426 continue;
6427 }
6428
Michal Vaskoa1424542019-11-14 16:08:52 +01006429 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006430 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 +02006431 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006432 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006433
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006434 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006435 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006436 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006437
Michal Vasko03ff5a72019-09-11 13:49:33 +02006438 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006439 /* 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 +02006440 new_type = LYXP_NODE_ELEM;
6441 }
6442
Michal Vasko03ff5a72019-09-11 13:49:33 +02006443 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006444 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006445 } else {
6446 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006447 }
6448 }
6449
Michal Vasko2caefc12019-11-14 16:07:56 +01006450 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006451 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006452
6453 return LY_SUCCESS;
6454}
6455
6456/**
6457 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6458 * (or LYXP_SET_EMPTY).
6459 *
6460 * @param[in] set Set to use.
6461 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6462 * @param[in] options XPath options.
6463 * @return LY_ERR
6464 */
6465static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006466moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006467{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006468 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006469 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006470 const struct lysc_node *node, *new_node;
6471 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006472
aPiecek8b0cc152021-05-31 16:40:31 +02006473 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006474 return LY_SUCCESS;
6475 }
6476
6477 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006478 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006479 return LY_EVALID;
6480 }
6481
6482 if (all_desc) {
6483 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006484 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006485 }
6486
Michal Vasko03ff5a72019-09-11 13:49:33 +02006487 orig_used = set->used;
6488 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006489 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6490 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006491 continue;
6492 }
6493
6494 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006495 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006496 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006497 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006498 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006499
6500 node = set->val.scnodes[i].scnode;
6501
6502 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006503 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006504 } else {
6505 /* root does not have a parent */
6506 continue;
6507 }
6508
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006509 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006510 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006511 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006512
Michal Vasko03ff5a72019-09-11 13:49:33 +02006513 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006514 /* 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 +02006515 new_type = LYXP_NODE_ELEM;
6516 }
6517
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006518 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6519 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006520 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006521 temp_ctx = 1;
6522 }
6523 }
6524
6525 if (temp_ctx) {
6526 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006527 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6528 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006529 }
6530 }
6531 }
6532
6533 return LY_SUCCESS;
6534}
6535
6536/**
6537 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6538 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6539 *
6540 * @param[in,out] set1 Set to use for the result.
6541 * @param[in] set2 Set acting as the second operand for @p op.
6542 * @param[in] op Comparison operator to process.
6543 * @param[in] options XPath options.
6544 * @return LY_ERR
6545 */
6546static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006547moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006548{
6549 /*
6550 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6551 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6552 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6553 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6554 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6555 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6556 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6557 *
6558 * '=' or '!='
6559 * BOOLEAN + BOOLEAN
6560 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6561 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6562 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6563 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6564 * NUMBER + NUMBER
6565 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6566 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6567 * STRING + STRING
6568 *
6569 * '<=', '<', '>=', '>'
6570 * NUMBER + NUMBER
6571 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6572 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6573 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6574 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6575 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6576 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6577 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6578 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6579 */
6580 struct lyxp_set iter1, iter2;
6581 int result;
6582 int64_t i;
6583 LY_ERR rc;
6584
Michal Vasko004d3152020-06-11 19:59:22 +02006585 memset(&iter1, 0, sizeof iter1);
6586 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006587
6588 /* iterative evaluation with node-sets */
6589 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6590 if (set1->type == LYXP_SET_NODE_SET) {
6591 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006592 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006593 switch (set2->type) {
6594 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006595 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006596 break;
6597 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006598 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006599 break;
6600 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006601 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006602 break;
6603 }
6604 LY_CHECK_RET(rc);
6605
Michal Vasko4c7763f2020-07-27 17:40:37 +02006606 /* canonize set2 */
6607 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6608
6609 /* compare recursively */
6610 rc = moveto_op_comp(&iter1, &iter2, op, options);
6611 lyxp_set_free_content(&iter2);
6612 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006613
6614 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006615 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006616 set_fill_boolean(set1, 1);
6617 return LY_SUCCESS;
6618 }
6619 }
6620 } else {
6621 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006622 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006623 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006624 case LYXP_SET_NUMBER:
6625 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6626 break;
6627 case LYXP_SET_BOOLEAN:
6628 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6629 break;
6630 default:
6631 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6632 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006633 }
6634 LY_CHECK_RET(rc);
6635
Michal Vasko4c7763f2020-07-27 17:40:37 +02006636 /* canonize set1 */
6637 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 +02006638
Michal Vasko4c7763f2020-07-27 17:40:37 +02006639 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006640 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006641 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006642 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006643
6644 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006645 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006646 set_fill_boolean(set1, 1);
6647 return LY_SUCCESS;
6648 }
6649 }
6650 }
6651
6652 /* false for all nodes */
6653 set_fill_boolean(set1, 0);
6654 return LY_SUCCESS;
6655 }
6656
6657 /* first convert properly */
6658 if ((op[0] == '=') || (op[0] == '!')) {
6659 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006660 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6661 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006662 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006663 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006664 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006665 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006666 LY_CHECK_RET(rc);
6667 } /* else we have 2 strings */
6668 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006669 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006670 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006671 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006672 LY_CHECK_RET(rc);
6673 }
6674
6675 assert(set1->type == set2->type);
6676
6677 /* compute result */
6678 if (op[0] == '=') {
6679 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006680 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006681 } else if (set1->type == LYXP_SET_NUMBER) {
6682 result = (set1->val.num == set2->val.num);
6683 } else {
6684 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006685 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006686 }
6687 } else if (op[0] == '!') {
6688 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006689 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006690 } else if (set1->type == LYXP_SET_NUMBER) {
6691 result = (set1->val.num != set2->val.num);
6692 } else {
6693 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006694 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006695 }
6696 } else {
6697 assert(set1->type == LYXP_SET_NUMBER);
6698 if (op[0] == '<') {
6699 if (op[1] == '=') {
6700 result = (set1->val.num <= set2->val.num);
6701 } else {
6702 result = (set1->val.num < set2->val.num);
6703 }
6704 } else {
6705 if (op[1] == '=') {
6706 result = (set1->val.num >= set2->val.num);
6707 } else {
6708 result = (set1->val.num > set2->val.num);
6709 }
6710 }
6711 }
6712
6713 /* assign result */
6714 if (result) {
6715 set_fill_boolean(set1, 1);
6716 } else {
6717 set_fill_boolean(set1, 0);
6718 }
6719
6720 return LY_SUCCESS;
6721}
6722
6723/**
6724 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6725 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6726 *
6727 * @param[in,out] set1 Set to use for the result.
6728 * @param[in] set2 Set acting as the second operand for @p op.
6729 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006730 * @return LY_ERR
6731 */
6732static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006733moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006734{
6735 LY_ERR rc;
6736
6737 /* unary '-' */
6738 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006739 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006740 LY_CHECK_RET(rc);
6741 set1->val.num *= -1;
6742 lyxp_set_free(set2);
6743 return LY_SUCCESS;
6744 }
6745
6746 assert(set1 && set2);
6747
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006748 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006749 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006750 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006751 LY_CHECK_RET(rc);
6752
6753 switch (op[0]) {
6754 /* '+' */
6755 case '+':
6756 set1->val.num += set2->val.num;
6757 break;
6758
6759 /* '-' */
6760 case '-':
6761 set1->val.num -= set2->val.num;
6762 break;
6763
6764 /* '*' */
6765 case '*':
6766 set1->val.num *= set2->val.num;
6767 break;
6768
6769 /* 'div' */
6770 case 'd':
6771 set1->val.num /= set2->val.num;
6772 break;
6773
6774 /* 'mod' */
6775 case 'm':
6776 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6777 break;
6778
6779 default:
6780 LOGINT_RET(set1->ctx);
6781 }
6782
6783 return LY_SUCCESS;
6784}
6785
6786/*
6787 * eval functions
6788 *
6789 * They execute a parsed XPath expression on some data subtree.
6790 */
6791
6792/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006793 * @brief Evaluate Predicate. Logs directly on error.
6794 *
Michal Vaskod3678892020-05-21 10:06:58 +02006795 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006796 *
6797 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006798 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02006799 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006800 * @param[in] options XPath options.
6801 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6802 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6803 */
6804static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006805eval_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 +02006806{
6807 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006808 uint16_t orig_exp;
6809 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006810 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006811 struct lyxp_set set2;
6812 struct lyd_node *orig_parent;
6813
6814 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02006815 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006816 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006817 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006818
aPiecek8b0cc152021-05-31 16:40:31 +02006819 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006820only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02006821 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006822 LY_CHECK_RET(rc);
6823 } else if (set->type == LYXP_SET_NODE_SET) {
6824 /* 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 +01006825 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006826
6827 /* empty set, nothing to evaluate */
6828 if (!set->used) {
6829 goto only_parse;
6830 }
6831
Michal Vasko004d3152020-06-11 19:59:22 +02006832 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006833 orig_pos = 0;
6834 orig_size = set->used;
6835 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006836 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006837 set_init(&set2, set);
6838 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6839 /* remember the node context position for position() and context size for last(),
6840 * predicates should always be evaluated with respect to the child axis (since we do
6841 * not support explicit axes) so we assign positions based on their parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006842 if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) {
6843 orig_parent = lyd_parent(set->val.nodes[i].node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006844 orig_pos = 1;
6845 } else {
6846 ++orig_pos;
6847 }
6848
6849 set2.ctx_pos = orig_pos;
6850 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006851 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006852
Michal Vasko004d3152020-06-11 19:59:22 +02006853 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006854 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006855 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006856 return rc;
6857 }
6858
6859 /* number is a position */
6860 if (set2.type == LYXP_SET_NUMBER) {
6861 if ((long long)set2.val.num == orig_pos) {
6862 set2.val.num = 1;
6863 } else {
6864 set2.val.num = 0;
6865 }
6866 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006867 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006868
6869 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006870 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006871 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006872 }
6873 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006874 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006875
6876 } else if (set->type == LYXP_SET_SCNODE_SET) {
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 Vasko03ff5a72019-09-11 13:49:33 +02006879 /* there is a currently-valid node */
6880 break;
6881 }
6882 }
6883 /* empty set, nothing to evaluate */
6884 if (i == set->used) {
6885 goto only_parse;
6886 }
6887
Michal Vasko004d3152020-06-11 19:59:22 +02006888 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006889
Michal Vasko03ff5a72019-09-11 13:49:33 +02006890 /* set special in_ctx to all the valid snodes */
6891 pred_in_ctx = set_scnode_new_in_ctx(set);
6892
6893 /* use the valid snodes one-by-one */
6894 for (i = 0; i < set->used; ++i) {
6895 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6896 continue;
6897 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006898 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006899
Michal Vasko004d3152020-06-11 19:59:22 +02006900 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006901
Michal Vasko004d3152020-06-11 19:59:22 +02006902 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006903 LY_CHECK_RET(rc);
6904
6905 set->val.scnodes[i].in_ctx = pred_in_ctx;
6906 }
6907
6908 /* restore the state as it was before the predicate */
6909 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006910 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02006911 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006912 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006913 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006914 }
6915 }
6916
6917 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006918 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006919 set_fill_set(&set2, set);
6920
Michal Vasko004d3152020-06-11 19:59:22 +02006921 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006922 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006923 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006924 return rc;
6925 }
6926
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006927 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006928 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006929 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006930 }
Michal Vaskod3678892020-05-21 10:06:58 +02006931 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006932 }
6933
6934 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006935 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02006936 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006937 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006938 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006939
6940 return LY_SUCCESS;
6941}
6942
6943/**
Michal Vaskod3678892020-05-21 10:06:58 +02006944 * @brief Evaluate Literal. Logs directly on error.
6945 *
6946 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006947 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006948 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6949 */
6950static void
Michal Vasko40308e72020-10-20 16:38:40 +02006951eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006952{
6953 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006954 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006955 set_fill_string(set, "", 0);
6956 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006957 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 +02006958 }
6959 }
6960 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006961 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006962 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006963}
6964
6965/**
Michal Vasko004d3152020-06-11 19:59:22 +02006966 * @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 +02006967 *
Michal Vasko004d3152020-06-11 19:59:22 +02006968 * @param[in] exp Full parsed XPath expression.
6969 * @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 +02006970 * @param[in] ctx_node Found schema node as the context for the predicate.
6971 * @param[in] cur_mod Current module for the expression.
6972 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006973 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006974 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006975 * @param[out] predicates Parsed predicates.
6976 * @param[out] pred_type Type of @p predicates.
6977 * @return LY_SUCCESS on success,
6978 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006979 */
6980static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006981eval_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 +02006982 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 +02006983 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006984{
Michal Vasko004d3152020-06-11 19:59:22 +02006985 LY_ERR ret = LY_SUCCESS;
6986 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006987 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006988 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006989 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006990 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006991
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006992 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006993
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006994 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006995 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006996 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006997 return LY_EINVAL;
6998 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006999 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 +02007000 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02007001
Michal Vasko004d3152020-06-11 19:59:22 +02007002 /* learn where the predicates end */
7003 e_idx = *tok_idx;
7004 while (key_count) {
7005 /* '[' */
7006 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7007 return LY_EINVAL;
7008 }
7009 ++e_idx;
7010
Michal Vasko3354d272021-04-06 09:40:06 +02007011 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
7012 /* definitely not a key */
7013 return LY_EINVAL;
7014 }
7015
Michal Vasko004d3152020-06-11 19:59:22 +02007016 /* ']' */
7017 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7018 ++e_idx;
7019 }
7020 ++e_idx;
7021
7022 /* another presumably key predicate parsed */
7023 --key_count;
7024 }
Michal Vasko004d3152020-06-11 19:59:22 +02007025 } else {
7026 /* learn just where this single predicate ends */
7027 e_idx = *tok_idx;
7028
Michal Vaskod3678892020-05-21 10:06:58 +02007029 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007030 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7031 return LY_EINVAL;
7032 }
7033 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007034
Michal Vasko3354d272021-04-06 09:40:06 +02007035 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
7036 /* definitely not the value */
7037 return LY_EINVAL;
7038 }
7039
Michal Vaskod3678892020-05-21 10:06:58 +02007040 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007041 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7042 ++e_idx;
7043 }
7044 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007045 }
7046
Michal Vasko004d3152020-06-11 19:59:22 +02007047 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
7048 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
7049
7050 /* turn logging off */
7051 prev_lo = ly_log_options(0);
7052
7053 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007054 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
7055 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007056
7057 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007058 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
7059 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02007060 LY_CHECK_GOTO(ret, cleanup);
7061
7062 /* success, the predicate must include all the needed information for hash-based search */
7063 *tok_idx = e_idx;
7064
7065cleanup:
7066 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007067 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02007068 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02007069}
7070
7071/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007072 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7073 * data node(s) could be found using a single hash-based search.
7074 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007075 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007076 * @param[in] node Next context node to check.
7077 * @param[in] name Expected node name.
7078 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007079 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007080 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007081 * @param[in] format Prefix format.
7082 * @param[in,out] found Previously found node, is updated.
7083 * @return LY_SUCCESS on success,
7084 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7085 */
7086static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007087eval_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 +02007088 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 +02007089 const struct lysc_node **found)
7090{
7091 const struct lysc_node *scnode;
7092 const struct lys_module *mod;
7093 uint32_t idx = 0;
7094
Radek Krejci8df109d2021-04-23 12:19:08 +02007095 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007096
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007097continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007098 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007099 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007100 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007101 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007102 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007103 if (!mod->implemented) {
7104 continue;
7105 }
7106
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007107 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7108 if (scnode) {
7109 /* we have found a match */
7110 break;
7111 }
7112 }
7113
Michal Vasko7d1d0912020-10-16 08:38:30 +02007114 if (!scnode) {
7115 /* all modules searched */
7116 idx = 0;
7117 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007118 } else {
7119 /* search in top-level */
7120 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7121 }
7122 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007123 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007124 /* we must adjust the module to inherit the one from the context node */
7125 moveto_mod = node->schema->module;
7126 }
7127
7128 /* search in children, do not repeat the same search */
7129 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007130 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007131
7132 /* additional context check */
7133 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7134 scnode = NULL;
7135 }
7136
7137 if (scnode) {
7138 if (*found) {
7139 /* we found a schema node with the same name but at different level, give up, too complicated
7140 * (more hash-based searches would be required, not supported) */
7141 return LY_ENOT;
7142 } else {
7143 /* remember the found schema node and continue to make sure it can be used */
7144 *found = scnode;
7145 }
7146 }
7147
7148 if (idx) {
7149 /* continue searching all the following models */
7150 goto continue_search;
7151 }
7152
7153 return LY_SUCCESS;
7154}
7155
7156/**
Michal Vaskod3678892020-05-21 10:06:58 +02007157 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7158 *
7159 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7160 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7161 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7162 *
7163 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007164 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007165 * @param[in] attr_axis Whether to search attributes or standard nodes.
7166 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007167 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007168 * @param[in] options XPath options.
7169 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7170 */
7171static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007172eval_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 +02007173 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007174{
Michal Vaskod3678892020-05-21 10:06:58 +02007175 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007176 const char *ncname, *ncname_dict = NULL;
7177 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007178 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007179 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007180 struct ly_path_predicate *predicates = NULL;
7181 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007182 LY_ERR rc = LY_SUCCESS;
7183
aPiecek8b0cc152021-05-31 16:40:31 +02007184 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007185 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007186 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007187
aPiecek8b0cc152021-05-31 16:40:31 +02007188 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02007189 goto moveto;
7190 }
7191
Michal Vasko004d3152020-06-11 19:59:22 +02007192 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7193 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007194
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007195 if ((ncname[0] == '*') && (ncname_len == 1)) {
7196 /* all nodes will match */
7197 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007198 }
7199
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007200 /* parse (and skip) module name */
7201 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007202 LY_CHECK_GOTO(rc, cleanup);
7203
Radek Krejci8df109d2021-04-23 12:19:08 +02007204 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 +02007205 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007206 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007207 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7208 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007209 /* check failed */
7210 scnode = NULL;
7211 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007212 }
7213 }
7214
Michal Vasko004d3152020-06-11 19:59:22 +02007215 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7216 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007217 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7218 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007219 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007220 scnode = NULL;
7221 }
7222 }
7223 }
7224
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007225 if (!scnode) {
7226 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007227 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007228 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007229 }
7230
7231moveto:
7232 /* move to the attribute(s), data node(s), or schema node(s) */
7233 if (attr_axis) {
aPiecek8b0cc152021-05-31 16:40:31 +02007234 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007235 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007236 } else {
7237 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007238 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007239 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007240 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007241 }
7242 LY_CHECK_GOTO(rc, cleanup);
7243 }
7244 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007245 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007246 int64_t i;
7247
Michal Vaskod3678892020-05-21 10:06:58 +02007248 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007249 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007250 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007251 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007252 }
7253 LY_CHECK_GOTO(rc, cleanup);
7254
7255 for (i = set->used - 1; i > -1; --i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007256 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vaskod3678892020-05-21 10:06:58 +02007257 break;
7258 }
7259 }
7260 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007261 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vasko90f12dc2020-12-03 14:20:42 +01007262 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".",
7263 ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007264 free(path);
7265 }
7266 } else {
7267 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007268 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007269 } else {
7270 if (scnode) {
7271 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007272 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007273 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007274 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007275 }
7276 }
7277 LY_CHECK_GOTO(rc, cleanup);
7278 }
7279 }
7280
7281 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007282 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7283 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007284 LY_CHECK_RET(rc);
7285 }
7286
7287cleanup:
aPiecek8b0cc152021-05-31 16:40:31 +02007288 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007289 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007290 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007291 }
Michal Vaskod3678892020-05-21 10:06:58 +02007292 return rc;
7293}
7294
7295/**
7296 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7297 *
7298 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7299 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7300 * [8] NodeType ::= 'text' | 'node'
7301 *
7302 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007303 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007304 * @param[in] attr_axis Whether to search attributes or standard nodes.
7305 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007306 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007307 * @param[in] options XPath options.
7308 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7309 */
7310static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007311eval_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 +02007312 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007313{
7314 LY_ERR rc;
7315
7316 /* TODO */
7317 (void)attr_axis;
7318 (void)all_desc;
7319
aPiecek8b0cc152021-05-31 16:40:31 +02007320 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007321 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007322 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007323 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
aPiecek8b0cc152021-05-31 16:40:31 +02007324 options |= LYXP_SKIP_EXPR;
Michal Vaskod3678892020-05-21 10:06:58 +02007325 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007326 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007327 rc = xpath_node(NULL, 0, set, options);
7328 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007329 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007330 rc = xpath_text(NULL, 0, set, options);
7331 }
7332 LY_CHECK_RET(rc);
7333 }
7334 }
aPiecek8b0cc152021-05-31 16:40:31 +02007335 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007336 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007337 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007338
7339 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007340 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007341 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007342 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007343 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007344
7345 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007346 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007347 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007348 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007349 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007350
7351 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007352 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7353 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007354 LY_CHECK_RET(rc);
7355 }
7356
7357 return LY_SUCCESS;
7358}
7359
7360/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007361 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7362 *
7363 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7364 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007365 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007366 *
7367 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007368 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007369 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007370 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007371 * @param[in] options XPath options.
7372 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7373 */
7374static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007375eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7376 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007377{
Radek Krejci857189e2020-09-01 13:26:36 +02007378 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007379 LY_ERR rc;
7380
7381 goto step;
7382 do {
7383 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007384 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007385 all_desc = 0;
7386 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007387 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007388 all_desc = 1;
7389 }
aPiecek8b0cc152021-05-31 16:40:31 +02007390 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007391 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007392 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007393
7394step:
Michal Vaskod3678892020-05-21 10:06:58 +02007395 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007396 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007397 attr_axis = 1;
7398
aPiecek8b0cc152021-05-31 16:40:31 +02007399 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007400 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007401 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007402 } else {
7403 attr_axis = 0;
7404 }
7405
Michal Vasko03ff5a72019-09-11 13:49:33 +02007406 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007407 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007408 case LYXP_TOKEN_DOT:
7409 /* evaluate '.' */
aPiecek8b0cc152021-05-31 16:40:31 +02007410 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007411 rc = moveto_scnode_self(set, all_desc, options);
7412 } else {
7413 rc = moveto_self(set, all_desc, options);
7414 }
7415 LY_CHECK_RET(rc);
7416 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007417 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007418 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007419 break;
7420
7421 case LYXP_TOKEN_DDOT:
7422 /* evaluate '..' */
aPiecek8b0cc152021-05-31 16:40:31 +02007423 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007424 rc = moveto_scnode_parent(set, all_desc, options);
7425 } else {
7426 rc = moveto_parent(set, all_desc, options);
7427 }
7428 LY_CHECK_RET(rc);
aPiecek8b0cc152021-05-31 16:40:31 +02007429 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007430 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007431 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007432 break;
7433
Michal Vasko03ff5a72019-09-11 13:49:33 +02007434 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007435 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007436 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007437 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007438 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007439
Michal Vaskod3678892020-05-21 10:06:58 +02007440 case LYXP_TOKEN_NODETYPE:
7441 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007442 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007443 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007444 break;
7445
7446 default:
aPiecek8b0cc152021-05-31 16:40:31 +02007447 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007448 }
Michal Vasko004d3152020-06-11 19:59:22 +02007449 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007450
7451 return LY_SUCCESS;
7452}
7453
7454/**
7455 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7456 *
7457 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7458 *
7459 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007460 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007461 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007462 * @param[in] options XPath options.
7463 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7464 */
7465static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007466eval_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 +02007467{
Radek Krejci857189e2020-09-01 13:26:36 +02007468 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007469
aPiecek8b0cc152021-05-31 16:40:31 +02007470 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007471 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007472 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007473 }
7474
7475 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007476 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007477 /* evaluate '/' - deferred */
7478 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02007479 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007480 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007481 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007482
Michal Vasko004d3152020-06-11 19:59:22 +02007483 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007484 return LY_SUCCESS;
7485 }
Michal Vasko004d3152020-06-11 19:59:22 +02007486 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007487 case LYXP_TOKEN_DOT:
7488 case LYXP_TOKEN_DDOT:
7489 case LYXP_TOKEN_AT:
7490 case LYXP_TOKEN_NAMETEST:
7491 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007492 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007493 break;
7494 default:
7495 break;
7496 }
7497
Michal Vasko03ff5a72019-09-11 13:49:33 +02007498 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007499 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007500 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7501 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02007502 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007503 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007504 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007505
Michal Vaskob0099a92020-08-31 14:55:23 +02007506 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007507 }
7508
7509 return LY_SUCCESS;
7510}
7511
7512/**
7513 * @brief Evaluate FunctionCall. Logs directly on error.
7514 *
Michal Vaskod3678892020-05-21 10:06:58 +02007515 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007516 *
7517 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007518 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007519 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007520 * @param[in] options XPath options.
7521 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7522 */
7523static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007524eval_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 +02007525{
7526 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007527
Radek Krejci1deb5be2020-08-26 16:43:36 +02007528 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007529 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007530 struct lyxp_set **args = NULL, **args_aux;
7531
aPiecek8b0cc152021-05-31 16:40:31 +02007532 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007533 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007534 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007535 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007536 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007537 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007538 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007539 xpath_func = &xpath_sum;
7540 }
7541 break;
7542 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007543 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007544 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007545 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007546 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007547 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007548 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007549 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007550 xpath_func = &xpath_true;
7551 }
7552 break;
7553 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007554 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007555 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007556 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007557 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007558 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007559 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007560 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007561 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007562 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007563 xpath_func = &xpath_deref;
7564 }
7565 break;
7566 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007567 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007568 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007569 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007571 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007572 xpath_func = &xpath_string;
7573 }
7574 break;
7575 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007576 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007577 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007578 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007579 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007580 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007581 xpath_func = &xpath_current;
7582 }
7583 break;
7584 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007585 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007586 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007587 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007588 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007589 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007590 xpath_func = &xpath_re_match;
7591 }
7592 break;
7593 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007594 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007595 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007596 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007597 xpath_func = &xpath_translate;
7598 }
7599 break;
7600 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007601 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007602 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007603 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007604 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007605 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007606 xpath_func = &xpath_bit_is_set;
7607 }
7608 break;
7609 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007610 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007611 xpath_func = &xpath_starts_with;
7612 }
7613 break;
7614 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007615 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007616 xpath_func = &xpath_derived_from;
7617 }
7618 break;
7619 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007620 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007621 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007622 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007623 xpath_func = &xpath_string_length;
7624 }
7625 break;
7626 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007627 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007628 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007629 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007630 xpath_func = &xpath_substring_after;
7631 }
7632 break;
7633 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007634 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007635 xpath_func = &xpath_substring_before;
7636 }
7637 break;
7638 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007639 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007640 xpath_func = &xpath_derived_from_or_self;
7641 }
7642 break;
7643 }
7644
7645 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007646 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 +02007647 return LY_EVALID;
7648 }
7649 }
7650
aPiecek8b0cc152021-05-31 16:40:31 +02007651 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007652 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007653 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007654
7655 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007656 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007657 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007658 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007659 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007660
7661 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007662 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02007663 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007664 args = malloc(sizeof *args);
7665 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7666 arg_count = 1;
7667 args[0] = set_copy(set);
7668 if (!args[0]) {
7669 rc = LY_EMEM;
7670 goto cleanup;
7671 }
7672
Michal Vasko004d3152020-06-11 19:59:22 +02007673 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007674 LY_CHECK_GOTO(rc, cleanup);
7675 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007676 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007677 LY_CHECK_GOTO(rc, cleanup);
7678 }
7679 }
Michal Vasko004d3152020-06-11 19:59:22 +02007680 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02007681 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007682 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007683 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007684
aPiecek8b0cc152021-05-31 16:40:31 +02007685 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007686 ++arg_count;
7687 args_aux = realloc(args, arg_count * sizeof *args);
7688 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7689 args = args_aux;
7690 args[arg_count - 1] = set_copy(set);
7691 if (!args[arg_count - 1]) {
7692 rc = LY_EMEM;
7693 goto cleanup;
7694 }
7695
Michal Vasko004d3152020-06-11 19:59:22 +02007696 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007697 LY_CHECK_GOTO(rc, cleanup);
7698 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007699 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007700 LY_CHECK_GOTO(rc, cleanup);
7701 }
7702 }
7703
7704 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007705 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007706 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007707 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007708 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007709
aPiecek8b0cc152021-05-31 16:40:31 +02007710 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007711 /* evaluate function */
7712 rc = xpath_func(args, arg_count, set, options);
7713
7714 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007715 /* merge all nodes from arg evaluations */
7716 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007717 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007718 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007719 }
7720 }
7721 } else {
7722 rc = LY_SUCCESS;
7723 }
7724
7725cleanup:
7726 for (i = 0; i < arg_count; ++i) {
7727 lyxp_set_free(args[i]);
7728 }
7729 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007730 return rc;
7731}
7732
7733/**
7734 * @brief Evaluate Number. Logs directly on error.
7735 *
7736 * @param[in] ctx Context for errors.
7737 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007738 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007739 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7740 * @return LY_ERR
7741 */
7742static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007743eval_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 +02007744{
7745 long double num;
7746 char *endptr;
7747
7748 if (set) {
7749 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007750 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007751 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007752 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7753 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007754 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007755 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007756 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007757 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7758 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007759 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007760 return LY_EVALID;
7761 }
7762
7763 set_fill_number(set, num);
7764 }
7765
7766 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007767 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007768 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007769 return LY_SUCCESS;
7770}
7771
7772/**
7773 * @brief Evaluate PathExpr. Logs directly on error.
7774 *
Michal Vaskod3678892020-05-21 10:06:58 +02007775 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007776 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7777 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7778 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007779 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007780 *
7781 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007782 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007783 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007784 * @param[in] options XPath options.
7785 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7786 */
7787static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007788eval_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 +02007789{
Radek Krejci857189e2020-09-01 13:26:36 +02007790 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007791 LY_ERR rc;
7792
Michal Vasko004d3152020-06-11 19:59:22 +02007793 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007794 case LYXP_TOKEN_PAR1:
7795 /* '(' Expr ')' */
7796
7797 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02007798 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007799 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007800 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007801
7802 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007803 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007804 LY_CHECK_RET(rc);
7805
7806 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007807 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007808 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007809 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007810 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007811
7812 parent_pos_pred = 0;
7813 goto predicate;
7814
7815 case LYXP_TOKEN_DOT:
7816 case LYXP_TOKEN_DDOT:
7817 case LYXP_TOKEN_AT:
7818 case LYXP_TOKEN_NAMETEST:
7819 case LYXP_TOKEN_NODETYPE:
7820 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007821 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007822 LY_CHECK_RET(rc);
7823 break;
7824
7825 case LYXP_TOKEN_FUNCNAME:
7826 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02007827 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007828 LY_CHECK_RET(rc);
7829
7830 parent_pos_pred = 1;
7831 goto predicate;
7832
Michal Vasko3e48bf32020-06-01 08:39:07 +02007833 case LYXP_TOKEN_OPER_PATH:
7834 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007835 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007836 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007837 LY_CHECK_RET(rc);
7838 break;
7839
7840 case LYXP_TOKEN_LITERAL:
7841 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02007842 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7843 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007844 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007845 }
Michal Vasko004d3152020-06-11 19:59:22 +02007846 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007847 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007848 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007849 }
7850
7851 parent_pos_pred = 1;
7852 goto predicate;
7853
7854 case LYXP_TOKEN_NUMBER:
7855 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02007856 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7857 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007858 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007859 }
Michal Vasko004d3152020-06-11 19:59:22 +02007860 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007861 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007862 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007863 }
7864 LY_CHECK_RET(rc);
7865
7866 parent_pos_pred = 1;
7867 goto predicate;
7868
7869 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01007870 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 +02007871 return LY_EVALID;
7872 }
7873
7874 return LY_SUCCESS;
7875
7876predicate:
7877 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007878 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7879 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007880 LY_CHECK_RET(rc);
7881 }
7882
7883 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007884 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007885
7886 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007887 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007888 all_desc = 0;
7889 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007890 all_desc = 1;
7891 }
7892
aPiecek8b0cc152021-05-31 16:40:31 +02007893 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007894 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007895 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007896
Michal Vasko004d3152020-06-11 19:59:22 +02007897 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007898 LY_CHECK_RET(rc);
7899 }
7900
7901 return LY_SUCCESS;
7902}
7903
7904/**
7905 * @brief Evaluate UnionExpr. Logs directly on error.
7906 *
Michal Vaskod3678892020-05-21 10:06:58 +02007907 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007908 *
7909 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007910 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007911 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007912 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007913 * @param[in] options XPath options.
7914 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7915 */
7916static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007917eval_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 +02007918{
7919 LY_ERR rc = LY_SUCCESS;
7920 struct lyxp_set orig_set, set2;
7921 uint16_t i;
7922
7923 assert(repeat);
7924
7925 set_init(&orig_set, set);
7926 set_init(&set2, set);
7927
7928 set_fill_set(&orig_set, set);
7929
Michal Vasko004d3152020-06-11 19:59:22 +02007930 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007931 LY_CHECK_GOTO(rc, cleanup);
7932
7933 /* ('|' PathExpr)* */
7934 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007935 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02007936 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007937 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007938 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007939
aPiecek8b0cc152021-05-31 16:40:31 +02007940 if (options & LYXP_SKIP_EXPR) {
7941 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007942 LY_CHECK_GOTO(rc, cleanup);
7943 continue;
7944 }
7945
7946 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007947 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007948 LY_CHECK_GOTO(rc, cleanup);
7949
7950 /* eval */
7951 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007952 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007953 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007954 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007955 LY_CHECK_GOTO(rc, cleanup);
7956 }
7957 }
7958
7959cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007960 lyxp_set_free_content(&orig_set);
7961 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007962 return rc;
7963}
7964
7965/**
7966 * @brief Evaluate UnaryExpr. Logs directly on error.
7967 *
Michal Vaskod3678892020-05-21 10:06:58 +02007968 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007969 *
7970 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007971 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007972 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007973 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007974 * @param[in] options XPath options.
7975 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7976 */
7977static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007978eval_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 +02007979{
7980 LY_ERR rc;
7981 uint16_t this_op, i;
7982
7983 assert(repeat);
7984
7985 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007986 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007987 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007988 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 +02007989
aPiecek8b0cc152021-05-31 16:40:31 +02007990 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007991 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007992 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007993 }
7994
Michal Vasko004d3152020-06-11 19:59:22 +02007995 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007996 LY_CHECK_RET(rc);
7997
aPiecek8b0cc152021-05-31 16:40:31 +02007998 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007999 if (options & LYXP_SCNODE_ALL) {
8000 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
8001 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008002 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008003 LY_CHECK_RET(rc);
8004 }
8005 }
8006
8007 return LY_SUCCESS;
8008}
8009
8010/**
8011 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
8012 *
Michal Vaskod3678892020-05-21 10:06:58 +02008013 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008014 * | MultiplicativeExpr '*' UnaryExpr
8015 * | MultiplicativeExpr 'div' UnaryExpr
8016 * | MultiplicativeExpr 'mod' UnaryExpr
8017 *
8018 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008019 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008020 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008021 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008022 * @param[in] options XPath options.
8023 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8024 */
8025static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008026eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
8027 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008028{
8029 LY_ERR rc;
8030 uint16_t this_op;
8031 struct lyxp_set orig_set, set2;
8032 uint16_t i;
8033
8034 assert(repeat);
8035
8036 set_init(&orig_set, set);
8037 set_init(&set2, set);
8038
8039 set_fill_set(&orig_set, set);
8040
Michal Vasko004d3152020-06-11 19:59:22 +02008041 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008042 LY_CHECK_GOTO(rc, cleanup);
8043
8044 /* ('*' / 'div' / 'mod' UnaryExpr)* */
8045 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008046 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008047
Michal Vasko004d3152020-06-11 19:59:22 +02008048 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02008049 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008050 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008051 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008052
aPiecek8b0cc152021-05-31 16:40:31 +02008053 if (options & LYXP_SKIP_EXPR) {
8054 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008055 LY_CHECK_GOTO(rc, cleanup);
8056 continue;
8057 }
8058
8059 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008060 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008061 LY_CHECK_GOTO(rc, cleanup);
8062
8063 /* eval */
8064 if (options & LYXP_SCNODE_ALL) {
8065 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008066 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008067 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008068 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008069 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008070 LY_CHECK_GOTO(rc, cleanup);
8071 }
8072 }
8073
8074cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008075 lyxp_set_free_content(&orig_set);
8076 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008077 return rc;
8078}
8079
8080/**
8081 * @brief Evaluate AdditiveExpr. Logs directly on error.
8082 *
Michal Vaskod3678892020-05-21 10:06:58 +02008083 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008084 * | AdditiveExpr '+' MultiplicativeExpr
8085 * | AdditiveExpr '-' MultiplicativeExpr
8086 *
8087 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008088 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008089 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008090 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008091 * @param[in] options XPath options.
8092 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8093 */
8094static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008095eval_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 +02008096{
8097 LY_ERR rc;
8098 uint16_t this_op;
8099 struct lyxp_set orig_set, set2;
8100 uint16_t i;
8101
8102 assert(repeat);
8103
8104 set_init(&orig_set, set);
8105 set_init(&set2, set);
8106
8107 set_fill_set(&orig_set, set);
8108
Michal Vasko004d3152020-06-11 19:59:22 +02008109 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008110 LY_CHECK_GOTO(rc, cleanup);
8111
8112 /* ('+' / '-' MultiplicativeExpr)* */
8113 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008114 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008115
Michal Vasko004d3152020-06-11 19:59:22 +02008116 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008117 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008118 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008119 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008120
aPiecek8b0cc152021-05-31 16:40:31 +02008121 if (options & LYXP_SKIP_EXPR) {
8122 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008123 LY_CHECK_GOTO(rc, cleanup);
8124 continue;
8125 }
8126
8127 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008128 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008129 LY_CHECK_GOTO(rc, cleanup);
8130
8131 /* eval */
8132 if (options & LYXP_SCNODE_ALL) {
8133 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008134 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008135 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008136 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008137 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008138 LY_CHECK_GOTO(rc, cleanup);
8139 }
8140 }
8141
8142cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008143 lyxp_set_free_content(&orig_set);
8144 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008145 return rc;
8146}
8147
8148/**
8149 * @brief Evaluate RelationalExpr. Logs directly on error.
8150 *
Michal Vaskod3678892020-05-21 10:06:58 +02008151 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008152 * | RelationalExpr '<' AdditiveExpr
8153 * | RelationalExpr '>' AdditiveExpr
8154 * | RelationalExpr '<=' AdditiveExpr
8155 * | RelationalExpr '>=' AdditiveExpr
8156 *
8157 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008158 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008159 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008160 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008161 * @param[in] options XPath options.
8162 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8163 */
8164static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008165eval_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 +02008166{
8167 LY_ERR rc;
8168 uint16_t this_op;
8169 struct lyxp_set orig_set, set2;
8170 uint16_t i;
8171
8172 assert(repeat);
8173
8174 set_init(&orig_set, set);
8175 set_init(&set2, set);
8176
8177 set_fill_set(&orig_set, set);
8178
Michal Vasko004d3152020-06-11 19:59:22 +02008179 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008180 LY_CHECK_GOTO(rc, cleanup);
8181
8182 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8183 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008184 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008185
Michal Vasko004d3152020-06-11 19:59:22 +02008186 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02008187 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008188 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008189 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008190
aPiecek8b0cc152021-05-31 16:40:31 +02008191 if (options & LYXP_SKIP_EXPR) {
8192 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008193 LY_CHECK_GOTO(rc, cleanup);
8194 continue;
8195 }
8196
8197 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008198 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008199 LY_CHECK_GOTO(rc, cleanup);
8200
8201 /* eval */
8202 if (options & LYXP_SCNODE_ALL) {
8203 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008204 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008205 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008206 } else {
8207 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8208 LY_CHECK_GOTO(rc, cleanup);
8209 }
8210 }
8211
8212cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008213 lyxp_set_free_content(&orig_set);
8214 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008215 return rc;
8216}
8217
8218/**
8219 * @brief Evaluate EqualityExpr. Logs directly on error.
8220 *
Michal Vaskod3678892020-05-21 10:06:58 +02008221 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008222 * | EqualityExpr '!=' RelationalExpr
8223 *
8224 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008225 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008226 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008227 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008228 * @param[in] options XPath options.
8229 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8230 */
8231static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008232eval_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 +02008233{
8234 LY_ERR rc;
8235 uint16_t this_op;
8236 struct lyxp_set orig_set, set2;
8237 uint16_t i;
8238
8239 assert(repeat);
8240
8241 set_init(&orig_set, set);
8242 set_init(&set2, set);
8243
8244 set_fill_set(&orig_set, set);
8245
Michal Vasko004d3152020-06-11 19:59:22 +02008246 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008247 LY_CHECK_GOTO(rc, cleanup);
8248
8249 /* ('=' / '!=' RelationalExpr)* */
8250 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008251 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008252
Michal Vasko004d3152020-06-11 19:59:22 +02008253 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02008254 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008255 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008256 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008257
aPiecek8b0cc152021-05-31 16:40:31 +02008258 if (options & LYXP_SKIP_EXPR) {
8259 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008260 LY_CHECK_GOTO(rc, cleanup);
8261 continue;
8262 }
8263
8264 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008265 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008266 LY_CHECK_GOTO(rc, cleanup);
8267
8268 /* eval */
8269 if (options & LYXP_SCNODE_ALL) {
8270 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008271 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8272 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008273 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008274 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008275 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008276 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8277 LY_CHECK_GOTO(rc, cleanup);
8278 }
8279 }
8280
8281cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008282 lyxp_set_free_content(&orig_set);
8283 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008284 return rc;
8285}
8286
8287/**
8288 * @brief Evaluate AndExpr. Logs directly on error.
8289 *
Michal Vaskod3678892020-05-21 10:06:58 +02008290 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008291 *
8292 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008293 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008294 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008295 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008296 * @param[in] options XPath options.
8297 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8298 */
8299static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008300eval_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 +02008301{
8302 LY_ERR rc;
8303 struct lyxp_set orig_set, set2;
8304 uint16_t i;
8305
8306 assert(repeat);
8307
8308 set_init(&orig_set, set);
8309 set_init(&set2, set);
8310
8311 set_fill_set(&orig_set, set);
8312
Michal Vasko004d3152020-06-11 19:59:22 +02008313 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008314 LY_CHECK_GOTO(rc, cleanup);
8315
8316 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008317 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008318 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008319 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008320 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008321 }
8322
8323 /* ('and' EqualityExpr)* */
8324 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008325 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008326 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008327 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008328 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008329
8330 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008331 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8332 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008333 LY_CHECK_GOTO(rc, cleanup);
8334 continue;
8335 }
8336
8337 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008338 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008339 LY_CHECK_GOTO(rc, cleanup);
8340
8341 /* eval - just get boolean value actually */
8342 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008343 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008344 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008345 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008346 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008347 set_fill_set(set, &set2);
8348 }
8349 }
8350
8351cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008352 lyxp_set_free_content(&orig_set);
8353 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008354 return rc;
8355}
8356
8357/**
8358 * @brief Evaluate OrExpr. Logs directly on error.
8359 *
Michal Vaskod3678892020-05-21 10:06:58 +02008360 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008361 *
8362 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008363 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008364 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008365 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008366 * @param[in] options XPath options.
8367 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8368 */
8369static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008370eval_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 +02008371{
8372 LY_ERR rc;
8373 struct lyxp_set orig_set, set2;
8374 uint16_t i;
8375
8376 assert(repeat);
8377
8378 set_init(&orig_set, set);
8379 set_init(&set2, set);
8380
8381 set_fill_set(&orig_set, set);
8382
Michal Vasko004d3152020-06-11 19:59:22 +02008383 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008384 LY_CHECK_GOTO(rc, cleanup);
8385
8386 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008387 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008388 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008389 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008390 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008391 }
8392
8393 /* ('or' AndExpr)* */
8394 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008395 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008396 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008397 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008398 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008399
8400 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008401 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8402 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008403 LY_CHECK_GOTO(rc, cleanup);
8404 continue;
8405 }
8406
8407 set_fill_set(&set2, &orig_set);
8408 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8409 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008410 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008411 LY_CHECK_GOTO(rc, cleanup);
8412
8413 /* eval - just get boolean value actually */
8414 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008415 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008416 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008417 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008418 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008419 set_fill_set(set, &set2);
8420 }
8421 }
8422
8423cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008424 lyxp_set_free_content(&orig_set);
8425 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008426 return rc;
8427}
8428
8429/**
Michal Vasko004d3152020-06-11 19:59:22 +02008430 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008431 *
8432 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008433 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008434 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02008435 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008436 * @param[in] options XPath options.
8437 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8438 */
8439static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008440eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8441 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008442{
8443 uint16_t i, count;
8444 enum lyxp_expr_type next_etype;
8445 LY_ERR rc;
8446
8447 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008448 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008449 next_etype = LYXP_EXPR_NONE;
8450 } else {
8451 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008452 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008453
8454 /* select one-priority lower because etype expression called us */
8455 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008456 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008457 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008458 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008459 } else {
8460 next_etype = LYXP_EXPR_NONE;
8461 }
8462 }
8463
8464 /* decide what expression are we parsing based on the repeat */
8465 switch (next_etype) {
8466 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008467 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008468 break;
8469 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008470 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008471 break;
8472 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008473 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008474 break;
8475 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008476 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008477 break;
8478 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008479 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008480 break;
8481 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008482 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008483 break;
8484 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008485 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008486 break;
8487 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008488 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008489 break;
8490 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008491 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008492 break;
8493 default:
8494 LOGINT_RET(set->ctx);
8495 }
8496
8497 return rc;
8498}
8499
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008500/**
8501 * @brief Get root type.
8502 *
8503 * @param[in] ctx_node Context node.
8504 * @param[in] ctx_scnode Schema context node.
8505 * @param[in] options XPath options.
8506 * @return Root type.
8507 */
8508static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008509lyxp_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 +01008510{
Michal Vasko6b26e742020-07-17 15:02:10 +02008511 const struct lysc_node *op;
8512
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008513 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008514 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008515 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008516
8517 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008518 /* general root that can access everything */
8519 return LYXP_NODE_ROOT;
8520 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8521 /* root context node can access only config data (because we said so, it is unspecified) */
8522 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008523 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008524 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008525 }
8526
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008527 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008528 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008529 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008530
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008531 if (op || !(options & LYXP_SCHEMA)) {
8532 /* general root that can access everything */
8533 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05008534 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008535 /* root context node can access only config data (because we said so, it is unspecified) */
8536 return LYXP_NODE_ROOT_CONFIG;
8537 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008538 return LYXP_NODE_ROOT;
8539}
8540
Michal Vasko03ff5a72019-09-11 13:49:33 +02008541LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008542lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008543 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 +01008544 struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008545{
Michal Vasko004d3152020-06-11 19:59:22 +02008546 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008547 LY_ERR rc;
8548
Michal Vasko400e9672021-01-11 13:39:17 +01008549 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008550 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008551 LOGARG(NULL, "Current module must be set if schema format is used.");
8552 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008553 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008554
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008555 if (tree) {
8556 /* adjust the pointer to be the first top-level sibling */
8557 while (tree->parent) {
8558 tree = lyd_parent(tree);
8559 }
8560 tree = lyd_first_sibling(tree);
8561 }
8562
Michal Vasko03ff5a72019-09-11 13:49:33 +02008563 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008564 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008565 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008566 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8567 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8568
Michal Vasko400e9672021-01-11 13:39:17 +01008569 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008570 set->cur_node = ctx_node;
8571 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008572 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8573 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008574 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008575 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008576 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008577 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008578
Radek Krejciddace2c2021-01-08 11:30:56 +01008579 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008580
Michal Vasko03ff5a72019-09-11 13:49:33 +02008581 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008582 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008583 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008584 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008585 }
8586
Radek Krejciddace2c2021-01-08 11:30:56 +01008587 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008588 return rc;
8589}
8590
8591#if 0
8592
8593/* full xml printing of set elements, not used currently */
8594
8595void
8596lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8597{
8598 uint32_t i;
8599 char *str_num;
8600 struct lyout out;
8601
8602 memset(&out, 0, sizeof out);
8603
8604 out.type = LYOUT_STREAM;
8605 out.method.f = f;
8606
8607 switch (set->type) {
8608 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008609 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008610 break;
8611 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008612 ly_print_(&out, "Boolean XPath set:\n");
8613 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008614 break;
8615 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008616 ly_print_(&out, "String XPath set:\n");
8617 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008618 break;
8619 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008620 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008621
8622 if (isnan(set->value.num)) {
8623 str_num = strdup("NaN");
8624 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8625 str_num = strdup("0");
8626 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8627 str_num = strdup("Infinity");
8628 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8629 str_num = strdup("-Infinity");
8630 } else if ((long long)set->value.num == set->value.num) {
8631 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8632 str_num = NULL;
8633 }
8634 } else {
8635 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8636 str_num = NULL;
8637 }
8638 }
8639 if (!str_num) {
8640 LOGMEM;
8641 return;
8642 }
Michal Vasko5233e962020-08-14 14:26:20 +02008643 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008644 free(str_num);
8645 break;
8646 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008647 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008648
8649 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008650 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008651 switch (set->node_type[i]) {
8652 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008653 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008654 break;
8655 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008656 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008657 break;
8658 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008659 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008660 break;
8661 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008662 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008663 break;
8664 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008665 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008666 break;
8667 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008668 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008669 break;
8670 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008671 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008672 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008673 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008674 break;
8675 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008676 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 +02008677 break;
8678 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008679 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 +02008680 break;
8681 }
8682 }
8683 break;
8684 }
8685}
8686
8687#endif
8688
8689LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008690lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008691{
8692 long double num;
8693 char *str;
8694 LY_ERR rc;
8695
8696 if (!set || (set->type == target)) {
8697 return LY_SUCCESS;
8698 }
8699
8700 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008701 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008702
8703 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008704 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008705 return LY_EINVAL;
8706 }
8707
8708 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008709 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008710 switch (set->type) {
8711 case LYXP_SET_NUMBER:
8712 if (isnan(set->val.num)) {
8713 set->val.str = strdup("NaN");
8714 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8715 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8716 set->val.str = strdup("0");
8717 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8718 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8719 set->val.str = strdup("Infinity");
8720 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8721 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8722 set->val.str = strdup("-Infinity");
8723 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8724 } else if ((long long)set->val.num == set->val.num) {
8725 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8726 LOGMEM_RET(set->ctx);
8727 }
8728 set->val.str = str;
8729 } else {
8730 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8731 LOGMEM_RET(set->ctx);
8732 }
8733 set->val.str = str;
8734 }
8735 break;
8736 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008737 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008738 set->val.str = strdup("true");
8739 } else {
8740 set->val.str = strdup("false");
8741 }
8742 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8743 break;
8744 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008745 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008746 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008747
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008748 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008749 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008750 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008751 set->val.str = str;
8752 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008753 default:
8754 LOGINT_RET(set->ctx);
8755 }
8756 set->type = LYXP_SET_STRING;
8757 }
8758
8759 /* to NUMBER */
8760 if (target == LYXP_SET_NUMBER) {
8761 switch (set->type) {
8762 case LYXP_SET_STRING:
8763 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008764 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008765 set->val.num = num;
8766 break;
8767 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008768 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008769 set->val.num = 1;
8770 } else {
8771 set->val.num = 0;
8772 }
8773 break;
8774 default:
8775 LOGINT_RET(set->ctx);
8776 }
8777 set->type = LYXP_SET_NUMBER;
8778 }
8779
8780 /* to BOOLEAN */
8781 if (target == LYXP_SET_BOOLEAN) {
8782 switch (set->type) {
8783 case LYXP_SET_NUMBER:
8784 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008785 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008786 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008787 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008788 }
8789 break;
8790 case LYXP_SET_STRING:
8791 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008792 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008793 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008794 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008795 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008796 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008797 }
8798 break;
8799 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008800 if (set->used) {
8801 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008802 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008803 } else {
8804 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008805 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008806 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008807 break;
8808 default:
8809 LOGINT_RET(set->ctx);
8810 }
8811 set->type = LYXP_SET_BOOLEAN;
8812 }
8813
Michal Vasko03ff5a72019-09-11 13:49:33 +02008814 return LY_SUCCESS;
8815}
8816
8817LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008818lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008819 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
Michal Vasko400e9672021-01-11 13:39:17 +01008820 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008821{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008822 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008823 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008824
Michal Vasko400e9672021-01-11 13:39:17 +01008825 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008826 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008827 LOGARG(NULL, "Current module must be set if schema format is used.");
8828 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008829 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008830
8831 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008832 memset(set, 0, sizeof *set);
8833 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008834 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8835 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 +01008836 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008837
Michal Vasko400e9672021-01-11 13:39:17 +01008838 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008839 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008840 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008841 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8842 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008843 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008844 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008845 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008846
Radek Krejciddace2c2021-01-08 11:30:56 +01008847 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008848
Michal Vasko03ff5a72019-09-11 13:49:33 +02008849 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01008850 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
8851
Radek Krejciddace2c2021-01-08 11:30:56 +01008852 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008853 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008854}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008855
8856API const char *
8857lyxp_get_expr(const struct lyxp_expr *path)
8858{
8859 if (!path) {
8860 return NULL;
8861 }
8862
8863 return path->expr;
8864}