blob: a4856bc81f8dff29bcb7da5bf182669227157e09 [file] [log] [blame]
Radek Krejcib1646a92018-11-02 16:08:26 +01001/**
2 * @file xpath.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG XPath evaluation functions
5 *
Michal Vasko61ac2f62020-05-25 12:39:51 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcib1646a92018-11-02 16:08:26 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Christian Hopps32874e12021-05-01 09:43:54 -040014#define _GNU_SOURCE /* asprintf, strdup */
Radek Krejcib1646a92018-11-02 16:08:26 +010015
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010017
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010019#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010023#include <stdio.h>
24#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010025#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010026
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020028#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010032#include "out.h"
Radek Krejci7931b192020-06-25 17:05:03 +020033#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020034#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020035#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020037#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "tree.h"
Radek Krejci77114102021-03-10 15:21:57 +010039#include "tree_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020040#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010041#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020042#include "tree_schema_internal.h"
43#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020044
aPiecekbf968d92021-05-27 14:35:05 +020045static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth);
Michal Vasko40308e72020-10-20 16:38:40 +020046static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype,
47 struct lyxp_set *set, uint32_t options);
Michal Vasko93923692021-05-07 15:28:02 +020048static LY_ERR moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
49 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod);
Michal Vasko03ff5a72019-09-11 13:49:33 +020050
51/**
52 * @brief Print the type of an XPath \p set.
53 *
54 * @param[in] set Set to use.
55 * @return Set type string.
56 */
57static const char *
58print_set_type(struct lyxp_set *set)
59{
60 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020061 case LYXP_SET_NODE_SET:
62 return "node set";
63 case LYXP_SET_SCNODE_SET:
64 return "schema node set";
65 case LYXP_SET_BOOLEAN:
66 return "boolean";
67 case LYXP_SET_NUMBER:
68 return "number";
69 case LYXP_SET_STRING:
70 return "string";
71 }
72
73 return NULL;
74}
75
Michal Vasko24cddf82020-06-01 08:17:01 +020076const char *
77lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020078{
79 switch (tok) {
80 case LYXP_TOKEN_PAR1:
81 return "(";
82 case LYXP_TOKEN_PAR2:
83 return ")";
84 case LYXP_TOKEN_BRACK1:
85 return "[";
86 case LYXP_TOKEN_BRACK2:
87 return "]";
88 case LYXP_TOKEN_DOT:
89 return ".";
90 case LYXP_TOKEN_DDOT:
91 return "..";
92 case LYXP_TOKEN_AT:
93 return "@";
94 case LYXP_TOKEN_COMMA:
95 return ",";
96 case LYXP_TOKEN_NAMETEST:
97 return "NameTest";
98 case LYXP_TOKEN_NODETYPE:
99 return "NodeType";
100 case LYXP_TOKEN_FUNCNAME:
101 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200102 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200103 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200104 case LYXP_TOKEN_OPER_EQUAL:
105 return "Operator(Equal)";
106 case LYXP_TOKEN_OPER_NEQUAL:
107 return "Operator(Non-equal)";
108 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200109 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200110 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200111 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200112 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200113 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200114 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200115 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200116 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200117 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200118 case LYXP_TOKEN_LITERAL:
119 return "Literal";
120 case LYXP_TOKEN_NUMBER:
121 return "Number";
122 default:
123 LOGINT(NULL);
124 return "";
125 }
126}
127
128/**
129 * @brief Print the whole expression \p exp to debug output.
130 *
131 * @param[in] exp Expression to use.
132 */
133static void
Michal Vasko40308e72020-10-20 16:38:40 +0200134print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200135{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100136#define MSG_BUFFER_SIZE 128
137 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100138 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200139
Radek Krejci52b6d512020-10-12 12:33:17 +0200140 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200141 return;
142 }
143
144 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
145 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200146 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200147 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100148 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200149 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
150 for (j = 1; exp->repeat[i][j]; ++j) {
151 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
152 }
153 strcat(tmp, ")");
154 }
155 LOGDBG(LY_LDGXPATH, tmp);
156 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100157#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200158}
159
160#ifndef NDEBUG
161
162/**
163 * @brief Print XPath set content to debug output.
164 *
165 * @param[in] set Set to print.
166 */
167static void
168print_set_debug(struct lyxp_set *set)
169{
170 uint32_t i;
171 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200172 struct lyxp_set_node *item;
173 struct lyxp_set_scnode *sitem;
174
Radek Krejci52b6d512020-10-12 12:33:17 +0200175 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200176 return;
177 }
178
179 switch (set->type) {
180 case LYXP_SET_NODE_SET:
181 LOGDBG(LY_LDGXPATH, "set NODE SET:");
182 for (i = 0; i < set->used; ++i) {
183 item = &set->val.nodes[i];
184
185 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100186 case LYXP_NODE_NONE:
187 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
188 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200189 case LYXP_NODE_ROOT:
190 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
191 break;
192 case LYXP_NODE_ROOT_CONFIG:
193 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
194 break;
195 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100196 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200197 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200198 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100199 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200200 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200201 item->node->schema->name, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200202 } else {
203 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
204 }
205 break;
206 case LYXP_NODE_TEXT:
207 if (item->node->schema->nodetype & LYS_ANYDATA) {
208 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200209 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200210 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200211 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200212 }
213 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100214 case LYXP_NODE_META:
215 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
Michal Vasko69730152020-10-09 16:30:07 +0200216 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200217 break;
218 }
219 }
220 break;
221
222 case LYXP_SET_SCNODE_SET:
223 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
224 for (i = 0; i < set->used; ++i) {
225 sitem = &set->val.scnodes[i];
226
227 switch (sitem->type) {
228 case LYXP_NODE_ROOT:
229 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
230 break;
231 case LYXP_NODE_ROOT_CONFIG:
232 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
233 break;
234 case LYXP_NODE_ELEM:
235 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
236 break;
237 default:
238 LOGINT(NULL);
239 break;
240 }
241 }
242 break;
243
Michal Vasko03ff5a72019-09-11 13:49:33 +0200244 case LYXP_SET_BOOLEAN:
245 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200246 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200247 break;
248
249 case LYXP_SET_STRING:
250 LOGDBG(LY_LDGXPATH, "set STRING");
251 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
252 break;
253
254 case LYXP_SET_NUMBER:
255 LOGDBG(LY_LDGXPATH, "set NUMBER");
256
257 if (isnan(set->val.num)) {
258 str = strdup("NaN");
259 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
260 str = strdup("0");
261 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
262 str = strdup("Infinity");
263 } else if (isinf(set->val.num) && signbit(set->val.num)) {
264 str = strdup("-Infinity");
265 } else if ((long long)set->val.num == set->val.num) {
266 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
267 str = NULL;
268 }
269 } else {
270 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
271 str = NULL;
272 }
273 }
274 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
275
276 LOGDBG(LY_LDGXPATH, "\t%s", str);
277 free(str);
278 }
279}
280
281#endif
282
283/**
284 * @brief Realloc the string \p str.
285 *
286 * @param[in] ctx libyang context for logging.
287 * @param[in] needed How much free space is required.
288 * @param[in,out] str Pointer to the string to use.
289 * @param[in,out] used Used bytes in \p str.
290 * @param[in,out] size Allocated bytes in \p str.
291 * @return LY_ERR
292 */
293static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100294cast_string_realloc(const struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200295{
296 if (*size - *used < needed) {
297 do {
298 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
299 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
300 return LY_EINVAL;
301 }
302 *size += LYXP_STRING_CAST_SIZE_STEP;
303 } while (*size - *used < needed);
304 *str = ly_realloc(*str, *size * sizeof(char));
305 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
306 }
307
308 return LY_SUCCESS;
309}
310
311/**
312 * @brief Cast nodes recursively to one string @p str.
313 *
314 * @param[in] node Node to cast.
315 * @param[in] fake_cont Whether to put the data into a "fake" container.
316 * @param[in] root_type Type of the XPath root.
317 * @param[in] indent Current indent.
318 * @param[in,out] str Resulting string.
319 * @param[in,out] used Used bytes in @p str.
320 * @param[in,out] size Allocated bytes in @p str.
321 * @return LY_ERR
322 */
323static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200324cast_string_recursive(const struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
Radek Krejci0f969882020-08-21 16:56:47 +0200325 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200326{
Radek Krejci7f769d72020-07-11 23:13:56 +0200327 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200328 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200329 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200330 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200331 struct lyd_node_any *any;
332 LY_ERR rc;
333
334 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
335 return LY_SUCCESS;
336 }
337
338 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200339 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200340 LY_CHECK_RET(rc);
341 strcpy(*str + (*used - 1), "\n");
342 ++(*used);
343
344 ++indent;
345 }
346
347 switch (node->schema->nodetype) {
348 case LYS_CONTAINER:
349 case LYS_LIST:
350 case LYS_RPC:
351 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200352 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200353 LY_CHECK_RET(rc);
354 strcpy(*str + (*used - 1), "\n");
355 ++(*used);
356
Radek Krejcia1c1e542020-09-29 16:06:52 +0200357 for (child = lyd_child(node); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200358 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
359 LY_CHECK_RET(rc);
360 }
361
362 break;
363
364 case LYS_LEAF:
365 case LYS_LEAFLIST:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200366 value_str = lyd_get_value(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200367
368 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200369 LY_CHECK_RET(cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200370 memset(*str + (*used - 1), ' ', indent * 2);
371 *used += indent * 2;
372
373 /* print value */
374 if (*used == 1) {
375 sprintf(*str + (*used - 1), "%s", value_str);
376 *used += strlen(value_str);
377 } else {
378 sprintf(*str + (*used - 1), "%s\n", value_str);
379 *used += strlen(value_str) + 1;
380 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200381
382 break;
383
384 case LYS_ANYXML:
385 case LYS_ANYDATA:
386 any = (struct lyd_node_any *)node;
387 if (!(void *)any->value.tree) {
388 /* no content */
389 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200390 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200391 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200392 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100393
Michal Vasko60ea6352020-06-29 13:39:39 +0200394 if (any->value_type == LYD_ANYDATA_LYB) {
395 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200396 if (lyd_parse_data_mem((struct ly_ctx *)LYD_CTX(node), any->value.mem, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200397 /* successfully parsed */
398 free(any->value.mem);
399 any->value.tree = tree;
400 any->value_type = LYD_ANYDATA_DATATREE;
401 }
Radek Krejci7931b192020-06-25 17:05:03 +0200402 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200403 }
404
Michal Vasko03ff5a72019-09-11 13:49:33 +0200405 switch (any->value_type) {
406 case LYD_ANYDATA_STRING:
407 case LYD_ANYDATA_XML:
408 case LYD_ANYDATA_JSON:
409 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200410 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200411 break;
412 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200413 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200414 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200415 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100416 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200417 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200418 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200419 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200420 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200421 }
422 }
423
424 line = strtok_r(buf, "\n", &ptr);
425 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200426 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200427 if (rc != LY_SUCCESS) {
428 free(buf);
429 return rc;
430 }
431 memset(*str + (*used - 1), ' ', indent * 2);
432 *used += indent * 2;
433
434 strcpy(*str + (*used - 1), line);
435 *used += strlen(line);
436
437 strcpy(*str + (*used - 1), "\n");
438 *used += 1;
439 } while ((line = strtok_r(NULL, "\n", &ptr)));
440
441 free(buf);
442 break;
443
444 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200445 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200446 }
447
448 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200449 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200450 LY_CHECK_RET(rc);
451 strcpy(*str + (*used - 1), "\n");
452 ++(*used);
453
454 --indent;
455 }
456
457 return LY_SUCCESS;
458}
459
460/**
461 * @brief Cast an element into a string.
462 *
463 * @param[in] node Node to cast.
464 * @param[in] fake_cont Whether to put the data into a "fake" container.
465 * @param[in] root_type Type of the XPath root.
466 * @param[out] str Element cast to dynamically-allocated string.
467 * @return LY_ERR
468 */
469static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200470cast_string_elem(struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200471{
472 uint16_t used, size;
473 LY_ERR rc;
474
475 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200476 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200477 (*str)[0] = '\0';
478 used = 1;
479 size = LYXP_STRING_CAST_SIZE_START;
480
481 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
482 if (rc != LY_SUCCESS) {
483 free(*str);
484 return rc;
485 }
486
487 if (size > used) {
488 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200489 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200490 }
491 return LY_SUCCESS;
492}
493
494/**
495 * @brief Cast a LYXP_SET_NODE_SET set into a string.
496 * Context position aware.
497 *
498 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200499 * @param[out] str Cast dynamically-allocated string.
500 * @return LY_ERR
501 */
502static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100503cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200504{
Michal Vaskoaa677062021-03-09 13:52:53 +0100505 if (!set->used) {
506 *str = strdup("");
507 if (!*str) {
508 LOGMEM_RET(set->ctx);
509 }
510 return LY_SUCCESS;
511 }
512
Michal Vasko03ff5a72019-09-11 13:49:33 +0200513 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100514 case LYXP_NODE_NONE:
515 /* invalid */
516 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200517 case LYXP_NODE_ROOT:
518 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100519 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200520 case LYXP_NODE_ELEM:
521 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100522 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100523 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200524 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200525 if (!*str) {
526 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200527 }
528 return LY_SUCCESS;
529 }
530
531 LOGINT_RET(set->ctx);
532}
533
534/**
535 * @brief Cast a string into an XPath number.
536 *
537 * @param[in] str String to use.
538 * @return Cast number.
539 */
540static long double
541cast_string_to_number(const char *str)
542{
543 long double num;
544 char *ptr;
545
546 errno = 0;
547 num = strtold(str, &ptr);
548 if (errno || *ptr) {
549 num = NAN;
550 }
551 return num;
552}
553
554/**
555 * @brief Callback for checking value equality.
556 *
Michal Vasko62524a92021-02-26 10:08:50 +0100557 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200558 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200559 * @param[in] val1_p First value.
560 * @param[in] val2_p Second value.
561 * @param[in] mod Whether hash table is being modified.
562 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200563 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200564 */
Radek Krejci857189e2020-09-01 13:26:36 +0200565static ly_bool
566set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200567{
568 struct lyxp_set_hash_node *val1, *val2;
569
570 val1 = (struct lyxp_set_hash_node *)val1_p;
571 val2 = (struct lyxp_set_hash_node *)val2_p;
572
573 if ((val1->node == val2->node) && (val1->type == val2->type)) {
574 return 1;
575 }
576
577 return 0;
578}
579
580/**
581 * @brief Insert node and its hash into set.
582 *
583 * @param[in] set et to insert to.
584 * @param[in] node Node with hash.
585 * @param[in] type Node type.
586 */
587static void
588set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
589{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200590 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200591 uint32_t i, hash;
592 struct lyxp_set_hash_node hnode;
593
594 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
595 /* create hash table and add all the nodes */
596 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
597 for (i = 0; i < set->used; ++i) {
598 hnode.node = set->val.nodes[i].node;
599 hnode.type = set->val.nodes[i].type;
600
601 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
602 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
603 hash = dict_hash_multi(hash, NULL, 0);
604
605 r = lyht_insert(set->ht, &hnode, hash, NULL);
606 assert(!r);
607 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200608
Michal Vasko9d6befd2019-12-11 14:56:56 +0100609 if (hnode.node == node) {
610 /* it was just added, do not add it twice */
611 node = NULL;
612 }
613 }
614 }
615
616 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200617 /* add the new node into hash table */
618 hnode.node = node;
619 hnode.type = type;
620
621 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
622 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
623 hash = dict_hash_multi(hash, NULL, 0);
624
625 r = lyht_insert(set->ht, &hnode, hash, NULL);
626 assert(!r);
627 (void)r;
628 }
629}
630
631/**
632 * @brief Remove node and its hash from set.
633 *
634 * @param[in] set Set to remove from.
635 * @param[in] node Node to remove.
636 * @param[in] type Node type.
637 */
638static void
639set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
640{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200641 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200642 struct lyxp_set_hash_node hnode;
643 uint32_t hash;
644
645 if (set->ht) {
646 hnode.node = node;
647 hnode.type = type;
648
649 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
650 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
651 hash = dict_hash_multi(hash, NULL, 0);
652
653 r = lyht_remove(set->ht, &hnode, hash);
654 assert(!r);
655 (void)r;
656
657 if (!set->ht->used) {
658 lyht_free(set->ht);
659 set->ht = NULL;
660 }
661 }
662}
663
664/**
665 * @brief Check whether node is in set based on its hash.
666 *
667 * @param[in] set Set to search in.
668 * @param[in] node Node to search for.
669 * @param[in] type Node type.
670 * @param[in] skip_idx Index in @p set to skip.
671 * @return LY_ERR
672 */
673static LY_ERR
674set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
675{
676 struct lyxp_set_hash_node hnode, *match_p;
677 uint32_t hash;
678
679 hnode.node = node;
680 hnode.type = type;
681
682 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
683 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
684 hash = dict_hash_multi(hash, NULL, 0);
685
686 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
687 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
688 /* we found it on the index that should be skipped, find another */
689 hnode = *match_p;
690 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
691 /* none other found */
692 return LY_SUCCESS;
693 }
694 }
695
696 return LY_EEXIST;
697 }
698
699 /* not found */
700 return LY_SUCCESS;
701}
702
Michal Vaskod3678892020-05-21 10:06:58 +0200703void
704lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200705{
706 if (!set) {
707 return;
708 }
709
710 if (set->type == LYXP_SET_NODE_SET) {
711 free(set->val.nodes);
712 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200713 } else if (set->type == LYXP_SET_SCNODE_SET) {
714 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200715 lyht_free(set->ht);
716 } else {
717 if (set->type == LYXP_SET_STRING) {
718 free(set->val.str);
719 }
720 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200721 }
Michal Vaskod3678892020-05-21 10:06:58 +0200722
723 set->val.nodes = NULL;
724 set->used = 0;
725 set->size = 0;
726 set->ht = NULL;
727 set->ctx_pos = 0;
aPiecek748da732021-06-01 09:56:43 +0200728 set->ctx_size = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200729}
730
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100731/**
732 * @brief Free dynamically-allocated set.
733 *
734 * @param[in] set Set to free.
735 */
736static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200737lyxp_set_free(struct lyxp_set *set)
738{
739 if (!set) {
740 return;
741 }
742
Michal Vaskod3678892020-05-21 10:06:58 +0200743 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200744 free(set);
745}
746
747/**
748 * @brief Initialize set context.
749 *
750 * @param[in] new Set to initialize.
751 * @param[in] set Arbitrary initialized set.
752 */
753static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200754set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200755{
756 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200757 if (set) {
758 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200759 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100760 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200761 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100762 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200763 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200764 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200765 new->prefix_data = set->prefix_data;
Michal Vasko02a77382019-09-12 11:47:35 +0200766 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200767}
768
769/**
770 * @brief Create a deep copy of a set.
771 *
772 * @param[in] set Set to copy.
773 * @return Copy of @p set.
774 */
775static struct lyxp_set *
776set_copy(struct lyxp_set *set)
777{
778 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100779 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200780
781 if (!set) {
782 return NULL;
783 }
784
785 ret = malloc(sizeof *ret);
786 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
787 set_init(ret, set);
788
789 if (set->type == LYXP_SET_SCNODE_SET) {
790 ret->type = set->type;
791
792 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100793 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
794 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200795 uint32_t idx;
796 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
797 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100798 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200799 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200800 lyxp_set_free(ret);
801 return NULL;
802 }
Michal Vaskoba716542019-12-16 10:01:58 +0100803 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200804 }
805 }
806 } else if (set->type == LYXP_SET_NODE_SET) {
807 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200808 if (set->used) {
809 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
810 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
811 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
812 } else {
813 ret->val.nodes = NULL;
814 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200815
816 ret->used = ret->size = set->used;
817 ret->ctx_pos = set->ctx_pos;
818 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100819 if (set->ht) {
820 ret->ht = lyht_dup(set->ht);
821 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200822 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200823 memcpy(ret, set, sizeof *ret);
824 if (set->type == LYXP_SET_STRING) {
825 ret->val.str = strdup(set->val.str);
826 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
827 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200828 }
829
830 return ret;
831}
832
833/**
834 * @brief Fill XPath set with a string. Any current data are disposed of.
835 *
836 * @param[in] set Set to fill.
837 * @param[in] string String to fill into \p set.
838 * @param[in] str_len Length of \p string. 0 is a valid value!
839 */
840static void
841set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
842{
Michal Vaskod3678892020-05-21 10:06:58 +0200843 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200844
845 set->type = LYXP_SET_STRING;
846 if ((str_len == 0) && (string[0] != '\0')) {
847 string = "";
848 }
849 set->val.str = strndup(string, str_len);
850}
851
852/**
853 * @brief Fill XPath set with a number. Any current data are disposed of.
854 *
855 * @param[in] set Set to fill.
856 * @param[in] number Number to fill into \p set.
857 */
858static void
859set_fill_number(struct lyxp_set *set, long double number)
860{
Michal Vaskod3678892020-05-21 10:06:58 +0200861 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200862
863 set->type = LYXP_SET_NUMBER;
864 set->val.num = number;
865}
866
867/**
868 * @brief Fill XPath set with a boolean. Any current data are disposed of.
869 *
870 * @param[in] set Set to fill.
871 * @param[in] boolean Boolean to fill into \p set.
872 */
873static void
Radek Krejci857189e2020-09-01 13:26:36 +0200874set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200875{
Michal Vaskod3678892020-05-21 10:06:58 +0200876 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200877
878 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200879 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200880}
881
882/**
883 * @brief Fill XPath set with the value from another set (deep assign).
884 * Any current data are disposed of.
885 *
886 * @param[in] trg Set to fill.
887 * @param[in] src Source set to copy into \p trg.
888 */
889static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200890set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200891{
892 if (!trg || !src) {
893 return;
894 }
895
896 if (trg->type == LYXP_SET_NODE_SET) {
897 free(trg->val.nodes);
898 } else if (trg->type == LYXP_SET_STRING) {
899 free(trg->val.str);
900 }
901 set_init(trg, src);
902
903 if (src->type == LYXP_SET_SCNODE_SET) {
904 trg->type = LYXP_SET_SCNODE_SET;
905 trg->used = src->used;
906 trg->size = src->used;
907
Michal Vasko08e9b112021-06-11 15:41:17 +0200908 if (trg->size) {
909 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
910 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
911 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
912 } else {
913 trg->val.scnodes = NULL;
914 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200915 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200916 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200917 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200918 set_fill_number(trg, src->val.num);
919 } else if (src->type == LYXP_SET_STRING) {
920 set_fill_string(trg, src->val.str, strlen(src->val.str));
921 } else {
922 if (trg->type == LYXP_SET_NODE_SET) {
923 free(trg->val.nodes);
924 } else if (trg->type == LYXP_SET_STRING) {
925 free(trg->val.str);
926 }
927
Michal Vaskod3678892020-05-21 10:06:58 +0200928 assert(src->type == LYXP_SET_NODE_SET);
929
930 trg->type = LYXP_SET_NODE_SET;
931 trg->used = src->used;
932 trg->size = src->used;
933 trg->ctx_pos = src->ctx_pos;
934 trg->ctx_size = src->ctx_size;
935
Michal Vasko08e9b112021-06-11 15:41:17 +0200936 if (trg->size) {
937 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
938 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
939 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
940 } else {
941 trg->val.nodes = NULL;
942 }
Michal Vaskod3678892020-05-21 10:06:58 +0200943 if (src->ht) {
944 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200945 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200946 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200947 }
948 }
949}
950
951/**
952 * @brief Clear context of all schema nodes.
953 *
954 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +0200955 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200956 */
957static void
Michal Vasko1a09b212021-05-06 13:00:10 +0200958set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200959{
960 uint32_t i;
961
962 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100963 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +0200964 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100965 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
966 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200967 }
968 }
969}
970
971/**
972 * @brief Remove a node from a set. Removing last node changes
973 * set into LYXP_SET_EMPTY. Context position aware.
974 *
975 * @param[in] set Set to use.
976 * @param[in] idx Index from @p set of the node to be removed.
977 */
978static void
979set_remove_node(struct lyxp_set *set, uint32_t idx)
980{
981 assert(set && (set->type == LYXP_SET_NODE_SET));
982 assert(idx < set->used);
983
984 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
985
986 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +0200987 if (idx < set->used) {
988 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
989 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +0200990 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200991 }
992}
993
994/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100995 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200996 *
997 * @param[in] set Set to use.
998 * @param[in] idx Index from @p set of the node to be removed.
999 */
1000static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001001set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001002{
1003 assert(set && (set->type == LYXP_SET_NODE_SET));
1004 assert(idx < set->used);
1005
Michal Vasko2caefc12019-11-14 16:07:56 +01001006 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1007 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1008 }
1009 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001010}
1011
1012/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001013 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001014 * set into LYXP_SET_EMPTY. Context position aware.
1015 *
1016 * @param[in] set Set to consolidate.
1017 */
1018static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001019set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001020{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001021 uint32_t i, orig_used, end = 0;
1022 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001023
Michal Vaskod3678892020-05-21 10:06:58 +02001024 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001025
1026 orig_used = set->used;
1027 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001028 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001029 start = -1;
1030 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001031 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001032 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001033 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001034 end = i;
1035 ++i;
1036 break;
1037 }
1038
1039 ++i;
1040 if (i == orig_used) {
1041 end = i;
1042 }
1043 } while (i < orig_used);
1044
1045 if (start > -1) {
1046 /* move the whole chunk of valid nodes together */
1047 if (set->used != (unsigned)start) {
1048 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1049 }
1050 set->used += end - start;
1051 }
1052 }
Michal Vasko57eab132019-09-24 11:46:26 +02001053}
1054
1055/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001056 * @brief Check for duplicates in a node set.
1057 *
1058 * @param[in] set Set to check.
1059 * @param[in] node Node to look for in @p set.
1060 * @param[in] node_type Type of @p node.
1061 * @param[in] skip_idx Index from @p set to skip.
1062 * @return LY_ERR
1063 */
1064static LY_ERR
1065set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1066{
1067 uint32_t i;
1068
Michal Vasko2caefc12019-11-14 16:07:56 +01001069 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001070 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1071 }
1072
1073 for (i = 0; i < set->used; ++i) {
1074 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1075 continue;
1076 }
1077
1078 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1079 return LY_EEXIST;
1080 }
1081 }
1082
1083 return LY_SUCCESS;
1084}
1085
Radek Krejci857189e2020-09-01 13:26:36 +02001086ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001087lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1088 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001089{
1090 uint32_t i;
1091
1092 for (i = 0; i < set->used; ++i) {
1093 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1094 continue;
1095 }
1096
1097 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001098 if (index_p) {
1099 *index_p = i;
1100 }
1101 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001102 }
1103 }
1104
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001105 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001106}
1107
Michal Vaskoecd62de2019-11-13 12:35:11 +01001108void
1109lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001110{
1111 uint32_t orig_used, i, j;
1112
Michal Vaskod3678892020-05-21 10:06:58 +02001113 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001114
Michal Vaskod3678892020-05-21 10:06:58 +02001115 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001116 return;
1117 }
1118
Michal Vaskod3678892020-05-21 10:06:58 +02001119 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001120 memcpy(set1, set2, sizeof *set1);
1121 return;
1122 }
1123
1124 if (set1->used + set2->used > set1->size) {
1125 set1->size = set1->used + set2->used;
1126 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1127 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1128 }
1129
1130 orig_used = set1->used;
1131
1132 for (i = 0; i < set2->used; ++i) {
1133 for (j = 0; j < orig_used; ++j) {
1134 /* detect duplicities */
1135 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1136 break;
1137 }
1138 }
1139
Michal Vasko0587bce2020-12-10 12:19:21 +01001140 if (j < orig_used) {
1141 /* node is there, but update its status if needed */
1142 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1143 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001144 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1145 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1146 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001147 }
1148 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001149 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1150 ++set1->used;
1151 }
1152 }
1153
Michal Vaskod3678892020-05-21 10:06:58 +02001154 lyxp_set_free_content(set2);
1155 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001156}
1157
1158/**
1159 * @brief Insert a node into a set. Context position aware.
1160 *
1161 * @param[in] set Set to use.
1162 * @param[in] node Node to insert to @p set.
1163 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1164 * @param[in] node_type Node type of @p node.
1165 * @param[in] idx Index in @p set to insert into.
1166 */
1167static void
1168set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1169{
Michal Vaskod3678892020-05-21 10:06:58 +02001170 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001171
Michal Vaskod3678892020-05-21 10:06:58 +02001172 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001173 /* first item */
1174 if (idx) {
1175 /* no real harm done, but it is a bug */
1176 LOGINT(set->ctx);
1177 idx = 0;
1178 }
1179 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1180 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1181 set->type = LYXP_SET_NODE_SET;
1182 set->used = 0;
1183 set->size = LYXP_SET_SIZE_START;
1184 set->ctx_pos = 1;
1185 set->ctx_size = 1;
1186 set->ht = NULL;
1187 } else {
1188 /* not an empty set */
1189 if (set->used == set->size) {
1190
1191 /* set is full */
1192 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1193 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1194 set->size += LYXP_SET_SIZE_STEP;
1195 }
1196
1197 if (idx > set->used) {
1198 LOGINT(set->ctx);
1199 idx = set->used;
1200 }
1201
1202 /* make space for the new node */
1203 if (idx < set->used) {
1204 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1205 }
1206 }
1207
1208 /* finally assign the value */
1209 set->val.nodes[idx].node = (struct lyd_node *)node;
1210 set->val.nodes[idx].type = node_type;
1211 set->val.nodes[idx].pos = pos;
1212 ++set->used;
1213
Michal Vasko2caefc12019-11-14 16:07:56 +01001214 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1215 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1216 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001217}
1218
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001219LY_ERR
1220lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001221{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001222 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001223
1224 assert(set->type == LYXP_SET_SCNODE_SET);
1225
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001226 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001227 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001228 } else {
1229 if (set->used == set->size) {
1230 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes);
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001231 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001232 set->size += LYXP_SET_SIZE_STEP;
1233 }
1234
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001235 index = set->used;
1236 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1237 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001238 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001239 ++set->used;
1240 }
1241
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001242 if (index_p) {
1243 *index_p = index;
1244 }
1245
1246 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001247}
1248
1249/**
1250 * @brief Replace a node in a set with another. Context position aware.
1251 *
1252 * @param[in] set Set to use.
1253 * @param[in] node Node to insert to @p set.
1254 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1255 * @param[in] node_type Node type of @p node.
1256 * @param[in] idx Index in @p set of the node to replace.
1257 */
1258static void
1259set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1260{
1261 assert(set && (idx < set->used));
1262
Michal Vasko2caefc12019-11-14 16:07:56 +01001263 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1264 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1265 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001266 set->val.nodes[idx].node = (struct lyd_node *)node;
1267 set->val.nodes[idx].type = node_type;
1268 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001269 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1270 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1271 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001272}
1273
1274/**
1275 * @brief Set all nodes with ctx 1 to a new unique context value.
1276 *
1277 * @param[in] set Set to modify.
1278 * @return New context value.
1279 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001280static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001281set_scnode_new_in_ctx(struct lyxp_set *set)
1282{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001283 uint32_t i;
1284 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001285
1286 assert(set->type == LYXP_SET_SCNODE_SET);
1287
Radek Krejcif13b87b2020-12-01 22:02:17 +01001288 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001289retry:
1290 for (i = 0; i < set->used; ++i) {
1291 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1292 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1293 goto retry;
1294 }
1295 }
1296 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001297 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001298 set->val.scnodes[i].in_ctx = ret_ctx;
1299 }
1300 }
1301
1302 return ret_ctx;
1303}
1304
1305/**
1306 * @brief Get unique @p node position in the data.
1307 *
1308 * @param[in] node Node to find.
1309 * @param[in] node_type Node type of @p node.
1310 * @param[in] root Root node.
1311 * @param[in] root_type Type of the XPath @p root node.
1312 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1313 * be used to increase efficiency and start the DFS from this node.
1314 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1315 * @return Node position.
1316 */
1317static uint32_t
1318get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root,
Radek Krejci0f969882020-08-21 16:56:47 +02001319 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001320{
Michal Vasko56daf732020-08-10 10:57:18 +02001321 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001322 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001323 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001324
1325 assert(prev && prev_pos && !root->prev->next);
1326
1327 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1328 return 0;
1329 }
1330
1331 if (*prev) {
1332 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001333 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001334 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001335 goto dfs_search;
1336 }
1337
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001338 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001339 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001340dfs_search:
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001341 LYD_TREE_DFS_continue = 0;
1342
Michal Vasko56daf732020-08-10 10:57:18 +02001343 if (*prev && !elem) {
1344 /* resume previous DFS */
1345 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1346 LYD_TREE_DFS_continue = 0;
1347 }
1348
Michal Vasko03ff5a72019-09-11 13:49:33 +02001349 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001350 /* skip */
1351 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001352 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001353 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001354 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001355 break;
1356 }
Michal Vasko56daf732020-08-10 10:57:18 +02001357 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001358 }
Michal Vasko56daf732020-08-10 10:57:18 +02001359
1360 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001361 }
1362
1363 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001364 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001365 break;
1366 }
1367 }
1368
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001369 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001370 if (!(*prev)) {
1371 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001372 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001373 return 0;
1374 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001375 /* start the search again from the beginning */
1376 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001377
Michal Vasko56daf732020-08-10 10:57:18 +02001378 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001379 pos = 1;
1380 goto dfs_search;
1381 }
1382 }
1383
1384 /* remember the last found node for next time */
1385 *prev = node;
1386 *prev_pos = pos;
1387
1388 return pos;
1389}
1390
1391/**
1392 * @brief Assign (fill) missing node positions.
1393 *
1394 * @param[in] set Set to fill positions in.
1395 * @param[in] root Context root node.
1396 * @param[in] root_type Context root type.
1397 * @return LY_ERR
1398 */
1399static LY_ERR
1400set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1401{
1402 const struct lyd_node *prev = NULL, *tmp_node;
1403 uint32_t i, tmp_pos = 0;
1404
1405 for (i = 0; i < set->used; ++i) {
1406 if (!set->val.nodes[i].pos) {
1407 tmp_node = NULL;
1408 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001409 case LYXP_NODE_META:
1410 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001411 if (!tmp_node) {
1412 LOGINT_RET(root->schema->module->ctx);
1413 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001414 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001415 case LYXP_NODE_ELEM:
1416 case LYXP_NODE_TEXT:
1417 if (!tmp_node) {
1418 tmp_node = set->val.nodes[i].node;
1419 }
1420 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1421 break;
1422 default:
1423 /* all roots have position 0 */
1424 break;
1425 }
1426 }
1427 }
1428
1429 return LY_SUCCESS;
1430}
1431
1432/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001433 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001434 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001435 * @param[in] meta Metadata to use.
1436 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001437 */
1438static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001439get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001440{
1441 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001442 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001443
Michal Vasko9f96a052020-03-10 09:41:45 +01001444 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001445 ++pos;
1446 }
1447
Michal Vasko9f96a052020-03-10 09:41:45 +01001448 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001449 return pos;
1450}
1451
1452/**
1453 * @brief Compare 2 nodes in respect to XPath document order.
1454 *
1455 * @param[in] item1 1st node.
1456 * @param[in] item2 2nd node.
1457 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1458 */
1459static int
1460set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1461{
Michal Vasko9f96a052020-03-10 09:41:45 +01001462 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001463
1464 if (item1->pos < item2->pos) {
1465 return -1;
1466 }
1467
1468 if (item1->pos > item2->pos) {
1469 return 1;
1470 }
1471
1472 /* node positions are equal, the fun case */
1473
1474 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1475 /* special case since text nodes are actually saved as their parents */
1476 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1477 if (item1->type == LYXP_NODE_ELEM) {
1478 assert(item2->type == LYXP_NODE_TEXT);
1479 return -1;
1480 } else {
1481 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1482 return 1;
1483 }
1484 }
1485
Michal Vasko9f96a052020-03-10 09:41:45 +01001486 /* we need meta positions now */
1487 if (item1->type == LYXP_NODE_META) {
1488 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001489 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001490 if (item2->type == LYXP_NODE_META) {
1491 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001492 }
1493
Michal Vasko9f96a052020-03-10 09:41:45 +01001494 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001495 /* check for duplicates */
1496 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001497 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001498 return 0;
1499 }
1500
Michal Vasko9f96a052020-03-10 09:41:45 +01001501 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001502 /* elem is always first, 2nd node is after it */
1503 if (item1->type == LYXP_NODE_ELEM) {
1504 assert(item2->type != LYXP_NODE_ELEM);
1505 return -1;
1506 }
1507
Michal Vasko9f96a052020-03-10 09:41:45 +01001508 /* 1st TEXT - 2nd ELEM, 1st TEXT - any pos - 2nd META, 1st META - any pos - 2nd ELEM, 1st META - >pos> - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001509 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001510 if (((item1->type == LYXP_NODE_TEXT) &&
1511 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1512 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1513 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1514 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001515 return 1;
1516 }
1517
Michal Vasko9f96a052020-03-10 09:41:45 +01001518 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001519 /* 2nd is after 1st */
1520 return -1;
1521}
1522
1523/**
1524 * @brief Set cast for comparisons.
1525 *
1526 * @param[in] trg Target set to cast source into.
1527 * @param[in] src Source set.
1528 * @param[in] type Target set type.
1529 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001530 * @return LY_ERR
1531 */
1532static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001533set_comp_cast(struct lyxp_set *trg, struct lyxp_set *src, enum lyxp_set_type type, uint32_t src_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001534{
1535 assert(src->type == LYXP_SET_NODE_SET);
1536
1537 set_init(trg, src);
1538
1539 /* insert node into target set */
1540 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1541
1542 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001543 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001544}
1545
Michal Vasko4c7763f2020-07-27 17:40:37 +02001546/**
1547 * @brief Set content canonization for comparisons.
1548 *
1549 * @param[in] trg Target set to put the canononized source into.
1550 * @param[in] src Source set.
1551 * @param[in] xp_node Source XPath node/meta to use for canonization.
1552 * @return LY_ERR
1553 */
1554static LY_ERR
1555set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1556{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001557 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001558 struct lyd_value val;
1559 struct ly_err_item *err = NULL;
1560 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001561 LY_ERR rc;
1562
1563 /* is there anything to canonize even? */
1564 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1565 /* do we have a type to use for canonization? */
1566 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1567 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1568 } else if (xp_node->type == LYXP_NODE_META) {
1569 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1570 }
1571 }
1572 if (!type) {
1573 goto fill;
1574 }
1575
1576 if (src->type == LYXP_SET_NUMBER) {
1577 /* canonize number */
1578 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1579 LOGMEM(src->ctx);
1580 return LY_EMEM;
1581 }
1582 } else {
1583 /* canonize string */
1584 str = strdup(src->val.str);
1585 }
1586
1587 /* ignore errors, the value may not satisfy schema constraints */
Radek Krejci0b013302021-03-29 15:22:32 +02001588 rc = type->plugin->store(src->ctx, type, str, strlen(str), LYPLG_TYPE_STORE_DYNAMIC, src->format, src->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001589 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001590 ly_err_free(err);
1591 if (rc) {
1592 /* invalid value */
Radek IÅ¡a48460222021-03-02 15:18:32 +01001593 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001594 goto fill;
1595 }
1596
Michal Vasko4c7763f2020-07-27 17:40:37 +02001597 /* use the canonized value */
1598 set_init(trg, src);
1599 trg->type = src->type;
1600 if (src->type == LYXP_SET_NUMBER) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001601 trg->val.num = strtold(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001602 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1603 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001604 trg->val.str = strdup(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko4c7763f2020-07-27 17:40:37 +02001605 }
1606 type->plugin->free(src->ctx, &val);
1607 return LY_SUCCESS;
1608
1609fill:
1610 /* no canonization needed/possible */
1611 set_fill_set(trg, src);
1612 return LY_SUCCESS;
1613}
1614
Michal Vasko03ff5a72019-09-11 13:49:33 +02001615#ifndef NDEBUG
1616
1617/**
1618 * @brief Bubble sort @p set into XPath document order.
1619 * Context position aware. Unused in the 'Release' build target.
1620 *
1621 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001622 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1623 */
1624static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001625set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001626{
1627 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001628 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001629 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001630 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001631 struct lyxp_set_node item;
1632 struct lyxp_set_hash_node hnode;
1633 uint64_t hash;
1634
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001635 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001636 return 0;
1637 }
1638
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001639 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001640 for (root = set->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001641 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001642
1643 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001644 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001645 return -1;
1646 }
1647
1648 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1649 print_set_debug(set);
1650
1651 for (i = 0; i < set->used; ++i) {
1652 inverted = 0;
1653 change = 0;
1654
1655 for (j = 1; j < set->used - i; ++j) {
1656 /* compare node positions */
1657 if (inverted) {
1658 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1659 } else {
1660 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1661 }
1662
1663 /* swap if needed */
1664 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1665 change = 1;
1666
1667 item = set->val.nodes[j - 1];
1668 set->val.nodes[j - 1] = set->val.nodes[j];
1669 set->val.nodes[j] = item;
1670 } else {
1671 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1672 inverted = !inverted;
1673 }
1674 }
1675
1676 ++ret;
1677
1678 if (!change) {
1679 break;
1680 }
1681 }
1682
1683 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1684 print_set_debug(set);
1685
1686 /* check node hashes */
1687 if (set->used >= LYD_HT_MIN_ITEMS) {
1688 assert(set->ht);
1689 for (i = 0; i < set->used; ++i) {
1690 hnode.node = set->val.nodes[i].node;
1691 hnode.type = set->val.nodes[i].type;
1692
1693 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1694 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1695 hash = dict_hash_multi(hash, NULL, 0);
1696
1697 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1698 }
1699 }
1700
1701 return ret - 1;
1702}
1703
1704/**
1705 * @brief Remove duplicate entries in a sorted node set.
1706 *
1707 * @param[in] set Sorted set to check.
1708 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1709 */
1710static LY_ERR
1711set_sorted_dup_node_clean(struct lyxp_set *set)
1712{
1713 uint32_t i = 0;
1714 LY_ERR ret = LY_SUCCESS;
1715
1716 if (set->used > 1) {
1717 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001718 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1719 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001720 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001721 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001722 }
Michal Vasko57eab132019-09-24 11:46:26 +02001723 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001724 }
1725 }
1726
Michal Vasko2caefc12019-11-14 16:07:56 +01001727 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001728 return ret;
1729}
1730
1731#endif
1732
1733/**
1734 * @brief Merge 2 sorted sets into one.
1735 *
1736 * @param[in,out] trg Set to merge into. Duplicates are removed.
1737 * @param[in] src Set to be merged into @p trg. It is cast to #LYXP_SET_EMPTY on success.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001738 * @return LY_ERR
1739 */
1740static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001741set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001742{
1743 uint32_t i, j, k, count, dup_count;
1744 int cmp;
1745 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001746
Michal Vaskod3678892020-05-21 10:06:58 +02001747 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001748 return LY_EINVAL;
1749 }
1750
Michal Vaskod3678892020-05-21 10:06:58 +02001751 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001752 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001753 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001754 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001755 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001756 return LY_SUCCESS;
1757 }
1758
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001759 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001760 for (root = trg->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001761 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001762
1763 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001764 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001765 return LY_EINT;
1766 }
1767
1768#ifndef NDEBUG
1769 LOGDBG(LY_LDGXPATH, "MERGE target");
1770 print_set_debug(trg);
1771 LOGDBG(LY_LDGXPATH, "MERGE source");
1772 print_set_debug(src);
1773#endif
1774
1775 /* make memory for the merge (duplicates are not detected yet, so space
1776 * will likely be wasted on them, too bad) */
1777 if (trg->size - trg->used < src->used) {
1778 trg->size = trg->used + src->used;
1779
1780 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1781 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1782 }
1783
1784 i = 0;
1785 j = 0;
1786 count = 0;
1787 dup_count = 0;
1788 do {
1789 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1790 if (!cmp) {
1791 if (!count) {
1792 /* duplicate, just skip it */
1793 ++i;
1794 ++j;
1795 } else {
1796 /* we are copying something already, so let's copy the duplicate too,
1797 * we are hoping that afterwards there are some more nodes to
1798 * copy and this way we can copy them all together */
1799 ++count;
1800 ++dup_count;
1801 ++i;
1802 ++j;
1803 }
1804 } else if (cmp < 0) {
1805 /* inserting src node into trg, just remember it for now */
1806 ++count;
1807 ++i;
1808
1809 /* insert the hash now */
1810 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1811 } else if (count) {
1812copy_nodes:
1813 /* time to actually copy the nodes, we have found the largest block of nodes */
1814 memmove(&trg->val.nodes[j + (count - dup_count)],
1815 &trg->val.nodes[j],
1816 (trg->used - j) * sizeof *trg->val.nodes);
1817 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1818
1819 trg->used += count - dup_count;
1820 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1821 j += count - dup_count;
1822
1823 count = 0;
1824 dup_count = 0;
1825 } else {
1826 ++j;
1827 }
1828 } while ((i < src->used) && (j < trg->used));
1829
1830 if ((i < src->used) || count) {
1831 /* insert all the hashes first */
1832 for (k = i; k < src->used; ++k) {
1833 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1834 }
1835
1836 /* loop ended, but we need to copy something at trg end */
1837 count += src->used - i;
1838 i = src->used;
1839 goto copy_nodes;
1840 }
1841
1842 /* we are inserting hashes before the actual node insert, which causes
1843 * situations when there were initially not enough items for a hash table,
1844 * but even after some were inserted, hash table was not created (during
1845 * insertion the number of items is not updated yet) */
1846 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1847 set_insert_node_hash(trg, NULL, 0);
1848 }
1849
1850#ifndef NDEBUG
1851 LOGDBG(LY_LDGXPATH, "MERGE result");
1852 print_set_debug(trg);
1853#endif
1854
Michal Vaskod3678892020-05-21 10:06:58 +02001855 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001856 return LY_SUCCESS;
1857}
1858
1859/*
1860 * (re)parse functions
1861 *
1862 * Parse functions parse the expression into
1863 * tokens (syntactic analysis).
1864 *
1865 * Reparse functions perform semantic analysis
1866 * (do not save the result, just a check) of
1867 * the expression and fill repeat indices.
1868 */
1869
Michal Vasko14676352020-05-29 11:35:55 +02001870LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001871lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001872{
Michal Vasko004d3152020-06-11 19:59:22 +02001873 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001874 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001875 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001876 }
Michal Vasko14676352020-05-29 11:35:55 +02001877 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001878 }
1879
Michal Vasko004d3152020-06-11 19:59:22 +02001880 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001881 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001882 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001883 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001884 }
Michal Vasko14676352020-05-29 11:35:55 +02001885 return LY_ENOT;
1886 }
1887
1888 return LY_SUCCESS;
1889}
1890
Michal Vasko004d3152020-06-11 19:59:22 +02001891LY_ERR
1892lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1893{
1894 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1895
1896 /* skip the token */
1897 ++(*tok_idx);
1898
1899 return LY_SUCCESS;
1900}
1901
Michal Vasko14676352020-05-29 11:35:55 +02001902/* just like lyxp_check_token() but tests for 2 tokens */
1903static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001904exp_check_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok1,
Radek Krejci0f969882020-08-21 16:56:47 +02001905 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001906{
Michal Vasko004d3152020-06-11 19:59:22 +02001907 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001908 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001909 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001910 }
1911 return LY_EINCOMPLETE;
1912 }
1913
Michal Vasko004d3152020-06-11 19:59:22 +02001914 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001915 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001916 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001917 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001918 }
1919 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001920 }
1921
1922 return LY_SUCCESS;
1923}
1924
Michal Vasko4911eeb2021-06-28 11:23:05 +02001925LY_ERR
1926lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok1,
1927 enum lyxp_token want_tok2)
1928{
1929 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
1930
1931 /* skip the token */
1932 ++(*tok_idx);
1933
1934 return LY_SUCCESS;
1935}
1936
Michal Vasko03ff5a72019-09-11 13:49:33 +02001937/**
1938 * @brief Stack operation push on the repeat array.
1939 *
1940 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001941 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001942 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1943 */
1944static void
Michal Vasko004d3152020-06-11 19:59:22 +02001945exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001946{
1947 uint16_t i;
1948
Michal Vasko004d3152020-06-11 19:59:22 +02001949 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001950 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001951 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1952 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1953 exp->repeat[tok_idx][i] = repeat_op_idx;
1954 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001955 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001956 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1957 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1958 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001959 }
1960}
1961
1962/**
1963 * @brief Reparse Predicate. Logs directly on error.
1964 *
1965 * [7] Predicate ::= '[' Expr ']'
1966 *
1967 * @param[in] ctx Context for logging.
1968 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001969 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02001970 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001971 * @return LY_ERR
1972 */
1973static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02001974reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001975{
1976 LY_ERR rc;
1977
Michal Vasko004d3152020-06-11 19:59:22 +02001978 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001979 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001980 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001981
aPiecekbf968d92021-05-27 14:35:05 +02001982 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001983 LY_CHECK_RET(rc);
1984
Michal Vasko004d3152020-06-11 19:59:22 +02001985 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001986 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001987 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001988
1989 return LY_SUCCESS;
1990}
1991
1992/**
1993 * @brief Reparse RelativeLocationPath. Logs directly on error.
1994 *
1995 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1996 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1997 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1998 *
1999 * @param[in] ctx Context for logging.
2000 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002001 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002002 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002003 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
2004 */
2005static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002006reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002007{
2008 LY_ERR rc;
2009
Michal Vasko004d3152020-06-11 19:59:22 +02002010 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002011 LY_CHECK_RET(rc);
2012
2013 goto step;
2014 do {
2015 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002016 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002017
Michal Vasko004d3152020-06-11 19:59:22 +02002018 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002019 LY_CHECK_RET(rc);
2020step:
2021 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002022 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002023 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002024 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002025 break;
2026
2027 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002028 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002029 break;
2030
2031 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002032 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002033
Michal Vasko004d3152020-06-11 19:59:22 +02002034 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002035 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002036 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002037 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002038 return LY_EVALID;
2039 }
Radek Krejci0f969882020-08-21 16:56:47 +02002040 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002041 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002042 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002043 goto reparse_predicate;
2044 break;
2045
2046 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002047 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002048
2049 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002050 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002051 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002052 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002053
2054 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002055 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002056 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002057 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002058
2059reparse_predicate:
2060 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002061 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002062 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002063 LY_CHECK_RET(rc);
2064 }
2065 break;
2066 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002067 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002068 return LY_EVALID;
2069 }
Michal Vasko004d3152020-06-11 19:59:22 +02002070 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002071
2072 return LY_SUCCESS;
2073}
2074
2075/**
2076 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2077 *
2078 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2079 *
2080 * @param[in] ctx Context for logging.
2081 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002082 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002083 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002084 * @return LY_ERR
2085 */
2086static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002087reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002088{
2089 LY_ERR rc;
2090
Michal Vasko004d3152020-06-11 19:59:22 +02002091 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002092
2093 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002094 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002095 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002096 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002097
Michal Vasko004d3152020-06-11 19:59:22 +02002098 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002099 return LY_SUCCESS;
2100 }
Michal Vasko004d3152020-06-11 19:59:22 +02002101 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002102 case LYXP_TOKEN_DOT:
2103 case LYXP_TOKEN_DDOT:
2104 case LYXP_TOKEN_AT:
2105 case LYXP_TOKEN_NAMETEST:
2106 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002107 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002108 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002109 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002110 default:
2111 break;
2112 }
2113
Michal Vasko03ff5a72019-09-11 13:49:33 +02002114 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002115 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002116 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002117
aPiecekbf968d92021-05-27 14:35:05 +02002118 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002119 LY_CHECK_RET(rc);
2120 }
2121
2122 return LY_SUCCESS;
2123}
2124
2125/**
2126 * @brief Reparse FunctionCall. Logs directly on error.
2127 *
2128 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2129 *
2130 * @param[in] ctx Context for logging.
2131 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002132 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002133 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002134 * @return LY_ERR
2135 */
2136static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002137reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002138{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002139 int8_t min_arg_count = -1;
2140 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002141 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002142 LY_ERR rc;
2143
Michal Vasko004d3152020-06-11 19:59:22 +02002144 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002145 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002146 func_tok_idx = *tok_idx;
2147 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002148 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002149 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002150 min_arg_count = 1;
2151 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002152 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002153 min_arg_count = 1;
2154 max_arg_count = 1;
2155 }
2156 break;
2157 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002158 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002159 min_arg_count = 1;
2160 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002161 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002162 min_arg_count = 0;
2163 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002164 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002165 min_arg_count = 0;
2166 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002167 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002168 min_arg_count = 0;
2169 max_arg_count = 0;
2170 }
2171 break;
2172 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002173 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002174 min_arg_count = 1;
2175 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002176 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002177 min_arg_count = 0;
2178 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002179 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002180 min_arg_count = 1;
2181 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002182 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002183 min_arg_count = 1;
2184 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002185 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002186 min_arg_count = 1;
2187 max_arg_count = 1;
2188 }
2189 break;
2190 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002191 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002192 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002193 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002194 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002195 min_arg_count = 0;
2196 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002197 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002198 min_arg_count = 0;
2199 max_arg_count = 1;
2200 }
2201 break;
2202 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002203 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002204 min_arg_count = 1;
2205 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002206 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002207 min_arg_count = 1;
2208 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002209 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002210 min_arg_count = 0;
2211 max_arg_count = 0;
2212 }
2213 break;
2214 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002215 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002216 min_arg_count = 2;
2217 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002218 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002219 min_arg_count = 0;
2220 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002221 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002222 min_arg_count = 2;
2223 max_arg_count = 2;
2224 }
2225 break;
2226 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002227 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002228 min_arg_count = 2;
2229 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002230 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002231 min_arg_count = 3;
2232 max_arg_count = 3;
2233 }
2234 break;
2235 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002236 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002237 min_arg_count = 0;
2238 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002239 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002240 min_arg_count = 1;
2241 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002242 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002243 min_arg_count = 2;
2244 max_arg_count = 2;
2245 }
2246 break;
2247 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002248 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002249 min_arg_count = 2;
2250 max_arg_count = 2;
2251 }
2252 break;
2253 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002254 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002255 min_arg_count = 2;
2256 max_arg_count = 2;
2257 }
2258 break;
2259 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002260 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002261 min_arg_count = 0;
2262 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002263 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002264 min_arg_count = 0;
2265 max_arg_count = 1;
2266 }
2267 break;
2268 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002269 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002270 min_arg_count = 0;
2271 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002272 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002273 min_arg_count = 2;
2274 max_arg_count = 2;
2275 }
2276 break;
2277 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002278 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002279 min_arg_count = 2;
2280 max_arg_count = 2;
2281 }
2282 break;
2283 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002284 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002285 min_arg_count = 2;
2286 max_arg_count = 2;
2287 }
2288 break;
2289 }
2290 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002291 LOGVAL(ctx, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002292 return LY_EINVAL;
2293 }
Michal Vasko004d3152020-06-11 19:59:22 +02002294 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002295
2296 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002297 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002298 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002299 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002300
2301 /* ( Expr ( ',' Expr )* )? */
2302 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002303 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002304 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002305 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002306 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002307 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002308 LY_CHECK_RET(rc);
2309 }
Michal Vasko004d3152020-06-11 19:59:22 +02002310 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2311 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002312
2313 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002314 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002315 LY_CHECK_RET(rc);
2316 }
2317
2318 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002319 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002320 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002321 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002322
Radek Krejci857189e2020-09-01 13:26:36 +02002323 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002324 LOGVAL(ctx, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002325 return LY_EVALID;
2326 }
2327
2328 return LY_SUCCESS;
2329}
2330
2331/**
2332 * @brief Reparse PathExpr. Logs directly on error.
2333 *
2334 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2335 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2336 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2337 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2338 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2339 *
2340 * @param[in] ctx Context for logging.
2341 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002342 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002343 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002344 * @return LY_ERR
2345 */
2346static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002347reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002348{
2349 LY_ERR rc;
2350
Michal Vasko004d3152020-06-11 19:59:22 +02002351 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002352 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002353 }
2354
Michal Vasko004d3152020-06-11 19:59:22 +02002355 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002356 case LYXP_TOKEN_PAR1:
2357 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002358 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002359
aPiecekbf968d92021-05-27 14:35:05 +02002360 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002361 LY_CHECK_RET(rc);
2362
Michal Vasko004d3152020-06-11 19:59:22 +02002363 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002364 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002365 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002366 goto predicate;
2367 break;
2368 case LYXP_TOKEN_DOT:
2369 case LYXP_TOKEN_DDOT:
2370 case LYXP_TOKEN_AT:
2371 case LYXP_TOKEN_NAMETEST:
2372 case LYXP_TOKEN_NODETYPE:
2373 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002374 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002375 LY_CHECK_RET(rc);
2376 break;
2377 case LYXP_TOKEN_FUNCNAME:
2378 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002379 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002380 LY_CHECK_RET(rc);
2381 goto predicate;
2382 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002383 case LYXP_TOKEN_OPER_PATH:
2384 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002385 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002386 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002387 LY_CHECK_RET(rc);
2388 break;
2389 case LYXP_TOKEN_LITERAL:
2390 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002391 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002392 goto predicate;
2393 break;
2394 case LYXP_TOKEN_NUMBER:
2395 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002396 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002397 goto predicate;
2398 break;
2399 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002400 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002401 return LY_EVALID;
2402 }
2403
2404 return LY_SUCCESS;
2405
2406predicate:
2407 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002408 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002409 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002410 LY_CHECK_RET(rc);
2411 }
2412
2413 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002414 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002415
2416 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002417 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002418
aPiecekbf968d92021-05-27 14:35:05 +02002419 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002420 LY_CHECK_RET(rc);
2421 }
2422
2423 return LY_SUCCESS;
2424}
2425
2426/**
2427 * @brief Reparse UnaryExpr. Logs directly on error.
2428 *
2429 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2430 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2431 *
2432 * @param[in] ctx Context for logging.
2433 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002434 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002435 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002436 * @return LY_ERR
2437 */
2438static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002439reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002440{
2441 uint16_t prev_exp;
2442 LY_ERR rc;
2443
2444 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002445 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002446 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2447 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002448 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002449 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002450 }
2451
2452 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002453 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002454 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002455 LY_CHECK_RET(rc);
2456
2457 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002458 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002459 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002460 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002461
aPiecekbf968d92021-05-27 14:35:05 +02002462 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002463 LY_CHECK_RET(rc);
2464 }
2465
2466 return LY_SUCCESS;
2467}
2468
2469/**
2470 * @brief Reparse AdditiveExpr. Logs directly on error.
2471 *
2472 * [15] AdditiveExpr ::= MultiplicativeExpr
2473 * | AdditiveExpr '+' MultiplicativeExpr
2474 * | AdditiveExpr '-' MultiplicativeExpr
2475 * [16] MultiplicativeExpr ::= UnaryExpr
2476 * | MultiplicativeExpr '*' UnaryExpr
2477 * | MultiplicativeExpr 'div' UnaryExpr
2478 * | MultiplicativeExpr 'mod' UnaryExpr
2479 *
2480 * @param[in] ctx Context for logging.
2481 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002482 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002483 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002484 * @return LY_ERR
2485 */
2486static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002487reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002488{
2489 uint16_t prev_add_exp, prev_mul_exp;
2490 LY_ERR rc;
2491
Michal Vasko004d3152020-06-11 19:59:22 +02002492 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002493 goto reparse_multiplicative_expr;
2494
2495 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002496 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2497 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002498 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002499 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002500
2501reparse_multiplicative_expr:
2502 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002503 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002504 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002505 LY_CHECK_RET(rc);
2506
2507 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002508 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2509 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002510 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002511 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002512
aPiecekbf968d92021-05-27 14:35:05 +02002513 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002514 LY_CHECK_RET(rc);
2515 }
2516 }
2517
2518 return LY_SUCCESS;
2519}
2520
2521/**
2522 * @brief Reparse EqualityExpr. Logs directly on error.
2523 *
2524 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2525 * | EqualityExpr '!=' RelationalExpr
2526 * [14] RelationalExpr ::= AdditiveExpr
2527 * | RelationalExpr '<' AdditiveExpr
2528 * | RelationalExpr '>' AdditiveExpr
2529 * | RelationalExpr '<=' AdditiveExpr
2530 * | RelationalExpr '>=' AdditiveExpr
2531 *
2532 * @param[in] ctx Context for logging.
2533 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002534 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002535 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002536 * @return LY_ERR
2537 */
2538static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002539reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002540{
2541 uint16_t prev_eq_exp, prev_rel_exp;
2542 LY_ERR rc;
2543
Michal Vasko004d3152020-06-11 19:59:22 +02002544 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002545 goto reparse_additive_expr;
2546
2547 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002548 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002549 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002550 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002551
2552reparse_additive_expr:
2553 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002554 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002555 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002556 LY_CHECK_RET(rc);
2557
2558 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002559 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002560 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002561 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002562
aPiecekbf968d92021-05-27 14:35:05 +02002563 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002564 LY_CHECK_RET(rc);
2565 }
2566 }
2567
2568 return LY_SUCCESS;
2569}
2570
2571/**
2572 * @brief Reparse OrExpr. Logs directly on error.
2573 *
2574 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2575 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2576 *
2577 * @param[in] ctx Context for logging.
2578 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002579 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002580 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002581 * @return LY_ERR
2582 */
2583static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002584reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002585{
2586 uint16_t prev_or_exp, prev_and_exp;
2587 LY_ERR rc;
2588
aPiecekbf968d92021-05-27 14:35:05 +02002589 ++depth;
2590 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2591
Michal Vasko004d3152020-06-11 19:59:22 +02002592 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002593 goto reparse_equality_expr;
2594
2595 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002596 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002597 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002598 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002599
2600reparse_equality_expr:
2601 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002602 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002603 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002604 LY_CHECK_RET(rc);
2605
2606 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002607 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002608 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002609 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002610
aPiecekbf968d92021-05-27 14:35:05 +02002611 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002612 LY_CHECK_RET(rc);
2613 }
2614 }
2615
2616 return LY_SUCCESS;
2617}
Radek Krejcib1646a92018-11-02 16:08:26 +01002618
2619/**
2620 * @brief Parse NCName.
2621 *
2622 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002623 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002624 */
Radek Krejcid4270262019-01-07 15:07:25 +01002625static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002626parse_ncname(const char *ncname)
2627{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002628 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002629 size_t size;
2630 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002631
2632 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2633 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2634 return len;
2635 }
2636
2637 do {
2638 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002639 if (!*ncname) {
2640 break;
2641 }
Radek Krejcid4270262019-01-07 15:07:25 +01002642 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002643 } while (is_xmlqnamechar(uc) && (uc != ':'));
2644
2645 return len;
2646}
2647
2648/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002649 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002650 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002651 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002652 * @param[in] exp Expression to use.
2653 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002654 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002655 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002656 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002657 */
2658static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002659exp_add_token(const struct ly_ctx *ctx, struct lyxp_expr *exp, enum lyxp_token token, uint16_t tok_pos, uint16_t tok_len)
Radek Krejcib1646a92018-11-02 16:08:26 +01002660{
2661 uint32_t prev;
2662
2663 if (exp->used == exp->size) {
2664 prev = exp->size;
2665 exp->size += LYXP_EXPR_SIZE_STEP;
2666 if (prev > exp->size) {
2667 LOGINT(ctx);
2668 return LY_EINT;
2669 }
2670
2671 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2672 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2673 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2674 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2675 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2676 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2677 }
2678
2679 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002680 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002681 exp->tok_len[exp->used] = tok_len;
2682 ++exp->used;
2683 return LY_SUCCESS;
2684}
2685
2686void
Michal Vasko14676352020-05-29 11:35:55 +02002687lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002688{
2689 uint16_t i;
2690
2691 if (!expr) {
2692 return;
2693 }
2694
2695 lydict_remove(ctx, expr->expr);
2696 free(expr->tokens);
2697 free(expr->tok_pos);
2698 free(expr->tok_len);
2699 if (expr->repeat) {
2700 for (i = 0; i < expr->used; ++i) {
2701 free(expr->repeat[i]);
2702 }
2703 }
2704 free(expr->repeat);
2705 free(expr);
2706}
2707
Radek Krejcif03a9e22020-09-18 20:09:31 +02002708LY_ERR
2709lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr_str, size_t expr_len, ly_bool reparse, struct lyxp_expr **expr_p)
Radek Krejcib1646a92018-11-02 16:08:26 +01002710{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002711 LY_ERR ret = LY_SUCCESS;
2712 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002713 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002714 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002715 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002716 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002717
Radek Krejcif03a9e22020-09-18 20:09:31 +02002718 assert(expr_p);
2719
2720 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002721 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002722 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002723 }
2724
2725 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002726 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002727 }
2728 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002729 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002730 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002731 }
2732
2733 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002734 expr = calloc(1, sizeof *expr);
2735 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2736 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2737 expr->used = 0;
2738 expr->size = LYXP_EXPR_SIZE_START;
2739 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2740 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002741
Radek Krejcif03a9e22020-09-18 20:09:31 +02002742 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2743 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002744
Radek Krejcif03a9e22020-09-18 20:09:31 +02002745 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2746 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002747
Michal Vasko004d3152020-06-11 19:59:22 +02002748 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002749 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002750
Radek Krejcif03a9e22020-09-18 20:09:31 +02002751 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002752 ++parsed;
2753 }
2754
2755 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002756 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002757
2758 /* '(' */
2759 tok_len = 1;
2760 tok_type = LYXP_TOKEN_PAR1;
2761
Radek Krejcif03a9e22020-09-18 20:09:31 +02002762 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002763 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002764 if (((expr->tok_len[expr->used - 1] == 4) &&
2765 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2766 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2767 ((expr->tok_len[expr->used - 1] == 7) &&
2768 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002769 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002770 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002771 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002772 }
2773 prev_function_check = 0;
2774 }
2775
Radek Krejcif03a9e22020-09-18 20:09:31 +02002776 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002777
2778 /* ')' */
2779 tok_len = 1;
2780 tok_type = LYXP_TOKEN_PAR2;
2781
Radek Krejcif03a9e22020-09-18 20:09:31 +02002782 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002783
2784 /* '[' */
2785 tok_len = 1;
2786 tok_type = LYXP_TOKEN_BRACK1;
2787
Radek Krejcif03a9e22020-09-18 20:09:31 +02002788 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002789
2790 /* ']' */
2791 tok_len = 1;
2792 tok_type = LYXP_TOKEN_BRACK2;
2793
Radek Krejcif03a9e22020-09-18 20:09:31 +02002794 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002795
2796 /* '..' */
2797 tok_len = 2;
2798 tok_type = LYXP_TOKEN_DDOT;
2799
Radek Krejcif03a9e22020-09-18 20:09:31 +02002800 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002801
2802 /* '.' */
2803 tok_len = 1;
2804 tok_type = LYXP_TOKEN_DOT;
2805
Radek Krejcif03a9e22020-09-18 20:09:31 +02002806 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002807
2808 /* '@' */
2809 tok_len = 1;
2810 tok_type = LYXP_TOKEN_AT;
2811
Radek Krejcif03a9e22020-09-18 20:09:31 +02002812 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002813
2814 /* ',' */
2815 tok_len = 1;
2816 tok_type = LYXP_TOKEN_COMMA;
2817
Radek Krejcif03a9e22020-09-18 20:09:31 +02002818 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002819
2820 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002821 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2822 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002823 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002824 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002825 ++tok_len;
2826 tok_type = LYXP_TOKEN_LITERAL;
2827
Radek Krejcif03a9e22020-09-18 20:09:31 +02002828 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002829
2830 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002831 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2832 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002833 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002834 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002835 ++tok_len;
2836 tok_type = LYXP_TOKEN_LITERAL;
2837
Radek Krejcif03a9e22020-09-18 20:09:31 +02002838 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002839
2840 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002841 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2842 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002843 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002844 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002845 }
2846 tok_type = LYXP_TOKEN_NUMBER;
2847
Radek Krejcif03a9e22020-09-18 20:09:31 +02002848 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002849
2850 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002851 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002852 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002853 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002854 } else {
2855 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002856 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002857 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002858
Radek Krejcif03a9e22020-09-18 20:09:31 +02002859 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002860
Michal Vasko3e48bf32020-06-01 08:39:07 +02002861 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002862 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002863 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2864
Radek Krejcif03a9e22020-09-18 20:09:31 +02002865 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002866
2867 /* Operator '<=', '>=' */
2868 tok_len = 2;
2869 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002870
Radek Krejcif03a9e22020-09-18 20:09:31 +02002871 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002872
2873 /* Operator '|' */
2874 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002875 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002876
Radek Krejcif03a9e22020-09-18 20:09:31 +02002877 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002878
2879 /* Operator '+', '-' */
2880 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002881 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002882
Radek Krejcif03a9e22020-09-18 20:09:31 +02002883 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002884
Michal Vasko3e48bf32020-06-01 08:39:07 +02002885 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002886 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002887 tok_type = LYXP_TOKEN_OPER_EQUAL;
2888
Radek Krejcif03a9e22020-09-18 20:09:31 +02002889 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002890
2891 /* Operator '<', '>' */
2892 tok_len = 1;
2893 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002894
Michal Vasko69730152020-10-09 16:30:07 +02002895 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2896 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2897 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2898 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2899 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2900 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2901 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2902 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2903 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2904 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2905 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2906 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002907
2908 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002909 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002910 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002911 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002912
Radek Krejcif03a9e22020-09-18 20:09:31 +02002913 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002914 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002915 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002916
Radek Krejcif03a9e22020-09-18 20:09:31 +02002917 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002918 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002919 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002920
Radek Krejcif03a9e22020-09-18 20:09:31 +02002921 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002922 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002923 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002924
2925 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002926 LOGVAL(ctx, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
Michal Vasko69730152020-10-09 16:30:07 +02002927 expr_str[parsed], expr_str[parsed], expr->tok_len[expr->used - 1], &expr->expr[expr->tok_pos[expr->used - 1]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002928 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002929 goto error;
2930 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02002931 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002932 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002933 goto error;
2934 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002935 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002936
2937 /* NameTest '*' */
2938 tok_len = 1;
2939 tok_type = LYXP_TOKEN_NAMETEST;
2940
2941 } else {
2942
2943 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002944 long int ncname_len = parse_ncname(&expr_str[parsed]);
Michal Vasko774ce402021-04-14 15:35:06 +02002945 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2946 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002947 tok_len = ncname_len;
2948
Radek Krejcif03a9e22020-09-18 20:09:31 +02002949 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002950 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002951 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002952 ++tok_len;
2953 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002954 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vasko774ce402021-04-14 15:35:06 +02002955 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2956 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002957 tok_len += ncname_len;
2958 }
2959 /* remove old flag to prevent ambiguities */
2960 prev_function_check = 0;
2961 tok_type = LYXP_TOKEN_NAMETEST;
2962 } else {
2963 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2964 prev_function_check = 1;
2965 tok_type = LYXP_TOKEN_NAMETEST;
2966 }
2967 }
2968
2969 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002970 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002971 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002972 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002973 ++parsed;
2974 }
2975
Radek Krejcif03a9e22020-09-18 20:09:31 +02002976 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002977
Michal Vasko004d3152020-06-11 19:59:22 +02002978 if (reparse) {
2979 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002980 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2981 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002982
Michal Vasko004d3152020-06-11 19:59:22 +02002983 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02002984 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002985 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002986 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002987 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002988 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002989 goto error;
2990 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002991 }
2992
Radek Krejcif03a9e22020-09-18 20:09:31 +02002993 print_expr_struct_debug(expr);
2994 *expr_p = expr;
2995 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002996
2997error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002998 lyxp_expr_free(ctx, expr);
2999 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01003000}
3001
Michal Vasko1734be92020-09-22 08:55:10 +02003002LY_ERR
3003lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003004{
Michal Vasko1734be92020-09-22 08:55:10 +02003005 LY_ERR ret = LY_SUCCESS;
3006 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003007 uint32_t i, j;
3008
Michal Vasko7f45cf22020-10-01 12:49:44 +02003009 if (!exp) {
3010 goto cleanup;
3011 }
3012
Michal Vasko004d3152020-06-11 19:59:22 +02003013 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003014 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003015
Michal Vasko08e9b112021-06-11 15:41:17 +02003016 if (exp->used) {
3017 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
3018 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3019 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003020
Michal Vasko08e9b112021-06-11 15:41:17 +02003021 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
3022 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3023 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003024
Michal Vasko08e9b112021-06-11 15:41:17 +02003025 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
3026 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3027 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003028
Michal Vasko08e9b112021-06-11 15:41:17 +02003029 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
3030 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3031 for (i = 0; i < exp->used; ++i) {
3032 if (!exp->repeat[i]) {
3033 dup->repeat[i] = NULL;
3034 } else {
3035 for (j = 0; exp->repeat[i][j]; ++j) {}
3036 /* the ending 0 as well */
3037 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003038
Michal Vasko08e9b112021-06-11 15:41:17 +02003039 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
3040 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3041 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3042 dup->repeat[i][j - 1] = 0;
3043 }
Michal Vasko004d3152020-06-11 19:59:22 +02003044 }
3045 }
3046
3047 dup->used = exp->used;
3048 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003049 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003050
Michal Vasko1734be92020-09-22 08:55:10 +02003051cleanup:
3052 if (ret) {
3053 lyxp_expr_free(ctx, dup);
3054 } else {
3055 *dup_p = dup;
3056 }
3057 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003058}
3059
Michal Vasko03ff5a72019-09-11 13:49:33 +02003060/*
3061 * warn functions
3062 *
3063 * Warn functions check specific reasonable conditions for schema XPath
3064 * and print a warning if they are not satisfied.
3065 */
3066
3067/**
3068 * @brief Get the last-added schema node that is currently in the context.
3069 *
3070 * @param[in] set Set to search in.
3071 * @return Last-added schema context node, NULL if no node is in context.
3072 */
3073static struct lysc_node *
3074warn_get_scnode_in_ctx(struct lyxp_set *set)
3075{
3076 uint32_t i;
3077
3078 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3079 return NULL;
3080 }
3081
3082 i = set->used;
3083 do {
3084 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003085 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003086 /* if there are more, simply return the first found (last added) */
3087 return set->val.scnodes[i].scnode;
3088 }
3089 } while (i);
3090
3091 return NULL;
3092}
3093
3094/**
3095 * @brief Test whether a type is numeric - integer type or decimal64.
3096 *
3097 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003098 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003099 */
Radek Krejci857189e2020-09-01 13:26:36 +02003100static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003101warn_is_numeric_type(struct lysc_type *type)
3102{
3103 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003104 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003105 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003106
3107 switch (type->basetype) {
3108 case LY_TYPE_DEC64:
3109 case LY_TYPE_INT8:
3110 case LY_TYPE_UINT8:
3111 case LY_TYPE_INT16:
3112 case LY_TYPE_UINT16:
3113 case LY_TYPE_INT32:
3114 case LY_TYPE_UINT32:
3115 case LY_TYPE_INT64:
3116 case LY_TYPE_UINT64:
3117 return 1;
3118 case LY_TYPE_UNION:
3119 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003120 LY_ARRAY_FOR(uni->types, u) {
3121 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003122 if (ret) {
3123 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003124 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003125 }
3126 }
3127 /* did not find any suitable type */
3128 return 0;
3129 case LY_TYPE_LEAFREF:
3130 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3131 default:
3132 return 0;
3133 }
3134}
3135
3136/**
3137 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3138 *
3139 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003140 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003141 */
Radek Krejci857189e2020-09-01 13:26:36 +02003142static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003143warn_is_string_type(struct lysc_type *type)
3144{
3145 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003146 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003147 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003148
3149 switch (type->basetype) {
3150 case LY_TYPE_BITS:
3151 case LY_TYPE_ENUM:
3152 case LY_TYPE_IDENT:
3153 case LY_TYPE_INST:
3154 case LY_TYPE_STRING:
3155 return 1;
3156 case LY_TYPE_UNION:
3157 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003158 LY_ARRAY_FOR(uni->types, u) {
3159 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003160 if (ret) {
3161 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003162 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003163 }
3164 }
3165 /* did not find any suitable type */
3166 return 0;
3167 case LY_TYPE_LEAFREF:
3168 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3169 default:
3170 return 0;
3171 }
3172}
3173
3174/**
3175 * @brief Test whether a type is one specific type.
3176 *
3177 * @param[in] type Type to test.
3178 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003179 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003180 */
Radek Krejci857189e2020-09-01 13:26:36 +02003181static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003182warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3183{
3184 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003185 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003186 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003187
3188 if (type->basetype == base) {
3189 return 1;
3190 } else if (type->basetype == LY_TYPE_UNION) {
3191 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003192 LY_ARRAY_FOR(uni->types, u) {
3193 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003194 if (ret) {
3195 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003196 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003197 }
3198 }
3199 /* did not find any suitable type */
3200 return 0;
3201 } else if (type->basetype == LY_TYPE_LEAFREF) {
3202 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3203 }
3204
3205 return 0;
3206}
3207
3208/**
3209 * @brief Get next type of a (union) type.
3210 *
3211 * @param[in] type Base type.
3212 * @param[in] prev_type Previously returned type.
3213 * @return Next type or NULL.
3214 */
3215static struct lysc_type *
3216warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3217{
3218 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003219 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003220 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003221
3222 switch (type->basetype) {
3223 case LY_TYPE_UNION:
3224 uni = (struct lysc_type_union *)type;
3225 if (!prev_type) {
3226 return uni->types[0];
3227 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003228 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003229 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003230 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003231 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003232 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003233 found = 1;
3234 }
3235 }
3236 return NULL;
3237 default:
3238 if (prev_type) {
3239 assert(type == prev_type);
3240 return NULL;
3241 } else {
3242 return type;
3243 }
3244 }
3245}
3246
3247/**
3248 * @brief Test whether 2 types have a common type.
3249 *
3250 * @param[in] type1 First type.
3251 * @param[in] type2 Second type.
3252 * @return 1 if they do, 0 otherwise.
3253 */
3254static int
3255warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3256{
3257 struct lysc_type *t1, *rt1, *t2, *rt2;
3258
3259 t1 = NULL;
3260 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3261 if (t1->basetype == LY_TYPE_LEAFREF) {
3262 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3263 } else {
3264 rt1 = t1;
3265 }
3266
3267 t2 = NULL;
3268 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3269 if (t2->basetype == LY_TYPE_LEAFREF) {
3270 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3271 } else {
3272 rt2 = t2;
3273 }
3274
3275 if (rt2->basetype == rt1->basetype) {
3276 /* match found */
3277 return 1;
3278 }
3279 }
3280 }
3281
3282 return 0;
3283}
3284
3285/**
3286 * @brief Check both operands of comparison operators.
3287 *
3288 * @param[in] ctx Context for errors.
3289 * @param[in] set1 First operand set.
3290 * @param[in] set2 Second operand set.
3291 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3292 * @param[in] expr Start of the expression to print with the warning.
3293 * @param[in] tok_pos Token position.
3294 */
3295static void
Radek Krejci857189e2020-09-01 13:26:36 +02003296warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr, uint16_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003297{
3298 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003299 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003300
3301 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3302 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3303
3304 if (!node1 && !node2) {
3305 /* no node-sets involved, nothing to do */
3306 return;
3307 }
3308
3309 if (node1) {
3310 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3311 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3312 warning = 1;
3313 leaves = 0;
3314 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3315 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3316 warning = 1;
3317 }
3318 }
3319
3320 if (node2) {
3321 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3322 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3323 warning = 1;
3324 leaves = 0;
3325 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3326 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3327 warning = 1;
3328 }
3329 }
3330
3331 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003332 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3333 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3334 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3335 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003336 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3337 warning = 1;
3338 }
3339 }
3340
3341 if (warning) {
3342 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3343 }
3344}
3345
3346/**
3347 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3348 *
3349 * @param[in] exp Parsed XPath expression.
3350 * @param[in] set Set with the leaf/leaf-list.
3351 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3352 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3353 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3354 */
3355static void
Michal Vasko40308e72020-10-20 16:38:40 +02003356warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3357 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003358{
3359 struct lysc_node *scnode;
3360 struct lysc_type *type;
3361 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003362 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003363 LY_ERR rc;
3364 struct ly_err_item *err = NULL;
3365
Michal Vasko69730152020-10-09 16:30:07 +02003366 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3367 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003368 /* check that the node can have the specified value */
3369 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3370 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3371 } else {
3372 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3373 }
3374 if (!value) {
3375 LOGMEM(set->ctx);
3376 return;
3377 }
3378
3379 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3380 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003381 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003382 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003383 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003384 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003385 }
3386
3387 type = ((struct lysc_node_leaf *)scnode)->type;
3388 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003389 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003390 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003391 if (rc == LY_EINCOMPLETE) {
3392 rc = LY_SUCCESS;
3393 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003394
3395 if (err) {
3396 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3397 ly_err_free(err);
3398 } else if (rc != LY_SUCCESS) {
3399 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3400 }
3401 if (rc != LY_SUCCESS) {
3402 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003403 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003404 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003405 } else {
3406 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003407 }
3408 }
3409 free(value);
3410 }
3411}
3412
3413/*
3414 * XPath functions
3415 */
3416
3417/**
3418 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3419 * depending on whether the first node bit value from the second argument is set.
3420 *
3421 * @param[in] args Array of arguments.
3422 * @param[in] arg_count Count of elements in @p args.
3423 * @param[in,out] set Context and result set at the same time.
3424 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003425 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003426 */
3427static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003428xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003429{
3430 struct lyd_node_term *leaf;
3431 struct lysc_node_leaf *sleaf;
3432 struct lysc_type_bits *bits;
3433 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003434 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003435
3436 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003437 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003438 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003439 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3440 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3441 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3442 sleaf->name);
3443 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3444 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3445 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003446 }
3447
3448 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3449 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003450 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3451 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003452 } else if (!warn_is_string_type(sleaf->type)) {
3453 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003454 }
3455 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003456 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003457 return rc;
3458 }
3459
Michal Vaskod3678892020-05-21 10:06:58 +02003460 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003461 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "bit-is-set(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003462 return LY_EVALID;
3463 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003464 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003465 LY_CHECK_RET(rc);
3466
3467 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003468 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003469 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003470 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3471 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003472 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003473 LY_ARRAY_FOR(bits->bits, u) {
3474 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003475 set_fill_boolean(set, 1);
3476 break;
3477 }
3478 }
3479 }
3480 }
3481
3482 return LY_SUCCESS;
3483}
3484
3485/**
3486 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3487 * with the argument converted to boolean.
3488 *
3489 * @param[in] args Array of arguments.
3490 * @param[in] arg_count Count of elements in @p args.
3491 * @param[in,out] set Context and result set at the same time.
3492 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003493 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003494 */
3495static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003496xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003497{
3498 LY_ERR rc;
3499
3500 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003501 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003502 return LY_SUCCESS;
3503 }
3504
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003505 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003506 LY_CHECK_RET(rc);
3507 set_fill_set(set, args[0]);
3508
3509 return LY_SUCCESS;
3510}
3511
3512/**
3513 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3514 * with the first argument rounded up to the nearest integer.
3515 *
3516 * @param[in] args Array of arguments.
3517 * @param[in] arg_count Count of elements in @p args.
3518 * @param[in,out] set Context and result set at the same time.
3519 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003520 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003521 */
3522static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003523xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003524{
3525 struct lysc_node_leaf *sleaf;
3526 LY_ERR rc = LY_SUCCESS;
3527
3528 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003529 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003530 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003531 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3532 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3533 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3534 sleaf->name);
3535 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3536 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3537 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003538 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003539 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003540 return rc;
3541 }
3542
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003543 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003544 LY_CHECK_RET(rc);
3545 if ((long long)args[0]->val.num != args[0]->val.num) {
3546 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3547 } else {
3548 set_fill_number(set, args[0]->val.num);
3549 }
3550
3551 return LY_SUCCESS;
3552}
3553
3554/**
3555 * @brief Execute the XPath concat(string, string, string*) function.
3556 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3557 *
3558 * @param[in] args Array of arguments.
3559 * @param[in] arg_count Count of elements in @p args.
3560 * @param[in,out] set Context and result set at the same time.
3561 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003562 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003563 */
3564static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003565xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003566{
3567 uint16_t i;
3568 char *str = NULL;
3569 size_t used = 1;
3570 LY_ERR rc = LY_SUCCESS;
3571 struct lysc_node_leaf *sleaf;
3572
3573 if (options & LYXP_SCNODE_ALL) {
3574 for (i = 0; i < arg_count; ++i) {
3575 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3576 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3577 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003578 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003579 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003580 LOGWRN(set->ctx, "Argument #%u of %s is node \"%s\", not of string-type.", i + 1, __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003581 }
3582 }
3583 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003584 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003585 return rc;
3586 }
3587
3588 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003589 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003590 if (rc != LY_SUCCESS) {
3591 free(str);
3592 return rc;
3593 }
3594
3595 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3596 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3597 strcpy(str + used - 1, args[i]->val.str);
3598 used += strlen(args[i]->val.str);
3599 }
3600
3601 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003602 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003603 set->type = LYXP_SET_STRING;
3604 set->val.str = str;
3605
3606 return LY_SUCCESS;
3607}
3608
3609/**
3610 * @brief Execute the XPath contains(string, string) function.
3611 * Returns LYXP_SET_BOOLEAN whether the second argument can
3612 * be found in the first or not.
3613 *
3614 * @param[in] args Array of arguments.
3615 * @param[in] arg_count Count of elements in @p args.
3616 * @param[in,out] set Context and result set at the same time.
3617 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003618 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003619 */
3620static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003621xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003622{
3623 struct lysc_node_leaf *sleaf;
3624 LY_ERR rc = LY_SUCCESS;
3625
3626 if (options & LYXP_SCNODE_ALL) {
3627 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3628 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003629 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3630 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003631 } else if (!warn_is_string_type(sleaf->type)) {
3632 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003633 }
3634 }
3635
3636 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3637 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003638 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3639 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003640 } else if (!warn_is_string_type(sleaf->type)) {
3641 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003642 }
3643 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003644 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003645 return rc;
3646 }
3647
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003648 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003649 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003650 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003651 LY_CHECK_RET(rc);
3652
3653 if (strstr(args[0]->val.str, args[1]->val.str)) {
3654 set_fill_boolean(set, 1);
3655 } else {
3656 set_fill_boolean(set, 0);
3657 }
3658
3659 return LY_SUCCESS;
3660}
3661
3662/**
3663 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3664 * with the size of the node-set from the argument.
3665 *
3666 * @param[in] args Array of arguments.
3667 * @param[in] arg_count Count of elements in @p args.
3668 * @param[in,out] set Context and result set at the same time.
3669 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003670 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003671 */
3672static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003673xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003674{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003675 LY_ERR rc = LY_SUCCESS;
3676
3677 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003678 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003679 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003680 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003681 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003682 return rc;
3683 }
3684
Michal Vasko03ff5a72019-09-11 13:49:33 +02003685 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003686 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003687 return LY_EVALID;
3688 }
3689
3690 set_fill_number(set, args[0]->used);
3691 return LY_SUCCESS;
3692}
3693
3694/**
3695 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3696 * with the context with the intial node.
3697 *
3698 * @param[in] args Array of arguments.
3699 * @param[in] arg_count Count of elements in @p args.
3700 * @param[in,out] set Context and result set at the same time.
3701 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003702 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003703 */
3704static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003705xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003706{
3707 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003708 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003709 return LY_EVALID;
3710 }
3711
3712 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003713 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003714
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003715 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003716 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003717 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003718
3719 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003720 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003721 }
3722
3723 return LY_SUCCESS;
3724}
3725
3726/**
3727 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3728 * leafref or instance-identifier target node(s).
3729 *
3730 * @param[in] args Array of arguments.
3731 * @param[in] arg_count Count of elements in @p args.
3732 * @param[in,out] set Context and result set at the same time.
3733 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003734 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003735 */
3736static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003737xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003738{
3739 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003740 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003741 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003742 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003743 struct ly_path *p;
3744 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003745 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003746 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02003747 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003748
3749 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003750 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003751 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003752 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3753 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3754 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3755 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02003756 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
3757 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003758 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3759 __func__, sleaf->name);
3760 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003761 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003762 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko42e497c2020-01-06 08:38:25 +01003763 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003764 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003765 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003766
3767 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02003768 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vaskoed725d72021-06-23 12:03:45 +02003769 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, NULL, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02003770 if (!r) {
3771 /* get the target node */
3772 target = p[LY_ARRAY_COUNT(p) - 1].node;
3773 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02003774
Michal Vasko741bb562021-06-24 11:59:50 +02003775 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
3776 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003777 }
3778
Michal Vasko741bb562021-06-24 11:59:50 +02003779 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003780 }
3781
Michal Vaskod3678892020-05-21 10:06:58 +02003782 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003783 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003784 return LY_EVALID;
3785 }
3786
Michal Vaskod3678892020-05-21 10:06:58 +02003787 lyxp_set_free_content(set);
3788 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003789 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3790 sleaf = (struct lysc_node_leaf *)leaf->schema;
3791 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3792 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3793 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02003794 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01003795 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003796 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003797 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003798 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003799 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003800 } else {
3801 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003802 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003803 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003804 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003805 return LY_EVALID;
3806 }
3807 }
Michal Vasko004d3152020-06-11 19:59:22 +02003808
3809 /* insert it */
3810 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003811 }
3812 }
3813
3814 return LY_SUCCESS;
3815}
3816
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003817static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003818xpath_derived_(struct lyxp_set **args, struct lyxp_set *set, uint32_t options, ly_bool self_match, const char *func)
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003819{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01003820 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003821 LY_ARRAY_COUNT_TYPE u;
3822 struct lyd_node_term *leaf;
3823 struct lysc_node_leaf *sleaf;
3824 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02003825 struct lyd_value *val;
3826 const struct lys_module *mod;
3827 const char *id_name;
3828 uint16_t id_len;
3829 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003830 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003831 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003832
3833 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003834 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003835 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003836 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3837 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3838 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3839 sleaf->name);
3840 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3841 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3842 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003843 }
3844
3845 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3846 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3847 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003848 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003849 } else if (!warn_is_string_type(sleaf->type)) {
3850 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3851 }
3852 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003853 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003854 return rc;
3855 }
3856
3857 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003858 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "derived-from(-or-self)(node-set, string)");
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003859 return LY_EVALID;
3860 }
3861 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3862 LY_CHECK_RET(rc);
3863
Michal Vasko93923692021-05-07 15:28:02 +02003864 /* parse the identity */
3865 id_name = args[1]->val.str;
3866 id_len = strlen(id_name);
3867 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
3868 LY_CHECK_RET(rc);
3869 if (!mod) {
3870 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
3871 return LY_EVALID;
3872 }
3873
3874 /* find the identity */
3875 found = 0;
3876 LY_ARRAY_FOR(mod->identities, u) {
3877 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
3878 /* we have match */
3879 found = 1;
3880 break;
3881 }
3882 }
3883 if (!found) {
3884 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
3885 return LY_EVALID;
3886 }
3887 id = &mod->identities[u];
3888
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003889 set_fill_boolean(set, 0);
3890 found = 0;
3891 for (i = 0; i < args[0]->used; ++i) {
3892 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3893 continue;
3894 }
3895
3896 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3897 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3898 sleaf = (struct lysc_node_leaf *)leaf->schema;
3899 val = &leaf->value;
3900 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3901 /* uninteresting */
3902 continue;
3903 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003904 } else {
3905 meta = args[0]->val.meta[i].meta;
3906 val = &meta->value;
3907 if (val->realtype->basetype != LY_TYPE_IDENT) {
3908 /* uninteresting */
3909 continue;
3910 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003911 }
3912
Michal Vasko93923692021-05-07 15:28:02 +02003913 /* check the identity itself */
3914 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003915 set_fill_boolean(set, 1);
3916 found = 1;
3917 }
Michal Vasko93923692021-05-07 15:28:02 +02003918 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
3919 set_fill_boolean(set, 1);
3920 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003921 }
3922
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003923 if (found) {
3924 break;
3925 }
3926 }
3927
3928 return LY_SUCCESS;
3929}
3930
Michal Vasko03ff5a72019-09-11 13:49:33 +02003931/**
3932 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3933 * on whether the first argument nodes contain a node of an identity derived from the second
3934 * argument identity.
3935 *
3936 * @param[in] args Array of arguments.
3937 * @param[in] arg_count Count of elements in @p args.
3938 * @param[in,out] set Context and result set at the same time.
3939 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003940 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003941 */
3942static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003943xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003944{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003945 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003946}
3947
3948/**
3949 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3950 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3951 * the second argument identity.
3952 *
3953 * @param[in] args Array of arguments.
3954 * @param[in] arg_count Count of elements in @p args.
3955 * @param[in,out] set Context and result set at the same time.
3956 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003957 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003958 */
3959static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003960xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003961{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003962 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003963}
3964
3965/**
3966 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3967 * with the integer value of the first node's enum value, otherwise NaN.
3968 *
3969 * @param[in] args Array of arguments.
3970 * @param[in] arg_count Count of elements in @p args.
3971 * @param[in,out] set Context and result set at the same time.
3972 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003973 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003974 */
3975static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003976xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003977{
3978 struct lyd_node_term *leaf;
3979 struct lysc_node_leaf *sleaf;
3980 LY_ERR rc = LY_SUCCESS;
3981
3982 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003983 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003984 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003985 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3986 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3987 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3988 sleaf->name);
3989 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3990 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
3991 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003992 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003993 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003994 return rc;
3995 }
3996
Michal Vaskod3678892020-05-21 10:06:58 +02003997 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003998 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003999 return LY_EVALID;
4000 }
4001
4002 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004003 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004004 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4005 sleaf = (struct lysc_node_leaf *)leaf->schema;
4006 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4007 set_fill_number(set, leaf->value.enum_item->value);
4008 }
4009 }
4010
4011 return LY_SUCCESS;
4012}
4013
4014/**
4015 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4016 * with false value.
4017 *
4018 * @param[in] args Array of arguments.
4019 * @param[in] arg_count Count of elements in @p args.
4020 * @param[in,out] set Context and result set at the same time.
4021 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004022 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004023 */
4024static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004025xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004026{
4027 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004028 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004029 return LY_SUCCESS;
4030 }
4031
4032 set_fill_boolean(set, 0);
4033 return LY_SUCCESS;
4034}
4035
4036/**
4037 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4038 * with the first argument floored (truncated).
4039 *
4040 * @param[in] args Array of arguments.
4041 * @param[in] arg_count Count of elements in @p args.
4042 * @param[in,out] set Context and result set at the same time.
4043 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004044 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004045 */
4046static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004047xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t UNUSED(options))
Michal Vasko03ff5a72019-09-11 13:49:33 +02004048{
4049 LY_ERR rc;
4050
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004051 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004052 LY_CHECK_RET(rc);
4053 if (isfinite(args[0]->val.num)) {
4054 set_fill_number(set, (long long)args[0]->val.num);
4055 }
4056
4057 return LY_SUCCESS;
4058}
4059
4060/**
4061 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4062 * whether the language of the text matches the one from the argument.
4063 *
4064 * @param[in] args Array of arguments.
4065 * @param[in] arg_count Count of elements in @p args.
4066 * @param[in,out] set Context and result set at the same time.
4067 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004068 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004069 */
4070static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004071xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004072{
4073 const struct lyd_node *node;
4074 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004075 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004076 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004077 LY_ERR rc = LY_SUCCESS;
4078
4079 if (options & LYXP_SCNODE_ALL) {
4080 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4081 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004082 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4083 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004084 } else if (!warn_is_string_type(sleaf->type)) {
4085 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004086 }
4087 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004088 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004089 return rc;
4090 }
4091
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004092 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004093 LY_CHECK_RET(rc);
4094
Michal Vasko03ff5a72019-09-11 13:49:33 +02004095 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004096 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004097 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004098 } else if (!set->used) {
4099 set_fill_boolean(set, 0);
4100 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004101 }
4102
4103 switch (set->val.nodes[0].type) {
4104 case LYXP_NODE_ELEM:
4105 case LYXP_NODE_TEXT:
4106 node = set->val.nodes[0].node;
4107 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004108 case LYXP_NODE_META:
4109 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004110 break;
4111 default:
4112 /* nothing to do with roots */
4113 set_fill_boolean(set, 0);
4114 return LY_SUCCESS;
4115 }
4116
Michal Vasko9f96a052020-03-10 09:41:45 +01004117 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004118 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004119 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004120 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004121 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004122 break;
4123 }
4124 }
4125
Michal Vasko9f96a052020-03-10 09:41:45 +01004126 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004127 break;
4128 }
4129 }
4130
4131 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004132 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004133 set_fill_boolean(set, 0);
4134 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004135 uint64_t i;
4136
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004137 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004138 for (i = 0; args[0]->val.str[i]; ++i) {
4139 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4140 set_fill_boolean(set, 0);
4141 break;
4142 }
4143 }
4144 if (!args[0]->val.str[i]) {
4145 if (!val[i] || (val[i] == '-')) {
4146 set_fill_boolean(set, 1);
4147 } else {
4148 set_fill_boolean(set, 0);
4149 }
4150 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004151 }
4152
4153 return LY_SUCCESS;
4154}
4155
4156/**
4157 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4158 * with the context size.
4159 *
4160 * @param[in] args Array of arguments.
4161 * @param[in] arg_count Count of elements in @p args.
4162 * @param[in,out] set Context and result set at the same time.
4163 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004164 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004165 */
4166static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004167xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004168{
4169 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004170 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004171 return LY_SUCCESS;
4172 }
4173
Michal Vasko03ff5a72019-09-11 13:49:33 +02004174 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004175 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004176 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004177 } else if (!set->used) {
4178 set_fill_number(set, 0);
4179 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004180 }
4181
4182 set_fill_number(set, set->ctx_size);
4183 return LY_SUCCESS;
4184}
4185
4186/**
4187 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4188 * with the node name without namespace from the argument or the context.
4189 *
4190 * @param[in] args Array of arguments.
4191 * @param[in] arg_count Count of elements in @p args.
4192 * @param[in,out] set Context and result set at the same time.
4193 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004194 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004195 */
4196static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004197xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004198{
4199 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004200
Michal Vasko03ff5a72019-09-11 13:49:33 +02004201 /* suppress unused variable warning */
4202 (void)options;
4203
4204 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004205 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004206 return LY_SUCCESS;
4207 }
4208
4209 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004210 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004211 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004212 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004213 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004214 } else if (!args[0]->used) {
4215 set_fill_string(set, "", 0);
4216 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004217 }
4218
4219 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004220 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004221
4222 item = &args[0]->val.nodes[0];
4223 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004224 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004225 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004226 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004227 } else if (!set->used) {
4228 set_fill_string(set, "", 0);
4229 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004230 }
4231
4232 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004233 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004234
4235 item = &set->val.nodes[0];
4236 }
4237
4238 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004239 case LYXP_NODE_NONE:
4240 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004241 case LYXP_NODE_ROOT:
4242 case LYXP_NODE_ROOT_CONFIG:
4243 case LYXP_NODE_TEXT:
4244 set_fill_string(set, "", 0);
4245 break;
4246 case LYXP_NODE_ELEM:
4247 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4248 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004249 case LYXP_NODE_META:
4250 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004251 break;
4252 }
4253
4254 return LY_SUCCESS;
4255}
4256
4257/**
4258 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4259 * with the node name fully qualified (with namespace) from the argument or the context.
4260 *
4261 * @param[in] args Array of arguments.
4262 * @param[in] arg_count Count of elements in @p args.
4263 * @param[in,out] set Context and result set at the same time.
4264 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004265 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004266 */
4267static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004268xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004269{
4270 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004271 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004272 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004273 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004274
4275 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004276 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004277 return LY_SUCCESS;
4278 }
4279
4280 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004281 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004282 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004283 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004284 } else if (!args[0]->used) {
4285 set_fill_string(set, "", 0);
4286 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004287 }
4288
4289 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004290 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004291
4292 item = &args[0]->val.nodes[0];
4293 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004294 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004295 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004296 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004297 } else if (!set->used) {
4298 set_fill_string(set, "", 0);
4299 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004300 }
4301
4302 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004303 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004304
4305 item = &set->val.nodes[0];
4306 }
4307
4308 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004309 case LYXP_NODE_NONE:
4310 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004311 case LYXP_NODE_ROOT:
4312 case LYXP_NODE_ROOT_CONFIG:
4313 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004314 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004315 break;
4316 case LYXP_NODE_ELEM:
4317 mod = item->node->schema->module;
4318 name = item->node->schema->name;
4319 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004320 case LYXP_NODE_META:
4321 mod = ((struct lyd_meta *)item->node)->annotation->module;
4322 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004323 break;
4324 }
4325
4326 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004327 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004328 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4329 set_fill_string(set, str, strlen(str));
4330 free(str);
4331 } else {
4332 set_fill_string(set, "", 0);
4333 }
4334
4335 return LY_SUCCESS;
4336}
4337
4338/**
4339 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4340 * with the namespace of the node from the argument or the context.
4341 *
4342 * @param[in] args Array of arguments.
4343 * @param[in] arg_count Count of elements in @p args.
4344 * @param[in,out] set Context and result set at the same time.
4345 * @param[in] options XPath options.
4346 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4347 */
4348static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004349xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004350{
4351 struct lyxp_set_node *item;
4352 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004353
Michal Vasko03ff5a72019-09-11 13:49:33 +02004354 /* suppress unused variable warning */
4355 (void)options;
4356
4357 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004358 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004359 return LY_SUCCESS;
4360 }
4361
4362 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004363 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004364 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004365 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004366 return LY_EVALID;
4367 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004368 set_fill_string(set, "", 0);
4369 return LY_SUCCESS;
4370 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004371
4372 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004373 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004374
4375 item = &args[0]->val.nodes[0];
4376 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004377 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004378 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004379 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004380 } else if (!set->used) {
4381 set_fill_string(set, "", 0);
4382 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004383 }
4384
4385 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004386 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004387
4388 item = &set->val.nodes[0];
4389 }
4390
4391 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004392 case LYXP_NODE_NONE:
4393 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004394 case LYXP_NODE_ROOT:
4395 case LYXP_NODE_ROOT_CONFIG:
4396 case LYXP_NODE_TEXT:
4397 set_fill_string(set, "", 0);
4398 break;
4399 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004400 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004401 if (item->type == LYXP_NODE_ELEM) {
4402 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004403 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004404 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004405 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004406 }
4407
4408 set_fill_string(set, mod->ns, strlen(mod->ns));
4409 break;
4410 }
4411
4412 return LY_SUCCESS;
4413}
4414
4415/**
4416 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4417 * with only nodes from the context. In practice it either leaves the context
4418 * as it is or returns an empty node set.
4419 *
4420 * @param[in] args Array of arguments.
4421 * @param[in] arg_count Count of elements in @p args.
4422 * @param[in,out] set Context and result set at the same time.
4423 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004424 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004425 */
4426static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004427xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004428{
4429 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004430 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004431 return LY_SUCCESS;
4432 }
4433
4434 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004435 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004436 }
4437 return LY_SUCCESS;
4438}
4439
4440/**
4441 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4442 * with normalized value (no leading, trailing, double white spaces) of the node
4443 * from the argument or the context.
4444 *
4445 * @param[in] args Array of arguments.
4446 * @param[in] arg_count Count of elements in @p args.
4447 * @param[in,out] set Context and result set at the same time.
4448 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004449 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004450 */
4451static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004452xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004453{
4454 uint16_t i, new_used;
4455 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004456 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004457 struct lysc_node_leaf *sleaf;
4458 LY_ERR rc = LY_SUCCESS;
4459
4460 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004461 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4462 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004463 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004464 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4465 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004466 } else if (!warn_is_string_type(sleaf->type)) {
4467 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004468 }
4469 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004470 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004471 return rc;
4472 }
4473
4474 if (arg_count) {
4475 set_fill_set(set, args[0]);
4476 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004477 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004478 LY_CHECK_RET(rc);
4479
4480 /* is there any normalization necessary? */
4481 for (i = 0; set->val.str[i]; ++i) {
4482 if (is_xmlws(set->val.str[i])) {
4483 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4484 have_spaces = 1;
4485 break;
4486 }
4487 space_before = 1;
4488 } else {
4489 space_before = 0;
4490 }
4491 }
4492
4493 /* yep, there is */
4494 if (have_spaces) {
4495 /* it's enough, at least one character will go, makes space for ending '\0' */
4496 new = malloc(strlen(set->val.str) * sizeof(char));
4497 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4498 new_used = 0;
4499
4500 space_before = 0;
4501 for (i = 0; set->val.str[i]; ++i) {
4502 if (is_xmlws(set->val.str[i])) {
4503 if ((i == 0) || space_before) {
4504 space_before = 1;
4505 continue;
4506 } else {
4507 space_before = 1;
4508 }
4509 } else {
4510 space_before = 0;
4511 }
4512
4513 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4514 ++new_used;
4515 }
4516
4517 /* at worst there is one trailing space now */
4518 if (new_used && is_xmlws(new[new_used - 1])) {
4519 --new_used;
4520 }
4521
4522 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4523 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4524 new[new_used] = '\0';
4525
4526 free(set->val.str);
4527 set->val.str = new;
4528 }
4529
4530 return LY_SUCCESS;
4531}
4532
4533/**
4534 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4535 * with the argument converted to boolean and logically inverted.
4536 *
4537 * @param[in] args Array of arguments.
4538 * @param[in] arg_count Count of elements in @p args.
4539 * @param[in,out] set Context and result set at the same time.
4540 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004541 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004542 */
4543static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004544xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004545{
4546 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004547 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004548 return LY_SUCCESS;
4549 }
4550
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004551 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004552 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004553 set_fill_boolean(set, 0);
4554 } else {
4555 set_fill_boolean(set, 1);
4556 }
4557
4558 return LY_SUCCESS;
4559}
4560
4561/**
4562 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4563 * with the number representation of either the argument or the context.
4564 *
4565 * @param[in] args Array of arguments.
4566 * @param[in] arg_count Count of elements in @p args.
4567 * @param[in,out] set Context and result set at the same time.
4568 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004569 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004570 */
4571static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004572xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004573{
4574 LY_ERR rc;
4575
4576 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004577 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004578 return LY_SUCCESS;
4579 }
4580
4581 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004582 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004583 LY_CHECK_RET(rc);
4584 set_fill_set(set, args[0]);
4585 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004586 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004587 LY_CHECK_RET(rc);
4588 }
4589
4590 return LY_SUCCESS;
4591}
4592
4593/**
4594 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4595 * with the context position.
4596 *
4597 * @param[in] args Array of arguments.
4598 * @param[in] arg_count Count of elements in @p args.
4599 * @param[in,out] set Context and result set at the same time.
4600 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004601 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004602 */
4603static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004604xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004605{
4606 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004607 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004608 return LY_SUCCESS;
4609 }
4610
Michal Vasko03ff5a72019-09-11 13:49:33 +02004611 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004612 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004613 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004614 } else if (!set->used) {
4615 set_fill_number(set, 0);
4616 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004617 }
4618
4619 set_fill_number(set, set->ctx_pos);
4620
4621 /* UNUSED in 'Release' build type */
4622 (void)options;
4623 return LY_SUCCESS;
4624}
4625
4626/**
4627 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4628 * depending on whether the second argument regex matches the first argument string. For details refer to
4629 * YANG 1.1 RFC section 10.2.1.
4630 *
4631 * @param[in] args Array of arguments.
4632 * @param[in] arg_count Count of elements in @p args.
4633 * @param[in,out] set Context and result set at the same time.
4634 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004635 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004636 */
4637static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004638xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004639{
4640 struct lysc_pattern **patterns = NULL, **pattern;
4641 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004642 LY_ERR rc = LY_SUCCESS;
4643 struct ly_err_item *err;
4644
4645 if (options & LYXP_SCNODE_ALL) {
4646 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4647 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4648 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004649 } else if (!warn_is_string_type(sleaf->type)) {
4650 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004651 }
4652 }
4653
4654 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4655 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4656 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004657 } else if (!warn_is_string_type(sleaf->type)) {
4658 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004659 }
4660 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004661 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004662 return rc;
4663 }
4664
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004665 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004666 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004667 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004668 LY_CHECK_RET(rc);
4669
4670 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek IÅ¡a45802b52021-02-09 09:21:58 +01004671 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004672 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004673 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Radek Krejciddace2c2021-01-08 11:30:56 +01004674 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004675 if (rc != LY_SUCCESS) {
4676 LY_ARRAY_FREE(patterns);
4677 return rc;
4678 }
4679
Radek Krejci0b013302021-03-29 15:22:32 +02004680 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004681 pcre2_code_free((*pattern)->code);
4682 free(*pattern);
4683 LY_ARRAY_FREE(patterns);
4684 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004685 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004686 ly_err_free(err);
4687 return rc;
4688 }
4689
4690 if (rc == LY_EVALID) {
4691 ly_err_free(err);
4692 set_fill_boolean(set, 0);
4693 } else {
4694 set_fill_boolean(set, 1);
4695 }
4696
4697 return LY_SUCCESS;
4698}
4699
4700/**
4701 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4702 * with the rounded first argument. For details refer to
4703 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4704 *
4705 * @param[in] args Array of arguments.
4706 * @param[in] arg_count Count of elements in @p args.
4707 * @param[in,out] set Context and result set at the same time.
4708 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004709 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004710 */
4711static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004712xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004713{
4714 struct lysc_node_leaf *sleaf;
4715 LY_ERR rc = LY_SUCCESS;
4716
4717 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004718 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004719 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004720 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4721 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4722 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4723 sleaf->name);
4724 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4725 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4726 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004727 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004728 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004729 return rc;
4730 }
4731
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004732 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004733 LY_CHECK_RET(rc);
4734
4735 /* cover only the cases where floor can't be used */
4736 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4737 set_fill_number(set, -0.0f);
4738 } else {
4739 args[0]->val.num += 0.5;
4740 rc = xpath_floor(args, 1, args[0], options);
4741 LY_CHECK_RET(rc);
4742 set_fill_number(set, args[0]->val.num);
4743 }
4744
4745 return LY_SUCCESS;
4746}
4747
4748/**
4749 * @brief Execute the XPath starts-with(string, string) function.
4750 * Returns LYXP_SET_BOOLEAN whether the second argument is
4751 * the prefix of the first or not.
4752 *
4753 * @param[in] args Array of arguments.
4754 * @param[in] arg_count Count of elements in @p args.
4755 * @param[in,out] set Context and result set at the same time.
4756 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004757 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004758 */
4759static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004760xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004761{
4762 struct lysc_node_leaf *sleaf;
4763 LY_ERR rc = LY_SUCCESS;
4764
4765 if (options & LYXP_SCNODE_ALL) {
4766 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4767 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4768 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004769 } else if (!warn_is_string_type(sleaf->type)) {
4770 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004771 }
4772 }
4773
4774 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4775 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4776 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004777 } else if (!warn_is_string_type(sleaf->type)) {
4778 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004779 }
4780 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004781 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004782 return rc;
4783 }
4784
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004785 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004786 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004787 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004788 LY_CHECK_RET(rc);
4789
4790 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4791 set_fill_boolean(set, 0);
4792 } else {
4793 set_fill_boolean(set, 1);
4794 }
4795
4796 return LY_SUCCESS;
4797}
4798
4799/**
4800 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4801 * with the string representation of either the argument or the context.
4802 *
4803 * @param[in] args Array of arguments.
4804 * @param[in] arg_count Count of elements in @p args.
4805 * @param[in,out] set Context and result set at the same time.
4806 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004807 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004808 */
4809static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004810xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004811{
4812 LY_ERR rc;
4813
4814 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004815 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004816 return LY_SUCCESS;
4817 }
4818
4819 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004820 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004821 LY_CHECK_RET(rc);
4822 set_fill_set(set, args[0]);
4823 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004824 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004825 LY_CHECK_RET(rc);
4826 }
4827
4828 return LY_SUCCESS;
4829}
4830
4831/**
4832 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4833 * with the length of the string in either the argument or the context.
4834 *
4835 * @param[in] args Array of arguments.
4836 * @param[in] arg_count Count of elements in @p args.
4837 * @param[in,out] set Context and result set at the same time.
4838 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004839 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004840 */
4841static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004842xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004843{
4844 struct lysc_node_leaf *sleaf;
4845 LY_ERR rc = LY_SUCCESS;
4846
4847 if (options & LYXP_SCNODE_ALL) {
4848 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4849 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4850 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004851 } else if (!warn_is_string_type(sleaf->type)) {
4852 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004853 }
4854 }
4855 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4856 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4857 LOGWRN(set->ctx, "Argument #0 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004858 } else if (!warn_is_string_type(sleaf->type)) {
4859 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004860 }
4861 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004862 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004863 return rc;
4864 }
4865
4866 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004867 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004868 LY_CHECK_RET(rc);
4869 set_fill_number(set, strlen(args[0]->val.str));
4870 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004871 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004872 LY_CHECK_RET(rc);
4873 set_fill_number(set, strlen(set->val.str));
4874 }
4875
4876 return LY_SUCCESS;
4877}
4878
4879/**
4880 * @brief Execute the XPath substring(string, number, number?) function.
4881 * Returns LYXP_SET_STRING substring of the first argument starting
4882 * on the second argument index ending on the third argument index,
4883 * indexed from 1. For exact definition refer to
4884 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4885 *
4886 * @param[in] args Array of arguments.
4887 * @param[in] arg_count Count of elements in @p args.
4888 * @param[in,out] set Context and result set at the same time.
4889 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004890 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004891 */
4892static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004893xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004894{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004895 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004896 uint16_t str_start, str_len, pos;
4897 struct lysc_node_leaf *sleaf;
4898 LY_ERR rc = LY_SUCCESS;
4899
4900 if (options & LYXP_SCNODE_ALL) {
4901 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4902 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4903 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004904 } else if (!warn_is_string_type(sleaf->type)) {
4905 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004906 }
4907 }
4908
4909 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4910 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4911 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004912 } else if (!warn_is_numeric_type(sleaf->type)) {
4913 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004914 }
4915 }
4916
Michal Vasko69730152020-10-09 16:30:07 +02004917 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4918 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004919 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4920 LOGWRN(set->ctx, "Argument #3 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004921 } else if (!warn_is_numeric_type(sleaf->type)) {
4922 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004923 }
4924 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004925 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004926 return rc;
4927 }
4928
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004929 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004930 LY_CHECK_RET(rc);
4931
4932 /* start */
4933 if (xpath_round(&args[1], 1, args[1], options)) {
4934 return -1;
4935 }
4936 if (isfinite(args[1]->val.num)) {
4937 start = args[1]->val.num - 1;
4938 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004939 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004940 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004941 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004942 }
4943
4944 /* len */
4945 if (arg_count == 3) {
4946 rc = xpath_round(&args[2], 1, args[2], options);
4947 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004948 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004949 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004950 } else if (isfinite(args[2]->val.num)) {
4951 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004952 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004953 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004954 }
4955 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004956 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004957 }
4958
4959 /* find matching character positions */
4960 str_start = 0;
4961 str_len = 0;
4962 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4963 if (pos < start) {
4964 ++str_start;
4965 } else if (pos < start + len) {
4966 ++str_len;
4967 } else {
4968 break;
4969 }
4970 }
4971
4972 set_fill_string(set, args[0]->val.str + str_start, str_len);
4973 return LY_SUCCESS;
4974}
4975
4976/**
4977 * @brief Execute the XPath substring-after(string, string) function.
4978 * Returns LYXP_SET_STRING with the string succeeding the occurance
4979 * of the second argument in the first or an empty string.
4980 *
4981 * @param[in] args Array of arguments.
4982 * @param[in] arg_count Count of elements in @p args.
4983 * @param[in,out] set Context and result set at the same time.
4984 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004985 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004986 */
4987static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004988xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004989{
4990 char *ptr;
4991 struct lysc_node_leaf *sleaf;
4992 LY_ERR rc = LY_SUCCESS;
4993
4994 if (options & LYXP_SCNODE_ALL) {
4995 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4996 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4997 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004998 } else if (!warn_is_string_type(sleaf->type)) {
4999 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005000 }
5001 }
5002
5003 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5004 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5005 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005006 } else if (!warn_is_string_type(sleaf->type)) {
5007 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005008 }
5009 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005010 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005011 return rc;
5012 }
5013
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005014 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005015 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005016 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005017 LY_CHECK_RET(rc);
5018
5019 ptr = strstr(args[0]->val.str, args[1]->val.str);
5020 if (ptr) {
5021 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5022 } else {
5023 set_fill_string(set, "", 0);
5024 }
5025
5026 return LY_SUCCESS;
5027}
5028
5029/**
5030 * @brief Execute the XPath substring-before(string, string) function.
5031 * Returns LYXP_SET_STRING with the string preceding the occurance
5032 * of the second argument in the first or an empty string.
5033 *
5034 * @param[in] args Array of arguments.
5035 * @param[in] arg_count Count of elements in @p args.
5036 * @param[in,out] set Context and result set at the same time.
5037 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005038 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005039 */
5040static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005041xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005042{
5043 char *ptr;
5044 struct lysc_node_leaf *sleaf;
5045 LY_ERR rc = LY_SUCCESS;
5046
5047 if (options & LYXP_SCNODE_ALL) {
5048 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5049 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5050 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005051 } else if (!warn_is_string_type(sleaf->type)) {
5052 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005053 }
5054 }
5055
5056 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5057 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5058 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005059 } else if (!warn_is_string_type(sleaf->type)) {
5060 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005061 }
5062 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005063 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005064 return rc;
5065 }
5066
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005067 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005068 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005069 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005070 LY_CHECK_RET(rc);
5071
5072 ptr = strstr(args[0]->val.str, args[1]->val.str);
5073 if (ptr) {
5074 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5075 } else {
5076 set_fill_string(set, "", 0);
5077 }
5078
5079 return LY_SUCCESS;
5080}
5081
5082/**
5083 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5084 * with the sum of all the nodes in the context.
5085 *
5086 * @param[in] args Array of arguments.
5087 * @param[in] arg_count Count of elements in @p args.
5088 * @param[in,out] set Context and result set at the same time.
5089 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005090 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005091 */
5092static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005093xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005094{
5095 long double num;
5096 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005097 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005098 struct lyxp_set set_item;
5099 struct lysc_node_leaf *sleaf;
5100 LY_ERR rc = LY_SUCCESS;
5101
5102 if (options & LYXP_SCNODE_ALL) {
5103 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5104 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005105 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005106 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5107 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5108 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005109 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005110 } else if (!warn_is_numeric_type(sleaf->type)) {
5111 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005112 }
5113 }
5114 }
5115 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005116 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005117 return rc;
5118 }
5119
5120 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005121
5122 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005123 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005124 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005125 } else if (!args[0]->used) {
5126 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005127 }
5128
Michal Vasko5c4e5892019-11-14 12:31:38 +01005129 set_init(&set_item, set);
5130
Michal Vasko03ff5a72019-09-11 13:49:33 +02005131 set_item.type = LYXP_SET_NODE_SET;
5132 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5133 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5134
5135 set_item.used = 1;
5136 set_item.size = 1;
5137
5138 for (i = 0; i < args[0]->used; ++i) {
5139 set_item.val.nodes[0] = args[0]->val.nodes[i];
5140
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005141 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005142 LY_CHECK_RET(rc);
5143 num = cast_string_to_number(str);
5144 free(str);
5145 set->val.num += num;
5146 }
5147
5148 free(set_item.val.nodes);
5149
5150 return LY_SUCCESS;
5151}
5152
5153/**
5154 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5155 * with the text content of the nodes in the context.
5156 *
5157 * @param[in] args Array of arguments.
5158 * @param[in] arg_count Count of elements in @p args.
5159 * @param[in,out] set Context and result set at the same time.
5160 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005161 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005162 */
5163static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005164xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005165{
5166 uint32_t i;
5167
5168 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005169 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005170 return LY_SUCCESS;
5171 }
5172
Michal Vasko03ff5a72019-09-11 13:49:33 +02005173 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005174 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005175 return LY_EVALID;
5176 }
5177
Michal Vaskod989ba02020-08-24 10:59:24 +02005178 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005179 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005180 case LYXP_NODE_NONE:
5181 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005182 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005183 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5184 set->val.nodes[i].type = LYXP_NODE_TEXT;
5185 ++i;
5186 break;
5187 }
Radek Krejci0f969882020-08-21 16:56:47 +02005188 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005189 case LYXP_NODE_ROOT:
5190 case LYXP_NODE_ROOT_CONFIG:
5191 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005192 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005193 set_remove_node(set, i);
5194 break;
5195 }
5196 }
5197
5198 return LY_SUCCESS;
5199}
5200
5201/**
5202 * @brief Execute the XPath translate(string, string, string) function.
5203 * Returns LYXP_SET_STRING with the first argument with the characters
5204 * from the second argument replaced by those on the corresponding
5205 * positions in the third argument.
5206 *
5207 * @param[in] args Array of arguments.
5208 * @param[in] arg_count Count of elements in @p args.
5209 * @param[in,out] set Context and result set at the same time.
5210 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005211 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005212 */
5213static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005214xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005215{
5216 uint16_t i, j, new_used;
5217 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005218 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005219 struct lysc_node_leaf *sleaf;
5220 LY_ERR rc = LY_SUCCESS;
5221
5222 if (options & LYXP_SCNODE_ALL) {
5223 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5224 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5225 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005226 } else if (!warn_is_string_type(sleaf->type)) {
5227 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005228 }
5229 }
5230
5231 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5232 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5233 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005234 } else if (!warn_is_string_type(sleaf->type)) {
5235 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005236 }
5237 }
5238
5239 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5240 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5241 LOGWRN(set->ctx, "Argument #3 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005242 } else if (!warn_is_string_type(sleaf->type)) {
5243 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005244 }
5245 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005246 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005247 return rc;
5248 }
5249
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005250 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005251 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005252 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005253 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005254 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005255 LY_CHECK_RET(rc);
5256
5257 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5258 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5259 new_used = 0;
5260
5261 have_removed = 0;
5262 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005263 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005264
5265 for (j = 0; args[1]->val.str[j]; ++j) {
5266 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5267 /* removing this char */
5268 if (j >= strlen(args[2]->val.str)) {
5269 have_removed = 1;
5270 found = 1;
5271 break;
5272 }
5273 /* replacing this char */
5274 new[new_used] = args[2]->val.str[j];
5275 ++new_used;
5276 found = 1;
5277 break;
5278 }
5279 }
5280
5281 /* copying this char */
5282 if (!found) {
5283 new[new_used] = args[0]->val.str[i];
5284 ++new_used;
5285 }
5286 }
5287
5288 if (have_removed) {
5289 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5290 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5291 }
5292 new[new_used] = '\0';
5293
Michal Vaskod3678892020-05-21 10:06:58 +02005294 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005295 set->type = LYXP_SET_STRING;
5296 set->val.str = new;
5297
5298 return LY_SUCCESS;
5299}
5300
5301/**
5302 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5303 * with true value.
5304 *
5305 * @param[in] args Array of arguments.
5306 * @param[in] arg_count Count of elements in @p args.
5307 * @param[in,out] set Context and result set at the same time.
5308 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005309 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005310 */
5311static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005312xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005313{
5314 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005315 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005316 return LY_SUCCESS;
5317 }
5318
5319 set_fill_boolean(set, 1);
5320 return LY_SUCCESS;
5321}
5322
5323/*
5324 * moveto functions
5325 *
5326 * They and only they actually change the context (set).
5327 */
5328
5329/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005330 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005331 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005332 * XPath @p set is expected to be a (sc)node set!
5333 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005334 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5335 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005336 * @param[in] set Set with general XPath context.
5337 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005338 * @param[out] moveto_mod Expected module of a matching node.
5339 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005340 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005341static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005342moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5343 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005344{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005345 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005346 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005347 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005348
Michal Vasko2104e9f2020-03-06 08:23:25 +01005349 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5350
Michal Vasko6346ece2019-09-24 13:12:53 +02005351 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5352 /* specific module */
5353 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005354 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005355
Michal Vasko004d3152020-06-11 19:59:22 +02005356 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005357 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005358 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005359 return LY_EVALID;
5360 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005361
Michal Vasko6346ece2019-09-24 13:12:53 +02005362 *qname += pref_len + 1;
5363 *qname_len -= pref_len + 1;
5364 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5365 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005366 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005367 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005368 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005369 case LY_VALUE_SCHEMA:
5370 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005371 /* current module */
5372 mod = set->cur_mod;
5373 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005374 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005375 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005376 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005377 /* inherit parent (context node) module */
5378 if (ctx_scnode) {
5379 mod = ctx_scnode->module;
5380 } else {
5381 mod = NULL;
5382 }
5383 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005384 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005385 /* all nodes need to be prefixed */
5386 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5387 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005388 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005389 }
5390
Michal Vasko6346ece2019-09-24 13:12:53 +02005391 *moveto_mod = mod;
5392 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005393}
5394
5395/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005396 * @brief Move context @p set to the root. Handles absolute path.
5397 * Result is LYXP_SET_NODE_SET.
5398 *
5399 * @param[in,out] set Set to use.
5400 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005401 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005402 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005403static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005404moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005405{
aPiecek8b0cc152021-05-31 16:40:31 +02005406 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005407
5408 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005409 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskob0099a92020-08-31 14:55:23 +02005410 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005411 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005412 set->type = LYXP_SET_NODE_SET;
5413 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005414 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005415 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005416
5417 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005418}
5419
5420/**
5421 * @brief Check @p node as a part of NameTest processing.
5422 *
5423 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005424 * @param[in] ctx_node Context node.
5425 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005426 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005427 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005428 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005429 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5430 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005431 */
5432static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005433moveto_node_check(const struct lyd_node *node, const struct lyd_node *ctx_node, const struct lyxp_set *set,
Michal Vaskocdad7122020-11-09 21:04:44 +01005434 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005435{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005436 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5437 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005438 case LY_VALUE_SCHEMA:
5439 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005440 /* use current module */
5441 moveto_mod = set->cur_mod;
5442 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005443 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005444 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005445 /* inherit module of the context node, if any */
5446 if (ctx_node) {
5447 moveto_mod = ctx_node->schema->module;
5448 }
5449 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005450 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005451 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005452 /* not defined */
5453 LOGINT(set->ctx);
5454 return LY_EINVAL;
5455 }
5456 }
5457
Michal Vasko03ff5a72019-09-11 13:49:33 +02005458 /* module check */
5459 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005460 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005461 }
5462
Michal Vasko5c4e5892019-11-14 12:31:38 +01005463 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005464 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005465 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005466 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5467 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005468 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005469 }
5470
5471 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005472 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005473 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005474 }
5475
Michal Vaskoa1424542019-11-14 16:08:52 +01005476 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005477 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005478 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005479 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005480
5481 /* match */
5482 return LY_SUCCESS;
5483}
5484
5485/**
5486 * @brief Check @p node as a part of schema NameTest processing.
5487 *
5488 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005489 * @param[in] ctx_scnode Context node.
5490 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005491 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005492 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005493 * @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 +02005494 */
5495static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005496moveto_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 +02005497 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005498{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005499 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5500 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005501 case LY_VALUE_SCHEMA:
5502 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005503 /* use current module */
5504 moveto_mod = set->cur_mod;
5505 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005506 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005507 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005508 /* inherit module of the context node, if any */
5509 if (ctx_scnode) {
5510 moveto_mod = ctx_scnode->module;
5511 }
5512 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005513 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005514 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005515 /* not defined */
5516 LOGINT(set->ctx);
5517 return LY_EINVAL;
5518 }
5519 }
5520
Michal Vasko03ff5a72019-09-11 13:49:33 +02005521 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005522 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005523 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005524 }
5525
5526 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005527 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005528 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005529 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005530 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005531 }
5532
5533 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005534 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005535 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005536 }
5537
5538 /* match */
5539 return LY_SUCCESS;
5540}
5541
5542/**
Michal Vaskod3678892020-05-21 10:06:58 +02005543 * @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 +02005544 *
5545 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005546 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005547 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005548 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005549 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5550 */
5551static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005552moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005553{
Michal Vaskof03ed032020-03-04 13:31:44 +01005554 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005555 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005556 LY_ERR rc;
5557
aPiecek8b0cc152021-05-31 16:40:31 +02005558 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005559 return LY_SUCCESS;
5560 }
5561
5562 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005563 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005564 return LY_EVALID;
5565 }
5566
Michal Vasko03ff5a72019-09-11 13:49:33 +02005567 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005568 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005569
5570 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 +01005571 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005572
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005573 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005574 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005575 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005576 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005577 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005578 ctx_node = set->val.nodes[i].node;
5579 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005580 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005581
Michal Vaskod3678892020-05-21 10:06:58 +02005582 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005583 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005584 if (rc == LY_SUCCESS) {
5585 if (!replaced) {
5586 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5587 replaced = 1;
5588 } else {
5589 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005590 }
Michal Vaskod3678892020-05-21 10:06:58 +02005591 ++i;
5592 } else if (rc == LY_EINCOMPLETE) {
5593 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005594 }
5595 }
5596
5597 if (!replaced) {
5598 /* no match */
5599 set_remove_node(set, i);
5600 }
5601 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005602
5603 return LY_SUCCESS;
5604}
5605
5606/**
Michal Vaskod3678892020-05-21 10:06:58 +02005607 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5608 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005609 *
5610 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005611 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005612 * @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 +01005613 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005614 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5615 */
5616static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005617moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5618 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005619{
Michal Vasko004d3152020-06-11 19:59:22 +02005620 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005621 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005622 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005623 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005624
Michal Vasko004d3152020-06-11 19:59:22 +02005625 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005626
aPiecek8b0cc152021-05-31 16:40:31 +02005627 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02005628 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005629 }
5630
5631 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005632 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005633 ret = LY_EVALID;
5634 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005635 }
5636
5637 /* context check for all the nodes since we have the schema node */
5638 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5639 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005640 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005641 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5642 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005643 lyxp_set_free_content(set);
5644 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005645 }
5646
5647 /* create specific data instance if needed */
5648 if (scnode->nodetype == LYS_LIST) {
5649 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5650 } else if (scnode->nodetype == LYS_LEAFLIST) {
5651 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005652 }
5653
5654 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005655 siblings = NULL;
5656
5657 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5658 assert(!set->val.nodes[i].node);
5659
5660 /* search in all the trees */
5661 siblings = set->tree;
5662 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5663 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005664 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005665 }
5666
5667 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005668 if (inst) {
5669 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005670 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005671 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005672 }
5673
5674 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005675 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005676 ret = LY_EINCOMPLETE;
5677 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005678 }
5679
5680 if (sub) {
5681 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005682 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005683 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005684 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005685 /* no match */
5686 set_remove_node(set, i);
5687 }
5688 }
5689
Michal Vasko004d3152020-06-11 19:59:22 +02005690cleanup:
5691 lyd_free_tree(inst);
5692 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005693}
5694
5695/**
5696 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5697 *
5698 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005699 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005700 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005701 * @param[in] options XPath options.
5702 * @return LY_ERR
5703 */
5704static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005705moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005706{
Radek Krejci857189e2020-09-01 13:26:36 +02005707 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005708 uint32_t getnext_opts;
5709 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005710 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005711 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005712 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005713
aPiecek8b0cc152021-05-31 16:40:31 +02005714 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005715 return LY_SUCCESS;
5716 }
5717
5718 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005719 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005720 return LY_EVALID;
5721 }
5722
Michal Vaskocafad9d2019-11-07 15:20:03 +01005723 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005724 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005725 if (options & LYXP_SCNODE_OUTPUT) {
5726 getnext_opts |= LYS_GETNEXT_OUTPUT;
5727 }
5728
Michal Vasko03ff5a72019-09-11 13:49:33 +02005729 orig_used = set->used;
5730 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005731 uint32_t idx;
5732
Radek Krejcif13b87b2020-12-01 22:02:17 +01005733 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5734 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005735 continue;
5736 }
5737
5738 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005739 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005740 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005741 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005742 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005743
5744 start_parent = set->val.scnodes[i].scnode;
5745
5746 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 +02005747 /* 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 +02005748 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005749 mod_idx = 0;
Michal Vaskoa51ef072021-07-02 10:40:30 +02005750 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005751 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005752 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005753 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005754 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005755 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5756
Michal Vasko03ff5a72019-09-11 13:49:33 +02005757 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005758 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005759 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005760 temp_ctx = 1;
5761 }
5762 }
5763 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005764 }
5765
Michal Vasko519fd602020-05-26 12:17:39 +02005766 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5767 iter = NULL;
5768 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005769 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005770 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5771
5772 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005773 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005774 temp_ctx = 1;
5775 }
5776 }
5777 }
5778 }
5779 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005780
5781 /* correct temporary in_ctx values */
5782 if (temp_ctx) {
5783 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005784 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5785 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005786 }
5787 }
5788 }
5789
5790 return LY_SUCCESS;
5791}
5792
5793/**
Michal Vaskod3678892020-05-21 10:06:58 +02005794 * @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 +02005795 * Context position aware.
5796 *
5797 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005798 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005799 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005800 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005801 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5802 */
5803static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005804moveto_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 +02005805{
5806 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005807 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005808 struct lyxp_set ret_set;
5809 LY_ERR rc;
5810
aPiecek8b0cc152021-05-31 16:40:31 +02005811 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005812 return LY_SUCCESS;
5813 }
5814
5815 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005816 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005817 return LY_EVALID;
5818 }
5819
Michal Vasko9f96a052020-03-10 09:41:45 +01005820 /* 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 +01005821 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005822 LY_CHECK_RET(rc);
5823
Michal Vasko6346ece2019-09-24 13:12:53 +02005824 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005825 set_init(&ret_set, set);
5826 for (i = 0; i < set->used; ++i) {
5827
5828 /* TREE DFS */
5829 start = set->val.nodes[i].node;
5830 for (elem = next = start; elem; elem = next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005831 rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005832 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005833 /* add matching node into result set */
5834 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5835 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5836 /* the node is a duplicate, we'll process it later in the set */
5837 goto skip_children;
5838 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005839 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005840 return rc;
5841 } else if (rc == LY_EINVAL) {
5842 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005843 }
5844
5845 /* TREE DFS NEXT ELEM */
5846 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005847 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005848 if (!next) {
5849skip_children:
5850 /* no children, so try siblings, but only if it's not the start,
5851 * that is considered to be the root and it's siblings are not traversed */
5852 if (elem != start) {
5853 next = elem->next;
5854 } else {
5855 break;
5856 }
5857 }
5858 while (!next) {
5859 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01005860 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005861 /* we are done, no next element to process */
5862 break;
5863 }
5864 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01005865 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005866 next = elem->next;
5867 }
5868 }
5869 }
5870
5871 /* make the temporary set the current one */
5872 ret_set.ctx_pos = set->ctx_pos;
5873 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005874 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005875 memcpy(set, &ret_set, sizeof *set);
5876
5877 return LY_SUCCESS;
5878}
5879
5880/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005881 * @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 +02005882 *
5883 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005884 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005885 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005886 * @param[in] options XPath options.
5887 * @return LY_ERR
5888 */
5889static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005890moveto_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 +02005891{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005892 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005893 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005894 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005895
aPiecek8b0cc152021-05-31 16:40:31 +02005896 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005897 return LY_SUCCESS;
5898 }
5899
5900 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005901 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005902 return LY_EVALID;
5903 }
5904
Michal Vasko03ff5a72019-09-11 13:49:33 +02005905 orig_used = set->used;
5906 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005907 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5908 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005909 continue;
5910 }
5911
5912 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005913 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005914 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005915 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005916 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005917
5918 /* TREE DFS */
5919 start = set->val.scnodes[i].scnode;
5920 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005921 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5922 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005923 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005924 }
5925
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005926 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005927 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005928 uint32_t idx;
5929
5930 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005931 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005932 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005933 /* we will process it later in the set */
5934 goto skip_children;
5935 }
5936 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005937 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005938 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005939 } else if (rc == LY_EINVAL) {
5940 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005941 }
5942
5943next_iter:
5944 /* TREE DFS NEXT ELEM */
5945 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005946 next = lysc_node_child(elem);
5947 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5948 next = next->next;
5949 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5950 next = next->next;
5951 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005952 if (!next) {
5953skip_children:
5954 /* no children, so try siblings, but only if it's not the start,
5955 * that is considered to be the root and it's siblings are not traversed */
5956 if (elem != start) {
5957 next = elem->next;
5958 } else {
5959 break;
5960 }
5961 }
5962 while (!next) {
5963 /* no siblings, go back through the parents */
5964 if (elem->parent == start) {
5965 /* we are done, no next element to process */
5966 break;
5967 }
5968 /* parent is already processed, go to its sibling */
5969 elem = elem->parent;
5970 next = elem->next;
5971 }
5972 }
5973 }
5974
5975 return LY_SUCCESS;
5976}
5977
5978/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005979 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005980 * Indirectly context position aware.
5981 *
5982 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005983 * @param[in] mod Matching metadata module, NULL for any.
5984 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02005985 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005986 * @return LY_ERR
5987 */
5988static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02005989moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005990{
Michal Vasko9f96a052020-03-10 09:41:45 +01005991 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005992
aPiecek8b0cc152021-05-31 16:40:31 +02005993 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005994 return LY_SUCCESS;
5995 }
5996
5997 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005998 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005999 return LY_EVALID;
6000 }
6001
Radek Krejci1deb5be2020-08-26 16:43:36 +02006002 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006003 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006004
6005 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
6006 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01006007 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006008 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006009
6010 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006011 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006012 continue;
6013 }
6014
Michal Vaskod3678892020-05-21 10:06:58 +02006015 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006016 /* match */
6017 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006018 set->val.meta[i].meta = sub;
6019 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006020 /* pos does not change */
6021 replaced = 1;
6022 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006023 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 +02006024 }
6025 ++i;
6026 }
6027 }
6028 }
6029
6030 if (!replaced) {
6031 /* no match */
6032 set_remove_node(set, i);
6033 }
6034 }
6035
6036 return LY_SUCCESS;
6037}
6038
6039/**
6040 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02006041 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006042 *
6043 * @param[in,out] set1 Set to use for the result.
6044 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006045 * @return LY_ERR
6046 */
6047static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006048moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006049{
6050 LY_ERR rc;
6051
Michal Vaskod3678892020-05-21 10:06:58 +02006052 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006053 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006054 return LY_EVALID;
6055 }
6056
6057 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02006058 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006059 return LY_SUCCESS;
6060 }
6061
Michal Vaskod3678892020-05-21 10:06:58 +02006062 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006063 memcpy(set1, set2, sizeof *set1);
6064 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02006065 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006066 return LY_SUCCESS;
6067 }
6068
6069 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006070 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006071
6072 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006073 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006074 LY_CHECK_RET(rc);
6075
6076 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006077 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006078
6079 return LY_SUCCESS;
6080}
6081
6082/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006083 * @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 +02006084 * Context position aware.
6085 *
6086 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006087 * @param[in] mod Matching metadata module, NULL for any.
6088 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006089 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006090 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6091 */
6092static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006093moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006094{
Michal Vasko9f96a052020-03-10 09:41:45 +01006095 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006096 struct lyxp_set *set_all_desc = NULL;
6097 LY_ERR rc;
6098
aPiecek8b0cc152021-05-31 16:40:31 +02006099 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006100 return LY_SUCCESS;
6101 }
6102
6103 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006104 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006105 return LY_EVALID;
6106 }
6107
Michal Vasko03ff5a72019-09-11 13:49:33 +02006108 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6109 * but it likely won't be used much, so it's a waste of time */
6110 /* copy the context */
6111 set_all_desc = set_copy(set);
6112 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006113 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006114 if (rc != LY_SUCCESS) {
6115 lyxp_set_free(set_all_desc);
6116 return rc;
6117 }
6118 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006119 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006120 if (rc != LY_SUCCESS) {
6121 lyxp_set_free(set_all_desc);
6122 return rc;
6123 }
6124 lyxp_set_free(set_all_desc);
6125
Radek Krejci1deb5be2020-08-26 16:43:36 +02006126 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006127 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006128
6129 /* only attributes of an elem can be in the result, skip all the rest,
6130 * we have all attributes qualified in lyd tree */
6131 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006132 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006133 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006134 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006135 continue;
6136 }
6137
Michal Vaskod3678892020-05-21 10:06:58 +02006138 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006139 /* match */
6140 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006141 set->val.meta[i].meta = sub;
6142 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006143 /* pos does not change */
6144 replaced = 1;
6145 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006146 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 +02006147 }
6148 ++i;
6149 }
6150 }
6151 }
6152
6153 if (!replaced) {
6154 /* no match */
6155 set_remove_node(set, i);
6156 }
6157 }
6158
6159 return LY_SUCCESS;
6160}
6161
6162/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006163 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6164 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006165 *
6166 * @param[in] parent Current parent.
6167 * @param[in] parent_pos Position of @p parent.
6168 * @param[in] parent_type Node type of @p parent.
6169 * @param[in,out] to_set Set to use.
6170 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006171 * @param[in] options XPath options.
6172 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6173 */
6174static LY_ERR
6175moveto_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 +02006176 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006177{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006178 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006179 LY_ERR rc;
6180
6181 switch (parent_type) {
6182 case LYXP_NODE_ROOT:
6183 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006184 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006185
Michal Vasko61ac2f62020-05-25 12:39:51 +02006186 /* add all top-level nodes as elements */
6187 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006188 break;
6189 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006190 /* add just the text node of this term element node */
6191 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006192 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6193 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6194 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006195 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006196 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006197
6198 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006199 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006200 break;
6201 default:
6202 LOGINT_RET(parent->schema->module->ctx);
6203 }
6204
Michal Vasko61ac2f62020-05-25 12:39:51 +02006205 /* add all top-level nodes as elements */
6206 LY_LIST_FOR(first, iter) {
6207 /* context check */
6208 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6209 continue;
6210 }
6211
6212 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006213 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006214 return LY_EINCOMPLETE;
6215 }
6216
6217 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6218 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6219
6220 /* also add all the children of this node, recursively */
6221 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6222 LY_CHECK_RET(rc);
6223 }
6224 }
6225
Michal Vasko03ff5a72019-09-11 13:49:33 +02006226 return LY_SUCCESS;
6227}
6228
6229/**
6230 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6231 * (or LYXP_SET_EMPTY). Context position aware.
6232 *
6233 * @param[in,out] set Set to use.
6234 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6235 * @param[in] options XPath options.
6236 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6237 */
6238static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006239moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006240{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006241 struct lyxp_set ret_set;
6242 LY_ERR rc;
6243
aPiecek8b0cc152021-05-31 16:40:31 +02006244 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006245 return LY_SUCCESS;
6246 }
6247
6248 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006249 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006250 return LY_EVALID;
6251 }
6252
6253 /* nothing to do */
6254 if (!all_desc) {
6255 return LY_SUCCESS;
6256 }
6257
Michal Vasko03ff5a72019-09-11 13:49:33 +02006258 /* add all the children, they get added recursively */
6259 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006260 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006261 /* copy the current node to tmp */
6262 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6263
6264 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006265 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006266 continue;
6267 }
6268
Michal Vasko03ff5a72019-09-11 13:49:33 +02006269 /* add all the children */
6270 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 +02006271 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006272 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006273 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006274 return rc;
6275 }
6276 }
6277
6278 /* use the temporary set as the current one */
6279 ret_set.ctx_pos = set->ctx_pos;
6280 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006281 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006282 memcpy(set, &ret_set, sizeof *set);
6283
6284 return LY_SUCCESS;
6285}
6286
6287/**
6288 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6289 * (or LYXP_SET_EMPTY).
6290 *
6291 * @param[in,out] set Set to use.
6292 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6293 * @param[in] options XPath options.
6294 * @return LY_ERR
6295 */
6296static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006297moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006298{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006299 uint32_t getnext_opts;
6300 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006301 const struct lysc_node *iter, *start_parent;
6302 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006303
aPiecek8b0cc152021-05-31 16:40:31 +02006304 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006305 return LY_SUCCESS;
6306 }
6307
6308 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006309 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006310 return LY_EVALID;
6311 }
6312
Michal Vasko519fd602020-05-26 12:17:39 +02006313 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006314 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006315 if (options & LYXP_SCNODE_OUTPUT) {
6316 getnext_opts |= LYS_GETNEXT_OUTPUT;
6317 }
6318
6319 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006320 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006321 if (!all_desc) {
6322 /* traverse the start node */
6323 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6324 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6325 }
6326 continue;
6327 }
6328
Radek Krejcif13b87b2020-12-01 22:02:17 +01006329 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6330 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006331 continue;
6332 }
6333
Michal Vasko519fd602020-05-26 12:17:39 +02006334 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006335 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006336 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006337 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006338 }
6339
Michal Vasko519fd602020-05-26 12:17:39 +02006340 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006341
Michal Vasko519fd602020-05-26 12:17:39 +02006342 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6343 /* it can actually be in any module, it's all <running> */
6344 mod_idx = 0;
Michal Vaskoa51ef072021-07-02 10:40:30 +02006345 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02006346 iter = NULL;
6347 /* module may not be implemented */
6348 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6349 /* context check */
6350 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6351 continue;
6352 }
6353
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006354 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006355 /* throw away the insert index, we want to consider that node again, recursively */
6356 }
6357 }
6358
6359 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6360 iter = NULL;
6361 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006362 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006363 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006364 continue;
6365 }
6366
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006367 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006368 }
6369 }
6370 }
6371
6372 return LY_SUCCESS;
6373}
6374
6375/**
6376 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6377 * (or LYXP_SET_EMPTY). Context position aware.
6378 *
6379 * @param[in] set Set to use.
6380 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6381 * @param[in] options XPath options.
6382 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6383 */
6384static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006385moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006386{
6387 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006388 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006389 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006390
aPiecek8b0cc152021-05-31 16:40:31 +02006391 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006392 return LY_SUCCESS;
6393 }
6394
6395 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006396 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006397 return LY_EVALID;
6398 }
6399
6400 if (all_desc) {
6401 /* <path>//.. == <path>//./.. */
6402 rc = moveto_self(set, 1, options);
6403 LY_CHECK_RET(rc);
6404 }
6405
Radek Krejci1deb5be2020-08-26 16:43:36 +02006406 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006407 node = set->val.nodes[i].node;
6408
6409 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9e685082021-01-29 14:49:09 +01006410 new_node = lyd_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006411 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6412 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006413 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6414 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006415 if (!new_node) {
6416 LOGINT_RET(set->ctx);
6417 }
6418 } else {
6419 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006420 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006421 continue;
6422 }
6423
Michal Vaskoa1424542019-11-14 16:08:52 +01006424 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006425 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 +02006426 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006427 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006428
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006429 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006430 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006431 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006432
Michal Vasko03ff5a72019-09-11 13:49:33 +02006433 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006434 /* 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 +02006435 new_type = LYXP_NODE_ELEM;
6436 }
6437
Michal Vasko03ff5a72019-09-11 13:49:33 +02006438 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006439 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006440 } else {
6441 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006442 }
6443 }
6444
Michal Vasko2caefc12019-11-14 16:07:56 +01006445 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006446 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006447
6448 return LY_SUCCESS;
6449}
6450
6451/**
6452 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6453 * (or LYXP_SET_EMPTY).
6454 *
6455 * @param[in] set Set to use.
6456 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6457 * @param[in] options XPath options.
6458 * @return LY_ERR
6459 */
6460static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006461moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006462{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006463 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006464 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006465 const struct lysc_node *node, *new_node;
6466 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006467
aPiecek8b0cc152021-05-31 16:40:31 +02006468 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006469 return LY_SUCCESS;
6470 }
6471
6472 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006473 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006474 return LY_EVALID;
6475 }
6476
6477 if (all_desc) {
6478 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006479 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006480 }
6481
Michal Vasko03ff5a72019-09-11 13:49:33 +02006482 orig_used = set->used;
6483 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006484 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6485 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006486 continue;
6487 }
6488
6489 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006490 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006491 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006492 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006493 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006494
6495 node = set->val.scnodes[i].scnode;
6496
6497 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006498 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006499 } else {
6500 /* root does not have a parent */
6501 continue;
6502 }
6503
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006504 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006505 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006506 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006507
Michal Vasko03ff5a72019-09-11 13:49:33 +02006508 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006509 /* 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 +02006510 new_type = LYXP_NODE_ELEM;
6511 }
6512
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006513 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6514 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006515 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006516 temp_ctx = 1;
6517 }
6518 }
6519
6520 if (temp_ctx) {
6521 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006522 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6523 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006524 }
6525 }
6526 }
6527
6528 return LY_SUCCESS;
6529}
6530
6531/**
6532 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6533 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6534 *
6535 * @param[in,out] set1 Set to use for the result.
6536 * @param[in] set2 Set acting as the second operand for @p op.
6537 * @param[in] op Comparison operator to process.
6538 * @param[in] options XPath options.
6539 * @return LY_ERR
6540 */
6541static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006542moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006543{
6544 /*
6545 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6546 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6547 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6548 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6549 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6550 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6551 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6552 *
6553 * '=' or '!='
6554 * BOOLEAN + BOOLEAN
6555 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6556 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6557 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6558 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6559 * NUMBER + NUMBER
6560 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6561 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6562 * STRING + STRING
6563 *
6564 * '<=', '<', '>=', '>'
6565 * NUMBER + NUMBER
6566 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6567 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6568 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6569 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6570 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6571 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6572 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6573 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6574 */
6575 struct lyxp_set iter1, iter2;
6576 int result;
6577 int64_t i;
6578 LY_ERR rc;
6579
Michal Vasko004d3152020-06-11 19:59:22 +02006580 memset(&iter1, 0, sizeof iter1);
6581 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006582
6583 /* iterative evaluation with node-sets */
6584 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6585 if (set1->type == LYXP_SET_NODE_SET) {
6586 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006587 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006588 switch (set2->type) {
6589 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006590 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006591 break;
6592 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006593 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006594 break;
6595 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006596 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006597 break;
6598 }
6599 LY_CHECK_RET(rc);
6600
Michal Vasko4c7763f2020-07-27 17:40:37 +02006601 /* canonize set2 */
6602 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6603
6604 /* compare recursively */
6605 rc = moveto_op_comp(&iter1, &iter2, op, options);
6606 lyxp_set_free_content(&iter2);
6607 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006608
6609 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006610 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006611 set_fill_boolean(set1, 1);
6612 return LY_SUCCESS;
6613 }
6614 }
6615 } else {
6616 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006617 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006618 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006619 case LYXP_SET_NUMBER:
6620 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6621 break;
6622 case LYXP_SET_BOOLEAN:
6623 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6624 break;
6625 default:
6626 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6627 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006628 }
6629 LY_CHECK_RET(rc);
6630
Michal Vasko4c7763f2020-07-27 17:40:37 +02006631 /* canonize set1 */
6632 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 +02006633
Michal Vasko4c7763f2020-07-27 17:40:37 +02006634 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006635 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006636 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006637 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006638
6639 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006640 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006641 set_fill_boolean(set1, 1);
6642 return LY_SUCCESS;
6643 }
6644 }
6645 }
6646
6647 /* false for all nodes */
6648 set_fill_boolean(set1, 0);
6649 return LY_SUCCESS;
6650 }
6651
6652 /* first convert properly */
6653 if ((op[0] == '=') || (op[0] == '!')) {
6654 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006655 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6656 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006657 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006658 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006659 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006660 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006661 LY_CHECK_RET(rc);
6662 } /* else we have 2 strings */
6663 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006664 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006665 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006666 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006667 LY_CHECK_RET(rc);
6668 }
6669
6670 assert(set1->type == set2->type);
6671
6672 /* compute result */
6673 if (op[0] == '=') {
6674 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006675 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006676 } else if (set1->type == LYXP_SET_NUMBER) {
6677 result = (set1->val.num == set2->val.num);
6678 } else {
6679 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006680 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006681 }
6682 } else if (op[0] == '!') {
6683 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006684 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006685 } else if (set1->type == LYXP_SET_NUMBER) {
6686 result = (set1->val.num != set2->val.num);
6687 } else {
6688 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006689 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006690 }
6691 } else {
6692 assert(set1->type == LYXP_SET_NUMBER);
6693 if (op[0] == '<') {
6694 if (op[1] == '=') {
6695 result = (set1->val.num <= set2->val.num);
6696 } else {
6697 result = (set1->val.num < set2->val.num);
6698 }
6699 } else {
6700 if (op[1] == '=') {
6701 result = (set1->val.num >= set2->val.num);
6702 } else {
6703 result = (set1->val.num > set2->val.num);
6704 }
6705 }
6706 }
6707
6708 /* assign result */
6709 if (result) {
6710 set_fill_boolean(set1, 1);
6711 } else {
6712 set_fill_boolean(set1, 0);
6713 }
6714
6715 return LY_SUCCESS;
6716}
6717
6718/**
6719 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6720 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6721 *
6722 * @param[in,out] set1 Set to use for the result.
6723 * @param[in] set2 Set acting as the second operand for @p op.
6724 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006725 * @return LY_ERR
6726 */
6727static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006728moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006729{
6730 LY_ERR rc;
6731
6732 /* unary '-' */
6733 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006734 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006735 LY_CHECK_RET(rc);
6736 set1->val.num *= -1;
6737 lyxp_set_free(set2);
6738 return LY_SUCCESS;
6739 }
6740
6741 assert(set1 && set2);
6742
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006743 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006744 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006745 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006746 LY_CHECK_RET(rc);
6747
6748 switch (op[0]) {
6749 /* '+' */
6750 case '+':
6751 set1->val.num += set2->val.num;
6752 break;
6753
6754 /* '-' */
6755 case '-':
6756 set1->val.num -= set2->val.num;
6757 break;
6758
6759 /* '*' */
6760 case '*':
6761 set1->val.num *= set2->val.num;
6762 break;
6763
6764 /* 'div' */
6765 case 'd':
6766 set1->val.num /= set2->val.num;
6767 break;
6768
6769 /* 'mod' */
6770 case 'm':
6771 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6772 break;
6773
6774 default:
6775 LOGINT_RET(set1->ctx);
6776 }
6777
6778 return LY_SUCCESS;
6779}
6780
6781/*
6782 * eval functions
6783 *
6784 * They execute a parsed XPath expression on some data subtree.
6785 */
6786
6787/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006788 * @brief Evaluate Predicate. Logs directly on error.
6789 *
Michal Vaskod3678892020-05-21 10:06:58 +02006790 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006791 *
6792 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006793 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02006794 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006795 * @param[in] options XPath options.
6796 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6797 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6798 */
6799static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006800eval_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 +02006801{
6802 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006803 uint16_t orig_exp;
6804 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006805 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006806 struct lyxp_set set2;
6807 struct lyd_node *orig_parent;
6808
6809 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02006810 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006811 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006812 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006813
aPiecek8b0cc152021-05-31 16:40:31 +02006814 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006815only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02006816 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006817 LY_CHECK_RET(rc);
6818 } else if (set->type == LYXP_SET_NODE_SET) {
6819 /* 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 +01006820 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006821
6822 /* empty set, nothing to evaluate */
6823 if (!set->used) {
6824 goto only_parse;
6825 }
6826
Michal Vasko004d3152020-06-11 19:59:22 +02006827 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006828 orig_pos = 0;
6829 orig_size = set->used;
6830 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006831 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006832 set_init(&set2, set);
6833 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6834 /* remember the node context position for position() and context size for last(),
6835 * predicates should always be evaluated with respect to the child axis (since we do
6836 * not support explicit axes) so we assign positions based on their parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006837 if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) {
6838 orig_parent = lyd_parent(set->val.nodes[i].node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006839 orig_pos = 1;
6840 } else {
6841 ++orig_pos;
6842 }
6843
6844 set2.ctx_pos = orig_pos;
6845 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006846 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006847
Michal Vasko004d3152020-06-11 19:59:22 +02006848 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006849 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006850 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006851 return rc;
6852 }
6853
6854 /* number is a position */
6855 if (set2.type == LYXP_SET_NUMBER) {
6856 if ((long long)set2.val.num == orig_pos) {
6857 set2.val.num = 1;
6858 } else {
6859 set2.val.num = 0;
6860 }
6861 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006862 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006863
6864 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006865 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006866 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006867 }
6868 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006869 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006870
6871 } else if (set->type == LYXP_SET_SCNODE_SET) {
6872 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006873 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006874 /* there is a currently-valid node */
6875 break;
6876 }
6877 }
6878 /* empty set, nothing to evaluate */
6879 if (i == set->used) {
6880 goto only_parse;
6881 }
6882
Michal Vasko004d3152020-06-11 19:59:22 +02006883 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006884
Michal Vasko03ff5a72019-09-11 13:49:33 +02006885 /* set special in_ctx to all the valid snodes */
6886 pred_in_ctx = set_scnode_new_in_ctx(set);
6887
6888 /* use the valid snodes one-by-one */
6889 for (i = 0; i < set->used; ++i) {
6890 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6891 continue;
6892 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006893 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006894
Michal Vasko004d3152020-06-11 19:59:22 +02006895 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006896
Michal Vasko004d3152020-06-11 19:59:22 +02006897 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006898 LY_CHECK_RET(rc);
6899
6900 set->val.scnodes[i].in_ctx = pred_in_ctx;
6901 }
6902
6903 /* restore the state as it was before the predicate */
6904 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006905 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02006906 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006907 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006908 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006909 }
6910 }
6911
6912 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006913 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006914 set_fill_set(&set2, set);
6915
Michal Vasko004d3152020-06-11 19:59:22 +02006916 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006917 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006918 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006919 return rc;
6920 }
6921
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006922 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006923 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006924 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006925 }
Michal Vaskod3678892020-05-21 10:06:58 +02006926 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006927 }
6928
6929 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006930 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02006931 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006932 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006933 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006934
6935 return LY_SUCCESS;
6936}
6937
6938/**
Michal Vaskod3678892020-05-21 10:06:58 +02006939 * @brief Evaluate Literal. Logs directly on error.
6940 *
6941 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006942 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006943 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6944 */
6945static void
Michal Vasko40308e72020-10-20 16:38:40 +02006946eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006947{
6948 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006949 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006950 set_fill_string(set, "", 0);
6951 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006952 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 +02006953 }
6954 }
6955 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006956 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006957 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006958}
6959
6960/**
Michal Vasko004d3152020-06-11 19:59:22 +02006961 * @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 +02006962 *
Michal Vasko004d3152020-06-11 19:59:22 +02006963 * @param[in] exp Full parsed XPath expression.
6964 * @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 +02006965 * @param[in] ctx_node Found schema node as the context for the predicate.
6966 * @param[in] cur_mod Current module for the expression.
6967 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006968 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006969 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006970 * @param[out] predicates Parsed predicates.
6971 * @param[out] pred_type Type of @p predicates.
6972 * @return LY_SUCCESS on success,
6973 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006974 */
6975static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006976eval_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 +02006977 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 +02006978 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006979{
Michal Vasko004d3152020-06-11 19:59:22 +02006980 LY_ERR ret = LY_SUCCESS;
6981 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006982 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006983 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006984 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006985 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006986
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006987 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006988
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006989 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006990 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006991 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006992 return LY_EINVAL;
6993 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006994 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 +02006995 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006996
Michal Vasko004d3152020-06-11 19:59:22 +02006997 /* learn where the predicates end */
6998 e_idx = *tok_idx;
6999 while (key_count) {
7000 /* '[' */
7001 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7002 return LY_EINVAL;
7003 }
7004 ++e_idx;
7005
Michal Vasko3354d272021-04-06 09:40:06 +02007006 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
7007 /* definitely not a key */
7008 return LY_EINVAL;
7009 }
7010
Michal Vasko004d3152020-06-11 19:59:22 +02007011 /* ']' */
7012 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7013 ++e_idx;
7014 }
7015 ++e_idx;
7016
7017 /* another presumably key predicate parsed */
7018 --key_count;
7019 }
Michal Vasko004d3152020-06-11 19:59:22 +02007020 } else {
7021 /* learn just where this single predicate ends */
7022 e_idx = *tok_idx;
7023
Michal Vaskod3678892020-05-21 10:06:58 +02007024 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007025 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7026 return LY_EINVAL;
7027 }
7028 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007029
Michal Vasko3354d272021-04-06 09:40:06 +02007030 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
7031 /* definitely not the value */
7032 return LY_EINVAL;
7033 }
7034
Michal Vaskod3678892020-05-21 10:06:58 +02007035 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007036 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7037 ++e_idx;
7038 }
7039 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007040 }
7041
Michal Vasko004d3152020-06-11 19:59:22 +02007042 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
7043 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
7044
7045 /* turn logging off */
7046 prev_lo = ly_log_options(0);
7047
7048 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007049 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
7050 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007051
7052 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007053 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
7054 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02007055 LY_CHECK_GOTO(ret, cleanup);
7056
7057 /* success, the predicate must include all the needed information for hash-based search */
7058 *tok_idx = e_idx;
7059
7060cleanup:
7061 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007062 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02007063 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02007064}
7065
7066/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007067 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7068 * data node(s) could be found using a single hash-based search.
7069 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007070 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007071 * @param[in] node Next context node to check.
7072 * @param[in] name Expected node name.
7073 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007074 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007075 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007076 * @param[in] format Prefix format.
7077 * @param[in,out] found Previously found node, is updated.
7078 * @return LY_SUCCESS on success,
7079 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7080 */
7081static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007082eval_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 +02007083 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 +02007084 const struct lysc_node **found)
7085{
7086 const struct lysc_node *scnode;
7087 const struct lys_module *mod;
7088 uint32_t idx = 0;
7089
Radek Krejci8df109d2021-04-23 12:19:08 +02007090 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007091
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007092continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007093 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007094 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007095 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007096 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007097 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007098 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7099 if (scnode) {
7100 /* we have found a match */
7101 break;
7102 }
7103 }
7104
Michal Vasko7d1d0912020-10-16 08:38:30 +02007105 if (!scnode) {
7106 /* all modules searched */
7107 idx = 0;
7108 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007109 } else {
7110 /* search in top-level */
7111 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7112 }
7113 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007114 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007115 /* we must adjust the module to inherit the one from the context node */
7116 moveto_mod = node->schema->module;
7117 }
7118
7119 /* search in children, do not repeat the same search */
7120 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007121 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007122
7123 /* additional context check */
7124 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7125 scnode = NULL;
7126 }
7127
7128 if (scnode) {
7129 if (*found) {
7130 /* we found a schema node with the same name but at different level, give up, too complicated
7131 * (more hash-based searches would be required, not supported) */
7132 return LY_ENOT;
7133 } else {
7134 /* remember the found schema node and continue to make sure it can be used */
7135 *found = scnode;
7136 }
7137 }
7138
7139 if (idx) {
7140 /* continue searching all the following models */
7141 goto continue_search;
7142 }
7143
7144 return LY_SUCCESS;
7145}
7146
7147/**
Michal Vaskod3678892020-05-21 10:06:58 +02007148 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7149 *
7150 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7151 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7152 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7153 *
7154 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007155 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007156 * @param[in] attr_axis Whether to search attributes or standard nodes.
7157 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007158 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007159 * @param[in] options XPath options.
7160 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7161 */
7162static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007163eval_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 +02007164 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007165{
Michal Vaskod3678892020-05-21 10:06:58 +02007166 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007167 const char *ncname, *ncname_dict = NULL;
7168 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007169 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007170 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007171 struct ly_path_predicate *predicates = NULL;
7172 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007173 LY_ERR rc = LY_SUCCESS;
7174
aPiecek8b0cc152021-05-31 16:40:31 +02007175 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007176 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007177 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007178
aPiecek8b0cc152021-05-31 16:40:31 +02007179 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02007180 goto moveto;
7181 }
7182
Michal Vasko004d3152020-06-11 19:59:22 +02007183 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7184 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007185
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007186 if ((ncname[0] == '*') && (ncname_len == 1)) {
7187 /* all nodes will match */
7188 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007189 }
7190
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007191 /* parse (and skip) module name */
7192 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007193 LY_CHECK_GOTO(rc, cleanup);
7194
Radek Krejci8df109d2021-04-23 12:19:08 +02007195 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 +02007196 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007197 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007198 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7199 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007200 /* check failed */
7201 scnode = NULL;
7202 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007203 }
7204 }
7205
Michal Vasko004d3152020-06-11 19:59:22 +02007206 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7207 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007208 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7209 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007210 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007211 scnode = NULL;
7212 }
7213 }
7214 }
7215
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007216 if (!scnode) {
7217 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007218 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007219 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007220 }
7221
7222moveto:
7223 /* move to the attribute(s), data node(s), or schema node(s) */
7224 if (attr_axis) {
aPiecek8b0cc152021-05-31 16:40:31 +02007225 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007226 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007227 } else {
7228 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007229 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007230 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007231 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007232 }
7233 LY_CHECK_GOTO(rc, cleanup);
7234 }
7235 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007236 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007237 int64_t i;
7238
Michal Vaskod3678892020-05-21 10:06:58 +02007239 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007240 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007241 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007242 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007243 }
7244 LY_CHECK_GOTO(rc, cleanup);
7245
7246 for (i = set->used - 1; i > -1; --i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007247 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vaskod3678892020-05-21 10:06:58 +02007248 break;
7249 }
7250 }
7251 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007252 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vasko90f12dc2020-12-03 14:20:42 +01007253 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".",
7254 ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007255 free(path);
7256 }
7257 } else {
7258 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007259 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007260 } else {
7261 if (scnode) {
7262 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007263 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007264 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007265 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007266 }
7267 }
7268 LY_CHECK_GOTO(rc, cleanup);
7269 }
7270 }
7271
7272 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007273 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7274 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007275 LY_CHECK_RET(rc);
7276 }
7277
7278cleanup:
aPiecek8b0cc152021-05-31 16:40:31 +02007279 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007280 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007281 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007282 }
Michal Vaskod3678892020-05-21 10:06:58 +02007283 return rc;
7284}
7285
7286/**
7287 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7288 *
7289 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7290 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7291 * [8] NodeType ::= 'text' | 'node'
7292 *
7293 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007294 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007295 * @param[in] attr_axis Whether to search attributes or standard nodes.
7296 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007297 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007298 * @param[in] options XPath options.
7299 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7300 */
7301static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007302eval_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 +02007303 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007304{
7305 LY_ERR rc;
7306
7307 /* TODO */
7308 (void)attr_axis;
7309 (void)all_desc;
7310
aPiecek8b0cc152021-05-31 16:40:31 +02007311 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007312 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007313 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007314 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
aPiecek8b0cc152021-05-31 16:40:31 +02007315 options |= LYXP_SKIP_EXPR;
Michal Vaskod3678892020-05-21 10:06:58 +02007316 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007317 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007318 rc = xpath_node(NULL, 0, set, options);
7319 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007320 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007321 rc = xpath_text(NULL, 0, set, options);
7322 }
7323 LY_CHECK_RET(rc);
7324 }
7325 }
aPiecek8b0cc152021-05-31 16:40:31 +02007326 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007327 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007328 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007329
7330 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007331 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007332 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007333 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007334 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007335
7336 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007337 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007338 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007339 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007340 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007341
7342 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007343 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7344 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007345 LY_CHECK_RET(rc);
7346 }
7347
7348 return LY_SUCCESS;
7349}
7350
7351/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007352 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7353 *
7354 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7355 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007356 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007357 *
7358 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007359 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007360 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007361 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007362 * @param[in] options XPath options.
7363 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7364 */
7365static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007366eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7367 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007368{
Radek Krejci857189e2020-09-01 13:26:36 +02007369 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007370 LY_ERR rc;
7371
7372 goto step;
7373 do {
7374 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007375 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007376 all_desc = 0;
7377 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007378 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007379 all_desc = 1;
7380 }
aPiecek8b0cc152021-05-31 16:40:31 +02007381 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007382 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007383 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007384
7385step:
Michal Vaskod3678892020-05-21 10:06:58 +02007386 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007387 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007388 attr_axis = 1;
7389
aPiecek8b0cc152021-05-31 16:40:31 +02007390 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007391 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007392 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007393 } else {
7394 attr_axis = 0;
7395 }
7396
Michal Vasko03ff5a72019-09-11 13:49:33 +02007397 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007398 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007399 case LYXP_TOKEN_DOT:
7400 /* evaluate '.' */
aPiecek8b0cc152021-05-31 16:40:31 +02007401 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007402 rc = moveto_scnode_self(set, all_desc, options);
7403 } else {
7404 rc = moveto_self(set, all_desc, options);
7405 }
7406 LY_CHECK_RET(rc);
7407 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007408 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007409 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007410 break;
7411
7412 case LYXP_TOKEN_DDOT:
7413 /* evaluate '..' */
aPiecek8b0cc152021-05-31 16:40:31 +02007414 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007415 rc = moveto_scnode_parent(set, all_desc, options);
7416 } else {
7417 rc = moveto_parent(set, all_desc, options);
7418 }
7419 LY_CHECK_RET(rc);
aPiecek8b0cc152021-05-31 16:40:31 +02007420 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007421 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007422 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007423 break;
7424
Michal Vasko03ff5a72019-09-11 13:49:33 +02007425 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007426 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007427 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007428 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007429 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007430
Michal Vaskod3678892020-05-21 10:06:58 +02007431 case LYXP_TOKEN_NODETYPE:
7432 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007433 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007434 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007435 break;
7436
7437 default:
aPiecek8b0cc152021-05-31 16:40:31 +02007438 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007439 }
Michal Vasko004d3152020-06-11 19:59:22 +02007440 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007441
7442 return LY_SUCCESS;
7443}
7444
7445/**
7446 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7447 *
7448 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7449 *
7450 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007451 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007452 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007453 * @param[in] options XPath options.
7454 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7455 */
7456static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007457eval_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 +02007458{
Radek Krejci857189e2020-09-01 13:26:36 +02007459 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007460
aPiecek8b0cc152021-05-31 16:40:31 +02007461 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007462 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007463 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007464 }
7465
7466 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007467 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007468 /* evaluate '/' - deferred */
7469 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02007470 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007471 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007472 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007473
Michal Vasko004d3152020-06-11 19:59:22 +02007474 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007475 return LY_SUCCESS;
7476 }
Michal Vasko004d3152020-06-11 19:59:22 +02007477 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007478 case LYXP_TOKEN_DOT:
7479 case LYXP_TOKEN_DDOT:
7480 case LYXP_TOKEN_AT:
7481 case LYXP_TOKEN_NAMETEST:
7482 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007483 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007484 break;
7485 default:
7486 break;
7487 }
7488
Michal Vasko03ff5a72019-09-11 13:49:33 +02007489 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007490 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007491 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7492 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02007493 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007494 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007495 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007496
Michal Vaskob0099a92020-08-31 14:55:23 +02007497 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007498 }
7499
7500 return LY_SUCCESS;
7501}
7502
7503/**
7504 * @brief Evaluate FunctionCall. Logs directly on error.
7505 *
Michal Vaskod3678892020-05-21 10:06:58 +02007506 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007507 *
7508 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007509 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007510 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007511 * @param[in] options XPath options.
7512 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7513 */
7514static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007515eval_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 +02007516{
7517 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007518
Radek Krejci1deb5be2020-08-26 16:43:36 +02007519 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007520 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007521 struct lyxp_set **args = NULL, **args_aux;
7522
aPiecek8b0cc152021-05-31 16:40:31 +02007523 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007524 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007525 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007526 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007527 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007528 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007529 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007530 xpath_func = &xpath_sum;
7531 }
7532 break;
7533 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007534 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007535 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007536 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007537 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007538 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007539 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007540 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007541 xpath_func = &xpath_true;
7542 }
7543 break;
7544 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007545 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007546 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007547 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007548 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007549 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007550 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007551 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007552 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007553 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007554 xpath_func = &xpath_deref;
7555 }
7556 break;
7557 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007558 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007559 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007560 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007561 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007562 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007563 xpath_func = &xpath_string;
7564 }
7565 break;
7566 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007567 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007568 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007569 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007571 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007572 xpath_func = &xpath_current;
7573 }
7574 break;
7575 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007576 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007577 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007578 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007579 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007580 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007581 xpath_func = &xpath_re_match;
7582 }
7583 break;
7584 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007585 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007586 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007587 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007588 xpath_func = &xpath_translate;
7589 }
7590 break;
7591 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007592 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007593 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007594 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007595 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007596 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007597 xpath_func = &xpath_bit_is_set;
7598 }
7599 break;
7600 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007601 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007602 xpath_func = &xpath_starts_with;
7603 }
7604 break;
7605 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007606 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007607 xpath_func = &xpath_derived_from;
7608 }
7609 break;
7610 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007611 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007612 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007613 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007614 xpath_func = &xpath_string_length;
7615 }
7616 break;
7617 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007618 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007619 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007620 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007621 xpath_func = &xpath_substring_after;
7622 }
7623 break;
7624 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007625 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007626 xpath_func = &xpath_substring_before;
7627 }
7628 break;
7629 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007630 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007631 xpath_func = &xpath_derived_from_or_self;
7632 }
7633 break;
7634 }
7635
7636 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007637 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 +02007638 return LY_EVALID;
7639 }
7640 }
7641
aPiecek8b0cc152021-05-31 16:40:31 +02007642 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007643 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007644 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007645
7646 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007647 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007648 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007649 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007650 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007651
7652 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007653 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02007654 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007655 args = malloc(sizeof *args);
7656 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7657 arg_count = 1;
7658 args[0] = set_copy(set);
7659 if (!args[0]) {
7660 rc = LY_EMEM;
7661 goto cleanup;
7662 }
7663
Michal Vasko004d3152020-06-11 19:59:22 +02007664 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007665 LY_CHECK_GOTO(rc, cleanup);
7666 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007667 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007668 LY_CHECK_GOTO(rc, cleanup);
7669 }
7670 }
Michal Vasko004d3152020-06-11 19:59:22 +02007671 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02007672 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007673 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007674 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007675
aPiecek8b0cc152021-05-31 16:40:31 +02007676 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007677 ++arg_count;
7678 args_aux = realloc(args, arg_count * sizeof *args);
7679 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7680 args = args_aux;
7681 args[arg_count - 1] = set_copy(set);
7682 if (!args[arg_count - 1]) {
7683 rc = LY_EMEM;
7684 goto cleanup;
7685 }
7686
Michal Vasko004d3152020-06-11 19:59:22 +02007687 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007688 LY_CHECK_GOTO(rc, cleanup);
7689 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007690 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007691 LY_CHECK_GOTO(rc, cleanup);
7692 }
7693 }
7694
7695 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007696 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007697 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007698 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007699 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007700
aPiecek8b0cc152021-05-31 16:40:31 +02007701 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007702 /* evaluate function */
7703 rc = xpath_func(args, arg_count, set, options);
7704
7705 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007706 /* merge all nodes from arg evaluations */
7707 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007708 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007709 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007710 }
7711 }
7712 } else {
7713 rc = LY_SUCCESS;
7714 }
7715
7716cleanup:
7717 for (i = 0; i < arg_count; ++i) {
7718 lyxp_set_free(args[i]);
7719 }
7720 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007721 return rc;
7722}
7723
7724/**
7725 * @brief Evaluate Number. Logs directly on error.
7726 *
7727 * @param[in] ctx Context for errors.
7728 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007729 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007730 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7731 * @return LY_ERR
7732 */
7733static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007734eval_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 +02007735{
7736 long double num;
7737 char *endptr;
7738
7739 if (set) {
7740 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007741 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007742 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007743 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7744 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007745 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007746 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007747 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007748 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7749 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007750 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007751 return LY_EVALID;
7752 }
7753
7754 set_fill_number(set, num);
7755 }
7756
7757 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007758 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007759 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007760 return LY_SUCCESS;
7761}
7762
7763/**
7764 * @brief Evaluate PathExpr. Logs directly on error.
7765 *
Michal Vaskod3678892020-05-21 10:06:58 +02007766 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007767 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7768 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7769 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007770 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007771 *
7772 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007773 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007774 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007775 * @param[in] options XPath options.
7776 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7777 */
7778static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007779eval_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 +02007780{
Radek Krejci857189e2020-09-01 13:26:36 +02007781 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007782 LY_ERR rc;
7783
Michal Vasko004d3152020-06-11 19:59:22 +02007784 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007785 case LYXP_TOKEN_PAR1:
7786 /* '(' Expr ')' */
7787
7788 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02007789 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007790 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007791 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007792
7793 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007794 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007795 LY_CHECK_RET(rc);
7796
7797 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007798 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007799 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007800 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007801 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007802
7803 parent_pos_pred = 0;
7804 goto predicate;
7805
7806 case LYXP_TOKEN_DOT:
7807 case LYXP_TOKEN_DDOT:
7808 case LYXP_TOKEN_AT:
7809 case LYXP_TOKEN_NAMETEST:
7810 case LYXP_TOKEN_NODETYPE:
7811 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007812 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007813 LY_CHECK_RET(rc);
7814 break;
7815
7816 case LYXP_TOKEN_FUNCNAME:
7817 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02007818 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007819 LY_CHECK_RET(rc);
7820
7821 parent_pos_pred = 1;
7822 goto predicate;
7823
Michal Vasko3e48bf32020-06-01 08:39:07 +02007824 case LYXP_TOKEN_OPER_PATH:
7825 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007826 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007827 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007828 LY_CHECK_RET(rc);
7829 break;
7830
7831 case LYXP_TOKEN_LITERAL:
7832 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02007833 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7834 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007835 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007836 }
Michal Vasko004d3152020-06-11 19:59:22 +02007837 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007838 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007839 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007840 }
7841
7842 parent_pos_pred = 1;
7843 goto predicate;
7844
7845 case LYXP_TOKEN_NUMBER:
7846 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02007847 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7848 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007849 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007850 }
Michal Vasko004d3152020-06-11 19:59:22 +02007851 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007852 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007853 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007854 }
7855 LY_CHECK_RET(rc);
7856
7857 parent_pos_pred = 1;
7858 goto predicate;
7859
7860 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01007861 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 +02007862 return LY_EVALID;
7863 }
7864
7865 return LY_SUCCESS;
7866
7867predicate:
7868 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007869 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7870 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007871 LY_CHECK_RET(rc);
7872 }
7873
7874 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007875 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007876
7877 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007878 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007879 all_desc = 0;
7880 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007881 all_desc = 1;
7882 }
7883
aPiecek8b0cc152021-05-31 16:40:31 +02007884 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007885 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007886 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007887
Michal Vasko004d3152020-06-11 19:59:22 +02007888 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007889 LY_CHECK_RET(rc);
7890 }
7891
7892 return LY_SUCCESS;
7893}
7894
7895/**
7896 * @brief Evaluate UnionExpr. Logs directly on error.
7897 *
Michal Vaskod3678892020-05-21 10:06:58 +02007898 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007899 *
7900 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007901 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007902 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007903 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007904 * @param[in] options XPath options.
7905 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7906 */
7907static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007908eval_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 +02007909{
7910 LY_ERR rc = LY_SUCCESS;
7911 struct lyxp_set orig_set, set2;
7912 uint16_t i;
7913
7914 assert(repeat);
7915
7916 set_init(&orig_set, set);
7917 set_init(&set2, set);
7918
7919 set_fill_set(&orig_set, set);
7920
Michal Vasko004d3152020-06-11 19:59:22 +02007921 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007922 LY_CHECK_GOTO(rc, cleanup);
7923
7924 /* ('|' PathExpr)* */
7925 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007926 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02007927 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007928 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007929 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007930
aPiecek8b0cc152021-05-31 16:40:31 +02007931 if (options & LYXP_SKIP_EXPR) {
7932 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007933 LY_CHECK_GOTO(rc, cleanup);
7934 continue;
7935 }
7936
7937 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007938 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007939 LY_CHECK_GOTO(rc, cleanup);
7940
7941 /* eval */
7942 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007943 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007944 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007945 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007946 LY_CHECK_GOTO(rc, cleanup);
7947 }
7948 }
7949
7950cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007951 lyxp_set_free_content(&orig_set);
7952 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007953 return rc;
7954}
7955
7956/**
7957 * @brief Evaluate UnaryExpr. Logs directly on error.
7958 *
Michal Vaskod3678892020-05-21 10:06:58 +02007959 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007960 *
7961 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007962 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007963 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007964 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007965 * @param[in] options XPath options.
7966 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7967 */
7968static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007969eval_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 +02007970{
7971 LY_ERR rc;
7972 uint16_t this_op, i;
7973
7974 assert(repeat);
7975
7976 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007977 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007978 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007979 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 +02007980
aPiecek8b0cc152021-05-31 16:40:31 +02007981 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007982 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007983 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007984 }
7985
Michal Vasko004d3152020-06-11 19:59:22 +02007986 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007987 LY_CHECK_RET(rc);
7988
aPiecek8b0cc152021-05-31 16:40:31 +02007989 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007990 if (options & LYXP_SCNODE_ALL) {
7991 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7992 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007993 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007994 LY_CHECK_RET(rc);
7995 }
7996 }
7997
7998 return LY_SUCCESS;
7999}
8000
8001/**
8002 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
8003 *
Michal Vaskod3678892020-05-21 10:06:58 +02008004 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008005 * | MultiplicativeExpr '*' UnaryExpr
8006 * | MultiplicativeExpr 'div' UnaryExpr
8007 * | MultiplicativeExpr 'mod' UnaryExpr
8008 *
8009 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008010 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008011 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008012 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008013 * @param[in] options XPath options.
8014 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8015 */
8016static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008017eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
8018 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008019{
8020 LY_ERR rc;
8021 uint16_t this_op;
8022 struct lyxp_set orig_set, set2;
8023 uint16_t i;
8024
8025 assert(repeat);
8026
8027 set_init(&orig_set, set);
8028 set_init(&set2, set);
8029
8030 set_fill_set(&orig_set, set);
8031
Michal Vasko004d3152020-06-11 19:59:22 +02008032 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008033 LY_CHECK_GOTO(rc, cleanup);
8034
8035 /* ('*' / 'div' / 'mod' UnaryExpr)* */
8036 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008037 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008038
Michal Vasko004d3152020-06-11 19:59:22 +02008039 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02008040 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008041 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008042 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008043
aPiecek8b0cc152021-05-31 16:40:31 +02008044 if (options & LYXP_SKIP_EXPR) {
8045 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008046 LY_CHECK_GOTO(rc, cleanup);
8047 continue;
8048 }
8049
8050 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008051 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008052 LY_CHECK_GOTO(rc, cleanup);
8053
8054 /* eval */
8055 if (options & LYXP_SCNODE_ALL) {
8056 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008057 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008058 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008059 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008060 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008061 LY_CHECK_GOTO(rc, cleanup);
8062 }
8063 }
8064
8065cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008066 lyxp_set_free_content(&orig_set);
8067 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008068 return rc;
8069}
8070
8071/**
8072 * @brief Evaluate AdditiveExpr. Logs directly on error.
8073 *
Michal Vaskod3678892020-05-21 10:06:58 +02008074 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008075 * | AdditiveExpr '+' MultiplicativeExpr
8076 * | AdditiveExpr '-' MultiplicativeExpr
8077 *
8078 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008079 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008080 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008081 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008082 * @param[in] options XPath options.
8083 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8084 */
8085static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008086eval_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 +02008087{
8088 LY_ERR rc;
8089 uint16_t this_op;
8090 struct lyxp_set orig_set, set2;
8091 uint16_t i;
8092
8093 assert(repeat);
8094
8095 set_init(&orig_set, set);
8096 set_init(&set2, set);
8097
8098 set_fill_set(&orig_set, set);
8099
Michal Vasko004d3152020-06-11 19:59:22 +02008100 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008101 LY_CHECK_GOTO(rc, cleanup);
8102
8103 /* ('+' / '-' MultiplicativeExpr)* */
8104 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008105 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008106
Michal Vasko004d3152020-06-11 19:59:22 +02008107 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008108 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008109 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008110 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008111
aPiecek8b0cc152021-05-31 16:40:31 +02008112 if (options & LYXP_SKIP_EXPR) {
8113 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008114 LY_CHECK_GOTO(rc, cleanup);
8115 continue;
8116 }
8117
8118 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008119 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008120 LY_CHECK_GOTO(rc, cleanup);
8121
8122 /* eval */
8123 if (options & LYXP_SCNODE_ALL) {
8124 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008125 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008126 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008127 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008128 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008129 LY_CHECK_GOTO(rc, cleanup);
8130 }
8131 }
8132
8133cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008134 lyxp_set_free_content(&orig_set);
8135 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008136 return rc;
8137}
8138
8139/**
8140 * @brief Evaluate RelationalExpr. Logs directly on error.
8141 *
Michal Vaskod3678892020-05-21 10:06:58 +02008142 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008143 * | RelationalExpr '<' AdditiveExpr
8144 * | RelationalExpr '>' AdditiveExpr
8145 * | RelationalExpr '<=' AdditiveExpr
8146 * | RelationalExpr '>=' AdditiveExpr
8147 *
8148 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008149 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008150 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008151 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008152 * @param[in] options XPath options.
8153 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8154 */
8155static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008156eval_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 +02008157{
8158 LY_ERR rc;
8159 uint16_t this_op;
8160 struct lyxp_set orig_set, set2;
8161 uint16_t i;
8162
8163 assert(repeat);
8164
8165 set_init(&orig_set, set);
8166 set_init(&set2, set);
8167
8168 set_fill_set(&orig_set, set);
8169
Michal Vasko004d3152020-06-11 19:59:22 +02008170 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008171 LY_CHECK_GOTO(rc, cleanup);
8172
8173 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8174 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008175 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008176
Michal Vasko004d3152020-06-11 19:59:22 +02008177 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02008178 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008179 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008180 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008181
aPiecek8b0cc152021-05-31 16:40:31 +02008182 if (options & LYXP_SKIP_EXPR) {
8183 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008184 LY_CHECK_GOTO(rc, cleanup);
8185 continue;
8186 }
8187
8188 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008189 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008190 LY_CHECK_GOTO(rc, cleanup);
8191
8192 /* eval */
8193 if (options & LYXP_SCNODE_ALL) {
8194 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008195 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008196 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008197 } else {
8198 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8199 LY_CHECK_GOTO(rc, cleanup);
8200 }
8201 }
8202
8203cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008204 lyxp_set_free_content(&orig_set);
8205 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008206 return rc;
8207}
8208
8209/**
8210 * @brief Evaluate EqualityExpr. Logs directly on error.
8211 *
Michal Vaskod3678892020-05-21 10:06:58 +02008212 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008213 * | EqualityExpr '!=' RelationalExpr
8214 *
8215 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008216 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008217 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008218 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008219 * @param[in] options XPath options.
8220 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8221 */
8222static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008223eval_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 +02008224{
8225 LY_ERR rc;
8226 uint16_t this_op;
8227 struct lyxp_set orig_set, set2;
8228 uint16_t i;
8229
8230 assert(repeat);
8231
8232 set_init(&orig_set, set);
8233 set_init(&set2, set);
8234
8235 set_fill_set(&orig_set, set);
8236
Michal Vasko004d3152020-06-11 19:59:22 +02008237 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008238 LY_CHECK_GOTO(rc, cleanup);
8239
8240 /* ('=' / '!=' RelationalExpr)* */
8241 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008242 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008243
Michal Vasko004d3152020-06-11 19:59:22 +02008244 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02008245 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008246 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008247 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008248
aPiecek8b0cc152021-05-31 16:40:31 +02008249 if (options & LYXP_SKIP_EXPR) {
8250 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008251 LY_CHECK_GOTO(rc, cleanup);
8252 continue;
8253 }
8254
8255 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008256 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008257 LY_CHECK_GOTO(rc, cleanup);
8258
8259 /* eval */
8260 if (options & LYXP_SCNODE_ALL) {
8261 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008262 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8263 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008264 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008265 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008266 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008267 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8268 LY_CHECK_GOTO(rc, cleanup);
8269 }
8270 }
8271
8272cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008273 lyxp_set_free_content(&orig_set);
8274 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008275 return rc;
8276}
8277
8278/**
8279 * @brief Evaluate AndExpr. Logs directly on error.
8280 *
Michal Vaskod3678892020-05-21 10:06:58 +02008281 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008282 *
8283 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008284 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008285 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008286 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008287 * @param[in] options XPath options.
8288 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8289 */
8290static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008291eval_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 +02008292{
8293 LY_ERR rc;
8294 struct lyxp_set orig_set, set2;
8295 uint16_t i;
8296
8297 assert(repeat);
8298
8299 set_init(&orig_set, set);
8300 set_init(&set2, set);
8301
8302 set_fill_set(&orig_set, set);
8303
Michal Vasko004d3152020-06-11 19:59:22 +02008304 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008305 LY_CHECK_GOTO(rc, cleanup);
8306
8307 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008308 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008309 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008310 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008311 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008312 }
8313
8314 /* ('and' EqualityExpr)* */
8315 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008316 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008317 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008318 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008319 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008320
8321 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008322 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8323 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008324 LY_CHECK_GOTO(rc, cleanup);
8325 continue;
8326 }
8327
8328 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008329 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008330 LY_CHECK_GOTO(rc, cleanup);
8331
8332 /* eval - just get boolean value actually */
8333 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008334 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008335 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008336 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008337 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008338 set_fill_set(set, &set2);
8339 }
8340 }
8341
8342cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008343 lyxp_set_free_content(&orig_set);
8344 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008345 return rc;
8346}
8347
8348/**
8349 * @brief Evaluate OrExpr. Logs directly on error.
8350 *
Michal Vaskod3678892020-05-21 10:06:58 +02008351 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008352 *
8353 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008354 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008355 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008356 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008357 * @param[in] options XPath options.
8358 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8359 */
8360static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008361eval_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 +02008362{
8363 LY_ERR rc;
8364 struct lyxp_set orig_set, set2;
8365 uint16_t i;
8366
8367 assert(repeat);
8368
8369 set_init(&orig_set, set);
8370 set_init(&set2, set);
8371
8372 set_fill_set(&orig_set, set);
8373
Michal Vasko004d3152020-06-11 19:59:22 +02008374 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008375 LY_CHECK_GOTO(rc, cleanup);
8376
8377 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008378 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008379 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008380 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008381 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008382 }
8383
8384 /* ('or' AndExpr)* */
8385 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008386 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008387 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008388 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008389 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008390
8391 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008392 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8393 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008394 LY_CHECK_GOTO(rc, cleanup);
8395 continue;
8396 }
8397
8398 set_fill_set(&set2, &orig_set);
8399 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8400 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008401 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008402 LY_CHECK_GOTO(rc, cleanup);
8403
8404 /* eval - just get boolean value actually */
8405 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008406 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008407 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008408 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008409 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008410 set_fill_set(set, &set2);
8411 }
8412 }
8413
8414cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008415 lyxp_set_free_content(&orig_set);
8416 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008417 return rc;
8418}
8419
8420/**
Michal Vasko004d3152020-06-11 19:59:22 +02008421 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008422 *
8423 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008424 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008425 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02008426 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008427 * @param[in] options XPath options.
8428 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8429 */
8430static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008431eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8432 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008433{
8434 uint16_t i, count;
8435 enum lyxp_expr_type next_etype;
8436 LY_ERR rc;
8437
8438 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008439 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008440 next_etype = LYXP_EXPR_NONE;
8441 } else {
8442 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008443 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008444
8445 /* select one-priority lower because etype expression called us */
8446 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008447 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008448 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008449 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008450 } else {
8451 next_etype = LYXP_EXPR_NONE;
8452 }
8453 }
8454
8455 /* decide what expression are we parsing based on the repeat */
8456 switch (next_etype) {
8457 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008458 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008459 break;
8460 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008461 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008462 break;
8463 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008464 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008465 break;
8466 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008467 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008468 break;
8469 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008470 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008471 break;
8472 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008473 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008474 break;
8475 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008476 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008477 break;
8478 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008479 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008480 break;
8481 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008482 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008483 break;
8484 default:
8485 LOGINT_RET(set->ctx);
8486 }
8487
8488 return rc;
8489}
8490
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008491/**
8492 * @brief Get root type.
8493 *
8494 * @param[in] ctx_node Context node.
8495 * @param[in] ctx_scnode Schema context node.
8496 * @param[in] options XPath options.
8497 * @return Root type.
8498 */
8499static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008500lyxp_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 +01008501{
Michal Vasko6b26e742020-07-17 15:02:10 +02008502 const struct lysc_node *op;
8503
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008504 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008505 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008506 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008507
8508 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008509 /* general root that can access everything */
8510 return LYXP_NODE_ROOT;
8511 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8512 /* root context node can access only config data (because we said so, it is unspecified) */
8513 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008514 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008515 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008516 }
8517
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008518 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008519 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008520 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008521
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008522 if (op || !(options & LYXP_SCHEMA)) {
8523 /* general root that can access everything */
8524 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05008525 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008526 /* root context node can access only config data (because we said so, it is unspecified) */
8527 return LYXP_NODE_ROOT_CONFIG;
8528 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008529 return LYXP_NODE_ROOT;
8530}
8531
Michal Vasko03ff5a72019-09-11 13:49:33 +02008532LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008533lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008534 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 +01008535 struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008536{
Michal Vasko004d3152020-06-11 19:59:22 +02008537 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008538 LY_ERR rc;
8539
Michal Vasko400e9672021-01-11 13:39:17 +01008540 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008541 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008542 LOGARG(NULL, "Current module must be set if schema format is used.");
8543 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008544 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008545
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008546 if (tree) {
8547 /* adjust the pointer to be the first top-level sibling */
8548 while (tree->parent) {
8549 tree = lyd_parent(tree);
8550 }
8551 tree = lyd_first_sibling(tree);
8552 }
8553
Michal Vasko03ff5a72019-09-11 13:49:33 +02008554 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008555 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008556 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008557 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8558 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8559
Michal Vasko400e9672021-01-11 13:39:17 +01008560 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008561 set->cur_node = ctx_node;
8562 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008563 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8564 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008565 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008566 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008567 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008568 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008569
Radek Krejciddace2c2021-01-08 11:30:56 +01008570 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008571
Michal Vasko03ff5a72019-09-11 13:49:33 +02008572 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008573 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008574 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008575 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008576 }
8577
Radek Krejciddace2c2021-01-08 11:30:56 +01008578 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008579 return rc;
8580}
8581
8582#if 0
8583
8584/* full xml printing of set elements, not used currently */
8585
8586void
8587lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8588{
8589 uint32_t i;
8590 char *str_num;
8591 struct lyout out;
8592
8593 memset(&out, 0, sizeof out);
8594
8595 out.type = LYOUT_STREAM;
8596 out.method.f = f;
8597
8598 switch (set->type) {
8599 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008600 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008601 break;
8602 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008603 ly_print_(&out, "Boolean XPath set:\n");
8604 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008605 break;
8606 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008607 ly_print_(&out, "String XPath set:\n");
8608 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008609 break;
8610 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008611 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008612
8613 if (isnan(set->value.num)) {
8614 str_num = strdup("NaN");
8615 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8616 str_num = strdup("0");
8617 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8618 str_num = strdup("Infinity");
8619 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8620 str_num = strdup("-Infinity");
8621 } else if ((long long)set->value.num == set->value.num) {
8622 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8623 str_num = NULL;
8624 }
8625 } else {
8626 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8627 str_num = NULL;
8628 }
8629 }
8630 if (!str_num) {
8631 LOGMEM;
8632 return;
8633 }
Michal Vasko5233e962020-08-14 14:26:20 +02008634 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008635 free(str_num);
8636 break;
8637 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008638 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008639
8640 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008641 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008642 switch (set->node_type[i]) {
8643 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008644 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008645 break;
8646 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008647 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008648 break;
8649 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008650 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008651 break;
8652 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008653 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008654 break;
8655 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008656 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008657 break;
8658 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008659 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008660 break;
8661 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008662 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008663 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008664 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008665 break;
8666 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008667 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 +02008668 break;
8669 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008670 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 +02008671 break;
8672 }
8673 }
8674 break;
8675 }
8676}
8677
8678#endif
8679
8680LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008681lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008682{
8683 long double num;
8684 char *str;
8685 LY_ERR rc;
8686
8687 if (!set || (set->type == target)) {
8688 return LY_SUCCESS;
8689 }
8690
8691 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008692 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008693
8694 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008695 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008696 return LY_EINVAL;
8697 }
8698
8699 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008700 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008701 switch (set->type) {
8702 case LYXP_SET_NUMBER:
8703 if (isnan(set->val.num)) {
8704 set->val.str = strdup("NaN");
8705 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8706 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8707 set->val.str = strdup("0");
8708 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8709 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8710 set->val.str = strdup("Infinity");
8711 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8712 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8713 set->val.str = strdup("-Infinity");
8714 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8715 } else if ((long long)set->val.num == set->val.num) {
8716 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8717 LOGMEM_RET(set->ctx);
8718 }
8719 set->val.str = str;
8720 } else {
8721 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8722 LOGMEM_RET(set->ctx);
8723 }
8724 set->val.str = str;
8725 }
8726 break;
8727 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008728 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008729 set->val.str = strdup("true");
8730 } else {
8731 set->val.str = strdup("false");
8732 }
8733 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8734 break;
8735 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008736 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008737 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008738
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008739 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008740 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008741 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008742 set->val.str = str;
8743 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008744 default:
8745 LOGINT_RET(set->ctx);
8746 }
8747 set->type = LYXP_SET_STRING;
8748 }
8749
8750 /* to NUMBER */
8751 if (target == LYXP_SET_NUMBER) {
8752 switch (set->type) {
8753 case LYXP_SET_STRING:
8754 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008755 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008756 set->val.num = num;
8757 break;
8758 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008759 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008760 set->val.num = 1;
8761 } else {
8762 set->val.num = 0;
8763 }
8764 break;
8765 default:
8766 LOGINT_RET(set->ctx);
8767 }
8768 set->type = LYXP_SET_NUMBER;
8769 }
8770
8771 /* to BOOLEAN */
8772 if (target == LYXP_SET_BOOLEAN) {
8773 switch (set->type) {
8774 case LYXP_SET_NUMBER:
8775 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008776 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008777 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008778 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008779 }
8780 break;
8781 case LYXP_SET_STRING:
8782 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008783 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008784 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008785 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008786 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008787 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008788 }
8789 break;
8790 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008791 if (set->used) {
8792 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008793 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008794 } else {
8795 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008796 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008797 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008798 break;
8799 default:
8800 LOGINT_RET(set->ctx);
8801 }
8802 set->type = LYXP_SET_BOOLEAN;
8803 }
8804
Michal Vasko03ff5a72019-09-11 13:49:33 +02008805 return LY_SUCCESS;
8806}
8807
8808LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008809lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008810 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
Michal Vasko400e9672021-01-11 13:39:17 +01008811 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008812{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008813 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008814 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008815
Michal Vasko400e9672021-01-11 13:39:17 +01008816 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008817 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008818 LOGARG(NULL, "Current module must be set if schema format is used.");
8819 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008820 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008821
8822 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008823 memset(set, 0, sizeof *set);
8824 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008825 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8826 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 +01008827 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008828
Michal Vasko400e9672021-01-11 13:39:17 +01008829 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008830 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008831 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008832 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8833 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008834 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008835 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008836 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008837
Radek Krejciddace2c2021-01-08 11:30:56 +01008838 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008839
Michal Vasko03ff5a72019-09-11 13:49:33 +02008840 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01008841 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
8842
Radek Krejciddace2c2021-01-08 11:30:56 +01008843 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008844 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008845}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008846
8847API const char *
8848lyxp_get_expr(const struct lyxp_expr *path)
8849{
8850 if (!path) {
8851 return NULL;
8852 }
8853
8854 return path->expr;
8855}