blob: 410dbc7d3e8403e451523516516aa12fd03265fd [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) {
aPiecekadc1e4f2021-10-07 11:15:12 +02001120 /* release hidden allocated data (lyxp_set.size) */
1121 lyxp_set_free_content(set1);
1122 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001123 memcpy(set1, set2, sizeof *set1);
1124 return;
1125 }
1126
1127 if (set1->used + set2->used > set1->size) {
1128 set1->size = set1->used + set2->used;
1129 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1130 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1131 }
1132
1133 orig_used = set1->used;
1134
1135 for (i = 0; i < set2->used; ++i) {
1136 for (j = 0; j < orig_used; ++j) {
1137 /* detect duplicities */
1138 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1139 break;
1140 }
1141 }
1142
Michal Vasko0587bce2020-12-10 12:19:21 +01001143 if (j < orig_used) {
1144 /* node is there, but update its status if needed */
1145 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1146 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001147 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1148 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1149 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001150 }
1151 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001152 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1153 ++set1->used;
1154 }
1155 }
1156
Michal Vaskod3678892020-05-21 10:06:58 +02001157 lyxp_set_free_content(set2);
1158 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001159}
1160
1161/**
1162 * @brief Insert a node into a set. Context position aware.
1163 *
1164 * @param[in] set Set to use.
1165 * @param[in] node Node to insert to @p set.
1166 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1167 * @param[in] node_type Node type of @p node.
1168 * @param[in] idx Index in @p set to insert into.
1169 */
1170static void
1171set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1172{
Michal Vaskod3678892020-05-21 10:06:58 +02001173 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001174
Michal Vaskod3678892020-05-21 10:06:58 +02001175 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001176 /* first item */
1177 if (idx) {
1178 /* no real harm done, but it is a bug */
1179 LOGINT(set->ctx);
1180 idx = 0;
1181 }
1182 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1183 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1184 set->type = LYXP_SET_NODE_SET;
1185 set->used = 0;
1186 set->size = LYXP_SET_SIZE_START;
1187 set->ctx_pos = 1;
1188 set->ctx_size = 1;
1189 set->ht = NULL;
1190 } else {
1191 /* not an empty set */
1192 if (set->used == set->size) {
1193
1194 /* set is full */
1195 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1196 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1197 set->size += LYXP_SET_SIZE_STEP;
1198 }
1199
1200 if (idx > set->used) {
1201 LOGINT(set->ctx);
1202 idx = set->used;
1203 }
1204
1205 /* make space for the new node */
1206 if (idx < set->used) {
1207 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1208 }
1209 }
1210
1211 /* finally assign the value */
1212 set->val.nodes[idx].node = (struct lyd_node *)node;
1213 set->val.nodes[idx].type = node_type;
1214 set->val.nodes[idx].pos = pos;
1215 ++set->used;
1216
Michal Vasko2416fdd2021-10-01 11:07:10 +02001217 /* add into hash table */
1218 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001219}
1220
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001221LY_ERR
1222lyxp_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 +02001223{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001224 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001225
1226 assert(set->type == LYXP_SET_SCNODE_SET);
1227
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001228 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001229 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001230 } else {
1231 if (set->used == set->size) {
1232 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 +02001233 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001234 set->size += LYXP_SET_SIZE_STEP;
1235 }
1236
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001237 index = set->used;
1238 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1239 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001240 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001241 ++set->used;
1242 }
1243
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001244 if (index_p) {
1245 *index_p = index;
1246 }
1247
1248 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001249}
1250
1251/**
1252 * @brief Replace a node in a set with another. Context position aware.
1253 *
1254 * @param[in] set Set to use.
1255 * @param[in] node Node to insert to @p set.
1256 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1257 * @param[in] node_type Node type of @p node.
1258 * @param[in] idx Index in @p set of the node to replace.
1259 */
1260static void
1261set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1262{
1263 assert(set && (idx < set->used));
1264
Michal Vasko2caefc12019-11-14 16:07:56 +01001265 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1266 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1267 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001268 set->val.nodes[idx].node = (struct lyd_node *)node;
1269 set->val.nodes[idx].type = node_type;
1270 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001271 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1272 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1273 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001274}
1275
1276/**
1277 * @brief Set all nodes with ctx 1 to a new unique context value.
1278 *
1279 * @param[in] set Set to modify.
1280 * @return New context value.
1281 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001282static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001283set_scnode_new_in_ctx(struct lyxp_set *set)
1284{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001285 uint32_t i;
1286 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001287
1288 assert(set->type == LYXP_SET_SCNODE_SET);
1289
Radek Krejcif13b87b2020-12-01 22:02:17 +01001290 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001291retry:
1292 for (i = 0; i < set->used; ++i) {
1293 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1294 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1295 goto retry;
1296 }
1297 }
1298 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001299 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001300 set->val.scnodes[i].in_ctx = ret_ctx;
1301 }
1302 }
1303
1304 return ret_ctx;
1305}
1306
1307/**
1308 * @brief Get unique @p node position in the data.
1309 *
1310 * @param[in] node Node to find.
1311 * @param[in] node_type Node type of @p node.
1312 * @param[in] root Root node.
1313 * @param[in] root_type Type of the XPath @p root node.
1314 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1315 * be used to increase efficiency and start the DFS from this node.
1316 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1317 * @return Node position.
1318 */
1319static uint32_t
1320get_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 +02001321 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001322{
Michal Vasko56daf732020-08-10 10:57:18 +02001323 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001324 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001325 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001326
1327 assert(prev && prev_pos && !root->prev->next);
1328
1329 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1330 return 0;
1331 }
1332
1333 if (*prev) {
1334 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001335 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001336 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001337 goto dfs_search;
1338 }
1339
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001340 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001341 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001342dfs_search:
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001343 LYD_TREE_DFS_continue = 0;
1344
Michal Vasko56daf732020-08-10 10:57:18 +02001345 if (*prev && !elem) {
1346 /* resume previous DFS */
1347 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1348 LYD_TREE_DFS_continue = 0;
1349 }
1350
Michal Vasko03ff5a72019-09-11 13:49:33 +02001351 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001352 /* skip */
1353 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001354 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001355 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001356 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001357 break;
1358 }
Michal Vasko56daf732020-08-10 10:57:18 +02001359 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001360 }
Michal Vasko56daf732020-08-10 10:57:18 +02001361
1362 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001363 }
1364
1365 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001366 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001367 break;
1368 }
1369 }
1370
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001371 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001372 if (!(*prev)) {
1373 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001374 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001375 return 0;
1376 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001377 /* start the search again from the beginning */
1378 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001379
Michal Vasko56daf732020-08-10 10:57:18 +02001380 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001381 pos = 1;
1382 goto dfs_search;
1383 }
1384 }
1385
1386 /* remember the last found node for next time */
1387 *prev = node;
1388 *prev_pos = pos;
1389
1390 return pos;
1391}
1392
1393/**
1394 * @brief Assign (fill) missing node positions.
1395 *
1396 * @param[in] set Set to fill positions in.
1397 * @param[in] root Context root node.
1398 * @param[in] root_type Context root type.
1399 * @return LY_ERR
1400 */
1401static LY_ERR
1402set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1403{
1404 const struct lyd_node *prev = NULL, *tmp_node;
1405 uint32_t i, tmp_pos = 0;
1406
1407 for (i = 0; i < set->used; ++i) {
1408 if (!set->val.nodes[i].pos) {
1409 tmp_node = NULL;
1410 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001411 case LYXP_NODE_META:
1412 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001413 if (!tmp_node) {
1414 LOGINT_RET(root->schema->module->ctx);
1415 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001416 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001417 case LYXP_NODE_ELEM:
1418 case LYXP_NODE_TEXT:
1419 if (!tmp_node) {
1420 tmp_node = set->val.nodes[i].node;
1421 }
1422 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1423 break;
1424 default:
1425 /* all roots have position 0 */
1426 break;
1427 }
1428 }
1429 }
1430
1431 return LY_SUCCESS;
1432}
1433
1434/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001435 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001436 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001437 * @param[in] meta Metadata to use.
1438 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001439 */
1440static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001441get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001442{
1443 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001444 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001445
Michal Vasko9f96a052020-03-10 09:41:45 +01001446 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001447 ++pos;
1448 }
1449
Michal Vasko9f96a052020-03-10 09:41:45 +01001450 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001451 return pos;
1452}
1453
1454/**
1455 * @brief Compare 2 nodes in respect to XPath document order.
1456 *
1457 * @param[in] item1 1st node.
1458 * @param[in] item2 2nd node.
1459 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1460 */
1461static int
1462set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1463{
Michal Vasko9f96a052020-03-10 09:41:45 +01001464 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001465
1466 if (item1->pos < item2->pos) {
1467 return -1;
1468 }
1469
1470 if (item1->pos > item2->pos) {
1471 return 1;
1472 }
1473
1474 /* node positions are equal, the fun case */
1475
1476 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1477 /* special case since text nodes are actually saved as their parents */
1478 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1479 if (item1->type == LYXP_NODE_ELEM) {
1480 assert(item2->type == LYXP_NODE_TEXT);
1481 return -1;
1482 } else {
1483 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1484 return 1;
1485 }
1486 }
1487
Michal Vasko9f96a052020-03-10 09:41:45 +01001488 /* we need meta positions now */
1489 if (item1->type == LYXP_NODE_META) {
1490 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001491 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001492 if (item2->type == LYXP_NODE_META) {
1493 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001494 }
1495
Michal Vasko9f96a052020-03-10 09:41:45 +01001496 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001497 /* check for duplicates */
1498 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001499 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001500 return 0;
1501 }
1502
Michal Vasko9f96a052020-03-10 09:41:45 +01001503 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001504 /* elem is always first, 2nd node is after it */
1505 if (item1->type == LYXP_NODE_ELEM) {
1506 assert(item2->type != LYXP_NODE_ELEM);
1507 return -1;
1508 }
1509
Michal Vasko9f96a052020-03-10 09:41:45 +01001510 /* 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 +02001511 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001512 if (((item1->type == LYXP_NODE_TEXT) &&
1513 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1514 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1515 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1516 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001517 return 1;
1518 }
1519
Michal Vasko9f96a052020-03-10 09:41:45 +01001520 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001521 /* 2nd is after 1st */
1522 return -1;
1523}
1524
1525/**
1526 * @brief Set cast for comparisons.
1527 *
1528 * @param[in] trg Target set to cast source into.
1529 * @param[in] src Source set.
1530 * @param[in] type Target set type.
1531 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001532 * @return LY_ERR
1533 */
1534static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001535set_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 +02001536{
1537 assert(src->type == LYXP_SET_NODE_SET);
1538
1539 set_init(trg, src);
1540
1541 /* insert node into target set */
1542 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1543
1544 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001545 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001546}
1547
Michal Vasko4c7763f2020-07-27 17:40:37 +02001548/**
1549 * @brief Set content canonization for comparisons.
1550 *
1551 * @param[in] trg Target set to put the canononized source into.
1552 * @param[in] src Source set.
1553 * @param[in] xp_node Source XPath node/meta to use for canonization.
1554 * @return LY_ERR
1555 */
1556static LY_ERR
1557set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1558{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001559 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001560 struct lyd_value val;
1561 struct ly_err_item *err = NULL;
1562 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001563 LY_ERR rc;
1564
1565 /* is there anything to canonize even? */
1566 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1567 /* do we have a type to use for canonization? */
1568 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1569 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1570 } else if (xp_node->type == LYXP_NODE_META) {
1571 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1572 }
1573 }
1574 if (!type) {
1575 goto fill;
1576 }
1577
1578 if (src->type == LYXP_SET_NUMBER) {
1579 /* canonize number */
1580 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1581 LOGMEM(src->ctx);
1582 return LY_EMEM;
1583 }
1584 } else {
1585 /* canonize string */
1586 str = strdup(src->val.str);
1587 }
1588
1589 /* ignore errors, the value may not satisfy schema constraints */
Radek Krejci0b013302021-03-29 15:22:32 +02001590 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 +01001591 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001592 ly_err_free(err);
1593 if (rc) {
1594 /* invalid value */
Radek IÅ¡a48460222021-03-02 15:18:32 +01001595 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001596 goto fill;
1597 }
1598
Michal Vasko4c7763f2020-07-27 17:40:37 +02001599 /* use the canonized value */
1600 set_init(trg, src);
1601 trg->type = src->type;
1602 if (src->type == LYXP_SET_NUMBER) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001603 trg->val.num = strtold(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001604 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1605 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001606 trg->val.str = strdup(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko4c7763f2020-07-27 17:40:37 +02001607 }
1608 type->plugin->free(src->ctx, &val);
1609 return LY_SUCCESS;
1610
1611fill:
1612 /* no canonization needed/possible */
1613 set_fill_set(trg, src);
1614 return LY_SUCCESS;
1615}
1616
Michal Vasko03ff5a72019-09-11 13:49:33 +02001617#ifndef NDEBUG
1618
1619/**
1620 * @brief Bubble sort @p set into XPath document order.
1621 * Context position aware. Unused in the 'Release' build target.
1622 *
1623 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001624 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1625 */
1626static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001627set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001628{
1629 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001630 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001631 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001632 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001633 struct lyxp_set_node item;
1634 struct lyxp_set_hash_node hnode;
1635 uint64_t hash;
1636
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001637 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001638 return 0;
1639 }
1640
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001641 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001642 for (root = set->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001643 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001644
1645 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001646 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001647 return -1;
1648 }
1649
1650 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1651 print_set_debug(set);
1652
1653 for (i = 0; i < set->used; ++i) {
1654 inverted = 0;
1655 change = 0;
1656
1657 for (j = 1; j < set->used - i; ++j) {
1658 /* compare node positions */
1659 if (inverted) {
1660 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1661 } else {
1662 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1663 }
1664
1665 /* swap if needed */
1666 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1667 change = 1;
1668
1669 item = set->val.nodes[j - 1];
1670 set->val.nodes[j - 1] = set->val.nodes[j];
1671 set->val.nodes[j] = item;
1672 } else {
1673 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1674 inverted = !inverted;
1675 }
1676 }
1677
1678 ++ret;
1679
1680 if (!change) {
1681 break;
1682 }
1683 }
1684
1685 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1686 print_set_debug(set);
1687
1688 /* check node hashes */
1689 if (set->used >= LYD_HT_MIN_ITEMS) {
1690 assert(set->ht);
1691 for (i = 0; i < set->used; ++i) {
1692 hnode.node = set->val.nodes[i].node;
1693 hnode.type = set->val.nodes[i].type;
1694
1695 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1696 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1697 hash = dict_hash_multi(hash, NULL, 0);
1698
1699 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1700 }
1701 }
1702
1703 return ret - 1;
1704}
1705
1706/**
1707 * @brief Remove duplicate entries in a sorted node set.
1708 *
1709 * @param[in] set Sorted set to check.
1710 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1711 */
1712static LY_ERR
1713set_sorted_dup_node_clean(struct lyxp_set *set)
1714{
1715 uint32_t i = 0;
1716 LY_ERR ret = LY_SUCCESS;
1717
1718 if (set->used > 1) {
1719 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001720 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1721 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001722 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001723 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001724 }
Michal Vasko57eab132019-09-24 11:46:26 +02001725 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001726 }
1727 }
1728
Michal Vasko2caefc12019-11-14 16:07:56 +01001729 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001730 return ret;
1731}
1732
1733#endif
1734
1735/**
1736 * @brief Merge 2 sorted sets into one.
1737 *
1738 * @param[in,out] trg Set to merge into. Duplicates are removed.
1739 * @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 +02001740 * @return LY_ERR
1741 */
1742static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001743set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001744{
1745 uint32_t i, j, k, count, dup_count;
1746 int cmp;
1747 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001748
Michal Vaskod3678892020-05-21 10:06:58 +02001749 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001750 return LY_EINVAL;
1751 }
1752
Michal Vaskod3678892020-05-21 10:06:58 +02001753 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001754 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001755 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001756 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001757 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001758 return LY_SUCCESS;
1759 }
1760
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001761 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001762 for (root = trg->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001763 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001764
1765 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001766 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001767 return LY_EINT;
1768 }
1769
1770#ifndef NDEBUG
1771 LOGDBG(LY_LDGXPATH, "MERGE target");
1772 print_set_debug(trg);
1773 LOGDBG(LY_LDGXPATH, "MERGE source");
1774 print_set_debug(src);
1775#endif
1776
1777 /* make memory for the merge (duplicates are not detected yet, so space
1778 * will likely be wasted on them, too bad) */
1779 if (trg->size - trg->used < src->used) {
1780 trg->size = trg->used + src->used;
1781
1782 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1783 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1784 }
1785
1786 i = 0;
1787 j = 0;
1788 count = 0;
1789 dup_count = 0;
1790 do {
1791 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1792 if (!cmp) {
1793 if (!count) {
1794 /* duplicate, just skip it */
1795 ++i;
1796 ++j;
1797 } else {
1798 /* we are copying something already, so let's copy the duplicate too,
1799 * we are hoping that afterwards there are some more nodes to
1800 * copy and this way we can copy them all together */
1801 ++count;
1802 ++dup_count;
1803 ++i;
1804 ++j;
1805 }
1806 } else if (cmp < 0) {
1807 /* inserting src node into trg, just remember it for now */
1808 ++count;
1809 ++i;
1810
1811 /* insert the hash now */
1812 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1813 } else if (count) {
1814copy_nodes:
1815 /* time to actually copy the nodes, we have found the largest block of nodes */
1816 memmove(&trg->val.nodes[j + (count - dup_count)],
1817 &trg->val.nodes[j],
1818 (trg->used - j) * sizeof *trg->val.nodes);
1819 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1820
1821 trg->used += count - dup_count;
1822 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1823 j += count - dup_count;
1824
1825 count = 0;
1826 dup_count = 0;
1827 } else {
1828 ++j;
1829 }
1830 } while ((i < src->used) && (j < trg->used));
1831
1832 if ((i < src->used) || count) {
1833 /* insert all the hashes first */
1834 for (k = i; k < src->used; ++k) {
1835 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1836 }
1837
1838 /* loop ended, but we need to copy something at trg end */
1839 count += src->used - i;
1840 i = src->used;
1841 goto copy_nodes;
1842 }
1843
1844 /* we are inserting hashes before the actual node insert, which causes
1845 * situations when there were initially not enough items for a hash table,
1846 * but even after some were inserted, hash table was not created (during
1847 * insertion the number of items is not updated yet) */
1848 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1849 set_insert_node_hash(trg, NULL, 0);
1850 }
1851
1852#ifndef NDEBUG
1853 LOGDBG(LY_LDGXPATH, "MERGE result");
1854 print_set_debug(trg);
1855#endif
1856
Michal Vaskod3678892020-05-21 10:06:58 +02001857 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001858 return LY_SUCCESS;
1859}
1860
1861/*
1862 * (re)parse functions
1863 *
1864 * Parse functions parse the expression into
1865 * tokens (syntactic analysis).
1866 *
1867 * Reparse functions perform semantic analysis
1868 * (do not save the result, just a check) of
1869 * the expression and fill repeat indices.
1870 */
1871
Michal Vasko14676352020-05-29 11:35:55 +02001872LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001873lyxp_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 +02001874{
Michal Vasko004d3152020-06-11 19:59:22 +02001875 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001876 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001877 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001878 }
Michal Vasko14676352020-05-29 11:35:55 +02001879 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001880 }
1881
Michal Vasko004d3152020-06-11 19:59:22 +02001882 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001883 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001884 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001885 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001886 }
Michal Vasko14676352020-05-29 11:35:55 +02001887 return LY_ENOT;
1888 }
1889
1890 return LY_SUCCESS;
1891}
1892
Michal Vasko004d3152020-06-11 19:59:22 +02001893LY_ERR
1894lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1895{
1896 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1897
1898 /* skip the token */
1899 ++(*tok_idx);
1900
1901 return LY_SUCCESS;
1902}
1903
Michal Vasko14676352020-05-29 11:35:55 +02001904/* just like lyxp_check_token() but tests for 2 tokens */
1905static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001906exp_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 +02001907 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001908{
Michal Vasko004d3152020-06-11 19:59:22 +02001909 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001910 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001911 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001912 }
1913 return LY_EINCOMPLETE;
1914 }
1915
Michal Vasko004d3152020-06-11 19:59:22 +02001916 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001917 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001918 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001919 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001920 }
1921 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001922 }
1923
1924 return LY_SUCCESS;
1925}
1926
Michal Vasko4911eeb2021-06-28 11:23:05 +02001927LY_ERR
1928lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok1,
1929 enum lyxp_token want_tok2)
1930{
1931 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
1932
1933 /* skip the token */
1934 ++(*tok_idx);
1935
1936 return LY_SUCCESS;
1937}
1938
Michal Vasko03ff5a72019-09-11 13:49:33 +02001939/**
1940 * @brief Stack operation push on the repeat array.
1941 *
1942 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001943 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001944 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1945 */
1946static void
Michal Vasko004d3152020-06-11 19:59:22 +02001947exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001948{
1949 uint16_t i;
1950
Michal Vasko004d3152020-06-11 19:59:22 +02001951 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001952 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001953 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1954 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1955 exp->repeat[tok_idx][i] = repeat_op_idx;
1956 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001957 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001958 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1959 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1960 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001961 }
1962}
1963
1964/**
1965 * @brief Reparse Predicate. Logs directly on error.
1966 *
1967 * [7] Predicate ::= '[' Expr ']'
1968 *
1969 * @param[in] ctx Context for logging.
1970 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001971 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02001972 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001973 * @return LY_ERR
1974 */
1975static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02001976reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001977{
1978 LY_ERR rc;
1979
Michal Vasko004d3152020-06-11 19:59:22 +02001980 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001981 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001982 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001983
aPiecekbf968d92021-05-27 14:35:05 +02001984 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001985 LY_CHECK_RET(rc);
1986
Michal Vasko004d3152020-06-11 19:59:22 +02001987 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001988 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001989 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001990
1991 return LY_SUCCESS;
1992}
1993
1994/**
1995 * @brief Reparse RelativeLocationPath. Logs directly on error.
1996 *
1997 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1998 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1999 * [6] NodeTest ::= NameTest | NodeType '(' ')'
2000 *
2001 * @param[in] ctx Context for logging.
2002 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002003 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002004 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002005 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
2006 */
2007static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002008reparse_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 +02002009{
2010 LY_ERR rc;
2011
Michal Vasko004d3152020-06-11 19:59:22 +02002012 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002013 LY_CHECK_RET(rc);
2014
2015 goto step;
2016 do {
2017 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002018 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002019
Michal Vasko004d3152020-06-11 19:59:22 +02002020 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002021 LY_CHECK_RET(rc);
2022step:
2023 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002024 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002025 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002026 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002027 break;
2028
2029 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002030 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002031 break;
2032
2033 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002034 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002035
Michal Vasko004d3152020-06-11 19:59:22 +02002036 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002037 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002038 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002039 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 +02002040 return LY_EVALID;
2041 }
Radek Krejci0f969882020-08-21 16:56:47 +02002042 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002043 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002044 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002045 goto reparse_predicate;
2046 break;
2047
2048 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002049 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002050
2051 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002052 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002053 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002054 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002055
2056 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002057 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002058 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002059 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002060
2061reparse_predicate:
2062 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002063 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002064 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002065 LY_CHECK_RET(rc);
2066 }
2067 break;
2068 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002069 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 +02002070 return LY_EVALID;
2071 }
Michal Vasko004d3152020-06-11 19:59:22 +02002072 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002073
2074 return LY_SUCCESS;
2075}
2076
2077/**
2078 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2079 *
2080 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2081 *
2082 * @param[in] ctx Context for logging.
2083 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002084 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002085 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002086 * @return LY_ERR
2087 */
2088static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002089reparse_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 +02002090{
2091 LY_ERR rc;
2092
Michal Vasko004d3152020-06-11 19:59:22 +02002093 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 +02002094
2095 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002096 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002097 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002098 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002099
Michal Vasko004d3152020-06-11 19:59:22 +02002100 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002101 return LY_SUCCESS;
2102 }
Michal Vasko004d3152020-06-11 19:59:22 +02002103 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002104 case LYXP_TOKEN_DOT:
2105 case LYXP_TOKEN_DDOT:
2106 case LYXP_TOKEN_AT:
2107 case LYXP_TOKEN_NAMETEST:
2108 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002109 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002110 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002111 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002112 default:
2113 break;
2114 }
2115
Michal Vasko03ff5a72019-09-11 13:49:33 +02002116 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002117 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002118 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002119
aPiecekbf968d92021-05-27 14:35:05 +02002120 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002121 LY_CHECK_RET(rc);
2122 }
2123
2124 return LY_SUCCESS;
2125}
2126
2127/**
2128 * @brief Reparse FunctionCall. Logs directly on error.
2129 *
2130 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2131 *
2132 * @param[in] ctx Context for logging.
2133 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002134 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002135 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002136 * @return LY_ERR
2137 */
2138static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002139reparse_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 +02002140{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002141 int8_t min_arg_count = -1;
2142 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002143 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002144 LY_ERR rc;
2145
Michal Vasko004d3152020-06-11 19:59:22 +02002146 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002147 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002148 func_tok_idx = *tok_idx;
2149 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002150 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002151 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002152 min_arg_count = 1;
2153 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002154 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002155 min_arg_count = 1;
2156 max_arg_count = 1;
2157 }
2158 break;
2159 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002160 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002161 min_arg_count = 1;
2162 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002163 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002164 min_arg_count = 0;
2165 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002166 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002167 min_arg_count = 0;
2168 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002169 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002170 min_arg_count = 0;
2171 max_arg_count = 0;
2172 }
2173 break;
2174 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002175 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002176 min_arg_count = 1;
2177 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002178 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002179 min_arg_count = 0;
2180 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002181 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002182 min_arg_count = 1;
2183 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002184 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002185 min_arg_count = 1;
2186 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002187 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002188 min_arg_count = 1;
2189 max_arg_count = 1;
2190 }
2191 break;
2192 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002193 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002194 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002195 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002196 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002197 min_arg_count = 0;
2198 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002199 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002200 min_arg_count = 0;
2201 max_arg_count = 1;
2202 }
2203 break;
2204 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002205 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002206 min_arg_count = 1;
2207 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002208 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002209 min_arg_count = 1;
2210 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002211 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002212 min_arg_count = 0;
2213 max_arg_count = 0;
2214 }
2215 break;
2216 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002217 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002218 min_arg_count = 2;
2219 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002220 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002221 min_arg_count = 0;
2222 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002223 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002224 min_arg_count = 2;
2225 max_arg_count = 2;
2226 }
2227 break;
2228 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002229 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002230 min_arg_count = 2;
2231 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002232 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002233 min_arg_count = 3;
2234 max_arg_count = 3;
2235 }
2236 break;
2237 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002238 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002239 min_arg_count = 0;
2240 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002241 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002242 min_arg_count = 1;
2243 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002244 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002245 min_arg_count = 2;
2246 max_arg_count = 2;
2247 }
2248 break;
2249 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002250 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002251 min_arg_count = 2;
2252 max_arg_count = 2;
2253 }
2254 break;
2255 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002256 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002257 min_arg_count = 2;
2258 max_arg_count = 2;
2259 }
2260 break;
2261 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002262 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002263 min_arg_count = 0;
2264 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002265 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002266 min_arg_count = 0;
2267 max_arg_count = 1;
2268 }
2269 break;
2270 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002271 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002272 min_arg_count = 0;
2273 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002274 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002275 min_arg_count = 2;
2276 max_arg_count = 2;
2277 }
2278 break;
2279 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002280 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002281 min_arg_count = 2;
2282 max_arg_count = 2;
2283 }
2284 break;
2285 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002286 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002287 min_arg_count = 2;
2288 max_arg_count = 2;
2289 }
2290 break;
2291 }
2292 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002293 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 +02002294 return LY_EINVAL;
2295 }
Michal Vasko004d3152020-06-11 19:59:22 +02002296 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002297
2298 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002299 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002300 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002301 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002302
2303 /* ( Expr ( ',' Expr )* )? */
2304 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002305 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002306 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002307 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002308 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002309 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002310 LY_CHECK_RET(rc);
2311 }
Michal Vasko004d3152020-06-11 19:59:22 +02002312 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2313 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002314
2315 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002316 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002317 LY_CHECK_RET(rc);
2318 }
2319
2320 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002321 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002322 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002323 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002324
Radek Krejci857189e2020-09-01 13:26:36 +02002325 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002326 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 +02002327 return LY_EVALID;
2328 }
2329
2330 return LY_SUCCESS;
2331}
2332
2333/**
2334 * @brief Reparse PathExpr. Logs directly on error.
2335 *
2336 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2337 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2338 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2339 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2340 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2341 *
2342 * @param[in] ctx Context for logging.
2343 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002344 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002345 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002346 * @return LY_ERR
2347 */
2348static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002349reparse_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 +02002350{
2351 LY_ERR rc;
2352
Michal Vasko004d3152020-06-11 19:59:22 +02002353 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002354 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002355 }
2356
Michal Vasko004d3152020-06-11 19:59:22 +02002357 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002358 case LYXP_TOKEN_PAR1:
2359 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002360 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002361
aPiecekbf968d92021-05-27 14:35:05 +02002362 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002363 LY_CHECK_RET(rc);
2364
Michal Vasko004d3152020-06-11 19:59:22 +02002365 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002366 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002367 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002368 goto predicate;
2369 break;
2370 case LYXP_TOKEN_DOT:
2371 case LYXP_TOKEN_DDOT:
2372 case LYXP_TOKEN_AT:
2373 case LYXP_TOKEN_NAMETEST:
2374 case LYXP_TOKEN_NODETYPE:
2375 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002376 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002377 LY_CHECK_RET(rc);
2378 break;
2379 case LYXP_TOKEN_FUNCNAME:
2380 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002381 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002382 LY_CHECK_RET(rc);
2383 goto predicate;
2384 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002385 case LYXP_TOKEN_OPER_PATH:
2386 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002387 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002388 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002389 LY_CHECK_RET(rc);
2390 break;
2391 case LYXP_TOKEN_LITERAL:
2392 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002393 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002394 goto predicate;
2395 break;
2396 case LYXP_TOKEN_NUMBER:
2397 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002398 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002399 goto predicate;
2400 break;
2401 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002402 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 +02002403 return LY_EVALID;
2404 }
2405
2406 return LY_SUCCESS;
2407
2408predicate:
2409 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002410 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002411 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002412 LY_CHECK_RET(rc);
2413 }
2414
2415 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002416 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002417
2418 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002419 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002420
aPiecekbf968d92021-05-27 14:35:05 +02002421 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002422 LY_CHECK_RET(rc);
2423 }
2424
2425 return LY_SUCCESS;
2426}
2427
2428/**
2429 * @brief Reparse UnaryExpr. Logs directly on error.
2430 *
2431 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2432 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2433 *
2434 * @param[in] ctx Context for logging.
2435 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002436 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002437 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002438 * @return LY_ERR
2439 */
2440static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002441reparse_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 +02002442{
2443 uint16_t prev_exp;
2444 LY_ERR rc;
2445
2446 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002447 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002448 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2449 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002450 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002451 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002452 }
2453
2454 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002455 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002456 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002457 LY_CHECK_RET(rc);
2458
2459 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002460 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002461 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002462 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002463
aPiecekbf968d92021-05-27 14:35:05 +02002464 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002465 LY_CHECK_RET(rc);
2466 }
2467
2468 return LY_SUCCESS;
2469}
2470
2471/**
2472 * @brief Reparse AdditiveExpr. Logs directly on error.
2473 *
2474 * [15] AdditiveExpr ::= MultiplicativeExpr
2475 * | AdditiveExpr '+' MultiplicativeExpr
2476 * | AdditiveExpr '-' MultiplicativeExpr
2477 * [16] MultiplicativeExpr ::= UnaryExpr
2478 * | MultiplicativeExpr '*' UnaryExpr
2479 * | MultiplicativeExpr 'div' UnaryExpr
2480 * | MultiplicativeExpr 'mod' UnaryExpr
2481 *
2482 * @param[in] ctx Context for logging.
2483 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002484 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002485 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002486 * @return LY_ERR
2487 */
2488static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002489reparse_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 +02002490{
2491 uint16_t prev_add_exp, prev_mul_exp;
2492 LY_ERR rc;
2493
Michal Vasko004d3152020-06-11 19:59:22 +02002494 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002495 goto reparse_multiplicative_expr;
2496
2497 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002498 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2499 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002500 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002501 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002502
2503reparse_multiplicative_expr:
2504 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002505 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002506 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002507 LY_CHECK_RET(rc);
2508
2509 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002510 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2511 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002512 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002513 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002514
aPiecekbf968d92021-05-27 14:35:05 +02002515 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002516 LY_CHECK_RET(rc);
2517 }
2518 }
2519
2520 return LY_SUCCESS;
2521}
2522
2523/**
2524 * @brief Reparse EqualityExpr. Logs directly on error.
2525 *
2526 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2527 * | EqualityExpr '!=' RelationalExpr
2528 * [14] RelationalExpr ::= AdditiveExpr
2529 * | RelationalExpr '<' AdditiveExpr
2530 * | RelationalExpr '>' AdditiveExpr
2531 * | RelationalExpr '<=' AdditiveExpr
2532 * | RelationalExpr '>=' AdditiveExpr
2533 *
2534 * @param[in] ctx Context for logging.
2535 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002536 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002537 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002538 * @return LY_ERR
2539 */
2540static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002541reparse_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 +02002542{
2543 uint16_t prev_eq_exp, prev_rel_exp;
2544 LY_ERR rc;
2545
Michal Vasko004d3152020-06-11 19:59:22 +02002546 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002547 goto reparse_additive_expr;
2548
2549 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002550 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002551 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002552 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002553
2554reparse_additive_expr:
2555 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002556 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002557 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002558 LY_CHECK_RET(rc);
2559
2560 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002561 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002562 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002563 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002564
aPiecekbf968d92021-05-27 14:35:05 +02002565 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002566 LY_CHECK_RET(rc);
2567 }
2568 }
2569
2570 return LY_SUCCESS;
2571}
2572
2573/**
2574 * @brief Reparse OrExpr. Logs directly on error.
2575 *
2576 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2577 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2578 *
2579 * @param[in] ctx Context for logging.
2580 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002581 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002582 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002583 * @return LY_ERR
2584 */
2585static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002586reparse_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 +02002587{
2588 uint16_t prev_or_exp, prev_and_exp;
2589 LY_ERR rc;
2590
aPiecekbf968d92021-05-27 14:35:05 +02002591 ++depth;
2592 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2593
Michal Vasko004d3152020-06-11 19:59:22 +02002594 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002595 goto reparse_equality_expr;
2596
2597 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002598 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 +02002599 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002600 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002601
2602reparse_equality_expr:
2603 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002604 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002605 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002606 LY_CHECK_RET(rc);
2607
2608 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002609 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 +02002610 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002611 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002612
aPiecekbf968d92021-05-27 14:35:05 +02002613 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002614 LY_CHECK_RET(rc);
2615 }
2616 }
2617
2618 return LY_SUCCESS;
2619}
Radek Krejcib1646a92018-11-02 16:08:26 +01002620
2621/**
2622 * @brief Parse NCName.
2623 *
2624 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002625 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002626 */
Radek Krejcid4270262019-01-07 15:07:25 +01002627static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002628parse_ncname(const char *ncname)
2629{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002630 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002631 size_t size;
2632 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002633
2634 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2635 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2636 return len;
2637 }
2638
2639 do {
2640 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002641 if (!*ncname) {
2642 break;
2643 }
Radek Krejcid4270262019-01-07 15:07:25 +01002644 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002645 } while (is_xmlqnamechar(uc) && (uc != ':'));
2646
2647 return len;
2648}
2649
2650/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002651 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002652 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002653 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002654 * @param[in] exp Expression to use.
2655 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002656 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002657 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002658 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002659 */
2660static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002661exp_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 +01002662{
2663 uint32_t prev;
2664
2665 if (exp->used == exp->size) {
2666 prev = exp->size;
2667 exp->size += LYXP_EXPR_SIZE_STEP;
2668 if (prev > exp->size) {
2669 LOGINT(ctx);
2670 return LY_EINT;
2671 }
2672
2673 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2674 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2675 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2676 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2677 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2678 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2679 }
2680
2681 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002682 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002683 exp->tok_len[exp->used] = tok_len;
2684 ++exp->used;
2685 return LY_SUCCESS;
2686}
2687
2688void
Michal Vasko14676352020-05-29 11:35:55 +02002689lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002690{
2691 uint16_t i;
2692
2693 if (!expr) {
2694 return;
2695 }
2696
2697 lydict_remove(ctx, expr->expr);
2698 free(expr->tokens);
2699 free(expr->tok_pos);
2700 free(expr->tok_len);
2701 if (expr->repeat) {
2702 for (i = 0; i < expr->used; ++i) {
2703 free(expr->repeat[i]);
2704 }
2705 }
2706 free(expr->repeat);
2707 free(expr);
2708}
2709
Radek Krejcif03a9e22020-09-18 20:09:31 +02002710LY_ERR
2711lyxp_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 +01002712{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002713 LY_ERR ret = LY_SUCCESS;
2714 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002715 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002716 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002717 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002718 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002719
Radek Krejcif03a9e22020-09-18 20:09:31 +02002720 assert(expr_p);
2721
2722 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002723 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002724 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002725 }
2726
2727 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002728 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002729 }
2730 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002731 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002732 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002733 }
2734
2735 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002736 expr = calloc(1, sizeof *expr);
2737 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2738 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2739 expr->used = 0;
2740 expr->size = LYXP_EXPR_SIZE_START;
2741 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2742 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002743
Radek Krejcif03a9e22020-09-18 20:09:31 +02002744 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2745 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002746
Radek Krejcif03a9e22020-09-18 20:09:31 +02002747 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2748 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002749
Michal Vasko004d3152020-06-11 19:59:22 +02002750 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002751 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002752
Radek Krejcif03a9e22020-09-18 20:09:31 +02002753 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002754 ++parsed;
2755 }
2756
2757 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002758 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002759
2760 /* '(' */
2761 tok_len = 1;
2762 tok_type = LYXP_TOKEN_PAR1;
2763
Radek Krejcif03a9e22020-09-18 20:09:31 +02002764 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002765 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002766 if (((expr->tok_len[expr->used - 1] == 4) &&
2767 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2768 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2769 ((expr->tok_len[expr->used - 1] == 7) &&
2770 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002771 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002772 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002773 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002774 }
2775 prev_function_check = 0;
2776 }
2777
Radek Krejcif03a9e22020-09-18 20:09:31 +02002778 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002779
2780 /* ')' */
2781 tok_len = 1;
2782 tok_type = LYXP_TOKEN_PAR2;
2783
Radek Krejcif03a9e22020-09-18 20:09:31 +02002784 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002785
2786 /* '[' */
2787 tok_len = 1;
2788 tok_type = LYXP_TOKEN_BRACK1;
2789
Radek Krejcif03a9e22020-09-18 20:09:31 +02002790 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002791
2792 /* ']' */
2793 tok_len = 1;
2794 tok_type = LYXP_TOKEN_BRACK2;
2795
Radek Krejcif03a9e22020-09-18 20:09:31 +02002796 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002797
2798 /* '..' */
2799 tok_len = 2;
2800 tok_type = LYXP_TOKEN_DDOT;
2801
Radek Krejcif03a9e22020-09-18 20:09:31 +02002802 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002803
2804 /* '.' */
2805 tok_len = 1;
2806 tok_type = LYXP_TOKEN_DOT;
2807
Radek Krejcif03a9e22020-09-18 20:09:31 +02002808 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002809
2810 /* '@' */
2811 tok_len = 1;
2812 tok_type = LYXP_TOKEN_AT;
2813
Radek Krejcif03a9e22020-09-18 20:09:31 +02002814 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002815
2816 /* ',' */
2817 tok_len = 1;
2818 tok_type = LYXP_TOKEN_COMMA;
2819
Radek Krejcif03a9e22020-09-18 20:09:31 +02002820 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002821
2822 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002823 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2824 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002825 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002826 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002827 ++tok_len;
2828 tok_type = LYXP_TOKEN_LITERAL;
2829
Radek Krejcif03a9e22020-09-18 20:09:31 +02002830 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002831
2832 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002833 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2834 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002835 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002836 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002837 ++tok_len;
2838 tok_type = LYXP_TOKEN_LITERAL;
2839
Radek Krejcif03a9e22020-09-18 20:09:31 +02002840 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002841
2842 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002843 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2844 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002845 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002846 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002847 }
2848 tok_type = LYXP_TOKEN_NUMBER;
2849
Radek Krejcif03a9e22020-09-18 20:09:31 +02002850 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002851
2852 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002853 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002854 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002855 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002856 } else {
2857 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002858 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002859 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002860
Radek Krejcif03a9e22020-09-18 20:09:31 +02002861 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002862
Michal Vasko3e48bf32020-06-01 08:39:07 +02002863 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002864 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002865 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2866
Radek Krejcif03a9e22020-09-18 20:09:31 +02002867 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002868
2869 /* Operator '<=', '>=' */
2870 tok_len = 2;
2871 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002872
Radek Krejcif03a9e22020-09-18 20:09:31 +02002873 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002874
2875 /* Operator '|' */
2876 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002877 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002878
Radek Krejcif03a9e22020-09-18 20:09:31 +02002879 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002880
2881 /* Operator '+', '-' */
2882 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002883 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002884
Radek Krejcif03a9e22020-09-18 20:09:31 +02002885 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002886
Michal Vasko3e48bf32020-06-01 08:39:07 +02002887 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002888 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002889 tok_type = LYXP_TOKEN_OPER_EQUAL;
2890
Radek Krejcif03a9e22020-09-18 20:09:31 +02002891 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002892
2893 /* Operator '<', '>' */
2894 tok_len = 1;
2895 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002896
Michal Vasko69730152020-10-09 16:30:07 +02002897 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2898 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2899 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2900 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2901 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2902 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2903 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2904 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2905 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2906 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2907 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2908 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002909
2910 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002911 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002912 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002913 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002914
Radek Krejcif03a9e22020-09-18 20:09:31 +02002915 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002916 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002917 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002918
Radek Krejcif03a9e22020-09-18 20:09:31 +02002919 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002920 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002921 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002922
Radek Krejcif03a9e22020-09-18 20:09:31 +02002923 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002924 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002925 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002926
2927 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002928 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 +02002929 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 +02002930 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002931 goto error;
2932 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02002933 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002934 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002935 goto error;
2936 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002937 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002938
2939 /* NameTest '*' */
2940 tok_len = 1;
2941 tok_type = LYXP_TOKEN_NAMETEST;
2942
2943 } else {
2944
2945 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002946 long int ncname_len = parse_ncname(&expr_str[parsed]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02002947 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
Michal Vasko774ce402021-04-14 15:35:06 +02002948 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002949 tok_len = ncname_len;
2950
Radek Krejcif03a9e22020-09-18 20:09:31 +02002951 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002952 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002953 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002954 ++tok_len;
2955 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002956 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02002957 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
Michal Vasko774ce402021-04-14 15:35:06 +02002958 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002959 tok_len += ncname_len;
2960 }
2961 /* remove old flag to prevent ambiguities */
2962 prev_function_check = 0;
2963 tok_type = LYXP_TOKEN_NAMETEST;
2964 } else {
2965 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2966 prev_function_check = 1;
2967 tok_type = LYXP_TOKEN_NAMETEST;
2968 }
2969 }
2970
2971 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002972 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002973 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002974 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002975 ++parsed;
2976 }
2977
Radek Krejcif03a9e22020-09-18 20:09:31 +02002978 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002979
Michal Vasko004d3152020-06-11 19:59:22 +02002980 if (reparse) {
2981 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002982 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2983 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002984
Michal Vasko004d3152020-06-11 19:59:22 +02002985 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02002986 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002987 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002988 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002989 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002990 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002991 goto error;
2992 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002993 }
2994
Radek Krejcif03a9e22020-09-18 20:09:31 +02002995 print_expr_struct_debug(expr);
2996 *expr_p = expr;
2997 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002998
2999error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02003000 lyxp_expr_free(ctx, expr);
3001 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01003002}
3003
Michal Vasko1734be92020-09-22 08:55:10 +02003004LY_ERR
3005lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003006{
Michal Vasko1734be92020-09-22 08:55:10 +02003007 LY_ERR ret = LY_SUCCESS;
3008 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003009 uint32_t i, j;
3010
Michal Vasko7f45cf22020-10-01 12:49:44 +02003011 if (!exp) {
3012 goto cleanup;
3013 }
3014
Michal Vasko004d3152020-06-11 19:59:22 +02003015 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003016 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003017
Michal Vasko08e9b112021-06-11 15:41:17 +02003018 if (exp->used) {
3019 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
3020 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3021 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003022
Michal Vasko08e9b112021-06-11 15:41:17 +02003023 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
3024 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3025 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003026
Michal Vasko08e9b112021-06-11 15:41:17 +02003027 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
3028 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3029 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003030
Michal Vasko08e9b112021-06-11 15:41:17 +02003031 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
3032 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3033 for (i = 0; i < exp->used; ++i) {
3034 if (!exp->repeat[i]) {
3035 dup->repeat[i] = NULL;
3036 } else {
3037 for (j = 0; exp->repeat[i][j]; ++j) {}
3038 /* the ending 0 as well */
3039 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003040
Michal Vasko08e9b112021-06-11 15:41:17 +02003041 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
3042 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3043 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3044 dup->repeat[i][j - 1] = 0;
3045 }
Michal Vasko004d3152020-06-11 19:59:22 +02003046 }
3047 }
3048
3049 dup->used = exp->used;
3050 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003051 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003052
Michal Vasko1734be92020-09-22 08:55:10 +02003053cleanup:
3054 if (ret) {
3055 lyxp_expr_free(ctx, dup);
3056 } else {
3057 *dup_p = dup;
3058 }
3059 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003060}
3061
Michal Vasko03ff5a72019-09-11 13:49:33 +02003062/*
3063 * warn functions
3064 *
3065 * Warn functions check specific reasonable conditions for schema XPath
3066 * and print a warning if they are not satisfied.
3067 */
3068
3069/**
3070 * @brief Get the last-added schema node that is currently in the context.
3071 *
3072 * @param[in] set Set to search in.
3073 * @return Last-added schema context node, NULL if no node is in context.
3074 */
3075static struct lysc_node *
3076warn_get_scnode_in_ctx(struct lyxp_set *set)
3077{
3078 uint32_t i;
3079
3080 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3081 return NULL;
3082 }
3083
3084 i = set->used;
3085 do {
3086 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003087 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003088 /* if there are more, simply return the first found (last added) */
3089 return set->val.scnodes[i].scnode;
3090 }
3091 } while (i);
3092
3093 return NULL;
3094}
3095
3096/**
3097 * @brief Test whether a type is numeric - integer type or decimal64.
3098 *
3099 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003100 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003101 */
Radek Krejci857189e2020-09-01 13:26:36 +02003102static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003103warn_is_numeric_type(struct lysc_type *type)
3104{
3105 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003106 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003107 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003108
3109 switch (type->basetype) {
3110 case LY_TYPE_DEC64:
3111 case LY_TYPE_INT8:
3112 case LY_TYPE_UINT8:
3113 case LY_TYPE_INT16:
3114 case LY_TYPE_UINT16:
3115 case LY_TYPE_INT32:
3116 case LY_TYPE_UINT32:
3117 case LY_TYPE_INT64:
3118 case LY_TYPE_UINT64:
3119 return 1;
3120 case LY_TYPE_UNION:
3121 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003122 LY_ARRAY_FOR(uni->types, u) {
3123 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003124 if (ret) {
3125 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003126 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003127 }
3128 }
3129 /* did not find any suitable type */
3130 return 0;
3131 case LY_TYPE_LEAFREF:
3132 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3133 default:
3134 return 0;
3135 }
3136}
3137
3138/**
3139 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3140 *
3141 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003142 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003143 */
Radek Krejci857189e2020-09-01 13:26:36 +02003144static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003145warn_is_string_type(struct lysc_type *type)
3146{
3147 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003148 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003149 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003150
3151 switch (type->basetype) {
3152 case LY_TYPE_BITS:
3153 case LY_TYPE_ENUM:
3154 case LY_TYPE_IDENT:
3155 case LY_TYPE_INST:
3156 case LY_TYPE_STRING:
3157 return 1;
3158 case LY_TYPE_UNION:
3159 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003160 LY_ARRAY_FOR(uni->types, u) {
3161 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003162 if (ret) {
3163 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003164 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003165 }
3166 }
3167 /* did not find any suitable type */
3168 return 0;
3169 case LY_TYPE_LEAFREF:
3170 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3171 default:
3172 return 0;
3173 }
3174}
3175
3176/**
3177 * @brief Test whether a type is one specific type.
3178 *
3179 * @param[in] type Type to test.
3180 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003181 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003182 */
Radek Krejci857189e2020-09-01 13:26:36 +02003183static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003184warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3185{
3186 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003187 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003188 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003189
3190 if (type->basetype == base) {
3191 return 1;
3192 } else if (type->basetype == LY_TYPE_UNION) {
3193 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003194 LY_ARRAY_FOR(uni->types, u) {
3195 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003196 if (ret) {
3197 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003198 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003199 }
3200 }
3201 /* did not find any suitable type */
3202 return 0;
3203 } else if (type->basetype == LY_TYPE_LEAFREF) {
3204 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3205 }
3206
3207 return 0;
3208}
3209
3210/**
3211 * @brief Get next type of a (union) type.
3212 *
3213 * @param[in] type Base type.
3214 * @param[in] prev_type Previously returned type.
3215 * @return Next type or NULL.
3216 */
3217static struct lysc_type *
3218warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3219{
3220 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003221 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003222 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003223
3224 switch (type->basetype) {
3225 case LY_TYPE_UNION:
3226 uni = (struct lysc_type_union *)type;
3227 if (!prev_type) {
3228 return uni->types[0];
3229 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003230 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003231 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003232 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003233 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003234 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003235 found = 1;
3236 }
3237 }
3238 return NULL;
3239 default:
3240 if (prev_type) {
3241 assert(type == prev_type);
3242 return NULL;
3243 } else {
3244 return type;
3245 }
3246 }
3247}
3248
3249/**
3250 * @brief Test whether 2 types have a common type.
3251 *
3252 * @param[in] type1 First type.
3253 * @param[in] type2 Second type.
3254 * @return 1 if they do, 0 otherwise.
3255 */
3256static int
3257warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3258{
3259 struct lysc_type *t1, *rt1, *t2, *rt2;
3260
3261 t1 = NULL;
3262 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3263 if (t1->basetype == LY_TYPE_LEAFREF) {
3264 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3265 } else {
3266 rt1 = t1;
3267 }
3268
3269 t2 = NULL;
3270 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3271 if (t2->basetype == LY_TYPE_LEAFREF) {
3272 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3273 } else {
3274 rt2 = t2;
3275 }
3276
3277 if (rt2->basetype == rt1->basetype) {
3278 /* match found */
3279 return 1;
3280 }
3281 }
3282 }
3283
3284 return 0;
3285}
3286
3287/**
3288 * @brief Check both operands of comparison operators.
3289 *
3290 * @param[in] ctx Context for errors.
3291 * @param[in] set1 First operand set.
3292 * @param[in] set2 Second operand set.
3293 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3294 * @param[in] expr Start of the expression to print with the warning.
3295 * @param[in] tok_pos Token position.
3296 */
3297static void
Radek Krejci857189e2020-09-01 13:26:36 +02003298warn_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 +02003299{
3300 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003301 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003302
3303 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3304 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3305
3306 if (!node1 && !node2) {
3307 /* no node-sets involved, nothing to do */
3308 return;
3309 }
3310
3311 if (node1) {
3312 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3313 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3314 warning = 1;
3315 leaves = 0;
3316 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3317 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3318 warning = 1;
3319 }
3320 }
3321
3322 if (node2) {
3323 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3324 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3325 warning = 1;
3326 leaves = 0;
3327 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3328 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3329 warning = 1;
3330 }
3331 }
3332
3333 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003334 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3335 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3336 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3337 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003338 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3339 warning = 1;
3340 }
3341 }
3342
3343 if (warning) {
3344 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3345 }
3346}
3347
3348/**
3349 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3350 *
3351 * @param[in] exp Parsed XPath expression.
3352 * @param[in] set Set with the leaf/leaf-list.
3353 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3354 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3355 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3356 */
3357static void
Michal Vasko40308e72020-10-20 16:38:40 +02003358warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3359 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003360{
3361 struct lysc_node *scnode;
3362 struct lysc_type *type;
3363 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003364 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003365 LY_ERR rc;
3366 struct ly_err_item *err = NULL;
3367
Michal Vasko69730152020-10-09 16:30:07 +02003368 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3369 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003370 /* check that the node can have the specified value */
3371 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3372 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3373 } else {
3374 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3375 }
3376 if (!value) {
3377 LOGMEM(set->ctx);
3378 return;
3379 }
3380
3381 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3382 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003383 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003384 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003385 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003386 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003387 }
3388
3389 type = ((struct lysc_node_leaf *)scnode)->type;
3390 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003391 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003392 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003393 if (rc == LY_EINCOMPLETE) {
3394 rc = LY_SUCCESS;
3395 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003396
3397 if (err) {
3398 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3399 ly_err_free(err);
3400 } else if (rc != LY_SUCCESS) {
3401 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3402 }
3403 if (rc != LY_SUCCESS) {
3404 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003405 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003406 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003407 } else {
3408 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003409 }
3410 }
3411 free(value);
3412 }
3413}
3414
3415/*
3416 * XPath functions
3417 */
3418
3419/**
3420 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3421 * depending on whether the first node bit value from the second argument is set.
3422 *
3423 * @param[in] args Array of arguments.
3424 * @param[in] arg_count Count of elements in @p args.
3425 * @param[in,out] set Context and result set at the same time.
3426 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003427 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003428 */
3429static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003430xpath_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 +02003431{
3432 struct lyd_node_term *leaf;
3433 struct lysc_node_leaf *sleaf;
Michal Vasko2588b952021-07-29 07:43:26 +02003434 struct lyd_value_bits *bits;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003435 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003436 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003437
3438 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003439 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003440 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003441 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3442 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3443 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3444 sleaf->name);
3445 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3446 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3447 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003448 }
3449
3450 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3451 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003452 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3453 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003454 } else if (!warn_is_string_type(sleaf->type)) {
3455 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003456 }
3457 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003458 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003459 return rc;
3460 }
3461
Michal Vaskod3678892020-05-21 10:06:58 +02003462 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003463 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 +02003464 return LY_EVALID;
3465 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003466 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003467 LY_CHECK_RET(rc);
3468
3469 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003470 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003471 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko2588b952021-07-29 07:43:26 +02003472 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) {
3473 LYD_VALUE_GET(&leaf->value, bits);
3474 LY_ARRAY_FOR(bits->items, u) {
3475 if (!strcmp(bits->items[u]->name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003476 set_fill_boolean(set, 1);
3477 break;
3478 }
3479 }
3480 }
3481 }
3482
3483 return LY_SUCCESS;
3484}
3485
3486/**
3487 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3488 * with the argument converted to boolean.
3489 *
3490 * @param[in] args Array of arguments.
3491 * @param[in] arg_count Count of elements in @p args.
3492 * @param[in,out] set Context and result set at the same time.
3493 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003494 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003495 */
3496static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003497xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003498{
3499 LY_ERR rc;
3500
3501 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003502 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003503 return LY_SUCCESS;
3504 }
3505
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003506 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003507 LY_CHECK_RET(rc);
3508 set_fill_set(set, args[0]);
3509
3510 return LY_SUCCESS;
3511}
3512
3513/**
3514 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3515 * with the first argument rounded up to the nearest integer.
3516 *
3517 * @param[in] args Array of arguments.
3518 * @param[in] arg_count Count of elements in @p args.
3519 * @param[in,out] set Context and result set at the same time.
3520 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003521 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003522 */
3523static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003524xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003525{
3526 struct lysc_node_leaf *sleaf;
3527 LY_ERR rc = LY_SUCCESS;
3528
3529 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003530 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003531 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003532 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3533 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3534 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3535 sleaf->name);
3536 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3537 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3538 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003539 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003540 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003541 return rc;
3542 }
3543
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003544 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003545 LY_CHECK_RET(rc);
3546 if ((long long)args[0]->val.num != args[0]->val.num) {
3547 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3548 } else {
3549 set_fill_number(set, args[0]->val.num);
3550 }
3551
3552 return LY_SUCCESS;
3553}
3554
3555/**
3556 * @brief Execute the XPath concat(string, string, string*) function.
3557 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3558 *
3559 * @param[in] args Array of arguments.
3560 * @param[in] arg_count Count of elements in @p args.
3561 * @param[in,out] set Context and result set at the same time.
3562 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003563 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003564 */
3565static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003566xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003567{
3568 uint16_t i;
3569 char *str = NULL;
3570 size_t used = 1;
3571 LY_ERR rc = LY_SUCCESS;
3572 struct lysc_node_leaf *sleaf;
3573
3574 if (options & LYXP_SCNODE_ALL) {
3575 for (i = 0; i < arg_count; ++i) {
3576 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3577 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3578 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003579 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003580 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003581 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 +02003582 }
3583 }
3584 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003585 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003586 return rc;
3587 }
3588
3589 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003590 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003591 if (rc != LY_SUCCESS) {
3592 free(str);
3593 return rc;
3594 }
3595
3596 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3597 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3598 strcpy(str + used - 1, args[i]->val.str);
3599 used += strlen(args[i]->val.str);
3600 }
3601
3602 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003603 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003604 set->type = LYXP_SET_STRING;
3605 set->val.str = str;
3606
3607 return LY_SUCCESS;
3608}
3609
3610/**
3611 * @brief Execute the XPath contains(string, string) function.
3612 * Returns LYXP_SET_BOOLEAN whether the second argument can
3613 * be found in the first or not.
3614 *
3615 * @param[in] args Array of arguments.
3616 * @param[in] arg_count Count of elements in @p args.
3617 * @param[in,out] set Context and result set at the same time.
3618 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003619 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003620 */
3621static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003622xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003623{
3624 struct lysc_node_leaf *sleaf;
3625 LY_ERR rc = LY_SUCCESS;
3626
3627 if (options & LYXP_SCNODE_ALL) {
3628 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3629 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003630 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3631 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003632 } else if (!warn_is_string_type(sleaf->type)) {
3633 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003634 }
3635 }
3636
3637 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3638 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003639 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3640 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003641 } else if (!warn_is_string_type(sleaf->type)) {
3642 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003643 }
3644 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003645 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003646 return rc;
3647 }
3648
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003649 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003650 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003651 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003652 LY_CHECK_RET(rc);
3653
3654 if (strstr(args[0]->val.str, args[1]->val.str)) {
3655 set_fill_boolean(set, 1);
3656 } else {
3657 set_fill_boolean(set, 0);
3658 }
3659
3660 return LY_SUCCESS;
3661}
3662
3663/**
3664 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3665 * with the size of the node-set from the argument.
3666 *
3667 * @param[in] args Array of arguments.
3668 * @param[in] arg_count Count of elements in @p args.
3669 * @param[in,out] set Context and result set at the same time.
3670 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003671 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003672 */
3673static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003674xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003675{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003676 LY_ERR rc = LY_SUCCESS;
3677
3678 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003679 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003680 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003681 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003682 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003683 return rc;
3684 }
3685
Michal Vasko03ff5a72019-09-11 13:49:33 +02003686 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003687 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003688 return LY_EVALID;
3689 }
3690
3691 set_fill_number(set, args[0]->used);
3692 return LY_SUCCESS;
3693}
3694
3695/**
3696 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3697 * with the context with the intial node.
3698 *
3699 * @param[in] args Array of arguments.
3700 * @param[in] arg_count Count of elements in @p args.
3701 * @param[in,out] set Context and result set at the same time.
3702 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003703 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003704 */
3705static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003706xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003707{
3708 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003709 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003710 return LY_EVALID;
3711 }
3712
3713 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003714 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003715
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003716 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003717 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003718 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003719
3720 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003721 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003722 }
3723
3724 return LY_SUCCESS;
3725}
3726
3727/**
3728 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3729 * leafref or instance-identifier target node(s).
3730 *
3731 * @param[in] args Array of arguments.
3732 * @param[in] arg_count Count of elements in @p args.
3733 * @param[in,out] set Context and result set at the same time.
3734 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003735 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003736 */
3737static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003738xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003739{
3740 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003741 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003742 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003743 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003744 struct ly_path *p;
3745 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003746 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003747 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02003748 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003749
3750 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003751 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003752 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003753 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko423ba3f2021-07-19 13:08:50 +02003754 if (!(sleaf->nodetype & LYD_NODE_TERM)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003755 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3756 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02003757 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
3758 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003759 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3760 __func__, sleaf->name);
3761 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003762 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003763 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko423ba3f2021-07-19 13:08:50 +02003764 if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003765 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003766 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003767
3768 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02003769 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02003770 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02003771 if (!r) {
3772 /* get the target node */
3773 target = p[LY_ARRAY_COUNT(p) - 1].node;
3774 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02003775
Michal Vasko741bb562021-06-24 11:59:50 +02003776 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
3777 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003778 }
3779
Michal Vasko741bb562021-06-24 11:59:50 +02003780 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003781 }
3782
Michal Vaskod3678892020-05-21 10:06:58 +02003783 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003784 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003785 return LY_EVALID;
3786 }
3787
Michal Vaskod3678892020-05-21 10:06:58 +02003788 lyxp_set_free_content(set);
3789 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003790 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3791 sleaf = (struct lysc_node_leaf *)leaf->schema;
3792 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3793 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3794 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02003795 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01003796 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003797 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003798 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003799 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003800 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003801 } else {
3802 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003803 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003804 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003805 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003806 return LY_EVALID;
3807 }
3808 }
Michal Vasko004d3152020-06-11 19:59:22 +02003809
3810 /* insert it */
3811 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003812 }
3813 }
3814
3815 return LY_SUCCESS;
3816}
3817
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003818static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003819xpath_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 +02003820{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01003821 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003822 LY_ARRAY_COUNT_TYPE u;
3823 struct lyd_node_term *leaf;
3824 struct lysc_node_leaf *sleaf;
3825 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02003826 struct lyd_value *val;
3827 const struct lys_module *mod;
3828 const char *id_name;
3829 uint16_t id_len;
3830 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003831 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003832 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003833
3834 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003835 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003836 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003837 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3838 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3839 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3840 sleaf->name);
3841 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3842 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3843 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003844 }
3845
3846 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3847 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3848 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003849 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003850 } else if (!warn_is_string_type(sleaf->type)) {
3851 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3852 }
3853 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003854 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003855 return rc;
3856 }
3857
3858 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003859 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 +02003860 return LY_EVALID;
3861 }
3862 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3863 LY_CHECK_RET(rc);
3864
Michal Vasko93923692021-05-07 15:28:02 +02003865 /* parse the identity */
3866 id_name = args[1]->val.str;
3867 id_len = strlen(id_name);
3868 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
3869 LY_CHECK_RET(rc);
3870 if (!mod) {
3871 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
3872 return LY_EVALID;
3873 }
3874
3875 /* find the identity */
3876 found = 0;
3877 LY_ARRAY_FOR(mod->identities, u) {
3878 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
3879 /* we have match */
3880 found = 1;
3881 break;
3882 }
3883 }
3884 if (!found) {
3885 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
3886 return LY_EVALID;
3887 }
3888 id = &mod->identities[u];
3889
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003890 set_fill_boolean(set, 0);
3891 found = 0;
3892 for (i = 0; i < args[0]->used; ++i) {
3893 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3894 continue;
3895 }
3896
3897 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3898 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3899 sleaf = (struct lysc_node_leaf *)leaf->schema;
3900 val = &leaf->value;
3901 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3902 /* uninteresting */
3903 continue;
3904 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003905 } else {
3906 meta = args[0]->val.meta[i].meta;
3907 val = &meta->value;
3908 if (val->realtype->basetype != LY_TYPE_IDENT) {
3909 /* uninteresting */
3910 continue;
3911 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003912 }
3913
Michal Vasko93923692021-05-07 15:28:02 +02003914 /* check the identity itself */
3915 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003916 set_fill_boolean(set, 1);
3917 found = 1;
3918 }
Michal Vasko93923692021-05-07 15:28:02 +02003919 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
3920 set_fill_boolean(set, 1);
3921 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003922 }
3923
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003924 if (found) {
3925 break;
3926 }
3927 }
3928
3929 return LY_SUCCESS;
3930}
3931
Michal Vasko03ff5a72019-09-11 13:49:33 +02003932/**
3933 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3934 * on whether the first argument nodes contain a node of an identity derived from the second
3935 * argument identity.
3936 *
3937 * @param[in] args Array of arguments.
3938 * @param[in] arg_count Count of elements in @p args.
3939 * @param[in,out] set Context and result set at the same time.
3940 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003941 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003942 */
3943static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003944xpath_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 +02003945{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003946 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003947}
3948
3949/**
3950 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3951 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3952 * the second argument identity.
3953 *
3954 * @param[in] args Array of arguments.
3955 * @param[in] arg_count Count of elements in @p args.
3956 * @param[in,out] set Context and result set at the same time.
3957 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003958 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003959 */
3960static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003961xpath_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 +02003962{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003963 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003964}
3965
3966/**
3967 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3968 * with the integer value of the first node's enum value, otherwise NaN.
3969 *
3970 * @param[in] args Array of arguments.
3971 * @param[in] arg_count Count of elements in @p args.
3972 * @param[in,out] set Context and result set at the same time.
3973 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003974 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003975 */
3976static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003977xpath_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 +02003978{
3979 struct lyd_node_term *leaf;
3980 struct lysc_node_leaf *sleaf;
3981 LY_ERR rc = LY_SUCCESS;
3982
3983 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003984 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003985 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003986 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3987 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3988 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3989 sleaf->name);
3990 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3991 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
3992 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003993 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003994 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003995 return rc;
3996 }
3997
Michal Vaskod3678892020-05-21 10:06:58 +02003998 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003999 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004000 return LY_EVALID;
4001 }
4002
4003 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004004 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004005 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4006 sleaf = (struct lysc_node_leaf *)leaf->schema;
4007 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4008 set_fill_number(set, leaf->value.enum_item->value);
4009 }
4010 }
4011
4012 return LY_SUCCESS;
4013}
4014
4015/**
4016 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4017 * with false value.
4018 *
4019 * @param[in] args Array of arguments.
4020 * @param[in] arg_count Count of elements in @p args.
4021 * @param[in,out] set Context and result set at the same time.
4022 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004023 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004024 */
4025static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004026xpath_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 +02004027{
4028 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004029 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004030 return LY_SUCCESS;
4031 }
4032
4033 set_fill_boolean(set, 0);
4034 return LY_SUCCESS;
4035}
4036
4037/**
4038 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4039 * with the first argument floored (truncated).
4040 *
4041 * @param[in] args Array of arguments.
4042 * @param[in] arg_count Count of elements in @p args.
4043 * @param[in,out] set Context and result set at the same time.
4044 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004045 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004046 */
4047static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004048xpath_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 +02004049{
4050 LY_ERR rc;
4051
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004052 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004053 LY_CHECK_RET(rc);
4054 if (isfinite(args[0]->val.num)) {
4055 set_fill_number(set, (long long)args[0]->val.num);
4056 }
4057
4058 return LY_SUCCESS;
4059}
4060
4061/**
4062 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4063 * whether the language of the text matches the one from the argument.
4064 *
4065 * @param[in] args Array of arguments.
4066 * @param[in] arg_count Count of elements in @p args.
4067 * @param[in,out] set Context and result set at the same time.
4068 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004069 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004070 */
4071static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004072xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004073{
4074 const struct lyd_node *node;
4075 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004076 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004077 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004078 LY_ERR rc = LY_SUCCESS;
4079
4080 if (options & LYXP_SCNODE_ALL) {
4081 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4082 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004083 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4084 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004085 } else if (!warn_is_string_type(sleaf->type)) {
4086 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004087 }
4088 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004089 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004090 return rc;
4091 }
4092
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004093 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004094 LY_CHECK_RET(rc);
4095
Michal Vasko03ff5a72019-09-11 13:49:33 +02004096 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004097 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004098 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004099 } else if (!set->used) {
4100 set_fill_boolean(set, 0);
4101 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004102 }
4103
4104 switch (set->val.nodes[0].type) {
4105 case LYXP_NODE_ELEM:
4106 case LYXP_NODE_TEXT:
4107 node = set->val.nodes[0].node;
4108 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004109 case LYXP_NODE_META:
4110 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004111 break;
4112 default:
4113 /* nothing to do with roots */
4114 set_fill_boolean(set, 0);
4115 return LY_SUCCESS;
4116 }
4117
Michal Vasko9f96a052020-03-10 09:41:45 +01004118 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004119 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004120 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004121 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004122 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004123 break;
4124 }
4125 }
4126
Michal Vasko9f96a052020-03-10 09:41:45 +01004127 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004128 break;
4129 }
4130 }
4131
4132 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004133 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004134 set_fill_boolean(set, 0);
4135 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004136 uint64_t i;
4137
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004138 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004139 for (i = 0; args[0]->val.str[i]; ++i) {
4140 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4141 set_fill_boolean(set, 0);
4142 break;
4143 }
4144 }
4145 if (!args[0]->val.str[i]) {
4146 if (!val[i] || (val[i] == '-')) {
4147 set_fill_boolean(set, 1);
4148 } else {
4149 set_fill_boolean(set, 0);
4150 }
4151 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004152 }
4153
4154 return LY_SUCCESS;
4155}
4156
4157/**
4158 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4159 * with the context size.
4160 *
4161 * @param[in] args Array of arguments.
4162 * @param[in] arg_count Count of elements in @p args.
4163 * @param[in,out] set Context and result set at the same time.
4164 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004165 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004166 */
4167static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004168xpath_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 +02004169{
4170 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004171 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004172 return LY_SUCCESS;
4173 }
4174
Michal Vasko03ff5a72019-09-11 13:49:33 +02004175 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004176 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004177 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004178 } else if (!set->used) {
4179 set_fill_number(set, 0);
4180 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004181 }
4182
4183 set_fill_number(set, set->ctx_size);
4184 return LY_SUCCESS;
4185}
4186
4187/**
4188 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4189 * with the node name without namespace from the argument or the context.
4190 *
4191 * @param[in] args Array of arguments.
4192 * @param[in] arg_count Count of elements in @p args.
4193 * @param[in,out] set Context and result set at the same time.
4194 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004195 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004196 */
4197static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004198xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004199{
4200 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004201
Michal Vasko03ff5a72019-09-11 13:49:33 +02004202 /* suppress unused variable warning */
4203 (void)options;
4204
4205 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004206 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004207 return LY_SUCCESS;
4208 }
4209
4210 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004211 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004212 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004213 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004214 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004215 } else if (!args[0]->used) {
4216 set_fill_string(set, "", 0);
4217 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004218 }
4219
4220 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004221 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004222
4223 item = &args[0]->val.nodes[0];
4224 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004225 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004226 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004227 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004228 } else if (!set->used) {
4229 set_fill_string(set, "", 0);
4230 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004231 }
4232
4233 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004234 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004235
4236 item = &set->val.nodes[0];
4237 }
4238
4239 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004240 case LYXP_NODE_NONE:
4241 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004242 case LYXP_NODE_ROOT:
4243 case LYXP_NODE_ROOT_CONFIG:
4244 case LYXP_NODE_TEXT:
4245 set_fill_string(set, "", 0);
4246 break;
4247 case LYXP_NODE_ELEM:
4248 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4249 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004250 case LYXP_NODE_META:
4251 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004252 break;
4253 }
4254
4255 return LY_SUCCESS;
4256}
4257
4258/**
4259 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4260 * with the node name fully qualified (with namespace) from the argument or the context.
4261 *
4262 * @param[in] args Array of arguments.
4263 * @param[in] arg_count Count of elements in @p args.
4264 * @param[in,out] set Context and result set at the same time.
4265 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004266 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004267 */
4268static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004269xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004270{
4271 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004272 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004273 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004274 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004275
4276 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004277 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004278 return LY_SUCCESS;
4279 }
4280
4281 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004282 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004283 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004284 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004285 } else if (!args[0]->used) {
4286 set_fill_string(set, "", 0);
4287 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004288 }
4289
4290 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004291 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004292
4293 item = &args[0]->val.nodes[0];
4294 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004295 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004296 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004297 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004298 } else if (!set->used) {
4299 set_fill_string(set, "", 0);
4300 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004301 }
4302
4303 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004304 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004305
4306 item = &set->val.nodes[0];
4307 }
4308
4309 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004310 case LYXP_NODE_NONE:
4311 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004312 case LYXP_NODE_ROOT:
4313 case LYXP_NODE_ROOT_CONFIG:
4314 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004315 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004316 break;
4317 case LYXP_NODE_ELEM:
4318 mod = item->node->schema->module;
4319 name = item->node->schema->name;
4320 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004321 case LYXP_NODE_META:
4322 mod = ((struct lyd_meta *)item->node)->annotation->module;
4323 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004324 break;
4325 }
4326
4327 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004328 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004329 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4330 set_fill_string(set, str, strlen(str));
4331 free(str);
4332 } else {
4333 set_fill_string(set, "", 0);
4334 }
4335
4336 return LY_SUCCESS;
4337}
4338
4339/**
4340 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4341 * with the namespace of the node from the argument or the context.
4342 *
4343 * @param[in] args Array of arguments.
4344 * @param[in] arg_count Count of elements in @p args.
4345 * @param[in,out] set Context and result set at the same time.
4346 * @param[in] options XPath options.
4347 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4348 */
4349static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004350xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004351{
4352 struct lyxp_set_node *item;
4353 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004354
Michal Vasko03ff5a72019-09-11 13:49:33 +02004355 /* suppress unused variable warning */
4356 (void)options;
4357
4358 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004359 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004360 return LY_SUCCESS;
4361 }
4362
4363 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004364 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004365 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004366 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004367 return LY_EVALID;
4368 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004369 set_fill_string(set, "", 0);
4370 return LY_SUCCESS;
4371 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004372
4373 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004374 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004375
4376 item = &args[0]->val.nodes[0];
4377 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004378 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004379 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004380 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004381 } else if (!set->used) {
4382 set_fill_string(set, "", 0);
4383 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004384 }
4385
4386 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004387 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004388
4389 item = &set->val.nodes[0];
4390 }
4391
4392 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004393 case LYXP_NODE_NONE:
4394 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004395 case LYXP_NODE_ROOT:
4396 case LYXP_NODE_ROOT_CONFIG:
4397 case LYXP_NODE_TEXT:
4398 set_fill_string(set, "", 0);
4399 break;
4400 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004401 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004402 if (item->type == LYXP_NODE_ELEM) {
4403 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004404 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004405 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004406 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004407 }
4408
4409 set_fill_string(set, mod->ns, strlen(mod->ns));
4410 break;
4411 }
4412
4413 return LY_SUCCESS;
4414}
4415
4416/**
4417 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4418 * with only nodes from the context. In practice it either leaves the context
4419 * as it is or returns an empty node set.
4420 *
4421 * @param[in] args Array of arguments.
4422 * @param[in] arg_count Count of elements in @p args.
4423 * @param[in,out] set Context and result set at the same time.
4424 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004425 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004426 */
4427static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004428xpath_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 +02004429{
4430 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004431 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004432 return LY_SUCCESS;
4433 }
4434
4435 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004436 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004437 }
4438 return LY_SUCCESS;
4439}
4440
4441/**
4442 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4443 * with normalized value (no leading, trailing, double white spaces) of the node
4444 * from the argument or the context.
4445 *
4446 * @param[in] args Array of arguments.
4447 * @param[in] arg_count Count of elements in @p args.
4448 * @param[in,out] set Context and result set at the same time.
4449 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004450 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004451 */
4452static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004453xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004454{
4455 uint16_t i, new_used;
4456 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004457 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004458 struct lysc_node_leaf *sleaf;
4459 LY_ERR rc = LY_SUCCESS;
4460
4461 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004462 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4463 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004464 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004465 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4466 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004467 } else if (!warn_is_string_type(sleaf->type)) {
4468 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004469 }
4470 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004471 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004472 return rc;
4473 }
4474
4475 if (arg_count) {
4476 set_fill_set(set, args[0]);
4477 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004478 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004479 LY_CHECK_RET(rc);
4480
4481 /* is there any normalization necessary? */
4482 for (i = 0; set->val.str[i]; ++i) {
4483 if (is_xmlws(set->val.str[i])) {
4484 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4485 have_spaces = 1;
4486 break;
4487 }
4488 space_before = 1;
4489 } else {
4490 space_before = 0;
4491 }
4492 }
4493
4494 /* yep, there is */
4495 if (have_spaces) {
4496 /* it's enough, at least one character will go, makes space for ending '\0' */
4497 new = malloc(strlen(set->val.str) * sizeof(char));
4498 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4499 new_used = 0;
4500
4501 space_before = 0;
4502 for (i = 0; set->val.str[i]; ++i) {
4503 if (is_xmlws(set->val.str[i])) {
4504 if ((i == 0) || space_before) {
4505 space_before = 1;
4506 continue;
4507 } else {
4508 space_before = 1;
4509 }
4510 } else {
4511 space_before = 0;
4512 }
4513
4514 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4515 ++new_used;
4516 }
4517
4518 /* at worst there is one trailing space now */
4519 if (new_used && is_xmlws(new[new_used - 1])) {
4520 --new_used;
4521 }
4522
4523 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4524 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4525 new[new_used] = '\0';
4526
4527 free(set->val.str);
4528 set->val.str = new;
4529 }
4530
4531 return LY_SUCCESS;
4532}
4533
4534/**
4535 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4536 * with the argument converted to boolean and logically inverted.
4537 *
4538 * @param[in] args Array of arguments.
4539 * @param[in] arg_count Count of elements in @p args.
4540 * @param[in,out] set Context and result set at the same time.
4541 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004542 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004543 */
4544static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004545xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004546{
4547 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004548 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004549 return LY_SUCCESS;
4550 }
4551
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004552 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004553 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004554 set_fill_boolean(set, 0);
4555 } else {
4556 set_fill_boolean(set, 1);
4557 }
4558
4559 return LY_SUCCESS;
4560}
4561
4562/**
4563 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4564 * with the number representation of either the argument or the context.
4565 *
4566 * @param[in] args Array of arguments.
4567 * @param[in] arg_count Count of elements in @p args.
4568 * @param[in,out] set Context and result set at the same time.
4569 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004570 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004571 */
4572static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004573xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004574{
4575 LY_ERR rc;
4576
4577 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004578 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004579 return LY_SUCCESS;
4580 }
4581
4582 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004583 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004584 LY_CHECK_RET(rc);
4585 set_fill_set(set, args[0]);
4586 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004587 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004588 LY_CHECK_RET(rc);
4589 }
4590
4591 return LY_SUCCESS;
4592}
4593
4594/**
4595 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4596 * with the context position.
4597 *
4598 * @param[in] args Array of arguments.
4599 * @param[in] arg_count Count of elements in @p args.
4600 * @param[in,out] set Context and result set at the same time.
4601 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004602 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004603 */
4604static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004605xpath_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 +02004606{
4607 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004608 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004609 return LY_SUCCESS;
4610 }
4611
Michal Vasko03ff5a72019-09-11 13:49:33 +02004612 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004613 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004614 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004615 } else if (!set->used) {
4616 set_fill_number(set, 0);
4617 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004618 }
4619
4620 set_fill_number(set, set->ctx_pos);
4621
4622 /* UNUSED in 'Release' build type */
4623 (void)options;
4624 return LY_SUCCESS;
4625}
4626
4627/**
4628 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4629 * depending on whether the second argument regex matches the first argument string. For details refer to
4630 * YANG 1.1 RFC section 10.2.1.
4631 *
4632 * @param[in] args Array of arguments.
4633 * @param[in] arg_count Count of elements in @p args.
4634 * @param[in,out] set Context and result set at the same time.
4635 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004636 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004637 */
4638static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004639xpath_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 +02004640{
4641 struct lysc_pattern **patterns = NULL, **pattern;
4642 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004643 LY_ERR rc = LY_SUCCESS;
4644 struct ly_err_item *err;
4645
4646 if (options & LYXP_SCNODE_ALL) {
4647 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4648 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4649 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 +02004650 } else if (!warn_is_string_type(sleaf->type)) {
4651 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004652 }
4653 }
4654
4655 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4656 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4657 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 +02004658 } else if (!warn_is_string_type(sleaf->type)) {
4659 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004660 }
4661 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004662 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004663 return rc;
4664 }
4665
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004666 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004667 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004668 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004669 LY_CHECK_RET(rc);
4670
4671 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek IÅ¡a45802b52021-02-09 09:21:58 +01004672 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004673 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004674 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Radek Krejciddace2c2021-01-08 11:30:56 +01004675 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004676 if (rc != LY_SUCCESS) {
4677 LY_ARRAY_FREE(patterns);
4678 return rc;
4679 }
4680
Radek Krejci0b013302021-03-29 15:22:32 +02004681 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004682 pcre2_code_free((*pattern)->code);
4683 free(*pattern);
4684 LY_ARRAY_FREE(patterns);
4685 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004686 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004687 ly_err_free(err);
4688 return rc;
4689 }
4690
4691 if (rc == LY_EVALID) {
4692 ly_err_free(err);
4693 set_fill_boolean(set, 0);
4694 } else {
4695 set_fill_boolean(set, 1);
4696 }
4697
4698 return LY_SUCCESS;
4699}
4700
4701/**
4702 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4703 * with the rounded first argument. For details refer to
4704 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4705 *
4706 * @param[in] args Array of arguments.
4707 * @param[in] arg_count Count of elements in @p args.
4708 * @param[in,out] set Context and result set at the same time.
4709 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004710 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004711 */
4712static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004713xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004714{
4715 struct lysc_node_leaf *sleaf;
4716 LY_ERR rc = LY_SUCCESS;
4717
4718 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004719 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004720 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004721 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4722 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4723 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4724 sleaf->name);
4725 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4726 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4727 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004728 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004729 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004730 return rc;
4731 }
4732
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004733 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004734 LY_CHECK_RET(rc);
4735
4736 /* cover only the cases where floor can't be used */
4737 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4738 set_fill_number(set, -0.0f);
4739 } else {
4740 args[0]->val.num += 0.5;
4741 rc = xpath_floor(args, 1, args[0], options);
4742 LY_CHECK_RET(rc);
4743 set_fill_number(set, args[0]->val.num);
4744 }
4745
4746 return LY_SUCCESS;
4747}
4748
4749/**
4750 * @brief Execute the XPath starts-with(string, string) function.
4751 * Returns LYXP_SET_BOOLEAN whether the second argument is
4752 * the prefix of the first or not.
4753 *
4754 * @param[in] args Array of arguments.
4755 * @param[in] arg_count Count of elements in @p args.
4756 * @param[in,out] set Context and result set at the same time.
4757 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004758 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004759 */
4760static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004761xpath_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 +02004762{
4763 struct lysc_node_leaf *sleaf;
4764 LY_ERR rc = LY_SUCCESS;
4765
4766 if (options & LYXP_SCNODE_ALL) {
4767 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4768 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4769 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 +02004770 } else if (!warn_is_string_type(sleaf->type)) {
4771 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004772 }
4773 }
4774
4775 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4776 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4777 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 +02004778 } else if (!warn_is_string_type(sleaf->type)) {
4779 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004780 }
4781 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004782 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004783 return rc;
4784 }
4785
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004786 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004787 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004788 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004789 LY_CHECK_RET(rc);
4790
4791 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4792 set_fill_boolean(set, 0);
4793 } else {
4794 set_fill_boolean(set, 1);
4795 }
4796
4797 return LY_SUCCESS;
4798}
4799
4800/**
4801 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4802 * with the string representation of either the argument or the context.
4803 *
4804 * @param[in] args Array of arguments.
4805 * @param[in] arg_count Count of elements in @p args.
4806 * @param[in,out] set Context and result set at the same time.
4807 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004808 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004809 */
4810static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004811xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004812{
4813 LY_ERR rc;
4814
4815 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004816 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004817 return LY_SUCCESS;
4818 }
4819
4820 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004821 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004822 LY_CHECK_RET(rc);
4823 set_fill_set(set, args[0]);
4824 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004825 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004826 LY_CHECK_RET(rc);
4827 }
4828
4829 return LY_SUCCESS;
4830}
4831
4832/**
4833 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4834 * with the length of the string in either the argument or the context.
4835 *
4836 * @param[in] args Array of arguments.
4837 * @param[in] arg_count Count of elements in @p args.
4838 * @param[in,out] set Context and result set at the same time.
4839 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004840 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004841 */
4842static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004843xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004844{
4845 struct lysc_node_leaf *sleaf;
4846 LY_ERR rc = LY_SUCCESS;
4847
4848 if (options & LYXP_SCNODE_ALL) {
4849 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4850 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4851 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 +02004852 } else if (!warn_is_string_type(sleaf->type)) {
4853 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004854 }
4855 }
4856 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4857 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4858 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 +02004859 } else if (!warn_is_string_type(sleaf->type)) {
4860 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004861 }
4862 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004863 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004864 return rc;
4865 }
4866
4867 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004868 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004869 LY_CHECK_RET(rc);
4870 set_fill_number(set, strlen(args[0]->val.str));
4871 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004872 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004873 LY_CHECK_RET(rc);
4874 set_fill_number(set, strlen(set->val.str));
4875 }
4876
4877 return LY_SUCCESS;
4878}
4879
4880/**
4881 * @brief Execute the XPath substring(string, number, number?) function.
4882 * Returns LYXP_SET_STRING substring of the first argument starting
4883 * on the second argument index ending on the third argument index,
4884 * indexed from 1. For exact definition refer to
4885 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4886 *
4887 * @param[in] args Array of arguments.
4888 * @param[in] arg_count Count of elements in @p args.
4889 * @param[in,out] set Context and result set at the same time.
4890 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004891 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004892 */
4893static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004894xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004895{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004896 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004897 uint16_t str_start, str_len, pos;
4898 struct lysc_node_leaf *sleaf;
4899 LY_ERR rc = LY_SUCCESS;
4900
4901 if (options & LYXP_SCNODE_ALL) {
4902 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4903 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4904 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 +02004905 } else if (!warn_is_string_type(sleaf->type)) {
4906 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004907 }
4908 }
4909
4910 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4911 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4912 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 +02004913 } else if (!warn_is_numeric_type(sleaf->type)) {
4914 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004915 }
4916 }
4917
Michal Vasko69730152020-10-09 16:30:07 +02004918 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4919 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004920 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4921 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 +02004922 } else if (!warn_is_numeric_type(sleaf->type)) {
4923 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004924 }
4925 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004926 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004927 return rc;
4928 }
4929
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004930 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004931 LY_CHECK_RET(rc);
4932
4933 /* start */
4934 if (xpath_round(&args[1], 1, args[1], options)) {
4935 return -1;
4936 }
4937 if (isfinite(args[1]->val.num)) {
4938 start = args[1]->val.num - 1;
4939 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004940 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004941 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004942 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004943 }
4944
4945 /* len */
4946 if (arg_count == 3) {
4947 rc = xpath_round(&args[2], 1, args[2], options);
4948 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004949 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004950 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004951 } else if (isfinite(args[2]->val.num)) {
4952 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004953 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004954 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004955 }
4956 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004957 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004958 }
4959
4960 /* find matching character positions */
4961 str_start = 0;
4962 str_len = 0;
4963 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4964 if (pos < start) {
4965 ++str_start;
4966 } else if (pos < start + len) {
4967 ++str_len;
4968 } else {
4969 break;
4970 }
4971 }
4972
4973 set_fill_string(set, args[0]->val.str + str_start, str_len);
4974 return LY_SUCCESS;
4975}
4976
4977/**
4978 * @brief Execute the XPath substring-after(string, string) function.
4979 * Returns LYXP_SET_STRING with the string succeeding the occurance
4980 * of the second argument in the first or an empty string.
4981 *
4982 * @param[in] args Array of arguments.
4983 * @param[in] arg_count Count of elements in @p args.
4984 * @param[in,out] set Context and result set at the same time.
4985 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004986 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004987 */
4988static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004989xpath_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 +02004990{
4991 char *ptr;
4992 struct lysc_node_leaf *sleaf;
4993 LY_ERR rc = LY_SUCCESS;
4994
4995 if (options & LYXP_SCNODE_ALL) {
4996 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4997 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4998 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 +02004999 } else if (!warn_is_string_type(sleaf->type)) {
5000 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005001 }
5002 }
5003
5004 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5005 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5006 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 +02005007 } else if (!warn_is_string_type(sleaf->type)) {
5008 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005009 }
5010 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005011 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005012 return rc;
5013 }
5014
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005015 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005016 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005017 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005018 LY_CHECK_RET(rc);
5019
5020 ptr = strstr(args[0]->val.str, args[1]->val.str);
5021 if (ptr) {
5022 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5023 } else {
5024 set_fill_string(set, "", 0);
5025 }
5026
5027 return LY_SUCCESS;
5028}
5029
5030/**
5031 * @brief Execute the XPath substring-before(string, string) function.
5032 * Returns LYXP_SET_STRING with the string preceding the occurance
5033 * of the second argument in the first or an empty string.
5034 *
5035 * @param[in] args Array of arguments.
5036 * @param[in] arg_count Count of elements in @p args.
5037 * @param[in,out] set Context and result set at the same time.
5038 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005039 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005040 */
5041static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005042xpath_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 +02005043{
5044 char *ptr;
5045 struct lysc_node_leaf *sleaf;
5046 LY_ERR rc = LY_SUCCESS;
5047
5048 if (options & LYXP_SCNODE_ALL) {
5049 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5050 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5051 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 +02005052 } else if (!warn_is_string_type(sleaf->type)) {
5053 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005054 }
5055 }
5056
5057 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5058 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5059 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 +02005060 } else if (!warn_is_string_type(sleaf->type)) {
5061 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005062 }
5063 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005064 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005065 return rc;
5066 }
5067
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005068 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005069 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005070 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005071 LY_CHECK_RET(rc);
5072
5073 ptr = strstr(args[0]->val.str, args[1]->val.str);
5074 if (ptr) {
5075 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5076 } else {
5077 set_fill_string(set, "", 0);
5078 }
5079
5080 return LY_SUCCESS;
5081}
5082
5083/**
5084 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5085 * with the sum of all the nodes in the context.
5086 *
5087 * @param[in] args Array of arguments.
5088 * @param[in] arg_count Count of elements in @p args.
5089 * @param[in,out] set Context and result set at the same time.
5090 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005091 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005092 */
5093static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005094xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005095{
5096 long double num;
5097 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005098 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005099 struct lyxp_set set_item;
5100 struct lysc_node_leaf *sleaf;
5101 LY_ERR rc = LY_SUCCESS;
5102
5103 if (options & LYXP_SCNODE_ALL) {
5104 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5105 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005106 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005107 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5108 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5109 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005110 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005111 } else if (!warn_is_numeric_type(sleaf->type)) {
5112 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005113 }
5114 }
5115 }
5116 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005117 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005118 return rc;
5119 }
5120
5121 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005122
5123 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005124 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005125 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005126 } else if (!args[0]->used) {
5127 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005128 }
5129
Michal Vasko5c4e5892019-11-14 12:31:38 +01005130 set_init(&set_item, set);
5131
Michal Vasko03ff5a72019-09-11 13:49:33 +02005132 set_item.type = LYXP_SET_NODE_SET;
5133 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5134 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5135
5136 set_item.used = 1;
5137 set_item.size = 1;
5138
5139 for (i = 0; i < args[0]->used; ++i) {
5140 set_item.val.nodes[0] = args[0]->val.nodes[i];
5141
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005142 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005143 LY_CHECK_RET(rc);
5144 num = cast_string_to_number(str);
5145 free(str);
5146 set->val.num += num;
5147 }
5148
5149 free(set_item.val.nodes);
5150
5151 return LY_SUCCESS;
5152}
5153
5154/**
5155 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5156 * with the text content of the nodes in the context.
5157 *
5158 * @param[in] args Array of arguments.
5159 * @param[in] arg_count Count of elements in @p args.
5160 * @param[in,out] set Context and result set at the same time.
5161 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005162 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005163 */
5164static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005165xpath_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 +02005166{
5167 uint32_t i;
5168
5169 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005170 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005171 return LY_SUCCESS;
5172 }
5173
Michal Vasko03ff5a72019-09-11 13:49:33 +02005174 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005175 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005176 return LY_EVALID;
5177 }
5178
Michal Vaskod989ba02020-08-24 10:59:24 +02005179 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005180 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005181 case LYXP_NODE_NONE:
5182 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005183 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005184 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5185 set->val.nodes[i].type = LYXP_NODE_TEXT;
5186 ++i;
5187 break;
5188 }
Radek Krejci0f969882020-08-21 16:56:47 +02005189 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005190 case LYXP_NODE_ROOT:
5191 case LYXP_NODE_ROOT_CONFIG:
5192 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005193 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005194 set_remove_node(set, i);
5195 break;
5196 }
5197 }
5198
5199 return LY_SUCCESS;
5200}
5201
5202/**
5203 * @brief Execute the XPath translate(string, string, string) function.
5204 * Returns LYXP_SET_STRING with the first argument with the characters
5205 * from the second argument replaced by those on the corresponding
5206 * positions in the third argument.
5207 *
5208 * @param[in] args Array of arguments.
5209 * @param[in] arg_count Count of elements in @p args.
5210 * @param[in,out] set Context and result set at the same time.
5211 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005212 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005213 */
5214static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005215xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005216{
5217 uint16_t i, j, new_used;
5218 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005219 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005220 struct lysc_node_leaf *sleaf;
5221 LY_ERR rc = LY_SUCCESS;
5222
5223 if (options & LYXP_SCNODE_ALL) {
5224 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5225 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5226 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 +02005227 } else if (!warn_is_string_type(sleaf->type)) {
5228 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005229 }
5230 }
5231
5232 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5233 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5234 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 +02005235 } else if (!warn_is_string_type(sleaf->type)) {
5236 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005237 }
5238 }
5239
5240 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5241 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5242 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 +02005243 } else if (!warn_is_string_type(sleaf->type)) {
5244 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005245 }
5246 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005247 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005248 return rc;
5249 }
5250
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005251 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005252 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005253 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005254 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005255 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005256 LY_CHECK_RET(rc);
5257
5258 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5259 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5260 new_used = 0;
5261
5262 have_removed = 0;
5263 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005264 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005265
5266 for (j = 0; args[1]->val.str[j]; ++j) {
5267 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5268 /* removing this char */
5269 if (j >= strlen(args[2]->val.str)) {
5270 have_removed = 1;
5271 found = 1;
5272 break;
5273 }
5274 /* replacing this char */
5275 new[new_used] = args[2]->val.str[j];
5276 ++new_used;
5277 found = 1;
5278 break;
5279 }
5280 }
5281
5282 /* copying this char */
5283 if (!found) {
5284 new[new_used] = args[0]->val.str[i];
5285 ++new_used;
5286 }
5287 }
5288
5289 if (have_removed) {
5290 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5291 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5292 }
5293 new[new_used] = '\0';
5294
Michal Vaskod3678892020-05-21 10:06:58 +02005295 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005296 set->type = LYXP_SET_STRING;
5297 set->val.str = new;
5298
5299 return LY_SUCCESS;
5300}
5301
5302/**
5303 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5304 * with true value.
5305 *
5306 * @param[in] args Array of arguments.
5307 * @param[in] arg_count Count of elements in @p args.
5308 * @param[in,out] set Context and result set at the same time.
5309 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005310 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005311 */
5312static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005313xpath_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 +02005314{
5315 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005316 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005317 return LY_SUCCESS;
5318 }
5319
5320 set_fill_boolean(set, 1);
5321 return LY_SUCCESS;
5322}
5323
5324/*
5325 * moveto functions
5326 *
5327 * They and only they actually change the context (set).
5328 */
5329
5330/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005331 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005332 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005333 * XPath @p set is expected to be a (sc)node set!
5334 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005335 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5336 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005337 * @param[in] set Set with general XPath context.
5338 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005339 * @param[out] moveto_mod Expected module of a matching node.
5340 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005341 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005342static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005343moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5344 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005345{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005346 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005347 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005348 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005349
Michal Vasko2104e9f2020-03-06 08:23:25 +01005350 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5351
Michal Vasko6346ece2019-09-24 13:12:53 +02005352 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5353 /* specific module */
5354 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005355 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005356
Michal Vasko004d3152020-06-11 19:59:22 +02005357 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005358 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005359 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005360 return LY_EVALID;
5361 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005362
Michal Vasko6346ece2019-09-24 13:12:53 +02005363 *qname += pref_len + 1;
5364 *qname_len -= pref_len + 1;
5365 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5366 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005367 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005368 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005369 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005370 case LY_VALUE_SCHEMA:
5371 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005372 /* current module */
5373 mod = set->cur_mod;
5374 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005375 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005376 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005377 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005378 /* inherit parent (context node) module */
5379 if (ctx_scnode) {
5380 mod = ctx_scnode->module;
5381 } else {
5382 mod = NULL;
5383 }
5384 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005385 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005386 /* all nodes need to be prefixed */
5387 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5388 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005389 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005390 }
5391
Michal Vasko6346ece2019-09-24 13:12:53 +02005392 *moveto_mod = mod;
5393 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005394}
5395
5396/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005397 * @brief Move context @p set to the root. Handles absolute path.
5398 * Result is LYXP_SET_NODE_SET.
5399 *
5400 * @param[in,out] set Set to use.
5401 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005402 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005403 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005404static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005405moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005406{
aPiecek8b0cc152021-05-31 16:40:31 +02005407 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005408
5409 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005410 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskob0099a92020-08-31 14:55:23 +02005411 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005412 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005413 set->type = LYXP_SET_NODE_SET;
5414 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005415 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005416 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005417
5418 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005419}
5420
5421/**
5422 * @brief Check @p node as a part of NameTest processing.
5423 *
5424 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005425 * @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 Vaskodb08ce52021-10-06 08:57:49 +02005433moveto_node_check(const struct lyd_node *node, const struct lyxp_set *set, const char *node_name,
5434 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 Vasko03ff5a72019-09-11 13:49:33 +02005441 /* module check */
5442 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005443 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005444 }
5445
Michal Vasko5c4e5892019-11-14 12:31:38 +01005446 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005447 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005448 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005449 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5450 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005451 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005452 }
5453
5454 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005455 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005456 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005457 }
5458
Michal Vaskoa1424542019-11-14 16:08:52 +01005459 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005460 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005461 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005462 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005463
5464 /* match */
5465 return LY_SUCCESS;
5466}
5467
5468/**
5469 * @brief Check @p node as a part of schema NameTest processing.
5470 *
5471 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005472 * @param[in] ctx_scnode Context node.
5473 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005474 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005475 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005476 * @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 +02005477 */
5478static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005479moveto_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 +02005480 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005481{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005482 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5483 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005484 case LY_VALUE_SCHEMA:
5485 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005486 /* use current module */
5487 moveto_mod = set->cur_mod;
5488 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005489 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005490 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005491 /* inherit module of the context node, if any */
5492 if (ctx_scnode) {
5493 moveto_mod = ctx_scnode->module;
5494 }
5495 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005496 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005497 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005498 /* not defined */
5499 LOGINT(set->ctx);
5500 return LY_EINVAL;
5501 }
5502 }
5503
Michal Vasko03ff5a72019-09-11 13:49:33 +02005504 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005505 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005506 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005507 }
5508
5509 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005510 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005511 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005512 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005513 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005514 }
5515
5516 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005517 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005518 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005519 }
5520
5521 /* match */
5522 return LY_SUCCESS;
5523}
5524
5525/**
Michal Vaskod3678892020-05-21 10:06:58 +02005526 * @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 +02005527 *
5528 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005529 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005530 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005531 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005532 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5533 */
5534static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005535moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005536{
Michal Vaskof03ed032020-03-04 13:31:44 +01005537 uint32_t i;
Michal Vaskodb08ce52021-10-06 08:57:49 +02005538 const struct lyd_node *siblings, *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005539 LY_ERR rc;
5540
aPiecek8b0cc152021-05-31 16:40:31 +02005541 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005542 return LY_SUCCESS;
5543 }
5544
5545 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005546 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005547 return LY_EVALID;
5548 }
5549
Michal Vasko03ff5a72019-09-11 13:49:33 +02005550 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005551 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005552
5553 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 +01005554 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005555
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005556 /* search in all the trees */
Michal Vaskod3678892020-05-21 10:06:58 +02005557 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005558 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005559 /* search in children */
Michal Vaskodb08ce52021-10-06 08:57:49 +02005560 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005561 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005562
Michal Vaskod3678892020-05-21 10:06:58 +02005563 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskodb08ce52021-10-06 08:57:49 +02005564 rc = moveto_node_check(sub, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005565 if (rc == LY_SUCCESS) {
5566 if (!replaced) {
5567 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5568 replaced = 1;
5569 } else {
5570 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005571 }
Michal Vaskod3678892020-05-21 10:06:58 +02005572 ++i;
5573 } else if (rc == LY_EINCOMPLETE) {
5574 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005575 }
5576 }
5577
5578 if (!replaced) {
5579 /* no match */
5580 set_remove_node(set, i);
5581 }
5582 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005583
5584 return LY_SUCCESS;
5585}
5586
5587/**
Michal Vaskod3678892020-05-21 10:06:58 +02005588 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5589 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005590 *
5591 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005592 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005593 * @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 +01005594 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005595 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5596 */
5597static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005598moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5599 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005600{
Michal Vasko004d3152020-06-11 19:59:22 +02005601 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005602 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005603 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005604 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005605
Michal Vasko004d3152020-06-11 19:59:22 +02005606 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005607
aPiecek8b0cc152021-05-31 16:40:31 +02005608 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02005609 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005610 }
5611
5612 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005613 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005614 ret = LY_EVALID;
5615 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005616 }
5617
5618 /* context check for all the nodes since we have the schema node */
5619 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5620 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005621 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005622 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5623 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005624 lyxp_set_free_content(set);
5625 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005626 }
5627
5628 /* create specific data instance if needed */
5629 if (scnode->nodetype == LYS_LIST) {
5630 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5631 } else if (scnode->nodetype == LYS_LEAFLIST) {
5632 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005633 }
5634
5635 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005636 siblings = NULL;
5637
5638 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5639 assert(!set->val.nodes[i].node);
5640
5641 /* search in all the trees */
5642 siblings = set->tree;
5643 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5644 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005645 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005646 }
5647
5648 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005649 if (inst) {
5650 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005651 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005652 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005653 }
5654
5655 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005656 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005657 ret = LY_EINCOMPLETE;
5658 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005659 }
5660
5661 if (sub) {
5662 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005663 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005664 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005665 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005666 /* no match */
5667 set_remove_node(set, i);
5668 }
5669 }
5670
Michal Vasko004d3152020-06-11 19:59:22 +02005671cleanup:
5672 lyd_free_tree(inst);
5673 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005674}
5675
5676/**
5677 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5678 *
5679 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005680 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005681 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005682 * @param[in] options XPath options.
5683 * @return LY_ERR
5684 */
5685static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005686moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005687{
Radek Krejci857189e2020-09-01 13:26:36 +02005688 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005689 uint32_t getnext_opts;
5690 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005691 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005692 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005693 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005694
aPiecek8b0cc152021-05-31 16:40:31 +02005695 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005696 return LY_SUCCESS;
5697 }
5698
5699 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005700 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005701 return LY_EVALID;
5702 }
5703
Michal Vaskocafad9d2019-11-07 15:20:03 +01005704 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005705 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005706 if (options & LYXP_SCNODE_OUTPUT) {
5707 getnext_opts |= LYS_GETNEXT_OUTPUT;
5708 }
5709
Michal Vasko03ff5a72019-09-11 13:49:33 +02005710 orig_used = set->used;
5711 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005712 uint32_t idx;
5713
Radek Krejcif13b87b2020-12-01 22:02:17 +01005714 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5715 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005716 continue;
5717 }
5718
5719 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005720 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005721 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005722 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005723 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005724
5725 start_parent = set->val.scnodes[i].scnode;
5726
5727 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 +02005728 /* 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 +02005729 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005730 mod_idx = 0;
Michal Vaskoa51ef072021-07-02 10:40:30 +02005731 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005732 iter = NULL;
Michal Vasko919954a2021-07-26 09:21:42 +02005733 /* module may not be implemented or not compiled yet */
5734 while (mod->compiled && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005735 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005736 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5737
Michal Vasko03ff5a72019-09-11 13:49:33 +02005738 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005739 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005740 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005741 temp_ctx = 1;
5742 }
5743 }
5744 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005745 }
5746
Michal Vasko519fd602020-05-26 12:17:39 +02005747 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5748 iter = NULL;
5749 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005750 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005751 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5752
5753 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005754 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005755 temp_ctx = 1;
5756 }
5757 }
5758 }
5759 }
5760 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005761
5762 /* correct temporary in_ctx values */
5763 if (temp_ctx) {
5764 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005765 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5766 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005767 }
5768 }
5769 }
5770
5771 return LY_SUCCESS;
5772}
5773
5774/**
Michal Vaskod3678892020-05-21 10:06:58 +02005775 * @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 +02005776 * Context position aware.
5777 *
5778 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005779 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005780 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005781 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005782 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5783 */
5784static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005785moveto_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 +02005786{
5787 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005788 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005789 struct lyxp_set ret_set;
5790 LY_ERR rc;
5791
aPiecek8b0cc152021-05-31 16:40:31 +02005792 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005793 return LY_SUCCESS;
5794 }
5795
5796 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005797 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005798 return LY_EVALID;
5799 }
5800
Michal Vasko9f96a052020-03-10 09:41:45 +01005801 /* 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 +01005802 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005803 LY_CHECK_RET(rc);
5804
Michal Vasko6346ece2019-09-24 13:12:53 +02005805 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005806 set_init(&ret_set, set);
5807 for (i = 0; i < set->used; ++i) {
5808
5809 /* TREE DFS */
5810 start = set->val.nodes[i].node;
5811 for (elem = next = start; elem; elem = next) {
Michal Vaskodb08ce52021-10-06 08:57:49 +02005812 rc = moveto_node_check(elem, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005813 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005814 /* add matching node into result set */
5815 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5816 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5817 /* the node is a duplicate, we'll process it later in the set */
5818 goto skip_children;
5819 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005820 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005821 return rc;
5822 } else if (rc == LY_EINVAL) {
5823 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005824 }
5825
5826 /* TREE DFS NEXT ELEM */
5827 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005828 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005829 if (!next) {
5830skip_children:
5831 /* no children, so try siblings, but only if it's not the start,
5832 * that is considered to be the root and it's siblings are not traversed */
5833 if (elem != start) {
5834 next = elem->next;
5835 } else {
5836 break;
5837 }
5838 }
5839 while (!next) {
5840 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01005841 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005842 /* we are done, no next element to process */
5843 break;
5844 }
5845 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01005846 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005847 next = elem->next;
5848 }
5849 }
5850 }
5851
5852 /* make the temporary set the current one */
5853 ret_set.ctx_pos = set->ctx_pos;
5854 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005855 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005856 memcpy(set, &ret_set, sizeof *set);
5857
5858 return LY_SUCCESS;
5859}
5860
5861/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005862 * @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 +02005863 *
5864 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005865 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005866 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005867 * @param[in] options XPath options.
5868 * @return LY_ERR
5869 */
5870static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005871moveto_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 +02005872{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005873 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005874 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005875 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005876
aPiecek8b0cc152021-05-31 16:40:31 +02005877 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005878 return LY_SUCCESS;
5879 }
5880
5881 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005882 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005883 return LY_EVALID;
5884 }
5885
Michal Vasko03ff5a72019-09-11 13:49:33 +02005886 orig_used = set->used;
5887 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005888 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5889 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005890 continue;
5891 }
5892
5893 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005894 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005895 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005896 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005897 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005898
5899 /* TREE DFS */
5900 start = set->val.scnodes[i].scnode;
5901 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005902 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5903 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005904 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005905 }
5906
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005907 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005908 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005909 uint32_t idx;
5910
5911 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005912 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005913 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005914 /* we will process it later in the set */
5915 goto skip_children;
5916 }
5917 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005918 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005919 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005920 } else if (rc == LY_EINVAL) {
5921 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005922 }
5923
5924next_iter:
5925 /* TREE DFS NEXT ELEM */
5926 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005927 next = lysc_node_child(elem);
5928 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5929 next = next->next;
5930 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5931 next = next->next;
5932 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005933 if (!next) {
5934skip_children:
5935 /* no children, so try siblings, but only if it's not the start,
5936 * that is considered to be the root and it's siblings are not traversed */
5937 if (elem != start) {
5938 next = elem->next;
5939 } else {
5940 break;
5941 }
5942 }
5943 while (!next) {
5944 /* no siblings, go back through the parents */
5945 if (elem->parent == start) {
5946 /* we are done, no next element to process */
5947 break;
5948 }
5949 /* parent is already processed, go to its sibling */
5950 elem = elem->parent;
5951 next = elem->next;
5952 }
5953 }
5954 }
5955
5956 return LY_SUCCESS;
5957}
5958
5959/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005960 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005961 * Indirectly context position aware.
5962 *
5963 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005964 * @param[in] mod Matching metadata module, NULL for any.
5965 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02005966 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005967 * @return LY_ERR
5968 */
5969static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02005970moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005971{
Michal Vasko9f96a052020-03-10 09:41:45 +01005972 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005973
aPiecek8b0cc152021-05-31 16:40:31 +02005974 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005975 return LY_SUCCESS;
5976 }
5977
5978 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005979 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005980 return LY_EVALID;
5981 }
5982
Radek Krejci1deb5be2020-08-26 16:43:36 +02005983 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005984 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005985
5986 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5987 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005988 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005989 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005990
5991 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005992 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005993 continue;
5994 }
5995
Michal Vaskod3678892020-05-21 10:06:58 +02005996 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005997 /* match */
5998 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005999 set->val.meta[i].meta = sub;
6000 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006001 /* pos does not change */
6002 replaced = 1;
6003 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006004 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 +02006005 }
6006 ++i;
6007 }
6008 }
6009 }
6010
6011 if (!replaced) {
6012 /* no match */
6013 set_remove_node(set, i);
6014 }
6015 }
6016
6017 return LY_SUCCESS;
6018}
6019
6020/**
6021 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02006022 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006023 *
6024 * @param[in,out] set1 Set to use for the result.
6025 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006026 * @return LY_ERR
6027 */
6028static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006029moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006030{
6031 LY_ERR rc;
6032
Michal Vaskod3678892020-05-21 10:06:58 +02006033 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006034 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006035 return LY_EVALID;
6036 }
6037
6038 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02006039 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006040 return LY_SUCCESS;
6041 }
6042
Michal Vaskod3678892020-05-21 10:06:58 +02006043 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02006044 /* release hidden allocated data (lyxp_set.size) */
6045 lyxp_set_free_content(set1);
6046 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006047 memcpy(set1, set2, sizeof *set1);
6048 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02006049 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006050 return LY_SUCCESS;
6051 }
6052
6053 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006054 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006055
6056 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006057 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006058 LY_CHECK_RET(rc);
6059
6060 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006061 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006062
6063 return LY_SUCCESS;
6064}
6065
6066/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006067 * @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 +02006068 * Context position aware.
6069 *
6070 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006071 * @param[in] mod Matching metadata module, NULL for any.
6072 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006073 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006074 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6075 */
6076static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006077moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006078{
Michal Vasko9f96a052020-03-10 09:41:45 +01006079 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006080 struct lyxp_set *set_all_desc = NULL;
6081 LY_ERR rc;
6082
aPiecek8b0cc152021-05-31 16:40:31 +02006083 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006084 return LY_SUCCESS;
6085 }
6086
6087 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006088 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006089 return LY_EVALID;
6090 }
6091
Michal Vasko03ff5a72019-09-11 13:49:33 +02006092 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6093 * but it likely won't be used much, so it's a waste of time */
6094 /* copy the context */
6095 set_all_desc = set_copy(set);
6096 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006097 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006098 if (rc != LY_SUCCESS) {
6099 lyxp_set_free(set_all_desc);
6100 return rc;
6101 }
6102 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006103 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006104 if (rc != LY_SUCCESS) {
6105 lyxp_set_free(set_all_desc);
6106 return rc;
6107 }
6108 lyxp_set_free(set_all_desc);
6109
Radek Krejci1deb5be2020-08-26 16:43:36 +02006110 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006111 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006112
6113 /* only attributes of an elem can be in the result, skip all the rest,
6114 * we have all attributes qualified in lyd tree */
6115 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006116 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006117 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006118 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006119 continue;
6120 }
6121
Michal Vaskod3678892020-05-21 10:06:58 +02006122 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006123 /* match */
6124 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006125 set->val.meta[i].meta = sub;
6126 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006127 /* pos does not change */
6128 replaced = 1;
6129 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006130 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 +02006131 }
6132 ++i;
6133 }
6134 }
6135 }
6136
6137 if (!replaced) {
6138 /* no match */
6139 set_remove_node(set, i);
6140 }
6141 }
6142
6143 return LY_SUCCESS;
6144}
6145
6146/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006147 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6148 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006149 *
6150 * @param[in] parent Current parent.
6151 * @param[in] parent_pos Position of @p parent.
6152 * @param[in] parent_type Node type of @p parent.
6153 * @param[in,out] to_set Set to use.
6154 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006155 * @param[in] options XPath options.
6156 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6157 */
6158static LY_ERR
6159moveto_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 +02006160 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006161{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006162 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006163 LY_ERR rc;
6164
6165 switch (parent_type) {
6166 case LYXP_NODE_ROOT:
6167 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006168 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006169
Michal Vasko61ac2f62020-05-25 12:39:51 +02006170 /* add all top-level nodes as elements */
6171 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006172 break;
6173 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006174 /* add just the text node of this term element node */
6175 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006176 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6177 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6178 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006179 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006180 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006181
6182 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006183 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006184 break;
6185 default:
6186 LOGINT_RET(parent->schema->module->ctx);
6187 }
6188
Michal Vasko61ac2f62020-05-25 12:39:51 +02006189 /* add all top-level nodes as elements */
6190 LY_LIST_FOR(first, iter) {
6191 /* context check */
6192 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6193 continue;
6194 }
6195
6196 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006197 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006198 return LY_EINCOMPLETE;
6199 }
6200
6201 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6202 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6203
6204 /* also add all the children of this node, recursively */
6205 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6206 LY_CHECK_RET(rc);
6207 }
6208 }
6209
Michal Vasko03ff5a72019-09-11 13:49:33 +02006210 return LY_SUCCESS;
6211}
6212
6213/**
6214 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6215 * (or LYXP_SET_EMPTY). Context position aware.
6216 *
6217 * @param[in,out] set Set to use.
6218 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6219 * @param[in] options XPath options.
6220 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6221 */
6222static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006223moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006224{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006225 struct lyxp_set ret_set;
6226 LY_ERR rc;
6227
aPiecek8b0cc152021-05-31 16:40:31 +02006228 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006229 return LY_SUCCESS;
6230 }
6231
6232 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006233 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006234 return LY_EVALID;
6235 }
6236
6237 /* nothing to do */
6238 if (!all_desc) {
6239 return LY_SUCCESS;
6240 }
6241
Michal Vasko03ff5a72019-09-11 13:49:33 +02006242 /* add all the children, they get added recursively */
6243 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006244 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006245 /* copy the current node to tmp */
6246 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6247
6248 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006249 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006250 continue;
6251 }
6252
Michal Vasko03ff5a72019-09-11 13:49:33 +02006253 /* add all the children */
6254 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 +02006255 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006256 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006257 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006258 return rc;
6259 }
6260 }
6261
6262 /* use the temporary set as the current one */
6263 ret_set.ctx_pos = set->ctx_pos;
6264 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006265 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006266 memcpy(set, &ret_set, sizeof *set);
6267
6268 return LY_SUCCESS;
6269}
6270
6271/**
6272 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6273 * (or LYXP_SET_EMPTY).
6274 *
6275 * @param[in,out] set Set to use.
6276 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6277 * @param[in] options XPath options.
6278 * @return LY_ERR
6279 */
6280static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006281moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006282{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006283 uint32_t getnext_opts;
6284 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006285 const struct lysc_node *iter, *start_parent;
6286 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006287
aPiecek8b0cc152021-05-31 16:40:31 +02006288 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006289 return LY_SUCCESS;
6290 }
6291
6292 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006293 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006294 return LY_EVALID;
6295 }
6296
Michal Vasko519fd602020-05-26 12:17:39 +02006297 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006298 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006299 if (options & LYXP_SCNODE_OUTPUT) {
6300 getnext_opts |= LYS_GETNEXT_OUTPUT;
6301 }
6302
6303 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006304 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006305 if (!all_desc) {
6306 /* traverse the start node */
6307 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6308 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6309 }
6310 continue;
6311 }
6312
Radek Krejcif13b87b2020-12-01 22:02:17 +01006313 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6314 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006315 continue;
6316 }
6317
Michal Vasko519fd602020-05-26 12:17:39 +02006318 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006319 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006320 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006321 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006322 }
6323
Michal Vasko519fd602020-05-26 12:17:39 +02006324 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006325
Michal Vasko519fd602020-05-26 12:17:39 +02006326 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6327 /* it can actually be in any module, it's all <running> */
6328 mod_idx = 0;
Michal Vaskoa51ef072021-07-02 10:40:30 +02006329 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02006330 iter = NULL;
6331 /* module may not be implemented */
6332 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6333 /* context check */
6334 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6335 continue;
6336 }
6337
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006338 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006339 /* throw away the insert index, we want to consider that node again, recursively */
6340 }
6341 }
6342
6343 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6344 iter = NULL;
6345 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006346 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006347 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006348 continue;
6349 }
6350
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006351 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006352 }
6353 }
6354 }
6355
6356 return LY_SUCCESS;
6357}
6358
6359/**
6360 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6361 * (or LYXP_SET_EMPTY). Context position aware.
6362 *
6363 * @param[in] set Set to use.
6364 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6365 * @param[in] options XPath options.
6366 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6367 */
6368static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006369moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006370{
6371 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006372 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006373 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006374
aPiecek8b0cc152021-05-31 16:40:31 +02006375 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006376 return LY_SUCCESS;
6377 }
6378
6379 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006380 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006381 return LY_EVALID;
6382 }
6383
6384 if (all_desc) {
6385 /* <path>//.. == <path>//./.. */
6386 rc = moveto_self(set, 1, options);
6387 LY_CHECK_RET(rc);
6388 }
6389
Radek Krejci1deb5be2020-08-26 16:43:36 +02006390 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006391 node = set->val.nodes[i].node;
6392
6393 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9e685082021-01-29 14:49:09 +01006394 new_node = lyd_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006395 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6396 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006397 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6398 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006399 if (!new_node) {
6400 LOGINT_RET(set->ctx);
6401 }
6402 } else {
6403 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006404 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006405 continue;
6406 }
6407
Michal Vaskoa1424542019-11-14 16:08:52 +01006408 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006409 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 +02006410 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006411 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006412
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006413 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006414 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006415 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006416
Michal Vasko03ff5a72019-09-11 13:49:33 +02006417 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006418 /* 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 +02006419 new_type = LYXP_NODE_ELEM;
6420 }
6421
Michal Vasko03ff5a72019-09-11 13:49:33 +02006422 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006423 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006424 } else {
6425 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006426 }
6427 }
6428
Michal Vasko2caefc12019-11-14 16:07:56 +01006429 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006430 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006431
6432 return LY_SUCCESS;
6433}
6434
6435/**
6436 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6437 * (or LYXP_SET_EMPTY).
6438 *
6439 * @param[in] set Set to use.
6440 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6441 * @param[in] options XPath options.
6442 * @return LY_ERR
6443 */
6444static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006445moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006446{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006447 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006448 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006449 const struct lysc_node *node, *new_node;
6450 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006451
aPiecek8b0cc152021-05-31 16:40:31 +02006452 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006453 return LY_SUCCESS;
6454 }
6455
6456 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006457 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006458 return LY_EVALID;
6459 }
6460
6461 if (all_desc) {
6462 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006463 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006464 }
6465
Michal Vasko03ff5a72019-09-11 13:49:33 +02006466 orig_used = set->used;
6467 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006468 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6469 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006470 continue;
6471 }
6472
6473 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006474 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006475 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006476 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006477 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006478
6479 node = set->val.scnodes[i].scnode;
6480
6481 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006482 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006483 } else {
6484 /* root does not have a parent */
6485 continue;
6486 }
6487
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006488 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006489 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006490 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006491
Michal Vasko03ff5a72019-09-11 13:49:33 +02006492 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006493 /* 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 +02006494 new_type = LYXP_NODE_ELEM;
6495 }
6496
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006497 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6498 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006499 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006500 temp_ctx = 1;
6501 }
6502 }
6503
6504 if (temp_ctx) {
6505 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006506 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6507 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006508 }
6509 }
6510 }
6511
6512 return LY_SUCCESS;
6513}
6514
6515/**
6516 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6517 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6518 *
6519 * @param[in,out] set1 Set to use for the result.
6520 * @param[in] set2 Set acting as the second operand for @p op.
6521 * @param[in] op Comparison operator to process.
6522 * @param[in] options XPath options.
6523 * @return LY_ERR
6524 */
6525static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006526moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006527{
6528 /*
6529 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6530 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6531 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6532 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6533 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6534 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6535 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6536 *
6537 * '=' or '!='
6538 * BOOLEAN + BOOLEAN
6539 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6540 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6541 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6542 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6543 * NUMBER + NUMBER
6544 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6545 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6546 * STRING + STRING
6547 *
6548 * '<=', '<', '>=', '>'
6549 * NUMBER + NUMBER
6550 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6551 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6552 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6553 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6554 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6555 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6556 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6557 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6558 */
6559 struct lyxp_set iter1, iter2;
6560 int result;
6561 int64_t i;
6562 LY_ERR rc;
6563
Michal Vasko004d3152020-06-11 19:59:22 +02006564 memset(&iter1, 0, sizeof iter1);
6565 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006566
6567 /* iterative evaluation with node-sets */
6568 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6569 if (set1->type == LYXP_SET_NODE_SET) {
6570 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006571 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006572 switch (set2->type) {
6573 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006574 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006575 break;
6576 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006577 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006578 break;
6579 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006580 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006581 break;
6582 }
6583 LY_CHECK_RET(rc);
6584
Michal Vasko4c7763f2020-07-27 17:40:37 +02006585 /* canonize set2 */
6586 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6587
6588 /* compare recursively */
6589 rc = moveto_op_comp(&iter1, &iter2, op, options);
6590 lyxp_set_free_content(&iter2);
6591 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006592
6593 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006594 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006595 set_fill_boolean(set1, 1);
6596 return LY_SUCCESS;
6597 }
6598 }
6599 } else {
6600 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006601 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006602 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006603 case LYXP_SET_NUMBER:
6604 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6605 break;
6606 case LYXP_SET_BOOLEAN:
6607 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6608 break;
6609 default:
6610 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6611 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006612 }
6613 LY_CHECK_RET(rc);
6614
Michal Vasko4c7763f2020-07-27 17:40:37 +02006615 /* canonize set1 */
6616 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 +02006617
Michal Vasko4c7763f2020-07-27 17:40:37 +02006618 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006619 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006620 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006621 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006622
6623 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006624 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006625 set_fill_boolean(set1, 1);
6626 return LY_SUCCESS;
6627 }
6628 }
6629 }
6630
6631 /* false for all nodes */
6632 set_fill_boolean(set1, 0);
6633 return LY_SUCCESS;
6634 }
6635
6636 /* first convert properly */
6637 if ((op[0] == '=') || (op[0] == '!')) {
6638 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006639 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6640 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006641 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006642 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006643 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006644 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006645 LY_CHECK_RET(rc);
6646 } /* else we have 2 strings */
6647 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006648 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006649 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006650 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006651 LY_CHECK_RET(rc);
6652 }
6653
6654 assert(set1->type == set2->type);
6655
6656 /* compute result */
6657 if (op[0] == '=') {
6658 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006659 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006660 } else if (set1->type == LYXP_SET_NUMBER) {
6661 result = (set1->val.num == set2->val.num);
6662 } else {
6663 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006664 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006665 }
6666 } else if (op[0] == '!') {
6667 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006668 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006669 } else if (set1->type == LYXP_SET_NUMBER) {
6670 result = (set1->val.num != set2->val.num);
6671 } else {
6672 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006673 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006674 }
6675 } else {
6676 assert(set1->type == LYXP_SET_NUMBER);
6677 if (op[0] == '<') {
6678 if (op[1] == '=') {
6679 result = (set1->val.num <= set2->val.num);
6680 } else {
6681 result = (set1->val.num < set2->val.num);
6682 }
6683 } else {
6684 if (op[1] == '=') {
6685 result = (set1->val.num >= set2->val.num);
6686 } else {
6687 result = (set1->val.num > set2->val.num);
6688 }
6689 }
6690 }
6691
6692 /* assign result */
6693 if (result) {
6694 set_fill_boolean(set1, 1);
6695 } else {
6696 set_fill_boolean(set1, 0);
6697 }
6698
6699 return LY_SUCCESS;
6700}
6701
6702/**
6703 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6704 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6705 *
6706 * @param[in,out] set1 Set to use for the result.
6707 * @param[in] set2 Set acting as the second operand for @p op.
6708 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006709 * @return LY_ERR
6710 */
6711static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006712moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006713{
6714 LY_ERR rc;
6715
6716 /* unary '-' */
6717 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006718 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006719 LY_CHECK_RET(rc);
6720 set1->val.num *= -1;
6721 lyxp_set_free(set2);
6722 return LY_SUCCESS;
6723 }
6724
6725 assert(set1 && set2);
6726
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006727 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006728 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006729 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006730 LY_CHECK_RET(rc);
6731
6732 switch (op[0]) {
6733 /* '+' */
6734 case '+':
6735 set1->val.num += set2->val.num;
6736 break;
6737
6738 /* '-' */
6739 case '-':
6740 set1->val.num -= set2->val.num;
6741 break;
6742
6743 /* '*' */
6744 case '*':
6745 set1->val.num *= set2->val.num;
6746 break;
6747
6748 /* 'div' */
6749 case 'd':
6750 set1->val.num /= set2->val.num;
6751 break;
6752
6753 /* 'mod' */
6754 case 'm':
6755 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6756 break;
6757
6758 default:
6759 LOGINT_RET(set1->ctx);
6760 }
6761
6762 return LY_SUCCESS;
6763}
6764
6765/*
6766 * eval functions
6767 *
6768 * They execute a parsed XPath expression on some data subtree.
6769 */
6770
6771/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006772 * @brief Evaluate Predicate. Logs directly on error.
6773 *
Michal Vaskod3678892020-05-21 10:06:58 +02006774 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006775 *
6776 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006777 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02006778 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006779 * @param[in] options XPath options.
6780 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6781 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6782 */
6783static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006784eval_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 +02006785{
6786 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006787 uint16_t orig_exp;
6788 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006789 int32_t pred_in_ctx;
aPiecekfff4dca2021-10-07 10:59:53 +02006790 struct lyxp_set set2 = {0};
Michal Vasko03ff5a72019-09-11 13:49:33 +02006791 struct lyd_node *orig_parent;
6792
6793 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02006794 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006795 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006796 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006797
aPiecek8b0cc152021-05-31 16:40:31 +02006798 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006799only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02006800 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006801 LY_CHECK_RET(rc);
6802 } else if (set->type == LYXP_SET_NODE_SET) {
6803 /* 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 +01006804 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006805
6806 /* empty set, nothing to evaluate */
6807 if (!set->used) {
6808 goto only_parse;
6809 }
6810
Michal Vasko004d3152020-06-11 19:59:22 +02006811 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006812 orig_pos = 0;
6813 orig_size = set->used;
6814 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006815 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006816 set_init(&set2, set);
6817 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6818 /* remember the node context position for position() and context size for last(),
6819 * predicates should always be evaluated with respect to the child axis (since we do
6820 * not support explicit axes) so we assign positions based on their parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006821 if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) {
6822 orig_parent = lyd_parent(set->val.nodes[i].node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006823 orig_pos = 1;
6824 } else {
6825 ++orig_pos;
6826 }
6827
6828 set2.ctx_pos = orig_pos;
6829 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006830 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006831
Michal Vasko004d3152020-06-11 19:59:22 +02006832 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006833 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006834 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006835 return rc;
6836 }
6837
6838 /* number is a position */
6839 if (set2.type == LYXP_SET_NUMBER) {
6840 if ((long long)set2.val.num == orig_pos) {
6841 set2.val.num = 1;
6842 } else {
6843 set2.val.num = 0;
6844 }
6845 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006846 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006847
6848 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006849 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006850 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006851 }
6852 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006853 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006854
6855 } else if (set->type == LYXP_SET_SCNODE_SET) {
6856 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006857 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006858 /* there is a currently-valid node */
6859 break;
6860 }
6861 }
6862 /* empty set, nothing to evaluate */
6863 if (i == set->used) {
6864 goto only_parse;
6865 }
6866
Michal Vasko004d3152020-06-11 19:59:22 +02006867 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006868
Michal Vasko03ff5a72019-09-11 13:49:33 +02006869 /* set special in_ctx to all the valid snodes */
6870 pred_in_ctx = set_scnode_new_in_ctx(set);
6871
6872 /* use the valid snodes one-by-one */
6873 for (i = 0; i < set->used; ++i) {
6874 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6875 continue;
6876 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006877 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006878
Michal Vasko004d3152020-06-11 19:59:22 +02006879 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006880
Michal Vasko004d3152020-06-11 19:59:22 +02006881 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006882 LY_CHECK_RET(rc);
6883
6884 set->val.scnodes[i].in_ctx = pred_in_ctx;
6885 }
6886
6887 /* restore the state as it was before the predicate */
6888 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006889 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02006890 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006891 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006892 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006893 }
6894 }
6895
6896 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006897 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006898 set_fill_set(&set2, set);
6899
Michal Vasko004d3152020-06-11 19:59:22 +02006900 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006901 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006902 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006903 return rc;
6904 }
6905
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006906 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006907 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006908 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006909 }
Michal Vaskod3678892020-05-21 10:06:58 +02006910 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006911 }
6912
6913 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006914 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02006915 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006916 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006917 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006918
6919 return LY_SUCCESS;
6920}
6921
6922/**
Michal Vaskod3678892020-05-21 10:06:58 +02006923 * @brief Evaluate Literal. Logs directly on error.
6924 *
6925 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006926 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006927 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6928 */
6929static void
Michal Vasko40308e72020-10-20 16:38:40 +02006930eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006931{
6932 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006933 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006934 set_fill_string(set, "", 0);
6935 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006936 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 +02006937 }
6938 }
6939 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006940 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006941 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006942}
6943
6944/**
Michal Vasko004d3152020-06-11 19:59:22 +02006945 * @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 +02006946 *
Michal Vasko004d3152020-06-11 19:59:22 +02006947 * @param[in] exp Full parsed XPath expression.
6948 * @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 +02006949 * @param[in] ctx_node Found schema node as the context for the predicate.
6950 * @param[in] cur_mod Current module for the expression.
6951 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006952 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006953 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006954 * @param[out] predicates Parsed predicates.
6955 * @param[out] pred_type Type of @p predicates.
6956 * @return LY_SUCCESS on success,
6957 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006958 */
6959static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006960eval_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 +02006961 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 +02006962 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006963{
Michal Vasko004d3152020-06-11 19:59:22 +02006964 LY_ERR ret = LY_SUCCESS;
6965 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006966 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006967 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006968 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006969 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006970
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006971 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006972
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006973 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006974 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006975 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006976 return LY_EINVAL;
6977 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006978 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 +02006979 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006980
Michal Vasko004d3152020-06-11 19:59:22 +02006981 /* learn where the predicates end */
6982 e_idx = *tok_idx;
6983 while (key_count) {
6984 /* '[' */
6985 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6986 return LY_EINVAL;
6987 }
6988 ++e_idx;
6989
Michal Vasko3354d272021-04-06 09:40:06 +02006990 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
6991 /* definitely not a key */
6992 return LY_EINVAL;
6993 }
6994
Michal Vasko004d3152020-06-11 19:59:22 +02006995 /* ']' */
6996 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6997 ++e_idx;
6998 }
6999 ++e_idx;
7000
7001 /* another presumably key predicate parsed */
7002 --key_count;
7003 }
Michal Vasko004d3152020-06-11 19:59:22 +02007004 } else {
7005 /* learn just where this single predicate ends */
7006 e_idx = *tok_idx;
7007
Michal Vaskod3678892020-05-21 10:06:58 +02007008 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007009 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7010 return LY_EINVAL;
7011 }
7012 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007013
Michal Vasko3354d272021-04-06 09:40:06 +02007014 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
7015 /* definitely not the value */
7016 return LY_EINVAL;
7017 }
7018
Michal Vaskod3678892020-05-21 10:06:58 +02007019 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007020 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7021 ++e_idx;
7022 }
7023 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007024 }
7025
Michal Vasko004d3152020-06-11 19:59:22 +02007026 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
7027 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
7028
7029 /* turn logging off */
7030 prev_lo = ly_log_options(0);
7031
7032 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007033 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
7034 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007035
7036 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007037 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
7038 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02007039 LY_CHECK_GOTO(ret, cleanup);
7040
7041 /* success, the predicate must include all the needed information for hash-based search */
7042 *tok_idx = e_idx;
7043
7044cleanup:
7045 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007046 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02007047 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02007048}
7049
7050/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007051 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7052 * data node(s) could be found using a single hash-based search.
7053 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007054 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007055 * @param[in] node Next context node to check.
7056 * @param[in] name Expected node name.
7057 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007058 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007059 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007060 * @param[in] format Prefix format.
7061 * @param[in,out] found Previously found node, is updated.
7062 * @return LY_SUCCESS on success,
7063 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7064 */
7065static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007066eval_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 +02007067 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 +02007068 const struct lysc_node **found)
7069{
7070 const struct lysc_node *scnode;
7071 const struct lys_module *mod;
7072 uint32_t idx = 0;
7073
Radek Krejci8df109d2021-04-23 12:19:08 +02007074 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007075
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007076continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007077 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007078 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007079 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007080 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007081 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007082 if (!mod->implemented) {
7083 continue;
7084 }
7085
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007086 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7087 if (scnode) {
7088 /* we have found a match */
7089 break;
7090 }
7091 }
7092
Michal Vasko7d1d0912020-10-16 08:38:30 +02007093 if (!scnode) {
7094 /* all modules searched */
7095 idx = 0;
7096 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007097 } else {
7098 /* search in top-level */
7099 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7100 }
7101 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007102 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007103 /* we must adjust the module to inherit the one from the context node */
7104 moveto_mod = node->schema->module;
7105 }
7106
7107 /* search in children, do not repeat the same search */
7108 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007109 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007110
7111 /* additional context check */
7112 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7113 scnode = NULL;
7114 }
7115
7116 if (scnode) {
7117 if (*found) {
7118 /* we found a schema node with the same name but at different level, give up, too complicated
7119 * (more hash-based searches would be required, not supported) */
7120 return LY_ENOT;
7121 } else {
7122 /* remember the found schema node and continue to make sure it can be used */
7123 *found = scnode;
7124 }
7125 }
7126
7127 if (idx) {
7128 /* continue searching all the following models */
7129 goto continue_search;
7130 }
7131
7132 return LY_SUCCESS;
7133}
7134
7135/**
Michal Vaskod3678892020-05-21 10:06:58 +02007136 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7137 *
7138 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7139 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7140 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7141 *
7142 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007143 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007144 * @param[in] attr_axis Whether to search attributes or standard nodes.
7145 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007146 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007147 * @param[in] options XPath options.
7148 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7149 */
7150static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007151eval_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 +02007152 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007153{
Michal Vaskod3678892020-05-21 10:06:58 +02007154 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007155 const char *ncname, *ncname_dict = NULL;
7156 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007157 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007158 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007159 struct ly_path_predicate *predicates = NULL;
7160 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007161 LY_ERR rc = LY_SUCCESS;
7162
aPiecek8b0cc152021-05-31 16:40:31 +02007163 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007164 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007165 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007166
aPiecek8b0cc152021-05-31 16:40:31 +02007167 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02007168 goto moveto;
7169 }
7170
Michal Vasko004d3152020-06-11 19:59:22 +02007171 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7172 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007173
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007174 if ((ncname[0] == '*') && (ncname_len == 1)) {
7175 /* all nodes will match */
7176 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007177 }
7178
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007179 /* parse (and skip) module name */
7180 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007181 LY_CHECK_GOTO(rc, cleanup);
7182
Radek Krejci8df109d2021-04-23 12:19:08 +02007183 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 +02007184 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007185 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007186 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7187 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007188 /* check failed */
7189 scnode = NULL;
7190 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007191 }
7192 }
7193
Michal Vasko004d3152020-06-11 19:59:22 +02007194 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7195 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007196 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7197 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007198 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007199 scnode = NULL;
7200 }
7201 }
7202 }
7203
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007204 if (!scnode) {
7205 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007206 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007207 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007208 }
7209
7210moveto:
7211 /* move to the attribute(s), data node(s), or schema node(s) */
7212 if (attr_axis) {
aPiecek8b0cc152021-05-31 16:40:31 +02007213 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007214 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007215 } else {
7216 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007217 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007218 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007219 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007220 }
7221 LY_CHECK_GOTO(rc, cleanup);
7222 }
7223 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007224 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007225 int64_t i;
7226
Michal Vaskod3678892020-05-21 10:06:58 +02007227 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007228 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007229 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007230 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007231 }
7232 LY_CHECK_GOTO(rc, cleanup);
7233
7234 for (i = set->used - 1; i > -1; --i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007235 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vaskod3678892020-05-21 10:06:58 +02007236 break;
7237 }
7238 }
7239 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007240 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vasko90f12dc2020-12-03 14:20:42 +01007241 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".",
7242 ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007243 free(path);
7244 }
7245 } else {
7246 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007247 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007248 } else {
7249 if (scnode) {
7250 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007251 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007252 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007253 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007254 }
7255 }
7256 LY_CHECK_GOTO(rc, cleanup);
7257 }
7258 }
7259
7260 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007261 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7262 rc = eval_predicate(exp, tok_idx, set, options, 1);
aPiecek12677592021-10-07 11:28:01 +02007263 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007264 }
7265
7266cleanup:
aPiecek8b0cc152021-05-31 16:40:31 +02007267 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007268 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007269 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007270 }
Michal Vaskod3678892020-05-21 10:06:58 +02007271 return rc;
7272}
7273
7274/**
7275 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7276 *
7277 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7278 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7279 * [8] NodeType ::= 'text' | 'node'
7280 *
7281 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007282 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007283 * @param[in] attr_axis Whether to search attributes or standard nodes.
7284 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007285 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007286 * @param[in] options XPath options.
7287 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7288 */
7289static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007290eval_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 +02007291 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007292{
7293 LY_ERR rc;
7294
7295 /* TODO */
7296 (void)attr_axis;
7297 (void)all_desc;
7298
aPiecek8b0cc152021-05-31 16:40:31 +02007299 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007300 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007301 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007302 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
aPiecek8b0cc152021-05-31 16:40:31 +02007303 options |= LYXP_SKIP_EXPR;
Michal Vaskod3678892020-05-21 10:06:58 +02007304 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007305 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007306 rc = xpath_node(NULL, 0, set, options);
7307 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007308 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007309 rc = xpath_text(NULL, 0, set, options);
7310 }
7311 LY_CHECK_RET(rc);
7312 }
7313 }
aPiecek8b0cc152021-05-31 16:40:31 +02007314 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007315 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007316 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007317
7318 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007319 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007320 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007321 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007322 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007323
7324 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007325 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007326 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007327 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007328 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007329
7330 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007331 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7332 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007333 LY_CHECK_RET(rc);
7334 }
7335
7336 return LY_SUCCESS;
7337}
7338
7339/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007340 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7341 *
7342 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7343 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007344 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007345 *
7346 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007347 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007348 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007349 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007350 * @param[in] options XPath options.
7351 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7352 */
7353static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007354eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7355 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007356{
Radek Krejci857189e2020-09-01 13:26:36 +02007357 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007358 LY_ERR rc;
7359
7360 goto step;
7361 do {
7362 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007363 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007364 all_desc = 0;
7365 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007366 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007367 all_desc = 1;
7368 }
aPiecek8b0cc152021-05-31 16:40:31 +02007369 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007370 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007371 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007372
7373step:
Michal Vaskod3678892020-05-21 10:06:58 +02007374 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007375 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007376 attr_axis = 1;
7377
aPiecek8b0cc152021-05-31 16:40:31 +02007378 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007379 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007380 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007381 } else {
7382 attr_axis = 0;
7383 }
7384
Michal Vasko03ff5a72019-09-11 13:49:33 +02007385 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007386 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007387 case LYXP_TOKEN_DOT:
7388 /* evaluate '.' */
aPiecek8b0cc152021-05-31 16:40:31 +02007389 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007390 rc = moveto_scnode_self(set, all_desc, options);
7391 } else {
7392 rc = moveto_self(set, all_desc, options);
7393 }
7394 LY_CHECK_RET(rc);
7395 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007396 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007397 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007398 break;
7399
7400 case LYXP_TOKEN_DDOT:
7401 /* evaluate '..' */
aPiecek8b0cc152021-05-31 16:40:31 +02007402 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007403 rc = moveto_scnode_parent(set, all_desc, options);
7404 } else {
7405 rc = moveto_parent(set, all_desc, options);
7406 }
7407 LY_CHECK_RET(rc);
aPiecek8b0cc152021-05-31 16:40:31 +02007408 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007409 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007410 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007411 break;
7412
Michal Vasko03ff5a72019-09-11 13:49:33 +02007413 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007414 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007415 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007416 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007417 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007418
Michal Vaskod3678892020-05-21 10:06:58 +02007419 case LYXP_TOKEN_NODETYPE:
7420 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007421 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007422 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007423 break;
7424
7425 default:
aPiecek8b0cc152021-05-31 16:40:31 +02007426 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007427 }
Michal Vasko004d3152020-06-11 19:59:22 +02007428 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007429
7430 return LY_SUCCESS;
7431}
7432
7433/**
7434 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7435 *
7436 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7437 *
7438 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007439 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007440 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007441 * @param[in] options XPath options.
7442 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7443 */
7444static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007445eval_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 +02007446{
Radek Krejci857189e2020-09-01 13:26:36 +02007447 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007448
aPiecek8b0cc152021-05-31 16:40:31 +02007449 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007450 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007451 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007452 }
7453
7454 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007455 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007456 /* evaluate '/' - deferred */
7457 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02007458 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007459 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007460 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007461
Michal Vasko004d3152020-06-11 19:59:22 +02007462 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007463 return LY_SUCCESS;
7464 }
Michal Vasko004d3152020-06-11 19:59:22 +02007465 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007466 case LYXP_TOKEN_DOT:
7467 case LYXP_TOKEN_DDOT:
7468 case LYXP_TOKEN_AT:
7469 case LYXP_TOKEN_NAMETEST:
7470 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007471 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007472 break;
7473 default:
7474 break;
7475 }
7476
Michal Vasko03ff5a72019-09-11 13:49:33 +02007477 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007478 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007479 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7480 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02007481 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007482 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007483 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007484
Michal Vaskob0099a92020-08-31 14:55:23 +02007485 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007486 }
7487
7488 return LY_SUCCESS;
7489}
7490
7491/**
7492 * @brief Evaluate FunctionCall. Logs directly on error.
7493 *
Michal Vaskod3678892020-05-21 10:06:58 +02007494 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007495 *
7496 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007497 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007498 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007499 * @param[in] options XPath options.
7500 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7501 */
7502static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007503eval_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 +02007504{
7505 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007506
Radek Krejci1deb5be2020-08-26 16:43:36 +02007507 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007508 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007509 struct lyxp_set **args = NULL, **args_aux;
7510
aPiecek8b0cc152021-05-31 16:40:31 +02007511 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007512 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007513 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007514 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007515 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007516 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007517 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007518 xpath_func = &xpath_sum;
7519 }
7520 break;
7521 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007522 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007523 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007524 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007525 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007526 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007527 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007528 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007529 xpath_func = &xpath_true;
7530 }
7531 break;
7532 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007533 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007534 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007535 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007536 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007537 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007538 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007539 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007540 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007541 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007542 xpath_func = &xpath_deref;
7543 }
7544 break;
7545 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007546 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007547 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007548 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007549 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007550 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007551 xpath_func = &xpath_string;
7552 }
7553 break;
7554 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007555 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007556 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007557 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007558 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007559 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007560 xpath_func = &xpath_current;
7561 }
7562 break;
7563 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007564 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007565 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007566 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007567 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007568 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007569 xpath_func = &xpath_re_match;
7570 }
7571 break;
7572 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007573 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007574 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007575 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007576 xpath_func = &xpath_translate;
7577 }
7578 break;
7579 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007580 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007581 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007582 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007583 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007584 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007585 xpath_func = &xpath_bit_is_set;
7586 }
7587 break;
7588 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007589 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007590 xpath_func = &xpath_starts_with;
7591 }
7592 break;
7593 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007594 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007595 xpath_func = &xpath_derived_from;
7596 }
7597 break;
7598 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007599 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007600 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007601 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007602 xpath_func = &xpath_string_length;
7603 }
7604 break;
7605 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007606 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007607 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007608 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007609 xpath_func = &xpath_substring_after;
7610 }
7611 break;
7612 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007613 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007614 xpath_func = &xpath_substring_before;
7615 }
7616 break;
7617 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007618 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007619 xpath_func = &xpath_derived_from_or_self;
7620 }
7621 break;
7622 }
7623
7624 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007625 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 +02007626 return LY_EVALID;
7627 }
7628 }
7629
aPiecek8b0cc152021-05-31 16:40:31 +02007630 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007631 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007632 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007633
7634 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007635 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007636 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007637 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007638 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007639
7640 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007641 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02007642 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007643 args = malloc(sizeof *args);
7644 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7645 arg_count = 1;
7646 args[0] = set_copy(set);
7647 if (!args[0]) {
7648 rc = LY_EMEM;
7649 goto cleanup;
7650 }
7651
Michal Vasko004d3152020-06-11 19:59:22 +02007652 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007653 LY_CHECK_GOTO(rc, cleanup);
7654 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007655 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007656 LY_CHECK_GOTO(rc, cleanup);
7657 }
7658 }
Michal Vasko004d3152020-06-11 19:59:22 +02007659 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02007660 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007661 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007662 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007663
aPiecek8b0cc152021-05-31 16:40:31 +02007664 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007665 ++arg_count;
7666 args_aux = realloc(args, arg_count * sizeof *args);
7667 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7668 args = args_aux;
7669 args[arg_count - 1] = set_copy(set);
7670 if (!args[arg_count - 1]) {
7671 rc = LY_EMEM;
7672 goto cleanup;
7673 }
7674
Michal Vasko004d3152020-06-11 19:59:22 +02007675 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007676 LY_CHECK_GOTO(rc, cleanup);
7677 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007678 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007679 LY_CHECK_GOTO(rc, cleanup);
7680 }
7681 }
7682
7683 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007684 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007685 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007686 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007687 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007688
aPiecek8b0cc152021-05-31 16:40:31 +02007689 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007690 /* evaluate function */
7691 rc = xpath_func(args, arg_count, set, options);
7692
7693 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007694 /* merge all nodes from arg evaluations */
7695 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007696 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007697 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007698 }
7699 }
7700 } else {
7701 rc = LY_SUCCESS;
7702 }
7703
7704cleanup:
7705 for (i = 0; i < arg_count; ++i) {
7706 lyxp_set_free(args[i]);
7707 }
7708 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007709 return rc;
7710}
7711
7712/**
7713 * @brief Evaluate Number. Logs directly on error.
7714 *
7715 * @param[in] ctx Context for errors.
7716 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007717 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007718 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7719 * @return LY_ERR
7720 */
7721static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007722eval_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 +02007723{
7724 long double num;
7725 char *endptr;
7726
7727 if (set) {
7728 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007729 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007730 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007731 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7732 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007733 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007734 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007735 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007736 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7737 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007738 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007739 return LY_EVALID;
7740 }
7741
7742 set_fill_number(set, num);
7743 }
7744
7745 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007746 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007747 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007748 return LY_SUCCESS;
7749}
7750
7751/**
7752 * @brief Evaluate PathExpr. Logs directly on error.
7753 *
Michal Vaskod3678892020-05-21 10:06:58 +02007754 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007755 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7756 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7757 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007758 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007759 *
7760 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007761 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007762 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007763 * @param[in] options XPath options.
7764 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7765 */
7766static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007767eval_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 +02007768{
Radek Krejci857189e2020-09-01 13:26:36 +02007769 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007770 LY_ERR rc;
7771
Michal Vasko004d3152020-06-11 19:59:22 +02007772 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007773 case LYXP_TOKEN_PAR1:
7774 /* '(' Expr ')' */
7775
7776 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02007777 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007778 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007779 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007780
7781 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007782 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007783 LY_CHECK_RET(rc);
7784
7785 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007786 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007787 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007788 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007789 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007790
7791 parent_pos_pred = 0;
7792 goto predicate;
7793
7794 case LYXP_TOKEN_DOT:
7795 case LYXP_TOKEN_DDOT:
7796 case LYXP_TOKEN_AT:
7797 case LYXP_TOKEN_NAMETEST:
7798 case LYXP_TOKEN_NODETYPE:
7799 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007800 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007801 LY_CHECK_RET(rc);
7802 break;
7803
7804 case LYXP_TOKEN_FUNCNAME:
7805 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02007806 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007807 LY_CHECK_RET(rc);
7808
7809 parent_pos_pred = 1;
7810 goto predicate;
7811
Michal Vasko3e48bf32020-06-01 08:39:07 +02007812 case LYXP_TOKEN_OPER_PATH:
7813 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007814 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007815 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007816 LY_CHECK_RET(rc);
7817 break;
7818
7819 case LYXP_TOKEN_LITERAL:
7820 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02007821 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7822 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007823 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007824 }
Michal Vasko004d3152020-06-11 19:59:22 +02007825 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007826 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007827 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007828 }
7829
7830 parent_pos_pred = 1;
7831 goto predicate;
7832
7833 case LYXP_TOKEN_NUMBER:
7834 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02007835 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7836 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007837 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007838 }
Michal Vasko004d3152020-06-11 19:59:22 +02007839 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007840 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007841 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007842 }
7843 LY_CHECK_RET(rc);
7844
7845 parent_pos_pred = 1;
7846 goto predicate;
7847
7848 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01007849 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 +02007850 return LY_EVALID;
7851 }
7852
7853 return LY_SUCCESS;
7854
7855predicate:
7856 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007857 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7858 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007859 LY_CHECK_RET(rc);
7860 }
7861
7862 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007863 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007864
7865 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007866 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007867 all_desc = 0;
7868 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007869 all_desc = 1;
7870 }
7871
aPiecek8b0cc152021-05-31 16:40:31 +02007872 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007873 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007874 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007875
Michal Vasko004d3152020-06-11 19:59:22 +02007876 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007877 LY_CHECK_RET(rc);
7878 }
7879
7880 return LY_SUCCESS;
7881}
7882
7883/**
7884 * @brief Evaluate UnionExpr. Logs directly on error.
7885 *
Michal Vaskod3678892020-05-21 10:06:58 +02007886 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007887 *
7888 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007889 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007890 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007891 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007892 * @param[in] options XPath options.
7893 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7894 */
7895static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007896eval_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 +02007897{
7898 LY_ERR rc = LY_SUCCESS;
7899 struct lyxp_set orig_set, set2;
7900 uint16_t i;
7901
7902 assert(repeat);
7903
7904 set_init(&orig_set, set);
7905 set_init(&set2, set);
7906
7907 set_fill_set(&orig_set, set);
7908
Michal Vasko004d3152020-06-11 19:59:22 +02007909 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007910 LY_CHECK_GOTO(rc, cleanup);
7911
7912 /* ('|' PathExpr)* */
7913 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007914 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02007915 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007916 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007917 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007918
aPiecek8b0cc152021-05-31 16:40:31 +02007919 if (options & LYXP_SKIP_EXPR) {
7920 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007921 LY_CHECK_GOTO(rc, cleanup);
7922 continue;
7923 }
7924
7925 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007926 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007927 LY_CHECK_GOTO(rc, cleanup);
7928
7929 /* eval */
7930 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007931 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007932 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007933 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007934 LY_CHECK_GOTO(rc, cleanup);
7935 }
7936 }
7937
7938cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007939 lyxp_set_free_content(&orig_set);
7940 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007941 return rc;
7942}
7943
7944/**
7945 * @brief Evaluate UnaryExpr. Logs directly on error.
7946 *
Michal Vaskod3678892020-05-21 10:06:58 +02007947 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007948 *
7949 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007950 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007951 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007952 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007953 * @param[in] options XPath options.
7954 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7955 */
7956static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007957eval_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 +02007958{
7959 LY_ERR rc;
7960 uint16_t this_op, i;
7961
7962 assert(repeat);
7963
7964 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007965 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007966 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007967 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 +02007968
aPiecek8b0cc152021-05-31 16:40:31 +02007969 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007970 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007971 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007972 }
7973
Michal Vasko004d3152020-06-11 19:59:22 +02007974 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007975 LY_CHECK_RET(rc);
7976
aPiecek8b0cc152021-05-31 16:40:31 +02007977 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007978 if (options & LYXP_SCNODE_ALL) {
7979 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7980 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007981 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007982 LY_CHECK_RET(rc);
7983 }
7984 }
7985
7986 return LY_SUCCESS;
7987}
7988
7989/**
7990 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7991 *
Michal Vaskod3678892020-05-21 10:06:58 +02007992 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007993 * | MultiplicativeExpr '*' UnaryExpr
7994 * | MultiplicativeExpr 'div' UnaryExpr
7995 * | MultiplicativeExpr 'mod' UnaryExpr
7996 *
7997 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007998 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007999 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008000 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008001 * @param[in] options XPath options.
8002 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8003 */
8004static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008005eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
8006 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008007{
8008 LY_ERR rc;
8009 uint16_t this_op;
8010 struct lyxp_set orig_set, set2;
8011 uint16_t i;
8012
8013 assert(repeat);
8014
8015 set_init(&orig_set, set);
8016 set_init(&set2, set);
8017
8018 set_fill_set(&orig_set, set);
8019
Michal Vasko004d3152020-06-11 19:59:22 +02008020 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008021 LY_CHECK_GOTO(rc, cleanup);
8022
8023 /* ('*' / 'div' / 'mod' UnaryExpr)* */
8024 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008025 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008026
Michal Vasko004d3152020-06-11 19:59:22 +02008027 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02008028 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008029 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008030 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008031
aPiecek8b0cc152021-05-31 16:40:31 +02008032 if (options & LYXP_SKIP_EXPR) {
8033 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008034 LY_CHECK_GOTO(rc, cleanup);
8035 continue;
8036 }
8037
8038 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008039 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008040 LY_CHECK_GOTO(rc, cleanup);
8041
8042 /* eval */
8043 if (options & LYXP_SCNODE_ALL) {
8044 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008045 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008046 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008047 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008048 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008049 LY_CHECK_GOTO(rc, cleanup);
8050 }
8051 }
8052
8053cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008054 lyxp_set_free_content(&orig_set);
8055 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008056 return rc;
8057}
8058
8059/**
8060 * @brief Evaluate AdditiveExpr. Logs directly on error.
8061 *
Michal Vaskod3678892020-05-21 10:06:58 +02008062 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008063 * | AdditiveExpr '+' MultiplicativeExpr
8064 * | AdditiveExpr '-' MultiplicativeExpr
8065 *
8066 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008067 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008068 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008069 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008070 * @param[in] options XPath options.
8071 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8072 */
8073static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008074eval_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 +02008075{
8076 LY_ERR rc;
8077 uint16_t this_op;
8078 struct lyxp_set orig_set, set2;
8079 uint16_t i;
8080
8081 assert(repeat);
8082
8083 set_init(&orig_set, set);
8084 set_init(&set2, set);
8085
8086 set_fill_set(&orig_set, set);
8087
Michal Vasko004d3152020-06-11 19:59:22 +02008088 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008089 LY_CHECK_GOTO(rc, cleanup);
8090
8091 /* ('+' / '-' MultiplicativeExpr)* */
8092 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008093 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008094
Michal Vasko004d3152020-06-11 19:59:22 +02008095 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008096 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008097 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008098 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008099
aPiecek8b0cc152021-05-31 16:40:31 +02008100 if (options & LYXP_SKIP_EXPR) {
8101 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008102 LY_CHECK_GOTO(rc, cleanup);
8103 continue;
8104 }
8105
8106 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008107 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008108 LY_CHECK_GOTO(rc, cleanup);
8109
8110 /* eval */
8111 if (options & LYXP_SCNODE_ALL) {
8112 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008113 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008114 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008115 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008116 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008117 LY_CHECK_GOTO(rc, cleanup);
8118 }
8119 }
8120
8121cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008122 lyxp_set_free_content(&orig_set);
8123 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008124 return rc;
8125}
8126
8127/**
8128 * @brief Evaluate RelationalExpr. Logs directly on error.
8129 *
Michal Vaskod3678892020-05-21 10:06:58 +02008130 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008131 * | RelationalExpr '<' AdditiveExpr
8132 * | RelationalExpr '>' AdditiveExpr
8133 * | RelationalExpr '<=' AdditiveExpr
8134 * | RelationalExpr '>=' AdditiveExpr
8135 *
8136 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008137 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008138 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008139 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008140 * @param[in] options XPath options.
8141 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8142 */
8143static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008144eval_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 +02008145{
8146 LY_ERR rc;
8147 uint16_t this_op;
8148 struct lyxp_set orig_set, set2;
8149 uint16_t i;
8150
8151 assert(repeat);
8152
8153 set_init(&orig_set, set);
8154 set_init(&set2, set);
8155
8156 set_fill_set(&orig_set, set);
8157
Michal Vasko004d3152020-06-11 19:59:22 +02008158 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008159 LY_CHECK_GOTO(rc, cleanup);
8160
8161 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8162 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008163 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008164
Michal Vasko004d3152020-06-11 19:59:22 +02008165 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02008166 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008167 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008168 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008169
aPiecek8b0cc152021-05-31 16:40:31 +02008170 if (options & LYXP_SKIP_EXPR) {
8171 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008172 LY_CHECK_GOTO(rc, cleanup);
8173 continue;
8174 }
8175
8176 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008177 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008178 LY_CHECK_GOTO(rc, cleanup);
8179
8180 /* eval */
8181 if (options & LYXP_SCNODE_ALL) {
8182 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008183 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008184 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008185 } else {
8186 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8187 LY_CHECK_GOTO(rc, cleanup);
8188 }
8189 }
8190
8191cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008192 lyxp_set_free_content(&orig_set);
8193 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008194 return rc;
8195}
8196
8197/**
8198 * @brief Evaluate EqualityExpr. Logs directly on error.
8199 *
Michal Vaskod3678892020-05-21 10:06:58 +02008200 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008201 * | EqualityExpr '!=' RelationalExpr
8202 *
8203 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008204 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008205 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008206 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008207 * @param[in] options XPath options.
8208 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8209 */
8210static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008211eval_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 +02008212{
8213 LY_ERR rc;
8214 uint16_t this_op;
8215 struct lyxp_set orig_set, set2;
8216 uint16_t i;
8217
8218 assert(repeat);
8219
8220 set_init(&orig_set, set);
8221 set_init(&set2, set);
8222
8223 set_fill_set(&orig_set, set);
8224
Michal Vasko004d3152020-06-11 19:59:22 +02008225 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008226 LY_CHECK_GOTO(rc, cleanup);
8227
8228 /* ('=' / '!=' RelationalExpr)* */
8229 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008230 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008231
Michal Vasko004d3152020-06-11 19:59:22 +02008232 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02008233 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008234 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008235 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008236
aPiecek8b0cc152021-05-31 16:40:31 +02008237 if (options & LYXP_SKIP_EXPR) {
8238 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008239 LY_CHECK_GOTO(rc, cleanup);
8240 continue;
8241 }
8242
8243 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008244 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008245 LY_CHECK_GOTO(rc, cleanup);
8246
8247 /* eval */
8248 if (options & LYXP_SCNODE_ALL) {
8249 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008250 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8251 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008252 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008253 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008254 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008255 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8256 LY_CHECK_GOTO(rc, cleanup);
8257 }
8258 }
8259
8260cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008261 lyxp_set_free_content(&orig_set);
8262 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008263 return rc;
8264}
8265
8266/**
8267 * @brief Evaluate AndExpr. Logs directly on error.
8268 *
Michal Vaskod3678892020-05-21 10:06:58 +02008269 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008270 *
8271 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008272 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008273 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008274 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008275 * @param[in] options XPath options.
8276 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8277 */
8278static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008279eval_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 +02008280{
8281 LY_ERR rc;
8282 struct lyxp_set orig_set, set2;
8283 uint16_t i;
8284
8285 assert(repeat);
8286
8287 set_init(&orig_set, set);
8288 set_init(&set2, set);
8289
8290 set_fill_set(&orig_set, set);
8291
Michal Vasko004d3152020-06-11 19:59:22 +02008292 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008293 LY_CHECK_GOTO(rc, cleanup);
8294
8295 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008296 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008297 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008298 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008299 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008300 }
8301
8302 /* ('and' EqualityExpr)* */
8303 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008304 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008305 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008306 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008307 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008308
8309 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008310 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8311 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008312 LY_CHECK_GOTO(rc, cleanup);
8313 continue;
8314 }
8315
8316 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008317 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008318 LY_CHECK_GOTO(rc, cleanup);
8319
8320 /* eval - just get boolean value actually */
8321 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008322 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008323 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008324 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008325 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008326 set_fill_set(set, &set2);
8327 }
8328 }
8329
8330cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008331 lyxp_set_free_content(&orig_set);
8332 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008333 return rc;
8334}
8335
8336/**
8337 * @brief Evaluate OrExpr. Logs directly on error.
8338 *
Michal Vaskod3678892020-05-21 10:06:58 +02008339 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008340 *
8341 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008342 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008343 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008344 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008345 * @param[in] options XPath options.
8346 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8347 */
8348static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008349eval_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 +02008350{
8351 LY_ERR rc;
8352 struct lyxp_set orig_set, set2;
8353 uint16_t i;
8354
8355 assert(repeat);
8356
8357 set_init(&orig_set, set);
8358 set_init(&set2, set);
8359
8360 set_fill_set(&orig_set, set);
8361
Michal Vasko004d3152020-06-11 19:59:22 +02008362 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008363 LY_CHECK_GOTO(rc, cleanup);
8364
8365 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008366 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008367 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008368 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008369 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008370 }
8371
8372 /* ('or' AndExpr)* */
8373 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008374 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008375 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008376 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008377 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008378
8379 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008380 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8381 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008382 LY_CHECK_GOTO(rc, cleanup);
8383 continue;
8384 }
8385
8386 set_fill_set(&set2, &orig_set);
8387 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8388 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008389 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008390 LY_CHECK_GOTO(rc, cleanup);
8391
8392 /* eval - just get boolean value actually */
8393 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008394 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008395 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008396 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008397 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008398 set_fill_set(set, &set2);
8399 }
8400 }
8401
8402cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008403 lyxp_set_free_content(&orig_set);
8404 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008405 return rc;
8406}
8407
8408/**
Michal Vasko004d3152020-06-11 19:59:22 +02008409 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008410 *
8411 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008412 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008413 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02008414 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008415 * @param[in] options XPath options.
8416 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8417 */
8418static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008419eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8420 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008421{
8422 uint16_t i, count;
8423 enum lyxp_expr_type next_etype;
8424 LY_ERR rc;
8425
8426 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008427 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008428 next_etype = LYXP_EXPR_NONE;
8429 } else {
8430 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008431 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008432
8433 /* select one-priority lower because etype expression called us */
8434 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008435 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008436 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008437 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008438 } else {
8439 next_etype = LYXP_EXPR_NONE;
8440 }
8441 }
8442
8443 /* decide what expression are we parsing based on the repeat */
8444 switch (next_etype) {
8445 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008446 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008447 break;
8448 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008449 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008450 break;
8451 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008452 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008453 break;
8454 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008455 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008456 break;
8457 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008458 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008459 break;
8460 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008461 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008462 break;
8463 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008464 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008465 break;
8466 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008467 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008468 break;
8469 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008470 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008471 break;
8472 default:
8473 LOGINT_RET(set->ctx);
8474 }
8475
8476 return rc;
8477}
8478
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008479/**
8480 * @brief Get root type.
8481 *
8482 * @param[in] ctx_node Context node.
8483 * @param[in] ctx_scnode Schema context node.
8484 * @param[in] options XPath options.
8485 * @return Root type.
8486 */
8487static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008488lyxp_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 +01008489{
Michal Vasko6b26e742020-07-17 15:02:10 +02008490 const struct lysc_node *op;
8491
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008492 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008493 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008494 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008495
8496 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008497 /* general root that can access everything */
8498 return LYXP_NODE_ROOT;
8499 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8500 /* root context node can access only config data (because we said so, it is unspecified) */
8501 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008502 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008503 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008504 }
8505
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008506 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008507 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008508 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008509
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008510 if (op || !(options & LYXP_SCHEMA)) {
8511 /* general root that can access everything */
8512 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05008513 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008514 /* root context node can access only config data (because we said so, it is unspecified) */
8515 return LYXP_NODE_ROOT_CONFIG;
8516 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008517 return LYXP_NODE_ROOT;
8518}
8519
Michal Vasko03ff5a72019-09-11 13:49:33 +02008520LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008521lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008522 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 +01008523 struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008524{
Michal Vasko004d3152020-06-11 19:59:22 +02008525 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008526 LY_ERR rc;
8527
Michal Vasko400e9672021-01-11 13:39:17 +01008528 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008529 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008530 LOGARG(NULL, "Current module must be set if schema format is used.");
8531 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008532 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008533
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008534 if (tree) {
8535 /* adjust the pointer to be the first top-level sibling */
8536 while (tree->parent) {
8537 tree = lyd_parent(tree);
8538 }
8539 tree = lyd_first_sibling(tree);
8540 }
8541
Michal Vasko03ff5a72019-09-11 13:49:33 +02008542 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008543 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008544 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008545 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8546 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8547
Michal Vasko400e9672021-01-11 13:39:17 +01008548 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008549 set->cur_node = ctx_node;
8550 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008551 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8552 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008553 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008554 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008555 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008556 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008557
Radek Krejciddace2c2021-01-08 11:30:56 +01008558 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008559
Michal Vasko03ff5a72019-09-11 13:49:33 +02008560 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008561 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008562 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008563 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008564 }
8565
Radek Krejciddace2c2021-01-08 11:30:56 +01008566 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008567 return rc;
8568}
8569
8570#if 0
8571
8572/* full xml printing of set elements, not used currently */
8573
8574void
8575lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8576{
8577 uint32_t i;
8578 char *str_num;
8579 struct lyout out;
8580
8581 memset(&out, 0, sizeof out);
8582
8583 out.type = LYOUT_STREAM;
8584 out.method.f = f;
8585
8586 switch (set->type) {
8587 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008588 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008589 break;
8590 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008591 ly_print_(&out, "Boolean XPath set:\n");
8592 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008593 break;
8594 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008595 ly_print_(&out, "String XPath set:\n");
8596 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008597 break;
8598 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008599 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008600
8601 if (isnan(set->value.num)) {
8602 str_num = strdup("NaN");
8603 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8604 str_num = strdup("0");
8605 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8606 str_num = strdup("Infinity");
8607 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8608 str_num = strdup("-Infinity");
8609 } else if ((long long)set->value.num == set->value.num) {
8610 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8611 str_num = NULL;
8612 }
8613 } else {
8614 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8615 str_num = NULL;
8616 }
8617 }
8618 if (!str_num) {
8619 LOGMEM;
8620 return;
8621 }
Michal Vasko5233e962020-08-14 14:26:20 +02008622 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008623 free(str_num);
8624 break;
8625 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008626 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008627
8628 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008629 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008630 switch (set->node_type[i]) {
8631 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008632 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008633 break;
8634 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008635 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008636 break;
8637 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008638 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008639 break;
8640 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008641 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008642 break;
8643 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008644 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008645 break;
8646 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008647 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008648 break;
8649 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008650 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008651 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008652 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008653 break;
8654 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008655 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 +02008656 break;
8657 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008658 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 +02008659 break;
8660 }
8661 }
8662 break;
8663 }
8664}
8665
8666#endif
8667
8668LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008669lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008670{
8671 long double num;
8672 char *str;
8673 LY_ERR rc;
8674
8675 if (!set || (set->type == target)) {
8676 return LY_SUCCESS;
8677 }
8678
8679 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008680 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008681
8682 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008683 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008684 return LY_EINVAL;
8685 }
8686
8687 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008688 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008689 switch (set->type) {
8690 case LYXP_SET_NUMBER:
8691 if (isnan(set->val.num)) {
8692 set->val.str = strdup("NaN");
8693 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8694 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8695 set->val.str = strdup("0");
8696 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8697 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8698 set->val.str = strdup("Infinity");
8699 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8700 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8701 set->val.str = strdup("-Infinity");
8702 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8703 } else if ((long long)set->val.num == set->val.num) {
8704 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8705 LOGMEM_RET(set->ctx);
8706 }
8707 set->val.str = str;
8708 } else {
8709 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8710 LOGMEM_RET(set->ctx);
8711 }
8712 set->val.str = str;
8713 }
8714 break;
8715 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008716 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008717 set->val.str = strdup("true");
8718 } else {
8719 set->val.str = strdup("false");
8720 }
8721 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8722 break;
8723 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008724 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008725 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008726
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008727 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008728 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008729 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008730 set->val.str = str;
8731 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008732 default:
8733 LOGINT_RET(set->ctx);
8734 }
8735 set->type = LYXP_SET_STRING;
8736 }
8737
8738 /* to NUMBER */
8739 if (target == LYXP_SET_NUMBER) {
8740 switch (set->type) {
8741 case LYXP_SET_STRING:
8742 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008743 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008744 set->val.num = num;
8745 break;
8746 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008747 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008748 set->val.num = 1;
8749 } else {
8750 set->val.num = 0;
8751 }
8752 break;
8753 default:
8754 LOGINT_RET(set->ctx);
8755 }
8756 set->type = LYXP_SET_NUMBER;
8757 }
8758
8759 /* to BOOLEAN */
8760 if (target == LYXP_SET_BOOLEAN) {
8761 switch (set->type) {
8762 case LYXP_SET_NUMBER:
8763 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008764 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008765 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008766 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008767 }
8768 break;
8769 case LYXP_SET_STRING:
8770 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008771 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008772 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008773 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008774 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008775 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008776 }
8777 break;
8778 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008779 if (set->used) {
8780 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008781 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008782 } else {
8783 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008784 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008785 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008786 break;
8787 default:
8788 LOGINT_RET(set->ctx);
8789 }
8790 set->type = LYXP_SET_BOOLEAN;
8791 }
8792
Michal Vasko03ff5a72019-09-11 13:49:33 +02008793 return LY_SUCCESS;
8794}
8795
8796LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008797lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008798 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
Michal Vasko400e9672021-01-11 13:39:17 +01008799 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008800{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008801 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008802 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008803
Michal Vasko400e9672021-01-11 13:39:17 +01008804 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008805 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008806 LOGARG(NULL, "Current module must be set if schema format is used.");
8807 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008808 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008809
8810 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008811 memset(set, 0, sizeof *set);
8812 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008813 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8814 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 +01008815 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008816
Michal Vasko400e9672021-01-11 13:39:17 +01008817 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008818 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008819 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008820 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8821 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008822 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008823 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008824 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008825
Radek Krejciddace2c2021-01-08 11:30:56 +01008826 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008827
Michal Vasko03ff5a72019-09-11 13:49:33 +02008828 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01008829 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
8830
Radek Krejciddace2c2021-01-08 11:30:56 +01008831 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008832 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008833}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008834
8835API const char *
8836lyxp_get_expr(const struct lyxp_expr *path)
8837{
8838 if (!path) {
8839 return NULL;
8840 }
8841
8842 return path->expr;
8843}