blob: 9f4d173412a2f8cca12f5951dcd3ca4acdfb47d6 [file] [log] [blame]
Radek Krejcib1646a92018-11-02 16:08:26 +01001/**
2 * @file xpath.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG XPath evaluation functions
5 *
Michal Vasko61ac2f62020-05-25 12:39:51 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcib1646a92018-11-02 16:08:26 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Christian Hopps32874e12021-05-01 09:43:54 -040014#define _GNU_SOURCE /* asprintf, strdup */
Radek Krejcib1646a92018-11-02 16:08:26 +010015
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010017
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010019#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010023#include <stdio.h>
24#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010025#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010026
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020028#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010032#include "out.h"
Radek Krejci7931b192020-06-25 17:05:03 +020033#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020034#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020035#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020037#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "tree.h"
Radek Krejci77114102021-03-10 15:21:57 +010039#include "tree_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020040#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010041#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020042#include "tree_schema_internal.h"
43#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020044
aPiecekbf968d92021-05-27 14:35:05 +020045static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth);
Michal Vasko40308e72020-10-20 16:38:40 +020046static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype,
47 struct lyxp_set *set, uint32_t options);
Michal Vasko93923692021-05-07 15:28:02 +020048static LY_ERR moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
49 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod);
Michal Vasko03ff5a72019-09-11 13:49:33 +020050
51/**
52 * @brief Print the type of an XPath \p set.
53 *
54 * @param[in] set Set to use.
55 * @return Set type string.
56 */
57static const char *
58print_set_type(struct lyxp_set *set)
59{
60 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020061 case LYXP_SET_NODE_SET:
62 return "node set";
63 case LYXP_SET_SCNODE_SET:
64 return "schema node set";
65 case LYXP_SET_BOOLEAN:
66 return "boolean";
67 case LYXP_SET_NUMBER:
68 return "number";
69 case LYXP_SET_STRING:
70 return "string";
71 }
72
73 return NULL;
74}
75
Michal Vasko24cddf82020-06-01 08:17:01 +020076const char *
77lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020078{
79 switch (tok) {
80 case LYXP_TOKEN_PAR1:
81 return "(";
82 case LYXP_TOKEN_PAR2:
83 return ")";
84 case LYXP_TOKEN_BRACK1:
85 return "[";
86 case LYXP_TOKEN_BRACK2:
87 return "]";
88 case LYXP_TOKEN_DOT:
89 return ".";
90 case LYXP_TOKEN_DDOT:
91 return "..";
92 case LYXP_TOKEN_AT:
93 return "@";
94 case LYXP_TOKEN_COMMA:
95 return ",";
96 case LYXP_TOKEN_NAMETEST:
97 return "NameTest";
98 case LYXP_TOKEN_NODETYPE:
99 return "NodeType";
100 case LYXP_TOKEN_FUNCNAME:
101 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200102 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200103 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200104 case LYXP_TOKEN_OPER_EQUAL:
105 return "Operator(Equal)";
106 case LYXP_TOKEN_OPER_NEQUAL:
107 return "Operator(Non-equal)";
108 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200109 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200110 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200111 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200112 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200113 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200114 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200115 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200116 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200117 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200118 case LYXP_TOKEN_LITERAL:
119 return "Literal";
120 case LYXP_TOKEN_NUMBER:
121 return "Number";
122 default:
123 LOGINT(NULL);
124 return "";
125 }
126}
127
128/**
129 * @brief Print the whole expression \p exp to debug output.
130 *
131 * @param[in] exp Expression to use.
132 */
133static void
Michal Vasko40308e72020-10-20 16:38:40 +0200134print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200135{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100136#define MSG_BUFFER_SIZE 128
137 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100138 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200139
Radek Krejci52b6d512020-10-12 12:33:17 +0200140 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200141 return;
142 }
143
144 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
145 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200146 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200147 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100148 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200149 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
150 for (j = 1; exp->repeat[i][j]; ++j) {
151 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
152 }
153 strcat(tmp, ")");
154 }
155 LOGDBG(LY_LDGXPATH, tmp);
156 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100157#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200158}
159
160#ifndef NDEBUG
161
162/**
163 * @brief Print XPath set content to debug output.
164 *
165 * @param[in] set Set to print.
166 */
167static void
168print_set_debug(struct lyxp_set *set)
169{
170 uint32_t i;
171 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200172 struct lyxp_set_node *item;
173 struct lyxp_set_scnode *sitem;
174
Radek Krejci52b6d512020-10-12 12:33:17 +0200175 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200176 return;
177 }
178
179 switch (set->type) {
180 case LYXP_SET_NODE_SET:
181 LOGDBG(LY_LDGXPATH, "set NODE SET:");
182 for (i = 0; i < set->used; ++i) {
183 item = &set->val.nodes[i];
184
185 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100186 case LYXP_NODE_NONE:
187 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
188 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200189 case LYXP_NODE_ROOT:
190 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
191 break;
192 case LYXP_NODE_ROOT_CONFIG:
193 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
194 break;
195 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100196 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200197 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200198 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100199 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200200 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200201 item->node->schema->name, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200202 } else {
203 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
204 }
205 break;
206 case LYXP_NODE_TEXT:
207 if (item->node->schema->nodetype & LYS_ANYDATA) {
208 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200209 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200210 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200211 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200212 }
213 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100214 case LYXP_NODE_META:
215 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
Michal Vasko69730152020-10-09 16:30:07 +0200216 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200217 break;
218 }
219 }
220 break;
221
222 case LYXP_SET_SCNODE_SET:
223 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
224 for (i = 0; i < set->used; ++i) {
225 sitem = &set->val.scnodes[i];
226
227 switch (sitem->type) {
228 case LYXP_NODE_ROOT:
229 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
230 break;
231 case LYXP_NODE_ROOT_CONFIG:
232 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
233 break;
234 case LYXP_NODE_ELEM:
235 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
236 break;
237 default:
238 LOGINT(NULL);
239 break;
240 }
241 }
242 break;
243
Michal Vasko03ff5a72019-09-11 13:49:33 +0200244 case LYXP_SET_BOOLEAN:
245 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200246 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200247 break;
248
249 case LYXP_SET_STRING:
250 LOGDBG(LY_LDGXPATH, "set STRING");
251 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
252 break;
253
254 case LYXP_SET_NUMBER:
255 LOGDBG(LY_LDGXPATH, "set NUMBER");
256
257 if (isnan(set->val.num)) {
258 str = strdup("NaN");
259 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
260 str = strdup("0");
261 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
262 str = strdup("Infinity");
263 } else if (isinf(set->val.num) && signbit(set->val.num)) {
264 str = strdup("-Infinity");
265 } else if ((long long)set->val.num == set->val.num) {
266 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
267 str = NULL;
268 }
269 } else {
270 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
271 str = NULL;
272 }
273 }
274 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
275
276 LOGDBG(LY_LDGXPATH, "\t%s", str);
277 free(str);
278 }
279}
280
281#endif
282
283/**
284 * @brief Realloc the string \p str.
285 *
286 * @param[in] ctx libyang context for logging.
287 * @param[in] needed How much free space is required.
288 * @param[in,out] str Pointer to the string to use.
289 * @param[in,out] used Used bytes in \p str.
290 * @param[in,out] size Allocated bytes in \p str.
291 * @return LY_ERR
292 */
293static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100294cast_string_realloc(const struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200295{
296 if (*size - *used < needed) {
297 do {
298 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
299 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
300 return LY_EINVAL;
301 }
302 *size += LYXP_STRING_CAST_SIZE_STEP;
303 } while (*size - *used < needed);
304 *str = ly_realloc(*str, *size * sizeof(char));
305 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
306 }
307
308 return LY_SUCCESS;
309}
310
311/**
312 * @brief Cast nodes recursively to one string @p str.
313 *
314 * @param[in] node Node to cast.
315 * @param[in] fake_cont Whether to put the data into a "fake" container.
316 * @param[in] root_type Type of the XPath root.
317 * @param[in] indent Current indent.
318 * @param[in,out] str Resulting string.
319 * @param[in,out] used Used bytes in @p str.
320 * @param[in,out] size Allocated bytes in @p str.
321 * @return LY_ERR
322 */
323static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200324cast_string_recursive(const struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
Radek Krejci0f969882020-08-21 16:56:47 +0200325 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200326{
Radek Krejci7f769d72020-07-11 23:13:56 +0200327 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200328 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200329 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200330 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200331 struct lyd_node_any *any;
332 LY_ERR rc;
333
334 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
335 return LY_SUCCESS;
336 }
337
338 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200339 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200340 LY_CHECK_RET(rc);
341 strcpy(*str + (*used - 1), "\n");
342 ++(*used);
343
344 ++indent;
345 }
346
347 switch (node->schema->nodetype) {
348 case LYS_CONTAINER:
349 case LYS_LIST:
350 case LYS_RPC:
351 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200352 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200353 LY_CHECK_RET(rc);
354 strcpy(*str + (*used - 1), "\n");
355 ++(*used);
356
Radek Krejcia1c1e542020-09-29 16:06:52 +0200357 for (child = lyd_child(node); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200358 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
359 LY_CHECK_RET(rc);
360 }
361
362 break;
363
364 case LYS_LEAF:
365 case LYS_LEAFLIST:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200366 value_str = lyd_get_value(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200367
368 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200369 LY_CHECK_RET(cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200370 memset(*str + (*used - 1), ' ', indent * 2);
371 *used += indent * 2;
372
373 /* print value */
374 if (*used == 1) {
375 sprintf(*str + (*used - 1), "%s", value_str);
376 *used += strlen(value_str);
377 } else {
378 sprintf(*str + (*used - 1), "%s\n", value_str);
379 *used += strlen(value_str) + 1;
380 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200381
382 break;
383
384 case LYS_ANYXML:
385 case LYS_ANYDATA:
386 any = (struct lyd_node_any *)node;
387 if (!(void *)any->value.tree) {
388 /* no content */
389 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200390 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200391 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200392 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100393
Michal Vasko60ea6352020-06-29 13:39:39 +0200394 if (any->value_type == LYD_ANYDATA_LYB) {
395 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200396 if (lyd_parse_data_mem((struct ly_ctx *)LYD_CTX(node), any->value.mem, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200397 /* successfully parsed */
398 free(any->value.mem);
399 any->value.tree = tree;
400 any->value_type = LYD_ANYDATA_DATATREE;
401 }
Radek Krejci7931b192020-06-25 17:05:03 +0200402 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200403 }
404
Michal Vasko03ff5a72019-09-11 13:49:33 +0200405 switch (any->value_type) {
406 case LYD_ANYDATA_STRING:
407 case LYD_ANYDATA_XML:
408 case LYD_ANYDATA_JSON:
409 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200410 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200411 break;
412 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200413 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200414 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200415 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100416 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200417 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200418 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200419 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200420 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200421 }
422 }
423
424 line = strtok_r(buf, "\n", &ptr);
425 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200426 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200427 if (rc != LY_SUCCESS) {
428 free(buf);
429 return rc;
430 }
431 memset(*str + (*used - 1), ' ', indent * 2);
432 *used += indent * 2;
433
434 strcpy(*str + (*used - 1), line);
435 *used += strlen(line);
436
437 strcpy(*str + (*used - 1), "\n");
438 *used += 1;
439 } while ((line = strtok_r(NULL, "\n", &ptr)));
440
441 free(buf);
442 break;
443
444 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200445 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200446 }
447
448 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200449 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200450 LY_CHECK_RET(rc);
451 strcpy(*str + (*used - 1), "\n");
452 ++(*used);
453
454 --indent;
455 }
456
457 return LY_SUCCESS;
458}
459
460/**
461 * @brief Cast an element into a string.
462 *
463 * @param[in] node Node to cast.
464 * @param[in] fake_cont Whether to put the data into a "fake" container.
465 * @param[in] root_type Type of the XPath root.
466 * @param[out] str Element cast to dynamically-allocated string.
467 * @return LY_ERR
468 */
469static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200470cast_string_elem(struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200471{
472 uint16_t used, size;
473 LY_ERR rc;
474
475 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200476 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200477 (*str)[0] = '\0';
478 used = 1;
479 size = LYXP_STRING_CAST_SIZE_START;
480
481 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
482 if (rc != LY_SUCCESS) {
483 free(*str);
484 return rc;
485 }
486
487 if (size > used) {
488 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200489 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200490 }
491 return LY_SUCCESS;
492}
493
494/**
495 * @brief Cast a LYXP_SET_NODE_SET set into a string.
496 * Context position aware.
497 *
498 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200499 * @param[out] str Cast dynamically-allocated string.
500 * @return LY_ERR
501 */
502static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100503cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200504{
Michal Vaskoaa677062021-03-09 13:52:53 +0100505 if (!set->used) {
506 *str = strdup("");
507 if (!*str) {
508 LOGMEM_RET(set->ctx);
509 }
510 return LY_SUCCESS;
511 }
512
Michal Vasko03ff5a72019-09-11 13:49:33 +0200513 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100514 case LYXP_NODE_NONE:
515 /* invalid */
516 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200517 case LYXP_NODE_ROOT:
518 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100519 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200520 case LYXP_NODE_ELEM:
521 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100522 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100523 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200524 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200525 if (!*str) {
526 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200527 }
528 return LY_SUCCESS;
529 }
530
531 LOGINT_RET(set->ctx);
532}
533
534/**
535 * @brief Cast a string into an XPath number.
536 *
537 * @param[in] str String to use.
538 * @return Cast number.
539 */
540static long double
541cast_string_to_number(const char *str)
542{
543 long double num;
544 char *ptr;
545
546 errno = 0;
547 num = strtold(str, &ptr);
548 if (errno || *ptr) {
549 num = NAN;
550 }
551 return num;
552}
553
554/**
555 * @brief Callback for checking value equality.
556 *
Michal Vasko62524a92021-02-26 10:08:50 +0100557 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200558 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200559 * @param[in] val1_p First value.
560 * @param[in] val2_p Second value.
561 * @param[in] mod Whether hash table is being modified.
562 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200563 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200564 */
Radek Krejci857189e2020-09-01 13:26:36 +0200565static ly_bool
566set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200567{
568 struct lyxp_set_hash_node *val1, *val2;
569
570 val1 = (struct lyxp_set_hash_node *)val1_p;
571 val2 = (struct lyxp_set_hash_node *)val2_p;
572
573 if ((val1->node == val2->node) && (val1->type == val2->type)) {
574 return 1;
575 }
576
577 return 0;
578}
579
580/**
581 * @brief Insert node and its hash into set.
582 *
583 * @param[in] set et to insert to.
584 * @param[in] node Node with hash.
585 * @param[in] type Node type.
586 */
587static void
588set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
589{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200590 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200591 uint32_t i, hash;
592 struct lyxp_set_hash_node hnode;
593
594 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
595 /* create hash table and add all the nodes */
596 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
597 for (i = 0; i < set->used; ++i) {
598 hnode.node = set->val.nodes[i].node;
599 hnode.type = set->val.nodes[i].type;
600
601 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
602 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
603 hash = dict_hash_multi(hash, NULL, 0);
604
605 r = lyht_insert(set->ht, &hnode, hash, NULL);
606 assert(!r);
607 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200608
Michal Vasko9d6befd2019-12-11 14:56:56 +0100609 if (hnode.node == node) {
610 /* it was just added, do not add it twice */
611 node = NULL;
612 }
613 }
614 }
615
616 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200617 /* add the new node into hash table */
618 hnode.node = node;
619 hnode.type = type;
620
621 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
622 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
623 hash = dict_hash_multi(hash, NULL, 0);
624
625 r = lyht_insert(set->ht, &hnode, hash, NULL);
626 assert(!r);
627 (void)r;
628 }
629}
630
631/**
632 * @brief Remove node and its hash from set.
633 *
634 * @param[in] set Set to remove from.
635 * @param[in] node Node to remove.
636 * @param[in] type Node type.
637 */
638static void
639set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
640{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200641 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200642 struct lyxp_set_hash_node hnode;
643 uint32_t hash;
644
645 if (set->ht) {
646 hnode.node = node;
647 hnode.type = type;
648
649 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
650 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
651 hash = dict_hash_multi(hash, NULL, 0);
652
653 r = lyht_remove(set->ht, &hnode, hash);
654 assert(!r);
655 (void)r;
656
657 if (!set->ht->used) {
658 lyht_free(set->ht);
659 set->ht = NULL;
660 }
661 }
662}
663
664/**
665 * @brief Check whether node is in set based on its hash.
666 *
667 * @param[in] set Set to search in.
668 * @param[in] node Node to search for.
669 * @param[in] type Node type.
670 * @param[in] skip_idx Index in @p set to skip.
671 * @return LY_ERR
672 */
673static LY_ERR
674set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
675{
676 struct lyxp_set_hash_node hnode, *match_p;
677 uint32_t hash;
678
679 hnode.node = node;
680 hnode.type = type;
681
682 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
683 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
684 hash = dict_hash_multi(hash, NULL, 0);
685
686 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
687 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
688 /* we found it on the index that should be skipped, find another */
689 hnode = *match_p;
690 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
691 /* none other found */
692 return LY_SUCCESS;
693 }
694 }
695
696 return LY_EEXIST;
697 }
698
699 /* not found */
700 return LY_SUCCESS;
701}
702
Michal Vaskod3678892020-05-21 10:06:58 +0200703void
704lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200705{
706 if (!set) {
707 return;
708 }
709
710 if (set->type == LYXP_SET_NODE_SET) {
711 free(set->val.nodes);
712 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200713 } else if (set->type == LYXP_SET_SCNODE_SET) {
714 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200715 lyht_free(set->ht);
716 } else {
717 if (set->type == LYXP_SET_STRING) {
718 free(set->val.str);
719 }
720 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200721 }
Michal Vaskod3678892020-05-21 10:06:58 +0200722
723 set->val.nodes = NULL;
724 set->used = 0;
725 set->size = 0;
726 set->ht = NULL;
727 set->ctx_pos = 0;
aPiecek748da732021-06-01 09:56:43 +0200728 set->ctx_size = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200729}
730
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100731/**
732 * @brief Free dynamically-allocated set.
733 *
734 * @param[in] set Set to free.
735 */
736static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200737lyxp_set_free(struct lyxp_set *set)
738{
739 if (!set) {
740 return;
741 }
742
Michal Vaskod3678892020-05-21 10:06:58 +0200743 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200744 free(set);
745}
746
747/**
748 * @brief Initialize set context.
749 *
750 * @param[in] new Set to initialize.
751 * @param[in] set Arbitrary initialized set.
752 */
753static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200754set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200755{
756 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200757 if (set) {
758 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200759 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100760 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200761 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100762 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200763 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200764 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200765 new->prefix_data = set->prefix_data;
Michal Vasko02a77382019-09-12 11:47:35 +0200766 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200767}
768
769/**
770 * @brief Create a deep copy of a set.
771 *
772 * @param[in] set Set to copy.
773 * @return Copy of @p set.
774 */
775static struct lyxp_set *
776set_copy(struct lyxp_set *set)
777{
778 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100779 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200780
781 if (!set) {
782 return NULL;
783 }
784
785 ret = malloc(sizeof *ret);
786 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
787 set_init(ret, set);
788
789 if (set->type == LYXP_SET_SCNODE_SET) {
790 ret->type = set->type;
791
792 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100793 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
794 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200795 uint32_t idx;
796 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
797 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100798 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200799 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200800 lyxp_set_free(ret);
801 return NULL;
802 }
Michal Vaskoba716542019-12-16 10:01:58 +0100803 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200804 }
805 }
806 } else if (set->type == LYXP_SET_NODE_SET) {
807 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200808 if (set->used) {
809 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
810 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
811 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
812 } else {
813 ret->val.nodes = NULL;
814 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200815
816 ret->used = ret->size = set->used;
817 ret->ctx_pos = set->ctx_pos;
818 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100819 if (set->ht) {
820 ret->ht = lyht_dup(set->ht);
821 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200822 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200823 memcpy(ret, set, sizeof *ret);
824 if (set->type == LYXP_SET_STRING) {
825 ret->val.str = strdup(set->val.str);
826 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
827 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200828 }
829
830 return ret;
831}
832
833/**
834 * @brief Fill XPath set with a string. Any current data are disposed of.
835 *
836 * @param[in] set Set to fill.
837 * @param[in] string String to fill into \p set.
838 * @param[in] str_len Length of \p string. 0 is a valid value!
839 */
840static void
841set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
842{
Michal Vaskod3678892020-05-21 10:06:58 +0200843 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200844
845 set->type = LYXP_SET_STRING;
846 if ((str_len == 0) && (string[0] != '\0')) {
847 string = "";
848 }
849 set->val.str = strndup(string, str_len);
850}
851
852/**
853 * @brief Fill XPath set with a number. Any current data are disposed of.
854 *
855 * @param[in] set Set to fill.
856 * @param[in] number Number to fill into \p set.
857 */
858static void
859set_fill_number(struct lyxp_set *set, long double number)
860{
Michal Vaskod3678892020-05-21 10:06:58 +0200861 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200862
863 set->type = LYXP_SET_NUMBER;
864 set->val.num = number;
865}
866
867/**
868 * @brief Fill XPath set with a boolean. Any current data are disposed of.
869 *
870 * @param[in] set Set to fill.
871 * @param[in] boolean Boolean to fill into \p set.
872 */
873static void
Radek Krejci857189e2020-09-01 13:26:36 +0200874set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200875{
Michal Vaskod3678892020-05-21 10:06:58 +0200876 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200877
878 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200879 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200880}
881
882/**
883 * @brief Fill XPath set with the value from another set (deep assign).
884 * Any current data are disposed of.
885 *
886 * @param[in] trg Set to fill.
887 * @param[in] src Source set to copy into \p trg.
888 */
889static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200890set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200891{
892 if (!trg || !src) {
893 return;
894 }
895
896 if (trg->type == LYXP_SET_NODE_SET) {
897 free(trg->val.nodes);
898 } else if (trg->type == LYXP_SET_STRING) {
899 free(trg->val.str);
900 }
901 set_init(trg, src);
902
903 if (src->type == LYXP_SET_SCNODE_SET) {
904 trg->type = LYXP_SET_SCNODE_SET;
905 trg->used = src->used;
906 trg->size = src->used;
907
Michal Vasko08e9b112021-06-11 15:41:17 +0200908 if (trg->size) {
909 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
910 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
911 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
912 } else {
913 trg->val.scnodes = NULL;
914 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200915 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200916 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200917 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200918 set_fill_number(trg, src->val.num);
919 } else if (src->type == LYXP_SET_STRING) {
920 set_fill_string(trg, src->val.str, strlen(src->val.str));
921 } else {
922 if (trg->type == LYXP_SET_NODE_SET) {
923 free(trg->val.nodes);
924 } else if (trg->type == LYXP_SET_STRING) {
925 free(trg->val.str);
926 }
927
Michal Vaskod3678892020-05-21 10:06:58 +0200928 assert(src->type == LYXP_SET_NODE_SET);
929
930 trg->type = LYXP_SET_NODE_SET;
931 trg->used = src->used;
932 trg->size = src->used;
933 trg->ctx_pos = src->ctx_pos;
934 trg->ctx_size = src->ctx_size;
935
Michal Vasko08e9b112021-06-11 15:41:17 +0200936 if (trg->size) {
937 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
938 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
939 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
940 } else {
941 trg->val.nodes = NULL;
942 }
Michal Vaskod3678892020-05-21 10:06:58 +0200943 if (src->ht) {
944 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200945 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200946 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200947 }
948 }
949}
950
951/**
952 * @brief Clear context of all schema nodes.
953 *
954 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +0200955 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200956 */
957static void
Michal Vasko1a09b212021-05-06 13:00:10 +0200958set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200959{
960 uint32_t i;
961
962 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100963 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +0200964 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100965 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
966 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200967 }
968 }
969}
970
971/**
972 * @brief Remove a node from a set. Removing last node changes
973 * set into LYXP_SET_EMPTY. Context position aware.
974 *
975 * @param[in] set Set to use.
976 * @param[in] idx Index from @p set of the node to be removed.
977 */
978static void
979set_remove_node(struct lyxp_set *set, uint32_t idx)
980{
981 assert(set && (set->type == LYXP_SET_NODE_SET));
982 assert(idx < set->used);
983
984 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
985
986 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +0200987 if (idx < set->used) {
988 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
989 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +0200990 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200991 }
992}
993
994/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100995 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200996 *
997 * @param[in] set Set to use.
998 * @param[in] idx Index from @p set of the node to be removed.
999 */
1000static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001001set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001002{
1003 assert(set && (set->type == LYXP_SET_NODE_SET));
1004 assert(idx < set->used);
1005
Michal Vasko2caefc12019-11-14 16:07:56 +01001006 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1007 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1008 }
1009 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001010}
1011
1012/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001013 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001014 * set into LYXP_SET_EMPTY. Context position aware.
1015 *
1016 * @param[in] set Set to consolidate.
1017 */
1018static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001019set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001020{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001021 uint32_t i, orig_used, end = 0;
1022 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001023
Michal Vaskod3678892020-05-21 10:06:58 +02001024 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001025
1026 orig_used = set->used;
1027 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001028 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001029 start = -1;
1030 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001031 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001032 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001033 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001034 end = i;
1035 ++i;
1036 break;
1037 }
1038
1039 ++i;
1040 if (i == orig_used) {
1041 end = i;
1042 }
1043 } while (i < orig_used);
1044
1045 if (start > -1) {
1046 /* move the whole chunk of valid nodes together */
1047 if (set->used != (unsigned)start) {
1048 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1049 }
1050 set->used += end - start;
1051 }
1052 }
Michal Vasko57eab132019-09-24 11:46:26 +02001053}
1054
1055/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001056 * @brief Check for duplicates in a node set.
1057 *
1058 * @param[in] set Set to check.
1059 * @param[in] node Node to look for in @p set.
1060 * @param[in] node_type Type of @p node.
1061 * @param[in] skip_idx Index from @p set to skip.
1062 * @return LY_ERR
1063 */
1064static LY_ERR
1065set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1066{
1067 uint32_t i;
1068
Michal Vasko2caefc12019-11-14 16:07:56 +01001069 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001070 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1071 }
1072
1073 for (i = 0; i < set->used; ++i) {
1074 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1075 continue;
1076 }
1077
1078 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1079 return LY_EEXIST;
1080 }
1081 }
1082
1083 return LY_SUCCESS;
1084}
1085
Radek Krejci857189e2020-09-01 13:26:36 +02001086ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001087lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1088 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001089{
1090 uint32_t i;
1091
1092 for (i = 0; i < set->used; ++i) {
1093 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1094 continue;
1095 }
1096
1097 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001098 if (index_p) {
1099 *index_p = i;
1100 }
1101 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001102 }
1103 }
1104
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001105 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001106}
1107
Michal Vaskoecd62de2019-11-13 12:35:11 +01001108void
1109lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001110{
1111 uint32_t orig_used, i, j;
1112
Michal Vaskod3678892020-05-21 10:06:58 +02001113 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001114
Michal Vaskod3678892020-05-21 10:06:58 +02001115 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001116 return;
1117 }
1118
Michal Vaskod3678892020-05-21 10:06:58 +02001119 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001120 memcpy(set1, set2, sizeof *set1);
1121 return;
1122 }
1123
1124 if (set1->used + set2->used > set1->size) {
1125 set1->size = set1->used + set2->used;
1126 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1127 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1128 }
1129
1130 orig_used = set1->used;
1131
1132 for (i = 0; i < set2->used; ++i) {
1133 for (j = 0; j < orig_used; ++j) {
1134 /* detect duplicities */
1135 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1136 break;
1137 }
1138 }
1139
Michal Vasko0587bce2020-12-10 12:19:21 +01001140 if (j < orig_used) {
1141 /* node is there, but update its status if needed */
1142 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1143 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001144 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1145 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1146 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001147 }
1148 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001149 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1150 ++set1->used;
1151 }
1152 }
1153
Michal Vaskod3678892020-05-21 10:06:58 +02001154 lyxp_set_free_content(set2);
1155 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001156}
1157
1158/**
1159 * @brief Insert a node into a set. Context position aware.
1160 *
1161 * @param[in] set Set to use.
1162 * @param[in] node Node to insert to @p set.
1163 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1164 * @param[in] node_type Node type of @p node.
1165 * @param[in] idx Index in @p set to insert into.
1166 */
1167static void
1168set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1169{
Michal Vaskod3678892020-05-21 10:06:58 +02001170 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001171
Michal Vaskod3678892020-05-21 10:06:58 +02001172 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001173 /* first item */
1174 if (idx) {
1175 /* no real harm done, but it is a bug */
1176 LOGINT(set->ctx);
1177 idx = 0;
1178 }
1179 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1180 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1181 set->type = LYXP_SET_NODE_SET;
1182 set->used = 0;
1183 set->size = LYXP_SET_SIZE_START;
1184 set->ctx_pos = 1;
1185 set->ctx_size = 1;
1186 set->ht = NULL;
1187 } else {
1188 /* not an empty set */
1189 if (set->used == set->size) {
1190
1191 /* set is full */
1192 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1193 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1194 set->size += LYXP_SET_SIZE_STEP;
1195 }
1196
1197 if (idx > set->used) {
1198 LOGINT(set->ctx);
1199 idx = set->used;
1200 }
1201
1202 /* make space for the new node */
1203 if (idx < set->used) {
1204 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1205 }
1206 }
1207
1208 /* finally assign the value */
1209 set->val.nodes[idx].node = (struct lyd_node *)node;
1210 set->val.nodes[idx].type = node_type;
1211 set->val.nodes[idx].pos = pos;
1212 ++set->used;
1213
Michal Vasko2caefc12019-11-14 16:07:56 +01001214 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1215 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1216 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001217}
1218
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001219LY_ERR
1220lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001221{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001222 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001223
1224 assert(set->type == LYXP_SET_SCNODE_SET);
1225
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001226 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001227 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001228 } else {
1229 if (set->used == set->size) {
1230 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes);
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001231 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001232 set->size += LYXP_SET_SIZE_STEP;
1233 }
1234
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001235 index = set->used;
1236 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1237 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001238 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001239 ++set->used;
1240 }
1241
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001242 if (index_p) {
1243 *index_p = index;
1244 }
1245
1246 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001247}
1248
1249/**
1250 * @brief Replace a node in a set with another. Context position aware.
1251 *
1252 * @param[in] set Set to use.
1253 * @param[in] node Node to insert to @p set.
1254 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1255 * @param[in] node_type Node type of @p node.
1256 * @param[in] idx Index in @p set of the node to replace.
1257 */
1258static void
1259set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1260{
1261 assert(set && (idx < set->used));
1262
Michal Vasko2caefc12019-11-14 16:07:56 +01001263 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1264 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1265 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001266 set->val.nodes[idx].node = (struct lyd_node *)node;
1267 set->val.nodes[idx].type = node_type;
1268 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001269 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1270 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1271 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001272}
1273
1274/**
1275 * @brief Set all nodes with ctx 1 to a new unique context value.
1276 *
1277 * @param[in] set Set to modify.
1278 * @return New context value.
1279 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001280static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001281set_scnode_new_in_ctx(struct lyxp_set *set)
1282{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001283 uint32_t i;
1284 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001285
1286 assert(set->type == LYXP_SET_SCNODE_SET);
1287
Radek Krejcif13b87b2020-12-01 22:02:17 +01001288 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001289retry:
1290 for (i = 0; i < set->used; ++i) {
1291 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1292 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1293 goto retry;
1294 }
1295 }
1296 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001297 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001298 set->val.scnodes[i].in_ctx = ret_ctx;
1299 }
1300 }
1301
1302 return ret_ctx;
1303}
1304
1305/**
1306 * @brief Get unique @p node position in the data.
1307 *
1308 * @param[in] node Node to find.
1309 * @param[in] node_type Node type of @p node.
1310 * @param[in] root Root node.
1311 * @param[in] root_type Type of the XPath @p root node.
1312 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1313 * be used to increase efficiency and start the DFS from this node.
1314 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1315 * @return Node position.
1316 */
1317static uint32_t
1318get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root,
Radek Krejci0f969882020-08-21 16:56:47 +02001319 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001320{
Michal Vasko56daf732020-08-10 10:57:18 +02001321 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001322 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001323 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001324
1325 assert(prev && prev_pos && !root->prev->next);
1326
1327 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1328 return 0;
1329 }
1330
1331 if (*prev) {
1332 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001333 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001334 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001335 goto dfs_search;
1336 }
1337
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001338 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001339 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001340dfs_search:
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001341 LYD_TREE_DFS_continue = 0;
1342
Michal Vasko56daf732020-08-10 10:57:18 +02001343 if (*prev && !elem) {
1344 /* resume previous DFS */
1345 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1346 LYD_TREE_DFS_continue = 0;
1347 }
1348
Michal Vasko03ff5a72019-09-11 13:49:33 +02001349 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001350 /* skip */
1351 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001352 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001353 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001354 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001355 break;
1356 }
Michal Vasko56daf732020-08-10 10:57:18 +02001357 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001358 }
Michal Vasko56daf732020-08-10 10:57:18 +02001359
1360 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001361 }
1362
1363 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001364 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001365 break;
1366 }
1367 }
1368
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001369 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001370 if (!(*prev)) {
1371 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001372 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001373 return 0;
1374 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001375 /* start the search again from the beginning */
1376 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001377
Michal Vasko56daf732020-08-10 10:57:18 +02001378 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001379 pos = 1;
1380 goto dfs_search;
1381 }
1382 }
1383
1384 /* remember the last found node for next time */
1385 *prev = node;
1386 *prev_pos = pos;
1387
1388 return pos;
1389}
1390
1391/**
1392 * @brief Assign (fill) missing node positions.
1393 *
1394 * @param[in] set Set to fill positions in.
1395 * @param[in] root Context root node.
1396 * @param[in] root_type Context root type.
1397 * @return LY_ERR
1398 */
1399static LY_ERR
1400set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1401{
1402 const struct lyd_node *prev = NULL, *tmp_node;
1403 uint32_t i, tmp_pos = 0;
1404
1405 for (i = 0; i < set->used; ++i) {
1406 if (!set->val.nodes[i].pos) {
1407 tmp_node = NULL;
1408 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001409 case LYXP_NODE_META:
1410 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001411 if (!tmp_node) {
1412 LOGINT_RET(root->schema->module->ctx);
1413 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001414 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001415 case LYXP_NODE_ELEM:
1416 case LYXP_NODE_TEXT:
1417 if (!tmp_node) {
1418 tmp_node = set->val.nodes[i].node;
1419 }
1420 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1421 break;
1422 default:
1423 /* all roots have position 0 */
1424 break;
1425 }
1426 }
1427 }
1428
1429 return LY_SUCCESS;
1430}
1431
1432/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001433 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001434 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001435 * @param[in] meta Metadata to use.
1436 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001437 */
1438static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001439get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001440{
1441 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001442 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001443
Michal Vasko9f96a052020-03-10 09:41:45 +01001444 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001445 ++pos;
1446 }
1447
Michal Vasko9f96a052020-03-10 09:41:45 +01001448 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001449 return pos;
1450}
1451
1452/**
1453 * @brief Compare 2 nodes in respect to XPath document order.
1454 *
1455 * @param[in] item1 1st node.
1456 * @param[in] item2 2nd node.
1457 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1458 */
1459static int
1460set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1461{
Michal Vasko9f96a052020-03-10 09:41:45 +01001462 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001463
1464 if (item1->pos < item2->pos) {
1465 return -1;
1466 }
1467
1468 if (item1->pos > item2->pos) {
1469 return 1;
1470 }
1471
1472 /* node positions are equal, the fun case */
1473
1474 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1475 /* special case since text nodes are actually saved as their parents */
1476 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1477 if (item1->type == LYXP_NODE_ELEM) {
1478 assert(item2->type == LYXP_NODE_TEXT);
1479 return -1;
1480 } else {
1481 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1482 return 1;
1483 }
1484 }
1485
Michal Vasko9f96a052020-03-10 09:41:45 +01001486 /* we need meta positions now */
1487 if (item1->type == LYXP_NODE_META) {
1488 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001489 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001490 if (item2->type == LYXP_NODE_META) {
1491 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001492 }
1493
Michal Vasko9f96a052020-03-10 09:41:45 +01001494 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001495 /* check for duplicates */
1496 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001497 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001498 return 0;
1499 }
1500
Michal Vasko9f96a052020-03-10 09:41:45 +01001501 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001502 /* elem is always first, 2nd node is after it */
1503 if (item1->type == LYXP_NODE_ELEM) {
1504 assert(item2->type != LYXP_NODE_ELEM);
1505 return -1;
1506 }
1507
Michal Vasko9f96a052020-03-10 09:41:45 +01001508 /* 1st TEXT - 2nd ELEM, 1st TEXT - any pos - 2nd META, 1st META - any pos - 2nd ELEM, 1st META - >pos> - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001509 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001510 if (((item1->type == LYXP_NODE_TEXT) &&
1511 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1512 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1513 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1514 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001515 return 1;
1516 }
1517
Michal Vasko9f96a052020-03-10 09:41:45 +01001518 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001519 /* 2nd is after 1st */
1520 return -1;
1521}
1522
1523/**
1524 * @brief Set cast for comparisons.
1525 *
1526 * @param[in] trg Target set to cast source into.
1527 * @param[in] src Source set.
1528 * @param[in] type Target set type.
1529 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001530 * @return LY_ERR
1531 */
1532static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001533set_comp_cast(struct lyxp_set *trg, struct lyxp_set *src, enum lyxp_set_type type, uint32_t src_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001534{
1535 assert(src->type == LYXP_SET_NODE_SET);
1536
1537 set_init(trg, src);
1538
1539 /* insert node into target set */
1540 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1541
1542 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001543 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001544}
1545
Michal Vasko4c7763f2020-07-27 17:40:37 +02001546/**
1547 * @brief Set content canonization for comparisons.
1548 *
1549 * @param[in] trg Target set to put the canononized source into.
1550 * @param[in] src Source set.
1551 * @param[in] xp_node Source XPath node/meta to use for canonization.
1552 * @return LY_ERR
1553 */
1554static LY_ERR
1555set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1556{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001557 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001558 struct lyd_value val;
1559 struct ly_err_item *err = NULL;
1560 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001561 LY_ERR rc;
1562
1563 /* is there anything to canonize even? */
1564 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1565 /* do we have a type to use for canonization? */
1566 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1567 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1568 } else if (xp_node->type == LYXP_NODE_META) {
1569 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1570 }
1571 }
1572 if (!type) {
1573 goto fill;
1574 }
1575
1576 if (src->type == LYXP_SET_NUMBER) {
1577 /* canonize number */
1578 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1579 LOGMEM(src->ctx);
1580 return LY_EMEM;
1581 }
1582 } else {
1583 /* canonize string */
1584 str = strdup(src->val.str);
1585 }
1586
1587 /* ignore errors, the value may not satisfy schema constraints */
Radek Krejci0b013302021-03-29 15:22:32 +02001588 rc = type->plugin->store(src->ctx, type, str, strlen(str), LYPLG_TYPE_STORE_DYNAMIC, src->format, src->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001589 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001590 ly_err_free(err);
1591 if (rc) {
1592 /* invalid value */
Radek IÅ¡a48460222021-03-02 15:18:32 +01001593 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001594 goto fill;
1595 }
1596
Michal Vasko4c7763f2020-07-27 17:40:37 +02001597 /* use the canonized value */
1598 set_init(trg, src);
1599 trg->type = src->type;
1600 if (src->type == LYXP_SET_NUMBER) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001601 trg->val.num = strtold(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001602 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1603 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001604 trg->val.str = strdup(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko4c7763f2020-07-27 17:40:37 +02001605 }
1606 type->plugin->free(src->ctx, &val);
1607 return LY_SUCCESS;
1608
1609fill:
1610 /* no canonization needed/possible */
1611 set_fill_set(trg, src);
1612 return LY_SUCCESS;
1613}
1614
Michal Vasko03ff5a72019-09-11 13:49:33 +02001615#ifndef NDEBUG
1616
1617/**
1618 * @brief Bubble sort @p set into XPath document order.
1619 * Context position aware. Unused in the 'Release' build target.
1620 *
1621 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001622 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1623 */
1624static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001625set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001626{
1627 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001628 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001629 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001630 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001631 struct lyxp_set_node item;
1632 struct lyxp_set_hash_node hnode;
1633 uint64_t hash;
1634
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001635 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001636 return 0;
1637 }
1638
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001639 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001640 for (root = set->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001641 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001642
1643 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001644 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001645 return -1;
1646 }
1647
1648 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1649 print_set_debug(set);
1650
1651 for (i = 0; i < set->used; ++i) {
1652 inverted = 0;
1653 change = 0;
1654
1655 for (j = 1; j < set->used - i; ++j) {
1656 /* compare node positions */
1657 if (inverted) {
1658 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1659 } else {
1660 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1661 }
1662
1663 /* swap if needed */
1664 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1665 change = 1;
1666
1667 item = set->val.nodes[j - 1];
1668 set->val.nodes[j - 1] = set->val.nodes[j];
1669 set->val.nodes[j] = item;
1670 } else {
1671 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1672 inverted = !inverted;
1673 }
1674 }
1675
1676 ++ret;
1677
1678 if (!change) {
1679 break;
1680 }
1681 }
1682
1683 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1684 print_set_debug(set);
1685
1686 /* check node hashes */
1687 if (set->used >= LYD_HT_MIN_ITEMS) {
1688 assert(set->ht);
1689 for (i = 0; i < set->used; ++i) {
1690 hnode.node = set->val.nodes[i].node;
1691 hnode.type = set->val.nodes[i].type;
1692
1693 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1694 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1695 hash = dict_hash_multi(hash, NULL, 0);
1696
1697 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1698 }
1699 }
1700
1701 return ret - 1;
1702}
1703
1704/**
1705 * @brief Remove duplicate entries in a sorted node set.
1706 *
1707 * @param[in] set Sorted set to check.
1708 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1709 */
1710static LY_ERR
1711set_sorted_dup_node_clean(struct lyxp_set *set)
1712{
1713 uint32_t i = 0;
1714 LY_ERR ret = LY_SUCCESS;
1715
1716 if (set->used > 1) {
1717 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001718 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1719 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001720 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001721 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001722 }
Michal Vasko57eab132019-09-24 11:46:26 +02001723 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001724 }
1725 }
1726
Michal Vasko2caefc12019-11-14 16:07:56 +01001727 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001728 return ret;
1729}
1730
1731#endif
1732
1733/**
1734 * @brief Merge 2 sorted sets into one.
1735 *
1736 * @param[in,out] trg Set to merge into. Duplicates are removed.
1737 * @param[in] src Set to be merged into @p trg. It is cast to #LYXP_SET_EMPTY on success.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001738 * @return LY_ERR
1739 */
1740static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001741set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001742{
1743 uint32_t i, j, k, count, dup_count;
1744 int cmp;
1745 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001746
Michal Vaskod3678892020-05-21 10:06:58 +02001747 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001748 return LY_EINVAL;
1749 }
1750
Michal Vaskod3678892020-05-21 10:06:58 +02001751 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001752 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001753 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001754 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001755 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001756 return LY_SUCCESS;
1757 }
1758
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001759 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001760 for (root = trg->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001761 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001762
1763 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001764 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001765 return LY_EINT;
1766 }
1767
1768#ifndef NDEBUG
1769 LOGDBG(LY_LDGXPATH, "MERGE target");
1770 print_set_debug(trg);
1771 LOGDBG(LY_LDGXPATH, "MERGE source");
1772 print_set_debug(src);
1773#endif
1774
1775 /* make memory for the merge (duplicates are not detected yet, so space
1776 * will likely be wasted on them, too bad) */
1777 if (trg->size - trg->used < src->used) {
1778 trg->size = trg->used + src->used;
1779
1780 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1781 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1782 }
1783
1784 i = 0;
1785 j = 0;
1786 count = 0;
1787 dup_count = 0;
1788 do {
1789 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1790 if (!cmp) {
1791 if (!count) {
1792 /* duplicate, just skip it */
1793 ++i;
1794 ++j;
1795 } else {
1796 /* we are copying something already, so let's copy the duplicate too,
1797 * we are hoping that afterwards there are some more nodes to
1798 * copy and this way we can copy them all together */
1799 ++count;
1800 ++dup_count;
1801 ++i;
1802 ++j;
1803 }
1804 } else if (cmp < 0) {
1805 /* inserting src node into trg, just remember it for now */
1806 ++count;
1807 ++i;
1808
1809 /* insert the hash now */
1810 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1811 } else if (count) {
1812copy_nodes:
1813 /* time to actually copy the nodes, we have found the largest block of nodes */
1814 memmove(&trg->val.nodes[j + (count - dup_count)],
1815 &trg->val.nodes[j],
1816 (trg->used - j) * sizeof *trg->val.nodes);
1817 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1818
1819 trg->used += count - dup_count;
1820 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1821 j += count - dup_count;
1822
1823 count = 0;
1824 dup_count = 0;
1825 } else {
1826 ++j;
1827 }
1828 } while ((i < src->used) && (j < trg->used));
1829
1830 if ((i < src->used) || count) {
1831 /* insert all the hashes first */
1832 for (k = i; k < src->used; ++k) {
1833 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1834 }
1835
1836 /* loop ended, but we need to copy something at trg end */
1837 count += src->used - i;
1838 i = src->used;
1839 goto copy_nodes;
1840 }
1841
1842 /* we are inserting hashes before the actual node insert, which causes
1843 * situations when there were initially not enough items for a hash table,
1844 * but even after some were inserted, hash table was not created (during
1845 * insertion the number of items is not updated yet) */
1846 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1847 set_insert_node_hash(trg, NULL, 0);
1848 }
1849
1850#ifndef NDEBUG
1851 LOGDBG(LY_LDGXPATH, "MERGE result");
1852 print_set_debug(trg);
1853#endif
1854
Michal Vaskod3678892020-05-21 10:06:58 +02001855 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001856 return LY_SUCCESS;
1857}
1858
1859/*
1860 * (re)parse functions
1861 *
1862 * Parse functions parse the expression into
1863 * tokens (syntactic analysis).
1864 *
1865 * Reparse functions perform semantic analysis
1866 * (do not save the result, just a check) of
1867 * the expression and fill repeat indices.
1868 */
1869
Michal Vasko14676352020-05-29 11:35:55 +02001870LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001871lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001872{
Michal Vasko004d3152020-06-11 19:59:22 +02001873 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001874 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001875 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001876 }
Michal Vasko14676352020-05-29 11:35:55 +02001877 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001878 }
1879
Michal Vasko004d3152020-06-11 19:59:22 +02001880 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001881 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001882 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001883 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001884 }
Michal Vasko14676352020-05-29 11:35:55 +02001885 return LY_ENOT;
1886 }
1887
1888 return LY_SUCCESS;
1889}
1890
Michal Vasko004d3152020-06-11 19:59:22 +02001891LY_ERR
1892lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1893{
1894 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1895
1896 /* skip the token */
1897 ++(*tok_idx);
1898
1899 return LY_SUCCESS;
1900}
1901
Michal Vasko14676352020-05-29 11:35:55 +02001902/* just like lyxp_check_token() but tests for 2 tokens */
1903static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001904exp_check_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok1,
Radek Krejci0f969882020-08-21 16:56:47 +02001905 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001906{
Michal Vasko004d3152020-06-11 19:59:22 +02001907 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001908 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001909 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001910 }
1911 return LY_EINCOMPLETE;
1912 }
1913
Michal Vasko004d3152020-06-11 19:59:22 +02001914 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001915 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001916 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001917 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001918 }
1919 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001920 }
1921
1922 return LY_SUCCESS;
1923}
1924
Michal Vasko4911eeb2021-06-28 11:23:05 +02001925LY_ERR
1926lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok1,
1927 enum lyxp_token want_tok2)
1928{
1929 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
1930
1931 /* skip the token */
1932 ++(*tok_idx);
1933
1934 return LY_SUCCESS;
1935}
1936
Michal Vasko03ff5a72019-09-11 13:49:33 +02001937/**
1938 * @brief Stack operation push on the repeat array.
1939 *
1940 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001941 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001942 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1943 */
1944static void
Michal Vasko004d3152020-06-11 19:59:22 +02001945exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001946{
1947 uint16_t i;
1948
Michal Vasko004d3152020-06-11 19:59:22 +02001949 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001950 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001951 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1952 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1953 exp->repeat[tok_idx][i] = repeat_op_idx;
1954 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001955 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001956 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1957 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1958 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001959 }
1960}
1961
1962/**
1963 * @brief Reparse Predicate. Logs directly on error.
1964 *
1965 * [7] Predicate ::= '[' Expr ']'
1966 *
1967 * @param[in] ctx Context for logging.
1968 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001969 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02001970 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001971 * @return LY_ERR
1972 */
1973static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02001974reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001975{
1976 LY_ERR rc;
1977
Michal Vasko004d3152020-06-11 19:59:22 +02001978 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001979 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001980 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001981
aPiecekbf968d92021-05-27 14:35:05 +02001982 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001983 LY_CHECK_RET(rc);
1984
Michal Vasko004d3152020-06-11 19:59:22 +02001985 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001986 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001987 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001988
1989 return LY_SUCCESS;
1990}
1991
1992/**
1993 * @brief Reparse RelativeLocationPath. Logs directly on error.
1994 *
1995 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1996 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1997 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1998 *
1999 * @param[in] ctx Context for logging.
2000 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002001 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002002 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002003 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
2004 */
2005static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002006reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002007{
2008 LY_ERR rc;
2009
Michal Vasko004d3152020-06-11 19:59:22 +02002010 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002011 LY_CHECK_RET(rc);
2012
2013 goto step;
2014 do {
2015 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002016 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002017
Michal Vasko004d3152020-06-11 19:59:22 +02002018 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002019 LY_CHECK_RET(rc);
2020step:
2021 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002022 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002023 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002024 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002025 break;
2026
2027 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002028 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002029 break;
2030
2031 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002032 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002033
Michal Vasko004d3152020-06-11 19:59:22 +02002034 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002035 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002036 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002037 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002038 return LY_EVALID;
2039 }
Radek Krejci0f969882020-08-21 16:56:47 +02002040 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002041 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002042 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002043 goto reparse_predicate;
2044 break;
2045
2046 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002047 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002048
2049 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002050 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002051 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002052 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002053
2054 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002055 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002056 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002057 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002058
2059reparse_predicate:
2060 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002061 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002062 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002063 LY_CHECK_RET(rc);
2064 }
2065 break;
2066 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002067 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002068 return LY_EVALID;
2069 }
Michal Vasko004d3152020-06-11 19:59:22 +02002070 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002071
2072 return LY_SUCCESS;
2073}
2074
2075/**
2076 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2077 *
2078 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2079 *
2080 * @param[in] ctx Context for logging.
2081 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002082 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002083 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002084 * @return LY_ERR
2085 */
2086static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002087reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002088{
2089 LY_ERR rc;
2090
Michal Vasko004d3152020-06-11 19:59:22 +02002091 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002092
2093 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002094 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002095 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002096 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002097
Michal Vasko004d3152020-06-11 19:59:22 +02002098 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002099 return LY_SUCCESS;
2100 }
Michal Vasko004d3152020-06-11 19:59:22 +02002101 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002102 case LYXP_TOKEN_DOT:
2103 case LYXP_TOKEN_DDOT:
2104 case LYXP_TOKEN_AT:
2105 case LYXP_TOKEN_NAMETEST:
2106 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002107 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002108 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002109 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002110 default:
2111 break;
2112 }
2113
Michal Vasko03ff5a72019-09-11 13:49:33 +02002114 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002115 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002116 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002117
aPiecekbf968d92021-05-27 14:35:05 +02002118 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002119 LY_CHECK_RET(rc);
2120 }
2121
2122 return LY_SUCCESS;
2123}
2124
2125/**
2126 * @brief Reparse FunctionCall. Logs directly on error.
2127 *
2128 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2129 *
2130 * @param[in] ctx Context for logging.
2131 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002132 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002133 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002134 * @return LY_ERR
2135 */
2136static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002137reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002138{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002139 int8_t min_arg_count = -1;
2140 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002141 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002142 LY_ERR rc;
2143
Michal Vasko004d3152020-06-11 19:59:22 +02002144 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002145 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002146 func_tok_idx = *tok_idx;
2147 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002148 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002149 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002150 min_arg_count = 1;
2151 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002152 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002153 min_arg_count = 1;
2154 max_arg_count = 1;
2155 }
2156 break;
2157 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002158 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002159 min_arg_count = 1;
2160 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002161 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002162 min_arg_count = 0;
2163 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002164 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002165 min_arg_count = 0;
2166 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002167 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002168 min_arg_count = 0;
2169 max_arg_count = 0;
2170 }
2171 break;
2172 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002173 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002174 min_arg_count = 1;
2175 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002176 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002177 min_arg_count = 0;
2178 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002179 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002180 min_arg_count = 1;
2181 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002182 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002183 min_arg_count = 1;
2184 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002185 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002186 min_arg_count = 1;
2187 max_arg_count = 1;
2188 }
2189 break;
2190 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002191 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002192 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002193 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002194 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002195 min_arg_count = 0;
2196 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002197 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002198 min_arg_count = 0;
2199 max_arg_count = 1;
2200 }
2201 break;
2202 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002203 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002204 min_arg_count = 1;
2205 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002206 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002207 min_arg_count = 1;
2208 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002209 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002210 min_arg_count = 0;
2211 max_arg_count = 0;
2212 }
2213 break;
2214 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002215 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002216 min_arg_count = 2;
2217 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002218 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002219 min_arg_count = 0;
2220 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002221 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002222 min_arg_count = 2;
2223 max_arg_count = 2;
2224 }
2225 break;
2226 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002227 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002228 min_arg_count = 2;
2229 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002230 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002231 min_arg_count = 3;
2232 max_arg_count = 3;
2233 }
2234 break;
2235 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002236 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002237 min_arg_count = 0;
2238 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002239 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002240 min_arg_count = 1;
2241 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002242 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002243 min_arg_count = 2;
2244 max_arg_count = 2;
2245 }
2246 break;
2247 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002248 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002249 min_arg_count = 2;
2250 max_arg_count = 2;
2251 }
2252 break;
2253 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002254 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002255 min_arg_count = 2;
2256 max_arg_count = 2;
2257 }
2258 break;
2259 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002260 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002261 min_arg_count = 0;
2262 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002263 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002264 min_arg_count = 0;
2265 max_arg_count = 1;
2266 }
2267 break;
2268 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002269 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002270 min_arg_count = 0;
2271 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002272 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002273 min_arg_count = 2;
2274 max_arg_count = 2;
2275 }
2276 break;
2277 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002278 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002279 min_arg_count = 2;
2280 max_arg_count = 2;
2281 }
2282 break;
2283 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002284 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002285 min_arg_count = 2;
2286 max_arg_count = 2;
2287 }
2288 break;
2289 }
2290 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002291 LOGVAL(ctx, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002292 return LY_EINVAL;
2293 }
Michal Vasko004d3152020-06-11 19:59:22 +02002294 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002295
2296 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002297 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002298 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002299 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002300
2301 /* ( Expr ( ',' Expr )* )? */
2302 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002303 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002304 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002305 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002306 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002307 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002308 LY_CHECK_RET(rc);
2309 }
Michal Vasko004d3152020-06-11 19:59:22 +02002310 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2311 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002312
2313 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002314 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002315 LY_CHECK_RET(rc);
2316 }
2317
2318 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002319 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002320 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002321 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002322
Radek Krejci857189e2020-09-01 13:26:36 +02002323 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002324 LOGVAL(ctx, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002325 return LY_EVALID;
2326 }
2327
2328 return LY_SUCCESS;
2329}
2330
2331/**
2332 * @brief Reparse PathExpr. Logs directly on error.
2333 *
2334 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2335 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2336 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2337 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2338 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2339 *
2340 * @param[in] ctx Context for logging.
2341 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002342 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002343 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002344 * @return LY_ERR
2345 */
2346static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002347reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002348{
2349 LY_ERR rc;
2350
Michal Vasko004d3152020-06-11 19:59:22 +02002351 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002352 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002353 }
2354
Michal Vasko004d3152020-06-11 19:59:22 +02002355 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002356 case LYXP_TOKEN_PAR1:
2357 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002358 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002359
aPiecekbf968d92021-05-27 14:35:05 +02002360 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002361 LY_CHECK_RET(rc);
2362
Michal Vasko004d3152020-06-11 19:59:22 +02002363 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002364 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002365 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002366 goto predicate;
2367 break;
2368 case LYXP_TOKEN_DOT:
2369 case LYXP_TOKEN_DDOT:
2370 case LYXP_TOKEN_AT:
2371 case LYXP_TOKEN_NAMETEST:
2372 case LYXP_TOKEN_NODETYPE:
2373 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002374 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002375 LY_CHECK_RET(rc);
2376 break;
2377 case LYXP_TOKEN_FUNCNAME:
2378 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002379 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002380 LY_CHECK_RET(rc);
2381 goto predicate;
2382 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002383 case LYXP_TOKEN_OPER_PATH:
2384 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002385 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002386 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002387 LY_CHECK_RET(rc);
2388 break;
2389 case LYXP_TOKEN_LITERAL:
2390 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002391 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002392 goto predicate;
2393 break;
2394 case LYXP_TOKEN_NUMBER:
2395 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002396 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002397 goto predicate;
2398 break;
2399 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002400 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002401 return LY_EVALID;
2402 }
2403
2404 return LY_SUCCESS;
2405
2406predicate:
2407 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002408 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002409 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002410 LY_CHECK_RET(rc);
2411 }
2412
2413 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002414 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002415
2416 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002417 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002418
aPiecekbf968d92021-05-27 14:35:05 +02002419 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002420 LY_CHECK_RET(rc);
2421 }
2422
2423 return LY_SUCCESS;
2424}
2425
2426/**
2427 * @brief Reparse UnaryExpr. Logs directly on error.
2428 *
2429 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2430 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2431 *
2432 * @param[in] ctx Context for logging.
2433 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002434 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002435 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002436 * @return LY_ERR
2437 */
2438static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002439reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002440{
2441 uint16_t prev_exp;
2442 LY_ERR rc;
2443
2444 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002445 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002446 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2447 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002448 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002449 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002450 }
2451
2452 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002453 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002454 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002455 LY_CHECK_RET(rc);
2456
2457 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002458 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002459 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002460 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002461
aPiecekbf968d92021-05-27 14:35:05 +02002462 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002463 LY_CHECK_RET(rc);
2464 }
2465
2466 return LY_SUCCESS;
2467}
2468
2469/**
2470 * @brief Reparse AdditiveExpr. Logs directly on error.
2471 *
2472 * [15] AdditiveExpr ::= MultiplicativeExpr
2473 * | AdditiveExpr '+' MultiplicativeExpr
2474 * | AdditiveExpr '-' MultiplicativeExpr
2475 * [16] MultiplicativeExpr ::= UnaryExpr
2476 * | MultiplicativeExpr '*' UnaryExpr
2477 * | MultiplicativeExpr 'div' UnaryExpr
2478 * | MultiplicativeExpr 'mod' UnaryExpr
2479 *
2480 * @param[in] ctx Context for logging.
2481 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002482 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002483 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002484 * @return LY_ERR
2485 */
2486static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002487reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002488{
2489 uint16_t prev_add_exp, prev_mul_exp;
2490 LY_ERR rc;
2491
Michal Vasko004d3152020-06-11 19:59:22 +02002492 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002493 goto reparse_multiplicative_expr;
2494
2495 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002496 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2497 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002498 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002499 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002500
2501reparse_multiplicative_expr:
2502 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002503 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002504 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002505 LY_CHECK_RET(rc);
2506
2507 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002508 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2509 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002510 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002511 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002512
aPiecekbf968d92021-05-27 14:35:05 +02002513 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002514 LY_CHECK_RET(rc);
2515 }
2516 }
2517
2518 return LY_SUCCESS;
2519}
2520
2521/**
2522 * @brief Reparse EqualityExpr. Logs directly on error.
2523 *
2524 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2525 * | EqualityExpr '!=' RelationalExpr
2526 * [14] RelationalExpr ::= AdditiveExpr
2527 * | RelationalExpr '<' AdditiveExpr
2528 * | RelationalExpr '>' AdditiveExpr
2529 * | RelationalExpr '<=' AdditiveExpr
2530 * | RelationalExpr '>=' AdditiveExpr
2531 *
2532 * @param[in] ctx Context for logging.
2533 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002534 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002535 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002536 * @return LY_ERR
2537 */
2538static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002539reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002540{
2541 uint16_t prev_eq_exp, prev_rel_exp;
2542 LY_ERR rc;
2543
Michal Vasko004d3152020-06-11 19:59:22 +02002544 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002545 goto reparse_additive_expr;
2546
2547 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002548 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002549 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002550 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002551
2552reparse_additive_expr:
2553 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002554 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002555 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002556 LY_CHECK_RET(rc);
2557
2558 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002559 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002560 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002561 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002562
aPiecekbf968d92021-05-27 14:35:05 +02002563 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002564 LY_CHECK_RET(rc);
2565 }
2566 }
2567
2568 return LY_SUCCESS;
2569}
2570
2571/**
2572 * @brief Reparse OrExpr. Logs directly on error.
2573 *
2574 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2575 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2576 *
2577 * @param[in] ctx Context for logging.
2578 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002579 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002580 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002581 * @return LY_ERR
2582 */
2583static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002584reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002585{
2586 uint16_t prev_or_exp, prev_and_exp;
2587 LY_ERR rc;
2588
aPiecekbf968d92021-05-27 14:35:05 +02002589 ++depth;
2590 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2591
Michal Vasko004d3152020-06-11 19:59:22 +02002592 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002593 goto reparse_equality_expr;
2594
2595 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002596 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002597 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002598 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002599
2600reparse_equality_expr:
2601 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002602 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002603 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002604 LY_CHECK_RET(rc);
2605
2606 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002607 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002608 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002609 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002610
aPiecekbf968d92021-05-27 14:35:05 +02002611 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002612 LY_CHECK_RET(rc);
2613 }
2614 }
2615
2616 return LY_SUCCESS;
2617}
Radek Krejcib1646a92018-11-02 16:08:26 +01002618
2619/**
2620 * @brief Parse NCName.
2621 *
2622 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002623 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002624 */
Radek Krejcid4270262019-01-07 15:07:25 +01002625static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002626parse_ncname(const char *ncname)
2627{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002628 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002629 size_t size;
2630 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002631
2632 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2633 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2634 return len;
2635 }
2636
2637 do {
2638 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002639 if (!*ncname) {
2640 break;
2641 }
Radek Krejcid4270262019-01-07 15:07:25 +01002642 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002643 } while (is_xmlqnamechar(uc) && (uc != ':'));
2644
2645 return len;
2646}
2647
2648/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002649 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002650 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002651 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002652 * @param[in] exp Expression to use.
2653 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002654 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002655 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002656 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002657 */
2658static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002659exp_add_token(const struct ly_ctx *ctx, struct lyxp_expr *exp, enum lyxp_token token, uint16_t tok_pos, uint16_t tok_len)
Radek Krejcib1646a92018-11-02 16:08:26 +01002660{
2661 uint32_t prev;
2662
2663 if (exp->used == exp->size) {
2664 prev = exp->size;
2665 exp->size += LYXP_EXPR_SIZE_STEP;
2666 if (prev > exp->size) {
2667 LOGINT(ctx);
2668 return LY_EINT;
2669 }
2670
2671 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2672 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2673 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2674 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2675 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2676 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2677 }
2678
2679 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002680 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002681 exp->tok_len[exp->used] = tok_len;
2682 ++exp->used;
2683 return LY_SUCCESS;
2684}
2685
2686void
Michal Vasko14676352020-05-29 11:35:55 +02002687lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002688{
2689 uint16_t i;
2690
2691 if (!expr) {
2692 return;
2693 }
2694
2695 lydict_remove(ctx, expr->expr);
2696 free(expr->tokens);
2697 free(expr->tok_pos);
2698 free(expr->tok_len);
2699 if (expr->repeat) {
2700 for (i = 0; i < expr->used; ++i) {
2701 free(expr->repeat[i]);
2702 }
2703 }
2704 free(expr->repeat);
2705 free(expr);
2706}
2707
Radek Krejcif03a9e22020-09-18 20:09:31 +02002708LY_ERR
2709lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr_str, size_t expr_len, ly_bool reparse, struct lyxp_expr **expr_p)
Radek Krejcib1646a92018-11-02 16:08:26 +01002710{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002711 LY_ERR ret = LY_SUCCESS;
2712 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002713 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002714 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002715 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002716 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002717
Radek Krejcif03a9e22020-09-18 20:09:31 +02002718 assert(expr_p);
2719
2720 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002721 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002722 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002723 }
2724
2725 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002726 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002727 }
2728 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002729 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002730 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002731 }
2732
2733 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002734 expr = calloc(1, sizeof *expr);
2735 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2736 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2737 expr->used = 0;
2738 expr->size = LYXP_EXPR_SIZE_START;
2739 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2740 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002741
Radek Krejcif03a9e22020-09-18 20:09:31 +02002742 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2743 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002744
Radek Krejcif03a9e22020-09-18 20:09:31 +02002745 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2746 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002747
Michal Vasko004d3152020-06-11 19:59:22 +02002748 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002749 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002750
Radek Krejcif03a9e22020-09-18 20:09:31 +02002751 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002752 ++parsed;
2753 }
2754
2755 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002756 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002757
2758 /* '(' */
2759 tok_len = 1;
2760 tok_type = LYXP_TOKEN_PAR1;
2761
Radek Krejcif03a9e22020-09-18 20:09:31 +02002762 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002763 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002764 if (((expr->tok_len[expr->used - 1] == 4) &&
2765 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2766 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2767 ((expr->tok_len[expr->used - 1] == 7) &&
2768 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002769 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002770 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002771 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002772 }
2773 prev_function_check = 0;
2774 }
2775
Radek Krejcif03a9e22020-09-18 20:09:31 +02002776 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002777
2778 /* ')' */
2779 tok_len = 1;
2780 tok_type = LYXP_TOKEN_PAR2;
2781
Radek Krejcif03a9e22020-09-18 20:09:31 +02002782 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002783
2784 /* '[' */
2785 tok_len = 1;
2786 tok_type = LYXP_TOKEN_BRACK1;
2787
Radek Krejcif03a9e22020-09-18 20:09:31 +02002788 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002789
2790 /* ']' */
2791 tok_len = 1;
2792 tok_type = LYXP_TOKEN_BRACK2;
2793
Radek Krejcif03a9e22020-09-18 20:09:31 +02002794 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002795
2796 /* '..' */
2797 tok_len = 2;
2798 tok_type = LYXP_TOKEN_DDOT;
2799
Radek Krejcif03a9e22020-09-18 20:09:31 +02002800 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002801
2802 /* '.' */
2803 tok_len = 1;
2804 tok_type = LYXP_TOKEN_DOT;
2805
Radek Krejcif03a9e22020-09-18 20:09:31 +02002806 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002807
2808 /* '@' */
2809 tok_len = 1;
2810 tok_type = LYXP_TOKEN_AT;
2811
Radek Krejcif03a9e22020-09-18 20:09:31 +02002812 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002813
2814 /* ',' */
2815 tok_len = 1;
2816 tok_type = LYXP_TOKEN_COMMA;
2817
Radek Krejcif03a9e22020-09-18 20:09:31 +02002818 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002819
2820 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002821 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2822 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002823 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002824 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002825 ++tok_len;
2826 tok_type = LYXP_TOKEN_LITERAL;
2827
Radek Krejcif03a9e22020-09-18 20:09:31 +02002828 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002829
2830 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002831 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2832 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002833 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002834 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002835 ++tok_len;
2836 tok_type = LYXP_TOKEN_LITERAL;
2837
Radek Krejcif03a9e22020-09-18 20:09:31 +02002838 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002839
2840 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002841 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2842 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002843 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002844 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002845 }
2846 tok_type = LYXP_TOKEN_NUMBER;
2847
Radek Krejcif03a9e22020-09-18 20:09:31 +02002848 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002849
2850 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002851 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002852 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002853 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002854 } else {
2855 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002856 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002857 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002858
Radek Krejcif03a9e22020-09-18 20:09:31 +02002859 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002860
Michal Vasko3e48bf32020-06-01 08:39:07 +02002861 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002862 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002863 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2864
Radek Krejcif03a9e22020-09-18 20:09:31 +02002865 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002866
2867 /* Operator '<=', '>=' */
2868 tok_len = 2;
2869 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002870
Radek Krejcif03a9e22020-09-18 20:09:31 +02002871 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002872
2873 /* Operator '|' */
2874 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002875 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002876
Radek Krejcif03a9e22020-09-18 20:09:31 +02002877 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002878
2879 /* Operator '+', '-' */
2880 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002881 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002882
Radek Krejcif03a9e22020-09-18 20:09:31 +02002883 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002884
Michal Vasko3e48bf32020-06-01 08:39:07 +02002885 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002886 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002887 tok_type = LYXP_TOKEN_OPER_EQUAL;
2888
Radek Krejcif03a9e22020-09-18 20:09:31 +02002889 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002890
2891 /* Operator '<', '>' */
2892 tok_len = 1;
2893 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002894
Michal Vasko69730152020-10-09 16:30:07 +02002895 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2896 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2897 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2898 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2899 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2900 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2901 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2902 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2903 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2904 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2905 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2906 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002907
2908 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002909 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002910 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002911 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002912
Radek Krejcif03a9e22020-09-18 20:09:31 +02002913 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002914 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002915 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002916
Radek Krejcif03a9e22020-09-18 20:09:31 +02002917 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002918 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002919 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002920
Radek Krejcif03a9e22020-09-18 20:09:31 +02002921 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002922 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002923 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002924
2925 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002926 LOGVAL(ctx, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
Michal Vasko69730152020-10-09 16:30:07 +02002927 expr_str[parsed], expr_str[parsed], expr->tok_len[expr->used - 1], &expr->expr[expr->tok_pos[expr->used - 1]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002928 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002929 goto error;
2930 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02002931 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002932 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002933 goto error;
2934 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002935 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002936
2937 /* NameTest '*' */
2938 tok_len = 1;
2939 tok_type = LYXP_TOKEN_NAMETEST;
2940
2941 } else {
2942
2943 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002944 long int ncname_len = parse_ncname(&expr_str[parsed]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02002945 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
Michal Vasko774ce402021-04-14 15:35:06 +02002946 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002947 tok_len = ncname_len;
2948
Radek Krejcif03a9e22020-09-18 20:09:31 +02002949 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002950 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002951 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002952 ++tok_len;
2953 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002954 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02002955 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
Michal Vasko774ce402021-04-14 15:35:06 +02002956 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002957 tok_len += ncname_len;
2958 }
2959 /* remove old flag to prevent ambiguities */
2960 prev_function_check = 0;
2961 tok_type = LYXP_TOKEN_NAMETEST;
2962 } else {
2963 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2964 prev_function_check = 1;
2965 tok_type = LYXP_TOKEN_NAMETEST;
2966 }
2967 }
2968
2969 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002970 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002971 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002972 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002973 ++parsed;
2974 }
2975
Radek Krejcif03a9e22020-09-18 20:09:31 +02002976 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002977
Michal Vasko004d3152020-06-11 19:59:22 +02002978 if (reparse) {
2979 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002980 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2981 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002982
Michal Vasko004d3152020-06-11 19:59:22 +02002983 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02002984 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002985 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002986 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002987 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002988 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002989 goto error;
2990 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002991 }
2992
Radek Krejcif03a9e22020-09-18 20:09:31 +02002993 print_expr_struct_debug(expr);
2994 *expr_p = expr;
2995 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002996
2997error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002998 lyxp_expr_free(ctx, expr);
2999 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01003000}
3001
Michal Vasko1734be92020-09-22 08:55:10 +02003002LY_ERR
3003lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003004{
Michal Vasko1734be92020-09-22 08:55:10 +02003005 LY_ERR ret = LY_SUCCESS;
3006 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003007 uint32_t i, j;
3008
Michal Vasko7f45cf22020-10-01 12:49:44 +02003009 if (!exp) {
3010 goto cleanup;
3011 }
3012
Michal Vasko004d3152020-06-11 19:59:22 +02003013 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003014 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003015
Michal Vasko08e9b112021-06-11 15:41:17 +02003016 if (exp->used) {
3017 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
3018 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3019 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003020
Michal Vasko08e9b112021-06-11 15:41:17 +02003021 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
3022 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3023 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003024
Michal Vasko08e9b112021-06-11 15:41:17 +02003025 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
3026 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3027 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003028
Michal Vasko08e9b112021-06-11 15:41:17 +02003029 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
3030 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3031 for (i = 0; i < exp->used; ++i) {
3032 if (!exp->repeat[i]) {
3033 dup->repeat[i] = NULL;
3034 } else {
3035 for (j = 0; exp->repeat[i][j]; ++j) {}
3036 /* the ending 0 as well */
3037 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003038
Michal Vasko08e9b112021-06-11 15:41:17 +02003039 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
3040 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3041 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3042 dup->repeat[i][j - 1] = 0;
3043 }
Michal Vasko004d3152020-06-11 19:59:22 +02003044 }
3045 }
3046
3047 dup->used = exp->used;
3048 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003049 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003050
Michal Vasko1734be92020-09-22 08:55:10 +02003051cleanup:
3052 if (ret) {
3053 lyxp_expr_free(ctx, dup);
3054 } else {
3055 *dup_p = dup;
3056 }
3057 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003058}
3059
Michal Vasko03ff5a72019-09-11 13:49:33 +02003060/*
3061 * warn functions
3062 *
3063 * Warn functions check specific reasonable conditions for schema XPath
3064 * and print a warning if they are not satisfied.
3065 */
3066
3067/**
3068 * @brief Get the last-added schema node that is currently in the context.
3069 *
3070 * @param[in] set Set to search in.
3071 * @return Last-added schema context node, NULL if no node is in context.
3072 */
3073static struct lysc_node *
3074warn_get_scnode_in_ctx(struct lyxp_set *set)
3075{
3076 uint32_t i;
3077
3078 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3079 return NULL;
3080 }
3081
3082 i = set->used;
3083 do {
3084 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003085 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003086 /* if there are more, simply return the first found (last added) */
3087 return set->val.scnodes[i].scnode;
3088 }
3089 } while (i);
3090
3091 return NULL;
3092}
3093
3094/**
3095 * @brief Test whether a type is numeric - integer type or decimal64.
3096 *
3097 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003098 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003099 */
Radek Krejci857189e2020-09-01 13:26:36 +02003100static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003101warn_is_numeric_type(struct lysc_type *type)
3102{
3103 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003104 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003105 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003106
3107 switch (type->basetype) {
3108 case LY_TYPE_DEC64:
3109 case LY_TYPE_INT8:
3110 case LY_TYPE_UINT8:
3111 case LY_TYPE_INT16:
3112 case LY_TYPE_UINT16:
3113 case LY_TYPE_INT32:
3114 case LY_TYPE_UINT32:
3115 case LY_TYPE_INT64:
3116 case LY_TYPE_UINT64:
3117 return 1;
3118 case LY_TYPE_UNION:
3119 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003120 LY_ARRAY_FOR(uni->types, u) {
3121 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003122 if (ret) {
3123 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003124 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003125 }
3126 }
3127 /* did not find any suitable type */
3128 return 0;
3129 case LY_TYPE_LEAFREF:
3130 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3131 default:
3132 return 0;
3133 }
3134}
3135
3136/**
3137 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3138 *
3139 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003140 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003141 */
Radek Krejci857189e2020-09-01 13:26:36 +02003142static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003143warn_is_string_type(struct lysc_type *type)
3144{
3145 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003146 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003147 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003148
3149 switch (type->basetype) {
3150 case LY_TYPE_BITS:
3151 case LY_TYPE_ENUM:
3152 case LY_TYPE_IDENT:
3153 case LY_TYPE_INST:
3154 case LY_TYPE_STRING:
3155 return 1;
3156 case LY_TYPE_UNION:
3157 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003158 LY_ARRAY_FOR(uni->types, u) {
3159 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003160 if (ret) {
3161 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003162 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003163 }
3164 }
3165 /* did not find any suitable type */
3166 return 0;
3167 case LY_TYPE_LEAFREF:
3168 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3169 default:
3170 return 0;
3171 }
3172}
3173
3174/**
3175 * @brief Test whether a type is one specific type.
3176 *
3177 * @param[in] type Type to test.
3178 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003179 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003180 */
Radek Krejci857189e2020-09-01 13:26:36 +02003181static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003182warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3183{
3184 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003185 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003186 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003187
3188 if (type->basetype == base) {
3189 return 1;
3190 } else if (type->basetype == LY_TYPE_UNION) {
3191 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003192 LY_ARRAY_FOR(uni->types, u) {
3193 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003194 if (ret) {
3195 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003196 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003197 }
3198 }
3199 /* did not find any suitable type */
3200 return 0;
3201 } else if (type->basetype == LY_TYPE_LEAFREF) {
3202 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3203 }
3204
3205 return 0;
3206}
3207
3208/**
3209 * @brief Get next type of a (union) type.
3210 *
3211 * @param[in] type Base type.
3212 * @param[in] prev_type Previously returned type.
3213 * @return Next type or NULL.
3214 */
3215static struct lysc_type *
3216warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3217{
3218 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003219 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003220 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003221
3222 switch (type->basetype) {
3223 case LY_TYPE_UNION:
3224 uni = (struct lysc_type_union *)type;
3225 if (!prev_type) {
3226 return uni->types[0];
3227 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003228 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003229 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003230 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003231 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003232 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003233 found = 1;
3234 }
3235 }
3236 return NULL;
3237 default:
3238 if (prev_type) {
3239 assert(type == prev_type);
3240 return NULL;
3241 } else {
3242 return type;
3243 }
3244 }
3245}
3246
3247/**
3248 * @brief Test whether 2 types have a common type.
3249 *
3250 * @param[in] type1 First type.
3251 * @param[in] type2 Second type.
3252 * @return 1 if they do, 0 otherwise.
3253 */
3254static int
3255warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3256{
3257 struct lysc_type *t1, *rt1, *t2, *rt2;
3258
3259 t1 = NULL;
3260 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3261 if (t1->basetype == LY_TYPE_LEAFREF) {
3262 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3263 } else {
3264 rt1 = t1;
3265 }
3266
3267 t2 = NULL;
3268 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3269 if (t2->basetype == LY_TYPE_LEAFREF) {
3270 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3271 } else {
3272 rt2 = t2;
3273 }
3274
3275 if (rt2->basetype == rt1->basetype) {
3276 /* match found */
3277 return 1;
3278 }
3279 }
3280 }
3281
3282 return 0;
3283}
3284
3285/**
3286 * @brief Check both operands of comparison operators.
3287 *
3288 * @param[in] ctx Context for errors.
3289 * @param[in] set1 First operand set.
3290 * @param[in] set2 Second operand set.
3291 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3292 * @param[in] expr Start of the expression to print with the warning.
3293 * @param[in] tok_pos Token position.
3294 */
3295static void
Radek Krejci857189e2020-09-01 13:26:36 +02003296warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr, uint16_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003297{
3298 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003299 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003300
3301 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3302 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3303
3304 if (!node1 && !node2) {
3305 /* no node-sets involved, nothing to do */
3306 return;
3307 }
3308
3309 if (node1) {
3310 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3311 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3312 warning = 1;
3313 leaves = 0;
3314 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3315 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3316 warning = 1;
3317 }
3318 }
3319
3320 if (node2) {
3321 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3322 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3323 warning = 1;
3324 leaves = 0;
3325 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3326 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3327 warning = 1;
3328 }
3329 }
3330
3331 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003332 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3333 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3334 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3335 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003336 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3337 warning = 1;
3338 }
3339 }
3340
3341 if (warning) {
3342 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3343 }
3344}
3345
3346/**
3347 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3348 *
3349 * @param[in] exp Parsed XPath expression.
3350 * @param[in] set Set with the leaf/leaf-list.
3351 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3352 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3353 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3354 */
3355static void
Michal Vasko40308e72020-10-20 16:38:40 +02003356warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3357 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003358{
3359 struct lysc_node *scnode;
3360 struct lysc_type *type;
3361 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003362 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003363 LY_ERR rc;
3364 struct ly_err_item *err = NULL;
3365
Michal Vasko69730152020-10-09 16:30:07 +02003366 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3367 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003368 /* check that the node can have the specified value */
3369 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3370 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3371 } else {
3372 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3373 }
3374 if (!value) {
3375 LOGMEM(set->ctx);
3376 return;
3377 }
3378
3379 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3380 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003381 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003382 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003383 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003384 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003385 }
3386
3387 type = ((struct lysc_node_leaf *)scnode)->type;
3388 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003389 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003390 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003391 if (rc == LY_EINCOMPLETE) {
3392 rc = LY_SUCCESS;
3393 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003394
3395 if (err) {
3396 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3397 ly_err_free(err);
3398 } else if (rc != LY_SUCCESS) {
3399 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3400 }
3401 if (rc != LY_SUCCESS) {
3402 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003403 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003404 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003405 } else {
3406 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003407 }
3408 }
3409 free(value);
3410 }
3411}
3412
3413/*
3414 * XPath functions
3415 */
3416
3417/**
3418 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3419 * depending on whether the first node bit value from the second argument is set.
3420 *
3421 * @param[in] args Array of arguments.
3422 * @param[in] arg_count Count of elements in @p args.
3423 * @param[in,out] set Context and result set at the same time.
3424 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003425 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003426 */
3427static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003428xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003429{
3430 struct lyd_node_term *leaf;
3431 struct lysc_node_leaf *sleaf;
Michal Vasko2588b952021-07-29 07:43:26 +02003432 struct lyd_value_bits *bits;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003433 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003434 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003435
3436 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003437 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003438 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003439 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3440 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3441 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3442 sleaf->name);
3443 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3444 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3445 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003446 }
3447
3448 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3449 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003450 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3451 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003452 } else if (!warn_is_string_type(sleaf->type)) {
3453 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003454 }
3455 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003456 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003457 return rc;
3458 }
3459
Michal Vaskod3678892020-05-21 10:06:58 +02003460 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003461 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "bit-is-set(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003462 return LY_EVALID;
3463 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003464 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003465 LY_CHECK_RET(rc);
3466
3467 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003468 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003469 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko2588b952021-07-29 07:43:26 +02003470 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) {
3471 LYD_VALUE_GET(&leaf->value, bits);
3472 LY_ARRAY_FOR(bits->items, u) {
3473 if (!strcmp(bits->items[u]->name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003474 set_fill_boolean(set, 1);
3475 break;
3476 }
3477 }
3478 }
3479 }
3480
3481 return LY_SUCCESS;
3482}
3483
3484/**
3485 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3486 * with the argument converted to boolean.
3487 *
3488 * @param[in] args Array of arguments.
3489 * @param[in] arg_count Count of elements in @p args.
3490 * @param[in,out] set Context and result set at the same time.
3491 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003492 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003493 */
3494static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003495xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003496{
3497 LY_ERR rc;
3498
3499 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003500 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003501 return LY_SUCCESS;
3502 }
3503
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003504 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003505 LY_CHECK_RET(rc);
3506 set_fill_set(set, args[0]);
3507
3508 return LY_SUCCESS;
3509}
3510
3511/**
3512 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3513 * with the first argument rounded up to the nearest integer.
3514 *
3515 * @param[in] args Array of arguments.
3516 * @param[in] arg_count Count of elements in @p args.
3517 * @param[in,out] set Context and result set at the same time.
3518 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003519 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003520 */
3521static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003522xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003523{
3524 struct lysc_node_leaf *sleaf;
3525 LY_ERR rc = LY_SUCCESS;
3526
3527 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003528 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003529 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003530 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3531 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3532 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3533 sleaf->name);
3534 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3535 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3536 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003537 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003538 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003539 return rc;
3540 }
3541
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003542 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003543 LY_CHECK_RET(rc);
3544 if ((long long)args[0]->val.num != args[0]->val.num) {
3545 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3546 } else {
3547 set_fill_number(set, args[0]->val.num);
3548 }
3549
3550 return LY_SUCCESS;
3551}
3552
3553/**
3554 * @brief Execute the XPath concat(string, string, string*) function.
3555 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3556 *
3557 * @param[in] args Array of arguments.
3558 * @param[in] arg_count Count of elements in @p args.
3559 * @param[in,out] set Context and result set at the same time.
3560 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003561 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003562 */
3563static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003564xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003565{
3566 uint16_t i;
3567 char *str = NULL;
3568 size_t used = 1;
3569 LY_ERR rc = LY_SUCCESS;
3570 struct lysc_node_leaf *sleaf;
3571
3572 if (options & LYXP_SCNODE_ALL) {
3573 for (i = 0; i < arg_count; ++i) {
3574 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3575 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3576 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003577 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003578 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003579 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 +02003580 }
3581 }
3582 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003583 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003584 return rc;
3585 }
3586
3587 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003588 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003589 if (rc != LY_SUCCESS) {
3590 free(str);
3591 return rc;
3592 }
3593
3594 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3595 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3596 strcpy(str + used - 1, args[i]->val.str);
3597 used += strlen(args[i]->val.str);
3598 }
3599
3600 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003601 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003602 set->type = LYXP_SET_STRING;
3603 set->val.str = str;
3604
3605 return LY_SUCCESS;
3606}
3607
3608/**
3609 * @brief Execute the XPath contains(string, string) function.
3610 * Returns LYXP_SET_BOOLEAN whether the second argument can
3611 * be found in the first or not.
3612 *
3613 * @param[in] args Array of arguments.
3614 * @param[in] arg_count Count of elements in @p args.
3615 * @param[in,out] set Context and result set at the same time.
3616 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003617 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003618 */
3619static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003620xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003621{
3622 struct lysc_node_leaf *sleaf;
3623 LY_ERR rc = LY_SUCCESS;
3624
3625 if (options & LYXP_SCNODE_ALL) {
3626 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3627 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003628 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3629 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003630 } else if (!warn_is_string_type(sleaf->type)) {
3631 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003632 }
3633 }
3634
3635 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3636 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003637 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3638 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003639 } else if (!warn_is_string_type(sleaf->type)) {
3640 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003641 }
3642 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003643 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003644 return rc;
3645 }
3646
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003647 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003648 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003649 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003650 LY_CHECK_RET(rc);
3651
3652 if (strstr(args[0]->val.str, args[1]->val.str)) {
3653 set_fill_boolean(set, 1);
3654 } else {
3655 set_fill_boolean(set, 0);
3656 }
3657
3658 return LY_SUCCESS;
3659}
3660
3661/**
3662 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3663 * with the size of the node-set from the argument.
3664 *
3665 * @param[in] args Array of arguments.
3666 * @param[in] arg_count Count of elements in @p args.
3667 * @param[in,out] set Context and result set at the same time.
3668 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003669 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003670 */
3671static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003672xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003673{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003674 LY_ERR rc = LY_SUCCESS;
3675
3676 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003677 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003678 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003679 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003680 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003681 return rc;
3682 }
3683
Michal Vasko03ff5a72019-09-11 13:49:33 +02003684 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003685 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003686 return LY_EVALID;
3687 }
3688
3689 set_fill_number(set, args[0]->used);
3690 return LY_SUCCESS;
3691}
3692
3693/**
3694 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3695 * with the context with the intial node.
3696 *
3697 * @param[in] args Array of arguments.
3698 * @param[in] arg_count Count of elements in @p args.
3699 * @param[in,out] set Context and result set at the same time.
3700 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003701 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003702 */
3703static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003704xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003705{
3706 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003707 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003708 return LY_EVALID;
3709 }
3710
3711 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003712 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003713
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003714 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003715 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003716 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003717
3718 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003719 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003720 }
3721
3722 return LY_SUCCESS;
3723}
3724
3725/**
3726 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3727 * leafref or instance-identifier target node(s).
3728 *
3729 * @param[in] args Array of arguments.
3730 * @param[in] arg_count Count of elements in @p args.
3731 * @param[in,out] set Context and result set at the same time.
3732 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003733 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003734 */
3735static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003736xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003737{
3738 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003739 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003740 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003741 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003742 struct ly_path *p;
3743 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003744 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003745 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02003746 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003747
3748 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003749 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003750 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003751 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko423ba3f2021-07-19 13:08:50 +02003752 if (!(sleaf->nodetype & LYD_NODE_TERM)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003753 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3754 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02003755 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
3756 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003757 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3758 __func__, sleaf->name);
3759 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003760 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003761 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko423ba3f2021-07-19 13:08:50 +02003762 if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003763 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003764 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003765
3766 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02003767 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02003768 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02003769 if (!r) {
3770 /* get the target node */
3771 target = p[LY_ARRAY_COUNT(p) - 1].node;
3772 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02003773
Michal Vasko741bb562021-06-24 11:59:50 +02003774 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
3775 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003776 }
3777
Michal Vasko741bb562021-06-24 11:59:50 +02003778 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003779 }
3780
Michal Vaskod3678892020-05-21 10:06:58 +02003781 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003782 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003783 return LY_EVALID;
3784 }
3785
Michal Vaskod3678892020-05-21 10:06:58 +02003786 lyxp_set_free_content(set);
3787 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003788 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3789 sleaf = (struct lysc_node_leaf *)leaf->schema;
3790 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3791 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3792 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02003793 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01003794 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003795 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003796 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003797 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003798 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003799 } else {
3800 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003801 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003802 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003803 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003804 return LY_EVALID;
3805 }
3806 }
Michal Vasko004d3152020-06-11 19:59:22 +02003807
3808 /* insert it */
3809 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003810 }
3811 }
3812
3813 return LY_SUCCESS;
3814}
3815
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003816static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003817xpath_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 +02003818{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01003819 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003820 LY_ARRAY_COUNT_TYPE u;
3821 struct lyd_node_term *leaf;
3822 struct lysc_node_leaf *sleaf;
3823 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02003824 struct lyd_value *val;
3825 const struct lys_module *mod;
3826 const char *id_name;
3827 uint16_t id_len;
3828 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003829 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003830 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003831
3832 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003833 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003834 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003835 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3836 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3837 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3838 sleaf->name);
3839 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3840 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3841 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003842 }
3843
3844 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3845 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3846 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003847 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003848 } else if (!warn_is_string_type(sleaf->type)) {
3849 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3850 }
3851 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003852 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003853 return rc;
3854 }
3855
3856 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003857 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 +02003858 return LY_EVALID;
3859 }
3860 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3861 LY_CHECK_RET(rc);
3862
Michal Vasko93923692021-05-07 15:28:02 +02003863 /* parse the identity */
3864 id_name = args[1]->val.str;
3865 id_len = strlen(id_name);
3866 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
3867 LY_CHECK_RET(rc);
3868 if (!mod) {
3869 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
3870 return LY_EVALID;
3871 }
3872
3873 /* find the identity */
3874 found = 0;
3875 LY_ARRAY_FOR(mod->identities, u) {
3876 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
3877 /* we have match */
3878 found = 1;
3879 break;
3880 }
3881 }
3882 if (!found) {
3883 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
3884 return LY_EVALID;
3885 }
3886 id = &mod->identities[u];
3887
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003888 set_fill_boolean(set, 0);
3889 found = 0;
3890 for (i = 0; i < args[0]->used; ++i) {
3891 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3892 continue;
3893 }
3894
3895 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3896 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3897 sleaf = (struct lysc_node_leaf *)leaf->schema;
3898 val = &leaf->value;
3899 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3900 /* uninteresting */
3901 continue;
3902 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003903 } else {
3904 meta = args[0]->val.meta[i].meta;
3905 val = &meta->value;
3906 if (val->realtype->basetype != LY_TYPE_IDENT) {
3907 /* uninteresting */
3908 continue;
3909 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003910 }
3911
Michal Vasko93923692021-05-07 15:28:02 +02003912 /* check the identity itself */
3913 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003914 set_fill_boolean(set, 1);
3915 found = 1;
3916 }
Michal Vasko93923692021-05-07 15:28:02 +02003917 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
3918 set_fill_boolean(set, 1);
3919 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003920 }
3921
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003922 if (found) {
3923 break;
3924 }
3925 }
3926
3927 return LY_SUCCESS;
3928}
3929
Michal Vasko03ff5a72019-09-11 13:49:33 +02003930/**
3931 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3932 * on whether the first argument nodes contain a node of an identity derived from the second
3933 * argument identity.
3934 *
3935 * @param[in] args Array of arguments.
3936 * @param[in] arg_count Count of elements in @p args.
3937 * @param[in,out] set Context and result set at the same time.
3938 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003939 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003940 */
3941static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003942xpath_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 +02003943{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003944 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003945}
3946
3947/**
3948 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3949 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3950 * the second argument identity.
3951 *
3952 * @param[in] args Array of arguments.
3953 * @param[in] arg_count Count of elements in @p args.
3954 * @param[in,out] set Context and result set at the same time.
3955 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003956 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003957 */
3958static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003959xpath_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 +02003960{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003961 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003962}
3963
3964/**
3965 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3966 * with the integer value of the first node's enum value, otherwise NaN.
3967 *
3968 * @param[in] args Array of arguments.
3969 * @param[in] arg_count Count of elements in @p args.
3970 * @param[in,out] set Context and result set at the same time.
3971 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003972 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003973 */
3974static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003975xpath_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 +02003976{
3977 struct lyd_node_term *leaf;
3978 struct lysc_node_leaf *sleaf;
3979 LY_ERR rc = LY_SUCCESS;
3980
3981 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003982 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003983 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003984 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3985 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3986 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3987 sleaf->name);
3988 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3989 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
3990 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003991 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003992 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003993 return rc;
3994 }
3995
Michal Vaskod3678892020-05-21 10:06:58 +02003996 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003997 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003998 return LY_EVALID;
3999 }
4000
4001 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004002 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004003 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4004 sleaf = (struct lysc_node_leaf *)leaf->schema;
4005 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4006 set_fill_number(set, leaf->value.enum_item->value);
4007 }
4008 }
4009
4010 return LY_SUCCESS;
4011}
4012
4013/**
4014 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4015 * with false value.
4016 *
4017 * @param[in] args Array of arguments.
4018 * @param[in] arg_count Count of elements in @p args.
4019 * @param[in,out] set Context and result set at the same time.
4020 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004021 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004022 */
4023static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004024xpath_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 +02004025{
4026 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004027 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004028 return LY_SUCCESS;
4029 }
4030
4031 set_fill_boolean(set, 0);
4032 return LY_SUCCESS;
4033}
4034
4035/**
4036 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4037 * with the first argument floored (truncated).
4038 *
4039 * @param[in] args Array of arguments.
4040 * @param[in] arg_count Count of elements in @p args.
4041 * @param[in,out] set Context and result set at the same time.
4042 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004043 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004044 */
4045static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004046xpath_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 +02004047{
4048 LY_ERR rc;
4049
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004050 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004051 LY_CHECK_RET(rc);
4052 if (isfinite(args[0]->val.num)) {
4053 set_fill_number(set, (long long)args[0]->val.num);
4054 }
4055
4056 return LY_SUCCESS;
4057}
4058
4059/**
4060 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4061 * whether the language of the text matches the one from the argument.
4062 *
4063 * @param[in] args Array of arguments.
4064 * @param[in] arg_count Count of elements in @p args.
4065 * @param[in,out] set Context and result set at the same time.
4066 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004067 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004068 */
4069static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004070xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004071{
4072 const struct lyd_node *node;
4073 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004074 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004075 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004076 LY_ERR rc = LY_SUCCESS;
4077
4078 if (options & LYXP_SCNODE_ALL) {
4079 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4080 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004081 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4082 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004083 } else if (!warn_is_string_type(sleaf->type)) {
4084 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004085 }
4086 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004087 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004088 return rc;
4089 }
4090
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004091 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004092 LY_CHECK_RET(rc);
4093
Michal Vasko03ff5a72019-09-11 13:49:33 +02004094 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004095 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004096 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004097 } else if (!set->used) {
4098 set_fill_boolean(set, 0);
4099 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004100 }
4101
4102 switch (set->val.nodes[0].type) {
4103 case LYXP_NODE_ELEM:
4104 case LYXP_NODE_TEXT:
4105 node = set->val.nodes[0].node;
4106 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004107 case LYXP_NODE_META:
4108 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004109 break;
4110 default:
4111 /* nothing to do with roots */
4112 set_fill_boolean(set, 0);
4113 return LY_SUCCESS;
4114 }
4115
Michal Vasko9f96a052020-03-10 09:41:45 +01004116 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004117 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004118 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004119 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004120 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004121 break;
4122 }
4123 }
4124
Michal Vasko9f96a052020-03-10 09:41:45 +01004125 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004126 break;
4127 }
4128 }
4129
4130 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004131 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004132 set_fill_boolean(set, 0);
4133 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004134 uint64_t i;
4135
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004136 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004137 for (i = 0; args[0]->val.str[i]; ++i) {
4138 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4139 set_fill_boolean(set, 0);
4140 break;
4141 }
4142 }
4143 if (!args[0]->val.str[i]) {
4144 if (!val[i] || (val[i] == '-')) {
4145 set_fill_boolean(set, 1);
4146 } else {
4147 set_fill_boolean(set, 0);
4148 }
4149 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004150 }
4151
4152 return LY_SUCCESS;
4153}
4154
4155/**
4156 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4157 * with the context size.
4158 *
4159 * @param[in] args Array of arguments.
4160 * @param[in] arg_count Count of elements in @p args.
4161 * @param[in,out] set Context and result set at the same time.
4162 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004163 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004164 */
4165static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004166xpath_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 +02004167{
4168 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004169 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004170 return LY_SUCCESS;
4171 }
4172
Michal Vasko03ff5a72019-09-11 13:49:33 +02004173 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004174 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004175 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004176 } else if (!set->used) {
4177 set_fill_number(set, 0);
4178 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004179 }
4180
4181 set_fill_number(set, set->ctx_size);
4182 return LY_SUCCESS;
4183}
4184
4185/**
4186 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4187 * with the node name without namespace from the argument or the context.
4188 *
4189 * @param[in] args Array of arguments.
4190 * @param[in] arg_count Count of elements in @p args.
4191 * @param[in,out] set Context and result set at the same time.
4192 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004193 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004194 */
4195static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004196xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004197{
4198 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004199
Michal Vasko03ff5a72019-09-11 13:49:33 +02004200 /* suppress unused variable warning */
4201 (void)options;
4202
4203 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004204 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004205 return LY_SUCCESS;
4206 }
4207
4208 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004209 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004210 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004211 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004212 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004213 } else if (!args[0]->used) {
4214 set_fill_string(set, "", 0);
4215 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004216 }
4217
4218 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004219 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004220
4221 item = &args[0]->val.nodes[0];
4222 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004223 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004224 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004225 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004226 } else if (!set->used) {
4227 set_fill_string(set, "", 0);
4228 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004229 }
4230
4231 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004232 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004233
4234 item = &set->val.nodes[0];
4235 }
4236
4237 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004238 case LYXP_NODE_NONE:
4239 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004240 case LYXP_NODE_ROOT:
4241 case LYXP_NODE_ROOT_CONFIG:
4242 case LYXP_NODE_TEXT:
4243 set_fill_string(set, "", 0);
4244 break;
4245 case LYXP_NODE_ELEM:
4246 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4247 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004248 case LYXP_NODE_META:
4249 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004250 break;
4251 }
4252
4253 return LY_SUCCESS;
4254}
4255
4256/**
4257 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4258 * with the node name fully qualified (with namespace) from the argument or the context.
4259 *
4260 * @param[in] args Array of arguments.
4261 * @param[in] arg_count Count of elements in @p args.
4262 * @param[in,out] set Context and result set at the same time.
4263 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004264 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004265 */
4266static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004267xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004268{
4269 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004270 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004271 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004272 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004273
4274 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004275 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004276 return LY_SUCCESS;
4277 }
4278
4279 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004280 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004281 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004282 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004283 } else if (!args[0]->used) {
4284 set_fill_string(set, "", 0);
4285 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004286 }
4287
4288 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004289 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004290
4291 item = &args[0]->val.nodes[0];
4292 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004293 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004294 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004295 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004296 } else if (!set->used) {
4297 set_fill_string(set, "", 0);
4298 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004299 }
4300
4301 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004302 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004303
4304 item = &set->val.nodes[0];
4305 }
4306
4307 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004308 case LYXP_NODE_NONE:
4309 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004310 case LYXP_NODE_ROOT:
4311 case LYXP_NODE_ROOT_CONFIG:
4312 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004313 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004314 break;
4315 case LYXP_NODE_ELEM:
4316 mod = item->node->schema->module;
4317 name = item->node->schema->name;
4318 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004319 case LYXP_NODE_META:
4320 mod = ((struct lyd_meta *)item->node)->annotation->module;
4321 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004322 break;
4323 }
4324
4325 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004326 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004327 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4328 set_fill_string(set, str, strlen(str));
4329 free(str);
4330 } else {
4331 set_fill_string(set, "", 0);
4332 }
4333
4334 return LY_SUCCESS;
4335}
4336
4337/**
4338 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4339 * with the namespace of the node from the argument or the context.
4340 *
4341 * @param[in] args Array of arguments.
4342 * @param[in] arg_count Count of elements in @p args.
4343 * @param[in,out] set Context and result set at the same time.
4344 * @param[in] options XPath options.
4345 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4346 */
4347static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004348xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004349{
4350 struct lyxp_set_node *item;
4351 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004352
Michal Vasko03ff5a72019-09-11 13:49:33 +02004353 /* suppress unused variable warning */
4354 (void)options;
4355
4356 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004357 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004358 return LY_SUCCESS;
4359 }
4360
4361 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004362 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004363 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004364 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004365 return LY_EVALID;
4366 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004367 set_fill_string(set, "", 0);
4368 return LY_SUCCESS;
4369 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004370
4371 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004372 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004373
4374 item = &args[0]->val.nodes[0];
4375 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004376 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004377 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004378 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004379 } else if (!set->used) {
4380 set_fill_string(set, "", 0);
4381 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004382 }
4383
4384 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004385 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004386
4387 item = &set->val.nodes[0];
4388 }
4389
4390 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004391 case LYXP_NODE_NONE:
4392 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004393 case LYXP_NODE_ROOT:
4394 case LYXP_NODE_ROOT_CONFIG:
4395 case LYXP_NODE_TEXT:
4396 set_fill_string(set, "", 0);
4397 break;
4398 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004399 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004400 if (item->type == LYXP_NODE_ELEM) {
4401 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004402 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004403 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004404 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004405 }
4406
4407 set_fill_string(set, mod->ns, strlen(mod->ns));
4408 break;
4409 }
4410
4411 return LY_SUCCESS;
4412}
4413
4414/**
4415 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4416 * with only nodes from the context. In practice it either leaves the context
4417 * as it is or returns an empty node set.
4418 *
4419 * @param[in] args Array of arguments.
4420 * @param[in] arg_count Count of elements in @p args.
4421 * @param[in,out] set Context and result set at the same time.
4422 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004423 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004424 */
4425static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004426xpath_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 +02004427{
4428 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004429 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004430 return LY_SUCCESS;
4431 }
4432
4433 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004434 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004435 }
4436 return LY_SUCCESS;
4437}
4438
4439/**
4440 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4441 * with normalized value (no leading, trailing, double white spaces) of the node
4442 * from the argument or the context.
4443 *
4444 * @param[in] args Array of arguments.
4445 * @param[in] arg_count Count of elements in @p args.
4446 * @param[in,out] set Context and result set at the same time.
4447 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004448 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004449 */
4450static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004451xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004452{
4453 uint16_t i, new_used;
4454 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004455 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004456 struct lysc_node_leaf *sleaf;
4457 LY_ERR rc = LY_SUCCESS;
4458
4459 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004460 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4461 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004462 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004463 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4464 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004465 } else if (!warn_is_string_type(sleaf->type)) {
4466 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004467 }
4468 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004469 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004470 return rc;
4471 }
4472
4473 if (arg_count) {
4474 set_fill_set(set, args[0]);
4475 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004476 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004477 LY_CHECK_RET(rc);
4478
4479 /* is there any normalization necessary? */
4480 for (i = 0; set->val.str[i]; ++i) {
4481 if (is_xmlws(set->val.str[i])) {
4482 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4483 have_spaces = 1;
4484 break;
4485 }
4486 space_before = 1;
4487 } else {
4488 space_before = 0;
4489 }
4490 }
4491
4492 /* yep, there is */
4493 if (have_spaces) {
4494 /* it's enough, at least one character will go, makes space for ending '\0' */
4495 new = malloc(strlen(set->val.str) * sizeof(char));
4496 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4497 new_used = 0;
4498
4499 space_before = 0;
4500 for (i = 0; set->val.str[i]; ++i) {
4501 if (is_xmlws(set->val.str[i])) {
4502 if ((i == 0) || space_before) {
4503 space_before = 1;
4504 continue;
4505 } else {
4506 space_before = 1;
4507 }
4508 } else {
4509 space_before = 0;
4510 }
4511
4512 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4513 ++new_used;
4514 }
4515
4516 /* at worst there is one trailing space now */
4517 if (new_used && is_xmlws(new[new_used - 1])) {
4518 --new_used;
4519 }
4520
4521 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4522 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4523 new[new_used] = '\0';
4524
4525 free(set->val.str);
4526 set->val.str = new;
4527 }
4528
4529 return LY_SUCCESS;
4530}
4531
4532/**
4533 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4534 * with the argument converted to boolean and logically inverted.
4535 *
4536 * @param[in] args Array of arguments.
4537 * @param[in] arg_count Count of elements in @p args.
4538 * @param[in,out] set Context and result set at the same time.
4539 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004540 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004541 */
4542static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004543xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004544{
4545 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004546 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004547 return LY_SUCCESS;
4548 }
4549
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004550 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004551 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004552 set_fill_boolean(set, 0);
4553 } else {
4554 set_fill_boolean(set, 1);
4555 }
4556
4557 return LY_SUCCESS;
4558}
4559
4560/**
4561 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4562 * with the number representation of either the argument or the context.
4563 *
4564 * @param[in] args Array of arguments.
4565 * @param[in] arg_count Count of elements in @p args.
4566 * @param[in,out] set Context and result set at the same time.
4567 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004568 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004569 */
4570static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004571xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004572{
4573 LY_ERR rc;
4574
4575 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004576 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004577 return LY_SUCCESS;
4578 }
4579
4580 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004581 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004582 LY_CHECK_RET(rc);
4583 set_fill_set(set, args[0]);
4584 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004585 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004586 LY_CHECK_RET(rc);
4587 }
4588
4589 return LY_SUCCESS;
4590}
4591
4592/**
4593 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4594 * with the context position.
4595 *
4596 * @param[in] args Array of arguments.
4597 * @param[in] arg_count Count of elements in @p args.
4598 * @param[in,out] set Context and result set at the same time.
4599 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004600 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004601 */
4602static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004603xpath_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 +02004604{
4605 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004606 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004607 return LY_SUCCESS;
4608 }
4609
Michal Vasko03ff5a72019-09-11 13:49:33 +02004610 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004611 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004612 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004613 } else if (!set->used) {
4614 set_fill_number(set, 0);
4615 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004616 }
4617
4618 set_fill_number(set, set->ctx_pos);
4619
4620 /* UNUSED in 'Release' build type */
4621 (void)options;
4622 return LY_SUCCESS;
4623}
4624
4625/**
4626 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4627 * depending on whether the second argument regex matches the first argument string. For details refer to
4628 * YANG 1.1 RFC section 10.2.1.
4629 *
4630 * @param[in] args Array of arguments.
4631 * @param[in] arg_count Count of elements in @p args.
4632 * @param[in,out] set Context and result set at the same time.
4633 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004634 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004635 */
4636static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004637xpath_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 +02004638{
4639 struct lysc_pattern **patterns = NULL, **pattern;
4640 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004641 LY_ERR rc = LY_SUCCESS;
4642 struct ly_err_item *err;
4643
4644 if (options & LYXP_SCNODE_ALL) {
4645 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4646 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4647 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 +02004648 } else if (!warn_is_string_type(sleaf->type)) {
4649 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004650 }
4651 }
4652
4653 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4654 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4655 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 +02004656 } else if (!warn_is_string_type(sleaf->type)) {
4657 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004658 }
4659 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004660 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004661 return rc;
4662 }
4663
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004664 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004665 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004666 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004667 LY_CHECK_RET(rc);
4668
4669 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek IÅ¡a45802b52021-02-09 09:21:58 +01004670 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004671 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004672 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Radek Krejciddace2c2021-01-08 11:30:56 +01004673 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004674 if (rc != LY_SUCCESS) {
4675 LY_ARRAY_FREE(patterns);
4676 return rc;
4677 }
4678
Radek Krejci0b013302021-03-29 15:22:32 +02004679 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004680 pcre2_code_free((*pattern)->code);
4681 free(*pattern);
4682 LY_ARRAY_FREE(patterns);
4683 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004684 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004685 ly_err_free(err);
4686 return rc;
4687 }
4688
4689 if (rc == LY_EVALID) {
4690 ly_err_free(err);
4691 set_fill_boolean(set, 0);
4692 } else {
4693 set_fill_boolean(set, 1);
4694 }
4695
4696 return LY_SUCCESS;
4697}
4698
4699/**
4700 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4701 * with the rounded first argument. For details refer to
4702 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4703 *
4704 * @param[in] args Array of arguments.
4705 * @param[in] arg_count Count of elements in @p args.
4706 * @param[in,out] set Context and result set at the same time.
4707 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004708 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004709 */
4710static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004711xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004712{
4713 struct lysc_node_leaf *sleaf;
4714 LY_ERR rc = LY_SUCCESS;
4715
4716 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004717 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004718 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004719 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4720 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4721 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4722 sleaf->name);
4723 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4724 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4725 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004726 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004727 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004728 return rc;
4729 }
4730
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004731 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004732 LY_CHECK_RET(rc);
4733
4734 /* cover only the cases where floor can't be used */
4735 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4736 set_fill_number(set, -0.0f);
4737 } else {
4738 args[0]->val.num += 0.5;
4739 rc = xpath_floor(args, 1, args[0], options);
4740 LY_CHECK_RET(rc);
4741 set_fill_number(set, args[0]->val.num);
4742 }
4743
4744 return LY_SUCCESS;
4745}
4746
4747/**
4748 * @brief Execute the XPath starts-with(string, string) function.
4749 * Returns LYXP_SET_BOOLEAN whether the second argument is
4750 * the prefix of the first or not.
4751 *
4752 * @param[in] args Array of arguments.
4753 * @param[in] arg_count Count of elements in @p args.
4754 * @param[in,out] set Context and result set at the same time.
4755 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004756 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004757 */
4758static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004759xpath_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 +02004760{
4761 struct lysc_node_leaf *sleaf;
4762 LY_ERR rc = LY_SUCCESS;
4763
4764 if (options & LYXP_SCNODE_ALL) {
4765 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4766 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4767 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 +02004768 } else if (!warn_is_string_type(sleaf->type)) {
4769 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004770 }
4771 }
4772
4773 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4774 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4775 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 +02004776 } else if (!warn_is_string_type(sleaf->type)) {
4777 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004778 }
4779 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004780 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004781 return rc;
4782 }
4783
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004784 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004785 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004786 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004787 LY_CHECK_RET(rc);
4788
4789 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4790 set_fill_boolean(set, 0);
4791 } else {
4792 set_fill_boolean(set, 1);
4793 }
4794
4795 return LY_SUCCESS;
4796}
4797
4798/**
4799 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4800 * with the string representation of either the argument or the context.
4801 *
4802 * @param[in] args Array of arguments.
4803 * @param[in] arg_count Count of elements in @p args.
4804 * @param[in,out] set Context and result set at the same time.
4805 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004806 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004807 */
4808static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004809xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004810{
4811 LY_ERR rc;
4812
4813 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004814 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004815 return LY_SUCCESS;
4816 }
4817
4818 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004819 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004820 LY_CHECK_RET(rc);
4821 set_fill_set(set, args[0]);
4822 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004823 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004824 LY_CHECK_RET(rc);
4825 }
4826
4827 return LY_SUCCESS;
4828}
4829
4830/**
4831 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4832 * with the length of the string in either the argument or the context.
4833 *
4834 * @param[in] args Array of arguments.
4835 * @param[in] arg_count Count of elements in @p args.
4836 * @param[in,out] set Context and result set at the same time.
4837 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004838 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004839 */
4840static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004841xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004842{
4843 struct lysc_node_leaf *sleaf;
4844 LY_ERR rc = LY_SUCCESS;
4845
4846 if (options & LYXP_SCNODE_ALL) {
4847 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4848 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4849 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 +02004850 } else if (!warn_is_string_type(sleaf->type)) {
4851 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004852 }
4853 }
4854 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4855 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4856 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 +02004857 } else if (!warn_is_string_type(sleaf->type)) {
4858 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004859 }
4860 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004861 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004862 return rc;
4863 }
4864
4865 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004866 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004867 LY_CHECK_RET(rc);
4868 set_fill_number(set, strlen(args[0]->val.str));
4869 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004870 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004871 LY_CHECK_RET(rc);
4872 set_fill_number(set, strlen(set->val.str));
4873 }
4874
4875 return LY_SUCCESS;
4876}
4877
4878/**
4879 * @brief Execute the XPath substring(string, number, number?) function.
4880 * Returns LYXP_SET_STRING substring of the first argument starting
4881 * on the second argument index ending on the third argument index,
4882 * indexed from 1. For exact definition refer to
4883 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4884 *
4885 * @param[in] args Array of arguments.
4886 * @param[in] arg_count Count of elements in @p args.
4887 * @param[in,out] set Context and result set at the same time.
4888 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004889 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004890 */
4891static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004892xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004893{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004894 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004895 uint16_t str_start, str_len, pos;
4896 struct lysc_node_leaf *sleaf;
4897 LY_ERR rc = LY_SUCCESS;
4898
4899 if (options & LYXP_SCNODE_ALL) {
4900 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4901 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4902 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 +02004903 } else if (!warn_is_string_type(sleaf->type)) {
4904 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004905 }
4906 }
4907
4908 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4909 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4910 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 +02004911 } else if (!warn_is_numeric_type(sleaf->type)) {
4912 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004913 }
4914 }
4915
Michal Vasko69730152020-10-09 16:30:07 +02004916 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4917 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004918 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4919 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 +02004920 } else if (!warn_is_numeric_type(sleaf->type)) {
4921 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004922 }
4923 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004924 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004925 return rc;
4926 }
4927
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004928 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004929 LY_CHECK_RET(rc);
4930
4931 /* start */
4932 if (xpath_round(&args[1], 1, args[1], options)) {
4933 return -1;
4934 }
4935 if (isfinite(args[1]->val.num)) {
4936 start = args[1]->val.num - 1;
4937 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004938 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004939 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004940 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004941 }
4942
4943 /* len */
4944 if (arg_count == 3) {
4945 rc = xpath_round(&args[2], 1, args[2], options);
4946 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004947 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004948 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004949 } else if (isfinite(args[2]->val.num)) {
4950 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004951 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004952 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004953 }
4954 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004955 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004956 }
4957
4958 /* find matching character positions */
4959 str_start = 0;
4960 str_len = 0;
4961 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4962 if (pos < start) {
4963 ++str_start;
4964 } else if (pos < start + len) {
4965 ++str_len;
4966 } else {
4967 break;
4968 }
4969 }
4970
4971 set_fill_string(set, args[0]->val.str + str_start, str_len);
4972 return LY_SUCCESS;
4973}
4974
4975/**
4976 * @brief Execute the XPath substring-after(string, string) function.
4977 * Returns LYXP_SET_STRING with the string succeeding the occurance
4978 * of the second argument in the first or an empty string.
4979 *
4980 * @param[in] args Array of arguments.
4981 * @param[in] arg_count Count of elements in @p args.
4982 * @param[in,out] set Context and result set at the same time.
4983 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004984 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004985 */
4986static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004987xpath_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 +02004988{
4989 char *ptr;
4990 struct lysc_node_leaf *sleaf;
4991 LY_ERR rc = LY_SUCCESS;
4992
4993 if (options & LYXP_SCNODE_ALL) {
4994 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4995 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4996 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 +02004997 } else if (!warn_is_string_type(sleaf->type)) {
4998 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004999 }
5000 }
5001
5002 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5003 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5004 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 +02005005 } else if (!warn_is_string_type(sleaf->type)) {
5006 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005007 }
5008 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005009 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005010 return rc;
5011 }
5012
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005013 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005014 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005015 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005016 LY_CHECK_RET(rc);
5017
5018 ptr = strstr(args[0]->val.str, args[1]->val.str);
5019 if (ptr) {
5020 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5021 } else {
5022 set_fill_string(set, "", 0);
5023 }
5024
5025 return LY_SUCCESS;
5026}
5027
5028/**
5029 * @brief Execute the XPath substring-before(string, string) function.
5030 * Returns LYXP_SET_STRING with the string preceding the occurance
5031 * of the second argument in the first or an empty string.
5032 *
5033 * @param[in] args Array of arguments.
5034 * @param[in] arg_count Count of elements in @p args.
5035 * @param[in,out] set Context and result set at the same time.
5036 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005037 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005038 */
5039static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005040xpath_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 +02005041{
5042 char *ptr;
5043 struct lysc_node_leaf *sleaf;
5044 LY_ERR rc = LY_SUCCESS;
5045
5046 if (options & LYXP_SCNODE_ALL) {
5047 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5048 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5049 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 +02005050 } else if (!warn_is_string_type(sleaf->type)) {
5051 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005052 }
5053 }
5054
5055 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5056 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5057 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 +02005058 } else if (!warn_is_string_type(sleaf->type)) {
5059 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005060 }
5061 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005062 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005063 return rc;
5064 }
5065
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005066 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005067 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005068 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005069 LY_CHECK_RET(rc);
5070
5071 ptr = strstr(args[0]->val.str, args[1]->val.str);
5072 if (ptr) {
5073 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5074 } else {
5075 set_fill_string(set, "", 0);
5076 }
5077
5078 return LY_SUCCESS;
5079}
5080
5081/**
5082 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5083 * with the sum of all the nodes in the context.
5084 *
5085 * @param[in] args Array of arguments.
5086 * @param[in] arg_count Count of elements in @p args.
5087 * @param[in,out] set Context and result set at the same time.
5088 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005089 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005090 */
5091static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005092xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005093{
5094 long double num;
5095 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005096 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005097 struct lyxp_set set_item;
5098 struct lysc_node_leaf *sleaf;
5099 LY_ERR rc = LY_SUCCESS;
5100
5101 if (options & LYXP_SCNODE_ALL) {
5102 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5103 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005104 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005105 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5106 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5107 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005108 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005109 } else if (!warn_is_numeric_type(sleaf->type)) {
5110 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005111 }
5112 }
5113 }
5114 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005115 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005116 return rc;
5117 }
5118
5119 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005120
5121 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005122 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005123 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005124 } else if (!args[0]->used) {
5125 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005126 }
5127
Michal Vasko5c4e5892019-11-14 12:31:38 +01005128 set_init(&set_item, set);
5129
Michal Vasko03ff5a72019-09-11 13:49:33 +02005130 set_item.type = LYXP_SET_NODE_SET;
5131 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5132 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5133
5134 set_item.used = 1;
5135 set_item.size = 1;
5136
5137 for (i = 0; i < args[0]->used; ++i) {
5138 set_item.val.nodes[0] = args[0]->val.nodes[i];
5139
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005140 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005141 LY_CHECK_RET(rc);
5142 num = cast_string_to_number(str);
5143 free(str);
5144 set->val.num += num;
5145 }
5146
5147 free(set_item.val.nodes);
5148
5149 return LY_SUCCESS;
5150}
5151
5152/**
5153 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5154 * with the text content of the nodes in the context.
5155 *
5156 * @param[in] args Array of arguments.
5157 * @param[in] arg_count Count of elements in @p args.
5158 * @param[in,out] set Context and result set at the same time.
5159 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005160 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005161 */
5162static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005163xpath_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 +02005164{
5165 uint32_t i;
5166
5167 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005168 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005169 return LY_SUCCESS;
5170 }
5171
Michal Vasko03ff5a72019-09-11 13:49:33 +02005172 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005173 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005174 return LY_EVALID;
5175 }
5176
Michal Vaskod989ba02020-08-24 10:59:24 +02005177 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005178 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005179 case LYXP_NODE_NONE:
5180 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005181 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005182 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5183 set->val.nodes[i].type = LYXP_NODE_TEXT;
5184 ++i;
5185 break;
5186 }
Radek Krejci0f969882020-08-21 16:56:47 +02005187 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005188 case LYXP_NODE_ROOT:
5189 case LYXP_NODE_ROOT_CONFIG:
5190 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005191 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005192 set_remove_node(set, i);
5193 break;
5194 }
5195 }
5196
5197 return LY_SUCCESS;
5198}
5199
5200/**
5201 * @brief Execute the XPath translate(string, string, string) function.
5202 * Returns LYXP_SET_STRING with the first argument with the characters
5203 * from the second argument replaced by those on the corresponding
5204 * positions in the third argument.
5205 *
5206 * @param[in] args Array of arguments.
5207 * @param[in] arg_count Count of elements in @p args.
5208 * @param[in,out] set Context and result set at the same time.
5209 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005210 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005211 */
5212static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005213xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005214{
5215 uint16_t i, j, new_used;
5216 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005217 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005218 struct lysc_node_leaf *sleaf;
5219 LY_ERR rc = LY_SUCCESS;
5220
5221 if (options & LYXP_SCNODE_ALL) {
5222 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5223 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5224 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 +02005225 } else if (!warn_is_string_type(sleaf->type)) {
5226 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005227 }
5228 }
5229
5230 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5231 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5232 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 +02005233 } else if (!warn_is_string_type(sleaf->type)) {
5234 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005235 }
5236 }
5237
5238 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5239 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5240 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 +02005241 } else if (!warn_is_string_type(sleaf->type)) {
5242 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005243 }
5244 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005245 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005246 return rc;
5247 }
5248
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005249 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005250 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005251 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005252 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005253 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005254 LY_CHECK_RET(rc);
5255
5256 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5257 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5258 new_used = 0;
5259
5260 have_removed = 0;
5261 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005262 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005263
5264 for (j = 0; args[1]->val.str[j]; ++j) {
5265 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5266 /* removing this char */
5267 if (j >= strlen(args[2]->val.str)) {
5268 have_removed = 1;
5269 found = 1;
5270 break;
5271 }
5272 /* replacing this char */
5273 new[new_used] = args[2]->val.str[j];
5274 ++new_used;
5275 found = 1;
5276 break;
5277 }
5278 }
5279
5280 /* copying this char */
5281 if (!found) {
5282 new[new_used] = args[0]->val.str[i];
5283 ++new_used;
5284 }
5285 }
5286
5287 if (have_removed) {
5288 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5289 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5290 }
5291 new[new_used] = '\0';
5292
Michal Vaskod3678892020-05-21 10:06:58 +02005293 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005294 set->type = LYXP_SET_STRING;
5295 set->val.str = new;
5296
5297 return LY_SUCCESS;
5298}
5299
5300/**
5301 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5302 * with true value.
5303 *
5304 * @param[in] args Array of arguments.
5305 * @param[in] arg_count Count of elements in @p args.
5306 * @param[in,out] set Context and result set at the same time.
5307 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005308 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005309 */
5310static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005311xpath_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 +02005312{
5313 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005314 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005315 return LY_SUCCESS;
5316 }
5317
5318 set_fill_boolean(set, 1);
5319 return LY_SUCCESS;
5320}
5321
5322/*
5323 * moveto functions
5324 *
5325 * They and only they actually change the context (set).
5326 */
5327
5328/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005329 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005330 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005331 * XPath @p set is expected to be a (sc)node set!
5332 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005333 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5334 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005335 * @param[in] set Set with general XPath context.
5336 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005337 * @param[out] moveto_mod Expected module of a matching node.
5338 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005339 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005340static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005341moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5342 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005343{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005344 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005345 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005346 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005347
Michal Vasko2104e9f2020-03-06 08:23:25 +01005348 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5349
Michal Vasko6346ece2019-09-24 13:12:53 +02005350 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5351 /* specific module */
5352 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005353 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005354
Michal Vasko004d3152020-06-11 19:59:22 +02005355 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005356 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005357 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005358 return LY_EVALID;
5359 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005360
Michal Vasko6346ece2019-09-24 13:12:53 +02005361 *qname += pref_len + 1;
5362 *qname_len -= pref_len + 1;
5363 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5364 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005365 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005366 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005367 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005368 case LY_VALUE_SCHEMA:
5369 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005370 /* current module */
5371 mod = set->cur_mod;
5372 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005373 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005374 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005375 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005376 /* inherit parent (context node) module */
5377 if (ctx_scnode) {
5378 mod = ctx_scnode->module;
5379 } else {
5380 mod = NULL;
5381 }
5382 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005383 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005384 /* all nodes need to be prefixed */
5385 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5386 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005387 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005388 }
5389
Michal Vasko6346ece2019-09-24 13:12:53 +02005390 *moveto_mod = mod;
5391 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005392}
5393
5394/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005395 * @brief Move context @p set to the root. Handles absolute path.
5396 * Result is LYXP_SET_NODE_SET.
5397 *
5398 * @param[in,out] set Set to use.
5399 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005400 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005401 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005402static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005403moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005404{
aPiecek8b0cc152021-05-31 16:40:31 +02005405 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005406
5407 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005408 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskob0099a92020-08-31 14:55:23 +02005409 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005410 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005411 set->type = LYXP_SET_NODE_SET;
5412 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005413 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005414 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005415
5416 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005417}
5418
5419/**
5420 * @brief Check @p node as a part of NameTest processing.
5421 *
5422 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005423 * @param[in] ctx_node Context node.
5424 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005425 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005426 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005427 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005428 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5429 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005430 */
5431static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005432moveto_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 +01005433 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005434{
Michal Vaskodca9f122021-07-16 13:56:22 +02005435 if (!node->schema) {
5436 /* opaque node never matches */
5437 return LY_ENOT;
5438 }
5439
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005440 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5441 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005442 case LY_VALUE_SCHEMA:
5443 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005444 /* use current module */
5445 moveto_mod = set->cur_mod;
5446 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005447 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005448 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005449 /* inherit module of the context node, if any */
5450 if (ctx_node) {
5451 moveto_mod = ctx_node->schema->module;
5452 }
5453 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005454 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005455 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005456 /* not defined */
5457 LOGINT(set->ctx);
5458 return LY_EINVAL;
5459 }
5460 }
5461
Michal Vasko03ff5a72019-09-11 13:49:33 +02005462 /* module check */
5463 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005464 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005465 }
5466
Michal Vasko5c4e5892019-11-14 12:31:38 +01005467 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005468 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005469 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005470 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5471 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005472 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005473 }
5474
5475 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005476 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005477 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005478 }
5479
Michal Vaskoa1424542019-11-14 16:08:52 +01005480 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005481 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005482 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005483 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005484
5485 /* match */
5486 return LY_SUCCESS;
5487}
5488
5489/**
5490 * @brief Check @p node as a part of schema NameTest processing.
5491 *
5492 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005493 * @param[in] ctx_scnode Context node.
5494 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005495 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005496 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005497 * @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 +02005498 */
5499static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005500moveto_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 +02005501 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005502{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005503 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5504 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005505 case LY_VALUE_SCHEMA:
5506 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005507 /* use current module */
5508 moveto_mod = set->cur_mod;
5509 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005510 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005511 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005512 /* inherit module of the context node, if any */
5513 if (ctx_scnode) {
5514 moveto_mod = ctx_scnode->module;
5515 }
5516 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005517 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005518 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005519 /* not defined */
5520 LOGINT(set->ctx);
5521 return LY_EINVAL;
5522 }
5523 }
5524
Michal Vasko03ff5a72019-09-11 13:49:33 +02005525 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005526 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005527 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005528 }
5529
5530 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005531 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005532 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005533 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005534 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005535 }
5536
5537 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005538 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005539 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005540 }
5541
5542 /* match */
5543 return LY_SUCCESS;
5544}
5545
5546/**
Michal Vaskod3678892020-05-21 10:06:58 +02005547 * @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 +02005548 *
5549 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005550 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005551 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005552 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005553 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5554 */
5555static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005556moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005557{
Michal Vaskof03ed032020-03-04 13:31:44 +01005558 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005559 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005560 LY_ERR rc;
5561
aPiecek8b0cc152021-05-31 16:40:31 +02005562 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005563 return LY_SUCCESS;
5564 }
5565
5566 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005567 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005568 return LY_EVALID;
5569 }
5570
Michal Vasko03ff5a72019-09-11 13:49:33 +02005571 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005572 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005573
5574 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 +01005575 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005576
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005577 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005578 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005579 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005580 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005581 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005582 ctx_node = set->val.nodes[i].node;
5583 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005584 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005585
Michal Vaskod3678892020-05-21 10:06:58 +02005586 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005587 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005588 if (rc == LY_SUCCESS) {
5589 if (!replaced) {
5590 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5591 replaced = 1;
5592 } else {
5593 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005594 }
Michal Vaskod3678892020-05-21 10:06:58 +02005595 ++i;
5596 } else if (rc == LY_EINCOMPLETE) {
5597 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005598 }
5599 }
5600
5601 if (!replaced) {
5602 /* no match */
5603 set_remove_node(set, i);
5604 }
5605 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005606
5607 return LY_SUCCESS;
5608}
5609
5610/**
Michal Vaskod3678892020-05-21 10:06:58 +02005611 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5612 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005613 *
5614 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005615 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005616 * @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 +01005617 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005618 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5619 */
5620static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005621moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5622 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005623{
Michal Vasko004d3152020-06-11 19:59:22 +02005624 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005625 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005626 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005627 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005628
Michal Vasko004d3152020-06-11 19:59:22 +02005629 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005630
aPiecek8b0cc152021-05-31 16:40:31 +02005631 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02005632 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005633 }
5634
5635 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005636 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005637 ret = LY_EVALID;
5638 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005639 }
5640
5641 /* context check for all the nodes since we have the schema node */
5642 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5643 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005644 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005645 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5646 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005647 lyxp_set_free_content(set);
5648 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005649 }
5650
5651 /* create specific data instance if needed */
5652 if (scnode->nodetype == LYS_LIST) {
5653 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5654 } else if (scnode->nodetype == LYS_LEAFLIST) {
5655 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005656 }
5657
5658 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005659 siblings = NULL;
5660
5661 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5662 assert(!set->val.nodes[i].node);
5663
5664 /* search in all the trees */
5665 siblings = set->tree;
5666 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5667 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005668 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005669 }
5670
5671 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005672 if (inst) {
5673 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005674 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005675 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005676 }
5677
5678 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005679 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005680 ret = LY_EINCOMPLETE;
5681 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005682 }
5683
5684 if (sub) {
5685 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005686 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005687 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005688 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005689 /* no match */
5690 set_remove_node(set, i);
5691 }
5692 }
5693
Michal Vasko004d3152020-06-11 19:59:22 +02005694cleanup:
5695 lyd_free_tree(inst);
5696 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005697}
5698
5699/**
5700 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5701 *
5702 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005703 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005704 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005705 * @param[in] options XPath options.
5706 * @return LY_ERR
5707 */
5708static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005709moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005710{
Radek Krejci857189e2020-09-01 13:26:36 +02005711 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005712 uint32_t getnext_opts;
5713 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005714 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005715 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005716 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005717
aPiecek8b0cc152021-05-31 16:40:31 +02005718 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005719 return LY_SUCCESS;
5720 }
5721
5722 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005723 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005724 return LY_EVALID;
5725 }
5726
Michal Vaskocafad9d2019-11-07 15:20:03 +01005727 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005728 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005729 if (options & LYXP_SCNODE_OUTPUT) {
5730 getnext_opts |= LYS_GETNEXT_OUTPUT;
5731 }
5732
Michal Vasko03ff5a72019-09-11 13:49:33 +02005733 orig_used = set->used;
5734 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005735 uint32_t idx;
5736
Radek Krejcif13b87b2020-12-01 22:02:17 +01005737 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5738 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005739 continue;
5740 }
5741
5742 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005743 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005744 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005745 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005746 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005747
5748 start_parent = set->val.scnodes[i].scnode;
5749
5750 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 +02005751 /* 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 +02005752 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005753 mod_idx = 0;
Michal Vaskoa51ef072021-07-02 10:40:30 +02005754 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005755 iter = NULL;
Michal Vasko919954a2021-07-26 09:21:42 +02005756 /* module may not be implemented or not compiled yet */
5757 while (mod->compiled && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005758 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005759 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5760
Michal Vasko03ff5a72019-09-11 13:49:33 +02005761 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005762 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005763 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005764 temp_ctx = 1;
5765 }
5766 }
5767 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005768 }
5769
Michal Vasko519fd602020-05-26 12:17:39 +02005770 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5771 iter = NULL;
5772 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005773 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005774 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5775
5776 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005777 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005778 temp_ctx = 1;
5779 }
5780 }
5781 }
5782 }
5783 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005784
5785 /* correct temporary in_ctx values */
5786 if (temp_ctx) {
5787 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005788 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5789 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005790 }
5791 }
5792 }
5793
5794 return LY_SUCCESS;
5795}
5796
5797/**
Michal Vaskod3678892020-05-21 10:06:58 +02005798 * @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 +02005799 * Context position aware.
5800 *
5801 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005802 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005803 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005804 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005805 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5806 */
5807static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005808moveto_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 +02005809{
5810 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005811 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005812 struct lyxp_set ret_set;
5813 LY_ERR rc;
5814
aPiecek8b0cc152021-05-31 16:40:31 +02005815 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005816 return LY_SUCCESS;
5817 }
5818
5819 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005820 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005821 return LY_EVALID;
5822 }
5823
Michal Vasko9f96a052020-03-10 09:41:45 +01005824 /* 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 +01005825 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005826 LY_CHECK_RET(rc);
5827
Michal Vasko6346ece2019-09-24 13:12:53 +02005828 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005829 set_init(&ret_set, set);
5830 for (i = 0; i < set->used; ++i) {
5831
5832 /* TREE DFS */
5833 start = set->val.nodes[i].node;
5834 for (elem = next = start; elem; elem = next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005835 rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005836 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005837 /* add matching node into result set */
5838 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5839 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5840 /* the node is a duplicate, we'll process it later in the set */
5841 goto skip_children;
5842 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005843 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005844 return rc;
5845 } else if (rc == LY_EINVAL) {
5846 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005847 }
5848
5849 /* TREE DFS NEXT ELEM */
5850 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005851 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005852 if (!next) {
5853skip_children:
5854 /* no children, so try siblings, but only if it's not the start,
5855 * that is considered to be the root and it's siblings are not traversed */
5856 if (elem != start) {
5857 next = elem->next;
5858 } else {
5859 break;
5860 }
5861 }
5862 while (!next) {
5863 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01005864 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005865 /* we are done, no next element to process */
5866 break;
5867 }
5868 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01005869 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005870 next = elem->next;
5871 }
5872 }
5873 }
5874
5875 /* make the temporary set the current one */
5876 ret_set.ctx_pos = set->ctx_pos;
5877 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005878 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005879 memcpy(set, &ret_set, sizeof *set);
5880
5881 return LY_SUCCESS;
5882}
5883
5884/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005885 * @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 +02005886 *
5887 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005888 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005889 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005890 * @param[in] options XPath options.
5891 * @return LY_ERR
5892 */
5893static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005894moveto_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 +02005895{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005896 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005897 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005898 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005899
aPiecek8b0cc152021-05-31 16:40:31 +02005900 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005901 return LY_SUCCESS;
5902 }
5903
5904 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005905 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005906 return LY_EVALID;
5907 }
5908
Michal Vasko03ff5a72019-09-11 13:49:33 +02005909 orig_used = set->used;
5910 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005911 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5912 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005913 continue;
5914 }
5915
5916 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005917 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005918 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005919 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005920 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005921
5922 /* TREE DFS */
5923 start = set->val.scnodes[i].scnode;
5924 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005925 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5926 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005927 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005928 }
5929
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005930 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005931 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005932 uint32_t idx;
5933
5934 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005935 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005936 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005937 /* we will process it later in the set */
5938 goto skip_children;
5939 }
5940 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005941 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005942 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005943 } else if (rc == LY_EINVAL) {
5944 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005945 }
5946
5947next_iter:
5948 /* TREE DFS NEXT ELEM */
5949 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005950 next = lysc_node_child(elem);
5951 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5952 next = next->next;
5953 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5954 next = next->next;
5955 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005956 if (!next) {
5957skip_children:
5958 /* no children, so try siblings, but only if it's not the start,
5959 * that is considered to be the root and it's siblings are not traversed */
5960 if (elem != start) {
5961 next = elem->next;
5962 } else {
5963 break;
5964 }
5965 }
5966 while (!next) {
5967 /* no siblings, go back through the parents */
5968 if (elem->parent == start) {
5969 /* we are done, no next element to process */
5970 break;
5971 }
5972 /* parent is already processed, go to its sibling */
5973 elem = elem->parent;
5974 next = elem->next;
5975 }
5976 }
5977 }
5978
5979 return LY_SUCCESS;
5980}
5981
5982/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005983 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005984 * Indirectly context position aware.
5985 *
5986 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005987 * @param[in] mod Matching metadata module, NULL for any.
5988 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02005989 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005990 * @return LY_ERR
5991 */
5992static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02005993moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005994{
Michal Vasko9f96a052020-03-10 09:41:45 +01005995 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005996
aPiecek8b0cc152021-05-31 16:40:31 +02005997 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005998 return LY_SUCCESS;
5999 }
6000
6001 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006002 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006003 return LY_EVALID;
6004 }
6005
Radek Krejci1deb5be2020-08-26 16:43:36 +02006006 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006007 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006008
6009 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
6010 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01006011 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006012 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006013
6014 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006015 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006016 continue;
6017 }
6018
Michal Vaskod3678892020-05-21 10:06:58 +02006019 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006020 /* match */
6021 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006022 set->val.meta[i].meta = sub;
6023 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006024 /* pos does not change */
6025 replaced = 1;
6026 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006027 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 +02006028 }
6029 ++i;
6030 }
6031 }
6032 }
6033
6034 if (!replaced) {
6035 /* no match */
6036 set_remove_node(set, i);
6037 }
6038 }
6039
6040 return LY_SUCCESS;
6041}
6042
6043/**
6044 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02006045 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006046 *
6047 * @param[in,out] set1 Set to use for the result.
6048 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006049 * @return LY_ERR
6050 */
6051static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006052moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006053{
6054 LY_ERR rc;
6055
Michal Vaskod3678892020-05-21 10:06:58 +02006056 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006057 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006058 return LY_EVALID;
6059 }
6060
6061 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02006062 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006063 return LY_SUCCESS;
6064 }
6065
Michal Vaskod3678892020-05-21 10:06:58 +02006066 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006067 memcpy(set1, set2, sizeof *set1);
6068 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02006069 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006070 return LY_SUCCESS;
6071 }
6072
6073 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006074 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006075
6076 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006077 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006078 LY_CHECK_RET(rc);
6079
6080 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006081 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006082
6083 return LY_SUCCESS;
6084}
6085
6086/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006087 * @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 +02006088 * Context position aware.
6089 *
6090 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006091 * @param[in] mod Matching metadata module, NULL for any.
6092 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006093 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006094 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6095 */
6096static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006097moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006098{
Michal Vasko9f96a052020-03-10 09:41:45 +01006099 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006100 struct lyxp_set *set_all_desc = NULL;
6101 LY_ERR rc;
6102
aPiecek8b0cc152021-05-31 16:40:31 +02006103 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006104 return LY_SUCCESS;
6105 }
6106
6107 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006108 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006109 return LY_EVALID;
6110 }
6111
Michal Vasko03ff5a72019-09-11 13:49:33 +02006112 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6113 * but it likely won't be used much, so it's a waste of time */
6114 /* copy the context */
6115 set_all_desc = set_copy(set);
6116 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006117 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006118 if (rc != LY_SUCCESS) {
6119 lyxp_set_free(set_all_desc);
6120 return rc;
6121 }
6122 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006123 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006124 if (rc != LY_SUCCESS) {
6125 lyxp_set_free(set_all_desc);
6126 return rc;
6127 }
6128 lyxp_set_free(set_all_desc);
6129
Radek Krejci1deb5be2020-08-26 16:43:36 +02006130 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006131 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006132
6133 /* only attributes of an elem can be in the result, skip all the rest,
6134 * we have all attributes qualified in lyd tree */
6135 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006136 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006137 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006138 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006139 continue;
6140 }
6141
Michal Vaskod3678892020-05-21 10:06:58 +02006142 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006143 /* match */
6144 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006145 set->val.meta[i].meta = sub;
6146 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006147 /* pos does not change */
6148 replaced = 1;
6149 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006150 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 +02006151 }
6152 ++i;
6153 }
6154 }
6155 }
6156
6157 if (!replaced) {
6158 /* no match */
6159 set_remove_node(set, i);
6160 }
6161 }
6162
6163 return LY_SUCCESS;
6164}
6165
6166/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006167 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6168 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006169 *
6170 * @param[in] parent Current parent.
6171 * @param[in] parent_pos Position of @p parent.
6172 * @param[in] parent_type Node type of @p parent.
6173 * @param[in,out] to_set Set to use.
6174 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006175 * @param[in] options XPath options.
6176 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6177 */
6178static LY_ERR
6179moveto_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 +02006180 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006181{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006182 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006183 LY_ERR rc;
6184
6185 switch (parent_type) {
6186 case LYXP_NODE_ROOT:
6187 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006188 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006189
Michal Vasko61ac2f62020-05-25 12:39:51 +02006190 /* add all top-level nodes as elements */
6191 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006192 break;
6193 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006194 /* add just the text node of this term element node */
6195 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006196 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6197 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6198 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006199 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006200 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006201
6202 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006203 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006204 break;
6205 default:
6206 LOGINT_RET(parent->schema->module->ctx);
6207 }
6208
Michal Vasko61ac2f62020-05-25 12:39:51 +02006209 /* add all top-level nodes as elements */
6210 LY_LIST_FOR(first, iter) {
6211 /* context check */
6212 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6213 continue;
6214 }
6215
6216 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006217 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006218 return LY_EINCOMPLETE;
6219 }
6220
6221 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6222 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6223
6224 /* also add all the children of this node, recursively */
6225 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6226 LY_CHECK_RET(rc);
6227 }
6228 }
6229
Michal Vasko03ff5a72019-09-11 13:49:33 +02006230 return LY_SUCCESS;
6231}
6232
6233/**
6234 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6235 * (or LYXP_SET_EMPTY). Context position aware.
6236 *
6237 * @param[in,out] set Set to use.
6238 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6239 * @param[in] options XPath options.
6240 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6241 */
6242static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006243moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006244{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006245 struct lyxp_set ret_set;
6246 LY_ERR rc;
6247
aPiecek8b0cc152021-05-31 16:40:31 +02006248 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006249 return LY_SUCCESS;
6250 }
6251
6252 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006253 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006254 return LY_EVALID;
6255 }
6256
6257 /* nothing to do */
6258 if (!all_desc) {
6259 return LY_SUCCESS;
6260 }
6261
Michal Vasko03ff5a72019-09-11 13:49:33 +02006262 /* add all the children, they get added recursively */
6263 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006264 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006265 /* copy the current node to tmp */
6266 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6267
6268 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006269 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006270 continue;
6271 }
6272
Michal Vasko03ff5a72019-09-11 13:49:33 +02006273 /* add all the children */
6274 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 +02006275 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006276 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006277 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006278 return rc;
6279 }
6280 }
6281
6282 /* use the temporary set as the current one */
6283 ret_set.ctx_pos = set->ctx_pos;
6284 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006285 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006286 memcpy(set, &ret_set, sizeof *set);
6287
6288 return LY_SUCCESS;
6289}
6290
6291/**
6292 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6293 * (or LYXP_SET_EMPTY).
6294 *
6295 * @param[in,out] set Set to use.
6296 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6297 * @param[in] options XPath options.
6298 * @return LY_ERR
6299 */
6300static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006301moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006302{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006303 uint32_t getnext_opts;
6304 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006305 const struct lysc_node *iter, *start_parent;
6306 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006307
aPiecek8b0cc152021-05-31 16:40:31 +02006308 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006309 return LY_SUCCESS;
6310 }
6311
6312 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006313 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006314 return LY_EVALID;
6315 }
6316
Michal Vasko519fd602020-05-26 12:17:39 +02006317 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006318 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006319 if (options & LYXP_SCNODE_OUTPUT) {
6320 getnext_opts |= LYS_GETNEXT_OUTPUT;
6321 }
6322
6323 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006324 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006325 if (!all_desc) {
6326 /* traverse the start node */
6327 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6328 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6329 }
6330 continue;
6331 }
6332
Radek Krejcif13b87b2020-12-01 22:02:17 +01006333 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6334 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006335 continue;
6336 }
6337
Michal Vasko519fd602020-05-26 12:17:39 +02006338 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006339 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006340 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006341 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006342 }
6343
Michal Vasko519fd602020-05-26 12:17:39 +02006344 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006345
Michal Vasko519fd602020-05-26 12:17:39 +02006346 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6347 /* it can actually be in any module, it's all <running> */
6348 mod_idx = 0;
Michal Vaskoa51ef072021-07-02 10:40:30 +02006349 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02006350 iter = NULL;
6351 /* module may not be implemented */
6352 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6353 /* context check */
6354 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6355 continue;
6356 }
6357
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006358 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006359 /* throw away the insert index, we want to consider that node again, recursively */
6360 }
6361 }
6362
6363 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6364 iter = NULL;
6365 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006366 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006367 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006368 continue;
6369 }
6370
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006371 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006372 }
6373 }
6374 }
6375
6376 return LY_SUCCESS;
6377}
6378
6379/**
6380 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6381 * (or LYXP_SET_EMPTY). Context position aware.
6382 *
6383 * @param[in] set Set to use.
6384 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6385 * @param[in] options XPath options.
6386 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6387 */
6388static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006389moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006390{
6391 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006392 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006393 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006394
aPiecek8b0cc152021-05-31 16:40:31 +02006395 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006396 return LY_SUCCESS;
6397 }
6398
6399 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006400 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006401 return LY_EVALID;
6402 }
6403
6404 if (all_desc) {
6405 /* <path>//.. == <path>//./.. */
6406 rc = moveto_self(set, 1, options);
6407 LY_CHECK_RET(rc);
6408 }
6409
Radek Krejci1deb5be2020-08-26 16:43:36 +02006410 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006411 node = set->val.nodes[i].node;
6412
6413 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9e685082021-01-29 14:49:09 +01006414 new_node = lyd_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006415 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6416 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006417 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6418 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006419 if (!new_node) {
6420 LOGINT_RET(set->ctx);
6421 }
6422 } else {
6423 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006424 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006425 continue;
6426 }
6427
Michal Vaskoa1424542019-11-14 16:08:52 +01006428 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006429 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 +02006430 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006431 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006432
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006433 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006434 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006435 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006436
Michal Vasko03ff5a72019-09-11 13:49:33 +02006437 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006438 /* 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 +02006439 new_type = LYXP_NODE_ELEM;
6440 }
6441
Michal Vasko03ff5a72019-09-11 13:49:33 +02006442 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006443 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006444 } else {
6445 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006446 }
6447 }
6448
Michal Vasko2caefc12019-11-14 16:07:56 +01006449 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006450 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006451
6452 return LY_SUCCESS;
6453}
6454
6455/**
6456 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6457 * (or LYXP_SET_EMPTY).
6458 *
6459 * @param[in] set Set to use.
6460 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6461 * @param[in] options XPath options.
6462 * @return LY_ERR
6463 */
6464static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006465moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006466{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006467 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006468 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006469 const struct lysc_node *node, *new_node;
6470 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006471
aPiecek8b0cc152021-05-31 16:40:31 +02006472 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006473 return LY_SUCCESS;
6474 }
6475
6476 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006477 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006478 return LY_EVALID;
6479 }
6480
6481 if (all_desc) {
6482 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006483 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006484 }
6485
Michal Vasko03ff5a72019-09-11 13:49:33 +02006486 orig_used = set->used;
6487 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006488 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6489 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006490 continue;
6491 }
6492
6493 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006494 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006495 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006496 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006497 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006498
6499 node = set->val.scnodes[i].scnode;
6500
6501 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006502 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006503 } else {
6504 /* root does not have a parent */
6505 continue;
6506 }
6507
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006508 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006509 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006510 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006511
Michal Vasko03ff5a72019-09-11 13:49:33 +02006512 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006513 /* 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 +02006514 new_type = LYXP_NODE_ELEM;
6515 }
6516
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006517 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6518 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006519 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006520 temp_ctx = 1;
6521 }
6522 }
6523
6524 if (temp_ctx) {
6525 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006526 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6527 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006528 }
6529 }
6530 }
6531
6532 return LY_SUCCESS;
6533}
6534
6535/**
6536 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6537 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6538 *
6539 * @param[in,out] set1 Set to use for the result.
6540 * @param[in] set2 Set acting as the second operand for @p op.
6541 * @param[in] op Comparison operator to process.
6542 * @param[in] options XPath options.
6543 * @return LY_ERR
6544 */
6545static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006546moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006547{
6548 /*
6549 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6550 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6551 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6552 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6553 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6554 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6555 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6556 *
6557 * '=' or '!='
6558 * BOOLEAN + BOOLEAN
6559 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6560 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6561 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6562 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6563 * NUMBER + NUMBER
6564 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6565 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6566 * STRING + STRING
6567 *
6568 * '<=', '<', '>=', '>'
6569 * NUMBER + NUMBER
6570 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6571 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6572 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6573 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6574 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6575 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6576 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6577 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6578 */
6579 struct lyxp_set iter1, iter2;
6580 int result;
6581 int64_t i;
6582 LY_ERR rc;
6583
Michal Vasko004d3152020-06-11 19:59:22 +02006584 memset(&iter1, 0, sizeof iter1);
6585 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006586
6587 /* iterative evaluation with node-sets */
6588 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6589 if (set1->type == LYXP_SET_NODE_SET) {
6590 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006591 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006592 switch (set2->type) {
6593 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006594 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006595 break;
6596 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006597 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006598 break;
6599 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006600 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006601 break;
6602 }
6603 LY_CHECK_RET(rc);
6604
Michal Vasko4c7763f2020-07-27 17:40:37 +02006605 /* canonize set2 */
6606 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6607
6608 /* compare recursively */
6609 rc = moveto_op_comp(&iter1, &iter2, op, options);
6610 lyxp_set_free_content(&iter2);
6611 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006612
6613 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006614 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006615 set_fill_boolean(set1, 1);
6616 return LY_SUCCESS;
6617 }
6618 }
6619 } else {
6620 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006621 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006622 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006623 case LYXP_SET_NUMBER:
6624 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6625 break;
6626 case LYXP_SET_BOOLEAN:
6627 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6628 break;
6629 default:
6630 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6631 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006632 }
6633 LY_CHECK_RET(rc);
6634
Michal Vasko4c7763f2020-07-27 17:40:37 +02006635 /* canonize set1 */
6636 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 +02006637
Michal Vasko4c7763f2020-07-27 17:40:37 +02006638 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006639 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006640 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006641 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006642
6643 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006644 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006645 set_fill_boolean(set1, 1);
6646 return LY_SUCCESS;
6647 }
6648 }
6649 }
6650
6651 /* false for all nodes */
6652 set_fill_boolean(set1, 0);
6653 return LY_SUCCESS;
6654 }
6655
6656 /* first convert properly */
6657 if ((op[0] == '=') || (op[0] == '!')) {
6658 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006659 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6660 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006661 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
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 } /* else we have 2 strings */
6667 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006668 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006669 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006670 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006671 LY_CHECK_RET(rc);
6672 }
6673
6674 assert(set1->type == set2->type);
6675
6676 /* compute result */
6677 if (op[0] == '=') {
6678 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006679 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006680 } else if (set1->type == LYXP_SET_NUMBER) {
6681 result = (set1->val.num == set2->val.num);
6682 } else {
6683 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006684 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006685 }
6686 } else if (op[0] == '!') {
6687 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006688 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006689 } else if (set1->type == LYXP_SET_NUMBER) {
6690 result = (set1->val.num != set2->val.num);
6691 } else {
6692 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006693 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006694 }
6695 } else {
6696 assert(set1->type == LYXP_SET_NUMBER);
6697 if (op[0] == '<') {
6698 if (op[1] == '=') {
6699 result = (set1->val.num <= set2->val.num);
6700 } else {
6701 result = (set1->val.num < set2->val.num);
6702 }
6703 } else {
6704 if (op[1] == '=') {
6705 result = (set1->val.num >= set2->val.num);
6706 } else {
6707 result = (set1->val.num > set2->val.num);
6708 }
6709 }
6710 }
6711
6712 /* assign result */
6713 if (result) {
6714 set_fill_boolean(set1, 1);
6715 } else {
6716 set_fill_boolean(set1, 0);
6717 }
6718
6719 return LY_SUCCESS;
6720}
6721
6722/**
6723 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6724 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6725 *
6726 * @param[in,out] set1 Set to use for the result.
6727 * @param[in] set2 Set acting as the second operand for @p op.
6728 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006729 * @return LY_ERR
6730 */
6731static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006732moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006733{
6734 LY_ERR rc;
6735
6736 /* unary '-' */
6737 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006738 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006739 LY_CHECK_RET(rc);
6740 set1->val.num *= -1;
6741 lyxp_set_free(set2);
6742 return LY_SUCCESS;
6743 }
6744
6745 assert(set1 && set2);
6746
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006747 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006748 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006749 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006750 LY_CHECK_RET(rc);
6751
6752 switch (op[0]) {
6753 /* '+' */
6754 case '+':
6755 set1->val.num += set2->val.num;
6756 break;
6757
6758 /* '-' */
6759 case '-':
6760 set1->val.num -= set2->val.num;
6761 break;
6762
6763 /* '*' */
6764 case '*':
6765 set1->val.num *= set2->val.num;
6766 break;
6767
6768 /* 'div' */
6769 case 'd':
6770 set1->val.num /= set2->val.num;
6771 break;
6772
6773 /* 'mod' */
6774 case 'm':
6775 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6776 break;
6777
6778 default:
6779 LOGINT_RET(set1->ctx);
6780 }
6781
6782 return LY_SUCCESS;
6783}
6784
6785/*
6786 * eval functions
6787 *
6788 * They execute a parsed XPath expression on some data subtree.
6789 */
6790
6791/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006792 * @brief Evaluate Predicate. Logs directly on error.
6793 *
Michal Vaskod3678892020-05-21 10:06:58 +02006794 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006795 *
6796 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006797 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02006798 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006799 * @param[in] options XPath options.
6800 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6801 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6802 */
6803static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006804eval_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 +02006805{
6806 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006807 uint16_t orig_exp;
6808 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006809 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006810 struct lyxp_set set2;
6811 struct lyd_node *orig_parent;
6812
6813 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02006814 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006815 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006816 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006817
aPiecek8b0cc152021-05-31 16:40:31 +02006818 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006819only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02006820 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006821 LY_CHECK_RET(rc);
6822 } else if (set->type == LYXP_SET_NODE_SET) {
6823 /* 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 +01006824 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006825
6826 /* empty set, nothing to evaluate */
6827 if (!set->used) {
6828 goto only_parse;
6829 }
6830
Michal Vasko004d3152020-06-11 19:59:22 +02006831 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006832 orig_pos = 0;
6833 orig_size = set->used;
6834 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006835 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006836 set_init(&set2, set);
6837 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6838 /* remember the node context position for position() and context size for last(),
6839 * predicates should always be evaluated with respect to the child axis (since we do
6840 * not support explicit axes) so we assign positions based on their parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006841 if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) {
6842 orig_parent = lyd_parent(set->val.nodes[i].node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006843 orig_pos = 1;
6844 } else {
6845 ++orig_pos;
6846 }
6847
6848 set2.ctx_pos = orig_pos;
6849 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006850 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006851
Michal Vasko004d3152020-06-11 19:59:22 +02006852 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006853 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006854 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006855 return rc;
6856 }
6857
6858 /* number is a position */
6859 if (set2.type == LYXP_SET_NUMBER) {
6860 if ((long long)set2.val.num == orig_pos) {
6861 set2.val.num = 1;
6862 } else {
6863 set2.val.num = 0;
6864 }
6865 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006866 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006867
6868 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006869 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006870 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006871 }
6872 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006873 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006874
6875 } else if (set->type == LYXP_SET_SCNODE_SET) {
6876 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006877 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006878 /* there is a currently-valid node */
6879 break;
6880 }
6881 }
6882 /* empty set, nothing to evaluate */
6883 if (i == set->used) {
6884 goto only_parse;
6885 }
6886
Michal Vasko004d3152020-06-11 19:59:22 +02006887 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006888
Michal Vasko03ff5a72019-09-11 13:49:33 +02006889 /* set special in_ctx to all the valid snodes */
6890 pred_in_ctx = set_scnode_new_in_ctx(set);
6891
6892 /* use the valid snodes one-by-one */
6893 for (i = 0; i < set->used; ++i) {
6894 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6895 continue;
6896 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006897 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006898
Michal Vasko004d3152020-06-11 19:59:22 +02006899 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006900
Michal Vasko004d3152020-06-11 19:59:22 +02006901 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006902 LY_CHECK_RET(rc);
6903
6904 set->val.scnodes[i].in_ctx = pred_in_ctx;
6905 }
6906
6907 /* restore the state as it was before the predicate */
6908 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006909 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02006910 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006911 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006912 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006913 }
6914 }
6915
6916 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006917 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006918 set_fill_set(&set2, set);
6919
Michal Vasko004d3152020-06-11 19:59:22 +02006920 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006921 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006922 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006923 return rc;
6924 }
6925
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006926 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006927 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006928 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006929 }
Michal Vaskod3678892020-05-21 10:06:58 +02006930 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006931 }
6932
6933 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006934 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02006935 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006936 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006937 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006938
6939 return LY_SUCCESS;
6940}
6941
6942/**
Michal Vaskod3678892020-05-21 10:06:58 +02006943 * @brief Evaluate Literal. Logs directly on error.
6944 *
6945 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006946 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006947 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6948 */
6949static void
Michal Vasko40308e72020-10-20 16:38:40 +02006950eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006951{
6952 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006953 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006954 set_fill_string(set, "", 0);
6955 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006956 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 +02006957 }
6958 }
6959 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006960 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006961 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006962}
6963
6964/**
Michal Vasko004d3152020-06-11 19:59:22 +02006965 * @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 +02006966 *
Michal Vasko004d3152020-06-11 19:59:22 +02006967 * @param[in] exp Full parsed XPath expression.
6968 * @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 +02006969 * @param[in] ctx_node Found schema node as the context for the predicate.
6970 * @param[in] cur_mod Current module for the expression.
6971 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006972 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006973 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006974 * @param[out] predicates Parsed predicates.
6975 * @param[out] pred_type Type of @p predicates.
6976 * @return LY_SUCCESS on success,
6977 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006978 */
6979static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006980eval_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 +02006981 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 +02006982 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006983{
Michal Vasko004d3152020-06-11 19:59:22 +02006984 LY_ERR ret = LY_SUCCESS;
6985 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006986 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006987 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006988 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006989 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006990
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006991 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006992
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006993 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006994 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006995 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006996 return LY_EINVAL;
6997 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006998 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 +02006999 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02007000
Michal Vasko004d3152020-06-11 19:59:22 +02007001 /* learn where the predicates end */
7002 e_idx = *tok_idx;
7003 while (key_count) {
7004 /* '[' */
7005 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7006 return LY_EINVAL;
7007 }
7008 ++e_idx;
7009
Michal Vasko3354d272021-04-06 09:40:06 +02007010 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
7011 /* definitely not a key */
7012 return LY_EINVAL;
7013 }
7014
Michal Vasko004d3152020-06-11 19:59:22 +02007015 /* ']' */
7016 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7017 ++e_idx;
7018 }
7019 ++e_idx;
7020
7021 /* another presumably key predicate parsed */
7022 --key_count;
7023 }
Michal Vasko004d3152020-06-11 19:59:22 +02007024 } else {
7025 /* learn just where this single predicate ends */
7026 e_idx = *tok_idx;
7027
Michal Vaskod3678892020-05-21 10:06:58 +02007028 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007029 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7030 return LY_EINVAL;
7031 }
7032 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007033
Michal Vasko3354d272021-04-06 09:40:06 +02007034 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
7035 /* definitely not the value */
7036 return LY_EINVAL;
7037 }
7038
Michal Vaskod3678892020-05-21 10:06:58 +02007039 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007040 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7041 ++e_idx;
7042 }
7043 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007044 }
7045
Michal Vasko004d3152020-06-11 19:59:22 +02007046 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
7047 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
7048
7049 /* turn logging off */
7050 prev_lo = ly_log_options(0);
7051
7052 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007053 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
7054 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007055
7056 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007057 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
7058 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02007059 LY_CHECK_GOTO(ret, cleanup);
7060
7061 /* success, the predicate must include all the needed information for hash-based search */
7062 *tok_idx = e_idx;
7063
7064cleanup:
7065 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007066 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02007067 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02007068}
7069
7070/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007071 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7072 * data node(s) could be found using a single hash-based search.
7073 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007074 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007075 * @param[in] node Next context node to check.
7076 * @param[in] name Expected node name.
7077 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007078 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007079 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007080 * @param[in] format Prefix format.
7081 * @param[in,out] found Previously found node, is updated.
7082 * @return LY_SUCCESS on success,
7083 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7084 */
7085static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007086eval_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 +02007087 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 +02007088 const struct lysc_node **found)
7089{
7090 const struct lysc_node *scnode;
7091 const struct lys_module *mod;
7092 uint32_t idx = 0;
7093
Radek Krejci8df109d2021-04-23 12:19:08 +02007094 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007095
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007096continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007097 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007098 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007099 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007100 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007101 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007102 if (!mod->implemented) {
7103 continue;
7104 }
7105
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007106 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7107 if (scnode) {
7108 /* we have found a match */
7109 break;
7110 }
7111 }
7112
Michal Vasko7d1d0912020-10-16 08:38:30 +02007113 if (!scnode) {
7114 /* all modules searched */
7115 idx = 0;
7116 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007117 } else {
7118 /* search in top-level */
7119 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7120 }
7121 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007122 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007123 /* we must adjust the module to inherit the one from the context node */
7124 moveto_mod = node->schema->module;
7125 }
7126
7127 /* search in children, do not repeat the same search */
7128 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007129 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007130
7131 /* additional context check */
7132 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7133 scnode = NULL;
7134 }
7135
7136 if (scnode) {
7137 if (*found) {
7138 /* we found a schema node with the same name but at different level, give up, too complicated
7139 * (more hash-based searches would be required, not supported) */
7140 return LY_ENOT;
7141 } else {
7142 /* remember the found schema node and continue to make sure it can be used */
7143 *found = scnode;
7144 }
7145 }
7146
7147 if (idx) {
7148 /* continue searching all the following models */
7149 goto continue_search;
7150 }
7151
7152 return LY_SUCCESS;
7153}
7154
7155/**
Michal Vaskod3678892020-05-21 10:06:58 +02007156 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7157 *
7158 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7159 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7160 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7161 *
7162 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007163 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007164 * @param[in] attr_axis Whether to search attributes or standard nodes.
7165 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007166 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007167 * @param[in] options XPath options.
7168 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7169 */
7170static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007171eval_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 +02007172 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007173{
Michal Vaskod3678892020-05-21 10:06:58 +02007174 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007175 const char *ncname, *ncname_dict = NULL;
7176 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007177 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007178 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007179 struct ly_path_predicate *predicates = NULL;
7180 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007181 LY_ERR rc = LY_SUCCESS;
7182
aPiecek8b0cc152021-05-31 16:40:31 +02007183 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007184 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007185 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007186
aPiecek8b0cc152021-05-31 16:40:31 +02007187 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02007188 goto moveto;
7189 }
7190
Michal Vasko004d3152020-06-11 19:59:22 +02007191 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7192 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007193
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007194 if ((ncname[0] == '*') && (ncname_len == 1)) {
7195 /* all nodes will match */
7196 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007197 }
7198
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007199 /* parse (and skip) module name */
7200 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007201 LY_CHECK_GOTO(rc, cleanup);
7202
Radek Krejci8df109d2021-04-23 12:19:08 +02007203 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 +02007204 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007205 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007206 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7207 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007208 /* check failed */
7209 scnode = NULL;
7210 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007211 }
7212 }
7213
Michal Vasko004d3152020-06-11 19:59:22 +02007214 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7215 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007216 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7217 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007218 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007219 scnode = NULL;
7220 }
7221 }
7222 }
7223
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007224 if (!scnode) {
7225 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007226 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007227 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007228 }
7229
7230moveto:
7231 /* move to the attribute(s), data node(s), or schema node(s) */
7232 if (attr_axis) {
aPiecek8b0cc152021-05-31 16:40:31 +02007233 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007234 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007235 } else {
7236 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007237 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007238 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007239 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007240 }
7241 LY_CHECK_GOTO(rc, cleanup);
7242 }
7243 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007244 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007245 int64_t i;
7246
Michal Vaskod3678892020-05-21 10:06:58 +02007247 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007248 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007249 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007250 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007251 }
7252 LY_CHECK_GOTO(rc, cleanup);
7253
7254 for (i = set->used - 1; i > -1; --i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007255 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vaskod3678892020-05-21 10:06:58 +02007256 break;
7257 }
7258 }
7259 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007260 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vasko90f12dc2020-12-03 14:20:42 +01007261 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".",
7262 ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007263 free(path);
7264 }
7265 } else {
7266 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007267 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007268 } else {
7269 if (scnode) {
7270 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007271 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007272 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007273 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007274 }
7275 }
7276 LY_CHECK_GOTO(rc, cleanup);
7277 }
7278 }
7279
7280 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007281 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7282 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007283 LY_CHECK_RET(rc);
7284 }
7285
7286cleanup:
aPiecek8b0cc152021-05-31 16:40:31 +02007287 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007288 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007289 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007290 }
Michal Vaskod3678892020-05-21 10:06:58 +02007291 return rc;
7292}
7293
7294/**
7295 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7296 *
7297 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7298 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7299 * [8] NodeType ::= 'text' | 'node'
7300 *
7301 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007302 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007303 * @param[in] attr_axis Whether to search attributes or standard nodes.
7304 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007305 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007306 * @param[in] options XPath options.
7307 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7308 */
7309static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007310eval_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 +02007311 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007312{
7313 LY_ERR rc;
7314
7315 /* TODO */
7316 (void)attr_axis;
7317 (void)all_desc;
7318
aPiecek8b0cc152021-05-31 16:40:31 +02007319 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007320 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007321 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007322 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
aPiecek8b0cc152021-05-31 16:40:31 +02007323 options |= LYXP_SKIP_EXPR;
Michal Vaskod3678892020-05-21 10:06:58 +02007324 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007325 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007326 rc = xpath_node(NULL, 0, set, options);
7327 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007328 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007329 rc = xpath_text(NULL, 0, set, options);
7330 }
7331 LY_CHECK_RET(rc);
7332 }
7333 }
aPiecek8b0cc152021-05-31 16:40:31 +02007334 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007335 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007336 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007337
7338 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007339 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007340 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007341 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007342 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007343
7344 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007345 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007346 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007347 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007348 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007349
7350 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007351 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7352 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007353 LY_CHECK_RET(rc);
7354 }
7355
7356 return LY_SUCCESS;
7357}
7358
7359/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007360 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7361 *
7362 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7363 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007364 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007365 *
7366 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007367 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007368 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007369 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007370 * @param[in] options XPath options.
7371 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7372 */
7373static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007374eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7375 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007376{
Radek Krejci857189e2020-09-01 13:26:36 +02007377 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007378 LY_ERR rc;
7379
7380 goto step;
7381 do {
7382 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007383 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007384 all_desc = 0;
7385 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007386 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007387 all_desc = 1;
7388 }
aPiecek8b0cc152021-05-31 16:40:31 +02007389 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007390 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007391 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007392
7393step:
Michal Vaskod3678892020-05-21 10:06:58 +02007394 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007395 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007396 attr_axis = 1;
7397
aPiecek8b0cc152021-05-31 16:40:31 +02007398 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007399 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007400 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007401 } else {
7402 attr_axis = 0;
7403 }
7404
Michal Vasko03ff5a72019-09-11 13:49:33 +02007405 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007406 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007407 case LYXP_TOKEN_DOT:
7408 /* evaluate '.' */
aPiecek8b0cc152021-05-31 16:40:31 +02007409 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007410 rc = moveto_scnode_self(set, all_desc, options);
7411 } else {
7412 rc = moveto_self(set, all_desc, options);
7413 }
7414 LY_CHECK_RET(rc);
7415 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007416 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007417 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007418 break;
7419
7420 case LYXP_TOKEN_DDOT:
7421 /* evaluate '..' */
aPiecek8b0cc152021-05-31 16:40:31 +02007422 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007423 rc = moveto_scnode_parent(set, all_desc, options);
7424 } else {
7425 rc = moveto_parent(set, all_desc, options);
7426 }
7427 LY_CHECK_RET(rc);
aPiecek8b0cc152021-05-31 16:40:31 +02007428 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007429 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007430 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007431 break;
7432
Michal Vasko03ff5a72019-09-11 13:49:33 +02007433 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007434 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007435 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007436 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007437 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007438
Michal Vaskod3678892020-05-21 10:06:58 +02007439 case LYXP_TOKEN_NODETYPE:
7440 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007441 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007442 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007443 break;
7444
7445 default:
aPiecek8b0cc152021-05-31 16:40:31 +02007446 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007447 }
Michal Vasko004d3152020-06-11 19:59:22 +02007448 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007449
7450 return LY_SUCCESS;
7451}
7452
7453/**
7454 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7455 *
7456 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7457 *
7458 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007459 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007460 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007461 * @param[in] options XPath options.
7462 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7463 */
7464static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007465eval_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 +02007466{
Radek Krejci857189e2020-09-01 13:26:36 +02007467 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007468
aPiecek8b0cc152021-05-31 16:40:31 +02007469 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007470 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007471 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007472 }
7473
7474 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007475 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007476 /* evaluate '/' - deferred */
7477 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02007478 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007479 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007480 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007481
Michal Vasko004d3152020-06-11 19:59:22 +02007482 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007483 return LY_SUCCESS;
7484 }
Michal Vasko004d3152020-06-11 19:59:22 +02007485 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007486 case LYXP_TOKEN_DOT:
7487 case LYXP_TOKEN_DDOT:
7488 case LYXP_TOKEN_AT:
7489 case LYXP_TOKEN_NAMETEST:
7490 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007491 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007492 break;
7493 default:
7494 break;
7495 }
7496
Michal Vasko03ff5a72019-09-11 13:49:33 +02007497 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007498 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007499 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7500 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02007501 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007502 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007503 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007504
Michal Vaskob0099a92020-08-31 14:55:23 +02007505 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007506 }
7507
7508 return LY_SUCCESS;
7509}
7510
7511/**
7512 * @brief Evaluate FunctionCall. Logs directly on error.
7513 *
Michal Vaskod3678892020-05-21 10:06:58 +02007514 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007515 *
7516 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007517 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007518 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007519 * @param[in] options XPath options.
7520 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7521 */
7522static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007523eval_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 +02007524{
7525 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007526
Radek Krejci1deb5be2020-08-26 16:43:36 +02007527 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007528 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007529 struct lyxp_set **args = NULL, **args_aux;
7530
aPiecek8b0cc152021-05-31 16:40:31 +02007531 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007532 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007533 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007534 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007535 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007536 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007537 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007538 xpath_func = &xpath_sum;
7539 }
7540 break;
7541 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007542 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007543 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007544 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007546 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007547 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007548 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007549 xpath_func = &xpath_true;
7550 }
7551 break;
7552 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007553 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007554 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007555 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007556 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007557 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007558 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007559 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007560 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007561 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007562 xpath_func = &xpath_deref;
7563 }
7564 break;
7565 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007566 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007567 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007568 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007569 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007570 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007571 xpath_func = &xpath_string;
7572 }
7573 break;
7574 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007575 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007576 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007577 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007578 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007579 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007580 xpath_func = &xpath_current;
7581 }
7582 break;
7583 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007584 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007585 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007586 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007587 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007588 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007589 xpath_func = &xpath_re_match;
7590 }
7591 break;
7592 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007593 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007594 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007595 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007596 xpath_func = &xpath_translate;
7597 }
7598 break;
7599 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007600 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007601 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007602 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007603 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007604 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007605 xpath_func = &xpath_bit_is_set;
7606 }
7607 break;
7608 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007609 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007610 xpath_func = &xpath_starts_with;
7611 }
7612 break;
7613 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007614 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007615 xpath_func = &xpath_derived_from;
7616 }
7617 break;
7618 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007619 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007620 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007621 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007622 xpath_func = &xpath_string_length;
7623 }
7624 break;
7625 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007626 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007627 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007628 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007629 xpath_func = &xpath_substring_after;
7630 }
7631 break;
7632 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007633 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007634 xpath_func = &xpath_substring_before;
7635 }
7636 break;
7637 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007638 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007639 xpath_func = &xpath_derived_from_or_self;
7640 }
7641 break;
7642 }
7643
7644 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007645 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 +02007646 return LY_EVALID;
7647 }
7648 }
7649
aPiecek8b0cc152021-05-31 16:40:31 +02007650 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007651 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007652 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007653
7654 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007655 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007656 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007657 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007658 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007659
7660 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007661 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02007662 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007663 args = malloc(sizeof *args);
7664 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7665 arg_count = 1;
7666 args[0] = set_copy(set);
7667 if (!args[0]) {
7668 rc = LY_EMEM;
7669 goto cleanup;
7670 }
7671
Michal Vasko004d3152020-06-11 19:59:22 +02007672 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007673 LY_CHECK_GOTO(rc, cleanup);
7674 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007675 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007676 LY_CHECK_GOTO(rc, cleanup);
7677 }
7678 }
Michal Vasko004d3152020-06-11 19:59:22 +02007679 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02007680 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007681 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007682 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007683
aPiecek8b0cc152021-05-31 16:40:31 +02007684 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007685 ++arg_count;
7686 args_aux = realloc(args, arg_count * sizeof *args);
7687 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7688 args = args_aux;
7689 args[arg_count - 1] = set_copy(set);
7690 if (!args[arg_count - 1]) {
7691 rc = LY_EMEM;
7692 goto cleanup;
7693 }
7694
Michal Vasko004d3152020-06-11 19:59:22 +02007695 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007696 LY_CHECK_GOTO(rc, cleanup);
7697 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007698 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007699 LY_CHECK_GOTO(rc, cleanup);
7700 }
7701 }
7702
7703 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007704 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007705 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007706 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007707 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007708
aPiecek8b0cc152021-05-31 16:40:31 +02007709 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007710 /* evaluate function */
7711 rc = xpath_func(args, arg_count, set, options);
7712
7713 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007714 /* merge all nodes from arg evaluations */
7715 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007716 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007717 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007718 }
7719 }
7720 } else {
7721 rc = LY_SUCCESS;
7722 }
7723
7724cleanup:
7725 for (i = 0; i < arg_count; ++i) {
7726 lyxp_set_free(args[i]);
7727 }
7728 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007729 return rc;
7730}
7731
7732/**
7733 * @brief Evaluate Number. Logs directly on error.
7734 *
7735 * @param[in] ctx Context for errors.
7736 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007737 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007738 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7739 * @return LY_ERR
7740 */
7741static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007742eval_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 +02007743{
7744 long double num;
7745 char *endptr;
7746
7747 if (set) {
7748 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007749 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007750 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007751 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7752 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007753 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007754 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007755 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007756 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7757 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007758 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007759 return LY_EVALID;
7760 }
7761
7762 set_fill_number(set, num);
7763 }
7764
7765 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007766 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007767 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007768 return LY_SUCCESS;
7769}
7770
7771/**
7772 * @brief Evaluate PathExpr. Logs directly on error.
7773 *
Michal Vaskod3678892020-05-21 10:06:58 +02007774 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007775 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7776 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7777 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007778 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007779 *
7780 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007781 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007782 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007783 * @param[in] options XPath options.
7784 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7785 */
7786static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007787eval_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 +02007788{
Radek Krejci857189e2020-09-01 13:26:36 +02007789 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007790 LY_ERR rc;
7791
Michal Vasko004d3152020-06-11 19:59:22 +02007792 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007793 case LYXP_TOKEN_PAR1:
7794 /* '(' Expr ')' */
7795
7796 /* '(' */
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 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007802 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007803 LY_CHECK_RET(rc);
7804
7805 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007806 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007807 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007808 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007809 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007810
7811 parent_pos_pred = 0;
7812 goto predicate;
7813
7814 case LYXP_TOKEN_DOT:
7815 case LYXP_TOKEN_DDOT:
7816 case LYXP_TOKEN_AT:
7817 case LYXP_TOKEN_NAMETEST:
7818 case LYXP_TOKEN_NODETYPE:
7819 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007820 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007821 LY_CHECK_RET(rc);
7822 break;
7823
7824 case LYXP_TOKEN_FUNCNAME:
7825 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02007826 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007827 LY_CHECK_RET(rc);
7828
7829 parent_pos_pred = 1;
7830 goto predicate;
7831
Michal Vasko3e48bf32020-06-01 08:39:07 +02007832 case LYXP_TOKEN_OPER_PATH:
7833 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007834 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007835 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007836 LY_CHECK_RET(rc);
7837 break;
7838
7839 case LYXP_TOKEN_LITERAL:
7840 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02007841 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7842 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007843 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007844 }
Michal Vasko004d3152020-06-11 19:59:22 +02007845 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007846 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007847 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007848 }
7849
7850 parent_pos_pred = 1;
7851 goto predicate;
7852
7853 case LYXP_TOKEN_NUMBER:
7854 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02007855 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7856 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007857 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007858 }
Michal Vasko004d3152020-06-11 19:59:22 +02007859 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007860 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007861 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007862 }
7863 LY_CHECK_RET(rc);
7864
7865 parent_pos_pred = 1;
7866 goto predicate;
7867
7868 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01007869 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 +02007870 return LY_EVALID;
7871 }
7872
7873 return LY_SUCCESS;
7874
7875predicate:
7876 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007877 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7878 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007879 LY_CHECK_RET(rc);
7880 }
7881
7882 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007883 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007884
7885 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007886 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007887 all_desc = 0;
7888 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007889 all_desc = 1;
7890 }
7891
aPiecek8b0cc152021-05-31 16:40:31 +02007892 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007893 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007894 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007895
Michal Vasko004d3152020-06-11 19:59:22 +02007896 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007897 LY_CHECK_RET(rc);
7898 }
7899
7900 return LY_SUCCESS;
7901}
7902
7903/**
7904 * @brief Evaluate UnionExpr. Logs directly on error.
7905 *
Michal Vaskod3678892020-05-21 10:06:58 +02007906 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007907 *
7908 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007909 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007910 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007911 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007912 * @param[in] options XPath options.
7913 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7914 */
7915static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007916eval_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 +02007917{
7918 LY_ERR rc = LY_SUCCESS;
7919 struct lyxp_set orig_set, set2;
7920 uint16_t i;
7921
7922 assert(repeat);
7923
7924 set_init(&orig_set, set);
7925 set_init(&set2, set);
7926
7927 set_fill_set(&orig_set, set);
7928
Michal Vasko004d3152020-06-11 19:59:22 +02007929 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007930 LY_CHECK_GOTO(rc, cleanup);
7931
7932 /* ('|' PathExpr)* */
7933 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007934 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02007935 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007936 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007937 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007938
aPiecek8b0cc152021-05-31 16:40:31 +02007939 if (options & LYXP_SKIP_EXPR) {
7940 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007941 LY_CHECK_GOTO(rc, cleanup);
7942 continue;
7943 }
7944
7945 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007946 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007947 LY_CHECK_GOTO(rc, cleanup);
7948
7949 /* eval */
7950 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007951 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007952 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007953 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007954 LY_CHECK_GOTO(rc, cleanup);
7955 }
7956 }
7957
7958cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007959 lyxp_set_free_content(&orig_set);
7960 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007961 return rc;
7962}
7963
7964/**
7965 * @brief Evaluate UnaryExpr. Logs directly on error.
7966 *
Michal Vaskod3678892020-05-21 10:06:58 +02007967 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007968 *
7969 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007970 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007971 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007972 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007973 * @param[in] options XPath options.
7974 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7975 */
7976static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007977eval_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 +02007978{
7979 LY_ERR rc;
7980 uint16_t this_op, i;
7981
7982 assert(repeat);
7983
7984 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007985 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007986 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007987 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 +02007988
aPiecek8b0cc152021-05-31 16:40:31 +02007989 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007990 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007991 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007992 }
7993
Michal Vasko004d3152020-06-11 19:59:22 +02007994 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007995 LY_CHECK_RET(rc);
7996
aPiecek8b0cc152021-05-31 16:40:31 +02007997 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007998 if (options & LYXP_SCNODE_ALL) {
7999 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
8000 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008001 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008002 LY_CHECK_RET(rc);
8003 }
8004 }
8005
8006 return LY_SUCCESS;
8007}
8008
8009/**
8010 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
8011 *
Michal Vaskod3678892020-05-21 10:06:58 +02008012 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008013 * | MultiplicativeExpr '*' UnaryExpr
8014 * | MultiplicativeExpr 'div' UnaryExpr
8015 * | MultiplicativeExpr 'mod' UnaryExpr
8016 *
8017 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008018 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008019 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008020 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008021 * @param[in] options XPath options.
8022 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8023 */
8024static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008025eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
8026 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008027{
8028 LY_ERR rc;
8029 uint16_t this_op;
8030 struct lyxp_set orig_set, set2;
8031 uint16_t i;
8032
8033 assert(repeat);
8034
8035 set_init(&orig_set, set);
8036 set_init(&set2, set);
8037
8038 set_fill_set(&orig_set, set);
8039
Michal Vasko004d3152020-06-11 19:59:22 +02008040 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008041 LY_CHECK_GOTO(rc, cleanup);
8042
8043 /* ('*' / 'div' / 'mod' UnaryExpr)* */
8044 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008045 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008046
Michal Vasko004d3152020-06-11 19:59:22 +02008047 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02008048 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008049 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008050 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008051
aPiecek8b0cc152021-05-31 16:40:31 +02008052 if (options & LYXP_SKIP_EXPR) {
8053 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008054 LY_CHECK_GOTO(rc, cleanup);
8055 continue;
8056 }
8057
8058 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008059 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008060 LY_CHECK_GOTO(rc, cleanup);
8061
8062 /* eval */
8063 if (options & LYXP_SCNODE_ALL) {
8064 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008065 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008066 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008067 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008068 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008069 LY_CHECK_GOTO(rc, cleanup);
8070 }
8071 }
8072
8073cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008074 lyxp_set_free_content(&orig_set);
8075 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008076 return rc;
8077}
8078
8079/**
8080 * @brief Evaluate AdditiveExpr. Logs directly on error.
8081 *
Michal Vaskod3678892020-05-21 10:06:58 +02008082 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008083 * | AdditiveExpr '+' MultiplicativeExpr
8084 * | AdditiveExpr '-' MultiplicativeExpr
8085 *
8086 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008087 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008088 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008089 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008090 * @param[in] options XPath options.
8091 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8092 */
8093static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008094eval_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 +02008095{
8096 LY_ERR rc;
8097 uint16_t this_op;
8098 struct lyxp_set orig_set, set2;
8099 uint16_t i;
8100
8101 assert(repeat);
8102
8103 set_init(&orig_set, set);
8104 set_init(&set2, set);
8105
8106 set_fill_set(&orig_set, set);
8107
Michal Vasko004d3152020-06-11 19:59:22 +02008108 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008109 LY_CHECK_GOTO(rc, cleanup);
8110
8111 /* ('+' / '-' MultiplicativeExpr)* */
8112 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008113 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008114
Michal Vasko004d3152020-06-11 19:59:22 +02008115 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008116 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008117 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008118 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008119
aPiecek8b0cc152021-05-31 16:40:31 +02008120 if (options & LYXP_SKIP_EXPR) {
8121 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008122 LY_CHECK_GOTO(rc, cleanup);
8123 continue;
8124 }
8125
8126 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008127 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008128 LY_CHECK_GOTO(rc, cleanup);
8129
8130 /* eval */
8131 if (options & LYXP_SCNODE_ALL) {
8132 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008133 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008134 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008135 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008136 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008137 LY_CHECK_GOTO(rc, cleanup);
8138 }
8139 }
8140
8141cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008142 lyxp_set_free_content(&orig_set);
8143 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008144 return rc;
8145}
8146
8147/**
8148 * @brief Evaluate RelationalExpr. Logs directly on error.
8149 *
Michal Vaskod3678892020-05-21 10:06:58 +02008150 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008151 * | RelationalExpr '<' AdditiveExpr
8152 * | RelationalExpr '>' AdditiveExpr
8153 * | RelationalExpr '<=' AdditiveExpr
8154 * | RelationalExpr '>=' AdditiveExpr
8155 *
8156 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008157 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008158 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008159 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008160 * @param[in] options XPath options.
8161 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8162 */
8163static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008164eval_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 +02008165{
8166 LY_ERR rc;
8167 uint16_t this_op;
8168 struct lyxp_set orig_set, set2;
8169 uint16_t i;
8170
8171 assert(repeat);
8172
8173 set_init(&orig_set, set);
8174 set_init(&set2, set);
8175
8176 set_fill_set(&orig_set, set);
8177
Michal Vasko004d3152020-06-11 19:59:22 +02008178 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008179 LY_CHECK_GOTO(rc, cleanup);
8180
8181 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8182 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008183 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008184
Michal Vasko004d3152020-06-11 19:59:22 +02008185 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02008186 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008187 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008188 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008189
aPiecek8b0cc152021-05-31 16:40:31 +02008190 if (options & LYXP_SKIP_EXPR) {
8191 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008192 LY_CHECK_GOTO(rc, cleanup);
8193 continue;
8194 }
8195
8196 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008197 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008198 LY_CHECK_GOTO(rc, cleanup);
8199
8200 /* eval */
8201 if (options & LYXP_SCNODE_ALL) {
8202 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008203 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008204 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008205 } else {
8206 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8207 LY_CHECK_GOTO(rc, cleanup);
8208 }
8209 }
8210
8211cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008212 lyxp_set_free_content(&orig_set);
8213 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008214 return rc;
8215}
8216
8217/**
8218 * @brief Evaluate EqualityExpr. Logs directly on error.
8219 *
Michal Vaskod3678892020-05-21 10:06:58 +02008220 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008221 * | EqualityExpr '!=' RelationalExpr
8222 *
8223 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008224 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008225 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008226 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008227 * @param[in] options XPath options.
8228 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8229 */
8230static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008231eval_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 +02008232{
8233 LY_ERR rc;
8234 uint16_t this_op;
8235 struct lyxp_set orig_set, set2;
8236 uint16_t i;
8237
8238 assert(repeat);
8239
8240 set_init(&orig_set, set);
8241 set_init(&set2, set);
8242
8243 set_fill_set(&orig_set, set);
8244
Michal Vasko004d3152020-06-11 19:59:22 +02008245 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008246 LY_CHECK_GOTO(rc, cleanup);
8247
8248 /* ('=' / '!=' RelationalExpr)* */
8249 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008250 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008251
Michal Vasko004d3152020-06-11 19:59:22 +02008252 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02008253 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008254 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008255 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008256
aPiecek8b0cc152021-05-31 16:40:31 +02008257 if (options & LYXP_SKIP_EXPR) {
8258 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008259 LY_CHECK_GOTO(rc, cleanup);
8260 continue;
8261 }
8262
8263 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008264 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008265 LY_CHECK_GOTO(rc, cleanup);
8266
8267 /* eval */
8268 if (options & LYXP_SCNODE_ALL) {
8269 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008270 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8271 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008272 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008273 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008274 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008275 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8276 LY_CHECK_GOTO(rc, cleanup);
8277 }
8278 }
8279
8280cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008281 lyxp_set_free_content(&orig_set);
8282 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008283 return rc;
8284}
8285
8286/**
8287 * @brief Evaluate AndExpr. Logs directly on error.
8288 *
Michal Vaskod3678892020-05-21 10:06:58 +02008289 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008290 *
8291 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008292 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008293 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008294 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008295 * @param[in] options XPath options.
8296 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8297 */
8298static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008299eval_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 +02008300{
8301 LY_ERR rc;
8302 struct lyxp_set orig_set, set2;
8303 uint16_t i;
8304
8305 assert(repeat);
8306
8307 set_init(&orig_set, set);
8308 set_init(&set2, set);
8309
8310 set_fill_set(&orig_set, set);
8311
Michal Vasko004d3152020-06-11 19:59:22 +02008312 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008313 LY_CHECK_GOTO(rc, cleanup);
8314
8315 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008316 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008317 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008318 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008319 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008320 }
8321
8322 /* ('and' EqualityExpr)* */
8323 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008324 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008325 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008326 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008327 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008328
8329 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008330 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8331 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008332 LY_CHECK_GOTO(rc, cleanup);
8333 continue;
8334 }
8335
8336 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008337 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008338 LY_CHECK_GOTO(rc, cleanup);
8339
8340 /* eval - just get boolean value actually */
8341 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008342 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008343 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008344 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008345 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008346 set_fill_set(set, &set2);
8347 }
8348 }
8349
8350cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008351 lyxp_set_free_content(&orig_set);
8352 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008353 return rc;
8354}
8355
8356/**
8357 * @brief Evaluate OrExpr. Logs directly on error.
8358 *
Michal Vaskod3678892020-05-21 10:06:58 +02008359 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008360 *
8361 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008362 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008363 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008364 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008365 * @param[in] options XPath options.
8366 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8367 */
8368static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008369eval_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 +02008370{
8371 LY_ERR rc;
8372 struct lyxp_set orig_set, set2;
8373 uint16_t i;
8374
8375 assert(repeat);
8376
8377 set_init(&orig_set, set);
8378 set_init(&set2, set);
8379
8380 set_fill_set(&orig_set, set);
8381
Michal Vasko004d3152020-06-11 19:59:22 +02008382 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008383 LY_CHECK_GOTO(rc, cleanup);
8384
8385 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008386 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008387 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008388 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008389 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008390 }
8391
8392 /* ('or' AndExpr)* */
8393 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008394 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008395 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008396 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008397 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008398
8399 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008400 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8401 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008402 LY_CHECK_GOTO(rc, cleanup);
8403 continue;
8404 }
8405
8406 set_fill_set(&set2, &orig_set);
8407 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8408 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008409 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008410 LY_CHECK_GOTO(rc, cleanup);
8411
8412 /* eval - just get boolean value actually */
8413 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008414 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008415 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008416 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008417 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008418 set_fill_set(set, &set2);
8419 }
8420 }
8421
8422cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008423 lyxp_set_free_content(&orig_set);
8424 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008425 return rc;
8426}
8427
8428/**
Michal Vasko004d3152020-06-11 19:59:22 +02008429 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008430 *
8431 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008432 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008433 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02008434 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008435 * @param[in] options XPath options.
8436 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8437 */
8438static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008439eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8440 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008441{
8442 uint16_t i, count;
8443 enum lyxp_expr_type next_etype;
8444 LY_ERR rc;
8445
8446 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008447 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008448 next_etype = LYXP_EXPR_NONE;
8449 } else {
8450 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008451 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008452
8453 /* select one-priority lower because etype expression called us */
8454 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008455 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008456 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008457 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008458 } else {
8459 next_etype = LYXP_EXPR_NONE;
8460 }
8461 }
8462
8463 /* decide what expression are we parsing based on the repeat */
8464 switch (next_etype) {
8465 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008466 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008467 break;
8468 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008469 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008470 break;
8471 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008472 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008473 break;
8474 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008475 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008476 break;
8477 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008478 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008479 break;
8480 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008481 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008482 break;
8483 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008484 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008485 break;
8486 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008487 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008488 break;
8489 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008490 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008491 break;
8492 default:
8493 LOGINT_RET(set->ctx);
8494 }
8495
8496 return rc;
8497}
8498
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008499/**
8500 * @brief Get root type.
8501 *
8502 * @param[in] ctx_node Context node.
8503 * @param[in] ctx_scnode Schema context node.
8504 * @param[in] options XPath options.
8505 * @return Root type.
8506 */
8507static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008508lyxp_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 +01008509{
Michal Vasko6b26e742020-07-17 15:02:10 +02008510 const struct lysc_node *op;
8511
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008512 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008513 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008514 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008515
8516 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008517 /* general root that can access everything */
8518 return LYXP_NODE_ROOT;
8519 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8520 /* root context node can access only config data (because we said so, it is unspecified) */
8521 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008522 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008523 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008524 }
8525
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008526 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008527 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008528 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008529
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008530 if (op || !(options & LYXP_SCHEMA)) {
8531 /* general root that can access everything */
8532 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05008533 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008534 /* root context node can access only config data (because we said so, it is unspecified) */
8535 return LYXP_NODE_ROOT_CONFIG;
8536 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008537 return LYXP_NODE_ROOT;
8538}
8539
Michal Vasko03ff5a72019-09-11 13:49:33 +02008540LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008541lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008542 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 +01008543 struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008544{
Michal Vasko004d3152020-06-11 19:59:22 +02008545 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008546 LY_ERR rc;
8547
Michal Vasko400e9672021-01-11 13:39:17 +01008548 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008549 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008550 LOGARG(NULL, "Current module must be set if schema format is used.");
8551 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008552 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008553
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008554 if (tree) {
8555 /* adjust the pointer to be the first top-level sibling */
8556 while (tree->parent) {
8557 tree = lyd_parent(tree);
8558 }
8559 tree = lyd_first_sibling(tree);
8560 }
8561
Michal Vasko03ff5a72019-09-11 13:49:33 +02008562 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008563 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008564 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008565 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8566 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8567
Michal Vasko400e9672021-01-11 13:39:17 +01008568 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008569 set->cur_node = ctx_node;
8570 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008571 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8572 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008573 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008574 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008575 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008576 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008577
Radek Krejciddace2c2021-01-08 11:30:56 +01008578 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008579
Michal Vasko03ff5a72019-09-11 13:49:33 +02008580 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008581 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008582 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008583 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008584 }
8585
Radek Krejciddace2c2021-01-08 11:30:56 +01008586 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008587 return rc;
8588}
8589
8590#if 0
8591
8592/* full xml printing of set elements, not used currently */
8593
8594void
8595lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8596{
8597 uint32_t i;
8598 char *str_num;
8599 struct lyout out;
8600
8601 memset(&out, 0, sizeof out);
8602
8603 out.type = LYOUT_STREAM;
8604 out.method.f = f;
8605
8606 switch (set->type) {
8607 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008608 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008609 break;
8610 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008611 ly_print_(&out, "Boolean XPath set:\n");
8612 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008613 break;
8614 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008615 ly_print_(&out, "String XPath set:\n");
8616 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008617 break;
8618 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008619 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008620
8621 if (isnan(set->value.num)) {
8622 str_num = strdup("NaN");
8623 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8624 str_num = strdup("0");
8625 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8626 str_num = strdup("Infinity");
8627 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8628 str_num = strdup("-Infinity");
8629 } else if ((long long)set->value.num == set->value.num) {
8630 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8631 str_num = NULL;
8632 }
8633 } else {
8634 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8635 str_num = NULL;
8636 }
8637 }
8638 if (!str_num) {
8639 LOGMEM;
8640 return;
8641 }
Michal Vasko5233e962020-08-14 14:26:20 +02008642 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008643 free(str_num);
8644 break;
8645 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008646 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008647
8648 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008649 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008650 switch (set->node_type[i]) {
8651 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008652 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008653 break;
8654 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008655 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008656 break;
8657 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008658 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008659 break;
8660 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008661 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008662 break;
8663 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008664 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008665 break;
8666 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008667 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008668 break;
8669 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008670 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008671 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008672 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008673 break;
8674 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008675 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 +02008676 break;
8677 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008678 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 +02008679 break;
8680 }
8681 }
8682 break;
8683 }
8684}
8685
8686#endif
8687
8688LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008689lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008690{
8691 long double num;
8692 char *str;
8693 LY_ERR rc;
8694
8695 if (!set || (set->type == target)) {
8696 return LY_SUCCESS;
8697 }
8698
8699 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008700 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008701
8702 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008703 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008704 return LY_EINVAL;
8705 }
8706
8707 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008708 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008709 switch (set->type) {
8710 case LYXP_SET_NUMBER:
8711 if (isnan(set->val.num)) {
8712 set->val.str = strdup("NaN");
8713 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8714 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8715 set->val.str = strdup("0");
8716 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8717 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8718 set->val.str = strdup("Infinity");
8719 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8720 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8721 set->val.str = strdup("-Infinity");
8722 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8723 } else if ((long long)set->val.num == set->val.num) {
8724 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8725 LOGMEM_RET(set->ctx);
8726 }
8727 set->val.str = str;
8728 } else {
8729 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8730 LOGMEM_RET(set->ctx);
8731 }
8732 set->val.str = str;
8733 }
8734 break;
8735 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008736 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008737 set->val.str = strdup("true");
8738 } else {
8739 set->val.str = strdup("false");
8740 }
8741 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8742 break;
8743 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008744 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008745 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008746
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008747 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008748 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008749 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008750 set->val.str = str;
8751 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008752 default:
8753 LOGINT_RET(set->ctx);
8754 }
8755 set->type = LYXP_SET_STRING;
8756 }
8757
8758 /* to NUMBER */
8759 if (target == LYXP_SET_NUMBER) {
8760 switch (set->type) {
8761 case LYXP_SET_STRING:
8762 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008763 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008764 set->val.num = num;
8765 break;
8766 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008767 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008768 set->val.num = 1;
8769 } else {
8770 set->val.num = 0;
8771 }
8772 break;
8773 default:
8774 LOGINT_RET(set->ctx);
8775 }
8776 set->type = LYXP_SET_NUMBER;
8777 }
8778
8779 /* to BOOLEAN */
8780 if (target == LYXP_SET_BOOLEAN) {
8781 switch (set->type) {
8782 case LYXP_SET_NUMBER:
8783 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008784 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008785 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008786 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008787 }
8788 break;
8789 case LYXP_SET_STRING:
8790 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008791 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008792 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008793 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008794 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008795 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008796 }
8797 break;
8798 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008799 if (set->used) {
8800 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008801 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008802 } else {
8803 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008804 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008805 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008806 break;
8807 default:
8808 LOGINT_RET(set->ctx);
8809 }
8810 set->type = LYXP_SET_BOOLEAN;
8811 }
8812
Michal Vasko03ff5a72019-09-11 13:49:33 +02008813 return LY_SUCCESS;
8814}
8815
8816LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008817lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008818 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
Michal Vasko400e9672021-01-11 13:39:17 +01008819 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008820{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008821 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008822 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008823
Michal Vasko400e9672021-01-11 13:39:17 +01008824 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008825 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008826 LOGARG(NULL, "Current module must be set if schema format is used.");
8827 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008828 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008829
8830 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008831 memset(set, 0, sizeof *set);
8832 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008833 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8834 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 +01008835 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008836
Michal Vasko400e9672021-01-11 13:39:17 +01008837 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008838 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008839 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008840 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8841 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008842 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008843 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008844 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008845
Radek Krejciddace2c2021-01-08 11:30:56 +01008846 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008847
Michal Vasko03ff5a72019-09-11 13:49:33 +02008848 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01008849 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
8850
Radek Krejciddace2c2021-01-08 11:30:56 +01008851 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008852 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008853}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008854
8855API const char *
8856lyxp_get_expr(const struct lyxp_expr *path)
8857{
8858 if (!path) {
8859 return NULL;
8860 }
8861
8862 return path->expr;
8863}