blob: 9dc8d2475a751fcbbcd74500e75675a5de5f8949 [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 */
Michal Vasko03ff5a72019-09-11 13:49:33 +020014#define _GNU_SOURCE
Radek Krejcif8dc59a2020-11-25 13:47:44 +010015#define _POSIX_C_SOURCE 200809L /* strdup, strndup */
Michal Vasko03ff5a72019-09-11 13:49:33 +020016
17/* needed by libmath functions isfinite(), isinf(), isnan(), signbit(), ... */
18#define _ISOC99_SOURCE
Radek Krejcib1646a92018-11-02 16:08:26 +010019
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010021
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010023#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010027#include <stdio.h>
28#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010029#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010030
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020032#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020033#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020035#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010036#include "out.h"
Radek Krejci7931b192020-06-25 17:05:03 +020037#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020038#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020039#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020040#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020041#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020042#include "tree.h"
43#include "tree_data_internal.h"
44#include "tree_schema_internal.h"
45#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020046
Michal Vasko004d3152020-06-11 19:59:22 +020047static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx);
Michal Vasko40308e72020-10-20 16:38:40 +020048static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype,
49 struct lyxp_set *set, uint32_t options);
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 Vasko03ff5a72019-09-11 13:49:33 +0200148 if (exp->repeat[i]) {
149 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,
Michal Vasko69730152020-10-09 16:30:07 +0200198 item->node->schema->name, LYD_CANON_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,
Michal Vasko69730152020-10-09 16:30:07 +0200201 item->node->schema->name, LYD_CANON_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 {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200211 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, LYD_CANON_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:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200366 value_str = LYD_CANON_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 Vasko03ff5a72019-09-11 13:49:33 +0200505 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100506 case LYXP_NODE_NONE:
507 /* invalid */
508 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200509 case LYXP_NODE_ROOT:
510 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100511 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200512 case LYXP_NODE_ELEM:
513 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100514 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100515 case LYXP_NODE_META:
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200516 *str = strdup(set->val.meta[0].meta->value.canonical);
517 if (!*str) {
518 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200519 }
520 return LY_SUCCESS;
521 }
522
523 LOGINT_RET(set->ctx);
524}
525
526/**
527 * @brief Cast a string into an XPath number.
528 *
529 * @param[in] str String to use.
530 * @return Cast number.
531 */
532static long double
533cast_string_to_number(const char *str)
534{
535 long double num;
536 char *ptr;
537
538 errno = 0;
539 num = strtold(str, &ptr);
540 if (errno || *ptr) {
541 num = NAN;
542 }
543 return num;
544}
545
546/**
547 * @brief Callback for checking value equality.
548 *
Radek Krejci857189e2020-09-01 13:26:36 +0200549 * Implementation of ::values_equal_cb.
550 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200551 * @param[in] val1_p First value.
552 * @param[in] val2_p Second value.
553 * @param[in] mod Whether hash table is being modified.
554 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200555 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200556 */
Radek Krejci857189e2020-09-01 13:26:36 +0200557static ly_bool
558set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200559{
560 struct lyxp_set_hash_node *val1, *val2;
561
562 val1 = (struct lyxp_set_hash_node *)val1_p;
563 val2 = (struct lyxp_set_hash_node *)val2_p;
564
565 if ((val1->node == val2->node) && (val1->type == val2->type)) {
566 return 1;
567 }
568
569 return 0;
570}
571
572/**
573 * @brief Insert node and its hash into set.
574 *
575 * @param[in] set et to insert to.
576 * @param[in] node Node with hash.
577 * @param[in] type Node type.
578 */
579static void
580set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
581{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200582 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200583 uint32_t i, hash;
584 struct lyxp_set_hash_node hnode;
585
586 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
587 /* create hash table and add all the nodes */
588 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
589 for (i = 0; i < set->used; ++i) {
590 hnode.node = set->val.nodes[i].node;
591 hnode.type = set->val.nodes[i].type;
592
593 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
594 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
595 hash = dict_hash_multi(hash, NULL, 0);
596
597 r = lyht_insert(set->ht, &hnode, hash, NULL);
598 assert(!r);
599 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200600
Michal Vasko9d6befd2019-12-11 14:56:56 +0100601 if (hnode.node == node) {
602 /* it was just added, do not add it twice */
603 node = NULL;
604 }
605 }
606 }
607
608 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200609 /* add the new node into hash table */
610 hnode.node = node;
611 hnode.type = type;
612
613 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
614 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
615 hash = dict_hash_multi(hash, NULL, 0);
616
617 r = lyht_insert(set->ht, &hnode, hash, NULL);
618 assert(!r);
619 (void)r;
620 }
621}
622
623/**
624 * @brief Remove node and its hash from set.
625 *
626 * @param[in] set Set to remove from.
627 * @param[in] node Node to remove.
628 * @param[in] type Node type.
629 */
630static void
631set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
632{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200633 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200634 struct lyxp_set_hash_node hnode;
635 uint32_t hash;
636
637 if (set->ht) {
638 hnode.node = node;
639 hnode.type = type;
640
641 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
642 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
643 hash = dict_hash_multi(hash, NULL, 0);
644
645 r = lyht_remove(set->ht, &hnode, hash);
646 assert(!r);
647 (void)r;
648
649 if (!set->ht->used) {
650 lyht_free(set->ht);
651 set->ht = NULL;
652 }
653 }
654}
655
656/**
657 * @brief Check whether node is in set based on its hash.
658 *
659 * @param[in] set Set to search in.
660 * @param[in] node Node to search for.
661 * @param[in] type Node type.
662 * @param[in] skip_idx Index in @p set to skip.
663 * @return LY_ERR
664 */
665static LY_ERR
666set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
667{
668 struct lyxp_set_hash_node hnode, *match_p;
669 uint32_t hash;
670
671 hnode.node = node;
672 hnode.type = type;
673
674 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
675 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
676 hash = dict_hash_multi(hash, NULL, 0);
677
678 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
679 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
680 /* we found it on the index that should be skipped, find another */
681 hnode = *match_p;
682 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
683 /* none other found */
684 return LY_SUCCESS;
685 }
686 }
687
688 return LY_EEXIST;
689 }
690
691 /* not found */
692 return LY_SUCCESS;
693}
694
Michal Vaskod3678892020-05-21 10:06:58 +0200695void
696lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200697{
698 if (!set) {
699 return;
700 }
701
702 if (set->type == LYXP_SET_NODE_SET) {
703 free(set->val.nodes);
704 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200705 } else if (set->type == LYXP_SET_SCNODE_SET) {
706 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200707 lyht_free(set->ht);
708 } else {
709 if (set->type == LYXP_SET_STRING) {
710 free(set->val.str);
711 }
712 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200713 }
Michal Vaskod3678892020-05-21 10:06:58 +0200714
715 set->val.nodes = NULL;
716 set->used = 0;
717 set->size = 0;
718 set->ht = NULL;
719 set->ctx_pos = 0;
720 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200721}
722
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100723/**
724 * @brief Free dynamically-allocated set.
725 *
726 * @param[in] set Set to free.
727 */
728static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200729lyxp_set_free(struct lyxp_set *set)
730{
731 if (!set) {
732 return;
733 }
734
Michal Vaskod3678892020-05-21 10:06:58 +0200735 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200736 free(set);
737}
738
739/**
740 * @brief Initialize set context.
741 *
742 * @param[in] new Set to initialize.
743 * @param[in] set Arbitrary initialized set.
744 */
745static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200746set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200747{
748 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200749 if (set) {
750 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200751 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100752 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200753 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100754 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200755 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200756 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200757 new->prefix_data = set->prefix_data;
Michal Vasko02a77382019-09-12 11:47:35 +0200758 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200759}
760
761/**
762 * @brief Create a deep copy of a set.
763 *
764 * @param[in] set Set to copy.
765 * @return Copy of @p set.
766 */
767static struct lyxp_set *
768set_copy(struct lyxp_set *set)
769{
770 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100771 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200772
773 if (!set) {
774 return NULL;
775 }
776
777 ret = malloc(sizeof *ret);
778 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
779 set_init(ret, set);
780
781 if (set->type == LYXP_SET_SCNODE_SET) {
782 ret->type = set->type;
783
784 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100785 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
786 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200787 uint32_t idx;
788 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
789 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100790 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200791 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200792 lyxp_set_free(ret);
793 return NULL;
794 }
Michal Vaskoba716542019-12-16 10:01:58 +0100795 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200796 }
797 }
798 } else if (set->type == LYXP_SET_NODE_SET) {
799 ret->type = set->type;
800 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
801 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
802 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
803
804 ret->used = ret->size = set->used;
805 ret->ctx_pos = set->ctx_pos;
806 ret->ctx_size = set->ctx_size;
807 ret->ht = lyht_dup(set->ht);
808 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200809 memcpy(ret, set, sizeof *ret);
810 if (set->type == LYXP_SET_STRING) {
811 ret->val.str = strdup(set->val.str);
812 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
813 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200814 }
815
816 return ret;
817}
818
819/**
820 * @brief Fill XPath set with a string. Any current data are disposed of.
821 *
822 * @param[in] set Set to fill.
823 * @param[in] string String to fill into \p set.
824 * @param[in] str_len Length of \p string. 0 is a valid value!
825 */
826static void
827set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
828{
Michal Vaskod3678892020-05-21 10:06:58 +0200829 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200830
831 set->type = LYXP_SET_STRING;
832 if ((str_len == 0) && (string[0] != '\0')) {
833 string = "";
834 }
835 set->val.str = strndup(string, str_len);
836}
837
838/**
839 * @brief Fill XPath set with a number. Any current data are disposed of.
840 *
841 * @param[in] set Set to fill.
842 * @param[in] number Number to fill into \p set.
843 */
844static void
845set_fill_number(struct lyxp_set *set, long double number)
846{
Michal Vaskod3678892020-05-21 10:06:58 +0200847 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200848
849 set->type = LYXP_SET_NUMBER;
850 set->val.num = number;
851}
852
853/**
854 * @brief Fill XPath set with a boolean. Any current data are disposed of.
855 *
856 * @param[in] set Set to fill.
857 * @param[in] boolean Boolean to fill into \p set.
858 */
859static void
Radek Krejci857189e2020-09-01 13:26:36 +0200860set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200861{
Michal Vaskod3678892020-05-21 10:06:58 +0200862 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200863
864 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200865 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200866}
867
868/**
869 * @brief Fill XPath set with the value from another set (deep assign).
870 * Any current data are disposed of.
871 *
872 * @param[in] trg Set to fill.
873 * @param[in] src Source set to copy into \p trg.
874 */
875static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200876set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200877{
878 if (!trg || !src) {
879 return;
880 }
881
882 if (trg->type == LYXP_SET_NODE_SET) {
883 free(trg->val.nodes);
884 } else if (trg->type == LYXP_SET_STRING) {
885 free(trg->val.str);
886 }
887 set_init(trg, src);
888
889 if (src->type == LYXP_SET_SCNODE_SET) {
890 trg->type = LYXP_SET_SCNODE_SET;
891 trg->used = src->used;
892 trg->size = src->used;
893
894 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
895 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
896 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
897 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200898 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200899 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200900 set_fill_number(trg, src->val.num);
901 } else if (src->type == LYXP_SET_STRING) {
902 set_fill_string(trg, src->val.str, strlen(src->val.str));
903 } else {
904 if (trg->type == LYXP_SET_NODE_SET) {
905 free(trg->val.nodes);
906 } else if (trg->type == LYXP_SET_STRING) {
907 free(trg->val.str);
908 }
909
Michal Vaskod3678892020-05-21 10:06:58 +0200910 assert(src->type == LYXP_SET_NODE_SET);
911
912 trg->type = LYXP_SET_NODE_SET;
913 trg->used = src->used;
914 trg->size = src->used;
915 trg->ctx_pos = src->ctx_pos;
916 trg->ctx_size = src->ctx_size;
917
918 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
919 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
920 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
921 if (src->ht) {
922 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200923 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200924 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200925 }
926 }
927}
928
929/**
930 * @brief Clear context of all schema nodes.
931 *
932 * @param[in] set Set to clear.
933 */
934static void
935set_scnode_clear_ctx(struct lyxp_set *set)
936{
937 uint32_t i;
938
939 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100940 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
941 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
942 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
943 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200944 }
945 }
946}
947
948/**
949 * @brief Remove a node from a set. Removing last node changes
950 * set into LYXP_SET_EMPTY. Context position aware.
951 *
952 * @param[in] set Set to use.
953 * @param[in] idx Index from @p set of the node to be removed.
954 */
955static void
956set_remove_node(struct lyxp_set *set, uint32_t idx)
957{
958 assert(set && (set->type == LYXP_SET_NODE_SET));
959 assert(idx < set->used);
960
961 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
962
963 --set->used;
964 if (set->used) {
965 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
966 (set->used - idx) * sizeof *set->val.nodes);
967 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200968 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200969 }
970}
971
972/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100973 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200974 *
975 * @param[in] set Set to use.
976 * @param[in] idx Index from @p set of the node to be removed.
977 */
978static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100979set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200980{
981 assert(set && (set->type == LYXP_SET_NODE_SET));
982 assert(idx < set->used);
983
Michal Vasko2caefc12019-11-14 16:07:56 +0100984 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
985 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
986 }
987 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +0200988}
989
990/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100991 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +0200992 * set into LYXP_SET_EMPTY. Context position aware.
993 *
994 * @param[in] set Set to consolidate.
995 */
996static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100997set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +0200998{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100999 uint32_t i, orig_used, end = 0;
1000 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001001
Michal Vaskod3678892020-05-21 10:06:58 +02001002 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001003
1004 orig_used = set->used;
1005 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001006 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001007 start = -1;
1008 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001009 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001010 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001011 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001012 end = i;
1013 ++i;
1014 break;
1015 }
1016
1017 ++i;
1018 if (i == orig_used) {
1019 end = i;
1020 }
1021 } while (i < orig_used);
1022
1023 if (start > -1) {
1024 /* move the whole chunk of valid nodes together */
1025 if (set->used != (unsigned)start) {
1026 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1027 }
1028 set->used += end - start;
1029 }
1030 }
Michal Vasko57eab132019-09-24 11:46:26 +02001031}
1032
1033/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001034 * @brief Check for duplicates in a node set.
1035 *
1036 * @param[in] set Set to check.
1037 * @param[in] node Node to look for in @p set.
1038 * @param[in] node_type Type of @p node.
1039 * @param[in] skip_idx Index from @p set to skip.
1040 * @return LY_ERR
1041 */
1042static LY_ERR
1043set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1044{
1045 uint32_t i;
1046
Michal Vasko2caefc12019-11-14 16:07:56 +01001047 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001048 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1049 }
1050
1051 for (i = 0; i < set->used; ++i) {
1052 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1053 continue;
1054 }
1055
1056 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1057 return LY_EEXIST;
1058 }
1059 }
1060
1061 return LY_SUCCESS;
1062}
1063
Radek Krejci857189e2020-09-01 13:26:36 +02001064ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001065lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1066 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001067{
1068 uint32_t i;
1069
1070 for (i = 0; i < set->used; ++i) {
1071 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1072 continue;
1073 }
1074
1075 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001076 if (index_p) {
1077 *index_p = i;
1078 }
1079 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001080 }
1081 }
1082
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001083 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001084}
1085
Michal Vaskoecd62de2019-11-13 12:35:11 +01001086void
1087lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001088{
1089 uint32_t orig_used, i, j;
1090
Michal Vaskod3678892020-05-21 10:06:58 +02001091 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001092
Michal Vaskod3678892020-05-21 10:06:58 +02001093 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001094 return;
1095 }
1096
Michal Vaskod3678892020-05-21 10:06:58 +02001097 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001098 memcpy(set1, set2, sizeof *set1);
1099 return;
1100 }
1101
1102 if (set1->used + set2->used > set1->size) {
1103 set1->size = set1->used + set2->used;
1104 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1105 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1106 }
1107
1108 orig_used = set1->used;
1109
1110 for (i = 0; i < set2->used; ++i) {
1111 for (j = 0; j < orig_used; ++j) {
1112 /* detect duplicities */
1113 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1114 break;
1115 }
1116 }
1117
Michal Vasko0587bce2020-12-10 12:19:21 +01001118 if (j < orig_used) {
1119 /* node is there, but update its status if needed */
1120 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1121 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
1122 }
1123 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001124 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1125 ++set1->used;
1126 }
1127 }
1128
Michal Vaskod3678892020-05-21 10:06:58 +02001129 lyxp_set_free_content(set2);
1130 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001131}
1132
1133/**
1134 * @brief Insert a node into a set. Context position aware.
1135 *
1136 * @param[in] set Set to use.
1137 * @param[in] node Node to insert to @p set.
1138 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1139 * @param[in] node_type Node type of @p node.
1140 * @param[in] idx Index in @p set to insert into.
1141 */
1142static void
1143set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1144{
Michal Vaskod3678892020-05-21 10:06:58 +02001145 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001146
Michal Vaskod3678892020-05-21 10:06:58 +02001147 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001148 /* first item */
1149 if (idx) {
1150 /* no real harm done, but it is a bug */
1151 LOGINT(set->ctx);
1152 idx = 0;
1153 }
1154 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1155 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1156 set->type = LYXP_SET_NODE_SET;
1157 set->used = 0;
1158 set->size = LYXP_SET_SIZE_START;
1159 set->ctx_pos = 1;
1160 set->ctx_size = 1;
1161 set->ht = NULL;
1162 } else {
1163 /* not an empty set */
1164 if (set->used == set->size) {
1165
1166 /* set is full */
1167 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1168 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1169 set->size += LYXP_SET_SIZE_STEP;
1170 }
1171
1172 if (idx > set->used) {
1173 LOGINT(set->ctx);
1174 idx = set->used;
1175 }
1176
1177 /* make space for the new node */
1178 if (idx < set->used) {
1179 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1180 }
1181 }
1182
1183 /* finally assign the value */
1184 set->val.nodes[idx].node = (struct lyd_node *)node;
1185 set->val.nodes[idx].type = node_type;
1186 set->val.nodes[idx].pos = pos;
1187 ++set->used;
1188
Michal Vasko2caefc12019-11-14 16:07:56 +01001189 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1190 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1191 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001192}
1193
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001194LY_ERR
1195lyxp_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 +02001196{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001197 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001198
1199 assert(set->type == LYXP_SET_SCNODE_SET);
1200
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001201 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001202 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001203 } else {
1204 if (set->used == set->size) {
1205 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 +02001206 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001207 set->size += LYXP_SET_SIZE_STEP;
1208 }
1209
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001210 index = set->used;
1211 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1212 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001213 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001214 ++set->used;
1215 }
1216
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001217 if (index_p) {
1218 *index_p = index;
1219 }
1220
1221 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001222}
1223
1224/**
1225 * @brief Replace a node in a set with another. Context position aware.
1226 *
1227 * @param[in] set Set to use.
1228 * @param[in] node Node to insert to @p set.
1229 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1230 * @param[in] node_type Node type of @p node.
1231 * @param[in] idx Index in @p set of the node to replace.
1232 */
1233static void
1234set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1235{
1236 assert(set && (idx < set->used));
1237
Michal Vasko2caefc12019-11-14 16:07:56 +01001238 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1239 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1240 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001241 set->val.nodes[idx].node = (struct lyd_node *)node;
1242 set->val.nodes[idx].type = node_type;
1243 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001244 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1245 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1246 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001247}
1248
1249/**
1250 * @brief Set all nodes with ctx 1 to a new unique context value.
1251 *
1252 * @param[in] set Set to modify.
1253 * @return New context value.
1254 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001255static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001256set_scnode_new_in_ctx(struct lyxp_set *set)
1257{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001258 uint32_t i;
1259 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001260
1261 assert(set->type == LYXP_SET_SCNODE_SET);
1262
Radek Krejcif13b87b2020-12-01 22:02:17 +01001263 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001264retry:
1265 for (i = 0; i < set->used; ++i) {
1266 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1267 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1268 goto retry;
1269 }
1270 }
1271 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001272 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001273 set->val.scnodes[i].in_ctx = ret_ctx;
1274 }
1275 }
1276
1277 return ret_ctx;
1278}
1279
1280/**
1281 * @brief Get unique @p node position in the data.
1282 *
1283 * @param[in] node Node to find.
1284 * @param[in] node_type Node type of @p node.
1285 * @param[in] root Root node.
1286 * @param[in] root_type Type of the XPath @p root node.
1287 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1288 * be used to increase efficiency and start the DFS from this node.
1289 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1290 * @return Node position.
1291 */
1292static uint32_t
1293get_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 +02001294 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001295{
Michal Vasko56daf732020-08-10 10:57:18 +02001296 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001297 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001298 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001299
1300 assert(prev && prev_pos && !root->prev->next);
1301
1302 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1303 return 0;
1304 }
1305
1306 if (*prev) {
1307 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001308 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001309 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001310 goto dfs_search;
1311 }
1312
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001313 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001314 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001315dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001316 if (*prev && !elem) {
1317 /* resume previous DFS */
1318 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1319 LYD_TREE_DFS_continue = 0;
1320 }
1321
Michal Vasko03ff5a72019-09-11 13:49:33 +02001322 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001323 /* skip */
1324 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001325 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001326 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001327 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001328 break;
1329 }
Michal Vasko56daf732020-08-10 10:57:18 +02001330 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001331 }
Michal Vasko56daf732020-08-10 10:57:18 +02001332
1333 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001334 }
1335
1336 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001337 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001338 break;
1339 }
1340 }
1341
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001342 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001343 if (!(*prev)) {
1344 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001345 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001346 return 0;
1347 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001348 /* start the search again from the beginning */
1349 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001350
Michal Vasko56daf732020-08-10 10:57:18 +02001351 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001352 pos = 1;
1353 goto dfs_search;
1354 }
1355 }
1356
1357 /* remember the last found node for next time */
1358 *prev = node;
1359 *prev_pos = pos;
1360
1361 return pos;
1362}
1363
1364/**
1365 * @brief Assign (fill) missing node positions.
1366 *
1367 * @param[in] set Set to fill positions in.
1368 * @param[in] root Context root node.
1369 * @param[in] root_type Context root type.
1370 * @return LY_ERR
1371 */
1372static LY_ERR
1373set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1374{
1375 const struct lyd_node *prev = NULL, *tmp_node;
1376 uint32_t i, tmp_pos = 0;
1377
1378 for (i = 0; i < set->used; ++i) {
1379 if (!set->val.nodes[i].pos) {
1380 tmp_node = NULL;
1381 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001382 case LYXP_NODE_META:
1383 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001384 if (!tmp_node) {
1385 LOGINT_RET(root->schema->module->ctx);
1386 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001387 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001388 case LYXP_NODE_ELEM:
1389 case LYXP_NODE_TEXT:
1390 if (!tmp_node) {
1391 tmp_node = set->val.nodes[i].node;
1392 }
1393 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1394 break;
1395 default:
1396 /* all roots have position 0 */
1397 break;
1398 }
1399 }
1400 }
1401
1402 return LY_SUCCESS;
1403}
1404
1405/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001406 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001407 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001408 * @param[in] meta Metadata to use.
1409 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001410 */
1411static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001412get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001413{
1414 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001415 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001416
Michal Vasko9f96a052020-03-10 09:41:45 +01001417 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001418 ++pos;
1419 }
1420
Michal Vasko9f96a052020-03-10 09:41:45 +01001421 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001422 return pos;
1423}
1424
1425/**
1426 * @brief Compare 2 nodes in respect to XPath document order.
1427 *
1428 * @param[in] item1 1st node.
1429 * @param[in] item2 2nd node.
1430 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1431 */
1432static int
1433set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1434{
Michal Vasko9f96a052020-03-10 09:41:45 +01001435 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001436
1437 if (item1->pos < item2->pos) {
1438 return -1;
1439 }
1440
1441 if (item1->pos > item2->pos) {
1442 return 1;
1443 }
1444
1445 /* node positions are equal, the fun case */
1446
1447 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1448 /* special case since text nodes are actually saved as their parents */
1449 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1450 if (item1->type == LYXP_NODE_ELEM) {
1451 assert(item2->type == LYXP_NODE_TEXT);
1452 return -1;
1453 } else {
1454 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1455 return 1;
1456 }
1457 }
1458
Michal Vasko9f96a052020-03-10 09:41:45 +01001459 /* we need meta positions now */
1460 if (item1->type == LYXP_NODE_META) {
1461 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001462 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001463 if (item2->type == LYXP_NODE_META) {
1464 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001465 }
1466
Michal Vasko9f96a052020-03-10 09:41:45 +01001467 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001468 /* check for duplicates */
1469 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001470 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001471 return 0;
1472 }
1473
Michal Vasko9f96a052020-03-10 09:41:45 +01001474 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001475 /* elem is always first, 2nd node is after it */
1476 if (item1->type == LYXP_NODE_ELEM) {
1477 assert(item2->type != LYXP_NODE_ELEM);
1478 return -1;
1479 }
1480
Michal Vasko9f96a052020-03-10 09:41:45 +01001481 /* 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 +02001482 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001483 if (((item1->type == LYXP_NODE_TEXT) &&
1484 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1485 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1486 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1487 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001488 return 1;
1489 }
1490
Michal Vasko9f96a052020-03-10 09:41:45 +01001491 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001492 /* 2nd is after 1st */
1493 return -1;
1494}
1495
1496/**
1497 * @brief Set cast for comparisons.
1498 *
1499 * @param[in] trg Target set to cast source into.
1500 * @param[in] src Source set.
1501 * @param[in] type Target set type.
1502 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001503 * @return LY_ERR
1504 */
1505static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001506set_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 +02001507{
1508 assert(src->type == LYXP_SET_NODE_SET);
1509
1510 set_init(trg, src);
1511
1512 /* insert node into target set */
1513 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1514
1515 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001516 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001517}
1518
Michal Vasko4c7763f2020-07-27 17:40:37 +02001519/**
1520 * @brief Set content canonization for comparisons.
1521 *
1522 * @param[in] trg Target set to put the canononized source into.
1523 * @param[in] src Source set.
1524 * @param[in] xp_node Source XPath node/meta to use for canonization.
1525 * @return LY_ERR
1526 */
1527static LY_ERR
1528set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1529{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001530 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001531 struct lyd_value val;
1532 struct ly_err_item *err = NULL;
1533 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001534 LY_ERR rc;
1535
1536 /* is there anything to canonize even? */
1537 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1538 /* do we have a type to use for canonization? */
1539 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1540 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1541 } else if (xp_node->type == LYXP_NODE_META) {
1542 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1543 }
1544 }
1545 if (!type) {
1546 goto fill;
1547 }
1548
1549 if (src->type == LYXP_SET_NUMBER) {
1550 /* canonize number */
1551 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1552 LOGMEM(src->ctx);
1553 return LY_EMEM;
1554 }
1555 } else {
1556 /* canonize string */
1557 str = strdup(src->val.str);
1558 }
1559
1560 /* ignore errors, the value may not satisfy schema constraints */
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001561 rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_STORE_DYNAMIC, src->format, src->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001562 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001563 ly_err_free(err);
1564 if (rc) {
1565 /* invalid value */
1566 free(str);
1567 goto fill;
1568 }
1569
Michal Vasko4c7763f2020-07-27 17:40:37 +02001570 /* use the canonized value */
1571 set_init(trg, src);
1572 trg->type = src->type;
1573 if (src->type == LYXP_SET_NUMBER) {
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001574 trg->val.num = strtold(val.canonical, &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001575 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1576 } else {
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001577 trg->val.str = strdup(val.canonical);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001578 }
1579 type->plugin->free(src->ctx, &val);
1580 return LY_SUCCESS;
1581
1582fill:
1583 /* no canonization needed/possible */
1584 set_fill_set(trg, src);
1585 return LY_SUCCESS;
1586}
1587
Michal Vasko03ff5a72019-09-11 13:49:33 +02001588#ifndef NDEBUG
1589
1590/**
1591 * @brief Bubble sort @p set into XPath document order.
1592 * Context position aware. Unused in the 'Release' build target.
1593 *
1594 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001595 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1596 */
1597static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001598set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001599{
1600 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001601 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001602 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001603 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001604 struct lyxp_set_node item;
1605 struct lyxp_set_hash_node hnode;
1606 uint64_t hash;
1607
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001608 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001609 return 0;
1610 }
1611
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001612 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001613 for (root = set->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001614 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001615
1616 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001617 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001618 return -1;
1619 }
1620
1621 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1622 print_set_debug(set);
1623
1624 for (i = 0; i < set->used; ++i) {
1625 inverted = 0;
1626 change = 0;
1627
1628 for (j = 1; j < set->used - i; ++j) {
1629 /* compare node positions */
1630 if (inverted) {
1631 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1632 } else {
1633 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1634 }
1635
1636 /* swap if needed */
1637 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1638 change = 1;
1639
1640 item = set->val.nodes[j - 1];
1641 set->val.nodes[j - 1] = set->val.nodes[j];
1642 set->val.nodes[j] = item;
1643 } else {
1644 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1645 inverted = !inverted;
1646 }
1647 }
1648
1649 ++ret;
1650
1651 if (!change) {
1652 break;
1653 }
1654 }
1655
1656 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1657 print_set_debug(set);
1658
1659 /* check node hashes */
1660 if (set->used >= LYD_HT_MIN_ITEMS) {
1661 assert(set->ht);
1662 for (i = 0; i < set->used; ++i) {
1663 hnode.node = set->val.nodes[i].node;
1664 hnode.type = set->val.nodes[i].type;
1665
1666 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1667 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1668 hash = dict_hash_multi(hash, NULL, 0);
1669
1670 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1671 }
1672 }
1673
1674 return ret - 1;
1675}
1676
1677/**
1678 * @brief Remove duplicate entries in a sorted node set.
1679 *
1680 * @param[in] set Sorted set to check.
1681 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1682 */
1683static LY_ERR
1684set_sorted_dup_node_clean(struct lyxp_set *set)
1685{
1686 uint32_t i = 0;
1687 LY_ERR ret = LY_SUCCESS;
1688
1689 if (set->used > 1) {
1690 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001691 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1692 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001693 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001694 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001695 }
Michal Vasko57eab132019-09-24 11:46:26 +02001696 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001697 }
1698 }
1699
Michal Vasko2caefc12019-11-14 16:07:56 +01001700 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001701 return ret;
1702}
1703
1704#endif
1705
1706/**
1707 * @brief Merge 2 sorted sets into one.
1708 *
1709 * @param[in,out] trg Set to merge into. Duplicates are removed.
1710 * @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 +02001711 * @return LY_ERR
1712 */
1713static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001714set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001715{
1716 uint32_t i, j, k, count, dup_count;
1717 int cmp;
1718 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001719
Michal Vaskod3678892020-05-21 10:06:58 +02001720 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001721 return LY_EINVAL;
1722 }
1723
Michal Vaskod3678892020-05-21 10:06:58 +02001724 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001725 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001726 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001727 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001728 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001729 return LY_SUCCESS;
1730 }
1731
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001732 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001733 for (root = trg->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001734 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001735
1736 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001737 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001738 return LY_EINT;
1739 }
1740
1741#ifndef NDEBUG
1742 LOGDBG(LY_LDGXPATH, "MERGE target");
1743 print_set_debug(trg);
1744 LOGDBG(LY_LDGXPATH, "MERGE source");
1745 print_set_debug(src);
1746#endif
1747
1748 /* make memory for the merge (duplicates are not detected yet, so space
1749 * will likely be wasted on them, too bad) */
1750 if (trg->size - trg->used < src->used) {
1751 trg->size = trg->used + src->used;
1752
1753 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1754 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1755 }
1756
1757 i = 0;
1758 j = 0;
1759 count = 0;
1760 dup_count = 0;
1761 do {
1762 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1763 if (!cmp) {
1764 if (!count) {
1765 /* duplicate, just skip it */
1766 ++i;
1767 ++j;
1768 } else {
1769 /* we are copying something already, so let's copy the duplicate too,
1770 * we are hoping that afterwards there are some more nodes to
1771 * copy and this way we can copy them all together */
1772 ++count;
1773 ++dup_count;
1774 ++i;
1775 ++j;
1776 }
1777 } else if (cmp < 0) {
1778 /* inserting src node into trg, just remember it for now */
1779 ++count;
1780 ++i;
1781
1782 /* insert the hash now */
1783 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1784 } else if (count) {
1785copy_nodes:
1786 /* time to actually copy the nodes, we have found the largest block of nodes */
1787 memmove(&trg->val.nodes[j + (count - dup_count)],
1788 &trg->val.nodes[j],
1789 (trg->used - j) * sizeof *trg->val.nodes);
1790 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1791
1792 trg->used += count - dup_count;
1793 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1794 j += count - dup_count;
1795
1796 count = 0;
1797 dup_count = 0;
1798 } else {
1799 ++j;
1800 }
1801 } while ((i < src->used) && (j < trg->used));
1802
1803 if ((i < src->used) || count) {
1804 /* insert all the hashes first */
1805 for (k = i; k < src->used; ++k) {
1806 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1807 }
1808
1809 /* loop ended, but we need to copy something at trg end */
1810 count += src->used - i;
1811 i = src->used;
1812 goto copy_nodes;
1813 }
1814
1815 /* we are inserting hashes before the actual node insert, which causes
1816 * situations when there were initially not enough items for a hash table,
1817 * but even after some were inserted, hash table was not created (during
1818 * insertion the number of items is not updated yet) */
1819 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1820 set_insert_node_hash(trg, NULL, 0);
1821 }
1822
1823#ifndef NDEBUG
1824 LOGDBG(LY_LDGXPATH, "MERGE result");
1825 print_set_debug(trg);
1826#endif
1827
Michal Vaskod3678892020-05-21 10:06:58 +02001828 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001829 return LY_SUCCESS;
1830}
1831
1832/*
1833 * (re)parse functions
1834 *
1835 * Parse functions parse the expression into
1836 * tokens (syntactic analysis).
1837 *
1838 * Reparse functions perform semantic analysis
1839 * (do not save the result, just a check) of
1840 * the expression and fill repeat indices.
1841 */
1842
Michal Vasko14676352020-05-29 11:35:55 +02001843LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001844lyxp_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 +02001845{
Michal Vasko004d3152020-06-11 19:59:22 +02001846 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001847 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001848 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001849 }
Michal Vasko14676352020-05-29 11:35:55 +02001850 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001851 }
1852
Michal Vasko004d3152020-06-11 19:59:22 +02001853 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001854 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001855 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001856 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001857 }
Michal Vasko14676352020-05-29 11:35:55 +02001858 return LY_ENOT;
1859 }
1860
1861 return LY_SUCCESS;
1862}
1863
Michal Vasko004d3152020-06-11 19:59:22 +02001864LY_ERR
1865lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1866{
1867 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1868
1869 /* skip the token */
1870 ++(*tok_idx);
1871
1872 return LY_SUCCESS;
1873}
1874
Michal Vasko14676352020-05-29 11:35:55 +02001875/* just like lyxp_check_token() but tests for 2 tokens */
1876static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001877exp_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 +02001878 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001879{
Michal Vasko004d3152020-06-11 19:59:22 +02001880 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001881 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001882 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001883 }
1884 return LY_EINCOMPLETE;
1885 }
1886
Michal Vasko004d3152020-06-11 19:59:22 +02001887 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001888 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001889 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001890 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001891 }
1892 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001893 }
1894
1895 return LY_SUCCESS;
1896}
1897
1898/**
1899 * @brief Stack operation push on the repeat array.
1900 *
1901 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001902 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001903 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1904 */
1905static void
Michal Vasko004d3152020-06-11 19:59:22 +02001906exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001907{
1908 uint16_t i;
1909
Michal Vasko004d3152020-06-11 19:59:22 +02001910 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001911 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001912 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1913 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1914 exp->repeat[tok_idx][i] = repeat_op_idx;
1915 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001916 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001917 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1918 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1919 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001920 }
1921}
1922
1923/**
1924 * @brief Reparse Predicate. Logs directly on error.
1925 *
1926 * [7] Predicate ::= '[' Expr ']'
1927 *
1928 * @param[in] ctx Context for logging.
1929 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001930 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001931 * @return LY_ERR
1932 */
1933static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001934reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001935{
1936 LY_ERR rc;
1937
Michal Vasko004d3152020-06-11 19:59:22 +02001938 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001939 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001940 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001941
Michal Vasko004d3152020-06-11 19:59:22 +02001942 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001943 LY_CHECK_RET(rc);
1944
Michal Vasko004d3152020-06-11 19:59:22 +02001945 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001946 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001947 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001948
1949 return LY_SUCCESS;
1950}
1951
1952/**
1953 * @brief Reparse RelativeLocationPath. Logs directly on error.
1954 *
1955 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1956 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1957 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1958 *
1959 * @param[in] ctx Context for logging.
1960 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001961 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001962 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1963 */
1964static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001965reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001966{
1967 LY_ERR rc;
1968
Michal Vasko004d3152020-06-11 19:59:22 +02001969 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001970 LY_CHECK_RET(rc);
1971
1972 goto step;
1973 do {
1974 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001975 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001976
Michal Vasko004d3152020-06-11 19:59:22 +02001977 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001978 LY_CHECK_RET(rc);
1979step:
1980 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001981 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001982 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001983 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001984 break;
1985
1986 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001987 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001988 break;
1989
1990 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02001991 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001992
Michal Vasko004d3152020-06-11 19:59:22 +02001993 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001994 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001995 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001996 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 +02001997 return LY_EVALID;
1998 }
Radek Krejci0f969882020-08-21 16:56:47 +02001999 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002000 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002001 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002002 goto reparse_predicate;
2003 break;
2004
2005 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002006 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002007
2008 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002009 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002010 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002011 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002012
2013 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002014 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002015 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002016 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002017
2018reparse_predicate:
2019 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002020 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2021 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002022 LY_CHECK_RET(rc);
2023 }
2024 break;
2025 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002026 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 +02002027 return LY_EVALID;
2028 }
Michal Vasko004d3152020-06-11 19:59:22 +02002029 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002030
2031 return LY_SUCCESS;
2032}
2033
2034/**
2035 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2036 *
2037 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2038 *
2039 * @param[in] ctx Context for logging.
2040 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002041 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002042 * @return LY_ERR
2043 */
2044static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002045reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002046{
2047 LY_ERR rc;
2048
Michal Vasko004d3152020-06-11 19:59:22 +02002049 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 +02002050
2051 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002052 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002053 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002054 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002055
Michal Vasko004d3152020-06-11 19:59:22 +02002056 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002057 return LY_SUCCESS;
2058 }
Michal Vasko004d3152020-06-11 19:59:22 +02002059 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002060 case LYXP_TOKEN_DOT:
2061 case LYXP_TOKEN_DDOT:
2062 case LYXP_TOKEN_AT:
2063 case LYXP_TOKEN_NAMETEST:
2064 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002065 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002066 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002067 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002068 default:
2069 break;
2070 }
2071
Michal Vasko03ff5a72019-09-11 13:49:33 +02002072 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002073 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002074 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002075
Michal Vasko004d3152020-06-11 19:59:22 +02002076 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002077 LY_CHECK_RET(rc);
2078 }
2079
2080 return LY_SUCCESS;
2081}
2082
2083/**
2084 * @brief Reparse FunctionCall. Logs directly on error.
2085 *
2086 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2087 *
2088 * @param[in] ctx Context for logging.
2089 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002090 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002091 * @return LY_ERR
2092 */
2093static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002094reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002095{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002096 int8_t min_arg_count = -1;
2097 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002098 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002099 LY_ERR rc;
2100
Michal Vasko004d3152020-06-11 19:59:22 +02002101 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002102 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002103 func_tok_idx = *tok_idx;
2104 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002105 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002106 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002107 min_arg_count = 1;
2108 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002109 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002110 min_arg_count = 1;
2111 max_arg_count = 1;
2112 }
2113 break;
2114 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002115 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002116 min_arg_count = 1;
2117 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002118 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002119 min_arg_count = 0;
2120 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002121 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002122 min_arg_count = 0;
2123 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002124 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002125 min_arg_count = 0;
2126 max_arg_count = 0;
2127 }
2128 break;
2129 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002130 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002131 min_arg_count = 1;
2132 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002133 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002134 min_arg_count = 0;
2135 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002136 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002137 min_arg_count = 1;
2138 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002139 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002140 min_arg_count = 1;
2141 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002142 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002143 min_arg_count = 1;
2144 max_arg_count = 1;
2145 }
2146 break;
2147 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002148 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002149 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002150 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002151 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002152 min_arg_count = 0;
2153 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002154 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002155 min_arg_count = 0;
2156 max_arg_count = 1;
2157 }
2158 break;
2159 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002160 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002161 min_arg_count = 1;
2162 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002163 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002164 min_arg_count = 1;
2165 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002166 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002167 min_arg_count = 0;
2168 max_arg_count = 0;
2169 }
2170 break;
2171 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002172 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002173 min_arg_count = 2;
2174 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002175 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002176 min_arg_count = 0;
2177 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002178 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002179 min_arg_count = 2;
2180 max_arg_count = 2;
2181 }
2182 break;
2183 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002184 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002185 min_arg_count = 2;
2186 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002187 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002188 min_arg_count = 3;
2189 max_arg_count = 3;
2190 }
2191 break;
2192 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002193 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002194 min_arg_count = 0;
2195 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002196 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002197 min_arg_count = 1;
2198 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002199 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002200 min_arg_count = 2;
2201 max_arg_count = 2;
2202 }
2203 break;
2204 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002205 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002206 min_arg_count = 2;
2207 max_arg_count = 2;
2208 }
2209 break;
2210 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002211 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002212 min_arg_count = 2;
2213 max_arg_count = 2;
2214 }
2215 break;
2216 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002217 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002218 min_arg_count = 0;
2219 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002220 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002221 min_arg_count = 0;
2222 max_arg_count = 1;
2223 }
2224 break;
2225 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002226 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002227 min_arg_count = 0;
2228 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002229 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002230 min_arg_count = 2;
2231 max_arg_count = 2;
2232 }
2233 break;
2234 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002235 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002236 min_arg_count = 2;
2237 max_arg_count = 2;
2238 }
2239 break;
2240 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002241 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002242 min_arg_count = 2;
2243 max_arg_count = 2;
2244 }
2245 break;
2246 }
2247 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002248 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 +02002249 return LY_EINVAL;
2250 }
Michal Vasko004d3152020-06-11 19:59:22 +02002251 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002252
2253 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002254 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002255 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002256 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002257
2258 /* ( Expr ( ',' Expr )* )? */
2259 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002260 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002261 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002262 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002263 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002264 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002265 LY_CHECK_RET(rc);
2266 }
Michal Vasko004d3152020-06-11 19:59:22 +02002267 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2268 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002269
2270 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002271 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002272 LY_CHECK_RET(rc);
2273 }
2274
2275 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002276 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002277 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002278 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002279
Radek Krejci857189e2020-09-01 13:26:36 +02002280 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002281 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 +02002282 return LY_EVALID;
2283 }
2284
2285 return LY_SUCCESS;
2286}
2287
2288/**
2289 * @brief Reparse PathExpr. Logs directly on error.
2290 *
2291 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2292 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2293 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2294 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2295 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2296 *
2297 * @param[in] ctx Context for logging.
2298 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002299 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002300 * @return LY_ERR
2301 */
2302static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002303reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002304{
2305 LY_ERR rc;
2306
Michal Vasko004d3152020-06-11 19:59:22 +02002307 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002308 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002309 }
2310
Michal Vasko004d3152020-06-11 19:59:22 +02002311 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002312 case LYXP_TOKEN_PAR1:
2313 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002314 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002315
Michal Vasko004d3152020-06-11 19:59:22 +02002316 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002317 LY_CHECK_RET(rc);
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 goto predicate;
2323 break;
2324 case LYXP_TOKEN_DOT:
2325 case LYXP_TOKEN_DDOT:
2326 case LYXP_TOKEN_AT:
2327 case LYXP_TOKEN_NAMETEST:
2328 case LYXP_TOKEN_NODETYPE:
2329 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002330 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002331 LY_CHECK_RET(rc);
2332 break;
2333 case LYXP_TOKEN_FUNCNAME:
2334 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002335 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002336 LY_CHECK_RET(rc);
2337 goto predicate;
2338 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002339 case LYXP_TOKEN_OPER_PATH:
2340 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002341 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002342 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002343 LY_CHECK_RET(rc);
2344 break;
2345 case LYXP_TOKEN_LITERAL:
2346 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002347 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002348 goto predicate;
2349 break;
2350 case LYXP_TOKEN_NUMBER:
2351 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002352 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002353 goto predicate;
2354 break;
2355 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002356 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 +02002357 return LY_EVALID;
2358 }
2359
2360 return LY_SUCCESS;
2361
2362predicate:
2363 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002364 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2365 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002366 LY_CHECK_RET(rc);
2367 }
2368
2369 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002370 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002371
2372 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002373 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002374
Michal Vasko004d3152020-06-11 19:59:22 +02002375 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002376 LY_CHECK_RET(rc);
2377 }
2378
2379 return LY_SUCCESS;
2380}
2381
2382/**
2383 * @brief Reparse UnaryExpr. Logs directly on error.
2384 *
2385 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2386 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2387 *
2388 * @param[in] ctx Context for logging.
2389 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002390 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002391 * @return LY_ERR
2392 */
2393static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002394reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002395{
2396 uint16_t prev_exp;
2397 LY_ERR rc;
2398
2399 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002400 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002401 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2402 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002403 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002404 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002405 }
2406
2407 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002408 prev_exp = *tok_idx;
2409 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002410 LY_CHECK_RET(rc);
2411
2412 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002413 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002414 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002415 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002416
Michal Vasko004d3152020-06-11 19:59:22 +02002417 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002418 LY_CHECK_RET(rc);
2419 }
2420
2421 return LY_SUCCESS;
2422}
2423
2424/**
2425 * @brief Reparse AdditiveExpr. Logs directly on error.
2426 *
2427 * [15] AdditiveExpr ::= MultiplicativeExpr
2428 * | AdditiveExpr '+' MultiplicativeExpr
2429 * | AdditiveExpr '-' MultiplicativeExpr
2430 * [16] MultiplicativeExpr ::= UnaryExpr
2431 * | MultiplicativeExpr '*' UnaryExpr
2432 * | MultiplicativeExpr 'div' UnaryExpr
2433 * | MultiplicativeExpr 'mod' UnaryExpr
2434 *
2435 * @param[in] ctx Context for logging.
2436 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002437 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002438 * @return LY_ERR
2439 */
2440static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002441reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002442{
2443 uint16_t prev_add_exp, prev_mul_exp;
2444 LY_ERR rc;
2445
Michal Vasko004d3152020-06-11 19:59:22 +02002446 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002447 goto reparse_multiplicative_expr;
2448
2449 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002450 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2451 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002452 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002453 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002454
2455reparse_multiplicative_expr:
2456 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002457 prev_mul_exp = *tok_idx;
2458 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002459 LY_CHECK_RET(rc);
2460
2461 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002462 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2463 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002464 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002465 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002466
Michal Vasko004d3152020-06-11 19:59:22 +02002467 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002468 LY_CHECK_RET(rc);
2469 }
2470 }
2471
2472 return LY_SUCCESS;
2473}
2474
2475/**
2476 * @brief Reparse EqualityExpr. Logs directly on error.
2477 *
2478 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2479 * | EqualityExpr '!=' RelationalExpr
2480 * [14] RelationalExpr ::= AdditiveExpr
2481 * | RelationalExpr '<' AdditiveExpr
2482 * | RelationalExpr '>' AdditiveExpr
2483 * | RelationalExpr '<=' AdditiveExpr
2484 * | RelationalExpr '>=' AdditiveExpr
2485 *
2486 * @param[in] ctx Context for logging.
2487 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002488 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002489 * @return LY_ERR
2490 */
2491static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002492reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002493{
2494 uint16_t prev_eq_exp, prev_rel_exp;
2495 LY_ERR rc;
2496
Michal Vasko004d3152020-06-11 19:59:22 +02002497 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002498 goto reparse_additive_expr;
2499
2500 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002501 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002502 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002503 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002504
2505reparse_additive_expr:
2506 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002507 prev_rel_exp = *tok_idx;
2508 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002509 LY_CHECK_RET(rc);
2510
2511 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002512 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002513 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002514 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002515
Michal Vasko004d3152020-06-11 19:59:22 +02002516 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002517 LY_CHECK_RET(rc);
2518 }
2519 }
2520
2521 return LY_SUCCESS;
2522}
2523
2524/**
2525 * @brief Reparse OrExpr. Logs directly on error.
2526 *
2527 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2528 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2529 *
2530 * @param[in] ctx Context for logging.
2531 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002532 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002533 * @return LY_ERR
2534 */
2535static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002536reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002537{
2538 uint16_t prev_or_exp, prev_and_exp;
2539 LY_ERR rc;
2540
Michal Vasko004d3152020-06-11 19:59:22 +02002541 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002542 goto reparse_equality_expr;
2543
2544 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002545 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 +02002546 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002547 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002548
2549reparse_equality_expr:
2550 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002551 prev_and_exp = *tok_idx;
2552 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002553 LY_CHECK_RET(rc);
2554
2555 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002556 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 +02002557 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002558 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002559
Michal Vasko004d3152020-06-11 19:59:22 +02002560 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002561 LY_CHECK_RET(rc);
2562 }
2563 }
2564
2565 return LY_SUCCESS;
2566}
Radek Krejcib1646a92018-11-02 16:08:26 +01002567
2568/**
2569 * @brief Parse NCName.
2570 *
2571 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002572 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002573 */
Radek Krejcid4270262019-01-07 15:07:25 +01002574static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002575parse_ncname(const char *ncname)
2576{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002577 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002578 size_t size;
2579 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002580
2581 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2582 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2583 return len;
2584 }
2585
2586 do {
2587 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002588 if (!*ncname) {
2589 break;
2590 }
Radek Krejcid4270262019-01-07 15:07:25 +01002591 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002592 } while (is_xmlqnamechar(uc) && (uc != ':'));
2593
2594 return len;
2595}
2596
2597/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002598 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002599 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002600 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002601 * @param[in] exp Expression to use.
2602 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002603 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002604 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002605 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002606 */
2607static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002608exp_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 +01002609{
2610 uint32_t prev;
2611
2612 if (exp->used == exp->size) {
2613 prev = exp->size;
2614 exp->size += LYXP_EXPR_SIZE_STEP;
2615 if (prev > exp->size) {
2616 LOGINT(ctx);
2617 return LY_EINT;
2618 }
2619
2620 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2621 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2622 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2623 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2624 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2625 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2626 }
2627
2628 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002629 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002630 exp->tok_len[exp->used] = tok_len;
2631 ++exp->used;
2632 return LY_SUCCESS;
2633}
2634
2635void
Michal Vasko14676352020-05-29 11:35:55 +02002636lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002637{
2638 uint16_t i;
2639
2640 if (!expr) {
2641 return;
2642 }
2643
2644 lydict_remove(ctx, expr->expr);
2645 free(expr->tokens);
2646 free(expr->tok_pos);
2647 free(expr->tok_len);
2648 if (expr->repeat) {
2649 for (i = 0; i < expr->used; ++i) {
2650 free(expr->repeat[i]);
2651 }
2652 }
2653 free(expr->repeat);
2654 free(expr);
2655}
2656
Radek Krejcif03a9e22020-09-18 20:09:31 +02002657LY_ERR
2658lyxp_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 +01002659{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002660 LY_ERR ret = LY_SUCCESS;
2661 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002662 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002663 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002664 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002665 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002666
Radek Krejcif03a9e22020-09-18 20:09:31 +02002667 assert(expr_p);
2668
2669 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002670 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002671 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002672 }
2673
2674 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002675 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002676 }
2677 if (expr_len > UINT16_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002678 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002679 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002680 }
2681
2682 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002683 expr = calloc(1, sizeof *expr);
2684 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2685 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2686 expr->used = 0;
2687 expr->size = LYXP_EXPR_SIZE_START;
2688 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2689 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002690
Radek Krejcif03a9e22020-09-18 20:09:31 +02002691 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2692 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002693
Radek Krejcif03a9e22020-09-18 20:09:31 +02002694 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2695 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002696
Michal Vasko004d3152020-06-11 19:59:22 +02002697 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002698 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002699
Radek Krejcif03a9e22020-09-18 20:09:31 +02002700 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002701 ++parsed;
2702 }
2703
2704 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002705 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002706
2707 /* '(' */
2708 tok_len = 1;
2709 tok_type = LYXP_TOKEN_PAR1;
2710
Radek Krejcif03a9e22020-09-18 20:09:31 +02002711 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002712 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002713 if (((expr->tok_len[expr->used - 1] == 4) &&
2714 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2715 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2716 ((expr->tok_len[expr->used - 1] == 7) &&
2717 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002718 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002719 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002720 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002721 }
2722 prev_function_check = 0;
2723 }
2724
Radek Krejcif03a9e22020-09-18 20:09:31 +02002725 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002726
2727 /* ')' */
2728 tok_len = 1;
2729 tok_type = LYXP_TOKEN_PAR2;
2730
Radek Krejcif03a9e22020-09-18 20:09:31 +02002731 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002732
2733 /* '[' */
2734 tok_len = 1;
2735 tok_type = LYXP_TOKEN_BRACK1;
2736
Radek Krejcif03a9e22020-09-18 20:09:31 +02002737 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002738
2739 /* ']' */
2740 tok_len = 1;
2741 tok_type = LYXP_TOKEN_BRACK2;
2742
Radek Krejcif03a9e22020-09-18 20:09:31 +02002743 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002744
2745 /* '..' */
2746 tok_len = 2;
2747 tok_type = LYXP_TOKEN_DDOT;
2748
Radek Krejcif03a9e22020-09-18 20:09:31 +02002749 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002750
2751 /* '.' */
2752 tok_len = 1;
2753 tok_type = LYXP_TOKEN_DOT;
2754
Radek Krejcif03a9e22020-09-18 20:09:31 +02002755 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002756
2757 /* '@' */
2758 tok_len = 1;
2759 tok_type = LYXP_TOKEN_AT;
2760
Radek Krejcif03a9e22020-09-18 20:09:31 +02002761 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002762
2763 /* ',' */
2764 tok_len = 1;
2765 tok_type = LYXP_TOKEN_COMMA;
2766
Radek Krejcif03a9e22020-09-18 20:09:31 +02002767 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002768
2769 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002770 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2771 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002772 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002773 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002774 ++tok_len;
2775 tok_type = LYXP_TOKEN_LITERAL;
2776
Radek Krejcif03a9e22020-09-18 20:09:31 +02002777 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002778
2779 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002780 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2781 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002782 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002783 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002784 ++tok_len;
2785 tok_type = LYXP_TOKEN_LITERAL;
2786
Radek Krejcif03a9e22020-09-18 20:09:31 +02002787 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002788
2789 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002790 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2791 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002792 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002793 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002794 }
2795 tok_type = LYXP_TOKEN_NUMBER;
2796
Radek Krejcif03a9e22020-09-18 20:09:31 +02002797 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002798
2799 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002800 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002801 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002802 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002803 } else {
2804 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002805 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002806 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002807
Radek Krejcif03a9e22020-09-18 20:09:31 +02002808 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002809
Michal Vasko3e48bf32020-06-01 08:39:07 +02002810 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002811 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002812 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2813
Radek Krejcif03a9e22020-09-18 20:09:31 +02002814 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002815
2816 /* Operator '<=', '>=' */
2817 tok_len = 2;
2818 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002819
Radek Krejcif03a9e22020-09-18 20:09:31 +02002820 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002821
2822 /* Operator '|' */
2823 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002824 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002825
Radek Krejcif03a9e22020-09-18 20:09:31 +02002826 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002827
2828 /* Operator '+', '-' */
2829 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002830 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002831
Radek Krejcif03a9e22020-09-18 20:09:31 +02002832 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002833
Michal Vasko3e48bf32020-06-01 08:39:07 +02002834 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002835 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002836 tok_type = LYXP_TOKEN_OPER_EQUAL;
2837
Radek Krejcif03a9e22020-09-18 20:09:31 +02002838 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002839
2840 /* Operator '<', '>' */
2841 tok_len = 1;
2842 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002843
Michal Vasko69730152020-10-09 16:30:07 +02002844 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2845 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2846 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2847 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2848 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2849 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2850 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2851 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2852 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2853 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2854 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2855 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002856
2857 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002858 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002859 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002860 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002861
Radek Krejcif03a9e22020-09-18 20:09:31 +02002862 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002863 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002864 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002865
Radek Krejcif03a9e22020-09-18 20:09:31 +02002866 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002867 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002868 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002869
Radek Krejcif03a9e22020-09-18 20:09:31 +02002870 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002871 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002872 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002873
2874 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002875 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 +02002876 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 +02002877 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002878 goto error;
2879 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002880 LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002881 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002882 goto error;
2883 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002884 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002885
2886 /* NameTest '*' */
2887 tok_len = 1;
2888 tok_type = LYXP_TOKEN_NAMETEST;
2889
2890 } else {
2891
2892 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002893 long int ncname_len = parse_ncname(&expr_str[parsed]);
2894 LY_CHECK_ERR_GOTO(ncname_len < 0,
Radek Krejci2efc45b2020-12-22 16:25:44 +01002895 LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002896 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002897 tok_len = ncname_len;
2898
Radek Krejcif03a9e22020-09-18 20:09:31 +02002899 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002900 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002901 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002902 ++tok_len;
2903 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002904 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
2905 LY_CHECK_ERR_GOTO(ncname_len < 0,
Radek Krejci2efc45b2020-12-22 16:25:44 +01002906 LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002907 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002908 tok_len += ncname_len;
2909 }
2910 /* remove old flag to prevent ambiguities */
2911 prev_function_check = 0;
2912 tok_type = LYXP_TOKEN_NAMETEST;
2913 } else {
2914 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2915 prev_function_check = 1;
2916 tok_type = LYXP_TOKEN_NAMETEST;
2917 }
2918 }
2919
2920 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002921 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002922 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002923 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002924 ++parsed;
2925 }
2926
Radek Krejcif03a9e22020-09-18 20:09:31 +02002927 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002928
Michal Vasko004d3152020-06-11 19:59:22 +02002929 if (reparse) {
2930 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002931 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2932 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002933
Michal Vasko004d3152020-06-11 19:59:22 +02002934 /* fill repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002935 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error);
2936 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002937 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002938 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002939 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002940 goto error;
2941 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002942 }
2943
Radek Krejcif03a9e22020-09-18 20:09:31 +02002944 print_expr_struct_debug(expr);
2945 *expr_p = expr;
2946 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002947
2948error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002949 lyxp_expr_free(ctx, expr);
2950 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002951}
2952
Michal Vasko1734be92020-09-22 08:55:10 +02002953LY_ERR
2954lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02002955{
Michal Vasko1734be92020-09-22 08:55:10 +02002956 LY_ERR ret = LY_SUCCESS;
2957 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02002958 uint32_t i, j;
2959
Michal Vasko7f45cf22020-10-01 12:49:44 +02002960 if (!exp) {
2961 goto cleanup;
2962 }
2963
Michal Vasko004d3152020-06-11 19:59:22 +02002964 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02002965 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002966
2967 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
Michal Vasko1734be92020-09-22 08:55:10 +02002968 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002969 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2970
2971 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
Michal Vasko1734be92020-09-22 08:55:10 +02002972 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002973 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2974
2975 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
Michal Vasko1734be92020-09-22 08:55:10 +02002976 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002977 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2978
2979 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002980 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002981 for (i = 0; i < exp->used; ++i) {
2982 if (!exp->repeat[i]) {
2983 dup->repeat[i] = NULL;
2984 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02002985 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002986 /* the ending 0 as well */
2987 ++j;
2988
Michal Vasko99c71642020-07-03 13:33:36 +02002989 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002990 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002991 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
2992 dup->repeat[i][j - 1] = 0;
2993 }
2994 }
2995
2996 dup->used = exp->used;
2997 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02002998 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002999
Michal Vasko1734be92020-09-22 08:55:10 +02003000cleanup:
3001 if (ret) {
3002 lyxp_expr_free(ctx, dup);
3003 } else {
3004 *dup_p = dup;
3005 }
3006 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003007}
3008
Michal Vasko03ff5a72019-09-11 13:49:33 +02003009/*
3010 * warn functions
3011 *
3012 * Warn functions check specific reasonable conditions for schema XPath
3013 * and print a warning if they are not satisfied.
3014 */
3015
3016/**
3017 * @brief Get the last-added schema node that is currently in the context.
3018 *
3019 * @param[in] set Set to search in.
3020 * @return Last-added schema context node, NULL if no node is in context.
3021 */
3022static struct lysc_node *
3023warn_get_scnode_in_ctx(struct lyxp_set *set)
3024{
3025 uint32_t i;
3026
3027 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3028 return NULL;
3029 }
3030
3031 i = set->used;
3032 do {
3033 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003034 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003035 /* if there are more, simply return the first found (last added) */
3036 return set->val.scnodes[i].scnode;
3037 }
3038 } while (i);
3039
3040 return NULL;
3041}
3042
3043/**
3044 * @brief Test whether a type is numeric - integer type or decimal64.
3045 *
3046 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003047 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003048 */
Radek Krejci857189e2020-09-01 13:26:36 +02003049static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003050warn_is_numeric_type(struct lysc_type *type)
3051{
3052 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003053 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003054 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003055
3056 switch (type->basetype) {
3057 case LY_TYPE_DEC64:
3058 case LY_TYPE_INT8:
3059 case LY_TYPE_UINT8:
3060 case LY_TYPE_INT16:
3061 case LY_TYPE_UINT16:
3062 case LY_TYPE_INT32:
3063 case LY_TYPE_UINT32:
3064 case LY_TYPE_INT64:
3065 case LY_TYPE_UINT64:
3066 return 1;
3067 case LY_TYPE_UNION:
3068 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003069 LY_ARRAY_FOR(uni->types, u) {
3070 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003071 if (ret) {
3072 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003073 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003074 }
3075 }
3076 /* did not find any suitable type */
3077 return 0;
3078 case LY_TYPE_LEAFREF:
3079 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3080 default:
3081 return 0;
3082 }
3083}
3084
3085/**
3086 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3087 *
3088 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003089 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003090 */
Radek Krejci857189e2020-09-01 13:26:36 +02003091static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003092warn_is_string_type(struct lysc_type *type)
3093{
3094 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003095 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003096 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003097
3098 switch (type->basetype) {
3099 case LY_TYPE_BITS:
3100 case LY_TYPE_ENUM:
3101 case LY_TYPE_IDENT:
3102 case LY_TYPE_INST:
3103 case LY_TYPE_STRING:
3104 return 1;
3105 case LY_TYPE_UNION:
3106 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003107 LY_ARRAY_FOR(uni->types, u) {
3108 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003109 if (ret) {
3110 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003111 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003112 }
3113 }
3114 /* did not find any suitable type */
3115 return 0;
3116 case LY_TYPE_LEAFREF:
3117 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3118 default:
3119 return 0;
3120 }
3121}
3122
3123/**
3124 * @brief Test whether a type is one specific type.
3125 *
3126 * @param[in] type Type to test.
3127 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003128 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003129 */
Radek Krejci857189e2020-09-01 13:26:36 +02003130static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003131warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3132{
3133 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003134 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003135 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003136
3137 if (type->basetype == base) {
3138 return 1;
3139 } else if (type->basetype == LY_TYPE_UNION) {
3140 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003141 LY_ARRAY_FOR(uni->types, u) {
3142 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003143 if (ret) {
3144 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003145 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003146 }
3147 }
3148 /* did not find any suitable type */
3149 return 0;
3150 } else if (type->basetype == LY_TYPE_LEAFREF) {
3151 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3152 }
3153
3154 return 0;
3155}
3156
3157/**
3158 * @brief Get next type of a (union) type.
3159 *
3160 * @param[in] type Base type.
3161 * @param[in] prev_type Previously returned type.
3162 * @return Next type or NULL.
3163 */
3164static struct lysc_type *
3165warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3166{
3167 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003168 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003169 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003170
3171 switch (type->basetype) {
3172 case LY_TYPE_UNION:
3173 uni = (struct lysc_type_union *)type;
3174 if (!prev_type) {
3175 return uni->types[0];
3176 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003177 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003178 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003179 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003180 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003181 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003182 found = 1;
3183 }
3184 }
3185 return NULL;
3186 default:
3187 if (prev_type) {
3188 assert(type == prev_type);
3189 return NULL;
3190 } else {
3191 return type;
3192 }
3193 }
3194}
3195
3196/**
3197 * @brief Test whether 2 types have a common type.
3198 *
3199 * @param[in] type1 First type.
3200 * @param[in] type2 Second type.
3201 * @return 1 if they do, 0 otherwise.
3202 */
3203static int
3204warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3205{
3206 struct lysc_type *t1, *rt1, *t2, *rt2;
3207
3208 t1 = NULL;
3209 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3210 if (t1->basetype == LY_TYPE_LEAFREF) {
3211 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3212 } else {
3213 rt1 = t1;
3214 }
3215
3216 t2 = NULL;
3217 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3218 if (t2->basetype == LY_TYPE_LEAFREF) {
3219 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3220 } else {
3221 rt2 = t2;
3222 }
3223
3224 if (rt2->basetype == rt1->basetype) {
3225 /* match found */
3226 return 1;
3227 }
3228 }
3229 }
3230
3231 return 0;
3232}
3233
3234/**
3235 * @brief Check both operands of comparison operators.
3236 *
3237 * @param[in] ctx Context for errors.
3238 * @param[in] set1 First operand set.
3239 * @param[in] set2 Second operand set.
3240 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3241 * @param[in] expr Start of the expression to print with the warning.
3242 * @param[in] tok_pos Token position.
3243 */
3244static void
Radek Krejci857189e2020-09-01 13:26:36 +02003245warn_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 +02003246{
3247 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003248 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003249
3250 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3251 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3252
3253 if (!node1 && !node2) {
3254 /* no node-sets involved, nothing to do */
3255 return;
3256 }
3257
3258 if (node1) {
3259 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3260 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3261 warning = 1;
3262 leaves = 0;
3263 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3264 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3265 warning = 1;
3266 }
3267 }
3268
3269 if (node2) {
3270 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3271 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3272 warning = 1;
3273 leaves = 0;
3274 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3275 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3276 warning = 1;
3277 }
3278 }
3279
3280 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003281 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3282 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3283 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3284 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003285 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3286 warning = 1;
3287 }
3288 }
3289
3290 if (warning) {
3291 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3292 }
3293}
3294
3295/**
3296 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3297 *
3298 * @param[in] exp Parsed XPath expression.
3299 * @param[in] set Set with the leaf/leaf-list.
3300 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3301 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3302 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3303 */
3304static void
Michal Vasko40308e72020-10-20 16:38:40 +02003305warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3306 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003307{
3308 struct lysc_node *scnode;
3309 struct lysc_type *type;
3310 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003311 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003312 LY_ERR rc;
3313 struct ly_err_item *err = NULL;
3314
Michal Vasko69730152020-10-09 16:30:07 +02003315 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3316 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003317 /* check that the node can have the specified value */
3318 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3319 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3320 } else {
3321 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3322 }
3323 if (!value) {
3324 LOGMEM(set->ctx);
3325 return;
3326 }
3327
3328 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3329 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003330 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003331 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003332 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003333 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003334 }
3335
3336 type = ((struct lysc_node_leaf *)scnode)->type;
3337 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003338 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003339 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003340 if (rc == LY_EINCOMPLETE) {
3341 rc = LY_SUCCESS;
3342 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003343
3344 if (err) {
3345 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3346 ly_err_free(err);
3347 } else if (rc != LY_SUCCESS) {
3348 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3349 }
3350 if (rc != LY_SUCCESS) {
3351 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003352 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003353 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003354 } else {
3355 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003356 }
3357 }
3358 free(value);
3359 }
3360}
3361
3362/*
3363 * XPath functions
3364 */
3365
3366/**
3367 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3368 * depending on whether the first node bit value from the second argument is set.
3369 *
3370 * @param[in] args Array of arguments.
3371 * @param[in] arg_count Count of elements in @p args.
3372 * @param[in,out] set Context and result set at the same time.
3373 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003374 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003375 */
3376static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003377xpath_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 +02003378{
3379 struct lyd_node_term *leaf;
3380 struct lysc_node_leaf *sleaf;
3381 struct lysc_type_bits *bits;
3382 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003383 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003384
3385 if (options & LYXP_SCNODE_ALL) {
3386 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3387 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003388 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3389 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 +02003390 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3391 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003392 }
3393
3394 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3395 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3396 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 +02003397 } else if (!warn_is_string_type(sleaf->type)) {
3398 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003399 }
3400 }
3401 set_scnode_clear_ctx(set);
3402 return rc;
3403 }
3404
Michal Vaskod3678892020-05-21 10:06:58 +02003405 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003406 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 +02003407 return LY_EVALID;
3408 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003409 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003410 LY_CHECK_RET(rc);
3411
3412 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003413 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003414 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003415 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3416 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003417 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003418 LY_ARRAY_FOR(bits->bits, u) {
3419 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003420 set_fill_boolean(set, 1);
3421 break;
3422 }
3423 }
3424 }
3425 }
3426
3427 return LY_SUCCESS;
3428}
3429
3430/**
3431 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3432 * with the argument converted to boolean.
3433 *
3434 * @param[in] args Array of arguments.
3435 * @param[in] arg_count Count of elements in @p args.
3436 * @param[in,out] set Context and result set at the same time.
3437 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003438 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003439 */
3440static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003441xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003442{
3443 LY_ERR rc;
3444
3445 if (options & LYXP_SCNODE_ALL) {
3446 set_scnode_clear_ctx(set);
3447 return LY_SUCCESS;
3448 }
3449
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003450 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003451 LY_CHECK_RET(rc);
3452 set_fill_set(set, args[0]);
3453
3454 return LY_SUCCESS;
3455}
3456
3457/**
3458 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3459 * with the first argument rounded up to the nearest integer.
3460 *
3461 * @param[in] args Array of arguments.
3462 * @param[in] arg_count Count of elements in @p args.
3463 * @param[in,out] set Context and result set at the same time.
3464 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003465 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003466 */
3467static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003468xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003469{
3470 struct lysc_node_leaf *sleaf;
3471 LY_ERR rc = LY_SUCCESS;
3472
3473 if (options & LYXP_SCNODE_ALL) {
3474 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3475 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003476 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3477 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 +02003478 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3479 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003480 }
3481 set_scnode_clear_ctx(set);
3482 return rc;
3483 }
3484
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003485 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003486 LY_CHECK_RET(rc);
3487 if ((long long)args[0]->val.num != args[0]->val.num) {
3488 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3489 } else {
3490 set_fill_number(set, args[0]->val.num);
3491 }
3492
3493 return LY_SUCCESS;
3494}
3495
3496/**
3497 * @brief Execute the XPath concat(string, string, string*) function.
3498 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3499 *
3500 * @param[in] args Array of arguments.
3501 * @param[in] arg_count Count of elements in @p args.
3502 * @param[in,out] set Context and result set at the same time.
3503 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003504 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003505 */
3506static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003507xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003508{
3509 uint16_t i;
3510 char *str = NULL;
3511 size_t used = 1;
3512 LY_ERR rc = LY_SUCCESS;
3513 struct lysc_node_leaf *sleaf;
3514
3515 if (options & LYXP_SCNODE_ALL) {
3516 for (i = 0; i < arg_count; ++i) {
3517 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3518 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3519 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003520 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003521 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003522 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 +02003523 }
3524 }
3525 }
3526 set_scnode_clear_ctx(set);
3527 return rc;
3528 }
3529
3530 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003531 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003532 if (rc != LY_SUCCESS) {
3533 free(str);
3534 return rc;
3535 }
3536
3537 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3538 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3539 strcpy(str + used - 1, args[i]->val.str);
3540 used += strlen(args[i]->val.str);
3541 }
3542
3543 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003544 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003545 set->type = LYXP_SET_STRING;
3546 set->val.str = str;
3547
3548 return LY_SUCCESS;
3549}
3550
3551/**
3552 * @brief Execute the XPath contains(string, string) function.
3553 * Returns LYXP_SET_BOOLEAN whether the second argument can
3554 * be found in the first or not.
3555 *
3556 * @param[in] args Array of arguments.
3557 * @param[in] arg_count Count of elements in @p args.
3558 * @param[in,out] set Context and result set at the same time.
3559 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003560 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003561 */
3562static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003563xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003564{
3565 struct lysc_node_leaf *sleaf;
3566 LY_ERR rc = LY_SUCCESS;
3567
3568 if (options & LYXP_SCNODE_ALL) {
3569 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3570 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3571 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 +02003572 } else if (!warn_is_string_type(sleaf->type)) {
3573 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003574 }
3575 }
3576
3577 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3578 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3579 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 +02003580 } else if (!warn_is_string_type(sleaf->type)) {
3581 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003582 }
3583 }
3584 set_scnode_clear_ctx(set);
3585 return rc;
3586 }
3587
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003588 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003589 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003590 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003591 LY_CHECK_RET(rc);
3592
3593 if (strstr(args[0]->val.str, args[1]->val.str)) {
3594 set_fill_boolean(set, 1);
3595 } else {
3596 set_fill_boolean(set, 0);
3597 }
3598
3599 return LY_SUCCESS;
3600}
3601
3602/**
3603 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3604 * with the size of the node-set from the argument.
3605 *
3606 * @param[in] args Array of arguments.
3607 * @param[in] arg_count Count of elements in @p args.
3608 * @param[in,out] set Context and result set at the same time.
3609 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003610 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003611 */
3612static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003613xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003614{
3615 struct lysc_node *scnode = NULL;
3616 LY_ERR rc = LY_SUCCESS;
3617
3618 if (options & LYXP_SCNODE_ALL) {
3619 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3620 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003621 }
3622 set_scnode_clear_ctx(set);
3623 return rc;
3624 }
3625
Michal Vasko03ff5a72019-09-11 13:49:33 +02003626 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003627 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003628 return LY_EVALID;
3629 }
3630
3631 set_fill_number(set, args[0]->used);
3632 return LY_SUCCESS;
3633}
3634
3635/**
3636 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3637 * with the context with the intial node.
3638 *
3639 * @param[in] args Array of arguments.
3640 * @param[in] arg_count Count of elements in @p args.
3641 * @param[in,out] set Context and result set at the same time.
3642 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003643 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003644 */
3645static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003646xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003647{
3648 if (arg_count || args) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003649 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003650 return LY_EVALID;
3651 }
3652
3653 if (options & LYXP_SCNODE_ALL) {
3654 set_scnode_clear_ctx(set);
3655
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003656 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003657 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003658 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003659
3660 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003661 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003662 }
3663
3664 return LY_SUCCESS;
3665}
3666
3667/**
3668 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3669 * leafref or instance-identifier target node(s).
3670 *
3671 * @param[in] args Array of arguments.
3672 * @param[in] arg_count Count of elements in @p args.
3673 * @param[in,out] set Context and result set at the same time.
3674 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003675 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003676 */
3677static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003678xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003679{
3680 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003681 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003682 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003683 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003684 struct ly_path *p;
3685 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003686 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003687 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003688 LY_ERR rc = LY_SUCCESS;
3689
3690 if (options & LYXP_SCNODE_ALL) {
3691 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3692 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003693 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3694 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 +02003695 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3696 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
Michal Vasko69730152020-10-09 16:30:07 +02003697 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003698 }
3699 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003700 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003701 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003702 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003703
3704 /* it was already evaluated on schema, it must succeed */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003705 rc = ly_path_compile(set->ctx, lref->cur_mod, &sleaf->node, lref->path, LY_PATH_LREF_TRUE, oper,
3706 LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, NULL, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003707 assert(!rc);
3708
3709 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003710 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003711 ly_path_free(set->ctx, p);
3712
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003713 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003714 }
3715
Michal Vasko03ff5a72019-09-11 13:49:33 +02003716 return rc;
3717 }
3718
Michal Vaskod3678892020-05-21 10:06:58 +02003719 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003720 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003721 return LY_EVALID;
3722 }
3723
Michal Vaskod3678892020-05-21 10:06:58 +02003724 lyxp_set_free_content(set);
3725 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003726 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3727 sleaf = (struct lysc_node_leaf *)leaf->schema;
3728 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3729 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3730 /* find leafref target */
Michal Vasko9e685082021-01-29 14:49:09 +01003731 if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
3732 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003733 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003734 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003735 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003736 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003737 } else {
3738 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003739 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003740 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Michal Vasko69730152020-10-09 16:30:07 +02003741 LYD_CANON_VALUE(leaf));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003742 return LY_EVALID;
3743 }
3744 }
Michal Vasko004d3152020-06-11 19:59:22 +02003745
3746 /* insert it */
3747 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003748 }
3749 }
3750
3751 return LY_SUCCESS;
3752}
3753
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003754static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003755xpath_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 +02003756{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01003757 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003758 LY_ARRAY_COUNT_TYPE u;
3759 struct lyd_node_term *leaf;
3760 struct lysc_node_leaf *sleaf;
3761 struct lyd_meta *meta;
3762 struct lyd_value data = {0}, *val;
3763 struct ly_err_item *err = NULL;
3764 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003765 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003766
3767 if (options & LYXP_SCNODE_ALL) {
3768 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3769 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
3770 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3771 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003772 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003773 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3774 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3775 }
3776
3777 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3778 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3779 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003780 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003781 } else if (!warn_is_string_type(sleaf->type)) {
3782 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3783 }
3784 }
3785 set_scnode_clear_ctx(set);
3786 return rc;
3787 }
3788
3789 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003790 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 +02003791 return LY_EVALID;
3792 }
3793 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3794 LY_CHECK_RET(rc);
3795
3796 set_fill_boolean(set, 0);
3797 found = 0;
3798 for (i = 0; i < args[0]->used; ++i) {
3799 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3800 continue;
3801 }
3802
3803 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3804 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3805 sleaf = (struct lysc_node_leaf *)leaf->schema;
3806 val = &leaf->value;
3807 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3808 /* uninteresting */
3809 continue;
3810 }
3811
3812 /* store args[1] as ident */
3813 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str),
Michal Vasko405cc9e2020-12-01 12:01:27 +01003814 0, set->format, set->prefix_data, LYD_HINT_DATA, leaf->schema, &data, NULL, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003815 } else {
3816 meta = args[0]->val.meta[i].meta;
3817 val = &meta->value;
3818 if (val->realtype->basetype != LY_TYPE_IDENT) {
3819 /* uninteresting */
3820 continue;
3821 }
3822
3823 /* store args[1] as ident */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003824 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), 0,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003825 set->format, set->prefix_data, LYD_HINT_DATA, meta->parent->schema, &data, NULL, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003826 }
3827
3828 if (err) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01003829 ly_err_print(set->ctx, err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003830 ly_err_free(err);
3831 }
3832 LY_CHECK_RET(rc);
3833
3834 /* finally check the identity itself */
3835 if (self_match && (data.ident == val->ident)) {
3836 set_fill_boolean(set, 1);
3837 found = 1;
3838 }
3839 if (!found) {
3840 LY_ARRAY_FOR(data.ident->derived, u) {
3841 if (data.ident->derived[u] == val->ident) {
3842 set_fill_boolean(set, 1);
3843 found = 1;
3844 break;
3845 }
3846 }
3847 }
3848
3849 /* free temporary value */
3850 val->realtype->plugin->free(set->ctx, &data);
3851 if (found) {
3852 break;
3853 }
3854 }
3855
3856 return LY_SUCCESS;
3857}
3858
Michal Vasko03ff5a72019-09-11 13:49:33 +02003859/**
3860 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3861 * on whether the first argument nodes contain a node of an identity derived from the second
3862 * argument identity.
3863 *
3864 * @param[in] args Array of arguments.
3865 * @param[in] arg_count Count of elements in @p args.
3866 * @param[in,out] set Context and result set at the same time.
3867 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003868 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003869 */
3870static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003871xpath_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 +02003872{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003873 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003874}
3875
3876/**
3877 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3878 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3879 * the second argument identity.
3880 *
3881 * @param[in] args Array of arguments.
3882 * @param[in] arg_count Count of elements in @p args.
3883 * @param[in,out] set Context and result set at the same time.
3884 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003885 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003886 */
3887static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003888xpath_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 +02003889{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003890 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003891}
3892
3893/**
3894 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3895 * with the integer value of the first node's enum value, otherwise NaN.
3896 *
3897 * @param[in] args Array of arguments.
3898 * @param[in] arg_count Count of elements in @p args.
3899 * @param[in,out] set Context and result set at the same time.
3900 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003901 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003902 */
3903static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003904xpath_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 +02003905{
3906 struct lyd_node_term *leaf;
3907 struct lysc_node_leaf *sleaf;
3908 LY_ERR rc = LY_SUCCESS;
3909
3910 if (options & LYXP_SCNODE_ALL) {
3911 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3912 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003913 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3914 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 +02003915 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3916 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003917 }
3918 set_scnode_clear_ctx(set);
3919 return rc;
3920 }
3921
Michal Vaskod3678892020-05-21 10:06:58 +02003922 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003923 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003924 return LY_EVALID;
3925 }
3926
3927 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003928 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003929 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3930 sleaf = (struct lysc_node_leaf *)leaf->schema;
3931 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3932 set_fill_number(set, leaf->value.enum_item->value);
3933 }
3934 }
3935
3936 return LY_SUCCESS;
3937}
3938
3939/**
3940 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3941 * with false value.
3942 *
3943 * @param[in] args Array of arguments.
3944 * @param[in] arg_count Count of elements in @p args.
3945 * @param[in,out] set Context and result set at the same time.
3946 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003947 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003948 */
3949static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003950xpath_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 +02003951{
3952 if (options & LYXP_SCNODE_ALL) {
3953 set_scnode_clear_ctx(set);
3954 return LY_SUCCESS;
3955 }
3956
3957 set_fill_boolean(set, 0);
3958 return LY_SUCCESS;
3959}
3960
3961/**
3962 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3963 * with the first argument floored (truncated).
3964 *
3965 * @param[in] args Array of arguments.
3966 * @param[in] arg_count Count of elements in @p args.
3967 * @param[in,out] set Context and result set at the same time.
3968 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003969 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003970 */
3971static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003972xpath_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 +02003973{
3974 LY_ERR rc;
3975
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003976 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003977 LY_CHECK_RET(rc);
3978 if (isfinite(args[0]->val.num)) {
3979 set_fill_number(set, (long long)args[0]->val.num);
3980 }
3981
3982 return LY_SUCCESS;
3983}
3984
3985/**
3986 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3987 * whether the language of the text matches the one from the argument.
3988 *
3989 * @param[in] args Array of arguments.
3990 * @param[in] arg_count Count of elements in @p args.
3991 * @param[in,out] set Context and result set at the same time.
3992 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003993 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003994 */
3995static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003996xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003997{
3998 const struct lyd_node *node;
3999 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004000 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004001 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004002 LY_ERR rc = LY_SUCCESS;
4003
4004 if (options & LYXP_SCNODE_ALL) {
4005 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4006 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4007 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 +02004008 } else if (!warn_is_string_type(sleaf->type)) {
4009 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004010 }
4011 }
4012 set_scnode_clear_ctx(set);
4013 return rc;
4014 }
4015
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004016 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004017 LY_CHECK_RET(rc);
4018
Michal Vasko03ff5a72019-09-11 13:49:33 +02004019 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004020 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004021 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004022 } else if (!set->used) {
4023 set_fill_boolean(set, 0);
4024 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004025 }
4026
4027 switch (set->val.nodes[0].type) {
4028 case LYXP_NODE_ELEM:
4029 case LYXP_NODE_TEXT:
4030 node = set->val.nodes[0].node;
4031 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004032 case LYXP_NODE_META:
4033 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004034 break;
4035 default:
4036 /* nothing to do with roots */
4037 set_fill_boolean(set, 0);
4038 return LY_SUCCESS;
4039 }
4040
Michal Vasko9f96a052020-03-10 09:41:45 +01004041 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004042 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004043 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004044 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004045 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004046 break;
4047 }
4048 }
4049
Michal Vasko9f96a052020-03-10 09:41:45 +01004050 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004051 break;
4052 }
4053 }
4054
4055 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004056 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004057 set_fill_boolean(set, 0);
4058 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004059 uint64_t i;
4060
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004061 val = meta->value.canonical;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004062 for (i = 0; args[0]->val.str[i]; ++i) {
4063 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4064 set_fill_boolean(set, 0);
4065 break;
4066 }
4067 }
4068 if (!args[0]->val.str[i]) {
4069 if (!val[i] || (val[i] == '-')) {
4070 set_fill_boolean(set, 1);
4071 } else {
4072 set_fill_boolean(set, 0);
4073 }
4074 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004075 }
4076
4077 return LY_SUCCESS;
4078}
4079
4080/**
4081 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4082 * with the context size.
4083 *
4084 * @param[in] args Array of arguments.
4085 * @param[in] arg_count Count of elements in @p args.
4086 * @param[in,out] set Context and result set at the same time.
4087 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004088 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004089 */
4090static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004091xpath_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 +02004092{
4093 if (options & LYXP_SCNODE_ALL) {
4094 set_scnode_clear_ctx(set);
4095 return LY_SUCCESS;
4096 }
4097
Michal Vasko03ff5a72019-09-11 13:49:33 +02004098 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004099 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004100 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004101 } else if (!set->used) {
4102 set_fill_number(set, 0);
4103 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004104 }
4105
4106 set_fill_number(set, set->ctx_size);
4107 return LY_SUCCESS;
4108}
4109
4110/**
4111 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4112 * with the node name without namespace from the argument or the context.
4113 *
4114 * @param[in] args Array of arguments.
4115 * @param[in] arg_count Count of elements in @p args.
4116 * @param[in,out] set Context and result set at the same time.
4117 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004118 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004119 */
4120static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004121xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004122{
4123 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004124
Michal Vasko03ff5a72019-09-11 13:49:33 +02004125 /* suppress unused variable warning */
4126 (void)options;
4127
4128 if (options & LYXP_SCNODE_ALL) {
4129 set_scnode_clear_ctx(set);
4130 return LY_SUCCESS;
4131 }
4132
4133 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004134 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004135 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004136 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004137 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004138 } else if (!args[0]->used) {
4139 set_fill_string(set, "", 0);
4140 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004141 }
4142
4143 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004144 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004145
4146 item = &args[0]->val.nodes[0];
4147 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004148 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004149 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004150 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004151 } else if (!set->used) {
4152 set_fill_string(set, "", 0);
4153 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004154 }
4155
4156 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004157 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004158
4159 item = &set->val.nodes[0];
4160 }
4161
4162 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004163 case LYXP_NODE_NONE:
4164 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004165 case LYXP_NODE_ROOT:
4166 case LYXP_NODE_ROOT_CONFIG:
4167 case LYXP_NODE_TEXT:
4168 set_fill_string(set, "", 0);
4169 break;
4170 case LYXP_NODE_ELEM:
4171 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4172 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004173 case LYXP_NODE_META:
4174 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004175 break;
4176 }
4177
4178 return LY_SUCCESS;
4179}
4180
4181/**
4182 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4183 * with the node name fully qualified (with namespace) from the argument or the context.
4184 *
4185 * @param[in] args Array of arguments.
4186 * @param[in] arg_count Count of elements in @p args.
4187 * @param[in,out] set Context and result set at the same time.
4188 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004189 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004190 */
4191static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004192xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004193{
4194 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004195 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004196 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004197 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004198
4199 if (options & LYXP_SCNODE_ALL) {
4200 set_scnode_clear_ctx(set);
4201 return LY_SUCCESS;
4202 }
4203
4204 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004205 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004206 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004207 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004208 } else if (!args[0]->used) {
4209 set_fill_string(set, "", 0);
4210 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004211 }
4212
4213 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004214 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004215
4216 item = &args[0]->val.nodes[0];
4217 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004218 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004219 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004220 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004221 } else if (!set->used) {
4222 set_fill_string(set, "", 0);
4223 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004224 }
4225
4226 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004227 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004228
4229 item = &set->val.nodes[0];
4230 }
4231
4232 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004233 case LYXP_NODE_NONE:
4234 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004235 case LYXP_NODE_ROOT:
4236 case LYXP_NODE_ROOT_CONFIG:
4237 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004238 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004239 break;
4240 case LYXP_NODE_ELEM:
4241 mod = item->node->schema->module;
4242 name = item->node->schema->name;
4243 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004244 case LYXP_NODE_META:
4245 mod = ((struct lyd_meta *)item->node)->annotation->module;
4246 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004247 break;
4248 }
4249
4250 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004251 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004252 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4253 set_fill_string(set, str, strlen(str));
4254 free(str);
4255 } else {
4256 set_fill_string(set, "", 0);
4257 }
4258
4259 return LY_SUCCESS;
4260}
4261
4262/**
4263 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4264 * with the namespace of the node from the argument or the context.
4265 *
4266 * @param[in] args Array of arguments.
4267 * @param[in] arg_count Count of elements in @p args.
4268 * @param[in,out] set Context and result set at the same time.
4269 * @param[in] options XPath options.
4270 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4271 */
4272static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004273xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004274{
4275 struct lyxp_set_node *item;
4276 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004277
Michal Vasko03ff5a72019-09-11 13:49:33 +02004278 /* suppress unused variable warning */
4279 (void)options;
4280
4281 if (options & LYXP_SCNODE_ALL) {
4282 set_scnode_clear_ctx(set);
4283 return LY_SUCCESS;
4284 }
4285
4286 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004287 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004288 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004289 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004290 return LY_EVALID;
4291 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004292 set_fill_string(set, "", 0);
4293 return LY_SUCCESS;
4294 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004295
4296 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004297 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004298
4299 item = &args[0]->val.nodes[0];
4300 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004301 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004302 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004303 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004304 } else if (!set->used) {
4305 set_fill_string(set, "", 0);
4306 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004307 }
4308
4309 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004310 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004311
4312 item = &set->val.nodes[0];
4313 }
4314
4315 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004316 case LYXP_NODE_NONE:
4317 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004318 case LYXP_NODE_ROOT:
4319 case LYXP_NODE_ROOT_CONFIG:
4320 case LYXP_NODE_TEXT:
4321 set_fill_string(set, "", 0);
4322 break;
4323 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004324 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004325 if (item->type == LYXP_NODE_ELEM) {
4326 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004327 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004328 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004329 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004330 }
4331
4332 set_fill_string(set, mod->ns, strlen(mod->ns));
4333 break;
4334 }
4335
4336 return LY_SUCCESS;
4337}
4338
4339/**
4340 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4341 * with only nodes from the context. In practice it either leaves the context
4342 * as it is or returns an empty node set.
4343 *
4344 * @param[in] args Array of arguments.
4345 * @param[in] arg_count Count of elements in @p args.
4346 * @param[in,out] set Context and result set at the same time.
4347 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004348 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004349 */
4350static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004351xpath_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 +02004352{
4353 if (options & LYXP_SCNODE_ALL) {
4354 set_scnode_clear_ctx(set);
4355 return LY_SUCCESS;
4356 }
4357
4358 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004359 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004360 }
4361 return LY_SUCCESS;
4362}
4363
4364/**
4365 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4366 * with normalized value (no leading, trailing, double white spaces) of the node
4367 * from the argument or the context.
4368 *
4369 * @param[in] args Array of arguments.
4370 * @param[in] arg_count Count of elements in @p args.
4371 * @param[in,out] set Context and result set at the same time.
4372 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004373 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004374 */
4375static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004376xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004377{
4378 uint16_t i, new_used;
4379 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004380 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004381 struct lysc_node_leaf *sleaf;
4382 LY_ERR rc = LY_SUCCESS;
4383
4384 if (options & LYXP_SCNODE_ALL) {
4385 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4386 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4387 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 +02004388 } else if (!warn_is_string_type(sleaf->type)) {
4389 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004390 }
4391 }
4392 set_scnode_clear_ctx(set);
4393 return rc;
4394 }
4395
4396 if (arg_count) {
4397 set_fill_set(set, args[0]);
4398 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004399 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004400 LY_CHECK_RET(rc);
4401
4402 /* is there any normalization necessary? */
4403 for (i = 0; set->val.str[i]; ++i) {
4404 if (is_xmlws(set->val.str[i])) {
4405 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4406 have_spaces = 1;
4407 break;
4408 }
4409 space_before = 1;
4410 } else {
4411 space_before = 0;
4412 }
4413 }
4414
4415 /* yep, there is */
4416 if (have_spaces) {
4417 /* it's enough, at least one character will go, makes space for ending '\0' */
4418 new = malloc(strlen(set->val.str) * sizeof(char));
4419 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4420 new_used = 0;
4421
4422 space_before = 0;
4423 for (i = 0; set->val.str[i]; ++i) {
4424 if (is_xmlws(set->val.str[i])) {
4425 if ((i == 0) || space_before) {
4426 space_before = 1;
4427 continue;
4428 } else {
4429 space_before = 1;
4430 }
4431 } else {
4432 space_before = 0;
4433 }
4434
4435 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4436 ++new_used;
4437 }
4438
4439 /* at worst there is one trailing space now */
4440 if (new_used && is_xmlws(new[new_used - 1])) {
4441 --new_used;
4442 }
4443
4444 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4445 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4446 new[new_used] = '\0';
4447
4448 free(set->val.str);
4449 set->val.str = new;
4450 }
4451
4452 return LY_SUCCESS;
4453}
4454
4455/**
4456 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4457 * with the argument converted to boolean and logically inverted.
4458 *
4459 * @param[in] args Array of arguments.
4460 * @param[in] arg_count Count of elements in @p args.
4461 * @param[in,out] set Context and result set at the same time.
4462 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004463 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004464 */
4465static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004466xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004467{
4468 if (options & LYXP_SCNODE_ALL) {
4469 set_scnode_clear_ctx(set);
4470 return LY_SUCCESS;
4471 }
4472
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004473 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004474 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004475 set_fill_boolean(set, 0);
4476 } else {
4477 set_fill_boolean(set, 1);
4478 }
4479
4480 return LY_SUCCESS;
4481}
4482
4483/**
4484 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4485 * with the number representation of either the argument or the context.
4486 *
4487 * @param[in] args Array of arguments.
4488 * @param[in] arg_count Count of elements in @p args.
4489 * @param[in,out] set Context and result set at the same time.
4490 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004491 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004492 */
4493static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004494xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004495{
4496 LY_ERR rc;
4497
4498 if (options & LYXP_SCNODE_ALL) {
4499 set_scnode_clear_ctx(set);
4500 return LY_SUCCESS;
4501 }
4502
4503 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004504 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004505 LY_CHECK_RET(rc);
4506 set_fill_set(set, args[0]);
4507 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004508 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004509 LY_CHECK_RET(rc);
4510 }
4511
4512 return LY_SUCCESS;
4513}
4514
4515/**
4516 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4517 * with the context position.
4518 *
4519 * @param[in] args Array of arguments.
4520 * @param[in] arg_count Count of elements in @p args.
4521 * @param[in,out] set Context and result set at the same time.
4522 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004523 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004524 */
4525static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004526xpath_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 +02004527{
4528 if (options & LYXP_SCNODE_ALL) {
4529 set_scnode_clear_ctx(set);
4530 return LY_SUCCESS;
4531 }
4532
Michal Vasko03ff5a72019-09-11 13:49:33 +02004533 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004534 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004535 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004536 } else if (!set->used) {
4537 set_fill_number(set, 0);
4538 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004539 }
4540
4541 set_fill_number(set, set->ctx_pos);
4542
4543 /* UNUSED in 'Release' build type */
4544 (void)options;
4545 return LY_SUCCESS;
4546}
4547
4548/**
4549 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4550 * depending on whether the second argument regex matches the first argument string. For details refer to
4551 * YANG 1.1 RFC section 10.2.1.
4552 *
4553 * @param[in] args Array of arguments.
4554 * @param[in] arg_count Count of elements in @p args.
4555 * @param[in,out] set Context and result set at the same time.
4556 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004557 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004558 */
4559static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004560xpath_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 +02004561{
4562 struct lysc_pattern **patterns = NULL, **pattern;
4563 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004564 LY_ERR rc = LY_SUCCESS;
4565 struct ly_err_item *err;
4566
4567 if (options & LYXP_SCNODE_ALL) {
4568 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4569 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4570 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 +02004571 } else if (!warn_is_string_type(sleaf->type)) {
4572 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004573 }
4574 }
4575
4576 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4577 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4578 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 +02004579 } else if (!warn_is_string_type(sleaf->type)) {
4580 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004581 }
4582 }
4583 set_scnode_clear_ctx(set);
4584 return rc;
4585 }
4586
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004587 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004588 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004589 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004590 LY_CHECK_RET(rc);
4591
4592 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4593 *pattern = malloc(sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004594 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004595 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Radek Krejciddace2c2021-01-08 11:30:56 +01004596 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004597 if (rc != LY_SUCCESS) {
4598 LY_ARRAY_FREE(patterns);
4599 return rc;
4600 }
4601
4602 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4603 pcre2_code_free((*pattern)->code);
4604 free(*pattern);
4605 LY_ARRAY_FREE(patterns);
4606 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004607 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004608 ly_err_free(err);
4609 return rc;
4610 }
4611
4612 if (rc == LY_EVALID) {
4613 ly_err_free(err);
4614 set_fill_boolean(set, 0);
4615 } else {
4616 set_fill_boolean(set, 1);
4617 }
4618
4619 return LY_SUCCESS;
4620}
4621
4622/**
4623 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4624 * with the rounded first argument. For details refer to
4625 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4626 *
4627 * @param[in] args Array of arguments.
4628 * @param[in] arg_count Count of elements in @p args.
4629 * @param[in,out] set Context and result set at the same time.
4630 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004631 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004632 */
4633static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004634xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004635{
4636 struct lysc_node_leaf *sleaf;
4637 LY_ERR rc = LY_SUCCESS;
4638
4639 if (options & LYXP_SCNODE_ALL) {
4640 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4641 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004642 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4643 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 +02004644 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4645 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004646 }
4647 set_scnode_clear_ctx(set);
4648 return rc;
4649 }
4650
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004651 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004652 LY_CHECK_RET(rc);
4653
4654 /* cover only the cases where floor can't be used */
4655 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4656 set_fill_number(set, -0.0f);
4657 } else {
4658 args[0]->val.num += 0.5;
4659 rc = xpath_floor(args, 1, args[0], options);
4660 LY_CHECK_RET(rc);
4661 set_fill_number(set, args[0]->val.num);
4662 }
4663
4664 return LY_SUCCESS;
4665}
4666
4667/**
4668 * @brief Execute the XPath starts-with(string, string) function.
4669 * Returns LYXP_SET_BOOLEAN whether the second argument is
4670 * the prefix of the first or not.
4671 *
4672 * @param[in] args Array of arguments.
4673 * @param[in] arg_count Count of elements in @p args.
4674 * @param[in,out] set Context and result set at the same time.
4675 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004676 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004677 */
4678static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004679xpath_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 +02004680{
4681 struct lysc_node_leaf *sleaf;
4682 LY_ERR rc = LY_SUCCESS;
4683
4684 if (options & LYXP_SCNODE_ALL) {
4685 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4686 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4687 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 +02004688 } else if (!warn_is_string_type(sleaf->type)) {
4689 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004690 }
4691 }
4692
4693 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4694 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4695 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 +02004696 } else if (!warn_is_string_type(sleaf->type)) {
4697 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004698 }
4699 }
4700 set_scnode_clear_ctx(set);
4701 return rc;
4702 }
4703
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004704 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004705 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004706 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004707 LY_CHECK_RET(rc);
4708
4709 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4710 set_fill_boolean(set, 0);
4711 } else {
4712 set_fill_boolean(set, 1);
4713 }
4714
4715 return LY_SUCCESS;
4716}
4717
4718/**
4719 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4720 * with the string representation of either the argument or the context.
4721 *
4722 * @param[in] args Array of arguments.
4723 * @param[in] arg_count Count of elements in @p args.
4724 * @param[in,out] set Context and result set at the same time.
4725 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004726 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004727 */
4728static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004729xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004730{
4731 LY_ERR rc;
4732
4733 if (options & LYXP_SCNODE_ALL) {
4734 set_scnode_clear_ctx(set);
4735 return LY_SUCCESS;
4736 }
4737
4738 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004739 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004740 LY_CHECK_RET(rc);
4741 set_fill_set(set, args[0]);
4742 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004743 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004744 LY_CHECK_RET(rc);
4745 }
4746
4747 return LY_SUCCESS;
4748}
4749
4750/**
4751 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4752 * with the length of the string in either the argument or the context.
4753 *
4754 * @param[in] args Array of arguments.
4755 * @param[in] arg_count Count of elements in @p args.
4756 * @param[in,out] set Context and result set at the same time.
4757 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004758 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004759 */
4760static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004761xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004762{
4763 struct lysc_node_leaf *sleaf;
4764 LY_ERR rc = LY_SUCCESS;
4765
4766 if (options & LYXP_SCNODE_ALL) {
4767 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4768 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4769 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004770 } else if (!warn_is_string_type(sleaf->type)) {
4771 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004772 }
4773 }
4774 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4775 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4776 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 +02004777 } else if (!warn_is_string_type(sleaf->type)) {
4778 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004779 }
4780 }
4781 set_scnode_clear_ctx(set);
4782 return rc;
4783 }
4784
4785 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004786 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004787 LY_CHECK_RET(rc);
4788 set_fill_number(set, strlen(args[0]->val.str));
4789 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004790 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004791 LY_CHECK_RET(rc);
4792 set_fill_number(set, strlen(set->val.str));
4793 }
4794
4795 return LY_SUCCESS;
4796}
4797
4798/**
4799 * @brief Execute the XPath substring(string, number, number?) function.
4800 * Returns LYXP_SET_STRING substring of the first argument starting
4801 * on the second argument index ending on the third argument index,
4802 * indexed from 1. For exact definition refer to
4803 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4804 *
4805 * @param[in] args Array of arguments.
4806 * @param[in] arg_count Count of elements in @p args.
4807 * @param[in,out] set Context and result set at the same time.
4808 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004809 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004810 */
4811static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004812xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004813{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004814 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004815 uint16_t str_start, str_len, pos;
4816 struct lysc_node_leaf *sleaf;
4817 LY_ERR rc = LY_SUCCESS;
4818
4819 if (options & LYXP_SCNODE_ALL) {
4820 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4821 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4822 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 +02004823 } else if (!warn_is_string_type(sleaf->type)) {
4824 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004825 }
4826 }
4827
4828 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4829 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4830 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 +02004831 } else if (!warn_is_numeric_type(sleaf->type)) {
4832 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004833 }
4834 }
4835
Michal Vasko69730152020-10-09 16:30:07 +02004836 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4837 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004838 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4839 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 +02004840 } else if (!warn_is_numeric_type(sleaf->type)) {
4841 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004842 }
4843 }
4844 set_scnode_clear_ctx(set);
4845 return rc;
4846 }
4847
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004848 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004849 LY_CHECK_RET(rc);
4850
4851 /* start */
4852 if (xpath_round(&args[1], 1, args[1], options)) {
4853 return -1;
4854 }
4855 if (isfinite(args[1]->val.num)) {
4856 start = args[1]->val.num - 1;
4857 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004858 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004859 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004860 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004861 }
4862
4863 /* len */
4864 if (arg_count == 3) {
4865 rc = xpath_round(&args[2], 1, args[2], options);
4866 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004867 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004868 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004869 } else if (isfinite(args[2]->val.num)) {
4870 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004871 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004872 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004873 }
4874 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004875 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004876 }
4877
4878 /* find matching character positions */
4879 str_start = 0;
4880 str_len = 0;
4881 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4882 if (pos < start) {
4883 ++str_start;
4884 } else if (pos < start + len) {
4885 ++str_len;
4886 } else {
4887 break;
4888 }
4889 }
4890
4891 set_fill_string(set, args[0]->val.str + str_start, str_len);
4892 return LY_SUCCESS;
4893}
4894
4895/**
4896 * @brief Execute the XPath substring-after(string, string) function.
4897 * Returns LYXP_SET_STRING with the string succeeding the occurance
4898 * of the second argument in the first or an empty string.
4899 *
4900 * @param[in] args Array of arguments.
4901 * @param[in] arg_count Count of elements in @p args.
4902 * @param[in,out] set Context and result set at the same time.
4903 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004904 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004905 */
4906static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004907xpath_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 +02004908{
4909 char *ptr;
4910 struct lysc_node_leaf *sleaf;
4911 LY_ERR rc = LY_SUCCESS;
4912
4913 if (options & LYXP_SCNODE_ALL) {
4914 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4915 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4916 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 +02004917 } else if (!warn_is_string_type(sleaf->type)) {
4918 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004919 }
4920 }
4921
4922 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4923 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4924 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 +02004925 } else if (!warn_is_string_type(sleaf->type)) {
4926 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004927 }
4928 }
4929 set_scnode_clear_ctx(set);
4930 return rc;
4931 }
4932
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004933 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004934 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004935 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004936 LY_CHECK_RET(rc);
4937
4938 ptr = strstr(args[0]->val.str, args[1]->val.str);
4939 if (ptr) {
4940 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4941 } else {
4942 set_fill_string(set, "", 0);
4943 }
4944
4945 return LY_SUCCESS;
4946}
4947
4948/**
4949 * @brief Execute the XPath substring-before(string, string) function.
4950 * Returns LYXP_SET_STRING with the string preceding the occurance
4951 * of the second argument in the first or an empty string.
4952 *
4953 * @param[in] args Array of arguments.
4954 * @param[in] arg_count Count of elements in @p args.
4955 * @param[in,out] set Context and result set at the same time.
4956 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004957 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004958 */
4959static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004960xpath_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 +02004961{
4962 char *ptr;
4963 struct lysc_node_leaf *sleaf;
4964 LY_ERR rc = LY_SUCCESS;
4965
4966 if (options & LYXP_SCNODE_ALL) {
4967 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4968 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4969 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 +02004970 } else if (!warn_is_string_type(sleaf->type)) {
4971 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004972 }
4973 }
4974
4975 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4976 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4977 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 +02004978 } else if (!warn_is_string_type(sleaf->type)) {
4979 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004980 }
4981 }
4982 set_scnode_clear_ctx(set);
4983 return rc;
4984 }
4985
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004986 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004987 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004988 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004989 LY_CHECK_RET(rc);
4990
4991 ptr = strstr(args[0]->val.str, args[1]->val.str);
4992 if (ptr) {
4993 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4994 } else {
4995 set_fill_string(set, "", 0);
4996 }
4997
4998 return LY_SUCCESS;
4999}
5000
5001/**
5002 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5003 * with the sum of all the nodes in the context.
5004 *
5005 * @param[in] args Array of arguments.
5006 * @param[in] arg_count Count of elements in @p args.
5007 * @param[in,out] set Context and result set at the same time.
5008 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005009 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005010 */
5011static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005012xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005013{
5014 long double num;
5015 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005016 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005017 struct lyxp_set set_item;
5018 struct lysc_node_leaf *sleaf;
5019 LY_ERR rc = LY_SUCCESS;
5020
5021 if (options & LYXP_SCNODE_ALL) {
5022 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5023 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005024 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005025 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5026 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5027 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005028 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005029 } else if (!warn_is_numeric_type(sleaf->type)) {
5030 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005031 }
5032 }
5033 }
5034 }
5035 set_scnode_clear_ctx(set);
5036 return rc;
5037 }
5038
5039 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005040
5041 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005042 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005043 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005044 } else if (!args[0]->used) {
5045 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005046 }
5047
Michal Vasko5c4e5892019-11-14 12:31:38 +01005048 set_init(&set_item, set);
5049
Michal Vasko03ff5a72019-09-11 13:49:33 +02005050 set_item.type = LYXP_SET_NODE_SET;
5051 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5052 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5053
5054 set_item.used = 1;
5055 set_item.size = 1;
5056
5057 for (i = 0; i < args[0]->used; ++i) {
5058 set_item.val.nodes[0] = args[0]->val.nodes[i];
5059
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005060 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005061 LY_CHECK_RET(rc);
5062 num = cast_string_to_number(str);
5063 free(str);
5064 set->val.num += num;
5065 }
5066
5067 free(set_item.val.nodes);
5068
5069 return LY_SUCCESS;
5070}
5071
5072/**
5073 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5074 * with the text content of the nodes in the context.
5075 *
5076 * @param[in] args Array of arguments.
5077 * @param[in] arg_count Count of elements in @p args.
5078 * @param[in,out] set Context and result set at the same time.
5079 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005080 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005081 */
5082static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005083xpath_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 +02005084{
5085 uint32_t i;
5086
5087 if (options & LYXP_SCNODE_ALL) {
5088 set_scnode_clear_ctx(set);
5089 return LY_SUCCESS;
5090 }
5091
Michal Vasko03ff5a72019-09-11 13:49:33 +02005092 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005093 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005094 return LY_EVALID;
5095 }
5096
Michal Vaskod989ba02020-08-24 10:59:24 +02005097 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005098 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005099 case LYXP_NODE_NONE:
5100 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005101 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005102 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5103 set->val.nodes[i].type = LYXP_NODE_TEXT;
5104 ++i;
5105 break;
5106 }
Radek Krejci0f969882020-08-21 16:56:47 +02005107 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005108 case LYXP_NODE_ROOT:
5109 case LYXP_NODE_ROOT_CONFIG:
5110 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005111 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005112 set_remove_node(set, i);
5113 break;
5114 }
5115 }
5116
5117 return LY_SUCCESS;
5118}
5119
5120/**
5121 * @brief Execute the XPath translate(string, string, string) function.
5122 * Returns LYXP_SET_STRING with the first argument with the characters
5123 * from the second argument replaced by those on the corresponding
5124 * positions in the third argument.
5125 *
5126 * @param[in] args Array of arguments.
5127 * @param[in] arg_count Count of elements in @p args.
5128 * @param[in,out] set Context and result set at the same time.
5129 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005130 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005131 */
5132static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005133xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005134{
5135 uint16_t i, j, new_used;
5136 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005137 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005138 struct lysc_node_leaf *sleaf;
5139 LY_ERR rc = LY_SUCCESS;
5140
5141 if (options & LYXP_SCNODE_ALL) {
5142 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5143 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5144 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 +02005145 } else if (!warn_is_string_type(sleaf->type)) {
5146 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005147 }
5148 }
5149
5150 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5151 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5152 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 +02005153 } else if (!warn_is_string_type(sleaf->type)) {
5154 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005155 }
5156 }
5157
5158 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5159 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5160 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 +02005161 } else if (!warn_is_string_type(sleaf->type)) {
5162 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005163 }
5164 }
5165 set_scnode_clear_ctx(set);
5166 return rc;
5167 }
5168
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005169 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005170 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005171 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005172 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005173 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005174 LY_CHECK_RET(rc);
5175
5176 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5177 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5178 new_used = 0;
5179
5180 have_removed = 0;
5181 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005182 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005183
5184 for (j = 0; args[1]->val.str[j]; ++j) {
5185 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5186 /* removing this char */
5187 if (j >= strlen(args[2]->val.str)) {
5188 have_removed = 1;
5189 found = 1;
5190 break;
5191 }
5192 /* replacing this char */
5193 new[new_used] = args[2]->val.str[j];
5194 ++new_used;
5195 found = 1;
5196 break;
5197 }
5198 }
5199
5200 /* copying this char */
5201 if (!found) {
5202 new[new_used] = args[0]->val.str[i];
5203 ++new_used;
5204 }
5205 }
5206
5207 if (have_removed) {
5208 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5209 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5210 }
5211 new[new_used] = '\0';
5212
Michal Vaskod3678892020-05-21 10:06:58 +02005213 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005214 set->type = LYXP_SET_STRING;
5215 set->val.str = new;
5216
5217 return LY_SUCCESS;
5218}
5219
5220/**
5221 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5222 * with true value.
5223 *
5224 * @param[in] args Array of arguments.
5225 * @param[in] arg_count Count of elements in @p args.
5226 * @param[in,out] set Context and result set at the same time.
5227 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005228 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005229 */
5230static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005231xpath_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 +02005232{
5233 if (options & LYXP_SCNODE_ALL) {
5234 set_scnode_clear_ctx(set);
5235 return LY_SUCCESS;
5236 }
5237
5238 set_fill_boolean(set, 1);
5239 return LY_SUCCESS;
5240}
5241
5242/*
5243 * moveto functions
5244 *
5245 * They and only they actually change the context (set).
5246 */
5247
5248/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005249 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005250 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005251 * XPath @p set is expected to be a (sc)node set!
5252 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005253 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5254 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005255 * @param[in] set Set with general XPath context.
5256 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005257 * @param[out] moveto_mod Expected module of a matching node.
5258 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005259 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005260static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005261moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5262 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005263{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005264 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005265 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005266 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005267
Michal Vasko2104e9f2020-03-06 08:23:25 +01005268 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5269
Michal Vasko6346ece2019-09-24 13:12:53 +02005270 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5271 /* specific module */
5272 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005273 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005274
Michal Vasko004d3152020-06-11 19:59:22 +02005275 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005276 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005277 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005278 return LY_EVALID;
5279 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005280
Michal Vasko6346ece2019-09-24 13:12:53 +02005281 *qname += pref_len + 1;
5282 *qname_len -= pref_len + 1;
5283 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5284 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005285 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005286 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005287 switch (set->format) {
5288 case LY_PREF_SCHEMA:
5289 case LY_PREF_SCHEMA_RESOLVED:
5290 /* current module */
5291 mod = set->cur_mod;
5292 break;
5293 case LY_PREF_JSON:
5294 /* inherit parent (context node) module */
5295 if (ctx_scnode) {
5296 mod = ctx_scnode->module;
5297 } else {
5298 mod = NULL;
5299 }
5300 break;
5301 case LY_PREF_XML:
5302 /* not defined */
5303 LOGINT_RET(set->ctx);
5304 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005305 }
5306
Michal Vasko6346ece2019-09-24 13:12:53 +02005307 *moveto_mod = mod;
5308 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005309}
5310
5311/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005312 * @brief Move context @p set to the root. Handles absolute path.
5313 * Result is LYXP_SET_NODE_SET.
5314 *
5315 * @param[in,out] set Set to use.
5316 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005317 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005318 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005319static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005320moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005321{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005322 if (!set) {
Michal Vaskob0099a92020-08-31 14:55:23 +02005323 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005324 }
5325
5326 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005327 set_scnode_clear_ctx(set);
Michal Vaskob0099a92020-08-31 14:55:23 +02005328 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005329 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005330 set->type = LYXP_SET_NODE_SET;
5331 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005332 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005333 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005334
5335 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005336}
5337
5338/**
5339 * @brief Check @p node as a part of NameTest processing.
5340 *
5341 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005342 * @param[in] ctx_node Context node.
5343 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005344 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005345 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005346 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005347 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5348 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005349 */
5350static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005351moveto_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 +01005352 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005353{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005354 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5355 switch (set->format) {
5356 case LY_PREF_SCHEMA:
5357 case LY_PREF_SCHEMA_RESOLVED:
5358 /* use current module */
5359 moveto_mod = set->cur_mod;
5360 break;
5361 case LY_PREF_JSON:
5362 /* inherit module of the context node, if any */
5363 if (ctx_node) {
5364 moveto_mod = ctx_node->schema->module;
5365 }
5366 break;
5367 case LY_PREF_XML:
5368 /* not defined */
5369 LOGINT(set->ctx);
5370 return LY_EINVAL;
5371 }
5372 }
5373
Michal Vasko03ff5a72019-09-11 13:49:33 +02005374 /* module check */
5375 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005376 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005377 }
5378
Michal Vasko5c4e5892019-11-14 12:31:38 +01005379 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005380 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005381 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005382 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5383 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005384 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005385 }
5386
5387 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005388 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005389 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005390 }
5391
Michal Vaskoa1424542019-11-14 16:08:52 +01005392 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005393 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005394 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005395 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005396
5397 /* match */
5398 return LY_SUCCESS;
5399}
5400
5401/**
5402 * @brief Check @p node as a part of schema NameTest processing.
5403 *
5404 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005405 * @param[in] ctx_scnode Context node.
5406 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005407 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005408 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005409 * @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 +02005410 */
5411static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005412moveto_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 +02005413 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005414{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005415 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5416 switch (set->format) {
5417 case LY_PREF_SCHEMA:
5418 case LY_PREF_SCHEMA_RESOLVED:
5419 /* use current module */
5420 moveto_mod = set->cur_mod;
5421 break;
5422 case LY_PREF_JSON:
5423 /* inherit module of the context node, if any */
5424 if (ctx_scnode) {
5425 moveto_mod = ctx_scnode->module;
5426 }
5427 break;
5428 case LY_PREF_XML:
5429 /* not defined */
5430 LOGINT(set->ctx);
5431 return LY_EINVAL;
5432 }
5433 }
5434
Michal Vasko03ff5a72019-09-11 13:49:33 +02005435 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005436 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005437 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005438 }
5439
5440 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005441 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005442 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005443 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005444 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005445 }
5446
5447 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005448 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005449 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005450 }
5451
5452 /* match */
5453 return LY_SUCCESS;
5454}
5455
5456/**
Michal Vaskod3678892020-05-21 10:06:58 +02005457 * @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 +02005458 *
5459 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005460 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005461 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005462 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005463 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5464 */
5465static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005466moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005467{
Michal Vaskof03ed032020-03-04 13:31:44 +01005468 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005469 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005470 LY_ERR rc;
5471
Michal Vaskod3678892020-05-21 10:06:58 +02005472 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005473 return LY_SUCCESS;
5474 }
5475
5476 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005477 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005478 return LY_EVALID;
5479 }
5480
Michal Vasko03ff5a72019-09-11 13:49:33 +02005481 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005482 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005483
5484 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 +01005485 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005486
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005487 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005488 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005489 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005490 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005491 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005492 ctx_node = set->val.nodes[i].node;
5493 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005494 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005495
Michal Vaskod3678892020-05-21 10:06:58 +02005496 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005497 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005498 if (rc == LY_SUCCESS) {
5499 if (!replaced) {
5500 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5501 replaced = 1;
5502 } else {
5503 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005504 }
Michal Vaskod3678892020-05-21 10:06:58 +02005505 ++i;
5506 } else if (rc == LY_EINCOMPLETE) {
5507 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005508 }
5509 }
5510
5511 if (!replaced) {
5512 /* no match */
5513 set_remove_node(set, i);
5514 }
5515 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005516
5517 return LY_SUCCESS;
5518}
5519
5520/**
Michal Vaskod3678892020-05-21 10:06:58 +02005521 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5522 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005523 *
5524 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005525 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005526 * @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 +01005527 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005528 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5529 */
5530static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005531moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5532 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005533{
Michal Vasko004d3152020-06-11 19:59:22 +02005534 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005535 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005536 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005537 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005538
Michal Vasko004d3152020-06-11 19:59:22 +02005539 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005540
5541 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005542 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005543 }
5544
5545 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005546 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005547 ret = LY_EVALID;
5548 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005549 }
5550
5551 /* context check for all the nodes since we have the schema node */
5552 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5553 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005554 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005555 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5556 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005557 lyxp_set_free_content(set);
5558 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005559 }
5560
5561 /* create specific data instance if needed */
5562 if (scnode->nodetype == LYS_LIST) {
5563 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5564 } else if (scnode->nodetype == LYS_LEAFLIST) {
5565 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005566 }
5567
5568 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005569 siblings = NULL;
5570
5571 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5572 assert(!set->val.nodes[i].node);
5573
5574 /* search in all the trees */
5575 siblings = set->tree;
5576 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5577 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005578 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005579 }
5580
5581 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005582 if (inst) {
5583 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005584 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005585 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005586 }
5587
5588 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005589 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005590 ret = LY_EINCOMPLETE;
5591 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005592 }
5593
5594 if (sub) {
5595 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005596 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005597 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005598 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005599 /* no match */
5600 set_remove_node(set, i);
5601 }
5602 }
5603
Michal Vasko004d3152020-06-11 19:59:22 +02005604cleanup:
5605 lyd_free_tree(inst);
5606 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005607}
5608
5609/**
5610 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5611 *
5612 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005613 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005614 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005615 * @param[in] options XPath options.
5616 * @return LY_ERR
5617 */
5618static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005619moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005620{
Radek Krejci857189e2020-09-01 13:26:36 +02005621 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005622 uint32_t getnext_opts;
5623 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005624 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005625 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005626 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005627
Michal Vaskod3678892020-05-21 10:06:58 +02005628 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005629 return LY_SUCCESS;
5630 }
5631
5632 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005633 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005634 return LY_EVALID;
5635 }
5636
Michal Vaskocafad9d2019-11-07 15:20:03 +01005637 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005638 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005639 if (options & LYXP_SCNODE_OUTPUT) {
5640 getnext_opts |= LYS_GETNEXT_OUTPUT;
5641 }
5642
Michal Vasko03ff5a72019-09-11 13:49:33 +02005643 orig_used = set->used;
5644 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005645 uint32_t idx;
5646
Radek Krejcif13b87b2020-12-01 22:02:17 +01005647 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5648 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005649 continue;
5650 }
5651
5652 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005653 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005654 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005655 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005656 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005657
5658 start_parent = set->val.scnodes[i].scnode;
5659
5660 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 +02005661 /* 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 +02005662 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005663 mod_idx = 0;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005664 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005665 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005666 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005667 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005668 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005669 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5670
Michal Vasko03ff5a72019-09-11 13:49:33 +02005671 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005672 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005673 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005674 temp_ctx = 1;
5675 }
5676 }
5677 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005678 }
5679
Michal Vasko519fd602020-05-26 12:17:39 +02005680 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5681 iter = NULL;
5682 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005683 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005684 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5685
5686 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005687 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005688 temp_ctx = 1;
5689 }
5690 }
5691 }
5692 }
5693 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005694
5695 /* correct temporary in_ctx values */
5696 if (temp_ctx) {
5697 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005698 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5699 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005700 }
5701 }
5702 }
5703
5704 return LY_SUCCESS;
5705}
5706
5707/**
Michal Vaskod3678892020-05-21 10:06:58 +02005708 * @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 +02005709 * Context position aware.
5710 *
5711 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005712 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005713 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005714 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005715 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5716 */
5717static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005718moveto_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 +02005719{
5720 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005721 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005722 struct lyxp_set ret_set;
5723 LY_ERR rc;
5724
Michal Vaskod3678892020-05-21 10:06:58 +02005725 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005726 return LY_SUCCESS;
5727 }
5728
5729 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005730 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005731 return LY_EVALID;
5732 }
5733
Michal Vasko9f96a052020-03-10 09:41:45 +01005734 /* 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 +01005735 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005736 LY_CHECK_RET(rc);
5737
Michal Vasko6346ece2019-09-24 13:12:53 +02005738 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005739 set_init(&ret_set, set);
5740 for (i = 0; i < set->used; ++i) {
5741
5742 /* TREE DFS */
5743 start = set->val.nodes[i].node;
5744 for (elem = next = start; elem; elem = next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005745 rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005746 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005747 /* add matching node into result set */
5748 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5749 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5750 /* the node is a duplicate, we'll process it later in the set */
5751 goto skip_children;
5752 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005753 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005754 return rc;
5755 } else if (rc == LY_EINVAL) {
5756 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005757 }
5758
5759 /* TREE DFS NEXT ELEM */
5760 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005761 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005762 if (!next) {
5763skip_children:
5764 /* no children, so try siblings, but only if it's not the start,
5765 * that is considered to be the root and it's siblings are not traversed */
5766 if (elem != start) {
5767 next = elem->next;
5768 } else {
5769 break;
5770 }
5771 }
5772 while (!next) {
5773 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01005774 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005775 /* we are done, no next element to process */
5776 break;
5777 }
5778 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01005779 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005780 next = elem->next;
5781 }
5782 }
5783 }
5784
5785 /* make the temporary set the current one */
5786 ret_set.ctx_pos = set->ctx_pos;
5787 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005788 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005789 memcpy(set, &ret_set, sizeof *set);
5790
5791 return LY_SUCCESS;
5792}
5793
5794/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005795 * @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 +02005796 *
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 Vasko03ff5a72019-09-11 13:49:33 +02005800 * @param[in] options XPath options.
5801 * @return LY_ERR
5802 */
5803static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005804moveto_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 +02005805{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005806 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005807 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005808 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005809
Michal Vaskod3678892020-05-21 10:06:58 +02005810 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005811 return LY_SUCCESS;
5812 }
5813
5814 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005815 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005816 return LY_EVALID;
5817 }
5818
Michal Vasko03ff5a72019-09-11 13:49:33 +02005819 orig_used = set->used;
5820 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005821 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5822 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005823 continue;
5824 }
5825
5826 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005827 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005828 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005829 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005830 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005831
5832 /* TREE DFS */
5833 start = set->val.scnodes[i].scnode;
5834 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005835 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5836 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005837 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005838 }
5839
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005840 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005841 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005842 uint32_t idx;
5843
5844 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005845 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005846 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005847 /* we will process it later in the set */
5848 goto skip_children;
5849 }
5850 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005851 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005852 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005853 } else if (rc == LY_EINVAL) {
5854 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005855 }
5856
5857next_iter:
5858 /* TREE DFS NEXT ELEM */
5859 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005860 next = lysc_node_child(elem);
5861 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5862 next = next->next;
5863 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5864 next = next->next;
5865 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005866 if (!next) {
5867skip_children:
5868 /* no children, so try siblings, but only if it's not the start,
5869 * that is considered to be the root and it's siblings are not traversed */
5870 if (elem != start) {
5871 next = elem->next;
5872 } else {
5873 break;
5874 }
5875 }
5876 while (!next) {
5877 /* no siblings, go back through the parents */
5878 if (elem->parent == start) {
5879 /* we are done, no next element to process */
5880 break;
5881 }
5882 /* parent is already processed, go to its sibling */
5883 elem = elem->parent;
5884 next = elem->next;
5885 }
5886 }
5887 }
5888
5889 return LY_SUCCESS;
5890}
5891
5892/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005893 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005894 * Indirectly context position aware.
5895 *
5896 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005897 * @param[in] mod Matching metadata module, NULL for any.
5898 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005899 * @return LY_ERR
5900 */
5901static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005902moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005903{
Michal Vasko9f96a052020-03-10 09:41:45 +01005904 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005905
Michal Vaskod3678892020-05-21 10:06:58 +02005906 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005907 return LY_SUCCESS;
5908 }
5909
5910 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005911 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005912 return LY_EVALID;
5913 }
5914
Radek Krejci1deb5be2020-08-26 16:43:36 +02005915 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005916 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005917
5918 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5919 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005920 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005921 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005922
5923 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005924 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005925 continue;
5926 }
5927
Michal Vaskod3678892020-05-21 10:06:58 +02005928 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005929 /* match */
5930 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005931 set->val.meta[i].meta = sub;
5932 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005933 /* pos does not change */
5934 replaced = 1;
5935 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005936 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 +02005937 }
5938 ++i;
5939 }
5940 }
5941 }
5942
5943 if (!replaced) {
5944 /* no match */
5945 set_remove_node(set, i);
5946 }
5947 }
5948
5949 return LY_SUCCESS;
5950}
5951
5952/**
5953 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005954 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005955 *
5956 * @param[in,out] set1 Set to use for the result.
5957 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005958 * @return LY_ERR
5959 */
5960static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005961moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005962{
5963 LY_ERR rc;
5964
Michal Vaskod3678892020-05-21 10:06:58 +02005965 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005966 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005967 return LY_EVALID;
5968 }
5969
5970 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005971 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005972 return LY_SUCCESS;
5973 }
5974
Michal Vaskod3678892020-05-21 10:06:58 +02005975 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005976 memcpy(set1, set2, sizeof *set1);
5977 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005978 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005979 return LY_SUCCESS;
5980 }
5981
5982 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005983 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005984
5985 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005986 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005987 LY_CHECK_RET(rc);
5988
5989 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005990 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005991
5992 return LY_SUCCESS;
5993}
5994
5995/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005996 * @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 +02005997 * Context position aware.
5998 *
5999 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006000 * @param[in] mod Matching metadata module, NULL for any.
6001 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006002 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006003 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6004 */
6005static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006006moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006007{
Michal Vasko9f96a052020-03-10 09:41:45 +01006008 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006009 struct lyxp_set *set_all_desc = NULL;
6010 LY_ERR rc;
6011
Michal Vaskod3678892020-05-21 10:06:58 +02006012 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006013 return LY_SUCCESS;
6014 }
6015
6016 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006017 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006018 return LY_EVALID;
6019 }
6020
Michal Vasko03ff5a72019-09-11 13:49:33 +02006021 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6022 * but it likely won't be used much, so it's a waste of time */
6023 /* copy the context */
6024 set_all_desc = set_copy(set);
6025 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006026 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006027 if (rc != LY_SUCCESS) {
6028 lyxp_set_free(set_all_desc);
6029 return rc;
6030 }
6031 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006032 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006033 if (rc != LY_SUCCESS) {
6034 lyxp_set_free(set_all_desc);
6035 return rc;
6036 }
6037 lyxp_set_free(set_all_desc);
6038
Radek Krejci1deb5be2020-08-26 16:43:36 +02006039 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006040 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006041
6042 /* only attributes of an elem can be in the result, skip all the rest,
6043 * we have all attributes qualified in lyd tree */
6044 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006045 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006046 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006047 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006048 continue;
6049 }
6050
Michal Vaskod3678892020-05-21 10:06:58 +02006051 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006052 /* match */
6053 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006054 set->val.meta[i].meta = sub;
6055 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006056 /* pos does not change */
6057 replaced = 1;
6058 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006059 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 +02006060 }
6061 ++i;
6062 }
6063 }
6064 }
6065
6066 if (!replaced) {
6067 /* no match */
6068 set_remove_node(set, i);
6069 }
6070 }
6071
6072 return LY_SUCCESS;
6073}
6074
6075/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006076 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6077 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006078 *
6079 * @param[in] parent Current parent.
6080 * @param[in] parent_pos Position of @p parent.
6081 * @param[in] parent_type Node type of @p parent.
6082 * @param[in,out] to_set Set to use.
6083 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006084 * @param[in] options XPath options.
6085 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6086 */
6087static LY_ERR
6088moveto_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 +02006089 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006090{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006091 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006092 LY_ERR rc;
6093
6094 switch (parent_type) {
6095 case LYXP_NODE_ROOT:
6096 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006097 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006098
Michal Vasko61ac2f62020-05-25 12:39:51 +02006099 /* add all top-level nodes as elements */
6100 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006101 break;
6102 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006103 /* add just the text node of this term element node */
6104 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006105 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6106 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6107 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006108 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006109 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006110
6111 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006112 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006113 break;
6114 default:
6115 LOGINT_RET(parent->schema->module->ctx);
6116 }
6117
Michal Vasko61ac2f62020-05-25 12:39:51 +02006118 /* add all top-level nodes as elements */
6119 LY_LIST_FOR(first, iter) {
6120 /* context check */
6121 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6122 continue;
6123 }
6124
6125 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006126 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006127 return LY_EINCOMPLETE;
6128 }
6129
6130 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6131 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6132
6133 /* also add all the children of this node, recursively */
6134 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6135 LY_CHECK_RET(rc);
6136 }
6137 }
6138
Michal Vasko03ff5a72019-09-11 13:49:33 +02006139 return LY_SUCCESS;
6140}
6141
6142/**
6143 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6144 * (or LYXP_SET_EMPTY). Context position aware.
6145 *
6146 * @param[in,out] set Set to use.
6147 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6148 * @param[in] options XPath options.
6149 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6150 */
6151static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006152moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006153{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006154 struct lyxp_set ret_set;
6155 LY_ERR rc;
6156
Michal Vaskod3678892020-05-21 10:06:58 +02006157 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006158 return LY_SUCCESS;
6159 }
6160
6161 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006162 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006163 return LY_EVALID;
6164 }
6165
6166 /* nothing to do */
6167 if (!all_desc) {
6168 return LY_SUCCESS;
6169 }
6170
Michal Vasko03ff5a72019-09-11 13:49:33 +02006171 /* add all the children, they get added recursively */
6172 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006173 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006174 /* copy the current node to tmp */
6175 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6176
6177 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006178 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006179 continue;
6180 }
6181
Michal Vasko03ff5a72019-09-11 13:49:33 +02006182 /* add all the children */
6183 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 +02006184 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006185 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006186 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006187 return rc;
6188 }
6189 }
6190
6191 /* use the temporary set as the current one */
6192 ret_set.ctx_pos = set->ctx_pos;
6193 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006194 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006195 memcpy(set, &ret_set, sizeof *set);
6196
6197 return LY_SUCCESS;
6198}
6199
6200/**
6201 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6202 * (or LYXP_SET_EMPTY).
6203 *
6204 * @param[in,out] set Set to use.
6205 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6206 * @param[in] options XPath options.
6207 * @return LY_ERR
6208 */
6209static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006210moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006211{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006212 uint32_t getnext_opts;
6213 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006214 const struct lysc_node *iter, *start_parent;
6215 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006216
Michal Vaskod3678892020-05-21 10:06:58 +02006217 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006218 return LY_SUCCESS;
6219 }
6220
6221 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006222 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006223 return LY_EVALID;
6224 }
6225
Michal Vasko519fd602020-05-26 12:17:39 +02006226 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006227 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006228 if (options & LYXP_SCNODE_OUTPUT) {
6229 getnext_opts |= LYS_GETNEXT_OUTPUT;
6230 }
6231
6232 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006233 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006234 if (!all_desc) {
6235 /* traverse the start node */
6236 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6237 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6238 }
6239 continue;
6240 }
6241
Radek Krejcif13b87b2020-12-01 22:02:17 +01006242 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6243 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006244 continue;
6245 }
6246
Michal Vasko519fd602020-05-26 12:17:39 +02006247 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006248 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006249 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006250 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006251 }
6252
Michal Vasko519fd602020-05-26 12:17:39 +02006253 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006254
Michal Vasko519fd602020-05-26 12:17:39 +02006255 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6256 /* it can actually be in any module, it's all <running> */
6257 mod_idx = 0;
6258 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6259 iter = NULL;
6260 /* module may not be implemented */
6261 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6262 /* context check */
6263 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6264 continue;
6265 }
6266
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006267 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006268 /* throw away the insert index, we want to consider that node again, recursively */
6269 }
6270 }
6271
6272 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6273 iter = NULL;
6274 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006275 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006276 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006277 continue;
6278 }
6279
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006280 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006281 }
6282 }
6283 }
6284
6285 return LY_SUCCESS;
6286}
6287
6288/**
6289 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6290 * (or LYXP_SET_EMPTY). Context position aware.
6291 *
6292 * @param[in] set Set to use.
6293 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6294 * @param[in] options XPath options.
6295 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6296 */
6297static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006298moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006299{
6300 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006301 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006302 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006303
Michal Vaskod3678892020-05-21 10:06:58 +02006304 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006305 return LY_SUCCESS;
6306 }
6307
6308 if (set->type != LYXP_SET_NODE_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
6313 if (all_desc) {
6314 /* <path>//.. == <path>//./.. */
6315 rc = moveto_self(set, 1, options);
6316 LY_CHECK_RET(rc);
6317 }
6318
Radek Krejci1deb5be2020-08-26 16:43:36 +02006319 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006320 node = set->val.nodes[i].node;
6321
6322 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9e685082021-01-29 14:49:09 +01006323 new_node = lyd_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006324 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6325 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006326 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6327 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006328 if (!new_node) {
6329 LOGINT_RET(set->ctx);
6330 }
6331 } else {
6332 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006333 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006334 continue;
6335 }
6336
Michal Vaskoa1424542019-11-14 16:08:52 +01006337 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006338 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 +02006339 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006340 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006341
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006342 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006343 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006344 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006345
Michal Vasko03ff5a72019-09-11 13:49:33 +02006346 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006347 /* 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 +02006348 new_type = LYXP_NODE_ELEM;
6349 }
6350
Michal Vasko03ff5a72019-09-11 13:49:33 +02006351 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006352 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006353 } else {
6354 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006355 }
6356 }
6357
Michal Vasko2caefc12019-11-14 16:07:56 +01006358 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006359 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006360
6361 return LY_SUCCESS;
6362}
6363
6364/**
6365 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6366 * (or LYXP_SET_EMPTY).
6367 *
6368 * @param[in] set Set to use.
6369 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6370 * @param[in] options XPath options.
6371 * @return LY_ERR
6372 */
6373static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006374moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006375{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006376 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006377 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006378 const struct lysc_node *node, *new_node;
6379 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006380
Michal Vaskod3678892020-05-21 10:06:58 +02006381 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006382 return LY_SUCCESS;
6383 }
6384
6385 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006386 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006387 return LY_EVALID;
6388 }
6389
6390 if (all_desc) {
6391 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006392 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006393 }
6394
Michal Vasko03ff5a72019-09-11 13:49:33 +02006395 orig_used = set->used;
6396 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006397 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6398 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006399 continue;
6400 }
6401
6402 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006403 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006404 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006405 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006406 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006407
6408 node = set->val.scnodes[i].scnode;
6409
6410 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006411 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006412 } else {
6413 /* root does not have a parent */
6414 continue;
6415 }
6416
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006417 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006418 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006419 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006420
Michal Vasko03ff5a72019-09-11 13:49:33 +02006421 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006422 /* 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 +02006423 new_type = LYXP_NODE_ELEM;
6424 }
6425
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006426 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6427 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006428 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006429 temp_ctx = 1;
6430 }
6431 }
6432
6433 if (temp_ctx) {
6434 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006435 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6436 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006437 }
6438 }
6439 }
6440
6441 return LY_SUCCESS;
6442}
6443
6444/**
6445 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6446 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6447 *
6448 * @param[in,out] set1 Set to use for the result.
6449 * @param[in] set2 Set acting as the second operand for @p op.
6450 * @param[in] op Comparison operator to process.
6451 * @param[in] options XPath options.
6452 * @return LY_ERR
6453 */
6454static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006455moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006456{
6457 /*
6458 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6459 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6460 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6461 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6462 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6463 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6464 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6465 *
6466 * '=' or '!='
6467 * BOOLEAN + BOOLEAN
6468 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6469 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6470 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6471 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6472 * NUMBER + NUMBER
6473 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6474 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6475 * STRING + STRING
6476 *
6477 * '<=', '<', '>=', '>'
6478 * NUMBER + NUMBER
6479 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6480 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6481 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6482 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6483 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6484 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6485 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6486 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6487 */
6488 struct lyxp_set iter1, iter2;
6489 int result;
6490 int64_t i;
6491 LY_ERR rc;
6492
Michal Vasko004d3152020-06-11 19:59:22 +02006493 memset(&iter1, 0, sizeof iter1);
6494 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006495
6496 /* iterative evaluation with node-sets */
6497 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6498 if (set1->type == LYXP_SET_NODE_SET) {
6499 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006500 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006501 switch (set2->type) {
6502 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006503 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006504 break;
6505 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006506 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006507 break;
6508 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006509 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006510 break;
6511 }
6512 LY_CHECK_RET(rc);
6513
Michal Vasko4c7763f2020-07-27 17:40:37 +02006514 /* canonize set2 */
6515 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6516
6517 /* compare recursively */
6518 rc = moveto_op_comp(&iter1, &iter2, op, options);
6519 lyxp_set_free_content(&iter2);
6520 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006521
6522 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006523 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006524 set_fill_boolean(set1, 1);
6525 return LY_SUCCESS;
6526 }
6527 }
6528 } else {
6529 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006530 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006531 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006532 case LYXP_SET_NUMBER:
6533 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6534 break;
6535 case LYXP_SET_BOOLEAN:
6536 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6537 break;
6538 default:
6539 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6540 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006541 }
6542 LY_CHECK_RET(rc);
6543
Michal Vasko4c7763f2020-07-27 17:40:37 +02006544 /* canonize set1 */
6545 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 +02006546
Michal Vasko4c7763f2020-07-27 17:40:37 +02006547 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006548 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006549 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006550 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006551
6552 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006553 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006554 set_fill_boolean(set1, 1);
6555 return LY_SUCCESS;
6556 }
6557 }
6558 }
6559
6560 /* false for all nodes */
6561 set_fill_boolean(set1, 0);
6562 return LY_SUCCESS;
6563 }
6564
6565 /* first convert properly */
6566 if ((op[0] == '=') || (op[0] == '!')) {
6567 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006568 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6569 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006570 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006571 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006572 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006573 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006574 LY_CHECK_RET(rc);
6575 } /* else we have 2 strings */
6576 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006577 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006578 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006579 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006580 LY_CHECK_RET(rc);
6581 }
6582
6583 assert(set1->type == set2->type);
6584
6585 /* compute result */
6586 if (op[0] == '=') {
6587 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006588 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006589 } else if (set1->type == LYXP_SET_NUMBER) {
6590 result = (set1->val.num == set2->val.num);
6591 } else {
6592 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006593 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006594 }
6595 } else if (op[0] == '!') {
6596 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006597 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006598 } else if (set1->type == LYXP_SET_NUMBER) {
6599 result = (set1->val.num != set2->val.num);
6600 } else {
6601 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006602 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006603 }
6604 } else {
6605 assert(set1->type == LYXP_SET_NUMBER);
6606 if (op[0] == '<') {
6607 if (op[1] == '=') {
6608 result = (set1->val.num <= set2->val.num);
6609 } else {
6610 result = (set1->val.num < set2->val.num);
6611 }
6612 } else {
6613 if (op[1] == '=') {
6614 result = (set1->val.num >= set2->val.num);
6615 } else {
6616 result = (set1->val.num > set2->val.num);
6617 }
6618 }
6619 }
6620
6621 /* assign result */
6622 if (result) {
6623 set_fill_boolean(set1, 1);
6624 } else {
6625 set_fill_boolean(set1, 0);
6626 }
6627
6628 return LY_SUCCESS;
6629}
6630
6631/**
6632 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6633 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6634 *
6635 * @param[in,out] set1 Set to use for the result.
6636 * @param[in] set2 Set acting as the second operand for @p op.
6637 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006638 * @return LY_ERR
6639 */
6640static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006641moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006642{
6643 LY_ERR rc;
6644
6645 /* unary '-' */
6646 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006647 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006648 LY_CHECK_RET(rc);
6649 set1->val.num *= -1;
6650 lyxp_set_free(set2);
6651 return LY_SUCCESS;
6652 }
6653
6654 assert(set1 && set2);
6655
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006656 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006657 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006658 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006659 LY_CHECK_RET(rc);
6660
6661 switch (op[0]) {
6662 /* '+' */
6663 case '+':
6664 set1->val.num += set2->val.num;
6665 break;
6666
6667 /* '-' */
6668 case '-':
6669 set1->val.num -= set2->val.num;
6670 break;
6671
6672 /* '*' */
6673 case '*':
6674 set1->val.num *= set2->val.num;
6675 break;
6676
6677 /* 'div' */
6678 case 'd':
6679 set1->val.num /= set2->val.num;
6680 break;
6681
6682 /* 'mod' */
6683 case 'm':
6684 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6685 break;
6686
6687 default:
6688 LOGINT_RET(set1->ctx);
6689 }
6690
6691 return LY_SUCCESS;
6692}
6693
6694/*
6695 * eval functions
6696 *
6697 * They execute a parsed XPath expression on some data subtree.
6698 */
6699
6700/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006701 * @brief Evaluate Predicate. Logs directly on error.
6702 *
Michal Vaskod3678892020-05-21 10:06:58 +02006703 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006704 *
6705 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006706 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006707 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6708 * @param[in] options XPath options.
6709 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6710 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6711 */
6712static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006713eval_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 +02006714{
6715 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006716 uint16_t orig_exp;
6717 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006718 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006719 struct lyxp_set set2;
6720 struct lyd_node *orig_parent;
6721
6722 /* '[' */
6723 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006724 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006725 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006726
6727 if (!set) {
6728only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006729 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006730 LY_CHECK_RET(rc);
6731 } else if (set->type == LYXP_SET_NODE_SET) {
6732 /* 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 +01006733 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006734
6735 /* empty set, nothing to evaluate */
6736 if (!set->used) {
6737 goto only_parse;
6738 }
6739
Michal Vasko004d3152020-06-11 19:59:22 +02006740 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006741 orig_pos = 0;
6742 orig_size = set->used;
6743 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006744 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006745 set_init(&set2, set);
6746 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6747 /* remember the node context position for position() and context size for last(),
6748 * predicates should always be evaluated with respect to the child axis (since we do
6749 * not support explicit axes) so we assign positions based on their parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006750 if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) {
6751 orig_parent = lyd_parent(set->val.nodes[i].node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006752 orig_pos = 1;
6753 } else {
6754 ++orig_pos;
6755 }
6756
6757 set2.ctx_pos = orig_pos;
6758 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006759 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006760
Michal Vasko004d3152020-06-11 19:59:22 +02006761 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006762 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006763 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006764 return rc;
6765 }
6766
6767 /* number is a position */
6768 if (set2.type == LYXP_SET_NUMBER) {
6769 if ((long long)set2.val.num == orig_pos) {
6770 set2.val.num = 1;
6771 } else {
6772 set2.val.num = 0;
6773 }
6774 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006775 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006776
6777 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006778 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006779 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006780 }
6781 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006782 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006783
6784 } else if (set->type == LYXP_SET_SCNODE_SET) {
6785 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006786 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006787 /* there is a currently-valid node */
6788 break;
6789 }
6790 }
6791 /* empty set, nothing to evaluate */
6792 if (i == set->used) {
6793 goto only_parse;
6794 }
6795
Michal Vasko004d3152020-06-11 19:59:22 +02006796 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006797
Michal Vasko03ff5a72019-09-11 13:49:33 +02006798 /* set special in_ctx to all the valid snodes */
6799 pred_in_ctx = set_scnode_new_in_ctx(set);
6800
6801 /* use the valid snodes one-by-one */
6802 for (i = 0; i < set->used; ++i) {
6803 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6804 continue;
6805 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006806 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006807
Michal Vasko004d3152020-06-11 19:59:22 +02006808 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006809
Michal Vasko004d3152020-06-11 19:59:22 +02006810 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006811 LY_CHECK_RET(rc);
6812
6813 set->val.scnodes[i].in_ctx = pred_in_ctx;
6814 }
6815
6816 /* restore the state as it was before the predicate */
6817 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006818 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
6819 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006820 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006821 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006822 }
6823 }
6824
6825 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006826 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006827 set_fill_set(&set2, set);
6828
Michal Vasko004d3152020-06-11 19:59:22 +02006829 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006830 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006831 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006832 return rc;
6833 }
6834
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006835 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006836 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006837 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006838 }
Michal Vaskod3678892020-05-21 10:06:58 +02006839 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006840 }
6841
6842 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006843 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006844 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006845 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006846 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006847
6848 return LY_SUCCESS;
6849}
6850
6851/**
Michal Vaskod3678892020-05-21 10:06:58 +02006852 * @brief Evaluate Literal. Logs directly on error.
6853 *
6854 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006855 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006856 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6857 */
6858static void
Michal Vasko40308e72020-10-20 16:38:40 +02006859eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006860{
6861 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006862 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006863 set_fill_string(set, "", 0);
6864 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006865 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 +02006866 }
6867 }
6868 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006869 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006870 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006871}
6872
6873/**
Michal Vasko004d3152020-06-11 19:59:22 +02006874 * @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 +02006875 *
Michal Vasko004d3152020-06-11 19:59:22 +02006876 * @param[in] exp Full parsed XPath expression.
6877 * @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 +02006878 * @param[in] ctx_node Found schema node as the context for the predicate.
6879 * @param[in] cur_mod Current module for the expression.
6880 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006881 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006882 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006883 * @param[out] predicates Parsed predicates.
6884 * @param[out] pred_type Type of @p predicates.
6885 * @return LY_SUCCESS on success,
6886 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006887 */
6888static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006889eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *ctx_node,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006890 const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_PREFIX_FORMAT format, void *prefix_data,
6891 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006892{
Michal Vasko004d3152020-06-11 19:59:22 +02006893 LY_ERR ret = LY_SUCCESS;
6894 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006895 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006896 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006897 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006898 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006899
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006900 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006901
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006902 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006903 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006904 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006905 return LY_EINVAL;
6906 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006907 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 +02006908 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006909
Michal Vasko004d3152020-06-11 19:59:22 +02006910 /* learn where the predicates end */
6911 e_idx = *tok_idx;
6912 while (key_count) {
6913 /* '[' */
6914 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6915 return LY_EINVAL;
6916 }
6917 ++e_idx;
6918
6919 /* ']' */
6920 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6921 ++e_idx;
6922 }
6923 ++e_idx;
6924
6925 /* another presumably key predicate parsed */
6926 --key_count;
6927 }
Michal Vasko004d3152020-06-11 19:59:22 +02006928 } else {
6929 /* learn just where this single predicate ends */
6930 e_idx = *tok_idx;
6931
Michal Vaskod3678892020-05-21 10:06:58 +02006932 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006933 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6934 return LY_EINVAL;
6935 }
6936 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006937
6938 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006939 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6940 ++e_idx;
6941 }
6942 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006943 }
6944
Michal Vasko004d3152020-06-11 19:59:22 +02006945 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6946 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6947
6948 /* turn logging off */
6949 prev_lo = ly_log_options(0);
6950
6951 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006952 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
6953 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02006954
6955 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006956 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
6957 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006958 LY_CHECK_GOTO(ret, cleanup);
6959
6960 /* success, the predicate must include all the needed information for hash-based search */
6961 *tok_idx = e_idx;
6962
6963cleanup:
6964 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006965 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02006966 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006967}
6968
6969/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006970 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
6971 * data node(s) could be found using a single hash-based search.
6972 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01006973 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006974 * @param[in] node Next context node to check.
6975 * @param[in] name Expected node name.
6976 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01006977 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006978 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006979 * @param[in] format Prefix format.
6980 * @param[in,out] found Previously found node, is updated.
6981 * @return LY_SUCCESS on success,
6982 * @return LY_ENOT if the whole check failed and hashes cannot be used.
6983 */
6984static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01006985eval_name_test_with_predicate_get_scnode(const struct ly_ctx *ctx, const struct lyd_node *node, const char *name,
6986 uint16_t name_len, const struct lys_module *moveto_mod, enum lyxp_node_type root_type, LY_PREFIX_FORMAT format,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006987 const struct lysc_node **found)
6988{
6989 const struct lysc_node *scnode;
6990 const struct lys_module *mod;
6991 uint32_t idx = 0;
6992
Michal Vaskoc71c37b2021-01-11 13:40:02 +01006993 assert((format == LY_PREF_JSON) || moveto_mod);
6994
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006995continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02006996 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006997 if (!node) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01006998 if ((format == LY_PREF_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006999 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007000 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007001 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7002 if (scnode) {
7003 /* we have found a match */
7004 break;
7005 }
7006 }
7007
Michal Vasko7d1d0912020-10-16 08:38:30 +02007008 if (!scnode) {
7009 /* all modules searched */
7010 idx = 0;
7011 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007012 } else {
7013 /* search in top-level */
7014 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7015 }
7016 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007017 if ((format == LY_PREF_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007018 /* we must adjust the module to inherit the one from the context node */
7019 moveto_mod = node->schema->module;
7020 }
7021
7022 /* search in children, do not repeat the same search */
7023 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007024 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007025
7026 /* additional context check */
7027 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7028 scnode = NULL;
7029 }
7030
7031 if (scnode) {
7032 if (*found) {
7033 /* we found a schema node with the same name but at different level, give up, too complicated
7034 * (more hash-based searches would be required, not supported) */
7035 return LY_ENOT;
7036 } else {
7037 /* remember the found schema node and continue to make sure it can be used */
7038 *found = scnode;
7039 }
7040 }
7041
7042 if (idx) {
7043 /* continue searching all the following models */
7044 goto continue_search;
7045 }
7046
7047 return LY_SUCCESS;
7048}
7049
7050/**
Michal Vaskod3678892020-05-21 10:06:58 +02007051 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7052 *
7053 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7054 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7055 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7056 *
7057 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007058 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007059 * @param[in] attr_axis Whether to search attributes or standard nodes.
7060 * @param[in] all_desc Whether to search all the descendants or children only.
7061 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7062 * @param[in] options XPath options.
7063 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7064 */
7065static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007066eval_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 +02007067 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007068{
Michal Vaskod3678892020-05-21 10:06:58 +02007069 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007070 const char *ncname, *ncname_dict = NULL;
7071 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007072 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007073 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007074 struct ly_path_predicate *predicates = NULL;
7075 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007076 LY_ERR rc = LY_SUCCESS;
7077
7078 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007079 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007080 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007081
7082 if (!set) {
7083 goto moveto;
7084 }
7085
Michal Vasko004d3152020-06-11 19:59:22 +02007086 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7087 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007088
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007089 if ((ncname[0] == '*') && (ncname_len == 1)) {
7090 /* all nodes will match */
7091 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007092 }
7093
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007094 /* parse (and skip) module name */
7095 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007096 LY_CHECK_GOTO(rc, cleanup);
7097
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007098 if (((set->format == LY_PREF_JSON) || moveto_mod) && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007099 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007100 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007101 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7102 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007103 /* check failed */
7104 scnode = NULL;
7105 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007106 }
7107 }
7108
Michal Vasko004d3152020-06-11 19:59:22 +02007109 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7110 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007111 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7112 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007113 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007114 scnode = NULL;
7115 }
7116 }
7117 }
7118
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007119 if (!scnode) {
7120 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007121 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007122 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007123 }
7124
7125moveto:
7126 /* move to the attribute(s), data node(s), or schema node(s) */
7127 if (attr_axis) {
7128 if (set && (options & LYXP_SCNODE_ALL)) {
7129 set_scnode_clear_ctx(set);
7130 } else {
7131 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007132 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007133 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007134 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007135 }
7136 LY_CHECK_GOTO(rc, cleanup);
7137 }
7138 } else {
7139 if (set && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007140 int64_t i;
7141
Michal Vaskod3678892020-05-21 10:06:58 +02007142 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007143 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007144 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007145 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007146 }
7147 LY_CHECK_GOTO(rc, cleanup);
7148
7149 for (i = set->used - 1; i > -1; --i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007150 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM) {
Michal Vaskod3678892020-05-21 10:06:58 +02007151 break;
7152 }
7153 }
7154 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007155 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vasko90f12dc2020-12-03 14:20:42 +01007156 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".",
7157 ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007158 free(path);
7159 }
7160 } else {
7161 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007162 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007163 } else {
7164 if (scnode) {
7165 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007166 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007167 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007168 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007169 }
7170 }
7171 LY_CHECK_GOTO(rc, cleanup);
7172 }
7173 }
7174
7175 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007176 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7177 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007178 LY_CHECK_RET(rc);
7179 }
7180
7181cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007182 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007183 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007184 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007185 }
Michal Vaskod3678892020-05-21 10:06:58 +02007186 return rc;
7187}
7188
7189/**
7190 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7191 *
7192 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7193 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7194 * [8] NodeType ::= 'text' | 'node'
7195 *
7196 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007197 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007198 * @param[in] attr_axis Whether to search attributes or standard nodes.
7199 * @param[in] all_desc Whether to search all the descendants or children only.
7200 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7201 * @param[in] options XPath options.
7202 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7203 */
7204static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007205eval_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 +02007206 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007207{
7208 LY_ERR rc;
7209
7210 /* TODO */
7211 (void)attr_axis;
7212 (void)all_desc;
7213
7214 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007215 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007216 if (set->type == LYXP_SET_SCNODE_SET) {
7217 set_scnode_clear_ctx(set);
7218 /* just for the debug message below */
7219 set = NULL;
7220 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007221 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007222 rc = xpath_node(NULL, 0, set, options);
7223 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007224 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007225 rc = xpath_text(NULL, 0, set, options);
7226 }
7227 LY_CHECK_RET(rc);
7228 }
7229 }
7230 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007231 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007232 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007233
7234 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007235 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007236 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007237 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007238 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007239
7240 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007241 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007242 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007243 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007244 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007245
7246 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007247 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7248 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007249 LY_CHECK_RET(rc);
7250 }
7251
7252 return LY_SUCCESS;
7253}
7254
7255/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007256 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7257 *
7258 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7259 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007260 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007261 *
7262 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007263 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007264 * @param[in] all_desc Whether to search all the descendants or children only.
7265 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7266 * @param[in] options XPath options.
7267 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7268 */
7269static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007270eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7271 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007272{
Radek Krejci857189e2020-09-01 13:26:36 +02007273 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007274 LY_ERR rc;
7275
7276 goto step;
7277 do {
7278 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007279 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007280 all_desc = 0;
7281 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007282 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007283 all_desc = 1;
7284 }
7285 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007286 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007287 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007288
7289step:
Michal Vaskod3678892020-05-21 10:06:58 +02007290 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007291 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007292 attr_axis = 1;
7293
7294 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007295 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007296 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007297 } else {
7298 attr_axis = 0;
7299 }
7300
Michal Vasko03ff5a72019-09-11 13:49:33 +02007301 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007302 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007303 case LYXP_TOKEN_DOT:
7304 /* evaluate '.' */
7305 if (set && (options & LYXP_SCNODE_ALL)) {
7306 rc = moveto_scnode_self(set, all_desc, options);
7307 } else {
7308 rc = moveto_self(set, all_desc, options);
7309 }
7310 LY_CHECK_RET(rc);
7311 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007312 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007313 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007314 break;
7315
7316 case LYXP_TOKEN_DDOT:
7317 /* evaluate '..' */
7318 if (set && (options & LYXP_SCNODE_ALL)) {
7319 rc = moveto_scnode_parent(set, all_desc, options);
7320 } else {
7321 rc = moveto_parent(set, all_desc, options);
7322 }
7323 LY_CHECK_RET(rc);
7324 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007325 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007326 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007327 break;
7328
Michal Vasko03ff5a72019-09-11 13:49:33 +02007329 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007330 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007331 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007332 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007333 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007334
Michal Vaskod3678892020-05-21 10:06:58 +02007335 case LYXP_TOKEN_NODETYPE:
7336 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007337 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007338 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007339 break;
7340
7341 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007342 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007343 }
Michal Vasko004d3152020-06-11 19:59:22 +02007344 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007345
7346 return LY_SUCCESS;
7347}
7348
7349/**
7350 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7351 *
7352 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7353 *
7354 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007355 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007356 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7357 * @param[in] options XPath options.
7358 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7359 */
7360static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007361eval_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 +02007362{
Radek Krejci857189e2020-09-01 13:26:36 +02007363 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007364
7365 if (set) {
7366 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007367 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007368 }
7369
7370 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007371 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007372 /* evaluate '/' - deferred */
7373 all_desc = 0;
7374 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007375 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007376 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007377
Michal Vasko004d3152020-06-11 19:59:22 +02007378 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007379 return LY_SUCCESS;
7380 }
Michal Vasko004d3152020-06-11 19:59:22 +02007381 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007382 case LYXP_TOKEN_DOT:
7383 case LYXP_TOKEN_DDOT:
7384 case LYXP_TOKEN_AT:
7385 case LYXP_TOKEN_NAMETEST:
7386 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007387 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007388 break;
7389 default:
7390 break;
7391 }
7392
Michal Vasko03ff5a72019-09-11 13:49:33 +02007393 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007394 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007395 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7396 all_desc = 1;
7397 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007398 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007399 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007400
Michal Vaskob0099a92020-08-31 14:55:23 +02007401 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007402 }
7403
7404 return LY_SUCCESS;
7405}
7406
7407/**
7408 * @brief Evaluate FunctionCall. Logs directly on error.
7409 *
Michal Vaskod3678892020-05-21 10:06:58 +02007410 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007411 *
7412 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007413 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007414 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7415 * @param[in] options XPath options.
7416 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7417 */
7418static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007419eval_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 +02007420{
7421 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007422
Radek Krejci1deb5be2020-08-26 16:43:36 +02007423 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007424 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007425 struct lyxp_set **args = NULL, **args_aux;
7426
7427 if (set) {
7428 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007429 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007430 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007431 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007432 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007433 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007434 xpath_func = &xpath_sum;
7435 }
7436 break;
7437 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007438 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007439 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007440 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007441 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007442 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007443 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007444 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007445 xpath_func = &xpath_true;
7446 }
7447 break;
7448 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007449 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007450 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007451 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007452 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007453 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007454 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007455 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007456 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007457 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007458 xpath_func = &xpath_deref;
7459 }
7460 break;
7461 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007462 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007463 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007464 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007465 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007466 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007467 xpath_func = &xpath_string;
7468 }
7469 break;
7470 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007471 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007472 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007473 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007474 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007475 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007476 xpath_func = &xpath_current;
7477 }
7478 break;
7479 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007480 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007481 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007482 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007483 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007484 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007485 xpath_func = &xpath_re_match;
7486 }
7487 break;
7488 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007489 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007490 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007491 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007492 xpath_func = &xpath_translate;
7493 }
7494 break;
7495 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007496 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007497 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007498 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007499 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007500 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007501 xpath_func = &xpath_bit_is_set;
7502 }
7503 break;
7504 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007505 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007506 xpath_func = &xpath_starts_with;
7507 }
7508 break;
7509 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007510 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007511 xpath_func = &xpath_derived_from;
7512 }
7513 break;
7514 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007515 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007516 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007517 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007518 xpath_func = &xpath_string_length;
7519 }
7520 break;
7521 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007522 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007523 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007524 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007525 xpath_func = &xpath_substring_after;
7526 }
7527 break;
7528 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007529 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007530 xpath_func = &xpath_substring_before;
7531 }
7532 break;
7533 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007534 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007535 xpath_func = &xpath_derived_from_or_self;
7536 }
7537 break;
7538 }
7539
7540 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007541 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 +02007542 return LY_EVALID;
7543 }
7544 }
7545
7546 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007547 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007548 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007549
7550 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007551 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007552 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007553 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007554 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007555
7556 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007557 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007558 if (set) {
7559 args = malloc(sizeof *args);
7560 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7561 arg_count = 1;
7562 args[0] = set_copy(set);
7563 if (!args[0]) {
7564 rc = LY_EMEM;
7565 goto cleanup;
7566 }
7567
Michal Vasko004d3152020-06-11 19:59:22 +02007568 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007569 LY_CHECK_GOTO(rc, cleanup);
7570 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007571 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007572 LY_CHECK_GOTO(rc, cleanup);
7573 }
7574 }
Michal Vasko004d3152020-06-11 19:59:22 +02007575 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007576 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007577 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007578 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007579
7580 if (set) {
7581 ++arg_count;
7582 args_aux = realloc(args, arg_count * sizeof *args);
7583 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7584 args = args_aux;
7585 args[arg_count - 1] = set_copy(set);
7586 if (!args[arg_count - 1]) {
7587 rc = LY_EMEM;
7588 goto cleanup;
7589 }
7590
Michal Vasko004d3152020-06-11 19:59:22 +02007591 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007592 LY_CHECK_GOTO(rc, cleanup);
7593 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007594 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007595 LY_CHECK_GOTO(rc, cleanup);
7596 }
7597 }
7598
7599 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007600 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007601 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007602 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007603 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007604
7605 if (set) {
7606 /* evaluate function */
7607 rc = xpath_func(args, arg_count, set, options);
7608
7609 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007610 /* merge all nodes from arg evaluations */
7611 for (i = 0; i < arg_count; ++i) {
7612 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007613 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007614 }
7615 }
7616 } else {
7617 rc = LY_SUCCESS;
7618 }
7619
7620cleanup:
7621 for (i = 0; i < arg_count; ++i) {
7622 lyxp_set_free(args[i]);
7623 }
7624 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007625 return rc;
7626}
7627
7628/**
7629 * @brief Evaluate Number. Logs directly on error.
7630 *
7631 * @param[in] ctx Context for errors.
7632 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007633 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007634 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7635 * @return LY_ERR
7636 */
7637static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007638eval_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 +02007639{
7640 long double num;
7641 char *endptr;
7642
7643 if (set) {
7644 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007645 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007646 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007647 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7648 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007649 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007650 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007651 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007652 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7653 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007654 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007655 return LY_EVALID;
7656 }
7657
7658 set_fill_number(set, num);
7659 }
7660
7661 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007662 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007663 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007664 return LY_SUCCESS;
7665}
7666
7667/**
7668 * @brief Evaluate PathExpr. Logs directly on error.
7669 *
Michal Vaskod3678892020-05-21 10:06:58 +02007670 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007671 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7672 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7673 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007674 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007675 *
7676 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007677 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007678 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7679 * @param[in] options XPath options.
7680 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7681 */
7682static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007683eval_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 +02007684{
Radek Krejci857189e2020-09-01 13:26:36 +02007685 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007686 LY_ERR rc;
7687
Michal Vasko004d3152020-06-11 19:59:22 +02007688 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007689 case LYXP_TOKEN_PAR1:
7690 /* '(' Expr ')' */
7691
7692 /* '(' */
7693 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007694 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007695 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007696
7697 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007698 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007699 LY_CHECK_RET(rc);
7700
7701 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007702 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007703 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007704 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007705 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007706
7707 parent_pos_pred = 0;
7708 goto predicate;
7709
7710 case LYXP_TOKEN_DOT:
7711 case LYXP_TOKEN_DDOT:
7712 case LYXP_TOKEN_AT:
7713 case LYXP_TOKEN_NAMETEST:
7714 case LYXP_TOKEN_NODETYPE:
7715 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007716 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007717 LY_CHECK_RET(rc);
7718 break;
7719
7720 case LYXP_TOKEN_FUNCNAME:
7721 /* FunctionCall */
7722 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007723 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007724 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007725 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007726 }
7727 LY_CHECK_RET(rc);
7728
7729 parent_pos_pred = 1;
7730 goto predicate;
7731
Michal Vasko3e48bf32020-06-01 08:39:07 +02007732 case LYXP_TOKEN_OPER_PATH:
7733 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007734 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007735 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007736 LY_CHECK_RET(rc);
7737 break;
7738
7739 case LYXP_TOKEN_LITERAL:
7740 /* Literal */
7741 if (!set || (options & LYXP_SCNODE_ALL)) {
7742 if (set) {
7743 set_scnode_clear_ctx(set);
7744 }
Michal Vasko004d3152020-06-11 19:59:22 +02007745 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007746 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007747 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007748 }
7749
7750 parent_pos_pred = 1;
7751 goto predicate;
7752
7753 case LYXP_TOKEN_NUMBER:
7754 /* Number */
7755 if (!set || (options & LYXP_SCNODE_ALL)) {
7756 if (set) {
7757 set_scnode_clear_ctx(set);
7758 }
Michal Vasko004d3152020-06-11 19:59:22 +02007759 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007760 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007761 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007762 }
7763 LY_CHECK_RET(rc);
7764
7765 parent_pos_pred = 1;
7766 goto predicate;
7767
7768 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01007769 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 +02007770 return LY_EVALID;
7771 }
7772
7773 return LY_SUCCESS;
7774
7775predicate:
7776 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007777 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7778 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007779 LY_CHECK_RET(rc);
7780 }
7781
7782 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007783 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007784
7785 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007786 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007787 all_desc = 0;
7788 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007789 all_desc = 1;
7790 }
7791
7792 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007793 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007794 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007795
Michal Vasko004d3152020-06-11 19:59:22 +02007796 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007797 LY_CHECK_RET(rc);
7798 }
7799
7800 return LY_SUCCESS;
7801}
7802
7803/**
7804 * @brief Evaluate UnionExpr. Logs directly on error.
7805 *
Michal Vaskod3678892020-05-21 10:06:58 +02007806 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007807 *
7808 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007809 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007810 * @param[in] repeat How many times this expression is repeated.
7811 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7812 * @param[in] options XPath options.
7813 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7814 */
7815static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007816eval_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 +02007817{
7818 LY_ERR rc = LY_SUCCESS;
7819 struct lyxp_set orig_set, set2;
7820 uint16_t i;
7821
7822 assert(repeat);
7823
7824 set_init(&orig_set, set);
7825 set_init(&set2, set);
7826
7827 set_fill_set(&orig_set, set);
7828
Michal Vasko004d3152020-06-11 19:59:22 +02007829 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007830 LY_CHECK_GOTO(rc, cleanup);
7831
7832 /* ('|' PathExpr)* */
7833 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007834 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007835 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007836 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007837 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007838
7839 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007840 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007841 LY_CHECK_GOTO(rc, cleanup);
7842 continue;
7843 }
7844
7845 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007846 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007847 LY_CHECK_GOTO(rc, cleanup);
7848
7849 /* eval */
7850 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007851 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007852 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007853 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007854 LY_CHECK_GOTO(rc, cleanup);
7855 }
7856 }
7857
7858cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007859 lyxp_set_free_content(&orig_set);
7860 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007861 return rc;
7862}
7863
7864/**
7865 * @brief Evaluate UnaryExpr. Logs directly on error.
7866 *
Michal Vaskod3678892020-05-21 10:06:58 +02007867 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007868 *
7869 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007870 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007871 * @param[in] repeat How many times this expression is repeated.
7872 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7873 * @param[in] options XPath options.
7874 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7875 */
7876static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007877eval_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 +02007878{
7879 LY_ERR rc;
7880 uint16_t this_op, i;
7881
7882 assert(repeat);
7883
7884 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007885 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007886 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007887 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 +02007888
7889 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007890 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007891 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007892 }
7893
Michal Vasko004d3152020-06-11 19:59:22 +02007894 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007895 LY_CHECK_RET(rc);
7896
7897 if (set && (repeat % 2)) {
7898 if (options & LYXP_SCNODE_ALL) {
7899 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7900 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007901 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007902 LY_CHECK_RET(rc);
7903 }
7904 }
7905
7906 return LY_SUCCESS;
7907}
7908
7909/**
7910 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7911 *
Michal Vaskod3678892020-05-21 10:06:58 +02007912 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007913 * | MultiplicativeExpr '*' UnaryExpr
7914 * | MultiplicativeExpr 'div' UnaryExpr
7915 * | MultiplicativeExpr 'mod' UnaryExpr
7916 *
7917 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007918 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007919 * @param[in] repeat How many times this expression is repeated.
7920 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7921 * @param[in] options XPath options.
7922 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7923 */
7924static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007925eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
7926 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007927{
7928 LY_ERR rc;
7929 uint16_t this_op;
7930 struct lyxp_set orig_set, set2;
7931 uint16_t i;
7932
7933 assert(repeat);
7934
7935 set_init(&orig_set, set);
7936 set_init(&set2, set);
7937
7938 set_fill_set(&orig_set, set);
7939
Michal Vasko004d3152020-06-11 19:59:22 +02007940 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007941 LY_CHECK_GOTO(rc, cleanup);
7942
7943 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7944 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007945 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007946
Michal Vasko004d3152020-06-11 19:59:22 +02007947 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007948 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007949 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007950 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007951
7952 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007953 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007954 LY_CHECK_GOTO(rc, cleanup);
7955 continue;
7956 }
7957
7958 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007959 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007960 LY_CHECK_GOTO(rc, cleanup);
7961
7962 /* eval */
7963 if (options & LYXP_SCNODE_ALL) {
7964 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007965 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007966 set_scnode_clear_ctx(set);
7967 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007968 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007969 LY_CHECK_GOTO(rc, cleanup);
7970 }
7971 }
7972
7973cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007974 lyxp_set_free_content(&orig_set);
7975 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007976 return rc;
7977}
7978
7979/**
7980 * @brief Evaluate AdditiveExpr. Logs directly on error.
7981 *
Michal Vaskod3678892020-05-21 10:06:58 +02007982 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007983 * | AdditiveExpr '+' MultiplicativeExpr
7984 * | AdditiveExpr '-' MultiplicativeExpr
7985 *
7986 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007987 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007988 * @param[in] repeat How many times this expression is repeated.
7989 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7990 * @param[in] options XPath options.
7991 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7992 */
7993static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007994eval_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 +02007995{
7996 LY_ERR rc;
7997 uint16_t this_op;
7998 struct lyxp_set orig_set, set2;
7999 uint16_t i;
8000
8001 assert(repeat);
8002
8003 set_init(&orig_set, set);
8004 set_init(&set2, set);
8005
8006 set_fill_set(&orig_set, set);
8007
Michal Vasko004d3152020-06-11 19:59:22 +02008008 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008009 LY_CHECK_GOTO(rc, cleanup);
8010
8011 /* ('+' / '-' MultiplicativeExpr)* */
8012 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008013 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008014
Michal Vasko004d3152020-06-11 19:59:22 +02008015 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008016 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008017 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008018 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008019
8020 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008021 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008022 LY_CHECK_GOTO(rc, cleanup);
8023 continue;
8024 }
8025
8026 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008027 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008028 LY_CHECK_GOTO(rc, cleanup);
8029
8030 /* eval */
8031 if (options & LYXP_SCNODE_ALL) {
8032 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008033 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008034 set_scnode_clear_ctx(set);
8035 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008036 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008037 LY_CHECK_GOTO(rc, cleanup);
8038 }
8039 }
8040
8041cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008042 lyxp_set_free_content(&orig_set);
8043 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008044 return rc;
8045}
8046
8047/**
8048 * @brief Evaluate RelationalExpr. Logs directly on error.
8049 *
Michal Vaskod3678892020-05-21 10:06:58 +02008050 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008051 * | RelationalExpr '<' AdditiveExpr
8052 * | RelationalExpr '>' AdditiveExpr
8053 * | RelationalExpr '<=' AdditiveExpr
8054 * | RelationalExpr '>=' AdditiveExpr
8055 *
8056 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008057 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008058 * @param[in] repeat How many times this expression is repeated.
8059 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8060 * @param[in] options XPath options.
8061 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8062 */
8063static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008064eval_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 +02008065{
8066 LY_ERR rc;
8067 uint16_t this_op;
8068 struct lyxp_set orig_set, set2;
8069 uint16_t i;
8070
8071 assert(repeat);
8072
8073 set_init(&orig_set, set);
8074 set_init(&set2, set);
8075
8076 set_fill_set(&orig_set, set);
8077
Michal Vasko004d3152020-06-11 19:59:22 +02008078 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008079 LY_CHECK_GOTO(rc, cleanup);
8080
8081 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8082 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008083 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008084
Michal Vasko004d3152020-06-11 19:59:22 +02008085 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008086 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008087 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008088 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008089
8090 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008091 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008092 LY_CHECK_GOTO(rc, cleanup);
8093 continue;
8094 }
8095
8096 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008097 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008098 LY_CHECK_GOTO(rc, cleanup);
8099
8100 /* eval */
8101 if (options & LYXP_SCNODE_ALL) {
8102 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008103 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008104 set_scnode_clear_ctx(set);
8105 } else {
8106 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8107 LY_CHECK_GOTO(rc, cleanup);
8108 }
8109 }
8110
8111cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008112 lyxp_set_free_content(&orig_set);
8113 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008114 return rc;
8115}
8116
8117/**
8118 * @brief Evaluate EqualityExpr. Logs directly on error.
8119 *
Michal Vaskod3678892020-05-21 10:06:58 +02008120 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008121 * | EqualityExpr '!=' RelationalExpr
8122 *
8123 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008124 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008125 * @param[in] repeat How many times this expression is repeated.
8126 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8127 * @param[in] options XPath options.
8128 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8129 */
8130static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008131eval_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 +02008132{
8133 LY_ERR rc;
8134 uint16_t this_op;
8135 struct lyxp_set orig_set, set2;
8136 uint16_t i;
8137
8138 assert(repeat);
8139
8140 set_init(&orig_set, set);
8141 set_init(&set2, set);
8142
8143 set_fill_set(&orig_set, set);
8144
Michal Vasko004d3152020-06-11 19:59:22 +02008145 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008146 LY_CHECK_GOTO(rc, cleanup);
8147
8148 /* ('=' / '!=' RelationalExpr)* */
8149 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008150 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008151
Michal Vasko004d3152020-06-11 19:59:22 +02008152 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008153 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008154 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008155 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008156
8157 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008158 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008159 LY_CHECK_GOTO(rc, cleanup);
8160 continue;
8161 }
8162
8163 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008164 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008165 LY_CHECK_GOTO(rc, cleanup);
8166
8167 /* eval */
8168 if (options & LYXP_SCNODE_ALL) {
8169 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008170 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8171 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008172 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008173 set_scnode_clear_ctx(set);
8174 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008175 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8176 LY_CHECK_GOTO(rc, cleanup);
8177 }
8178 }
8179
8180cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008181 lyxp_set_free_content(&orig_set);
8182 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008183 return rc;
8184}
8185
8186/**
8187 * @brief Evaluate AndExpr. Logs directly on error.
8188 *
Michal Vaskod3678892020-05-21 10:06:58 +02008189 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008190 *
8191 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008192 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008193 * @param[in] repeat How many times this expression is repeated.
8194 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8195 * @param[in] options XPath options.
8196 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8197 */
8198static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008199eval_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 +02008200{
8201 LY_ERR rc;
8202 struct lyxp_set orig_set, set2;
8203 uint16_t i;
8204
8205 assert(repeat);
8206
8207 set_init(&orig_set, set);
8208 set_init(&set2, set);
8209
8210 set_fill_set(&orig_set, set);
8211
Michal Vasko004d3152020-06-11 19:59:22 +02008212 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008213 LY_CHECK_GOTO(rc, cleanup);
8214
8215 /* cast to boolean, we know that will be the final result */
8216 if (set && (options & LYXP_SCNODE_ALL)) {
8217 set_scnode_clear_ctx(set);
8218 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008219 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008220 }
8221
8222 /* ('and' EqualityExpr)* */
8223 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008224 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8225 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008226 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008227 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008228
8229 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008230 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8231 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008232 LY_CHECK_GOTO(rc, cleanup);
8233 continue;
8234 }
8235
8236 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008237 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008238 LY_CHECK_GOTO(rc, cleanup);
8239
8240 /* eval - just get boolean value actually */
8241 if (set->type == LYXP_SET_SCNODE_SET) {
8242 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008243 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008244 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008245 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008246 set_fill_set(set, &set2);
8247 }
8248 }
8249
8250cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008251 lyxp_set_free_content(&orig_set);
8252 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008253 return rc;
8254}
8255
8256/**
8257 * @brief Evaluate OrExpr. Logs directly on error.
8258 *
Michal Vaskod3678892020-05-21 10:06:58 +02008259 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008260 *
8261 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008262 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008263 * @param[in] repeat How many times this expression is repeated.
8264 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8265 * @param[in] options XPath options.
8266 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8267 */
8268static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008269eval_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 +02008270{
8271 LY_ERR rc;
8272 struct lyxp_set orig_set, set2;
8273 uint16_t i;
8274
8275 assert(repeat);
8276
8277 set_init(&orig_set, set);
8278 set_init(&set2, set);
8279
8280 set_fill_set(&orig_set, set);
8281
Michal Vasko004d3152020-06-11 19:59:22 +02008282 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008283 LY_CHECK_GOTO(rc, cleanup);
8284
8285 /* cast to boolean, we know that will be the final result */
8286 if (set && (options & LYXP_SCNODE_ALL)) {
8287 set_scnode_clear_ctx(set);
8288 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008289 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008290 }
8291
8292 /* ('or' AndExpr)* */
8293 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008294 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8295 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008296 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008297 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008298
8299 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008300 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8301 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008302 LY_CHECK_GOTO(rc, cleanup);
8303 continue;
8304 }
8305
8306 set_fill_set(&set2, &orig_set);
8307 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8308 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008309 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008310 LY_CHECK_GOTO(rc, cleanup);
8311
8312 /* eval - just get boolean value actually */
8313 if (set->type == LYXP_SET_SCNODE_SET) {
8314 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008315 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008316 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008317 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008318 set_fill_set(set, &set2);
8319 }
8320 }
8321
8322cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008323 lyxp_set_free_content(&orig_set);
8324 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008325 return rc;
8326}
8327
8328/**
Michal Vasko004d3152020-06-11 19:59:22 +02008329 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008330 *
8331 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008332 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008333 * @param[in] etype Expression type to evaluate.
8334 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8335 * @param[in] options XPath options.
8336 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8337 */
8338static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008339eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8340 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008341{
8342 uint16_t i, count;
8343 enum lyxp_expr_type next_etype;
8344 LY_ERR rc;
8345
8346 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008347 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008348 next_etype = LYXP_EXPR_NONE;
8349 } else {
8350 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008351 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008352
8353 /* select one-priority lower because etype expression called us */
8354 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008355 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008356 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008357 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008358 } else {
8359 next_etype = LYXP_EXPR_NONE;
8360 }
8361 }
8362
8363 /* decide what expression are we parsing based on the repeat */
8364 switch (next_etype) {
8365 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008366 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008367 break;
8368 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008369 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008370 break;
8371 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008372 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008373 break;
8374 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008375 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008376 break;
8377 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008378 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008379 break;
8380 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008381 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008382 break;
8383 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008384 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008385 break;
8386 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008387 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008388 break;
8389 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008390 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008391 break;
8392 default:
8393 LOGINT_RET(set->ctx);
8394 }
8395
8396 return rc;
8397}
8398
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008399/**
8400 * @brief Get root type.
8401 *
8402 * @param[in] ctx_node Context node.
8403 * @param[in] ctx_scnode Schema context node.
8404 * @param[in] options XPath options.
8405 * @return Root type.
8406 */
8407static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008408lyxp_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 +01008409{
Michal Vasko6b26e742020-07-17 15:02:10 +02008410 const struct lysc_node *op;
8411
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008412 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008413 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008414 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008415
8416 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008417 /* general root that can access everything */
8418 return LYXP_NODE_ROOT;
8419 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8420 /* root context node can access only config data (because we said so, it is unspecified) */
8421 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008422 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008423 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008424 }
8425
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008426 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008427 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008428 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008429
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008430 if (op || !(options & LYXP_SCHEMA)) {
8431 /* general root that can access everything */
8432 return LYXP_NODE_ROOT;
8433 } else if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008434 /* root context node can access only config data (because we said so, it is unspecified) */
8435 return LYXP_NODE_ROOT_CONFIG;
8436 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008437 return LYXP_NODE_ROOT;
8438}
8439
Michal Vasko03ff5a72019-09-11 13:49:33 +02008440LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008441lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
8442 LY_PREFIX_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node, const struct lyd_node *tree,
8443 struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008444{
Michal Vasko004d3152020-06-11 19:59:22 +02008445 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008446 LY_ERR rc;
8447
Michal Vasko400e9672021-01-11 13:39:17 +01008448 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008449 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8450 LOGARG(NULL, "Current module must be set if schema format is used.");
8451 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008452 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008453
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008454 if (tree) {
8455 /* adjust the pointer to be the first top-level sibling */
8456 while (tree->parent) {
8457 tree = lyd_parent(tree);
8458 }
8459 tree = lyd_first_sibling(tree);
8460 }
8461
Michal Vasko03ff5a72019-09-11 13:49:33 +02008462 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008463 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008464 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008465 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8466 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8467
Michal Vasko400e9672021-01-11 13:39:17 +01008468 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008469 set->cur_node = ctx_node;
8470 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008471 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8472 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008473 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008474 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008475 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008476 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008477
Radek Krejciddace2c2021-01-08 11:30:56 +01008478 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008479
Michal Vasko03ff5a72019-09-11 13:49:33 +02008480 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008481 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008482 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008483 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008484 }
8485
Radek Krejciddace2c2021-01-08 11:30:56 +01008486 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008487 return rc;
8488}
8489
8490#if 0
8491
8492/* full xml printing of set elements, not used currently */
8493
8494void
8495lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8496{
8497 uint32_t i;
8498 char *str_num;
8499 struct lyout out;
8500
8501 memset(&out, 0, sizeof out);
8502
8503 out.type = LYOUT_STREAM;
8504 out.method.f = f;
8505
8506 switch (set->type) {
8507 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008508 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008509 break;
8510 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008511 ly_print_(&out, "Boolean XPath set:\n");
8512 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008513 break;
8514 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008515 ly_print_(&out, "String XPath set:\n");
8516 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008517 break;
8518 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008519 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008520
8521 if (isnan(set->value.num)) {
8522 str_num = strdup("NaN");
8523 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8524 str_num = strdup("0");
8525 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8526 str_num = strdup("Infinity");
8527 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8528 str_num = strdup("-Infinity");
8529 } else if ((long long)set->value.num == set->value.num) {
8530 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8531 str_num = NULL;
8532 }
8533 } else {
8534 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8535 str_num = NULL;
8536 }
8537 }
8538 if (!str_num) {
8539 LOGMEM;
8540 return;
8541 }
Michal Vasko5233e962020-08-14 14:26:20 +02008542 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008543 free(str_num);
8544 break;
8545 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008546 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008547
8548 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008549 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008550 switch (set->node_type[i]) {
8551 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008552 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008553 break;
8554 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008555 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008556 break;
8557 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008558 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008559 break;
8560 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008561 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008562 break;
8563 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008564 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008565 break;
8566 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008567 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008568 break;
8569 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008570 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008571 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008572 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008573 break;
8574 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008575 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 +02008576 break;
8577 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008578 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 +02008579 break;
8580 }
8581 }
8582 break;
8583 }
8584}
8585
8586#endif
8587
8588LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008589lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008590{
8591 long double num;
8592 char *str;
8593 LY_ERR rc;
8594
8595 if (!set || (set->type == target)) {
8596 return LY_SUCCESS;
8597 }
8598
8599 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008600 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008601
8602 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008603 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008604 return LY_EINVAL;
8605 }
8606
8607 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008608 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008609 switch (set->type) {
8610 case LYXP_SET_NUMBER:
8611 if (isnan(set->val.num)) {
8612 set->val.str = strdup("NaN");
8613 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8614 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8615 set->val.str = strdup("0");
8616 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8617 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8618 set->val.str = strdup("Infinity");
8619 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8620 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8621 set->val.str = strdup("-Infinity");
8622 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8623 } else if ((long long)set->val.num == set->val.num) {
8624 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8625 LOGMEM_RET(set->ctx);
8626 }
8627 set->val.str = str;
8628 } else {
8629 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8630 LOGMEM_RET(set->ctx);
8631 }
8632 set->val.str = str;
8633 }
8634 break;
8635 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008636 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008637 set->val.str = strdup("true");
8638 } else {
8639 set->val.str = strdup("false");
8640 }
8641 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8642 break;
8643 case LYXP_SET_NODE_SET:
8644 assert(set->used);
8645
8646 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008647 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008648
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008649 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008650 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008651 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008652 set->val.str = str;
8653 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008654 default:
8655 LOGINT_RET(set->ctx);
8656 }
8657 set->type = LYXP_SET_STRING;
8658 }
8659
8660 /* to NUMBER */
8661 if (target == LYXP_SET_NUMBER) {
8662 switch (set->type) {
8663 case LYXP_SET_STRING:
8664 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008665 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008666 set->val.num = num;
8667 break;
8668 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008669 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008670 set->val.num = 1;
8671 } else {
8672 set->val.num = 0;
8673 }
8674 break;
8675 default:
8676 LOGINT_RET(set->ctx);
8677 }
8678 set->type = LYXP_SET_NUMBER;
8679 }
8680
8681 /* to BOOLEAN */
8682 if (target == LYXP_SET_BOOLEAN) {
8683 switch (set->type) {
8684 case LYXP_SET_NUMBER:
8685 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008686 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008687 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008688 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008689 }
8690 break;
8691 case LYXP_SET_STRING:
8692 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008693 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008694 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008695 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008696 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008697 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008698 }
8699 break;
8700 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008701 if (set->used) {
8702 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008703 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008704 } else {
8705 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008706 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008707 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008708 break;
8709 default:
8710 LOGINT_RET(set->ctx);
8711 }
8712 set->type = LYXP_SET_BOOLEAN;
8713 }
8714
Michal Vasko03ff5a72019-09-11 13:49:33 +02008715 return LY_SUCCESS;
8716}
8717
8718LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008719lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
8720 LY_PREFIX_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
8721 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008722{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008723 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008724 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008725
Michal Vasko400e9672021-01-11 13:39:17 +01008726 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008727 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8728 LOGARG(NULL, "Current module must be set if schema format is used.");
8729 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008730 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008731
8732 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008733 memset(set, 0, sizeof *set);
8734 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008735 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8736 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 +01008737 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008738
Michal Vasko400e9672021-01-11 13:39:17 +01008739 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008740 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008741 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008742 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8743 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008744 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008745 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008746 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008747
Radek Krejciddace2c2021-01-08 11:30:56 +01008748 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008749
Michal Vasko03ff5a72019-09-11 13:49:33 +02008750 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01008751 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
8752
Radek Krejciddace2c2021-01-08 11:30:56 +01008753 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008754 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008755}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008756
8757API const char *
8758lyxp_get_expr(const struct lyxp_expr *path)
8759{
8760 if (!path) {
8761 return NULL;
8762 }
8763
8764 return path->expr;
8765}