blob: 57e0d2d51c662c3e389771bcdb57f5b49b120093 [file] [log] [blame]
Radek Krejcib1646a92018-11-02 16:08:26 +01001/**
2 * @file xpath.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG XPath evaluation functions
5 *
Michal Vasko61ac2f62020-05-25 12:39:51 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcib1646a92018-11-02 16:08:26 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Christian Hopps32874e12021-05-01 09:43:54 -040014#define _GNU_SOURCE /* asprintf, strdup */
Radek Krejcib1646a92018-11-02 16:08:26 +010015
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010017
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010019#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010023#include <stdio.h>
24#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010025#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010026
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020028#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010032#include "out.h"
Radek Krejci7931b192020-06-25 17:05:03 +020033#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020034#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020035#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020037#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "tree.h"
Radek Krejci77114102021-03-10 15:21:57 +010039#include "tree_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020040#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010041#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020042#include "tree_schema_internal.h"
43#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020044
aPiecekbf968d92021-05-27 14:35:05 +020045static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth);
Michal Vasko40308e72020-10-20 16:38:40 +020046static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype,
47 struct lyxp_set *set, uint32_t options);
Michal Vasko93923692021-05-07 15:28:02 +020048static LY_ERR moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
49 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod);
Michal Vasko03ff5a72019-09-11 13:49:33 +020050
51/**
52 * @brief Print the type of an XPath \p set.
53 *
54 * @param[in] set Set to use.
55 * @return Set type string.
56 */
57static const char *
58print_set_type(struct lyxp_set *set)
59{
60 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020061 case LYXP_SET_NODE_SET:
62 return "node set";
63 case LYXP_SET_SCNODE_SET:
64 return "schema node set";
65 case LYXP_SET_BOOLEAN:
66 return "boolean";
67 case LYXP_SET_NUMBER:
68 return "number";
69 case LYXP_SET_STRING:
70 return "string";
71 }
72
73 return NULL;
74}
75
Michal Vasko24cddf82020-06-01 08:17:01 +020076const char *
77lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020078{
79 switch (tok) {
80 case LYXP_TOKEN_PAR1:
81 return "(";
82 case LYXP_TOKEN_PAR2:
83 return ")";
84 case LYXP_TOKEN_BRACK1:
85 return "[";
86 case LYXP_TOKEN_BRACK2:
87 return "]";
88 case LYXP_TOKEN_DOT:
89 return ".";
90 case LYXP_TOKEN_DDOT:
91 return "..";
92 case LYXP_TOKEN_AT:
93 return "@";
94 case LYXP_TOKEN_COMMA:
95 return ",";
96 case LYXP_TOKEN_NAMETEST:
97 return "NameTest";
98 case LYXP_TOKEN_NODETYPE:
99 return "NodeType";
100 case LYXP_TOKEN_FUNCNAME:
101 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200102 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200103 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200104 case LYXP_TOKEN_OPER_EQUAL:
105 return "Operator(Equal)";
106 case LYXP_TOKEN_OPER_NEQUAL:
107 return "Operator(Non-equal)";
108 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200109 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200110 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200111 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200112 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200113 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200114 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200115 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200116 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200117 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200118 case LYXP_TOKEN_LITERAL:
119 return "Literal";
120 case LYXP_TOKEN_NUMBER:
121 return "Number";
122 default:
123 LOGINT(NULL);
124 return "";
125 }
126}
127
128/**
129 * @brief Print the whole expression \p exp to debug output.
130 *
131 * @param[in] exp Expression to use.
132 */
133static void
Michal Vasko40308e72020-10-20 16:38:40 +0200134print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200135{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100136#define MSG_BUFFER_SIZE 128
137 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100138 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200139
Radek Krejci52b6d512020-10-12 12:33:17 +0200140 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200141 return;
142 }
143
144 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
145 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200146 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200147 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100148 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200149 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
150 for (j = 1; exp->repeat[i][j]; ++j) {
151 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
152 }
153 strcat(tmp, ")");
154 }
155 LOGDBG(LY_LDGXPATH, tmp);
156 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100157#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200158}
159
160#ifndef NDEBUG
161
162/**
163 * @brief Print XPath set content to debug output.
164 *
165 * @param[in] set Set to print.
166 */
167static void
168print_set_debug(struct lyxp_set *set)
169{
170 uint32_t i;
171 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200172 struct lyxp_set_node *item;
173 struct lyxp_set_scnode *sitem;
174
Radek Krejci52b6d512020-10-12 12:33:17 +0200175 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200176 return;
177 }
178
179 switch (set->type) {
180 case LYXP_SET_NODE_SET:
181 LOGDBG(LY_LDGXPATH, "set NODE SET:");
182 for (i = 0; i < set->used; ++i) {
183 item = &set->val.nodes[i];
184
185 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100186 case LYXP_NODE_NONE:
187 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
188 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200189 case LYXP_NODE_ROOT:
190 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
191 break;
192 case LYXP_NODE_ROOT_CONFIG:
193 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
194 break;
195 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100196 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200197 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200198 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100199 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200200 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200201 item->node->schema->name, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200202 } else {
203 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
204 }
205 break;
206 case LYXP_NODE_TEXT:
207 if (item->node->schema->nodetype & LYS_ANYDATA) {
208 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200209 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200210 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200211 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200212 }
213 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100214 case LYXP_NODE_META:
215 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
Michal Vasko69730152020-10-09 16:30:07 +0200216 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200217 break;
218 }
219 }
220 break;
221
222 case LYXP_SET_SCNODE_SET:
223 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
224 for (i = 0; i < set->used; ++i) {
225 sitem = &set->val.scnodes[i];
226
227 switch (sitem->type) {
228 case LYXP_NODE_ROOT:
229 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
230 break;
231 case LYXP_NODE_ROOT_CONFIG:
232 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
233 break;
234 case LYXP_NODE_ELEM:
235 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
236 break;
237 default:
238 LOGINT(NULL);
239 break;
240 }
241 }
242 break;
243
Michal Vasko03ff5a72019-09-11 13:49:33 +0200244 case LYXP_SET_BOOLEAN:
245 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200246 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200247 break;
248
249 case LYXP_SET_STRING:
250 LOGDBG(LY_LDGXPATH, "set STRING");
251 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
252 break;
253
254 case LYXP_SET_NUMBER:
255 LOGDBG(LY_LDGXPATH, "set NUMBER");
256
257 if (isnan(set->val.num)) {
258 str = strdup("NaN");
259 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
260 str = strdup("0");
261 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
262 str = strdup("Infinity");
263 } else if (isinf(set->val.num) && signbit(set->val.num)) {
264 str = strdup("-Infinity");
265 } else if ((long long)set->val.num == set->val.num) {
266 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
267 str = NULL;
268 }
269 } else {
270 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
271 str = NULL;
272 }
273 }
274 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
275
276 LOGDBG(LY_LDGXPATH, "\t%s", str);
277 free(str);
278 }
279}
280
281#endif
282
283/**
284 * @brief Realloc the string \p str.
285 *
286 * @param[in] ctx libyang context for logging.
287 * @param[in] needed How much free space is required.
288 * @param[in,out] str Pointer to the string to use.
289 * @param[in,out] used Used bytes in \p str.
290 * @param[in,out] size Allocated bytes in \p str.
291 * @return LY_ERR
292 */
293static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100294cast_string_realloc(const struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200295{
296 if (*size - *used < needed) {
297 do {
298 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
299 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
300 return LY_EINVAL;
301 }
302 *size += LYXP_STRING_CAST_SIZE_STEP;
303 } while (*size - *used < needed);
304 *str = ly_realloc(*str, *size * sizeof(char));
305 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
306 }
307
308 return LY_SUCCESS;
309}
310
311/**
312 * @brief Cast nodes recursively to one string @p str.
313 *
314 * @param[in] node Node to cast.
315 * @param[in] fake_cont Whether to put the data into a "fake" container.
316 * @param[in] root_type Type of the XPath root.
317 * @param[in] indent Current indent.
318 * @param[in,out] str Resulting string.
319 * @param[in,out] used Used bytes in @p str.
320 * @param[in,out] size Allocated bytes in @p str.
321 * @return LY_ERR
322 */
323static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200324cast_string_recursive(const struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
Radek Krejci0f969882020-08-21 16:56:47 +0200325 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200326{
Radek Krejci7f769d72020-07-11 23:13:56 +0200327 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200328 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200329 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200330 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200331 struct lyd_node_any *any;
332 LY_ERR rc;
333
334 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
335 return LY_SUCCESS;
336 }
337
338 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200339 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200340 LY_CHECK_RET(rc);
341 strcpy(*str + (*used - 1), "\n");
342 ++(*used);
343
344 ++indent;
345 }
346
347 switch (node->schema->nodetype) {
348 case LYS_CONTAINER:
349 case LYS_LIST:
350 case LYS_RPC:
351 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200352 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200353 LY_CHECK_RET(rc);
354 strcpy(*str + (*used - 1), "\n");
355 ++(*used);
356
Radek Krejcia1c1e542020-09-29 16:06:52 +0200357 for (child = lyd_child(node); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200358 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
359 LY_CHECK_RET(rc);
360 }
361
362 break;
363
364 case LYS_LEAF:
365 case LYS_LEAFLIST:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200366 value_str = lyd_get_value(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200367
368 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200369 LY_CHECK_RET(cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200370 memset(*str + (*used - 1), ' ', indent * 2);
371 *used += indent * 2;
372
373 /* print value */
374 if (*used == 1) {
375 sprintf(*str + (*used - 1), "%s", value_str);
376 *used += strlen(value_str);
377 } else {
378 sprintf(*str + (*used - 1), "%s\n", value_str);
379 *used += strlen(value_str) + 1;
380 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200381
382 break;
383
384 case LYS_ANYXML:
385 case LYS_ANYDATA:
386 any = (struct lyd_node_any *)node;
387 if (!(void *)any->value.tree) {
388 /* no content */
389 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200390 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200391 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200392 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100393
Michal Vasko60ea6352020-06-29 13:39:39 +0200394 if (any->value_type == LYD_ANYDATA_LYB) {
395 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200396 if (lyd_parse_data_mem((struct ly_ctx *)LYD_CTX(node), any->value.mem, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200397 /* successfully parsed */
398 free(any->value.mem);
399 any->value.tree = tree;
400 any->value_type = LYD_ANYDATA_DATATREE;
401 }
Radek Krejci7931b192020-06-25 17:05:03 +0200402 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200403 }
404
Michal Vasko03ff5a72019-09-11 13:49:33 +0200405 switch (any->value_type) {
406 case LYD_ANYDATA_STRING:
407 case LYD_ANYDATA_XML:
408 case LYD_ANYDATA_JSON:
409 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200410 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200411 break;
412 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200413 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200414 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200415 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100416 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200417 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200418 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200419 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200420 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200421 }
422 }
423
424 line = strtok_r(buf, "\n", &ptr);
425 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200426 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200427 if (rc != LY_SUCCESS) {
428 free(buf);
429 return rc;
430 }
431 memset(*str + (*used - 1), ' ', indent * 2);
432 *used += indent * 2;
433
434 strcpy(*str + (*used - 1), line);
435 *used += strlen(line);
436
437 strcpy(*str + (*used - 1), "\n");
438 *used += 1;
439 } while ((line = strtok_r(NULL, "\n", &ptr)));
440
441 free(buf);
442 break;
443
444 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200445 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200446 }
447
448 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200449 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200450 LY_CHECK_RET(rc);
451 strcpy(*str + (*used - 1), "\n");
452 ++(*used);
453
454 --indent;
455 }
456
457 return LY_SUCCESS;
458}
459
460/**
461 * @brief Cast an element into a string.
462 *
463 * @param[in] node Node to cast.
464 * @param[in] fake_cont Whether to put the data into a "fake" container.
465 * @param[in] root_type Type of the XPath root.
466 * @param[out] str Element cast to dynamically-allocated string.
467 * @return LY_ERR
468 */
469static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200470cast_string_elem(struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200471{
472 uint16_t used, size;
473 LY_ERR rc;
474
475 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200476 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200477 (*str)[0] = '\0';
478 used = 1;
479 size = LYXP_STRING_CAST_SIZE_START;
480
481 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
482 if (rc != LY_SUCCESS) {
483 free(*str);
484 return rc;
485 }
486
487 if (size > used) {
488 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200489 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200490 }
491 return LY_SUCCESS;
492}
493
494/**
495 * @brief Cast a LYXP_SET_NODE_SET set into a string.
496 * Context position aware.
497 *
498 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200499 * @param[out] str Cast dynamically-allocated string.
500 * @return LY_ERR
501 */
502static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100503cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200504{
Michal Vaskoaa677062021-03-09 13:52:53 +0100505 if (!set->used) {
506 *str = strdup("");
507 if (!*str) {
508 LOGMEM_RET(set->ctx);
509 }
510 return LY_SUCCESS;
511 }
512
Michal Vasko03ff5a72019-09-11 13:49:33 +0200513 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100514 case LYXP_NODE_NONE:
515 /* invalid */
516 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200517 case LYXP_NODE_ROOT:
518 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100519 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200520 case LYXP_NODE_ELEM:
521 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100522 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100523 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200524 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200525 if (!*str) {
526 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200527 }
528 return LY_SUCCESS;
529 }
530
531 LOGINT_RET(set->ctx);
532}
533
534/**
535 * @brief Cast a string into an XPath number.
536 *
537 * @param[in] str String to use.
538 * @return Cast number.
539 */
540static long double
541cast_string_to_number(const char *str)
542{
543 long double num;
544 char *ptr;
545
546 errno = 0;
547 num = strtold(str, &ptr);
548 if (errno || *ptr) {
549 num = NAN;
550 }
551 return num;
552}
553
554/**
555 * @brief Callback for checking value equality.
556 *
Michal Vasko62524a92021-02-26 10:08:50 +0100557 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200558 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200559 * @param[in] val1_p First value.
560 * @param[in] val2_p Second value.
561 * @param[in] mod Whether hash table is being modified.
562 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200563 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200564 */
Radek Krejci857189e2020-09-01 13:26:36 +0200565static ly_bool
566set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200567{
568 struct lyxp_set_hash_node *val1, *val2;
569
570 val1 = (struct lyxp_set_hash_node *)val1_p;
571 val2 = (struct lyxp_set_hash_node *)val2_p;
572
573 if ((val1->node == val2->node) && (val1->type == val2->type)) {
574 return 1;
575 }
576
577 return 0;
578}
579
580/**
581 * @brief Insert node and its hash into set.
582 *
583 * @param[in] set et to insert to.
584 * @param[in] node Node with hash.
585 * @param[in] type Node type.
586 */
587static void
588set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
589{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200590 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200591 uint32_t i, hash;
592 struct lyxp_set_hash_node hnode;
593
594 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
595 /* create hash table and add all the nodes */
596 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
597 for (i = 0; i < set->used; ++i) {
598 hnode.node = set->val.nodes[i].node;
599 hnode.type = set->val.nodes[i].type;
600
601 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
602 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
603 hash = dict_hash_multi(hash, NULL, 0);
604
605 r = lyht_insert(set->ht, &hnode, hash, NULL);
606 assert(!r);
607 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200608
Michal Vasko9d6befd2019-12-11 14:56:56 +0100609 if (hnode.node == node) {
610 /* it was just added, do not add it twice */
611 node = NULL;
612 }
613 }
614 }
615
616 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200617 /* add the new node into hash table */
618 hnode.node = node;
619 hnode.type = type;
620
621 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
622 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
623 hash = dict_hash_multi(hash, NULL, 0);
624
625 r = lyht_insert(set->ht, &hnode, hash, NULL);
626 assert(!r);
627 (void)r;
628 }
629}
630
631/**
632 * @brief Remove node and its hash from set.
633 *
634 * @param[in] set Set to remove from.
635 * @param[in] node Node to remove.
636 * @param[in] type Node type.
637 */
638static void
639set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
640{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200641 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200642 struct lyxp_set_hash_node hnode;
643 uint32_t hash;
644
645 if (set->ht) {
646 hnode.node = node;
647 hnode.type = type;
648
649 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
650 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
651 hash = dict_hash_multi(hash, NULL, 0);
652
653 r = lyht_remove(set->ht, &hnode, hash);
654 assert(!r);
655 (void)r;
656
657 if (!set->ht->used) {
658 lyht_free(set->ht);
659 set->ht = NULL;
660 }
661 }
662}
663
664/**
665 * @brief Check whether node is in set based on its hash.
666 *
667 * @param[in] set Set to search in.
668 * @param[in] node Node to search for.
669 * @param[in] type Node type.
670 * @param[in] skip_idx Index in @p set to skip.
671 * @return LY_ERR
672 */
673static LY_ERR
674set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
675{
676 struct lyxp_set_hash_node hnode, *match_p;
677 uint32_t hash;
678
679 hnode.node = node;
680 hnode.type = type;
681
682 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
683 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
684 hash = dict_hash_multi(hash, NULL, 0);
685
686 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
687 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
688 /* we found it on the index that should be skipped, find another */
689 hnode = *match_p;
690 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
691 /* none other found */
692 return LY_SUCCESS;
693 }
694 }
695
696 return LY_EEXIST;
697 }
698
699 /* not found */
700 return LY_SUCCESS;
701}
702
Michal Vaskod3678892020-05-21 10:06:58 +0200703void
704lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200705{
706 if (!set) {
707 return;
708 }
709
710 if (set->type == LYXP_SET_NODE_SET) {
711 free(set->val.nodes);
712 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200713 } else if (set->type == LYXP_SET_SCNODE_SET) {
714 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200715 lyht_free(set->ht);
716 } else {
717 if (set->type == LYXP_SET_STRING) {
718 free(set->val.str);
719 }
720 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200721 }
Michal Vaskod3678892020-05-21 10:06:58 +0200722
723 set->val.nodes = NULL;
724 set->used = 0;
725 set->size = 0;
726 set->ht = NULL;
727 set->ctx_pos = 0;
aPiecek748da732021-06-01 09:56:43 +0200728 set->ctx_size = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200729}
730
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100731/**
732 * @brief Free dynamically-allocated set.
733 *
734 * @param[in] set Set to free.
735 */
736static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200737lyxp_set_free(struct lyxp_set *set)
738{
739 if (!set) {
740 return;
741 }
742
Michal Vaskod3678892020-05-21 10:06:58 +0200743 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200744 free(set);
745}
746
747/**
748 * @brief Initialize set context.
749 *
750 * @param[in] new Set to initialize.
751 * @param[in] set Arbitrary initialized set.
752 */
753static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200754set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200755{
756 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200757 if (set) {
758 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200759 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100760 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200761 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100762 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200763 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200764 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200765 new->prefix_data = set->prefix_data;
Michal Vasko02a77382019-09-12 11:47:35 +0200766 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200767}
768
769/**
770 * @brief Create a deep copy of a set.
771 *
772 * @param[in] set Set to copy.
773 * @return Copy of @p set.
774 */
775static struct lyxp_set *
776set_copy(struct lyxp_set *set)
777{
778 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100779 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200780
781 if (!set) {
782 return NULL;
783 }
784
785 ret = malloc(sizeof *ret);
786 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
787 set_init(ret, set);
788
789 if (set->type == LYXP_SET_SCNODE_SET) {
790 ret->type = set->type;
791
792 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100793 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
794 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200795 uint32_t idx;
796 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
797 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100798 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200799 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200800 lyxp_set_free(ret);
801 return NULL;
802 }
Michal Vaskoba716542019-12-16 10:01:58 +0100803 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200804 }
805 }
806 } else if (set->type == LYXP_SET_NODE_SET) {
807 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200808 if (set->used) {
809 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
810 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
811 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
812 } else {
813 ret->val.nodes = NULL;
814 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200815
816 ret->used = ret->size = set->used;
817 ret->ctx_pos = set->ctx_pos;
818 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100819 if (set->ht) {
820 ret->ht = lyht_dup(set->ht);
821 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200822 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200823 memcpy(ret, set, sizeof *ret);
824 if (set->type == LYXP_SET_STRING) {
825 ret->val.str = strdup(set->val.str);
826 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
827 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200828 }
829
830 return ret;
831}
832
833/**
834 * @brief Fill XPath set with a string. Any current data are disposed of.
835 *
836 * @param[in] set Set to fill.
837 * @param[in] string String to fill into \p set.
838 * @param[in] str_len Length of \p string. 0 is a valid value!
839 */
840static void
841set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
842{
Michal Vaskod3678892020-05-21 10:06:58 +0200843 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200844
845 set->type = LYXP_SET_STRING;
846 if ((str_len == 0) && (string[0] != '\0')) {
847 string = "";
848 }
849 set->val.str = strndup(string, str_len);
850}
851
852/**
853 * @brief Fill XPath set with a number. Any current data are disposed of.
854 *
855 * @param[in] set Set to fill.
856 * @param[in] number Number to fill into \p set.
857 */
858static void
859set_fill_number(struct lyxp_set *set, long double number)
860{
Michal Vaskod3678892020-05-21 10:06:58 +0200861 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200862
863 set->type = LYXP_SET_NUMBER;
864 set->val.num = number;
865}
866
867/**
868 * @brief Fill XPath set with a boolean. Any current data are disposed of.
869 *
870 * @param[in] set Set to fill.
871 * @param[in] boolean Boolean to fill into \p set.
872 */
873static void
Radek Krejci857189e2020-09-01 13:26:36 +0200874set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200875{
Michal Vaskod3678892020-05-21 10:06:58 +0200876 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200877
878 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200879 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200880}
881
882/**
883 * @brief Fill XPath set with the value from another set (deep assign).
884 * Any current data are disposed of.
885 *
886 * @param[in] trg Set to fill.
887 * @param[in] src Source set to copy into \p trg.
888 */
889static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200890set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200891{
892 if (!trg || !src) {
893 return;
894 }
895
896 if (trg->type == LYXP_SET_NODE_SET) {
897 free(trg->val.nodes);
898 } else if (trg->type == LYXP_SET_STRING) {
899 free(trg->val.str);
900 }
901 set_init(trg, src);
902
903 if (src->type == LYXP_SET_SCNODE_SET) {
904 trg->type = LYXP_SET_SCNODE_SET;
905 trg->used = src->used;
906 trg->size = src->used;
907
Michal Vasko08e9b112021-06-11 15:41:17 +0200908 if (trg->size) {
909 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
910 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
911 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
912 } else {
913 trg->val.scnodes = NULL;
914 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200915 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200916 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200917 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200918 set_fill_number(trg, src->val.num);
919 } else if (src->type == LYXP_SET_STRING) {
920 set_fill_string(trg, src->val.str, strlen(src->val.str));
921 } else {
922 if (trg->type == LYXP_SET_NODE_SET) {
923 free(trg->val.nodes);
924 } else if (trg->type == LYXP_SET_STRING) {
925 free(trg->val.str);
926 }
927
Michal Vaskod3678892020-05-21 10:06:58 +0200928 assert(src->type == LYXP_SET_NODE_SET);
929
930 trg->type = LYXP_SET_NODE_SET;
931 trg->used = src->used;
932 trg->size = src->used;
933 trg->ctx_pos = src->ctx_pos;
934 trg->ctx_size = src->ctx_size;
935
Michal Vasko08e9b112021-06-11 15:41:17 +0200936 if (trg->size) {
937 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
938 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
939 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
940 } else {
941 trg->val.nodes = NULL;
942 }
Michal Vaskod3678892020-05-21 10:06:58 +0200943 if (src->ht) {
944 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200945 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200946 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200947 }
948 }
949}
950
951/**
952 * @brief Clear context of all schema nodes.
953 *
954 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +0200955 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200956 */
957static void
Michal Vasko1a09b212021-05-06 13:00:10 +0200958set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200959{
960 uint32_t i;
961
962 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100963 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +0200964 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100965 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
966 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200967 }
968 }
969}
970
971/**
972 * @brief Remove a node from a set. Removing last node changes
973 * set into LYXP_SET_EMPTY. Context position aware.
974 *
975 * @param[in] set Set to use.
976 * @param[in] idx Index from @p set of the node to be removed.
977 */
978static void
979set_remove_node(struct lyxp_set *set, uint32_t idx)
980{
981 assert(set && (set->type == LYXP_SET_NODE_SET));
982 assert(idx < set->used);
983
984 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
985
986 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +0200987 if (idx < set->used) {
988 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
989 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +0200990 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200991 }
992}
993
994/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100995 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200996 *
997 * @param[in] set Set to use.
998 * @param[in] idx Index from @p set of the node to be removed.
999 */
1000static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001001set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001002{
1003 assert(set && (set->type == LYXP_SET_NODE_SET));
1004 assert(idx < set->used);
1005
Michal Vasko2caefc12019-11-14 16:07:56 +01001006 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1007 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1008 }
1009 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001010}
1011
1012/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001013 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001014 * set into LYXP_SET_EMPTY. Context position aware.
1015 *
1016 * @param[in] set Set to consolidate.
1017 */
1018static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001019set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001020{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001021 uint32_t i, orig_used, end = 0;
1022 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001023
Michal Vaskod3678892020-05-21 10:06:58 +02001024 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001025
1026 orig_used = set->used;
1027 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001028 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001029 start = -1;
1030 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001031 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001032 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001033 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001034 end = i;
1035 ++i;
1036 break;
1037 }
1038
1039 ++i;
1040 if (i == orig_used) {
1041 end = i;
1042 }
1043 } while (i < orig_used);
1044
1045 if (start > -1) {
1046 /* move the whole chunk of valid nodes together */
1047 if (set->used != (unsigned)start) {
1048 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1049 }
1050 set->used += end - start;
1051 }
1052 }
Michal Vasko57eab132019-09-24 11:46:26 +02001053}
1054
1055/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001056 * @brief Check for duplicates in a node set.
1057 *
1058 * @param[in] set Set to check.
1059 * @param[in] node Node to look for in @p set.
1060 * @param[in] node_type Type of @p node.
1061 * @param[in] skip_idx Index from @p set to skip.
1062 * @return LY_ERR
1063 */
1064static LY_ERR
1065set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1066{
1067 uint32_t i;
1068
Michal Vasko2caefc12019-11-14 16:07:56 +01001069 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001070 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1071 }
1072
1073 for (i = 0; i < set->used; ++i) {
1074 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1075 continue;
1076 }
1077
1078 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1079 return LY_EEXIST;
1080 }
1081 }
1082
1083 return LY_SUCCESS;
1084}
1085
Radek Krejci857189e2020-09-01 13:26:36 +02001086ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001087lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1088 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001089{
1090 uint32_t i;
1091
1092 for (i = 0; i < set->used; ++i) {
1093 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1094 continue;
1095 }
1096
1097 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001098 if (index_p) {
1099 *index_p = i;
1100 }
1101 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001102 }
1103 }
1104
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001105 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001106}
1107
Michal Vaskoecd62de2019-11-13 12:35:11 +01001108void
1109lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001110{
1111 uint32_t orig_used, i, j;
1112
Michal Vaskod3678892020-05-21 10:06:58 +02001113 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001114
Michal Vaskod3678892020-05-21 10:06:58 +02001115 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001116 return;
1117 }
1118
Michal Vaskod3678892020-05-21 10:06:58 +02001119 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001120 memcpy(set1, set2, sizeof *set1);
1121 return;
1122 }
1123
1124 if (set1->used + set2->used > set1->size) {
1125 set1->size = set1->used + set2->used;
1126 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1127 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1128 }
1129
1130 orig_used = set1->used;
1131
1132 for (i = 0; i < set2->used; ++i) {
1133 for (j = 0; j < orig_used; ++j) {
1134 /* detect duplicities */
1135 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1136 break;
1137 }
1138 }
1139
Michal Vasko0587bce2020-12-10 12:19:21 +01001140 if (j < orig_used) {
1141 /* node is there, but update its status if needed */
1142 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1143 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001144 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1145 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1146 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001147 }
1148 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001149 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1150 ++set1->used;
1151 }
1152 }
1153
Michal Vaskod3678892020-05-21 10:06:58 +02001154 lyxp_set_free_content(set2);
1155 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001156}
1157
1158/**
1159 * @brief Insert a node into a set. Context position aware.
1160 *
1161 * @param[in] set Set to use.
1162 * @param[in] node Node to insert to @p set.
1163 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1164 * @param[in] node_type Node type of @p node.
1165 * @param[in] idx Index in @p set to insert into.
1166 */
1167static void
1168set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1169{
Michal Vaskod3678892020-05-21 10:06:58 +02001170 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001171
Michal Vaskod3678892020-05-21 10:06:58 +02001172 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001173 /* first item */
1174 if (idx) {
1175 /* no real harm done, but it is a bug */
1176 LOGINT(set->ctx);
1177 idx = 0;
1178 }
1179 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1180 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1181 set->type = LYXP_SET_NODE_SET;
1182 set->used = 0;
1183 set->size = LYXP_SET_SIZE_START;
1184 set->ctx_pos = 1;
1185 set->ctx_size = 1;
1186 set->ht = NULL;
1187 } else {
1188 /* not an empty set */
1189 if (set->used == set->size) {
1190
1191 /* set is full */
1192 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1193 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1194 set->size += LYXP_SET_SIZE_STEP;
1195 }
1196
1197 if (idx > set->used) {
1198 LOGINT(set->ctx);
1199 idx = set->used;
1200 }
1201
1202 /* make space for the new node */
1203 if (idx < set->used) {
1204 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1205 }
1206 }
1207
1208 /* finally assign the value */
1209 set->val.nodes[idx].node = (struct lyd_node *)node;
1210 set->val.nodes[idx].type = node_type;
1211 set->val.nodes[idx].pos = pos;
1212 ++set->used;
1213
Michal Vasko2caefc12019-11-14 16:07:56 +01001214 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1215 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1216 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001217}
1218
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001219LY_ERR
1220lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001221{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001222 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001223
1224 assert(set->type == LYXP_SET_SCNODE_SET);
1225
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001226 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001227 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001228 } else {
1229 if (set->used == set->size) {
1230 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes);
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001231 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001232 set->size += LYXP_SET_SIZE_STEP;
1233 }
1234
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001235 index = set->used;
1236 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1237 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001238 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001239 ++set->used;
1240 }
1241
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001242 if (index_p) {
1243 *index_p = index;
1244 }
1245
1246 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001247}
1248
1249/**
1250 * @brief Replace a node in a set with another. Context position aware.
1251 *
1252 * @param[in] set Set to use.
1253 * @param[in] node Node to insert to @p set.
1254 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1255 * @param[in] node_type Node type of @p node.
1256 * @param[in] idx Index in @p set of the node to replace.
1257 */
1258static void
1259set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1260{
1261 assert(set && (idx < set->used));
1262
Michal Vasko2caefc12019-11-14 16:07:56 +01001263 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1264 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1265 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001266 set->val.nodes[idx].node = (struct lyd_node *)node;
1267 set->val.nodes[idx].type = node_type;
1268 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001269 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1270 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1271 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001272}
1273
1274/**
1275 * @brief Set all nodes with ctx 1 to a new unique context value.
1276 *
1277 * @param[in] set Set to modify.
1278 * @return New context value.
1279 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001280static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001281set_scnode_new_in_ctx(struct lyxp_set *set)
1282{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001283 uint32_t i;
1284 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001285
1286 assert(set->type == LYXP_SET_SCNODE_SET);
1287
Radek Krejcif13b87b2020-12-01 22:02:17 +01001288 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001289retry:
1290 for (i = 0; i < set->used; ++i) {
1291 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1292 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1293 goto retry;
1294 }
1295 }
1296 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001297 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001298 set->val.scnodes[i].in_ctx = ret_ctx;
1299 }
1300 }
1301
1302 return ret_ctx;
1303}
1304
1305/**
1306 * @brief Get unique @p node position in the data.
1307 *
1308 * @param[in] node Node to find.
1309 * @param[in] node_type Node type of @p node.
1310 * @param[in] root Root node.
1311 * @param[in] root_type Type of the XPath @p root node.
1312 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1313 * be used to increase efficiency and start the DFS from this node.
1314 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1315 * @return Node position.
1316 */
1317static uint32_t
1318get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root,
Radek Krejci0f969882020-08-21 16:56:47 +02001319 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001320{
Michal Vasko56daf732020-08-10 10:57:18 +02001321 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001322 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001323 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001324
1325 assert(prev && prev_pos && !root->prev->next);
1326
1327 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1328 return 0;
1329 }
1330
1331 if (*prev) {
1332 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001333 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001334 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001335 goto dfs_search;
1336 }
1337
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001338 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001339 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001340dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001341 if (*prev && !elem) {
1342 /* resume previous DFS */
1343 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1344 LYD_TREE_DFS_continue = 0;
1345 }
1346
Michal Vasko03ff5a72019-09-11 13:49:33 +02001347 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001348 /* skip */
1349 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001350 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001351 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001352 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001353 break;
1354 }
Michal Vasko56daf732020-08-10 10:57:18 +02001355 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001356 }
Michal Vasko56daf732020-08-10 10:57:18 +02001357
1358 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001359 }
1360
1361 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001362 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001363 break;
1364 }
1365 }
1366
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001367 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001368 if (!(*prev)) {
1369 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001370 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001371 return 0;
1372 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001373 /* start the search again from the beginning */
1374 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001375
Michal Vasko56daf732020-08-10 10:57:18 +02001376 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001377 pos = 1;
1378 goto dfs_search;
1379 }
1380 }
1381
1382 /* remember the last found node for next time */
1383 *prev = node;
1384 *prev_pos = pos;
1385
1386 return pos;
1387}
1388
1389/**
1390 * @brief Assign (fill) missing node positions.
1391 *
1392 * @param[in] set Set to fill positions in.
1393 * @param[in] root Context root node.
1394 * @param[in] root_type Context root type.
1395 * @return LY_ERR
1396 */
1397static LY_ERR
1398set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1399{
1400 const struct lyd_node *prev = NULL, *tmp_node;
1401 uint32_t i, tmp_pos = 0;
1402
1403 for (i = 0; i < set->used; ++i) {
1404 if (!set->val.nodes[i].pos) {
1405 tmp_node = NULL;
1406 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001407 case LYXP_NODE_META:
1408 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001409 if (!tmp_node) {
1410 LOGINT_RET(root->schema->module->ctx);
1411 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001412 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001413 case LYXP_NODE_ELEM:
1414 case LYXP_NODE_TEXT:
1415 if (!tmp_node) {
1416 tmp_node = set->val.nodes[i].node;
1417 }
1418 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1419 break;
1420 default:
1421 /* all roots have position 0 */
1422 break;
1423 }
1424 }
1425 }
1426
1427 return LY_SUCCESS;
1428}
1429
1430/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001431 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001432 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001433 * @param[in] meta Metadata to use.
1434 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001435 */
1436static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001437get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001438{
1439 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001440 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001441
Michal Vasko9f96a052020-03-10 09:41:45 +01001442 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001443 ++pos;
1444 }
1445
Michal Vasko9f96a052020-03-10 09:41:45 +01001446 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001447 return pos;
1448}
1449
1450/**
1451 * @brief Compare 2 nodes in respect to XPath document order.
1452 *
1453 * @param[in] item1 1st node.
1454 * @param[in] item2 2nd node.
1455 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1456 */
1457static int
1458set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1459{
Michal Vasko9f96a052020-03-10 09:41:45 +01001460 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001461
1462 if (item1->pos < item2->pos) {
1463 return -1;
1464 }
1465
1466 if (item1->pos > item2->pos) {
1467 return 1;
1468 }
1469
1470 /* node positions are equal, the fun case */
1471
1472 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1473 /* special case since text nodes are actually saved as their parents */
1474 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1475 if (item1->type == LYXP_NODE_ELEM) {
1476 assert(item2->type == LYXP_NODE_TEXT);
1477 return -1;
1478 } else {
1479 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1480 return 1;
1481 }
1482 }
1483
Michal Vasko9f96a052020-03-10 09:41:45 +01001484 /* we need meta positions now */
1485 if (item1->type == LYXP_NODE_META) {
1486 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001487 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001488 if (item2->type == LYXP_NODE_META) {
1489 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001490 }
1491
Michal Vasko9f96a052020-03-10 09:41:45 +01001492 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001493 /* check for duplicates */
1494 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001495 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001496 return 0;
1497 }
1498
Michal Vasko9f96a052020-03-10 09:41:45 +01001499 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001500 /* elem is always first, 2nd node is after it */
1501 if (item1->type == LYXP_NODE_ELEM) {
1502 assert(item2->type != LYXP_NODE_ELEM);
1503 return -1;
1504 }
1505
Michal Vasko9f96a052020-03-10 09:41:45 +01001506 /* 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 +02001507 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001508 if (((item1->type == LYXP_NODE_TEXT) &&
1509 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1510 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1511 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1512 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001513 return 1;
1514 }
1515
Michal Vasko9f96a052020-03-10 09:41:45 +01001516 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001517 /* 2nd is after 1st */
1518 return -1;
1519}
1520
1521/**
1522 * @brief Set cast for comparisons.
1523 *
1524 * @param[in] trg Target set to cast source into.
1525 * @param[in] src Source set.
1526 * @param[in] type Target set type.
1527 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001528 * @return LY_ERR
1529 */
1530static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001531set_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 +02001532{
1533 assert(src->type == LYXP_SET_NODE_SET);
1534
1535 set_init(trg, src);
1536
1537 /* insert node into target set */
1538 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1539
1540 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001541 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001542}
1543
Michal Vasko4c7763f2020-07-27 17:40:37 +02001544/**
1545 * @brief Set content canonization for comparisons.
1546 *
1547 * @param[in] trg Target set to put the canononized source into.
1548 * @param[in] src Source set.
1549 * @param[in] xp_node Source XPath node/meta to use for canonization.
1550 * @return LY_ERR
1551 */
1552static LY_ERR
1553set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1554{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001555 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001556 struct lyd_value val;
1557 struct ly_err_item *err = NULL;
1558 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001559 LY_ERR rc;
1560
1561 /* is there anything to canonize even? */
1562 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1563 /* do we have a type to use for canonization? */
1564 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1565 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1566 } else if (xp_node->type == LYXP_NODE_META) {
1567 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1568 }
1569 }
1570 if (!type) {
1571 goto fill;
1572 }
1573
1574 if (src->type == LYXP_SET_NUMBER) {
1575 /* canonize number */
1576 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1577 LOGMEM(src->ctx);
1578 return LY_EMEM;
1579 }
1580 } else {
1581 /* canonize string */
1582 str = strdup(src->val.str);
1583 }
1584
1585 /* ignore errors, the value may not satisfy schema constraints */
Radek Krejci0b013302021-03-29 15:22:32 +02001586 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 +01001587 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001588 ly_err_free(err);
1589 if (rc) {
1590 /* invalid value */
Radek IÅ¡a48460222021-03-02 15:18:32 +01001591 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001592 goto fill;
1593 }
1594
Michal Vasko4c7763f2020-07-27 17:40:37 +02001595 /* use the canonized value */
1596 set_init(trg, src);
1597 trg->type = src->type;
1598 if (src->type == LYXP_SET_NUMBER) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001599 trg->val.num = strtold(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001600 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1601 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001602 trg->val.str = strdup(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko4c7763f2020-07-27 17:40:37 +02001603 }
1604 type->plugin->free(src->ctx, &val);
1605 return LY_SUCCESS;
1606
1607fill:
1608 /* no canonization needed/possible */
1609 set_fill_set(trg, src);
1610 return LY_SUCCESS;
1611}
1612
Michal Vasko03ff5a72019-09-11 13:49:33 +02001613#ifndef NDEBUG
1614
1615/**
1616 * @brief Bubble sort @p set into XPath document order.
1617 * Context position aware. Unused in the 'Release' build target.
1618 *
1619 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001620 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1621 */
1622static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001623set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001624{
1625 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001626 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001627 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001628 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001629 struct lyxp_set_node item;
1630 struct lyxp_set_hash_node hnode;
1631 uint64_t hash;
1632
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001633 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001634 return 0;
1635 }
1636
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001637 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001638 for (root = set->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001639 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001640
1641 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001642 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001643 return -1;
1644 }
1645
1646 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1647 print_set_debug(set);
1648
1649 for (i = 0; i < set->used; ++i) {
1650 inverted = 0;
1651 change = 0;
1652
1653 for (j = 1; j < set->used - i; ++j) {
1654 /* compare node positions */
1655 if (inverted) {
1656 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1657 } else {
1658 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1659 }
1660
1661 /* swap if needed */
1662 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1663 change = 1;
1664
1665 item = set->val.nodes[j - 1];
1666 set->val.nodes[j - 1] = set->val.nodes[j];
1667 set->val.nodes[j] = item;
1668 } else {
1669 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1670 inverted = !inverted;
1671 }
1672 }
1673
1674 ++ret;
1675
1676 if (!change) {
1677 break;
1678 }
1679 }
1680
1681 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1682 print_set_debug(set);
1683
1684 /* check node hashes */
1685 if (set->used >= LYD_HT_MIN_ITEMS) {
1686 assert(set->ht);
1687 for (i = 0; i < set->used; ++i) {
1688 hnode.node = set->val.nodes[i].node;
1689 hnode.type = set->val.nodes[i].type;
1690
1691 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1692 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1693 hash = dict_hash_multi(hash, NULL, 0);
1694
1695 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1696 }
1697 }
1698
1699 return ret - 1;
1700}
1701
1702/**
1703 * @brief Remove duplicate entries in a sorted node set.
1704 *
1705 * @param[in] set Sorted set to check.
1706 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1707 */
1708static LY_ERR
1709set_sorted_dup_node_clean(struct lyxp_set *set)
1710{
1711 uint32_t i = 0;
1712 LY_ERR ret = LY_SUCCESS;
1713
1714 if (set->used > 1) {
1715 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001716 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1717 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001718 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001719 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001720 }
Michal Vasko57eab132019-09-24 11:46:26 +02001721 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001722 }
1723 }
1724
Michal Vasko2caefc12019-11-14 16:07:56 +01001725 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001726 return ret;
1727}
1728
1729#endif
1730
1731/**
1732 * @brief Merge 2 sorted sets into one.
1733 *
1734 * @param[in,out] trg Set to merge into. Duplicates are removed.
1735 * @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 +02001736 * @return LY_ERR
1737 */
1738static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001739set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001740{
1741 uint32_t i, j, k, count, dup_count;
1742 int cmp;
1743 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001744
Michal Vaskod3678892020-05-21 10:06:58 +02001745 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001746 return LY_EINVAL;
1747 }
1748
Michal Vaskod3678892020-05-21 10:06:58 +02001749 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001750 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001751 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001752 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001753 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001754 return LY_SUCCESS;
1755 }
1756
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001757 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001758 for (root = trg->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001759 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001760
1761 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001762 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001763 return LY_EINT;
1764 }
1765
1766#ifndef NDEBUG
1767 LOGDBG(LY_LDGXPATH, "MERGE target");
1768 print_set_debug(trg);
1769 LOGDBG(LY_LDGXPATH, "MERGE source");
1770 print_set_debug(src);
1771#endif
1772
1773 /* make memory for the merge (duplicates are not detected yet, so space
1774 * will likely be wasted on them, too bad) */
1775 if (trg->size - trg->used < src->used) {
1776 trg->size = trg->used + src->used;
1777
1778 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1779 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1780 }
1781
1782 i = 0;
1783 j = 0;
1784 count = 0;
1785 dup_count = 0;
1786 do {
1787 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1788 if (!cmp) {
1789 if (!count) {
1790 /* duplicate, just skip it */
1791 ++i;
1792 ++j;
1793 } else {
1794 /* we are copying something already, so let's copy the duplicate too,
1795 * we are hoping that afterwards there are some more nodes to
1796 * copy and this way we can copy them all together */
1797 ++count;
1798 ++dup_count;
1799 ++i;
1800 ++j;
1801 }
1802 } else if (cmp < 0) {
1803 /* inserting src node into trg, just remember it for now */
1804 ++count;
1805 ++i;
1806
1807 /* insert the hash now */
1808 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1809 } else if (count) {
1810copy_nodes:
1811 /* time to actually copy the nodes, we have found the largest block of nodes */
1812 memmove(&trg->val.nodes[j + (count - dup_count)],
1813 &trg->val.nodes[j],
1814 (trg->used - j) * sizeof *trg->val.nodes);
1815 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1816
1817 trg->used += count - dup_count;
1818 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1819 j += count - dup_count;
1820
1821 count = 0;
1822 dup_count = 0;
1823 } else {
1824 ++j;
1825 }
1826 } while ((i < src->used) && (j < trg->used));
1827
1828 if ((i < src->used) || count) {
1829 /* insert all the hashes first */
1830 for (k = i; k < src->used; ++k) {
1831 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1832 }
1833
1834 /* loop ended, but we need to copy something at trg end */
1835 count += src->used - i;
1836 i = src->used;
1837 goto copy_nodes;
1838 }
1839
1840 /* we are inserting hashes before the actual node insert, which causes
1841 * situations when there were initially not enough items for a hash table,
1842 * but even after some were inserted, hash table was not created (during
1843 * insertion the number of items is not updated yet) */
1844 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1845 set_insert_node_hash(trg, NULL, 0);
1846 }
1847
1848#ifndef NDEBUG
1849 LOGDBG(LY_LDGXPATH, "MERGE result");
1850 print_set_debug(trg);
1851#endif
1852
Michal Vaskod3678892020-05-21 10:06:58 +02001853 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001854 return LY_SUCCESS;
1855}
1856
1857/*
1858 * (re)parse functions
1859 *
1860 * Parse functions parse the expression into
1861 * tokens (syntactic analysis).
1862 *
1863 * Reparse functions perform semantic analysis
1864 * (do not save the result, just a check) of
1865 * the expression and fill repeat indices.
1866 */
1867
Michal Vasko14676352020-05-29 11:35:55 +02001868LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001869lyxp_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 +02001870{
Michal Vasko004d3152020-06-11 19:59:22 +02001871 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001872 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001873 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001874 }
Michal Vasko14676352020-05-29 11:35:55 +02001875 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001876 }
1877
Michal Vasko004d3152020-06-11 19:59:22 +02001878 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001879 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001880 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001881 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001882 }
Michal Vasko14676352020-05-29 11:35:55 +02001883 return LY_ENOT;
1884 }
1885
1886 return LY_SUCCESS;
1887}
1888
Michal Vasko004d3152020-06-11 19:59:22 +02001889LY_ERR
1890lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1891{
1892 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1893
1894 /* skip the token */
1895 ++(*tok_idx);
1896
1897 return LY_SUCCESS;
1898}
1899
Michal Vasko14676352020-05-29 11:35:55 +02001900/* just like lyxp_check_token() but tests for 2 tokens */
1901static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001902exp_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 +02001903 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001904{
Michal Vasko004d3152020-06-11 19:59:22 +02001905 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001906 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001907 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001908 }
1909 return LY_EINCOMPLETE;
1910 }
1911
Michal Vasko004d3152020-06-11 19:59:22 +02001912 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001913 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001914 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001915 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001916 }
1917 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001918 }
1919
1920 return LY_SUCCESS;
1921}
1922
Michal Vasko4911eeb2021-06-28 11:23:05 +02001923LY_ERR
1924lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok1,
1925 enum lyxp_token want_tok2)
1926{
1927 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
1928
1929 /* skip the token */
1930 ++(*tok_idx);
1931
1932 return LY_SUCCESS;
1933}
1934
Michal Vasko03ff5a72019-09-11 13:49:33 +02001935/**
1936 * @brief Stack operation push on the repeat array.
1937 *
1938 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001939 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001940 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1941 */
1942static void
Michal Vasko004d3152020-06-11 19:59:22 +02001943exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001944{
1945 uint16_t i;
1946
Michal Vasko004d3152020-06-11 19:59:22 +02001947 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001948 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001949 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1950 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1951 exp->repeat[tok_idx][i] = repeat_op_idx;
1952 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001953 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001954 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1955 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1956 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001957 }
1958}
1959
1960/**
1961 * @brief Reparse Predicate. Logs directly on error.
1962 *
1963 * [7] Predicate ::= '[' Expr ']'
1964 *
1965 * @param[in] ctx Context for logging.
1966 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001967 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02001968 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001969 * @return LY_ERR
1970 */
1971static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02001972reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001973{
1974 LY_ERR rc;
1975
Michal Vasko004d3152020-06-11 19:59:22 +02001976 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001977 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001978 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001979
aPiecekbf968d92021-05-27 14:35:05 +02001980 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001981 LY_CHECK_RET(rc);
1982
Michal Vasko004d3152020-06-11 19:59:22 +02001983 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001984 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001985 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001986
1987 return LY_SUCCESS;
1988}
1989
1990/**
1991 * @brief Reparse RelativeLocationPath. Logs directly on error.
1992 *
1993 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1994 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1995 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1996 *
1997 * @param[in] ctx Context for logging.
1998 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001999 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002000 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002001 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
2002 */
2003static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002004reparse_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 +02002005{
2006 LY_ERR rc;
2007
Michal Vasko004d3152020-06-11 19:59:22 +02002008 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002009 LY_CHECK_RET(rc);
2010
2011 goto step;
2012 do {
2013 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002014 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002015
Michal Vasko004d3152020-06-11 19:59:22 +02002016 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002017 LY_CHECK_RET(rc);
2018step:
2019 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002020 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002021 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002022 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002023 break;
2024
2025 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002026 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002027 break;
2028
2029 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002030 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002031
Michal Vasko004d3152020-06-11 19:59:22 +02002032 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002033 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002034 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002035 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 +02002036 return LY_EVALID;
2037 }
Radek Krejci0f969882020-08-21 16:56:47 +02002038 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002039 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002040 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002041 goto reparse_predicate;
2042 break;
2043
2044 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002045 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002046
2047 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002048 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002049 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002050 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002051
2052 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002053 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002054 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002055 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002056
2057reparse_predicate:
2058 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002059 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002060 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002061 LY_CHECK_RET(rc);
2062 }
2063 break;
2064 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002065 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 +02002066 return LY_EVALID;
2067 }
Michal Vasko004d3152020-06-11 19:59:22 +02002068 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002069
2070 return LY_SUCCESS;
2071}
2072
2073/**
2074 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2075 *
2076 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2077 *
2078 * @param[in] ctx Context for logging.
2079 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002080 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002081 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002082 * @return LY_ERR
2083 */
2084static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002085reparse_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 +02002086{
2087 LY_ERR rc;
2088
Michal Vasko004d3152020-06-11 19:59:22 +02002089 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 +02002090
2091 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002092 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002093 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002094 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002095
Michal Vasko004d3152020-06-11 19:59:22 +02002096 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002097 return LY_SUCCESS;
2098 }
Michal Vasko004d3152020-06-11 19:59:22 +02002099 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002100 case LYXP_TOKEN_DOT:
2101 case LYXP_TOKEN_DDOT:
2102 case LYXP_TOKEN_AT:
2103 case LYXP_TOKEN_NAMETEST:
2104 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002105 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002106 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002107 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002108 default:
2109 break;
2110 }
2111
Michal Vasko03ff5a72019-09-11 13:49:33 +02002112 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002113 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002114 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002115
aPiecekbf968d92021-05-27 14:35:05 +02002116 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002117 LY_CHECK_RET(rc);
2118 }
2119
2120 return LY_SUCCESS;
2121}
2122
2123/**
2124 * @brief Reparse FunctionCall. Logs directly on error.
2125 *
2126 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2127 *
2128 * @param[in] ctx Context for logging.
2129 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002130 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002131 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002132 * @return LY_ERR
2133 */
2134static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002135reparse_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 +02002136{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002137 int8_t min_arg_count = -1;
2138 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002139 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002140 LY_ERR rc;
2141
Michal Vasko004d3152020-06-11 19:59:22 +02002142 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002143 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002144 func_tok_idx = *tok_idx;
2145 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002146 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002147 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002148 min_arg_count = 1;
2149 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002150 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002151 min_arg_count = 1;
2152 max_arg_count = 1;
2153 }
2154 break;
2155 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002156 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002157 min_arg_count = 1;
2158 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002159 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002160 min_arg_count = 0;
2161 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002162 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002163 min_arg_count = 0;
2164 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002165 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002166 min_arg_count = 0;
2167 max_arg_count = 0;
2168 }
2169 break;
2170 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002171 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002172 min_arg_count = 1;
2173 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002174 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002175 min_arg_count = 0;
2176 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002177 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002178 min_arg_count = 1;
2179 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002180 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002181 min_arg_count = 1;
2182 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002183 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002184 min_arg_count = 1;
2185 max_arg_count = 1;
2186 }
2187 break;
2188 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002189 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002190 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002191 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002192 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002193 min_arg_count = 0;
2194 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002195 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002196 min_arg_count = 0;
2197 max_arg_count = 1;
2198 }
2199 break;
2200 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002201 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002202 min_arg_count = 1;
2203 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002204 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002205 min_arg_count = 1;
2206 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002207 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002208 min_arg_count = 0;
2209 max_arg_count = 0;
2210 }
2211 break;
2212 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002213 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002214 min_arg_count = 2;
2215 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002216 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002217 min_arg_count = 0;
2218 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002219 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002220 min_arg_count = 2;
2221 max_arg_count = 2;
2222 }
2223 break;
2224 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002225 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002226 min_arg_count = 2;
2227 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002228 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002229 min_arg_count = 3;
2230 max_arg_count = 3;
2231 }
2232 break;
2233 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002234 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002235 min_arg_count = 0;
2236 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002237 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002238 min_arg_count = 1;
2239 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002240 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002241 min_arg_count = 2;
2242 max_arg_count = 2;
2243 }
2244 break;
2245 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002246 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002247 min_arg_count = 2;
2248 max_arg_count = 2;
2249 }
2250 break;
2251 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002252 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002253 min_arg_count = 2;
2254 max_arg_count = 2;
2255 }
2256 break;
2257 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002258 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002259 min_arg_count = 0;
2260 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002261 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002262 min_arg_count = 0;
2263 max_arg_count = 1;
2264 }
2265 break;
2266 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002267 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002268 min_arg_count = 0;
2269 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002270 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002271 min_arg_count = 2;
2272 max_arg_count = 2;
2273 }
2274 break;
2275 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002276 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002277 min_arg_count = 2;
2278 max_arg_count = 2;
2279 }
2280 break;
2281 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002282 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002283 min_arg_count = 2;
2284 max_arg_count = 2;
2285 }
2286 break;
2287 }
2288 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002289 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 +02002290 return LY_EINVAL;
2291 }
Michal Vasko004d3152020-06-11 19:59:22 +02002292 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002293
2294 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002295 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002296 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002297 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002298
2299 /* ( Expr ( ',' Expr )* )? */
2300 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002301 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002302 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002303 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002304 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002305 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002306 LY_CHECK_RET(rc);
2307 }
Michal Vasko004d3152020-06-11 19:59:22 +02002308 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2309 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002310
2311 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002312 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002313 LY_CHECK_RET(rc);
2314 }
2315
2316 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002317 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002318 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002319 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002320
Radek Krejci857189e2020-09-01 13:26:36 +02002321 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002322 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 +02002323 return LY_EVALID;
2324 }
2325
2326 return LY_SUCCESS;
2327}
2328
2329/**
2330 * @brief Reparse PathExpr. Logs directly on error.
2331 *
2332 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2333 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2334 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2335 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2336 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2337 *
2338 * @param[in] ctx Context for logging.
2339 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002340 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002341 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002342 * @return LY_ERR
2343 */
2344static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002345reparse_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 +02002346{
2347 LY_ERR rc;
2348
Michal Vasko004d3152020-06-11 19:59:22 +02002349 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002350 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002351 }
2352
Michal Vasko004d3152020-06-11 19:59:22 +02002353 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002354 case LYXP_TOKEN_PAR1:
2355 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002356 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002357
aPiecekbf968d92021-05-27 14:35:05 +02002358 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002359 LY_CHECK_RET(rc);
2360
Michal Vasko004d3152020-06-11 19:59:22 +02002361 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002362 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002363 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002364 goto predicate;
2365 break;
2366 case LYXP_TOKEN_DOT:
2367 case LYXP_TOKEN_DDOT:
2368 case LYXP_TOKEN_AT:
2369 case LYXP_TOKEN_NAMETEST:
2370 case LYXP_TOKEN_NODETYPE:
2371 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002372 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002373 LY_CHECK_RET(rc);
2374 break;
2375 case LYXP_TOKEN_FUNCNAME:
2376 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002377 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002378 LY_CHECK_RET(rc);
2379 goto predicate;
2380 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002381 case LYXP_TOKEN_OPER_PATH:
2382 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002383 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002384 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002385 LY_CHECK_RET(rc);
2386 break;
2387 case LYXP_TOKEN_LITERAL:
2388 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002389 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002390 goto predicate;
2391 break;
2392 case LYXP_TOKEN_NUMBER:
2393 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002394 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002395 goto predicate;
2396 break;
2397 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002398 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 +02002399 return LY_EVALID;
2400 }
2401
2402 return LY_SUCCESS;
2403
2404predicate:
2405 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002406 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002407 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002408 LY_CHECK_RET(rc);
2409 }
2410
2411 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002412 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002413
2414 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002415 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002416
aPiecekbf968d92021-05-27 14:35:05 +02002417 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002418 LY_CHECK_RET(rc);
2419 }
2420
2421 return LY_SUCCESS;
2422}
2423
2424/**
2425 * @brief Reparse UnaryExpr. Logs directly on error.
2426 *
2427 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2428 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2429 *
2430 * @param[in] ctx Context for logging.
2431 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002432 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002433 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002434 * @return LY_ERR
2435 */
2436static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002437reparse_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 +02002438{
2439 uint16_t prev_exp;
2440 LY_ERR rc;
2441
2442 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002443 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002444 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2445 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002446 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002447 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002448 }
2449
2450 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002451 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002452 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002453 LY_CHECK_RET(rc);
2454
2455 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002456 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002457 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002458 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002459
aPiecekbf968d92021-05-27 14:35:05 +02002460 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002461 LY_CHECK_RET(rc);
2462 }
2463
2464 return LY_SUCCESS;
2465}
2466
2467/**
2468 * @brief Reparse AdditiveExpr. Logs directly on error.
2469 *
2470 * [15] AdditiveExpr ::= MultiplicativeExpr
2471 * | AdditiveExpr '+' MultiplicativeExpr
2472 * | AdditiveExpr '-' MultiplicativeExpr
2473 * [16] MultiplicativeExpr ::= UnaryExpr
2474 * | MultiplicativeExpr '*' UnaryExpr
2475 * | MultiplicativeExpr 'div' UnaryExpr
2476 * | MultiplicativeExpr 'mod' UnaryExpr
2477 *
2478 * @param[in] ctx Context for logging.
2479 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002480 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002481 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002482 * @return LY_ERR
2483 */
2484static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002485reparse_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 +02002486{
2487 uint16_t prev_add_exp, prev_mul_exp;
2488 LY_ERR rc;
2489
Michal Vasko004d3152020-06-11 19:59:22 +02002490 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002491 goto reparse_multiplicative_expr;
2492
2493 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002494 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2495 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002496 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002497 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002498
2499reparse_multiplicative_expr:
2500 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002501 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002502 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002503 LY_CHECK_RET(rc);
2504
2505 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002506 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2507 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002508 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002509 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002510
aPiecekbf968d92021-05-27 14:35:05 +02002511 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002512 LY_CHECK_RET(rc);
2513 }
2514 }
2515
2516 return LY_SUCCESS;
2517}
2518
2519/**
2520 * @brief Reparse EqualityExpr. Logs directly on error.
2521 *
2522 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2523 * | EqualityExpr '!=' RelationalExpr
2524 * [14] RelationalExpr ::= AdditiveExpr
2525 * | RelationalExpr '<' AdditiveExpr
2526 * | RelationalExpr '>' AdditiveExpr
2527 * | RelationalExpr '<=' AdditiveExpr
2528 * | RelationalExpr '>=' AdditiveExpr
2529 *
2530 * @param[in] ctx Context for logging.
2531 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002532 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002533 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002534 * @return LY_ERR
2535 */
2536static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002537reparse_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 +02002538{
2539 uint16_t prev_eq_exp, prev_rel_exp;
2540 LY_ERR rc;
2541
Michal Vasko004d3152020-06-11 19:59:22 +02002542 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002543 goto reparse_additive_expr;
2544
2545 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002546 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002547 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002548 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002549
2550reparse_additive_expr:
2551 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002552 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002553 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002554 LY_CHECK_RET(rc);
2555
2556 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002557 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002558 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002559 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002560
aPiecekbf968d92021-05-27 14:35:05 +02002561 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002562 LY_CHECK_RET(rc);
2563 }
2564 }
2565
2566 return LY_SUCCESS;
2567}
2568
2569/**
2570 * @brief Reparse OrExpr. Logs directly on error.
2571 *
2572 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2573 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2574 *
2575 * @param[in] ctx Context for logging.
2576 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002577 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002578 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002579 * @return LY_ERR
2580 */
2581static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002582reparse_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 +02002583{
2584 uint16_t prev_or_exp, prev_and_exp;
2585 LY_ERR rc;
2586
aPiecekbf968d92021-05-27 14:35:05 +02002587 ++depth;
2588 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2589
Michal Vasko004d3152020-06-11 19:59:22 +02002590 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002591 goto reparse_equality_expr;
2592
2593 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002594 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 +02002595 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002596 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002597
2598reparse_equality_expr:
2599 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002600 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002601 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002602 LY_CHECK_RET(rc);
2603
2604 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002605 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 +02002606 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002607 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002608
aPiecekbf968d92021-05-27 14:35:05 +02002609 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002610 LY_CHECK_RET(rc);
2611 }
2612 }
2613
2614 return LY_SUCCESS;
2615}
Radek Krejcib1646a92018-11-02 16:08:26 +01002616
2617/**
2618 * @brief Parse NCName.
2619 *
2620 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002621 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002622 */
Radek Krejcid4270262019-01-07 15:07:25 +01002623static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002624parse_ncname(const char *ncname)
2625{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002626 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002627 size_t size;
2628 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002629
2630 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2631 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2632 return len;
2633 }
2634
2635 do {
2636 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002637 if (!*ncname) {
2638 break;
2639 }
Radek Krejcid4270262019-01-07 15:07:25 +01002640 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002641 } while (is_xmlqnamechar(uc) && (uc != ':'));
2642
2643 return len;
2644}
2645
2646/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002647 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002648 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002649 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002650 * @param[in] exp Expression to use.
2651 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002652 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002653 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002654 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002655 */
2656static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002657exp_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 +01002658{
2659 uint32_t prev;
2660
2661 if (exp->used == exp->size) {
2662 prev = exp->size;
2663 exp->size += LYXP_EXPR_SIZE_STEP;
2664 if (prev > exp->size) {
2665 LOGINT(ctx);
2666 return LY_EINT;
2667 }
2668
2669 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2670 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2671 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2672 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2673 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2674 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2675 }
2676
2677 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002678 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002679 exp->tok_len[exp->used] = tok_len;
2680 ++exp->used;
2681 return LY_SUCCESS;
2682}
2683
2684void
Michal Vasko14676352020-05-29 11:35:55 +02002685lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002686{
2687 uint16_t i;
2688
2689 if (!expr) {
2690 return;
2691 }
2692
2693 lydict_remove(ctx, expr->expr);
2694 free(expr->tokens);
2695 free(expr->tok_pos);
2696 free(expr->tok_len);
2697 if (expr->repeat) {
2698 for (i = 0; i < expr->used; ++i) {
2699 free(expr->repeat[i]);
2700 }
2701 }
2702 free(expr->repeat);
2703 free(expr);
2704}
2705
Radek Krejcif03a9e22020-09-18 20:09:31 +02002706LY_ERR
2707lyxp_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 +01002708{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002709 LY_ERR ret = LY_SUCCESS;
2710 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002711 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002712 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002713 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002714 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002715
Radek Krejcif03a9e22020-09-18 20:09:31 +02002716 assert(expr_p);
2717
2718 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002719 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002720 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002721 }
2722
2723 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002724 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002725 }
2726 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002727 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002728 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002729 }
2730
2731 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002732 expr = calloc(1, sizeof *expr);
2733 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2734 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2735 expr->used = 0;
2736 expr->size = LYXP_EXPR_SIZE_START;
2737 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2738 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002739
Radek Krejcif03a9e22020-09-18 20:09:31 +02002740 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2741 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002742
Radek Krejcif03a9e22020-09-18 20:09:31 +02002743 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2744 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002745
Michal Vasko004d3152020-06-11 19:59:22 +02002746 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002747 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002748
Radek Krejcif03a9e22020-09-18 20:09:31 +02002749 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002750 ++parsed;
2751 }
2752
2753 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002754 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002755
2756 /* '(' */
2757 tok_len = 1;
2758 tok_type = LYXP_TOKEN_PAR1;
2759
Radek Krejcif03a9e22020-09-18 20:09:31 +02002760 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002761 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002762 if (((expr->tok_len[expr->used - 1] == 4) &&
2763 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2764 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2765 ((expr->tok_len[expr->used - 1] == 7) &&
2766 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002767 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002768 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002769 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002770 }
2771 prev_function_check = 0;
2772 }
2773
Radek Krejcif03a9e22020-09-18 20:09:31 +02002774 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002775
2776 /* ')' */
2777 tok_len = 1;
2778 tok_type = LYXP_TOKEN_PAR2;
2779
Radek Krejcif03a9e22020-09-18 20:09:31 +02002780 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002781
2782 /* '[' */
2783 tok_len = 1;
2784 tok_type = LYXP_TOKEN_BRACK1;
2785
Radek Krejcif03a9e22020-09-18 20:09:31 +02002786 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002787
2788 /* ']' */
2789 tok_len = 1;
2790 tok_type = LYXP_TOKEN_BRACK2;
2791
Radek Krejcif03a9e22020-09-18 20:09:31 +02002792 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002793
2794 /* '..' */
2795 tok_len = 2;
2796 tok_type = LYXP_TOKEN_DDOT;
2797
Radek Krejcif03a9e22020-09-18 20:09:31 +02002798 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002799
2800 /* '.' */
2801 tok_len = 1;
2802 tok_type = LYXP_TOKEN_DOT;
2803
Radek Krejcif03a9e22020-09-18 20:09:31 +02002804 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002805
2806 /* '@' */
2807 tok_len = 1;
2808 tok_type = LYXP_TOKEN_AT;
2809
Radek Krejcif03a9e22020-09-18 20:09:31 +02002810 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002811
2812 /* ',' */
2813 tok_len = 1;
2814 tok_type = LYXP_TOKEN_COMMA;
2815
Radek Krejcif03a9e22020-09-18 20:09:31 +02002816 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002817
2818 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002819 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2820 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002821 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002822 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002823 ++tok_len;
2824 tok_type = LYXP_TOKEN_LITERAL;
2825
Radek Krejcif03a9e22020-09-18 20:09:31 +02002826 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002827
2828 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002829 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2830 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002831 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002832 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002833 ++tok_len;
2834 tok_type = LYXP_TOKEN_LITERAL;
2835
Radek Krejcif03a9e22020-09-18 20:09:31 +02002836 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002837
2838 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002839 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2840 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002841 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002842 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002843 }
2844 tok_type = LYXP_TOKEN_NUMBER;
2845
Radek Krejcif03a9e22020-09-18 20:09:31 +02002846 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002847
2848 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002849 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002850 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002851 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002852 } else {
2853 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002854 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002855 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002856
Radek Krejcif03a9e22020-09-18 20:09:31 +02002857 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002858
Michal Vasko3e48bf32020-06-01 08:39:07 +02002859 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002860 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002861 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2862
Radek Krejcif03a9e22020-09-18 20:09:31 +02002863 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002864
2865 /* Operator '<=', '>=' */
2866 tok_len = 2;
2867 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002868
Radek Krejcif03a9e22020-09-18 20:09:31 +02002869 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002870
2871 /* Operator '|' */
2872 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002873 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002874
Radek Krejcif03a9e22020-09-18 20:09:31 +02002875 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002876
2877 /* Operator '+', '-' */
2878 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002879 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002880
Radek Krejcif03a9e22020-09-18 20:09:31 +02002881 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002882
Michal Vasko3e48bf32020-06-01 08:39:07 +02002883 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002884 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002885 tok_type = LYXP_TOKEN_OPER_EQUAL;
2886
Radek Krejcif03a9e22020-09-18 20:09:31 +02002887 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002888
2889 /* Operator '<', '>' */
2890 tok_len = 1;
2891 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002892
Michal Vasko69730152020-10-09 16:30:07 +02002893 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2894 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2895 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2896 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2897 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2898 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2899 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2900 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2901 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2902 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2903 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2904 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002905
2906 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002907 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002908 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002909 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002910
Radek Krejcif03a9e22020-09-18 20:09:31 +02002911 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002912 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002913 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002914
Radek Krejcif03a9e22020-09-18 20:09:31 +02002915 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002916 tok_len = 3;
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], "mod", 3) || !strncmp(&expr_str[parsed], "div", 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_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002922
2923 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002924 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 +02002925 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 +02002926 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002927 goto error;
2928 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02002929 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002930 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002931 goto error;
2932 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002933 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002934
2935 /* NameTest '*' */
2936 tok_len = 1;
2937 tok_type = LYXP_TOKEN_NAMETEST;
2938
2939 } else {
2940
2941 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002942 long int ncname_len = parse_ncname(&expr_str[parsed]);
Michal Vasko774ce402021-04-14 15:35:06 +02002943 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2944 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002945 tok_len = ncname_len;
2946
Radek Krejcif03a9e22020-09-18 20:09:31 +02002947 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002948 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002949 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002950 ++tok_len;
2951 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002952 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vasko774ce402021-04-14 15:35:06 +02002953 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2954 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002955 tok_len += ncname_len;
2956 }
2957 /* remove old flag to prevent ambiguities */
2958 prev_function_check = 0;
2959 tok_type = LYXP_TOKEN_NAMETEST;
2960 } else {
2961 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2962 prev_function_check = 1;
2963 tok_type = LYXP_TOKEN_NAMETEST;
2964 }
2965 }
2966
2967 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002968 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002969 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002970 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002971 ++parsed;
2972 }
2973
Radek Krejcif03a9e22020-09-18 20:09:31 +02002974 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002975
Michal Vasko004d3152020-06-11 19:59:22 +02002976 if (reparse) {
2977 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002978 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2979 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002980
Michal Vasko004d3152020-06-11 19:59:22 +02002981 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02002982 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002983 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002984 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002985 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002986 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002987 goto error;
2988 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002989 }
2990
Radek Krejcif03a9e22020-09-18 20:09:31 +02002991 print_expr_struct_debug(expr);
2992 *expr_p = expr;
2993 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002994
2995error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002996 lyxp_expr_free(ctx, expr);
2997 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002998}
2999
Michal Vasko1734be92020-09-22 08:55:10 +02003000LY_ERR
3001lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003002{
Michal Vasko1734be92020-09-22 08:55:10 +02003003 LY_ERR ret = LY_SUCCESS;
3004 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003005 uint32_t i, j;
3006
Michal Vasko7f45cf22020-10-01 12:49:44 +02003007 if (!exp) {
3008 goto cleanup;
3009 }
3010
Michal Vasko004d3152020-06-11 19:59:22 +02003011 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003012 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003013
Michal Vasko08e9b112021-06-11 15:41:17 +02003014 if (exp->used) {
3015 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
3016 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3017 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003018
Michal Vasko08e9b112021-06-11 15:41:17 +02003019 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
3020 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3021 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003022
Michal Vasko08e9b112021-06-11 15:41:17 +02003023 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
3024 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3025 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003026
Michal Vasko08e9b112021-06-11 15:41:17 +02003027 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
3028 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3029 for (i = 0; i < exp->used; ++i) {
3030 if (!exp->repeat[i]) {
3031 dup->repeat[i] = NULL;
3032 } else {
3033 for (j = 0; exp->repeat[i][j]; ++j) {}
3034 /* the ending 0 as well */
3035 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003036
Michal Vasko08e9b112021-06-11 15:41:17 +02003037 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
3038 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3039 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3040 dup->repeat[i][j - 1] = 0;
3041 }
Michal Vasko004d3152020-06-11 19:59:22 +02003042 }
3043 }
3044
3045 dup->used = exp->used;
3046 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003047 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003048
Michal Vasko1734be92020-09-22 08:55:10 +02003049cleanup:
3050 if (ret) {
3051 lyxp_expr_free(ctx, dup);
3052 } else {
3053 *dup_p = dup;
3054 }
3055 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003056}
3057
Michal Vasko03ff5a72019-09-11 13:49:33 +02003058/*
3059 * warn functions
3060 *
3061 * Warn functions check specific reasonable conditions for schema XPath
3062 * and print a warning if they are not satisfied.
3063 */
3064
3065/**
3066 * @brief Get the last-added schema node that is currently in the context.
3067 *
3068 * @param[in] set Set to search in.
3069 * @return Last-added schema context node, NULL if no node is in context.
3070 */
3071static struct lysc_node *
3072warn_get_scnode_in_ctx(struct lyxp_set *set)
3073{
3074 uint32_t i;
3075
3076 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3077 return NULL;
3078 }
3079
3080 i = set->used;
3081 do {
3082 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003083 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003084 /* if there are more, simply return the first found (last added) */
3085 return set->val.scnodes[i].scnode;
3086 }
3087 } while (i);
3088
3089 return NULL;
3090}
3091
3092/**
3093 * @brief Test whether a type is numeric - integer type or decimal64.
3094 *
3095 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003096 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003097 */
Radek Krejci857189e2020-09-01 13:26:36 +02003098static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003099warn_is_numeric_type(struct lysc_type *type)
3100{
3101 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003102 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003103 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003104
3105 switch (type->basetype) {
3106 case LY_TYPE_DEC64:
3107 case LY_TYPE_INT8:
3108 case LY_TYPE_UINT8:
3109 case LY_TYPE_INT16:
3110 case LY_TYPE_UINT16:
3111 case LY_TYPE_INT32:
3112 case LY_TYPE_UINT32:
3113 case LY_TYPE_INT64:
3114 case LY_TYPE_UINT64:
3115 return 1;
3116 case LY_TYPE_UNION:
3117 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003118 LY_ARRAY_FOR(uni->types, u) {
3119 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003120 if (ret) {
3121 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003122 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003123 }
3124 }
3125 /* did not find any suitable type */
3126 return 0;
3127 case LY_TYPE_LEAFREF:
3128 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3129 default:
3130 return 0;
3131 }
3132}
3133
3134/**
3135 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3136 *
3137 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003138 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003139 */
Radek Krejci857189e2020-09-01 13:26:36 +02003140static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003141warn_is_string_type(struct lysc_type *type)
3142{
3143 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003144 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003145 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003146
3147 switch (type->basetype) {
3148 case LY_TYPE_BITS:
3149 case LY_TYPE_ENUM:
3150 case LY_TYPE_IDENT:
3151 case LY_TYPE_INST:
3152 case LY_TYPE_STRING:
3153 return 1;
3154 case LY_TYPE_UNION:
3155 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003156 LY_ARRAY_FOR(uni->types, u) {
3157 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003158 if (ret) {
3159 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003160 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003161 }
3162 }
3163 /* did not find any suitable type */
3164 return 0;
3165 case LY_TYPE_LEAFREF:
3166 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3167 default:
3168 return 0;
3169 }
3170}
3171
3172/**
3173 * @brief Test whether a type is one specific type.
3174 *
3175 * @param[in] type Type to test.
3176 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003177 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003178 */
Radek Krejci857189e2020-09-01 13:26:36 +02003179static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003180warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3181{
3182 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003183 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003184 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003185
3186 if (type->basetype == base) {
3187 return 1;
3188 } else if (type->basetype == LY_TYPE_UNION) {
3189 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003190 LY_ARRAY_FOR(uni->types, u) {
3191 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003192 if (ret) {
3193 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003194 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003195 }
3196 }
3197 /* did not find any suitable type */
3198 return 0;
3199 } else if (type->basetype == LY_TYPE_LEAFREF) {
3200 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3201 }
3202
3203 return 0;
3204}
3205
3206/**
3207 * @brief Get next type of a (union) type.
3208 *
3209 * @param[in] type Base type.
3210 * @param[in] prev_type Previously returned type.
3211 * @return Next type or NULL.
3212 */
3213static struct lysc_type *
3214warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3215{
3216 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003217 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003218 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003219
3220 switch (type->basetype) {
3221 case LY_TYPE_UNION:
3222 uni = (struct lysc_type_union *)type;
3223 if (!prev_type) {
3224 return uni->types[0];
3225 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003226 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003227 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003228 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003229 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003230 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003231 found = 1;
3232 }
3233 }
3234 return NULL;
3235 default:
3236 if (prev_type) {
3237 assert(type == prev_type);
3238 return NULL;
3239 } else {
3240 return type;
3241 }
3242 }
3243}
3244
3245/**
3246 * @brief Test whether 2 types have a common type.
3247 *
3248 * @param[in] type1 First type.
3249 * @param[in] type2 Second type.
3250 * @return 1 if they do, 0 otherwise.
3251 */
3252static int
3253warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3254{
3255 struct lysc_type *t1, *rt1, *t2, *rt2;
3256
3257 t1 = NULL;
3258 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3259 if (t1->basetype == LY_TYPE_LEAFREF) {
3260 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3261 } else {
3262 rt1 = t1;
3263 }
3264
3265 t2 = NULL;
3266 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3267 if (t2->basetype == LY_TYPE_LEAFREF) {
3268 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3269 } else {
3270 rt2 = t2;
3271 }
3272
3273 if (rt2->basetype == rt1->basetype) {
3274 /* match found */
3275 return 1;
3276 }
3277 }
3278 }
3279
3280 return 0;
3281}
3282
3283/**
3284 * @brief Check both operands of comparison operators.
3285 *
3286 * @param[in] ctx Context for errors.
3287 * @param[in] set1 First operand set.
3288 * @param[in] set2 Second operand set.
3289 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3290 * @param[in] expr Start of the expression to print with the warning.
3291 * @param[in] tok_pos Token position.
3292 */
3293static void
Radek Krejci857189e2020-09-01 13:26:36 +02003294warn_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 +02003295{
3296 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003297 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003298
3299 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3300 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3301
3302 if (!node1 && !node2) {
3303 /* no node-sets involved, nothing to do */
3304 return;
3305 }
3306
3307 if (node1) {
3308 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3309 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3310 warning = 1;
3311 leaves = 0;
3312 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3313 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3314 warning = 1;
3315 }
3316 }
3317
3318 if (node2) {
3319 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3320 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3321 warning = 1;
3322 leaves = 0;
3323 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3324 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3325 warning = 1;
3326 }
3327 }
3328
3329 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003330 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3331 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3332 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3333 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003334 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3335 warning = 1;
3336 }
3337 }
3338
3339 if (warning) {
3340 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3341 }
3342}
3343
3344/**
3345 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3346 *
3347 * @param[in] exp Parsed XPath expression.
3348 * @param[in] set Set with the leaf/leaf-list.
3349 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3350 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3351 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3352 */
3353static void
Michal Vasko40308e72020-10-20 16:38:40 +02003354warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3355 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003356{
3357 struct lysc_node *scnode;
3358 struct lysc_type *type;
3359 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003360 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003361 LY_ERR rc;
3362 struct ly_err_item *err = NULL;
3363
Michal Vasko69730152020-10-09 16:30:07 +02003364 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3365 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003366 /* check that the node can have the specified value */
3367 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3368 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3369 } else {
3370 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3371 }
3372 if (!value) {
3373 LOGMEM(set->ctx);
3374 return;
3375 }
3376
3377 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3378 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003379 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003380 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003381 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003382 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003383 }
3384
3385 type = ((struct lysc_node_leaf *)scnode)->type;
3386 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003387 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003388 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003389 if (rc == LY_EINCOMPLETE) {
3390 rc = LY_SUCCESS;
3391 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003392
3393 if (err) {
3394 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3395 ly_err_free(err);
3396 } else if (rc != LY_SUCCESS) {
3397 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3398 }
3399 if (rc != LY_SUCCESS) {
3400 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003401 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003402 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003403 } else {
3404 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003405 }
3406 }
3407 free(value);
3408 }
3409}
3410
3411/*
3412 * XPath functions
3413 */
3414
3415/**
3416 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3417 * depending on whether the first node bit value from the second argument is set.
3418 *
3419 * @param[in] args Array of arguments.
3420 * @param[in] arg_count Count of elements in @p args.
3421 * @param[in,out] set Context and result set at the same time.
3422 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003423 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003424 */
3425static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003426xpath_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 +02003427{
3428 struct lyd_node_term *leaf;
3429 struct lysc_node_leaf *sleaf;
3430 struct lysc_type_bits *bits;
3431 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003432 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003433
3434 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003435 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003436 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003437 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3438 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3439 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3440 sleaf->name);
3441 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3442 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3443 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003444 }
3445
3446 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3447 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003448 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3449 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003450 } else if (!warn_is_string_type(sleaf->type)) {
3451 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003452 }
3453 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003454 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003455 return rc;
3456 }
3457
Michal Vaskod3678892020-05-21 10:06:58 +02003458 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003459 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 +02003460 return LY_EVALID;
3461 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003462 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003463 LY_CHECK_RET(rc);
3464
3465 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003466 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003467 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003468 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3469 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003470 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003471 LY_ARRAY_FOR(bits->bits, u) {
3472 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003473 set_fill_boolean(set, 1);
3474 break;
3475 }
3476 }
3477 }
3478 }
3479
3480 return LY_SUCCESS;
3481}
3482
3483/**
3484 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3485 * with the argument converted to boolean.
3486 *
3487 * @param[in] args Array of arguments.
3488 * @param[in] arg_count Count of elements in @p args.
3489 * @param[in,out] set Context and result set at the same time.
3490 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003491 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003492 */
3493static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003494xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003495{
3496 LY_ERR rc;
3497
3498 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003499 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003500 return LY_SUCCESS;
3501 }
3502
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003503 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003504 LY_CHECK_RET(rc);
3505 set_fill_set(set, args[0]);
3506
3507 return LY_SUCCESS;
3508}
3509
3510/**
3511 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3512 * with the first argument rounded up to the nearest integer.
3513 *
3514 * @param[in] args Array of arguments.
3515 * @param[in] arg_count Count of elements in @p args.
3516 * @param[in,out] set Context and result set at the same time.
3517 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003518 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003519 */
3520static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003521xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003522{
3523 struct lysc_node_leaf *sleaf;
3524 LY_ERR rc = LY_SUCCESS;
3525
3526 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003527 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003528 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003529 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3530 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3531 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3532 sleaf->name);
3533 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3534 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3535 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003536 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003537 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003538 return rc;
3539 }
3540
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003541 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003542 LY_CHECK_RET(rc);
3543 if ((long long)args[0]->val.num != args[0]->val.num) {
3544 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3545 } else {
3546 set_fill_number(set, args[0]->val.num);
3547 }
3548
3549 return LY_SUCCESS;
3550}
3551
3552/**
3553 * @brief Execute the XPath concat(string, string, string*) function.
3554 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3555 *
3556 * @param[in] args Array of arguments.
3557 * @param[in] arg_count Count of elements in @p args.
3558 * @param[in,out] set Context and result set at the same time.
3559 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003560 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003561 */
3562static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003563xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003564{
3565 uint16_t i;
3566 char *str = NULL;
3567 size_t used = 1;
3568 LY_ERR rc = LY_SUCCESS;
3569 struct lysc_node_leaf *sleaf;
3570
3571 if (options & LYXP_SCNODE_ALL) {
3572 for (i = 0; i < arg_count; ++i) {
3573 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3574 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3575 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003576 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003577 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003578 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 +02003579 }
3580 }
3581 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003582 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003583 return rc;
3584 }
3585
3586 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003587 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003588 if (rc != LY_SUCCESS) {
3589 free(str);
3590 return rc;
3591 }
3592
3593 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3594 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3595 strcpy(str + used - 1, args[i]->val.str);
3596 used += strlen(args[i]->val.str);
3597 }
3598
3599 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003600 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003601 set->type = LYXP_SET_STRING;
3602 set->val.str = str;
3603
3604 return LY_SUCCESS;
3605}
3606
3607/**
3608 * @brief Execute the XPath contains(string, string) function.
3609 * Returns LYXP_SET_BOOLEAN whether the second argument can
3610 * be found in the first or not.
3611 *
3612 * @param[in] args Array of arguments.
3613 * @param[in] arg_count Count of elements in @p args.
3614 * @param[in,out] set Context and result set at the same time.
3615 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003616 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003617 */
3618static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003619xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003620{
3621 struct lysc_node_leaf *sleaf;
3622 LY_ERR rc = LY_SUCCESS;
3623
3624 if (options & LYXP_SCNODE_ALL) {
3625 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3626 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003627 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3628 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003629 } else if (!warn_is_string_type(sleaf->type)) {
3630 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003631 }
3632 }
3633
3634 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3635 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003636 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3637 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003638 } else if (!warn_is_string_type(sleaf->type)) {
3639 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003640 }
3641 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003642 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003643 return rc;
3644 }
3645
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003646 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003647 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003648 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003649 LY_CHECK_RET(rc);
3650
3651 if (strstr(args[0]->val.str, args[1]->val.str)) {
3652 set_fill_boolean(set, 1);
3653 } else {
3654 set_fill_boolean(set, 0);
3655 }
3656
3657 return LY_SUCCESS;
3658}
3659
3660/**
3661 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3662 * with the size of the node-set from the argument.
3663 *
3664 * @param[in] args Array of arguments.
3665 * @param[in] arg_count Count of elements in @p args.
3666 * @param[in,out] set Context and result set at the same time.
3667 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003668 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003669 */
3670static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003671xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003672{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003673 LY_ERR rc = LY_SUCCESS;
3674
3675 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003676 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003677 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003678 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003679 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003680 return rc;
3681 }
3682
Michal Vasko03ff5a72019-09-11 13:49:33 +02003683 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003684 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003685 return LY_EVALID;
3686 }
3687
3688 set_fill_number(set, args[0]->used);
3689 return LY_SUCCESS;
3690}
3691
3692/**
3693 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3694 * with the context with the intial node.
3695 *
3696 * @param[in] args Array of arguments.
3697 * @param[in] arg_count Count of elements in @p args.
3698 * @param[in,out] set Context and result set at the same time.
3699 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003700 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003701 */
3702static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003703xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003704{
3705 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003706 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003707 return LY_EVALID;
3708 }
3709
3710 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003711 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003712
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003713 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003714 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003715 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003716
3717 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003718 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003719 }
3720
3721 return LY_SUCCESS;
3722}
3723
3724/**
3725 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3726 * leafref or instance-identifier target node(s).
3727 *
3728 * @param[in] args Array of arguments.
3729 * @param[in] arg_count Count of elements in @p args.
3730 * @param[in,out] set Context and result set at the same time.
3731 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003732 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003733 */
3734static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003735xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003736{
3737 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003738 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003739 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003740 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003741 struct ly_path *p;
3742 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003743 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003744 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02003745 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003746
3747 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003748 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003749 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003750 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3751 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3752 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3753 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02003754 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
3755 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003756 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3757 __func__, sleaf->name);
3758 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003759 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003760 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko42e497c2020-01-06 08:38:25 +01003761 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003762 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003763 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003764
3765 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02003766 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vaskoed725d72021-06-23 12:03:45 +02003767 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, NULL, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02003768 if (!r) {
3769 /* get the target node */
3770 target = p[LY_ARRAY_COUNT(p) - 1].node;
3771 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02003772
Michal Vasko741bb562021-06-24 11:59:50 +02003773 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
3774 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003775 }
3776
Michal Vasko741bb562021-06-24 11:59:50 +02003777 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003778 }
3779
Michal Vaskod3678892020-05-21 10:06:58 +02003780 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003781 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003782 return LY_EVALID;
3783 }
3784
Michal Vaskod3678892020-05-21 10:06:58 +02003785 lyxp_set_free_content(set);
3786 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003787 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3788 sleaf = (struct lysc_node_leaf *)leaf->schema;
3789 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3790 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3791 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02003792 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01003793 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003794 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003795 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003796 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003797 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003798 } else {
3799 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003800 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003801 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003802 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003803 return LY_EVALID;
3804 }
3805 }
Michal Vasko004d3152020-06-11 19:59:22 +02003806
3807 /* insert it */
3808 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003809 }
3810 }
3811
3812 return LY_SUCCESS;
3813}
3814
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003815static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003816xpath_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 +02003817{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01003818 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003819 LY_ARRAY_COUNT_TYPE u;
3820 struct lyd_node_term *leaf;
3821 struct lysc_node_leaf *sleaf;
3822 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02003823 struct lyd_value *val;
3824 const struct lys_module *mod;
3825 const char *id_name;
3826 uint16_t id_len;
3827 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003828 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003829 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003830
3831 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003832 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003833 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003834 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3835 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3836 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3837 sleaf->name);
3838 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3839 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3840 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003841 }
3842
3843 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3844 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3845 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003846 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003847 } else if (!warn_is_string_type(sleaf->type)) {
3848 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3849 }
3850 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003851 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003852 return rc;
3853 }
3854
3855 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003856 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 +02003857 return LY_EVALID;
3858 }
3859 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3860 LY_CHECK_RET(rc);
3861
Michal Vasko93923692021-05-07 15:28:02 +02003862 /* parse the identity */
3863 id_name = args[1]->val.str;
3864 id_len = strlen(id_name);
3865 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
3866 LY_CHECK_RET(rc);
3867 if (!mod) {
3868 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
3869 return LY_EVALID;
3870 }
3871
3872 /* find the identity */
3873 found = 0;
3874 LY_ARRAY_FOR(mod->identities, u) {
3875 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
3876 /* we have match */
3877 found = 1;
3878 break;
3879 }
3880 }
3881 if (!found) {
3882 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
3883 return LY_EVALID;
3884 }
3885 id = &mod->identities[u];
3886
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003887 set_fill_boolean(set, 0);
3888 found = 0;
3889 for (i = 0; i < args[0]->used; ++i) {
3890 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3891 continue;
3892 }
3893
3894 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3895 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3896 sleaf = (struct lysc_node_leaf *)leaf->schema;
3897 val = &leaf->value;
3898 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3899 /* uninteresting */
3900 continue;
3901 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003902 } else {
3903 meta = args[0]->val.meta[i].meta;
3904 val = &meta->value;
3905 if (val->realtype->basetype != LY_TYPE_IDENT) {
3906 /* uninteresting */
3907 continue;
3908 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003909 }
3910
Michal Vasko93923692021-05-07 15:28:02 +02003911 /* check the identity itself */
3912 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003913 set_fill_boolean(set, 1);
3914 found = 1;
3915 }
Michal Vasko93923692021-05-07 15:28:02 +02003916 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
3917 set_fill_boolean(set, 1);
3918 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003919 }
3920
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003921 if (found) {
3922 break;
3923 }
3924 }
3925
3926 return LY_SUCCESS;
3927}
3928
Michal Vasko03ff5a72019-09-11 13:49:33 +02003929/**
3930 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3931 * on whether the first argument nodes contain a node of an identity derived from the second
3932 * argument identity.
3933 *
3934 * @param[in] args Array of arguments.
3935 * @param[in] arg_count Count of elements in @p args.
3936 * @param[in,out] set Context and result set at the same time.
3937 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003938 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003939 */
3940static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003941xpath_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 +02003942{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003943 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003944}
3945
3946/**
3947 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3948 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3949 * the second argument identity.
3950 *
3951 * @param[in] args Array of arguments.
3952 * @param[in] arg_count Count of elements in @p args.
3953 * @param[in,out] set Context and result set at the same time.
3954 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003955 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003956 */
3957static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003958xpath_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 +02003959{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003960 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003961}
3962
3963/**
3964 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3965 * with the integer value of the first node's enum value, otherwise NaN.
3966 *
3967 * @param[in] args Array of arguments.
3968 * @param[in] arg_count Count of elements in @p args.
3969 * @param[in,out] set Context and result set at the same time.
3970 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003971 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003972 */
3973static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003974xpath_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 +02003975{
3976 struct lyd_node_term *leaf;
3977 struct lysc_node_leaf *sleaf;
3978 LY_ERR rc = LY_SUCCESS;
3979
3980 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003981 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003982 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003983 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3984 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3985 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3986 sleaf->name);
3987 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3988 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
3989 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003990 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003991 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003992 return rc;
3993 }
3994
Michal Vaskod3678892020-05-21 10:06:58 +02003995 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003996 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003997 return LY_EVALID;
3998 }
3999
4000 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004001 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004002 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4003 sleaf = (struct lysc_node_leaf *)leaf->schema;
4004 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4005 set_fill_number(set, leaf->value.enum_item->value);
4006 }
4007 }
4008
4009 return LY_SUCCESS;
4010}
4011
4012/**
4013 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4014 * with false value.
4015 *
4016 * @param[in] args Array of arguments.
4017 * @param[in] arg_count Count of elements in @p args.
4018 * @param[in,out] set Context and result set at the same time.
4019 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004020 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004021 */
4022static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004023xpath_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 +02004024{
4025 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004026 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004027 return LY_SUCCESS;
4028 }
4029
4030 set_fill_boolean(set, 0);
4031 return LY_SUCCESS;
4032}
4033
4034/**
4035 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4036 * with the first argument floored (truncated).
4037 *
4038 * @param[in] args Array of arguments.
4039 * @param[in] arg_count Count of elements in @p args.
4040 * @param[in,out] set Context and result set at the same time.
4041 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004042 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004043 */
4044static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004045xpath_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 +02004046{
4047 LY_ERR rc;
4048
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004049 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004050 LY_CHECK_RET(rc);
4051 if (isfinite(args[0]->val.num)) {
4052 set_fill_number(set, (long long)args[0]->val.num);
4053 }
4054
4055 return LY_SUCCESS;
4056}
4057
4058/**
4059 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4060 * whether the language of the text matches the one from the argument.
4061 *
4062 * @param[in] args Array of arguments.
4063 * @param[in] arg_count Count of elements in @p args.
4064 * @param[in,out] set Context and result set at the same time.
4065 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004066 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004067 */
4068static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004069xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004070{
4071 const struct lyd_node *node;
4072 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004073 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004074 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004075 LY_ERR rc = LY_SUCCESS;
4076
4077 if (options & LYXP_SCNODE_ALL) {
4078 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4079 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004080 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4081 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004082 } else if (!warn_is_string_type(sleaf->type)) {
4083 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004084 }
4085 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004086 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004087 return rc;
4088 }
4089
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004090 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004091 LY_CHECK_RET(rc);
4092
Michal Vasko03ff5a72019-09-11 13:49:33 +02004093 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004094 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004095 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004096 } else if (!set->used) {
4097 set_fill_boolean(set, 0);
4098 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004099 }
4100
4101 switch (set->val.nodes[0].type) {
4102 case LYXP_NODE_ELEM:
4103 case LYXP_NODE_TEXT:
4104 node = set->val.nodes[0].node;
4105 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004106 case LYXP_NODE_META:
4107 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004108 break;
4109 default:
4110 /* nothing to do with roots */
4111 set_fill_boolean(set, 0);
4112 return LY_SUCCESS;
4113 }
4114
Michal Vasko9f96a052020-03-10 09:41:45 +01004115 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004116 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004117 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004118 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004119 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004120 break;
4121 }
4122 }
4123
Michal Vasko9f96a052020-03-10 09:41:45 +01004124 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004125 break;
4126 }
4127 }
4128
4129 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004130 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004131 set_fill_boolean(set, 0);
4132 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004133 uint64_t i;
4134
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004135 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004136 for (i = 0; args[0]->val.str[i]; ++i) {
4137 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4138 set_fill_boolean(set, 0);
4139 break;
4140 }
4141 }
4142 if (!args[0]->val.str[i]) {
4143 if (!val[i] || (val[i] == '-')) {
4144 set_fill_boolean(set, 1);
4145 } else {
4146 set_fill_boolean(set, 0);
4147 }
4148 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004149 }
4150
4151 return LY_SUCCESS;
4152}
4153
4154/**
4155 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4156 * with the context size.
4157 *
4158 * @param[in] args Array of arguments.
4159 * @param[in] arg_count Count of elements in @p args.
4160 * @param[in,out] set Context and result set at the same time.
4161 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004162 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004163 */
4164static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004165xpath_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 +02004166{
4167 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004168 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004169 return LY_SUCCESS;
4170 }
4171
Michal Vasko03ff5a72019-09-11 13:49:33 +02004172 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004173 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004174 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004175 } else if (!set->used) {
4176 set_fill_number(set, 0);
4177 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004178 }
4179
4180 set_fill_number(set, set->ctx_size);
4181 return LY_SUCCESS;
4182}
4183
4184/**
4185 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4186 * with the node name without namespace from the argument or the context.
4187 *
4188 * @param[in] args Array of arguments.
4189 * @param[in] arg_count Count of elements in @p args.
4190 * @param[in,out] set Context and result set at the same time.
4191 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004192 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004193 */
4194static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004195xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004196{
4197 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004198
Michal Vasko03ff5a72019-09-11 13:49:33 +02004199 /* suppress unused variable warning */
4200 (void)options;
4201
4202 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004203 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004204 return LY_SUCCESS;
4205 }
4206
4207 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004208 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004209 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004210 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004211 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004212 } else if (!args[0]->used) {
4213 set_fill_string(set, "", 0);
4214 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004215 }
4216
4217 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004218 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004219
4220 item = &args[0]->val.nodes[0];
4221 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004222 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004223 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004224 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004225 } else if (!set->used) {
4226 set_fill_string(set, "", 0);
4227 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004228 }
4229
4230 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004231 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004232
4233 item = &set->val.nodes[0];
4234 }
4235
4236 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004237 case LYXP_NODE_NONE:
4238 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004239 case LYXP_NODE_ROOT:
4240 case LYXP_NODE_ROOT_CONFIG:
4241 case LYXP_NODE_TEXT:
4242 set_fill_string(set, "", 0);
4243 break;
4244 case LYXP_NODE_ELEM:
4245 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4246 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004247 case LYXP_NODE_META:
4248 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004249 break;
4250 }
4251
4252 return LY_SUCCESS;
4253}
4254
4255/**
4256 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4257 * with the node name fully qualified (with namespace) from the argument or the context.
4258 *
4259 * @param[in] args Array of arguments.
4260 * @param[in] arg_count Count of elements in @p args.
4261 * @param[in,out] set Context and result set at the same time.
4262 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004263 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004264 */
4265static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004266xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004267{
4268 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004269 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004270 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004271 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004272
4273 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004274 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004275 return LY_SUCCESS;
4276 }
4277
4278 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004279 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004280 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004281 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004282 } else if (!args[0]->used) {
4283 set_fill_string(set, "", 0);
4284 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004285 }
4286
4287 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004288 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004289
4290 item = &args[0]->val.nodes[0];
4291 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004292 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004293 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004294 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004295 } else if (!set->used) {
4296 set_fill_string(set, "", 0);
4297 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004298 }
4299
4300 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004301 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004302
4303 item = &set->val.nodes[0];
4304 }
4305
4306 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004307 case LYXP_NODE_NONE:
4308 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004309 case LYXP_NODE_ROOT:
4310 case LYXP_NODE_ROOT_CONFIG:
4311 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004312 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004313 break;
4314 case LYXP_NODE_ELEM:
4315 mod = item->node->schema->module;
4316 name = item->node->schema->name;
4317 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004318 case LYXP_NODE_META:
4319 mod = ((struct lyd_meta *)item->node)->annotation->module;
4320 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004321 break;
4322 }
4323
4324 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004325 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004326 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4327 set_fill_string(set, str, strlen(str));
4328 free(str);
4329 } else {
4330 set_fill_string(set, "", 0);
4331 }
4332
4333 return LY_SUCCESS;
4334}
4335
4336/**
4337 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4338 * with the namespace of the node from the argument or the context.
4339 *
4340 * @param[in] args Array of arguments.
4341 * @param[in] arg_count Count of elements in @p args.
4342 * @param[in,out] set Context and result set at the same time.
4343 * @param[in] options XPath options.
4344 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4345 */
4346static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004347xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004348{
4349 struct lyxp_set_node *item;
4350 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004351
Michal Vasko03ff5a72019-09-11 13:49:33 +02004352 /* suppress unused variable warning */
4353 (void)options;
4354
4355 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004356 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004357 return LY_SUCCESS;
4358 }
4359
4360 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004361 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004362 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004363 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004364 return LY_EVALID;
4365 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004366 set_fill_string(set, "", 0);
4367 return LY_SUCCESS;
4368 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004369
4370 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004371 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004372
4373 item = &args[0]->val.nodes[0];
4374 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004375 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004376 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004377 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004378 } else if (!set->used) {
4379 set_fill_string(set, "", 0);
4380 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004381 }
4382
4383 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004384 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004385
4386 item = &set->val.nodes[0];
4387 }
4388
4389 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004390 case LYXP_NODE_NONE:
4391 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004392 case LYXP_NODE_ROOT:
4393 case LYXP_NODE_ROOT_CONFIG:
4394 case LYXP_NODE_TEXT:
4395 set_fill_string(set, "", 0);
4396 break;
4397 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004398 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004399 if (item->type == LYXP_NODE_ELEM) {
4400 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004401 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004402 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004403 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004404 }
4405
4406 set_fill_string(set, mod->ns, strlen(mod->ns));
4407 break;
4408 }
4409
4410 return LY_SUCCESS;
4411}
4412
4413/**
4414 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4415 * with only nodes from the context. In practice it either leaves the context
4416 * as it is or returns an empty node set.
4417 *
4418 * @param[in] args Array of arguments.
4419 * @param[in] arg_count Count of elements in @p args.
4420 * @param[in,out] set Context and result set at the same time.
4421 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004422 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004423 */
4424static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004425xpath_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 +02004426{
4427 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004428 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004429 return LY_SUCCESS;
4430 }
4431
4432 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004433 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004434 }
4435 return LY_SUCCESS;
4436}
4437
4438/**
4439 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4440 * with normalized value (no leading, trailing, double white spaces) of the node
4441 * from the argument or the context.
4442 *
4443 * @param[in] args Array of arguments.
4444 * @param[in] arg_count Count of elements in @p args.
4445 * @param[in,out] set Context and result set at the same time.
4446 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004447 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004448 */
4449static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004450xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004451{
4452 uint16_t i, new_used;
4453 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004454 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004455 struct lysc_node_leaf *sleaf;
4456 LY_ERR rc = LY_SUCCESS;
4457
4458 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004459 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4460 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004461 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004462 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4463 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004464 } else if (!warn_is_string_type(sleaf->type)) {
4465 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004466 }
4467 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004468 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004469 return rc;
4470 }
4471
4472 if (arg_count) {
4473 set_fill_set(set, args[0]);
4474 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004475 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004476 LY_CHECK_RET(rc);
4477
4478 /* is there any normalization necessary? */
4479 for (i = 0; set->val.str[i]; ++i) {
4480 if (is_xmlws(set->val.str[i])) {
4481 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4482 have_spaces = 1;
4483 break;
4484 }
4485 space_before = 1;
4486 } else {
4487 space_before = 0;
4488 }
4489 }
4490
4491 /* yep, there is */
4492 if (have_spaces) {
4493 /* it's enough, at least one character will go, makes space for ending '\0' */
4494 new = malloc(strlen(set->val.str) * sizeof(char));
4495 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4496 new_used = 0;
4497
4498 space_before = 0;
4499 for (i = 0; set->val.str[i]; ++i) {
4500 if (is_xmlws(set->val.str[i])) {
4501 if ((i == 0) || space_before) {
4502 space_before = 1;
4503 continue;
4504 } else {
4505 space_before = 1;
4506 }
4507 } else {
4508 space_before = 0;
4509 }
4510
4511 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4512 ++new_used;
4513 }
4514
4515 /* at worst there is one trailing space now */
4516 if (new_used && is_xmlws(new[new_used - 1])) {
4517 --new_used;
4518 }
4519
4520 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4521 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4522 new[new_used] = '\0';
4523
4524 free(set->val.str);
4525 set->val.str = new;
4526 }
4527
4528 return LY_SUCCESS;
4529}
4530
4531/**
4532 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4533 * with the argument converted to boolean and logically inverted.
4534 *
4535 * @param[in] args Array of arguments.
4536 * @param[in] arg_count Count of elements in @p args.
4537 * @param[in,out] set Context and result set at the same time.
4538 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004539 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004540 */
4541static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004542xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004543{
4544 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004545 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004546 return LY_SUCCESS;
4547 }
4548
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004549 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004550 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004551 set_fill_boolean(set, 0);
4552 } else {
4553 set_fill_boolean(set, 1);
4554 }
4555
4556 return LY_SUCCESS;
4557}
4558
4559/**
4560 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4561 * with the number representation of either the argument or the context.
4562 *
4563 * @param[in] args Array of arguments.
4564 * @param[in] arg_count Count of elements in @p args.
4565 * @param[in,out] set Context and result set at the same time.
4566 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004567 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004568 */
4569static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004570xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004571{
4572 LY_ERR rc;
4573
4574 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004575 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004576 return LY_SUCCESS;
4577 }
4578
4579 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004580 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004581 LY_CHECK_RET(rc);
4582 set_fill_set(set, args[0]);
4583 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004584 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004585 LY_CHECK_RET(rc);
4586 }
4587
4588 return LY_SUCCESS;
4589}
4590
4591/**
4592 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4593 * with the context position.
4594 *
4595 * @param[in] args Array of arguments.
4596 * @param[in] arg_count Count of elements in @p args.
4597 * @param[in,out] set Context and result set at the same time.
4598 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004599 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004600 */
4601static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004602xpath_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 +02004603{
4604 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004605 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004606 return LY_SUCCESS;
4607 }
4608
Michal Vasko03ff5a72019-09-11 13:49:33 +02004609 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004610 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004611 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004612 } else if (!set->used) {
4613 set_fill_number(set, 0);
4614 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004615 }
4616
4617 set_fill_number(set, set->ctx_pos);
4618
4619 /* UNUSED in 'Release' build type */
4620 (void)options;
4621 return LY_SUCCESS;
4622}
4623
4624/**
4625 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4626 * depending on whether the second argument regex matches the first argument string. For details refer to
4627 * YANG 1.1 RFC section 10.2.1.
4628 *
4629 * @param[in] args Array of arguments.
4630 * @param[in] arg_count Count of elements in @p args.
4631 * @param[in,out] set Context and result set at the same time.
4632 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004633 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004634 */
4635static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004636xpath_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 +02004637{
4638 struct lysc_pattern **patterns = NULL, **pattern;
4639 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004640 LY_ERR rc = LY_SUCCESS;
4641 struct ly_err_item *err;
4642
4643 if (options & LYXP_SCNODE_ALL) {
4644 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4645 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4646 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 +02004647 } else if (!warn_is_string_type(sleaf->type)) {
4648 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004649 }
4650 }
4651
4652 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4653 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4654 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 +02004655 } else if (!warn_is_string_type(sleaf->type)) {
4656 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004657 }
4658 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004659 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004660 return rc;
4661 }
4662
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004663 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004664 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004665 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004666 LY_CHECK_RET(rc);
4667
4668 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek IÅ¡a45802b52021-02-09 09:21:58 +01004669 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004670 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004671 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Radek Krejciddace2c2021-01-08 11:30:56 +01004672 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004673 if (rc != LY_SUCCESS) {
4674 LY_ARRAY_FREE(patterns);
4675 return rc;
4676 }
4677
Radek Krejci0b013302021-03-29 15:22:32 +02004678 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004679 pcre2_code_free((*pattern)->code);
4680 free(*pattern);
4681 LY_ARRAY_FREE(patterns);
4682 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004683 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004684 ly_err_free(err);
4685 return rc;
4686 }
4687
4688 if (rc == LY_EVALID) {
4689 ly_err_free(err);
4690 set_fill_boolean(set, 0);
4691 } else {
4692 set_fill_boolean(set, 1);
4693 }
4694
4695 return LY_SUCCESS;
4696}
4697
4698/**
4699 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4700 * with the rounded first argument. For details refer to
4701 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4702 *
4703 * @param[in] args Array of arguments.
4704 * @param[in] arg_count Count of elements in @p args.
4705 * @param[in,out] set Context and result set at the same time.
4706 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004707 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004708 */
4709static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004710xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004711{
4712 struct lysc_node_leaf *sleaf;
4713 LY_ERR rc = LY_SUCCESS;
4714
4715 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004716 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004717 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004718 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4719 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4720 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4721 sleaf->name);
4722 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4723 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4724 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004725 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004726 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004727 return rc;
4728 }
4729
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004730 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004731 LY_CHECK_RET(rc);
4732
4733 /* cover only the cases where floor can't be used */
4734 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4735 set_fill_number(set, -0.0f);
4736 } else {
4737 args[0]->val.num += 0.5;
4738 rc = xpath_floor(args, 1, args[0], options);
4739 LY_CHECK_RET(rc);
4740 set_fill_number(set, args[0]->val.num);
4741 }
4742
4743 return LY_SUCCESS;
4744}
4745
4746/**
4747 * @brief Execute the XPath starts-with(string, string) function.
4748 * Returns LYXP_SET_BOOLEAN whether the second argument is
4749 * the prefix of the first or not.
4750 *
4751 * @param[in] args Array of arguments.
4752 * @param[in] arg_count Count of elements in @p args.
4753 * @param[in,out] set Context and result set at the same time.
4754 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004755 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004756 */
4757static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004758xpath_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 +02004759{
4760 struct lysc_node_leaf *sleaf;
4761 LY_ERR rc = LY_SUCCESS;
4762
4763 if (options & LYXP_SCNODE_ALL) {
4764 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4765 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4766 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 +02004767 } else if (!warn_is_string_type(sleaf->type)) {
4768 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004769 }
4770 }
4771
4772 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4773 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4774 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 +02004775 } else if (!warn_is_string_type(sleaf->type)) {
4776 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004777 }
4778 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004779 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004780 return rc;
4781 }
4782
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004783 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004784 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004785 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004786 LY_CHECK_RET(rc);
4787
4788 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4789 set_fill_boolean(set, 0);
4790 } else {
4791 set_fill_boolean(set, 1);
4792 }
4793
4794 return LY_SUCCESS;
4795}
4796
4797/**
4798 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4799 * with the string representation of either the argument or the context.
4800 *
4801 * @param[in] args Array of arguments.
4802 * @param[in] arg_count Count of elements in @p args.
4803 * @param[in,out] set Context and result set at the same time.
4804 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004805 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004806 */
4807static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004808xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004809{
4810 LY_ERR rc;
4811
4812 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004813 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004814 return LY_SUCCESS;
4815 }
4816
4817 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004818 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004819 LY_CHECK_RET(rc);
4820 set_fill_set(set, args[0]);
4821 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004822 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004823 LY_CHECK_RET(rc);
4824 }
4825
4826 return LY_SUCCESS;
4827}
4828
4829/**
4830 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4831 * with the length of the string in either the argument or the context.
4832 *
4833 * @param[in] args Array of arguments.
4834 * @param[in] arg_count Count of elements in @p args.
4835 * @param[in,out] set Context and result set at the same time.
4836 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004837 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004838 */
4839static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004840xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004841{
4842 struct lysc_node_leaf *sleaf;
4843 LY_ERR rc = LY_SUCCESS;
4844
4845 if (options & LYXP_SCNODE_ALL) {
4846 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4847 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4848 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 +02004849 } else if (!warn_is_string_type(sleaf->type)) {
4850 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004851 }
4852 }
4853 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4854 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4855 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 +02004856 } else if (!warn_is_string_type(sleaf->type)) {
4857 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004858 }
4859 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004860 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004861 return rc;
4862 }
4863
4864 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004865 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004866 LY_CHECK_RET(rc);
4867 set_fill_number(set, strlen(args[0]->val.str));
4868 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004869 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004870 LY_CHECK_RET(rc);
4871 set_fill_number(set, strlen(set->val.str));
4872 }
4873
4874 return LY_SUCCESS;
4875}
4876
4877/**
4878 * @brief Execute the XPath substring(string, number, number?) function.
4879 * Returns LYXP_SET_STRING substring of the first argument starting
4880 * on the second argument index ending on the third argument index,
4881 * indexed from 1. For exact definition refer to
4882 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4883 *
4884 * @param[in] args Array of arguments.
4885 * @param[in] arg_count Count of elements in @p args.
4886 * @param[in,out] set Context and result set at the same time.
4887 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004888 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004889 */
4890static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004891xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004892{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004893 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004894 uint16_t str_start, str_len, pos;
4895 struct lysc_node_leaf *sleaf;
4896 LY_ERR rc = LY_SUCCESS;
4897
4898 if (options & LYXP_SCNODE_ALL) {
4899 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4900 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4901 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 +02004902 } else if (!warn_is_string_type(sleaf->type)) {
4903 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004904 }
4905 }
4906
4907 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4908 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4909 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 +02004910 } else if (!warn_is_numeric_type(sleaf->type)) {
4911 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004912 }
4913 }
4914
Michal Vasko69730152020-10-09 16:30:07 +02004915 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4916 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004917 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4918 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 +02004919 } else if (!warn_is_numeric_type(sleaf->type)) {
4920 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004921 }
4922 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004923 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004924 return rc;
4925 }
4926
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004927 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004928 LY_CHECK_RET(rc);
4929
4930 /* start */
4931 if (xpath_round(&args[1], 1, args[1], options)) {
4932 return -1;
4933 }
4934 if (isfinite(args[1]->val.num)) {
4935 start = args[1]->val.num - 1;
4936 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004937 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004938 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004939 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004940 }
4941
4942 /* len */
4943 if (arg_count == 3) {
4944 rc = xpath_round(&args[2], 1, args[2], options);
4945 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004946 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004947 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004948 } else if (isfinite(args[2]->val.num)) {
4949 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004950 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004951 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004952 }
4953 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004954 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004955 }
4956
4957 /* find matching character positions */
4958 str_start = 0;
4959 str_len = 0;
4960 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4961 if (pos < start) {
4962 ++str_start;
4963 } else if (pos < start + len) {
4964 ++str_len;
4965 } else {
4966 break;
4967 }
4968 }
4969
4970 set_fill_string(set, args[0]->val.str + str_start, str_len);
4971 return LY_SUCCESS;
4972}
4973
4974/**
4975 * @brief Execute the XPath substring-after(string, string) function.
4976 * Returns LYXP_SET_STRING with the string succeeding the occurance
4977 * of the second argument in the first or an empty string.
4978 *
4979 * @param[in] args Array of arguments.
4980 * @param[in] arg_count Count of elements in @p args.
4981 * @param[in,out] set Context and result set at the same time.
4982 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004983 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004984 */
4985static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004986xpath_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 +02004987{
4988 char *ptr;
4989 struct lysc_node_leaf *sleaf;
4990 LY_ERR rc = LY_SUCCESS;
4991
4992 if (options & LYXP_SCNODE_ALL) {
4993 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4994 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4995 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 +02004996 } else if (!warn_is_string_type(sleaf->type)) {
4997 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004998 }
4999 }
5000
5001 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5002 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5003 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 +02005004 } else if (!warn_is_string_type(sleaf->type)) {
5005 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005006 }
5007 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005008 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005009 return rc;
5010 }
5011
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005012 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005013 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005014 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005015 LY_CHECK_RET(rc);
5016
5017 ptr = strstr(args[0]->val.str, args[1]->val.str);
5018 if (ptr) {
5019 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5020 } else {
5021 set_fill_string(set, "", 0);
5022 }
5023
5024 return LY_SUCCESS;
5025}
5026
5027/**
5028 * @brief Execute the XPath substring-before(string, string) function.
5029 * Returns LYXP_SET_STRING with the string preceding the occurance
5030 * of the second argument in the first or an empty string.
5031 *
5032 * @param[in] args Array of arguments.
5033 * @param[in] arg_count Count of elements in @p args.
5034 * @param[in,out] set Context and result set at the same time.
5035 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005036 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005037 */
5038static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005039xpath_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 +02005040{
5041 char *ptr;
5042 struct lysc_node_leaf *sleaf;
5043 LY_ERR rc = LY_SUCCESS;
5044
5045 if (options & LYXP_SCNODE_ALL) {
5046 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5047 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5048 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 +02005049 } else if (!warn_is_string_type(sleaf->type)) {
5050 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005051 }
5052 }
5053
5054 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5055 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5056 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 +02005057 } else if (!warn_is_string_type(sleaf->type)) {
5058 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005059 }
5060 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005061 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005062 return rc;
5063 }
5064
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005065 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005066 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005067 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005068 LY_CHECK_RET(rc);
5069
5070 ptr = strstr(args[0]->val.str, args[1]->val.str);
5071 if (ptr) {
5072 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5073 } else {
5074 set_fill_string(set, "", 0);
5075 }
5076
5077 return LY_SUCCESS;
5078}
5079
5080/**
5081 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5082 * with the sum of all the nodes in the context.
5083 *
5084 * @param[in] args Array of arguments.
5085 * @param[in] arg_count Count of elements in @p args.
5086 * @param[in,out] set Context and result set at the same time.
5087 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005088 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005089 */
5090static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005091xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005092{
5093 long double num;
5094 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005095 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005096 struct lyxp_set set_item;
5097 struct lysc_node_leaf *sleaf;
5098 LY_ERR rc = LY_SUCCESS;
5099
5100 if (options & LYXP_SCNODE_ALL) {
5101 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5102 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005103 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005104 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5105 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5106 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005107 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005108 } else if (!warn_is_numeric_type(sleaf->type)) {
5109 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005110 }
5111 }
5112 }
5113 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005114 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005115 return rc;
5116 }
5117
5118 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005119
5120 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005121 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005122 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005123 } else if (!args[0]->used) {
5124 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005125 }
5126
Michal Vasko5c4e5892019-11-14 12:31:38 +01005127 set_init(&set_item, set);
5128
Michal Vasko03ff5a72019-09-11 13:49:33 +02005129 set_item.type = LYXP_SET_NODE_SET;
5130 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5131 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5132
5133 set_item.used = 1;
5134 set_item.size = 1;
5135
5136 for (i = 0; i < args[0]->used; ++i) {
5137 set_item.val.nodes[0] = args[0]->val.nodes[i];
5138
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005139 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005140 LY_CHECK_RET(rc);
5141 num = cast_string_to_number(str);
5142 free(str);
5143 set->val.num += num;
5144 }
5145
5146 free(set_item.val.nodes);
5147
5148 return LY_SUCCESS;
5149}
5150
5151/**
5152 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5153 * with the text content of the nodes in the context.
5154 *
5155 * @param[in] args Array of arguments.
5156 * @param[in] arg_count Count of elements in @p args.
5157 * @param[in,out] set Context and result set at the same time.
5158 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005159 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005160 */
5161static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005162xpath_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 +02005163{
5164 uint32_t i;
5165
5166 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005167 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005168 return LY_SUCCESS;
5169 }
5170
Michal Vasko03ff5a72019-09-11 13:49:33 +02005171 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005172 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005173 return LY_EVALID;
5174 }
5175
Michal Vaskod989ba02020-08-24 10:59:24 +02005176 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005177 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005178 case LYXP_NODE_NONE:
5179 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005180 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005181 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5182 set->val.nodes[i].type = LYXP_NODE_TEXT;
5183 ++i;
5184 break;
5185 }
Radek Krejci0f969882020-08-21 16:56:47 +02005186 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005187 case LYXP_NODE_ROOT:
5188 case LYXP_NODE_ROOT_CONFIG:
5189 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005190 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005191 set_remove_node(set, i);
5192 break;
5193 }
5194 }
5195
5196 return LY_SUCCESS;
5197}
5198
5199/**
5200 * @brief Execute the XPath translate(string, string, string) function.
5201 * Returns LYXP_SET_STRING with the first argument with the characters
5202 * from the second argument replaced by those on the corresponding
5203 * positions in the third argument.
5204 *
5205 * @param[in] args Array of arguments.
5206 * @param[in] arg_count Count of elements in @p args.
5207 * @param[in,out] set Context and result set at the same time.
5208 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005209 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005210 */
5211static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005212xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005213{
5214 uint16_t i, j, new_used;
5215 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005216 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005217 struct lysc_node_leaf *sleaf;
5218 LY_ERR rc = LY_SUCCESS;
5219
5220 if (options & LYXP_SCNODE_ALL) {
5221 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5222 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5223 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 +02005224 } else if (!warn_is_string_type(sleaf->type)) {
5225 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005226 }
5227 }
5228
5229 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5230 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5231 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 +02005232 } else if (!warn_is_string_type(sleaf->type)) {
5233 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005234 }
5235 }
5236
5237 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5238 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5239 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 +02005240 } else if (!warn_is_string_type(sleaf->type)) {
5241 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005242 }
5243 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005244 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005245 return rc;
5246 }
5247
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005248 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005249 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005250 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005251 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005252 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005253 LY_CHECK_RET(rc);
5254
5255 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5256 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5257 new_used = 0;
5258
5259 have_removed = 0;
5260 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005261 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005262
5263 for (j = 0; args[1]->val.str[j]; ++j) {
5264 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5265 /* removing this char */
5266 if (j >= strlen(args[2]->val.str)) {
5267 have_removed = 1;
5268 found = 1;
5269 break;
5270 }
5271 /* replacing this char */
5272 new[new_used] = args[2]->val.str[j];
5273 ++new_used;
5274 found = 1;
5275 break;
5276 }
5277 }
5278
5279 /* copying this char */
5280 if (!found) {
5281 new[new_used] = args[0]->val.str[i];
5282 ++new_used;
5283 }
5284 }
5285
5286 if (have_removed) {
5287 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5288 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5289 }
5290 new[new_used] = '\0';
5291
Michal Vaskod3678892020-05-21 10:06:58 +02005292 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005293 set->type = LYXP_SET_STRING;
5294 set->val.str = new;
5295
5296 return LY_SUCCESS;
5297}
5298
5299/**
5300 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5301 * with true value.
5302 *
5303 * @param[in] args Array of arguments.
5304 * @param[in] arg_count Count of elements in @p args.
5305 * @param[in,out] set Context and result set at the same time.
5306 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005307 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005308 */
5309static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005310xpath_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 +02005311{
5312 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005313 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005314 return LY_SUCCESS;
5315 }
5316
5317 set_fill_boolean(set, 1);
5318 return LY_SUCCESS;
5319}
5320
5321/*
5322 * moveto functions
5323 *
5324 * They and only they actually change the context (set).
5325 */
5326
5327/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005328 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005329 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005330 * XPath @p set is expected to be a (sc)node set!
5331 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005332 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5333 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005334 * @param[in] set Set with general XPath context.
5335 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005336 * @param[out] moveto_mod Expected module of a matching node.
5337 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005338 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005339static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005340moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5341 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005342{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005343 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005344 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005345 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005346
Michal Vasko2104e9f2020-03-06 08:23:25 +01005347 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5348
Michal Vasko6346ece2019-09-24 13:12:53 +02005349 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5350 /* specific module */
5351 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005352 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005353
Michal Vasko004d3152020-06-11 19:59:22 +02005354 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005355 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005356 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005357 return LY_EVALID;
5358 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005359
Michal Vasko6346ece2019-09-24 13:12:53 +02005360 *qname += pref_len + 1;
5361 *qname_len -= pref_len + 1;
5362 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5363 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005364 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005365 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005366 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005367 case LY_VALUE_SCHEMA:
5368 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005369 /* current module */
5370 mod = set->cur_mod;
5371 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005372 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005373 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005374 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005375 /* inherit parent (context node) module */
5376 if (ctx_scnode) {
5377 mod = ctx_scnode->module;
5378 } else {
5379 mod = NULL;
5380 }
5381 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005382 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005383 /* all nodes need to be prefixed */
5384 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5385 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005386 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005387 }
5388
Michal Vasko6346ece2019-09-24 13:12:53 +02005389 *moveto_mod = mod;
5390 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005391}
5392
5393/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005394 * @brief Move context @p set to the root. Handles absolute path.
5395 * Result is LYXP_SET_NODE_SET.
5396 *
5397 * @param[in,out] set Set to use.
5398 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005399 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005400 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005401static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005402moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005403{
aPiecek8b0cc152021-05-31 16:40:31 +02005404 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005405
5406 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005407 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskob0099a92020-08-31 14:55:23 +02005408 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005409 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005410 set->type = LYXP_SET_NODE_SET;
5411 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005412 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005413 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005414
5415 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005416}
5417
5418/**
5419 * @brief Check @p node as a part of NameTest processing.
5420 *
5421 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005422 * @param[in] ctx_node Context node.
5423 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005424 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005425 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005426 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005427 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5428 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005429 */
5430static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005431moveto_node_check(const struct lyd_node *node, const struct lyd_node *ctx_node, const struct lyxp_set *set,
Michal Vaskocdad7122020-11-09 21:04:44 +01005432 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005433{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005434 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5435 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005436 case LY_VALUE_SCHEMA:
5437 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005438 /* use current module */
5439 moveto_mod = set->cur_mod;
5440 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005441 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005442 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005443 /* inherit module of the context node, if any */
5444 if (ctx_node) {
5445 moveto_mod = ctx_node->schema->module;
5446 }
5447 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005448 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005449 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005450 /* not defined */
5451 LOGINT(set->ctx);
5452 return LY_EINVAL;
5453 }
5454 }
5455
Michal Vasko03ff5a72019-09-11 13:49:33 +02005456 /* module check */
5457 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005458 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005459 }
5460
Michal Vasko5c4e5892019-11-14 12:31:38 +01005461 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005462 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005463 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005464 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5465 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005466 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005467 }
5468
5469 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005470 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005471 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005472 }
5473
Michal Vaskoa1424542019-11-14 16:08:52 +01005474 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005475 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005476 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005477 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005478
5479 /* match */
5480 return LY_SUCCESS;
5481}
5482
5483/**
5484 * @brief Check @p node as a part of schema NameTest processing.
5485 *
5486 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005487 * @param[in] ctx_scnode Context node.
5488 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005489 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005490 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005491 * @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 +02005492 */
5493static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005494moveto_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 +02005495 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005496{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005497 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5498 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005499 case LY_VALUE_SCHEMA:
5500 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005501 /* use current module */
5502 moveto_mod = set->cur_mod;
5503 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005504 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005505 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005506 /* inherit module of the context node, if any */
5507 if (ctx_scnode) {
5508 moveto_mod = ctx_scnode->module;
5509 }
5510 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005511 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005512 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005513 /* not defined */
5514 LOGINT(set->ctx);
5515 return LY_EINVAL;
5516 }
5517 }
5518
Michal Vasko03ff5a72019-09-11 13:49:33 +02005519 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005520 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005521 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005522 }
5523
5524 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005525 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005526 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005527 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005528 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005529 }
5530
5531 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005532 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005533 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005534 }
5535
5536 /* match */
5537 return LY_SUCCESS;
5538}
5539
5540/**
Michal Vaskod3678892020-05-21 10:06:58 +02005541 * @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 +02005542 *
5543 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005544 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005545 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005546 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005547 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5548 */
5549static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005550moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005551{
Michal Vaskof03ed032020-03-04 13:31:44 +01005552 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005553 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005554 LY_ERR rc;
5555
aPiecek8b0cc152021-05-31 16:40:31 +02005556 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005557 return LY_SUCCESS;
5558 }
5559
5560 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005561 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005562 return LY_EVALID;
5563 }
5564
Michal Vasko03ff5a72019-09-11 13:49:33 +02005565 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005566 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005567
5568 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 +01005569 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005570
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005571 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005572 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005573 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005574 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005575 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005576 ctx_node = set->val.nodes[i].node;
5577 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005578 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005579
Michal Vaskod3678892020-05-21 10:06:58 +02005580 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005581 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005582 if (rc == LY_SUCCESS) {
5583 if (!replaced) {
5584 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5585 replaced = 1;
5586 } else {
5587 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005588 }
Michal Vaskod3678892020-05-21 10:06:58 +02005589 ++i;
5590 } else if (rc == LY_EINCOMPLETE) {
5591 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005592 }
5593 }
5594
5595 if (!replaced) {
5596 /* no match */
5597 set_remove_node(set, i);
5598 }
5599 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005600
5601 return LY_SUCCESS;
5602}
5603
5604/**
Michal Vaskod3678892020-05-21 10:06:58 +02005605 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5606 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005607 *
5608 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005609 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005610 * @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 +01005611 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005612 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5613 */
5614static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005615moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5616 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005617{
Michal Vasko004d3152020-06-11 19:59:22 +02005618 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005619 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005620 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005621 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005622
Michal Vasko004d3152020-06-11 19:59:22 +02005623 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005624
aPiecek8b0cc152021-05-31 16:40:31 +02005625 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02005626 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005627 }
5628
5629 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005630 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005631 ret = LY_EVALID;
5632 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005633 }
5634
5635 /* context check for all the nodes since we have the schema node */
5636 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5637 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005638 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005639 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5640 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005641 lyxp_set_free_content(set);
5642 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005643 }
5644
5645 /* create specific data instance if needed */
5646 if (scnode->nodetype == LYS_LIST) {
5647 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5648 } else if (scnode->nodetype == LYS_LEAFLIST) {
5649 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005650 }
5651
5652 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005653 siblings = NULL;
5654
5655 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5656 assert(!set->val.nodes[i].node);
5657
5658 /* search in all the trees */
5659 siblings = set->tree;
5660 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5661 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005662 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005663 }
5664
5665 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005666 if (inst) {
5667 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005668 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005669 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005670 }
5671
5672 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005673 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005674 ret = LY_EINCOMPLETE;
5675 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005676 }
5677
5678 if (sub) {
5679 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005680 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005681 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005682 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005683 /* no match */
5684 set_remove_node(set, i);
5685 }
5686 }
5687
Michal Vasko004d3152020-06-11 19:59:22 +02005688cleanup:
5689 lyd_free_tree(inst);
5690 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005691}
5692
5693/**
5694 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5695 *
5696 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005697 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005698 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005699 * @param[in] options XPath options.
5700 * @return LY_ERR
5701 */
5702static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005703moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005704{
Radek Krejci857189e2020-09-01 13:26:36 +02005705 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005706 uint32_t getnext_opts;
5707 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005708 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005709 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005710 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005711
aPiecek8b0cc152021-05-31 16:40:31 +02005712 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005713 return LY_SUCCESS;
5714 }
5715
5716 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005717 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005718 return LY_EVALID;
5719 }
5720
Michal Vaskocafad9d2019-11-07 15:20:03 +01005721 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005722 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005723 if (options & LYXP_SCNODE_OUTPUT) {
5724 getnext_opts |= LYS_GETNEXT_OUTPUT;
5725 }
5726
Michal Vasko03ff5a72019-09-11 13:49:33 +02005727 orig_used = set->used;
5728 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005729 uint32_t idx;
5730
Radek Krejcif13b87b2020-12-01 22:02:17 +01005731 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5732 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005733 continue;
5734 }
5735
5736 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005737 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005738 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005739 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005740 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005741
5742 start_parent = set->val.scnodes[i].scnode;
5743
5744 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 +02005745 /* 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 +02005746 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005747 mod_idx = 0;
Michal Vaskoa51ef072021-07-02 10:40:30 +02005748 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005749 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005750 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005751 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005752 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005753 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5754
Michal Vasko03ff5a72019-09-11 13:49:33 +02005755 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005756 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005757 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005758 temp_ctx = 1;
5759 }
5760 }
5761 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005762 }
5763
Michal Vasko519fd602020-05-26 12:17:39 +02005764 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5765 iter = NULL;
5766 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005767 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005768 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5769
5770 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005771 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005772 temp_ctx = 1;
5773 }
5774 }
5775 }
5776 }
5777 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005778
5779 /* correct temporary in_ctx values */
5780 if (temp_ctx) {
5781 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005782 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5783 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005784 }
5785 }
5786 }
5787
5788 return LY_SUCCESS;
5789}
5790
5791/**
Michal Vaskod3678892020-05-21 10:06:58 +02005792 * @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 +02005793 * Context position aware.
5794 *
5795 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005796 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005797 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005798 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005799 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5800 */
5801static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005802moveto_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 +02005803{
5804 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005805 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005806 struct lyxp_set ret_set;
5807 LY_ERR rc;
5808
aPiecek8b0cc152021-05-31 16:40:31 +02005809 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005810 return LY_SUCCESS;
5811 }
5812
5813 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005814 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005815 return LY_EVALID;
5816 }
5817
Michal Vasko9f96a052020-03-10 09:41:45 +01005818 /* 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 +01005819 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005820 LY_CHECK_RET(rc);
5821
Michal Vasko6346ece2019-09-24 13:12:53 +02005822 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005823 set_init(&ret_set, set);
5824 for (i = 0; i < set->used; ++i) {
5825
5826 /* TREE DFS */
5827 start = set->val.nodes[i].node;
5828 for (elem = next = start; elem; elem = next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005829 rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005830 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005831 /* add matching node into result set */
5832 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5833 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5834 /* the node is a duplicate, we'll process it later in the set */
5835 goto skip_children;
5836 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005837 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005838 return rc;
5839 } else if (rc == LY_EINVAL) {
5840 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005841 }
5842
5843 /* TREE DFS NEXT ELEM */
5844 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005845 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005846 if (!next) {
5847skip_children:
5848 /* no children, so try siblings, but only if it's not the start,
5849 * that is considered to be the root and it's siblings are not traversed */
5850 if (elem != start) {
5851 next = elem->next;
5852 } else {
5853 break;
5854 }
5855 }
5856 while (!next) {
5857 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01005858 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005859 /* we are done, no next element to process */
5860 break;
5861 }
5862 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01005863 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005864 next = elem->next;
5865 }
5866 }
5867 }
5868
5869 /* make the temporary set the current one */
5870 ret_set.ctx_pos = set->ctx_pos;
5871 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005872 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005873 memcpy(set, &ret_set, sizeof *set);
5874
5875 return LY_SUCCESS;
5876}
5877
5878/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005879 * @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 +02005880 *
5881 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005882 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005883 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005884 * @param[in] options XPath options.
5885 * @return LY_ERR
5886 */
5887static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005888moveto_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 +02005889{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005890 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005891 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005892 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005893
aPiecek8b0cc152021-05-31 16:40:31 +02005894 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005895 return LY_SUCCESS;
5896 }
5897
5898 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005899 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005900 return LY_EVALID;
5901 }
5902
Michal Vasko03ff5a72019-09-11 13:49:33 +02005903 orig_used = set->used;
5904 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005905 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5906 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005907 continue;
5908 }
5909
5910 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005911 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005912 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005913 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005914 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005915
5916 /* TREE DFS */
5917 start = set->val.scnodes[i].scnode;
5918 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005919 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5920 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005921 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005922 }
5923
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005924 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005925 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005926 uint32_t idx;
5927
5928 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005929 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005930 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005931 /* we will process it later in the set */
5932 goto skip_children;
5933 }
5934 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005935 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005936 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005937 } else if (rc == LY_EINVAL) {
5938 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005939 }
5940
5941next_iter:
5942 /* TREE DFS NEXT ELEM */
5943 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005944 next = lysc_node_child(elem);
5945 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5946 next = next->next;
5947 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5948 next = next->next;
5949 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005950 if (!next) {
5951skip_children:
5952 /* no children, so try siblings, but only if it's not the start,
5953 * that is considered to be the root and it's siblings are not traversed */
5954 if (elem != start) {
5955 next = elem->next;
5956 } else {
5957 break;
5958 }
5959 }
5960 while (!next) {
5961 /* no siblings, go back through the parents */
5962 if (elem->parent == start) {
5963 /* we are done, no next element to process */
5964 break;
5965 }
5966 /* parent is already processed, go to its sibling */
5967 elem = elem->parent;
5968 next = elem->next;
5969 }
5970 }
5971 }
5972
5973 return LY_SUCCESS;
5974}
5975
5976/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005977 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005978 * Indirectly context position aware.
5979 *
5980 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005981 * @param[in] mod Matching metadata module, NULL for any.
5982 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02005983 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005984 * @return LY_ERR
5985 */
5986static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02005987moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005988{
Michal Vasko9f96a052020-03-10 09:41:45 +01005989 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005990
aPiecek8b0cc152021-05-31 16:40:31 +02005991 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005992 return LY_SUCCESS;
5993 }
5994
5995 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005996 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005997 return LY_EVALID;
5998 }
5999
Radek Krejci1deb5be2020-08-26 16:43:36 +02006000 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006001 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006002
6003 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
6004 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01006005 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006006 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006007
6008 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006009 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006010 continue;
6011 }
6012
Michal Vaskod3678892020-05-21 10:06:58 +02006013 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006014 /* match */
6015 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006016 set->val.meta[i].meta = sub;
6017 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006018 /* pos does not change */
6019 replaced = 1;
6020 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006021 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 +02006022 }
6023 ++i;
6024 }
6025 }
6026 }
6027
6028 if (!replaced) {
6029 /* no match */
6030 set_remove_node(set, i);
6031 }
6032 }
6033
6034 return LY_SUCCESS;
6035}
6036
6037/**
6038 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02006039 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006040 *
6041 * @param[in,out] set1 Set to use for the result.
6042 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006043 * @return LY_ERR
6044 */
6045static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006046moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006047{
6048 LY_ERR rc;
6049
Michal Vaskod3678892020-05-21 10:06:58 +02006050 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006051 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006052 return LY_EVALID;
6053 }
6054
6055 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02006056 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006057 return LY_SUCCESS;
6058 }
6059
Michal Vaskod3678892020-05-21 10:06:58 +02006060 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006061 memcpy(set1, set2, sizeof *set1);
6062 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02006063 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006064 return LY_SUCCESS;
6065 }
6066
6067 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006068 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006069
6070 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006071 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006072 LY_CHECK_RET(rc);
6073
6074 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006075 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006076
6077 return LY_SUCCESS;
6078}
6079
6080/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006081 * @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 +02006082 * Context position aware.
6083 *
6084 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006085 * @param[in] mod Matching metadata module, NULL for any.
6086 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006087 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006088 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6089 */
6090static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006091moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006092{
Michal Vasko9f96a052020-03-10 09:41:45 +01006093 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006094 struct lyxp_set *set_all_desc = NULL;
6095 LY_ERR rc;
6096
aPiecek8b0cc152021-05-31 16:40:31 +02006097 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006098 return LY_SUCCESS;
6099 }
6100
6101 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006102 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006103 return LY_EVALID;
6104 }
6105
Michal Vasko03ff5a72019-09-11 13:49:33 +02006106 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6107 * but it likely won't be used much, so it's a waste of time */
6108 /* copy the context */
6109 set_all_desc = set_copy(set);
6110 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006111 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006112 if (rc != LY_SUCCESS) {
6113 lyxp_set_free(set_all_desc);
6114 return rc;
6115 }
6116 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006117 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006118 if (rc != LY_SUCCESS) {
6119 lyxp_set_free(set_all_desc);
6120 return rc;
6121 }
6122 lyxp_set_free(set_all_desc);
6123
Radek Krejci1deb5be2020-08-26 16:43:36 +02006124 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006125 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006126
6127 /* only attributes of an elem can be in the result, skip all the rest,
6128 * we have all attributes qualified in lyd tree */
6129 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006130 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006131 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006132 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006133 continue;
6134 }
6135
Michal Vaskod3678892020-05-21 10:06:58 +02006136 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006137 /* match */
6138 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006139 set->val.meta[i].meta = sub;
6140 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006141 /* pos does not change */
6142 replaced = 1;
6143 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006144 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 +02006145 }
6146 ++i;
6147 }
6148 }
6149 }
6150
6151 if (!replaced) {
6152 /* no match */
6153 set_remove_node(set, i);
6154 }
6155 }
6156
6157 return LY_SUCCESS;
6158}
6159
6160/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006161 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6162 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006163 *
6164 * @param[in] parent Current parent.
6165 * @param[in] parent_pos Position of @p parent.
6166 * @param[in] parent_type Node type of @p parent.
6167 * @param[in,out] to_set Set to use.
6168 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006169 * @param[in] options XPath options.
6170 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6171 */
6172static LY_ERR
6173moveto_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 +02006174 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006175{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006176 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006177 LY_ERR rc;
6178
6179 switch (parent_type) {
6180 case LYXP_NODE_ROOT:
6181 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006182 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006183
Michal Vasko61ac2f62020-05-25 12:39:51 +02006184 /* add all top-level nodes as elements */
6185 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006186 break;
6187 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006188 /* add just the text node of this term element node */
6189 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006190 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6191 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6192 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006193 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006194 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006195
6196 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006197 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006198 break;
6199 default:
6200 LOGINT_RET(parent->schema->module->ctx);
6201 }
6202
Michal Vasko61ac2f62020-05-25 12:39:51 +02006203 /* add all top-level nodes as elements */
6204 LY_LIST_FOR(first, iter) {
6205 /* context check */
6206 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6207 continue;
6208 }
6209
6210 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006211 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006212 return LY_EINCOMPLETE;
6213 }
6214
6215 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6216 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6217
6218 /* also add all the children of this node, recursively */
6219 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6220 LY_CHECK_RET(rc);
6221 }
6222 }
6223
Michal Vasko03ff5a72019-09-11 13:49:33 +02006224 return LY_SUCCESS;
6225}
6226
6227/**
6228 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6229 * (or LYXP_SET_EMPTY). Context position aware.
6230 *
6231 * @param[in,out] set Set to use.
6232 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6233 * @param[in] options XPath options.
6234 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6235 */
6236static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006237moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006238{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006239 struct lyxp_set ret_set;
6240 LY_ERR rc;
6241
aPiecek8b0cc152021-05-31 16:40:31 +02006242 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006243 return LY_SUCCESS;
6244 }
6245
6246 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006247 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006248 return LY_EVALID;
6249 }
6250
6251 /* nothing to do */
6252 if (!all_desc) {
6253 return LY_SUCCESS;
6254 }
6255
Michal Vasko03ff5a72019-09-11 13:49:33 +02006256 /* add all the children, they get added recursively */
6257 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006258 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006259 /* copy the current node to tmp */
6260 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6261
6262 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006263 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006264 continue;
6265 }
6266
Michal Vasko03ff5a72019-09-11 13:49:33 +02006267 /* add all the children */
6268 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 +02006269 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006270 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006271 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006272 return rc;
6273 }
6274 }
6275
6276 /* use the temporary set as the current one */
6277 ret_set.ctx_pos = set->ctx_pos;
6278 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006279 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006280 memcpy(set, &ret_set, sizeof *set);
6281
6282 return LY_SUCCESS;
6283}
6284
6285/**
6286 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6287 * (or LYXP_SET_EMPTY).
6288 *
6289 * @param[in,out] set Set to use.
6290 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6291 * @param[in] options XPath options.
6292 * @return LY_ERR
6293 */
6294static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006295moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006296{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006297 uint32_t getnext_opts;
6298 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006299 const struct lysc_node *iter, *start_parent;
6300 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006301
aPiecek8b0cc152021-05-31 16:40:31 +02006302 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006303 return LY_SUCCESS;
6304 }
6305
6306 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006307 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006308 return LY_EVALID;
6309 }
6310
Michal Vasko519fd602020-05-26 12:17:39 +02006311 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006312 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006313 if (options & LYXP_SCNODE_OUTPUT) {
6314 getnext_opts |= LYS_GETNEXT_OUTPUT;
6315 }
6316
6317 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006318 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006319 if (!all_desc) {
6320 /* traverse the start node */
6321 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6322 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6323 }
6324 continue;
6325 }
6326
Radek Krejcif13b87b2020-12-01 22:02:17 +01006327 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6328 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006329 continue;
6330 }
6331
Michal Vasko519fd602020-05-26 12:17:39 +02006332 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006333 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006334 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006335 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006336 }
6337
Michal Vasko519fd602020-05-26 12:17:39 +02006338 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006339
Michal Vasko519fd602020-05-26 12:17:39 +02006340 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6341 /* it can actually be in any module, it's all <running> */
6342 mod_idx = 0;
Michal Vaskoa51ef072021-07-02 10:40:30 +02006343 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02006344 iter = NULL;
6345 /* module may not be implemented */
6346 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6347 /* context check */
6348 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6349 continue;
6350 }
6351
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006352 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006353 /* throw away the insert index, we want to consider that node again, recursively */
6354 }
6355 }
6356
6357 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6358 iter = NULL;
6359 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006360 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006361 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006362 continue;
6363 }
6364
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006365 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006366 }
6367 }
6368 }
6369
6370 return LY_SUCCESS;
6371}
6372
6373/**
6374 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6375 * (or LYXP_SET_EMPTY). Context position aware.
6376 *
6377 * @param[in] set Set to use.
6378 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6379 * @param[in] options XPath options.
6380 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6381 */
6382static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006383moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006384{
6385 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006386 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006387 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006388
aPiecek8b0cc152021-05-31 16:40:31 +02006389 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006390 return LY_SUCCESS;
6391 }
6392
6393 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006394 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006395 return LY_EVALID;
6396 }
6397
6398 if (all_desc) {
6399 /* <path>//.. == <path>//./.. */
6400 rc = moveto_self(set, 1, options);
6401 LY_CHECK_RET(rc);
6402 }
6403
Radek Krejci1deb5be2020-08-26 16:43:36 +02006404 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006405 node = set->val.nodes[i].node;
6406
6407 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9e685082021-01-29 14:49:09 +01006408 new_node = lyd_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006409 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6410 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006411 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6412 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006413 if (!new_node) {
6414 LOGINT_RET(set->ctx);
6415 }
6416 } else {
6417 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006418 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006419 continue;
6420 }
6421
Michal Vaskoa1424542019-11-14 16:08:52 +01006422 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006423 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 +02006424 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006425 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006426
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006427 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006428 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006429 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006430
Michal Vasko03ff5a72019-09-11 13:49:33 +02006431 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006432 /* 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 +02006433 new_type = LYXP_NODE_ELEM;
6434 }
6435
Michal Vasko03ff5a72019-09-11 13:49:33 +02006436 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006437 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006438 } else {
6439 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006440 }
6441 }
6442
Michal Vasko2caefc12019-11-14 16:07:56 +01006443 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006444 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006445
6446 return LY_SUCCESS;
6447}
6448
6449/**
6450 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6451 * (or LYXP_SET_EMPTY).
6452 *
6453 * @param[in] set Set to use.
6454 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6455 * @param[in] options XPath options.
6456 * @return LY_ERR
6457 */
6458static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006459moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006460{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006461 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006462 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006463 const struct lysc_node *node, *new_node;
6464 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006465
aPiecek8b0cc152021-05-31 16:40:31 +02006466 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006467 return LY_SUCCESS;
6468 }
6469
6470 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006471 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006472 return LY_EVALID;
6473 }
6474
6475 if (all_desc) {
6476 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006477 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006478 }
6479
Michal Vasko03ff5a72019-09-11 13:49:33 +02006480 orig_used = set->used;
6481 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006482 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6483 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006484 continue;
6485 }
6486
6487 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006488 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006489 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006490 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006491 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006492
6493 node = set->val.scnodes[i].scnode;
6494
6495 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006496 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006497 } else {
6498 /* root does not have a parent */
6499 continue;
6500 }
6501
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006502 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006503 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006504 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006505
Michal Vasko03ff5a72019-09-11 13:49:33 +02006506 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006507 /* 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 +02006508 new_type = LYXP_NODE_ELEM;
6509 }
6510
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006511 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6512 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006513 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006514 temp_ctx = 1;
6515 }
6516 }
6517
6518 if (temp_ctx) {
6519 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006520 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6521 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006522 }
6523 }
6524 }
6525
6526 return LY_SUCCESS;
6527}
6528
6529/**
6530 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6531 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6532 *
6533 * @param[in,out] set1 Set to use for the result.
6534 * @param[in] set2 Set acting as the second operand for @p op.
6535 * @param[in] op Comparison operator to process.
6536 * @param[in] options XPath options.
6537 * @return LY_ERR
6538 */
6539static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006540moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006541{
6542 /*
6543 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6544 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6545 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6546 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6547 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6548 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6549 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6550 *
6551 * '=' or '!='
6552 * BOOLEAN + BOOLEAN
6553 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6554 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6555 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6556 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6557 * NUMBER + NUMBER
6558 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6559 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6560 * STRING + STRING
6561 *
6562 * '<=', '<', '>=', '>'
6563 * NUMBER + NUMBER
6564 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6565 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6566 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6567 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6568 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6569 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6570 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6571 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6572 */
6573 struct lyxp_set iter1, iter2;
6574 int result;
6575 int64_t i;
6576 LY_ERR rc;
6577
Michal Vasko004d3152020-06-11 19:59:22 +02006578 memset(&iter1, 0, sizeof iter1);
6579 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006580
6581 /* iterative evaluation with node-sets */
6582 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6583 if (set1->type == LYXP_SET_NODE_SET) {
6584 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006585 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006586 switch (set2->type) {
6587 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006588 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006589 break;
6590 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006591 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006592 break;
6593 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006594 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006595 break;
6596 }
6597 LY_CHECK_RET(rc);
6598
Michal Vasko4c7763f2020-07-27 17:40:37 +02006599 /* canonize set2 */
6600 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6601
6602 /* compare recursively */
6603 rc = moveto_op_comp(&iter1, &iter2, op, options);
6604 lyxp_set_free_content(&iter2);
6605 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006606
6607 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006608 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006609 set_fill_boolean(set1, 1);
6610 return LY_SUCCESS;
6611 }
6612 }
6613 } else {
6614 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006615 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006616 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006617 case LYXP_SET_NUMBER:
6618 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6619 break;
6620 case LYXP_SET_BOOLEAN:
6621 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6622 break;
6623 default:
6624 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6625 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006626 }
6627 LY_CHECK_RET(rc);
6628
Michal Vasko4c7763f2020-07-27 17:40:37 +02006629 /* canonize set1 */
6630 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 +02006631
Michal Vasko4c7763f2020-07-27 17:40:37 +02006632 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006633 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006634 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006635 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006636
6637 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006638 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006639 set_fill_boolean(set1, 1);
6640 return LY_SUCCESS;
6641 }
6642 }
6643 }
6644
6645 /* false for all nodes */
6646 set_fill_boolean(set1, 0);
6647 return LY_SUCCESS;
6648 }
6649
6650 /* first convert properly */
6651 if ((op[0] == '=') || (op[0] == '!')) {
6652 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006653 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6654 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006655 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006656 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006657 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006658 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006659 LY_CHECK_RET(rc);
6660 } /* else we have 2 strings */
6661 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006662 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006663 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006664 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006665 LY_CHECK_RET(rc);
6666 }
6667
6668 assert(set1->type == set2->type);
6669
6670 /* compute result */
6671 if (op[0] == '=') {
6672 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006673 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006674 } else if (set1->type == LYXP_SET_NUMBER) {
6675 result = (set1->val.num == set2->val.num);
6676 } else {
6677 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006678 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006679 }
6680 } else if (op[0] == '!') {
6681 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006682 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006683 } else if (set1->type == LYXP_SET_NUMBER) {
6684 result = (set1->val.num != set2->val.num);
6685 } else {
6686 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006687 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006688 }
6689 } else {
6690 assert(set1->type == LYXP_SET_NUMBER);
6691 if (op[0] == '<') {
6692 if (op[1] == '=') {
6693 result = (set1->val.num <= set2->val.num);
6694 } else {
6695 result = (set1->val.num < set2->val.num);
6696 }
6697 } else {
6698 if (op[1] == '=') {
6699 result = (set1->val.num >= set2->val.num);
6700 } else {
6701 result = (set1->val.num > set2->val.num);
6702 }
6703 }
6704 }
6705
6706 /* assign result */
6707 if (result) {
6708 set_fill_boolean(set1, 1);
6709 } else {
6710 set_fill_boolean(set1, 0);
6711 }
6712
6713 return LY_SUCCESS;
6714}
6715
6716/**
6717 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6718 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6719 *
6720 * @param[in,out] set1 Set to use for the result.
6721 * @param[in] set2 Set acting as the second operand for @p op.
6722 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006723 * @return LY_ERR
6724 */
6725static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006726moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006727{
6728 LY_ERR rc;
6729
6730 /* unary '-' */
6731 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006732 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006733 LY_CHECK_RET(rc);
6734 set1->val.num *= -1;
6735 lyxp_set_free(set2);
6736 return LY_SUCCESS;
6737 }
6738
6739 assert(set1 && set2);
6740
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006741 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006742 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006743 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006744 LY_CHECK_RET(rc);
6745
6746 switch (op[0]) {
6747 /* '+' */
6748 case '+':
6749 set1->val.num += set2->val.num;
6750 break;
6751
6752 /* '-' */
6753 case '-':
6754 set1->val.num -= set2->val.num;
6755 break;
6756
6757 /* '*' */
6758 case '*':
6759 set1->val.num *= set2->val.num;
6760 break;
6761
6762 /* 'div' */
6763 case 'd':
6764 set1->val.num /= set2->val.num;
6765 break;
6766
6767 /* 'mod' */
6768 case 'm':
6769 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6770 break;
6771
6772 default:
6773 LOGINT_RET(set1->ctx);
6774 }
6775
6776 return LY_SUCCESS;
6777}
6778
6779/*
6780 * eval functions
6781 *
6782 * They execute a parsed XPath expression on some data subtree.
6783 */
6784
6785/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006786 * @brief Evaluate Predicate. Logs directly on error.
6787 *
Michal Vaskod3678892020-05-21 10:06:58 +02006788 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006789 *
6790 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006791 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02006792 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006793 * @param[in] options XPath options.
6794 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6795 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6796 */
6797static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006798eval_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 +02006799{
6800 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006801 uint16_t orig_exp;
6802 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006803 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006804 struct lyxp_set set2;
6805 struct lyd_node *orig_parent;
6806
6807 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02006808 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006809 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006810 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006811
aPiecek8b0cc152021-05-31 16:40:31 +02006812 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006813only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02006814 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006815 LY_CHECK_RET(rc);
6816 } else if (set->type == LYXP_SET_NODE_SET) {
6817 /* 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 +01006818 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006819
6820 /* empty set, nothing to evaluate */
6821 if (!set->used) {
6822 goto only_parse;
6823 }
6824
Michal Vasko004d3152020-06-11 19:59:22 +02006825 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006826 orig_pos = 0;
6827 orig_size = set->used;
6828 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006829 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006830 set_init(&set2, set);
6831 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6832 /* remember the node context position for position() and context size for last(),
6833 * predicates should always be evaluated with respect to the child axis (since we do
6834 * not support explicit axes) so we assign positions based on their parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006835 if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) {
6836 orig_parent = lyd_parent(set->val.nodes[i].node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006837 orig_pos = 1;
6838 } else {
6839 ++orig_pos;
6840 }
6841
6842 set2.ctx_pos = orig_pos;
6843 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006844 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006845
Michal Vasko004d3152020-06-11 19:59:22 +02006846 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006847 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006848 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006849 return rc;
6850 }
6851
6852 /* number is a position */
6853 if (set2.type == LYXP_SET_NUMBER) {
6854 if ((long long)set2.val.num == orig_pos) {
6855 set2.val.num = 1;
6856 } else {
6857 set2.val.num = 0;
6858 }
6859 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006860 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006861
6862 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006863 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006864 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006865 }
6866 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006867 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006868
6869 } else if (set->type == LYXP_SET_SCNODE_SET) {
6870 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006871 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006872 /* there is a currently-valid node */
6873 break;
6874 }
6875 }
6876 /* empty set, nothing to evaluate */
6877 if (i == set->used) {
6878 goto only_parse;
6879 }
6880
Michal Vasko004d3152020-06-11 19:59:22 +02006881 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006882
Michal Vasko03ff5a72019-09-11 13:49:33 +02006883 /* set special in_ctx to all the valid snodes */
6884 pred_in_ctx = set_scnode_new_in_ctx(set);
6885
6886 /* use the valid snodes one-by-one */
6887 for (i = 0; i < set->used; ++i) {
6888 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6889 continue;
6890 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006891 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006892
Michal Vasko004d3152020-06-11 19:59:22 +02006893 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006894
Michal Vasko004d3152020-06-11 19:59:22 +02006895 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006896 LY_CHECK_RET(rc);
6897
6898 set->val.scnodes[i].in_ctx = pred_in_ctx;
6899 }
6900
6901 /* restore the state as it was before the predicate */
6902 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006903 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02006904 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006905 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006906 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006907 }
6908 }
6909
6910 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006911 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006912 set_fill_set(&set2, set);
6913
Michal Vasko004d3152020-06-11 19:59:22 +02006914 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006915 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006916 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006917 return rc;
6918 }
6919
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006920 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006921 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006922 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006923 }
Michal Vaskod3678892020-05-21 10:06:58 +02006924 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006925 }
6926
6927 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006928 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02006929 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006930 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006931 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006932
6933 return LY_SUCCESS;
6934}
6935
6936/**
Michal Vaskod3678892020-05-21 10:06:58 +02006937 * @brief Evaluate Literal. Logs directly on error.
6938 *
6939 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006940 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006941 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6942 */
6943static void
Michal Vasko40308e72020-10-20 16:38:40 +02006944eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006945{
6946 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006947 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006948 set_fill_string(set, "", 0);
6949 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006950 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 +02006951 }
6952 }
6953 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006954 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006955 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006956}
6957
6958/**
Michal Vasko004d3152020-06-11 19:59:22 +02006959 * @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 +02006960 *
Michal Vasko004d3152020-06-11 19:59:22 +02006961 * @param[in] exp Full parsed XPath expression.
6962 * @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 +02006963 * @param[in] ctx_node Found schema node as the context for the predicate.
6964 * @param[in] cur_mod Current module for the expression.
6965 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006966 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006967 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006968 * @param[out] predicates Parsed predicates.
6969 * @param[out] pred_type Type of @p predicates.
6970 * @return LY_SUCCESS on success,
6971 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006972 */
6973static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006974eval_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 +02006975 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 +02006976 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006977{
Michal Vasko004d3152020-06-11 19:59:22 +02006978 LY_ERR ret = LY_SUCCESS;
6979 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006980 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006981 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006982 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006983 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006984
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006985 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006986
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006987 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006988 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006989 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006990 return LY_EINVAL;
6991 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006992 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 +02006993 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006994
Michal Vasko004d3152020-06-11 19:59:22 +02006995 /* learn where the predicates end */
6996 e_idx = *tok_idx;
6997 while (key_count) {
6998 /* '[' */
6999 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7000 return LY_EINVAL;
7001 }
7002 ++e_idx;
7003
Michal Vasko3354d272021-04-06 09:40:06 +02007004 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
7005 /* definitely not a key */
7006 return LY_EINVAL;
7007 }
7008
Michal Vasko004d3152020-06-11 19:59:22 +02007009 /* ']' */
7010 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7011 ++e_idx;
7012 }
7013 ++e_idx;
7014
7015 /* another presumably key predicate parsed */
7016 --key_count;
7017 }
Michal Vasko004d3152020-06-11 19:59:22 +02007018 } else {
7019 /* learn just where this single predicate ends */
7020 e_idx = *tok_idx;
7021
Michal Vaskod3678892020-05-21 10:06:58 +02007022 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007023 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7024 return LY_EINVAL;
7025 }
7026 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007027
Michal Vasko3354d272021-04-06 09:40:06 +02007028 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
7029 /* definitely not the value */
7030 return LY_EINVAL;
7031 }
7032
Michal Vaskod3678892020-05-21 10:06:58 +02007033 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007034 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7035 ++e_idx;
7036 }
7037 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007038 }
7039
Michal Vasko004d3152020-06-11 19:59:22 +02007040 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
7041 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
7042
7043 /* turn logging off */
7044 prev_lo = ly_log_options(0);
7045
7046 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007047 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
7048 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007049
7050 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007051 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
7052 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02007053 LY_CHECK_GOTO(ret, cleanup);
7054
7055 /* success, the predicate must include all the needed information for hash-based search */
7056 *tok_idx = e_idx;
7057
7058cleanup:
7059 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007060 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02007061 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02007062}
7063
7064/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007065 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7066 * data node(s) could be found using a single hash-based search.
7067 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007068 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007069 * @param[in] node Next context node to check.
7070 * @param[in] name Expected node name.
7071 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007072 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007073 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007074 * @param[in] format Prefix format.
7075 * @param[in,out] found Previously found node, is updated.
7076 * @return LY_SUCCESS on success,
7077 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7078 */
7079static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007080eval_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 +02007081 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 +02007082 const struct lysc_node **found)
7083{
7084 const struct lysc_node *scnode;
7085 const struct lys_module *mod;
7086 uint32_t idx = 0;
7087
Radek Krejci8df109d2021-04-23 12:19:08 +02007088 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007089
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007090continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007091 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007092 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007093 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007094 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007095 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007096 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7097 if (scnode) {
7098 /* we have found a match */
7099 break;
7100 }
7101 }
7102
Michal Vasko7d1d0912020-10-16 08:38:30 +02007103 if (!scnode) {
7104 /* all modules searched */
7105 idx = 0;
7106 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007107 } else {
7108 /* search in top-level */
7109 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7110 }
7111 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007112 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007113 /* we must adjust the module to inherit the one from the context node */
7114 moveto_mod = node->schema->module;
7115 }
7116
7117 /* search in children, do not repeat the same search */
7118 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007119 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007120
7121 /* additional context check */
7122 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7123 scnode = NULL;
7124 }
7125
7126 if (scnode) {
7127 if (*found) {
7128 /* we found a schema node with the same name but at different level, give up, too complicated
7129 * (more hash-based searches would be required, not supported) */
7130 return LY_ENOT;
7131 } else {
7132 /* remember the found schema node and continue to make sure it can be used */
7133 *found = scnode;
7134 }
7135 }
7136
7137 if (idx) {
7138 /* continue searching all the following models */
7139 goto continue_search;
7140 }
7141
7142 return LY_SUCCESS;
7143}
7144
7145/**
Michal Vaskod3678892020-05-21 10:06:58 +02007146 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7147 *
7148 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7149 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7150 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7151 *
7152 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007153 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007154 * @param[in] attr_axis Whether to search attributes or standard nodes.
7155 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007156 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007157 * @param[in] options XPath options.
7158 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7159 */
7160static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007161eval_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 +02007162 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007163{
Michal Vaskod3678892020-05-21 10:06:58 +02007164 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007165 const char *ncname, *ncname_dict = NULL;
7166 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007167 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007168 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007169 struct ly_path_predicate *predicates = NULL;
7170 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007171 LY_ERR rc = LY_SUCCESS;
7172
aPiecek8b0cc152021-05-31 16:40:31 +02007173 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007174 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007175 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007176
aPiecek8b0cc152021-05-31 16:40:31 +02007177 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02007178 goto moveto;
7179 }
7180
Michal Vasko004d3152020-06-11 19:59:22 +02007181 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7182 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007183
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007184 if ((ncname[0] == '*') && (ncname_len == 1)) {
7185 /* all nodes will match */
7186 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007187 }
7188
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007189 /* parse (and skip) module name */
7190 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007191 LY_CHECK_GOTO(rc, cleanup);
7192
Radek Krejci8df109d2021-04-23 12:19:08 +02007193 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 +02007194 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007195 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007196 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7197 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007198 /* check failed */
7199 scnode = NULL;
7200 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007201 }
7202 }
7203
Michal Vasko004d3152020-06-11 19:59:22 +02007204 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7205 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007206 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7207 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007208 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007209 scnode = NULL;
7210 }
7211 }
7212 }
7213
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007214 if (!scnode) {
7215 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007216 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007217 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007218 }
7219
7220moveto:
7221 /* move to the attribute(s), data node(s), or schema node(s) */
7222 if (attr_axis) {
aPiecek8b0cc152021-05-31 16:40:31 +02007223 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007224 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007225 } else {
7226 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007227 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007228 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007229 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007230 }
7231 LY_CHECK_GOTO(rc, cleanup);
7232 }
7233 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007234 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007235 int64_t i;
7236
Michal Vaskod3678892020-05-21 10:06:58 +02007237 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007238 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007239 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007240 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007241 }
7242 LY_CHECK_GOTO(rc, cleanup);
7243
7244 for (i = set->used - 1; i > -1; --i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007245 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vaskod3678892020-05-21 10:06:58 +02007246 break;
7247 }
7248 }
7249 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007250 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vasko90f12dc2020-12-03 14:20:42 +01007251 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".",
7252 ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007253 free(path);
7254 }
7255 } else {
7256 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007257 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007258 } else {
7259 if (scnode) {
7260 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007261 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007262 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007263 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007264 }
7265 }
7266 LY_CHECK_GOTO(rc, cleanup);
7267 }
7268 }
7269
7270 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007271 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7272 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007273 LY_CHECK_RET(rc);
7274 }
7275
7276cleanup:
aPiecek8b0cc152021-05-31 16:40:31 +02007277 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007278 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007279 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007280 }
Michal Vaskod3678892020-05-21 10:06:58 +02007281 return rc;
7282}
7283
7284/**
7285 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7286 *
7287 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7288 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7289 * [8] NodeType ::= 'text' | 'node'
7290 *
7291 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007292 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007293 * @param[in] attr_axis Whether to search attributes or standard nodes.
7294 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007295 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007296 * @param[in] options XPath options.
7297 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7298 */
7299static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007300eval_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 +02007301 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007302{
7303 LY_ERR rc;
7304
7305 /* TODO */
7306 (void)attr_axis;
7307 (void)all_desc;
7308
aPiecek8b0cc152021-05-31 16:40:31 +02007309 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007310 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007311 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007312 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
aPiecek8b0cc152021-05-31 16:40:31 +02007313 options |= LYXP_SKIP_EXPR;
Michal Vaskod3678892020-05-21 10:06:58 +02007314 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007315 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007316 rc = xpath_node(NULL, 0, set, options);
7317 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007318 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007319 rc = xpath_text(NULL, 0, set, options);
7320 }
7321 LY_CHECK_RET(rc);
7322 }
7323 }
aPiecek8b0cc152021-05-31 16:40:31 +02007324 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007325 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007326 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007327
7328 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007329 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007330 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007331 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007332 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007333
7334 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007335 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007336 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007337 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007338 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007339
7340 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007341 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7342 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007343 LY_CHECK_RET(rc);
7344 }
7345
7346 return LY_SUCCESS;
7347}
7348
7349/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007350 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7351 *
7352 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7353 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007354 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007355 *
7356 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007357 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007358 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007359 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007360 * @param[in] options XPath options.
7361 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7362 */
7363static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007364eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7365 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007366{
Radek Krejci857189e2020-09-01 13:26:36 +02007367 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007368 LY_ERR rc;
7369
7370 goto step;
7371 do {
7372 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007373 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007374 all_desc = 0;
7375 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007376 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007377 all_desc = 1;
7378 }
aPiecek8b0cc152021-05-31 16:40:31 +02007379 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007380 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007381 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007382
7383step:
Michal Vaskod3678892020-05-21 10:06:58 +02007384 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007385 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007386 attr_axis = 1;
7387
aPiecek8b0cc152021-05-31 16:40:31 +02007388 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007389 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007390 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007391 } else {
7392 attr_axis = 0;
7393 }
7394
Michal Vasko03ff5a72019-09-11 13:49:33 +02007395 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007396 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007397 case LYXP_TOKEN_DOT:
7398 /* evaluate '.' */
aPiecek8b0cc152021-05-31 16:40:31 +02007399 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007400 rc = moveto_scnode_self(set, all_desc, options);
7401 } else {
7402 rc = moveto_self(set, all_desc, options);
7403 }
7404 LY_CHECK_RET(rc);
7405 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007406 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007407 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007408 break;
7409
7410 case LYXP_TOKEN_DDOT:
7411 /* evaluate '..' */
aPiecek8b0cc152021-05-31 16:40:31 +02007412 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007413 rc = moveto_scnode_parent(set, all_desc, options);
7414 } else {
7415 rc = moveto_parent(set, all_desc, options);
7416 }
7417 LY_CHECK_RET(rc);
aPiecek8b0cc152021-05-31 16:40:31 +02007418 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007419 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007420 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007421 break;
7422
Michal Vasko03ff5a72019-09-11 13:49:33 +02007423 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007424 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007425 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007426 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007427 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007428
Michal Vaskod3678892020-05-21 10:06:58 +02007429 case LYXP_TOKEN_NODETYPE:
7430 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007431 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007432 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007433 break;
7434
7435 default:
aPiecek8b0cc152021-05-31 16:40:31 +02007436 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007437 }
Michal Vasko004d3152020-06-11 19:59:22 +02007438 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007439
7440 return LY_SUCCESS;
7441}
7442
7443/**
7444 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7445 *
7446 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7447 *
7448 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007449 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007450 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007451 * @param[in] options XPath options.
7452 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7453 */
7454static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007455eval_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 +02007456{
Radek Krejci857189e2020-09-01 13:26:36 +02007457 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007458
aPiecek8b0cc152021-05-31 16:40:31 +02007459 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007460 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007461 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007462 }
7463
7464 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007465 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007466 /* evaluate '/' - deferred */
7467 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02007468 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007469 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007470 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007471
Michal Vasko004d3152020-06-11 19:59:22 +02007472 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007473 return LY_SUCCESS;
7474 }
Michal Vasko004d3152020-06-11 19:59:22 +02007475 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007476 case LYXP_TOKEN_DOT:
7477 case LYXP_TOKEN_DDOT:
7478 case LYXP_TOKEN_AT:
7479 case LYXP_TOKEN_NAMETEST:
7480 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007481 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007482 break;
7483 default:
7484 break;
7485 }
7486
Michal Vasko03ff5a72019-09-11 13:49:33 +02007487 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007488 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007489 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7490 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02007491 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007492 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007493 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007494
Michal Vaskob0099a92020-08-31 14:55:23 +02007495 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007496 }
7497
7498 return LY_SUCCESS;
7499}
7500
7501/**
7502 * @brief Evaluate FunctionCall. Logs directly on error.
7503 *
Michal Vaskod3678892020-05-21 10:06:58 +02007504 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007505 *
7506 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007507 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007508 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007509 * @param[in] options XPath options.
7510 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7511 */
7512static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007513eval_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 +02007514{
7515 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007516
Radek Krejci1deb5be2020-08-26 16:43:36 +02007517 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007518 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007519 struct lyxp_set **args = NULL, **args_aux;
7520
aPiecek8b0cc152021-05-31 16:40:31 +02007521 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007522 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007523 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007524 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007525 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007526 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007527 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007528 xpath_func = &xpath_sum;
7529 }
7530 break;
7531 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007532 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007533 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007534 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007535 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007536 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007537 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007538 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007539 xpath_func = &xpath_true;
7540 }
7541 break;
7542 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007543 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007544 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007545 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007546 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007547 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007548 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007549 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007550 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007551 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007552 xpath_func = &xpath_deref;
7553 }
7554 break;
7555 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007556 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007557 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007558 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007559 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007560 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007561 xpath_func = &xpath_string;
7562 }
7563 break;
7564 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007565 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007566 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007567 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007568 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007569 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570 xpath_func = &xpath_current;
7571 }
7572 break;
7573 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007574 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007575 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007576 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007577 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007578 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007579 xpath_func = &xpath_re_match;
7580 }
7581 break;
7582 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007583 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007584 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007585 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007586 xpath_func = &xpath_translate;
7587 }
7588 break;
7589 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007590 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007591 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007592 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007593 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007594 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007595 xpath_func = &xpath_bit_is_set;
7596 }
7597 break;
7598 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007599 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007600 xpath_func = &xpath_starts_with;
7601 }
7602 break;
7603 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007604 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007605 xpath_func = &xpath_derived_from;
7606 }
7607 break;
7608 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007609 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007610 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007611 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007612 xpath_func = &xpath_string_length;
7613 }
7614 break;
7615 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007616 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007617 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007618 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007619 xpath_func = &xpath_substring_after;
7620 }
7621 break;
7622 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007623 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007624 xpath_func = &xpath_substring_before;
7625 }
7626 break;
7627 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007628 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007629 xpath_func = &xpath_derived_from_or_self;
7630 }
7631 break;
7632 }
7633
7634 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007635 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 +02007636 return LY_EVALID;
7637 }
7638 }
7639
aPiecek8b0cc152021-05-31 16:40:31 +02007640 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007641 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007642 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007643
7644 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007645 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007646 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007647 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007648 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007649
7650 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007651 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02007652 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007653 args = malloc(sizeof *args);
7654 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7655 arg_count = 1;
7656 args[0] = set_copy(set);
7657 if (!args[0]) {
7658 rc = LY_EMEM;
7659 goto cleanup;
7660 }
7661
Michal Vasko004d3152020-06-11 19:59:22 +02007662 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007663 LY_CHECK_GOTO(rc, cleanup);
7664 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007665 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007666 LY_CHECK_GOTO(rc, cleanup);
7667 }
7668 }
Michal Vasko004d3152020-06-11 19:59:22 +02007669 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02007670 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007671 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007672 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007673
aPiecek8b0cc152021-05-31 16:40:31 +02007674 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007675 ++arg_count;
7676 args_aux = realloc(args, arg_count * sizeof *args);
7677 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7678 args = args_aux;
7679 args[arg_count - 1] = set_copy(set);
7680 if (!args[arg_count - 1]) {
7681 rc = LY_EMEM;
7682 goto cleanup;
7683 }
7684
Michal Vasko004d3152020-06-11 19:59:22 +02007685 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007686 LY_CHECK_GOTO(rc, cleanup);
7687 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007688 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007689 LY_CHECK_GOTO(rc, cleanup);
7690 }
7691 }
7692
7693 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007694 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007695 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007696 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007697 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007698
aPiecek8b0cc152021-05-31 16:40:31 +02007699 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007700 /* evaluate function */
7701 rc = xpath_func(args, arg_count, set, options);
7702
7703 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007704 /* merge all nodes from arg evaluations */
7705 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007706 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007707 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007708 }
7709 }
7710 } else {
7711 rc = LY_SUCCESS;
7712 }
7713
7714cleanup:
7715 for (i = 0; i < arg_count; ++i) {
7716 lyxp_set_free(args[i]);
7717 }
7718 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007719 return rc;
7720}
7721
7722/**
7723 * @brief Evaluate Number. Logs directly on error.
7724 *
7725 * @param[in] ctx Context for errors.
7726 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007727 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007728 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7729 * @return LY_ERR
7730 */
7731static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007732eval_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 +02007733{
7734 long double num;
7735 char *endptr;
7736
7737 if (set) {
7738 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007739 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007740 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007741 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7742 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007743 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007744 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007745 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007746 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7747 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007748 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007749 return LY_EVALID;
7750 }
7751
7752 set_fill_number(set, num);
7753 }
7754
7755 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007756 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007757 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007758 return LY_SUCCESS;
7759}
7760
7761/**
7762 * @brief Evaluate PathExpr. Logs directly on error.
7763 *
Michal Vaskod3678892020-05-21 10:06:58 +02007764 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007765 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7766 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7767 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007768 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007769 *
7770 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007771 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007772 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007773 * @param[in] options XPath options.
7774 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7775 */
7776static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007777eval_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 +02007778{
Radek Krejci857189e2020-09-01 13:26:36 +02007779 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007780 LY_ERR rc;
7781
Michal Vasko004d3152020-06-11 19:59:22 +02007782 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007783 case LYXP_TOKEN_PAR1:
7784 /* '(' Expr ')' */
7785
7786 /* '(' */
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 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007792 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007793 LY_CHECK_RET(rc);
7794
7795 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007796 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007797 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007798 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007799 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007800
7801 parent_pos_pred = 0;
7802 goto predicate;
7803
7804 case LYXP_TOKEN_DOT:
7805 case LYXP_TOKEN_DDOT:
7806 case LYXP_TOKEN_AT:
7807 case LYXP_TOKEN_NAMETEST:
7808 case LYXP_TOKEN_NODETYPE:
7809 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007810 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007811 LY_CHECK_RET(rc);
7812 break;
7813
7814 case LYXP_TOKEN_FUNCNAME:
7815 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02007816 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007817 LY_CHECK_RET(rc);
7818
7819 parent_pos_pred = 1;
7820 goto predicate;
7821
Michal Vasko3e48bf32020-06-01 08:39:07 +02007822 case LYXP_TOKEN_OPER_PATH:
7823 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007824 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007825 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007826 LY_CHECK_RET(rc);
7827 break;
7828
7829 case LYXP_TOKEN_LITERAL:
7830 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02007831 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7832 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007833 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007834 }
Michal Vasko004d3152020-06-11 19:59:22 +02007835 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007836 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007837 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007838 }
7839
7840 parent_pos_pred = 1;
7841 goto predicate;
7842
7843 case LYXP_TOKEN_NUMBER:
7844 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02007845 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7846 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007847 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007848 }
Michal Vasko004d3152020-06-11 19:59:22 +02007849 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007850 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007851 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007852 }
7853 LY_CHECK_RET(rc);
7854
7855 parent_pos_pred = 1;
7856 goto predicate;
7857
7858 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01007859 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 +02007860 return LY_EVALID;
7861 }
7862
7863 return LY_SUCCESS;
7864
7865predicate:
7866 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007867 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7868 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007869 LY_CHECK_RET(rc);
7870 }
7871
7872 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007873 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007874
7875 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007876 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007877 all_desc = 0;
7878 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007879 all_desc = 1;
7880 }
7881
aPiecek8b0cc152021-05-31 16:40:31 +02007882 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007883 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007884 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007885
Michal Vasko004d3152020-06-11 19:59:22 +02007886 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007887 LY_CHECK_RET(rc);
7888 }
7889
7890 return LY_SUCCESS;
7891}
7892
7893/**
7894 * @brief Evaluate UnionExpr. Logs directly on error.
7895 *
Michal Vaskod3678892020-05-21 10:06:58 +02007896 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007897 *
7898 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007899 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007900 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007901 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007902 * @param[in] options XPath options.
7903 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7904 */
7905static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007906eval_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 +02007907{
7908 LY_ERR rc = LY_SUCCESS;
7909 struct lyxp_set orig_set, set2;
7910 uint16_t i;
7911
7912 assert(repeat);
7913
7914 set_init(&orig_set, set);
7915 set_init(&set2, set);
7916
7917 set_fill_set(&orig_set, set);
7918
Michal Vasko004d3152020-06-11 19:59:22 +02007919 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007920 LY_CHECK_GOTO(rc, cleanup);
7921
7922 /* ('|' PathExpr)* */
7923 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007924 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02007925 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007926 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007927 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007928
aPiecek8b0cc152021-05-31 16:40:31 +02007929 if (options & LYXP_SKIP_EXPR) {
7930 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007931 LY_CHECK_GOTO(rc, cleanup);
7932 continue;
7933 }
7934
7935 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007936 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007937 LY_CHECK_GOTO(rc, cleanup);
7938
7939 /* eval */
7940 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007941 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007942 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007943 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007944 LY_CHECK_GOTO(rc, cleanup);
7945 }
7946 }
7947
7948cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007949 lyxp_set_free_content(&orig_set);
7950 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007951 return rc;
7952}
7953
7954/**
7955 * @brief Evaluate UnaryExpr. Logs directly on error.
7956 *
Michal Vaskod3678892020-05-21 10:06:58 +02007957 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007958 *
7959 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007960 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007961 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007962 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007963 * @param[in] options XPath options.
7964 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7965 */
7966static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007967eval_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 +02007968{
7969 LY_ERR rc;
7970 uint16_t this_op, i;
7971
7972 assert(repeat);
7973
7974 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007975 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007976 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007977 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 +02007978
aPiecek8b0cc152021-05-31 16:40:31 +02007979 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007980 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007981 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007982 }
7983
Michal Vasko004d3152020-06-11 19:59:22 +02007984 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007985 LY_CHECK_RET(rc);
7986
aPiecek8b0cc152021-05-31 16:40:31 +02007987 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007988 if (options & LYXP_SCNODE_ALL) {
7989 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7990 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007991 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007992 LY_CHECK_RET(rc);
7993 }
7994 }
7995
7996 return LY_SUCCESS;
7997}
7998
7999/**
8000 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
8001 *
Michal Vaskod3678892020-05-21 10:06:58 +02008002 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008003 * | MultiplicativeExpr '*' UnaryExpr
8004 * | MultiplicativeExpr 'div' UnaryExpr
8005 * | MultiplicativeExpr 'mod' UnaryExpr
8006 *
8007 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008008 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008009 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008010 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008011 * @param[in] options XPath options.
8012 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8013 */
8014static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008015eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
8016 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008017{
8018 LY_ERR rc;
8019 uint16_t this_op;
8020 struct lyxp_set orig_set, set2;
8021 uint16_t i;
8022
8023 assert(repeat);
8024
8025 set_init(&orig_set, set);
8026 set_init(&set2, set);
8027
8028 set_fill_set(&orig_set, set);
8029
Michal Vasko004d3152020-06-11 19:59:22 +02008030 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008031 LY_CHECK_GOTO(rc, cleanup);
8032
8033 /* ('*' / 'div' / 'mod' UnaryExpr)* */
8034 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008035 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008036
Michal Vasko004d3152020-06-11 19:59:22 +02008037 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02008038 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008039 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008040 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008041
aPiecek8b0cc152021-05-31 16:40:31 +02008042 if (options & LYXP_SKIP_EXPR) {
8043 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008044 LY_CHECK_GOTO(rc, cleanup);
8045 continue;
8046 }
8047
8048 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008049 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008050 LY_CHECK_GOTO(rc, cleanup);
8051
8052 /* eval */
8053 if (options & LYXP_SCNODE_ALL) {
8054 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008055 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008056 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008057 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008058 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008059 LY_CHECK_GOTO(rc, cleanup);
8060 }
8061 }
8062
8063cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008064 lyxp_set_free_content(&orig_set);
8065 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008066 return rc;
8067}
8068
8069/**
8070 * @brief Evaluate AdditiveExpr. Logs directly on error.
8071 *
Michal Vaskod3678892020-05-21 10:06:58 +02008072 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008073 * | AdditiveExpr '+' MultiplicativeExpr
8074 * | AdditiveExpr '-' MultiplicativeExpr
8075 *
8076 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008077 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008078 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008079 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008080 * @param[in] options XPath options.
8081 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8082 */
8083static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008084eval_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 +02008085{
8086 LY_ERR rc;
8087 uint16_t this_op;
8088 struct lyxp_set orig_set, set2;
8089 uint16_t i;
8090
8091 assert(repeat);
8092
8093 set_init(&orig_set, set);
8094 set_init(&set2, set);
8095
8096 set_fill_set(&orig_set, set);
8097
Michal Vasko004d3152020-06-11 19:59:22 +02008098 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008099 LY_CHECK_GOTO(rc, cleanup);
8100
8101 /* ('+' / '-' MultiplicativeExpr)* */
8102 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008103 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008104
Michal Vasko004d3152020-06-11 19:59:22 +02008105 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008106 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008107 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008108 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008109
aPiecek8b0cc152021-05-31 16:40:31 +02008110 if (options & LYXP_SKIP_EXPR) {
8111 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008112 LY_CHECK_GOTO(rc, cleanup);
8113 continue;
8114 }
8115
8116 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008117 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008118 LY_CHECK_GOTO(rc, cleanup);
8119
8120 /* eval */
8121 if (options & LYXP_SCNODE_ALL) {
8122 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008123 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008124 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008125 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008126 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008127 LY_CHECK_GOTO(rc, cleanup);
8128 }
8129 }
8130
8131cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008132 lyxp_set_free_content(&orig_set);
8133 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008134 return rc;
8135}
8136
8137/**
8138 * @brief Evaluate RelationalExpr. Logs directly on error.
8139 *
Michal Vaskod3678892020-05-21 10:06:58 +02008140 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008141 * | RelationalExpr '<' AdditiveExpr
8142 * | RelationalExpr '>' AdditiveExpr
8143 * | RelationalExpr '<=' AdditiveExpr
8144 * | RelationalExpr '>=' AdditiveExpr
8145 *
8146 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008147 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008148 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008149 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008150 * @param[in] options XPath options.
8151 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8152 */
8153static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008154eval_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 +02008155{
8156 LY_ERR rc;
8157 uint16_t this_op;
8158 struct lyxp_set orig_set, set2;
8159 uint16_t i;
8160
8161 assert(repeat);
8162
8163 set_init(&orig_set, set);
8164 set_init(&set2, set);
8165
8166 set_fill_set(&orig_set, set);
8167
Michal Vasko004d3152020-06-11 19:59:22 +02008168 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008169 LY_CHECK_GOTO(rc, cleanup);
8170
8171 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8172 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008173 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008174
Michal Vasko004d3152020-06-11 19:59:22 +02008175 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02008176 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008177 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008178 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008179
aPiecek8b0cc152021-05-31 16:40:31 +02008180 if (options & LYXP_SKIP_EXPR) {
8181 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008182 LY_CHECK_GOTO(rc, cleanup);
8183 continue;
8184 }
8185
8186 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008187 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008188 LY_CHECK_GOTO(rc, cleanup);
8189
8190 /* eval */
8191 if (options & LYXP_SCNODE_ALL) {
8192 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008193 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008194 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008195 } else {
8196 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8197 LY_CHECK_GOTO(rc, cleanup);
8198 }
8199 }
8200
8201cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008202 lyxp_set_free_content(&orig_set);
8203 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008204 return rc;
8205}
8206
8207/**
8208 * @brief Evaluate EqualityExpr. Logs directly on error.
8209 *
Michal Vaskod3678892020-05-21 10:06:58 +02008210 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008211 * | EqualityExpr '!=' RelationalExpr
8212 *
8213 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008214 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008215 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008216 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008217 * @param[in] options XPath options.
8218 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8219 */
8220static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008221eval_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 +02008222{
8223 LY_ERR rc;
8224 uint16_t this_op;
8225 struct lyxp_set orig_set, set2;
8226 uint16_t i;
8227
8228 assert(repeat);
8229
8230 set_init(&orig_set, set);
8231 set_init(&set2, set);
8232
8233 set_fill_set(&orig_set, set);
8234
Michal Vasko004d3152020-06-11 19:59:22 +02008235 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008236 LY_CHECK_GOTO(rc, cleanup);
8237
8238 /* ('=' / '!=' RelationalExpr)* */
8239 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008240 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008241
Michal Vasko004d3152020-06-11 19:59:22 +02008242 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02008243 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008244 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008245 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008246
aPiecek8b0cc152021-05-31 16:40:31 +02008247 if (options & LYXP_SKIP_EXPR) {
8248 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008249 LY_CHECK_GOTO(rc, cleanup);
8250 continue;
8251 }
8252
8253 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008254 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008255 LY_CHECK_GOTO(rc, cleanup);
8256
8257 /* eval */
8258 if (options & LYXP_SCNODE_ALL) {
8259 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008260 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8261 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008262 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008263 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008264 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008265 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8266 LY_CHECK_GOTO(rc, cleanup);
8267 }
8268 }
8269
8270cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008271 lyxp_set_free_content(&orig_set);
8272 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008273 return rc;
8274}
8275
8276/**
8277 * @brief Evaluate AndExpr. Logs directly on error.
8278 *
Michal Vaskod3678892020-05-21 10:06:58 +02008279 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008280 *
8281 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008282 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008283 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008284 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008285 * @param[in] options XPath options.
8286 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8287 */
8288static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008289eval_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 +02008290{
8291 LY_ERR rc;
8292 struct lyxp_set orig_set, set2;
8293 uint16_t i;
8294
8295 assert(repeat);
8296
8297 set_init(&orig_set, set);
8298 set_init(&set2, set);
8299
8300 set_fill_set(&orig_set, set);
8301
Michal Vasko004d3152020-06-11 19:59:22 +02008302 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008303 LY_CHECK_GOTO(rc, cleanup);
8304
8305 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008306 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008307 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008308 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008309 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008310 }
8311
8312 /* ('and' EqualityExpr)* */
8313 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008314 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008315 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008316 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008317 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008318
8319 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008320 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8321 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008322 LY_CHECK_GOTO(rc, cleanup);
8323 continue;
8324 }
8325
8326 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008327 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008328 LY_CHECK_GOTO(rc, cleanup);
8329
8330 /* eval - just get boolean value actually */
8331 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008332 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008333 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008334 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008335 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008336 set_fill_set(set, &set2);
8337 }
8338 }
8339
8340cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008341 lyxp_set_free_content(&orig_set);
8342 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008343 return rc;
8344}
8345
8346/**
8347 * @brief Evaluate OrExpr. Logs directly on error.
8348 *
Michal Vaskod3678892020-05-21 10:06:58 +02008349 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008350 *
8351 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008352 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008353 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008354 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008355 * @param[in] options XPath options.
8356 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8357 */
8358static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008359eval_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 +02008360{
8361 LY_ERR rc;
8362 struct lyxp_set orig_set, set2;
8363 uint16_t i;
8364
8365 assert(repeat);
8366
8367 set_init(&orig_set, set);
8368 set_init(&set2, set);
8369
8370 set_fill_set(&orig_set, set);
8371
Michal Vasko004d3152020-06-11 19:59:22 +02008372 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008373 LY_CHECK_GOTO(rc, cleanup);
8374
8375 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008376 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008377 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008378 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008379 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008380 }
8381
8382 /* ('or' AndExpr)* */
8383 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008384 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008385 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008386 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008387 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008388
8389 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008390 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8391 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008392 LY_CHECK_GOTO(rc, cleanup);
8393 continue;
8394 }
8395
8396 set_fill_set(&set2, &orig_set);
8397 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8398 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008399 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008400 LY_CHECK_GOTO(rc, cleanup);
8401
8402 /* eval - just get boolean value actually */
8403 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008404 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008405 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008406 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008407 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008408 set_fill_set(set, &set2);
8409 }
8410 }
8411
8412cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008413 lyxp_set_free_content(&orig_set);
8414 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008415 return rc;
8416}
8417
8418/**
Michal Vasko004d3152020-06-11 19:59:22 +02008419 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008420 *
8421 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008422 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008423 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02008424 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008425 * @param[in] options XPath options.
8426 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8427 */
8428static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008429eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8430 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008431{
8432 uint16_t i, count;
8433 enum lyxp_expr_type next_etype;
8434 LY_ERR rc;
8435
8436 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008437 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008438 next_etype = LYXP_EXPR_NONE;
8439 } else {
8440 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008441 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008442
8443 /* select one-priority lower because etype expression called us */
8444 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008445 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008446 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008447 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008448 } else {
8449 next_etype = LYXP_EXPR_NONE;
8450 }
8451 }
8452
8453 /* decide what expression are we parsing based on the repeat */
8454 switch (next_etype) {
8455 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008456 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008457 break;
8458 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008459 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008460 break;
8461 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008462 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008463 break;
8464 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008465 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008466 break;
8467 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008468 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008469 break;
8470 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008471 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008472 break;
8473 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008474 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008475 break;
8476 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008477 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008478 break;
8479 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008480 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008481 break;
8482 default:
8483 LOGINT_RET(set->ctx);
8484 }
8485
8486 return rc;
8487}
8488
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008489/**
8490 * @brief Get root type.
8491 *
8492 * @param[in] ctx_node Context node.
8493 * @param[in] ctx_scnode Schema context node.
8494 * @param[in] options XPath options.
8495 * @return Root type.
8496 */
8497static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008498lyxp_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 +01008499{
Michal Vasko6b26e742020-07-17 15:02:10 +02008500 const struct lysc_node *op;
8501
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008502 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008503 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008504 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008505
8506 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008507 /* general root that can access everything */
8508 return LYXP_NODE_ROOT;
8509 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8510 /* root context node can access only config data (because we said so, it is unspecified) */
8511 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008512 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008513 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008514 }
8515
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008516 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008517 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008518 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008519
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008520 if (op || !(options & LYXP_SCHEMA)) {
8521 /* general root that can access everything */
8522 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05008523 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008524 /* root context node can access only config data (because we said so, it is unspecified) */
8525 return LYXP_NODE_ROOT_CONFIG;
8526 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008527 return LYXP_NODE_ROOT;
8528}
8529
Michal Vasko03ff5a72019-09-11 13:49:33 +02008530LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008531lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008532 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 +01008533 struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008534{
Michal Vasko004d3152020-06-11 19:59:22 +02008535 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008536 LY_ERR rc;
8537
Michal Vasko400e9672021-01-11 13:39:17 +01008538 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008539 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008540 LOGARG(NULL, "Current module must be set if schema format is used.");
8541 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008542 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008543
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008544 if (tree) {
8545 /* adjust the pointer to be the first top-level sibling */
8546 while (tree->parent) {
8547 tree = lyd_parent(tree);
8548 }
8549 tree = lyd_first_sibling(tree);
8550 }
8551
Michal Vasko03ff5a72019-09-11 13:49:33 +02008552 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008553 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008554 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008555 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8556 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8557
Michal Vasko400e9672021-01-11 13:39:17 +01008558 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008559 set->cur_node = ctx_node;
8560 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008561 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8562 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008563 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008564 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008565 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008566 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008567
Radek Krejciddace2c2021-01-08 11:30:56 +01008568 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008569
Michal Vasko03ff5a72019-09-11 13:49:33 +02008570 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008571 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008572 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008573 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008574 }
8575
Radek Krejciddace2c2021-01-08 11:30:56 +01008576 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008577 return rc;
8578}
8579
8580#if 0
8581
8582/* full xml printing of set elements, not used currently */
8583
8584void
8585lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8586{
8587 uint32_t i;
8588 char *str_num;
8589 struct lyout out;
8590
8591 memset(&out, 0, sizeof out);
8592
8593 out.type = LYOUT_STREAM;
8594 out.method.f = f;
8595
8596 switch (set->type) {
8597 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008598 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008599 break;
8600 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008601 ly_print_(&out, "Boolean XPath set:\n");
8602 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008603 break;
8604 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008605 ly_print_(&out, "String XPath set:\n");
8606 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008607 break;
8608 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008609 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008610
8611 if (isnan(set->value.num)) {
8612 str_num = strdup("NaN");
8613 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8614 str_num = strdup("0");
8615 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8616 str_num = strdup("Infinity");
8617 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8618 str_num = strdup("-Infinity");
8619 } else if ((long long)set->value.num == set->value.num) {
8620 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8621 str_num = NULL;
8622 }
8623 } else {
8624 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8625 str_num = NULL;
8626 }
8627 }
8628 if (!str_num) {
8629 LOGMEM;
8630 return;
8631 }
Michal Vasko5233e962020-08-14 14:26:20 +02008632 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008633 free(str_num);
8634 break;
8635 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008636 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008637
8638 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008639 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008640 switch (set->node_type[i]) {
8641 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008642 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008643 break;
8644 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008645 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008646 break;
8647 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008648 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008649 break;
8650 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008651 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008652 break;
8653 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008654 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008655 break;
8656 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008657 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008658 break;
8659 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008660 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008661 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008662 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008663 break;
8664 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008665 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 +02008666 break;
8667 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008668 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 +02008669 break;
8670 }
8671 }
8672 break;
8673 }
8674}
8675
8676#endif
8677
8678LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008679lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008680{
8681 long double num;
8682 char *str;
8683 LY_ERR rc;
8684
8685 if (!set || (set->type == target)) {
8686 return LY_SUCCESS;
8687 }
8688
8689 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008690 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008691
8692 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008693 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008694 return LY_EINVAL;
8695 }
8696
8697 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008698 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008699 switch (set->type) {
8700 case LYXP_SET_NUMBER:
8701 if (isnan(set->val.num)) {
8702 set->val.str = strdup("NaN");
8703 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8704 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8705 set->val.str = strdup("0");
8706 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8707 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8708 set->val.str = strdup("Infinity");
8709 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8710 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8711 set->val.str = strdup("-Infinity");
8712 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8713 } else if ((long long)set->val.num == set->val.num) {
8714 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8715 LOGMEM_RET(set->ctx);
8716 }
8717 set->val.str = str;
8718 } else {
8719 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8720 LOGMEM_RET(set->ctx);
8721 }
8722 set->val.str = str;
8723 }
8724 break;
8725 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008726 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008727 set->val.str = strdup("true");
8728 } else {
8729 set->val.str = strdup("false");
8730 }
8731 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8732 break;
8733 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008734 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008735 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008736
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008737 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008738 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008739 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008740 set->val.str = str;
8741 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008742 default:
8743 LOGINT_RET(set->ctx);
8744 }
8745 set->type = LYXP_SET_STRING;
8746 }
8747
8748 /* to NUMBER */
8749 if (target == LYXP_SET_NUMBER) {
8750 switch (set->type) {
8751 case LYXP_SET_STRING:
8752 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008753 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008754 set->val.num = num;
8755 break;
8756 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008757 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008758 set->val.num = 1;
8759 } else {
8760 set->val.num = 0;
8761 }
8762 break;
8763 default:
8764 LOGINT_RET(set->ctx);
8765 }
8766 set->type = LYXP_SET_NUMBER;
8767 }
8768
8769 /* to BOOLEAN */
8770 if (target == LYXP_SET_BOOLEAN) {
8771 switch (set->type) {
8772 case LYXP_SET_NUMBER:
8773 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008774 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008775 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008776 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008777 }
8778 break;
8779 case LYXP_SET_STRING:
8780 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008781 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008782 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008783 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008784 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008785 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008786 }
8787 break;
8788 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008789 if (set->used) {
8790 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008791 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008792 } else {
8793 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008794 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008795 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008796 break;
8797 default:
8798 LOGINT_RET(set->ctx);
8799 }
8800 set->type = LYXP_SET_BOOLEAN;
8801 }
8802
Michal Vasko03ff5a72019-09-11 13:49:33 +02008803 return LY_SUCCESS;
8804}
8805
8806LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008807lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008808 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
Michal Vasko400e9672021-01-11 13:39:17 +01008809 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008810{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008811 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008812 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008813
Michal Vasko400e9672021-01-11 13:39:17 +01008814 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008815 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008816 LOGARG(NULL, "Current module must be set if schema format is used.");
8817 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008818 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008819
8820 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008821 memset(set, 0, sizeof *set);
8822 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008823 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8824 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 +01008825 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008826
Michal Vasko400e9672021-01-11 13:39:17 +01008827 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008828 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008829 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008830 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8831 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008832 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008833 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008834 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008835
Radek Krejciddace2c2021-01-08 11:30:56 +01008836 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008837
Michal Vasko03ff5a72019-09-11 13:49:33 +02008838 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01008839 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
8840
Radek Krejciddace2c2021-01-08 11:30:56 +01008841 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008842 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008843}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008844
8845API const char *
8846lyxp_get_expr(const struct lyxp_expr *path)
8847{
8848 if (!path) {
8849 return NULL;
8850 }
8851
8852 return path->expr;
8853}