blob: 8f10af72b6619e22b4643c644ea9bc554fab6e6b [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 Vasko69730152020-10-09 16:30:07 +0200196 if ((item->node->schema->nodetype == LYS_LIST) &&
197 (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200198 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200199 item->node->schema->name, LYD_CANON_VALUE(lyd_child(item->node)));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200200 } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) {
201 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200202 item->node->schema->name, LYD_CANON_VALUE(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200203 } else {
204 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
205 }
206 break;
207 case LYXP_NODE_TEXT:
208 if (item->node->schema->nodetype & LYS_ANYDATA) {
209 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200210 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200211 } else {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200212 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 +0200213 }
214 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100215 case LYXP_NODE_META:
216 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 +0200217 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200218 break;
219 }
220 }
221 break;
222
223 case LYXP_SET_SCNODE_SET:
224 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
225 for (i = 0; i < set->used; ++i) {
226 sitem = &set->val.scnodes[i];
227
228 switch (sitem->type) {
229 case LYXP_NODE_ROOT:
230 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
231 break;
232 case LYXP_NODE_ROOT_CONFIG:
233 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
234 break;
235 case LYXP_NODE_ELEM:
236 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
237 break;
238 default:
239 LOGINT(NULL);
240 break;
241 }
242 }
243 break;
244
Michal Vasko03ff5a72019-09-11 13:49:33 +0200245 case LYXP_SET_BOOLEAN:
246 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200247 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200248 break;
249
250 case LYXP_SET_STRING:
251 LOGDBG(LY_LDGXPATH, "set STRING");
252 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
253 break;
254
255 case LYXP_SET_NUMBER:
256 LOGDBG(LY_LDGXPATH, "set NUMBER");
257
258 if (isnan(set->val.num)) {
259 str = strdup("NaN");
260 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
261 str = strdup("0");
262 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
263 str = strdup("Infinity");
264 } else if (isinf(set->val.num) && signbit(set->val.num)) {
265 str = strdup("-Infinity");
266 } else if ((long long)set->val.num == set->val.num) {
267 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
268 str = NULL;
269 }
270 } else {
271 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
272 str = NULL;
273 }
274 }
275 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
276
277 LOGDBG(LY_LDGXPATH, "\t%s", str);
278 free(str);
279 }
280}
281
282#endif
283
284/**
285 * @brief Realloc the string \p str.
286 *
287 * @param[in] ctx libyang context for logging.
288 * @param[in] needed How much free space is required.
289 * @param[in,out] str Pointer to the string to use.
290 * @param[in,out] used Used bytes in \p str.
291 * @param[in,out] size Allocated bytes in \p str.
292 * @return LY_ERR
293 */
294static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100295cast_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 +0200296{
297 if (*size - *used < needed) {
298 do {
299 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
300 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
301 return LY_EINVAL;
302 }
303 *size += LYXP_STRING_CAST_SIZE_STEP;
304 } while (*size - *used < needed);
305 *str = ly_realloc(*str, *size * sizeof(char));
306 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
307 }
308
309 return LY_SUCCESS;
310}
311
312/**
313 * @brief Cast nodes recursively to one string @p str.
314 *
315 * @param[in] node Node to cast.
316 * @param[in] fake_cont Whether to put the data into a "fake" container.
317 * @param[in] root_type Type of the XPath root.
318 * @param[in] indent Current indent.
319 * @param[in,out] str Resulting string.
320 * @param[in,out] used Used bytes in @p str.
321 * @param[in,out] size Allocated bytes in @p str.
322 * @return LY_ERR
323 */
324static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200325cast_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 +0200326 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200327{
Radek Krejci7f769d72020-07-11 23:13:56 +0200328 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200329 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200330 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200331 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200332 struct lyd_node_any *any;
333 LY_ERR rc;
334
335 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
336 return LY_SUCCESS;
337 }
338
339 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200340 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200341 LY_CHECK_RET(rc);
342 strcpy(*str + (*used - 1), "\n");
343 ++(*used);
344
345 ++indent;
346 }
347
348 switch (node->schema->nodetype) {
349 case LYS_CONTAINER:
350 case LYS_LIST:
351 case LYS_RPC:
352 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200353 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200354 LY_CHECK_RET(rc);
355 strcpy(*str + (*used - 1), "\n");
356 ++(*used);
357
Radek Krejcia1c1e542020-09-29 16:06:52 +0200358 for (child = lyd_child(node); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200359 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
360 LY_CHECK_RET(rc);
361 }
362
363 break;
364
365 case LYS_LEAF:
366 case LYS_LEAFLIST:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200367 value_str = LYD_CANON_VALUE(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200368
369 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200370 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 +0200371 memset(*str + (*used - 1), ' ', indent * 2);
372 *used += indent * 2;
373
374 /* print value */
375 if (*used == 1) {
376 sprintf(*str + (*used - 1), "%s", value_str);
377 *used += strlen(value_str);
378 } else {
379 sprintf(*str + (*used - 1), "%s\n", value_str);
380 *used += strlen(value_str) + 1;
381 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200382
383 break;
384
385 case LYS_ANYXML:
386 case LYS_ANYDATA:
387 any = (struct lyd_node_any *)node;
388 if (!(void *)any->value.tree) {
389 /* no content */
390 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200391 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200392 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200393 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100394
Michal Vasko60ea6352020-06-29 13:39:39 +0200395 if (any->value_type == LYD_ANYDATA_LYB) {
396 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200397 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 +0200398 /* successfully parsed */
399 free(any->value.mem);
400 any->value.tree = tree;
401 any->value_type = LYD_ANYDATA_DATATREE;
402 }
Radek Krejci7931b192020-06-25 17:05:03 +0200403 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200404 }
405
Michal Vasko03ff5a72019-09-11 13:49:33 +0200406 switch (any->value_type) {
407 case LYD_ANYDATA_STRING:
408 case LYD_ANYDATA_XML:
409 case LYD_ANYDATA_JSON:
410 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200411 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200412 break;
413 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200414 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200415 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200416 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100417 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200418 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200419 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200420 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200421 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200422 }
423 }
424
425 line = strtok_r(buf, "\n", &ptr);
426 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200427 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200428 if (rc != LY_SUCCESS) {
429 free(buf);
430 return rc;
431 }
432 memset(*str + (*used - 1), ' ', indent * 2);
433 *used += indent * 2;
434
435 strcpy(*str + (*used - 1), line);
436 *used += strlen(line);
437
438 strcpy(*str + (*used - 1), "\n");
439 *used += 1;
440 } while ((line = strtok_r(NULL, "\n", &ptr)));
441
442 free(buf);
443 break;
444
445 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200446 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200447 }
448
449 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200450 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200451 LY_CHECK_RET(rc);
452 strcpy(*str + (*used - 1), "\n");
453 ++(*used);
454
455 --indent;
456 }
457
458 return LY_SUCCESS;
459}
460
461/**
462 * @brief Cast an element into a string.
463 *
464 * @param[in] node Node to cast.
465 * @param[in] fake_cont Whether to put the data into a "fake" container.
466 * @param[in] root_type Type of the XPath root.
467 * @param[out] str Element cast to dynamically-allocated string.
468 * @return LY_ERR
469 */
470static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200471cast_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 +0200472{
473 uint16_t used, size;
474 LY_ERR rc;
475
476 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200477 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200478 (*str)[0] = '\0';
479 used = 1;
480 size = LYXP_STRING_CAST_SIZE_START;
481
482 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
483 if (rc != LY_SUCCESS) {
484 free(*str);
485 return rc;
486 }
487
488 if (size > used) {
489 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200490 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200491 }
492 return LY_SUCCESS;
493}
494
495/**
496 * @brief Cast a LYXP_SET_NODE_SET set into a string.
497 * Context position aware.
498 *
499 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200500 * @param[out] str Cast dynamically-allocated string.
501 * @return LY_ERR
502 */
503static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100504cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200505{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200506 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100507 case LYXP_NODE_NONE:
508 /* invalid */
509 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200510 case LYXP_NODE_ROOT:
511 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100512 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200513 case LYXP_NODE_ELEM:
514 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100515 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100516 case LYXP_NODE_META:
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200517 *str = strdup(set->val.meta[0].meta->value.canonical);
518 if (!*str) {
519 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200520 }
521 return LY_SUCCESS;
522 }
523
524 LOGINT_RET(set->ctx);
525}
526
527/**
528 * @brief Cast a string into an XPath number.
529 *
530 * @param[in] str String to use.
531 * @return Cast number.
532 */
533static long double
534cast_string_to_number(const char *str)
535{
536 long double num;
537 char *ptr;
538
539 errno = 0;
540 num = strtold(str, &ptr);
541 if (errno || *ptr) {
542 num = NAN;
543 }
544 return num;
545}
546
547/**
548 * @brief Callback for checking value equality.
549 *
Radek Krejci857189e2020-09-01 13:26:36 +0200550 * Implementation of ::values_equal_cb.
551 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200552 * @param[in] val1_p First value.
553 * @param[in] val2_p Second value.
554 * @param[in] mod Whether hash table is being modified.
555 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200556 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200557 */
Radek Krejci857189e2020-09-01 13:26:36 +0200558static ly_bool
559set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200560{
561 struct lyxp_set_hash_node *val1, *val2;
562
563 val1 = (struct lyxp_set_hash_node *)val1_p;
564 val2 = (struct lyxp_set_hash_node *)val2_p;
565
566 if ((val1->node == val2->node) && (val1->type == val2->type)) {
567 return 1;
568 }
569
570 return 0;
571}
572
573/**
574 * @brief Insert node and its hash into set.
575 *
576 * @param[in] set et to insert to.
577 * @param[in] node Node with hash.
578 * @param[in] type Node type.
579 */
580static void
581set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
582{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200583 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200584 uint32_t i, hash;
585 struct lyxp_set_hash_node hnode;
586
587 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
588 /* create hash table and add all the nodes */
589 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
590 for (i = 0; i < set->used; ++i) {
591 hnode.node = set->val.nodes[i].node;
592 hnode.type = set->val.nodes[i].type;
593
594 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
595 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
596 hash = dict_hash_multi(hash, NULL, 0);
597
598 r = lyht_insert(set->ht, &hnode, hash, NULL);
599 assert(!r);
600 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200601
Michal Vasko9d6befd2019-12-11 14:56:56 +0100602 if (hnode.node == node) {
603 /* it was just added, do not add it twice */
604 node = NULL;
605 }
606 }
607 }
608
609 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200610 /* add the new node into hash table */
611 hnode.node = node;
612 hnode.type = type;
613
614 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
615 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
616 hash = dict_hash_multi(hash, NULL, 0);
617
618 r = lyht_insert(set->ht, &hnode, hash, NULL);
619 assert(!r);
620 (void)r;
621 }
622}
623
624/**
625 * @brief Remove node and its hash from set.
626 *
627 * @param[in] set Set to remove from.
628 * @param[in] node Node to remove.
629 * @param[in] type Node type.
630 */
631static void
632set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
633{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200634 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200635 struct lyxp_set_hash_node hnode;
636 uint32_t hash;
637
638 if (set->ht) {
639 hnode.node = node;
640 hnode.type = type;
641
642 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
643 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
644 hash = dict_hash_multi(hash, NULL, 0);
645
646 r = lyht_remove(set->ht, &hnode, hash);
647 assert(!r);
648 (void)r;
649
650 if (!set->ht->used) {
651 lyht_free(set->ht);
652 set->ht = NULL;
653 }
654 }
655}
656
657/**
658 * @brief Check whether node is in set based on its hash.
659 *
660 * @param[in] set Set to search in.
661 * @param[in] node Node to search for.
662 * @param[in] type Node type.
663 * @param[in] skip_idx Index in @p set to skip.
664 * @return LY_ERR
665 */
666static LY_ERR
667set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
668{
669 struct lyxp_set_hash_node hnode, *match_p;
670 uint32_t hash;
671
672 hnode.node = node;
673 hnode.type = type;
674
675 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
676 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
677 hash = dict_hash_multi(hash, NULL, 0);
678
679 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
680 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
681 /* we found it on the index that should be skipped, find another */
682 hnode = *match_p;
683 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
684 /* none other found */
685 return LY_SUCCESS;
686 }
687 }
688
689 return LY_EEXIST;
690 }
691
692 /* not found */
693 return LY_SUCCESS;
694}
695
Michal Vaskod3678892020-05-21 10:06:58 +0200696void
697lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200698{
699 if (!set) {
700 return;
701 }
702
703 if (set->type == LYXP_SET_NODE_SET) {
704 free(set->val.nodes);
705 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200706 } else if (set->type == LYXP_SET_SCNODE_SET) {
707 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200708 lyht_free(set->ht);
709 } else {
710 if (set->type == LYXP_SET_STRING) {
711 free(set->val.str);
712 }
713 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200714 }
Michal Vaskod3678892020-05-21 10:06:58 +0200715
716 set->val.nodes = NULL;
717 set->used = 0;
718 set->size = 0;
719 set->ht = NULL;
720 set->ctx_pos = 0;
721 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200722}
723
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100724/**
725 * @brief Free dynamically-allocated set.
726 *
727 * @param[in] set Set to free.
728 */
729static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200730lyxp_set_free(struct lyxp_set *set)
731{
732 if (!set) {
733 return;
734 }
735
Michal Vaskod3678892020-05-21 10:06:58 +0200736 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200737 free(set);
738}
739
740/**
741 * @brief Initialize set context.
742 *
743 * @param[in] new Set to initialize.
744 * @param[in] set Arbitrary initialized set.
745 */
746static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200747set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200748{
749 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200750 if (set) {
751 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200752 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100753 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200754 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100755 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200756 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200757 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200758 new->prefix_data = set->prefix_data;
Michal Vasko02a77382019-09-12 11:47:35 +0200759 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200760}
761
762/**
763 * @brief Create a deep copy of a set.
764 *
765 * @param[in] set Set to copy.
766 * @return Copy of @p set.
767 */
768static struct lyxp_set *
769set_copy(struct lyxp_set *set)
770{
771 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100772 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200773
774 if (!set) {
775 return NULL;
776 }
777
778 ret = malloc(sizeof *ret);
779 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
780 set_init(ret, set);
781
782 if (set->type == LYXP_SET_SCNODE_SET) {
783 ret->type = set->type;
784
785 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100786 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
787 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200788 uint32_t idx;
789 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
790 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100791 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200792 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200793 lyxp_set_free(ret);
794 return NULL;
795 }
Michal Vaskoba716542019-12-16 10:01:58 +0100796 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200797 }
798 }
799 } else if (set->type == LYXP_SET_NODE_SET) {
800 ret->type = set->type;
801 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
802 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
803 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
804
805 ret->used = ret->size = set->used;
806 ret->ctx_pos = set->ctx_pos;
807 ret->ctx_size = set->ctx_size;
808 ret->ht = lyht_dup(set->ht);
809 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200810 memcpy(ret, set, sizeof *ret);
811 if (set->type == LYXP_SET_STRING) {
812 ret->val.str = strdup(set->val.str);
813 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
814 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200815 }
816
817 return ret;
818}
819
820/**
821 * @brief Fill XPath set with a string. Any current data are disposed of.
822 *
823 * @param[in] set Set to fill.
824 * @param[in] string String to fill into \p set.
825 * @param[in] str_len Length of \p string. 0 is a valid value!
826 */
827static void
828set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
829{
Michal Vaskod3678892020-05-21 10:06:58 +0200830 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200831
832 set->type = LYXP_SET_STRING;
833 if ((str_len == 0) && (string[0] != '\0')) {
834 string = "";
835 }
836 set->val.str = strndup(string, str_len);
837}
838
839/**
840 * @brief Fill XPath set with a number. Any current data are disposed of.
841 *
842 * @param[in] set Set to fill.
843 * @param[in] number Number to fill into \p set.
844 */
845static void
846set_fill_number(struct lyxp_set *set, long double number)
847{
Michal Vaskod3678892020-05-21 10:06:58 +0200848 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200849
850 set->type = LYXP_SET_NUMBER;
851 set->val.num = number;
852}
853
854/**
855 * @brief Fill XPath set with a boolean. Any current data are disposed of.
856 *
857 * @param[in] set Set to fill.
858 * @param[in] boolean Boolean to fill into \p set.
859 */
860static void
Radek Krejci857189e2020-09-01 13:26:36 +0200861set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200862{
Michal Vaskod3678892020-05-21 10:06:58 +0200863 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200864
865 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200866 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200867}
868
869/**
870 * @brief Fill XPath set with the value from another set (deep assign).
871 * Any current data are disposed of.
872 *
873 * @param[in] trg Set to fill.
874 * @param[in] src Source set to copy into \p trg.
875 */
876static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200877set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200878{
879 if (!trg || !src) {
880 return;
881 }
882
883 if (trg->type == LYXP_SET_NODE_SET) {
884 free(trg->val.nodes);
885 } else if (trg->type == LYXP_SET_STRING) {
886 free(trg->val.str);
887 }
888 set_init(trg, src);
889
890 if (src->type == LYXP_SET_SCNODE_SET) {
891 trg->type = LYXP_SET_SCNODE_SET;
892 trg->used = src->used;
893 trg->size = src->used;
894
895 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
896 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
897 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
898 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200899 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200900 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200901 set_fill_number(trg, src->val.num);
902 } else if (src->type == LYXP_SET_STRING) {
903 set_fill_string(trg, src->val.str, strlen(src->val.str));
904 } else {
905 if (trg->type == LYXP_SET_NODE_SET) {
906 free(trg->val.nodes);
907 } else if (trg->type == LYXP_SET_STRING) {
908 free(trg->val.str);
909 }
910
Michal Vaskod3678892020-05-21 10:06:58 +0200911 assert(src->type == LYXP_SET_NODE_SET);
912
913 trg->type = LYXP_SET_NODE_SET;
914 trg->used = src->used;
915 trg->size = src->used;
916 trg->ctx_pos = src->ctx_pos;
917 trg->ctx_size = src->ctx_size;
918
919 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
920 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
921 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
922 if (src->ht) {
923 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200924 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200925 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200926 }
927 }
928}
929
930/**
931 * @brief Clear context of all schema nodes.
932 *
933 * @param[in] set Set to clear.
934 */
935static void
936set_scnode_clear_ctx(struct lyxp_set *set)
937{
938 uint32_t i;
939
940 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100941 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
942 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
943 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
944 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200945 }
946 }
947}
948
949/**
950 * @brief Remove a node from a set. Removing last node changes
951 * set into LYXP_SET_EMPTY. Context position aware.
952 *
953 * @param[in] set Set to use.
954 * @param[in] idx Index from @p set of the node to be removed.
955 */
956static void
957set_remove_node(struct lyxp_set *set, uint32_t idx)
958{
959 assert(set && (set->type == LYXP_SET_NODE_SET));
960 assert(idx < set->used);
961
962 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
963
964 --set->used;
965 if (set->used) {
966 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
967 (set->used - idx) * sizeof *set->val.nodes);
968 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200969 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200970 }
971}
972
973/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100974 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200975 *
976 * @param[in] set Set to use.
977 * @param[in] idx Index from @p set of the node to be removed.
978 */
979static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100980set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200981{
982 assert(set && (set->type == LYXP_SET_NODE_SET));
983 assert(idx < set->used);
984
Michal Vasko2caefc12019-11-14 16:07:56 +0100985 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
986 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
987 }
988 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +0200989}
990
991/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100992 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +0200993 * set into LYXP_SET_EMPTY. Context position aware.
994 *
995 * @param[in] set Set to consolidate.
996 */
997static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100998set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +0200999{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001000 uint32_t i, orig_used, end = 0;
1001 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001002
Michal Vaskod3678892020-05-21 10:06:58 +02001003 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001004
1005 orig_used = set->used;
1006 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001007 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001008 start = -1;
1009 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001010 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001011 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001012 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001013 end = i;
1014 ++i;
1015 break;
1016 }
1017
1018 ++i;
1019 if (i == orig_used) {
1020 end = i;
1021 }
1022 } while (i < orig_used);
1023
1024 if (start > -1) {
1025 /* move the whole chunk of valid nodes together */
1026 if (set->used != (unsigned)start) {
1027 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1028 }
1029 set->used += end - start;
1030 }
1031 }
Michal Vasko57eab132019-09-24 11:46:26 +02001032}
1033
1034/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001035 * @brief Check for duplicates in a node set.
1036 *
1037 * @param[in] set Set to check.
1038 * @param[in] node Node to look for in @p set.
1039 * @param[in] node_type Type of @p node.
1040 * @param[in] skip_idx Index from @p set to skip.
1041 * @return LY_ERR
1042 */
1043static LY_ERR
1044set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1045{
1046 uint32_t i;
1047
Michal Vasko2caefc12019-11-14 16:07:56 +01001048 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001049 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1050 }
1051
1052 for (i = 0; i < set->used; ++i) {
1053 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1054 continue;
1055 }
1056
1057 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1058 return LY_EEXIST;
1059 }
1060 }
1061
1062 return LY_SUCCESS;
1063}
1064
Radek Krejci857189e2020-09-01 13:26:36 +02001065ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001066lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1067 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001068{
1069 uint32_t i;
1070
1071 for (i = 0; i < set->used; ++i) {
1072 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1073 continue;
1074 }
1075
1076 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001077 if (index_p) {
1078 *index_p = i;
1079 }
1080 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001081 }
1082 }
1083
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001084 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001085}
1086
Michal Vaskoecd62de2019-11-13 12:35:11 +01001087void
1088lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001089{
1090 uint32_t orig_used, i, j;
1091
Michal Vaskod3678892020-05-21 10:06:58 +02001092 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001093
Michal Vaskod3678892020-05-21 10:06:58 +02001094 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001095 return;
1096 }
1097
Michal Vaskod3678892020-05-21 10:06:58 +02001098 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001099 memcpy(set1, set2, sizeof *set1);
1100 return;
1101 }
1102
1103 if (set1->used + set2->used > set1->size) {
1104 set1->size = set1->used + set2->used;
1105 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1106 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1107 }
1108
1109 orig_used = set1->used;
1110
1111 for (i = 0; i < set2->used; ++i) {
1112 for (j = 0; j < orig_used; ++j) {
1113 /* detect duplicities */
1114 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1115 break;
1116 }
1117 }
1118
Michal Vasko0587bce2020-12-10 12:19:21 +01001119 if (j < orig_used) {
1120 /* node is there, but update its status if needed */
1121 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1122 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
1123 }
1124 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001125 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1126 ++set1->used;
1127 }
1128 }
1129
Michal Vaskod3678892020-05-21 10:06:58 +02001130 lyxp_set_free_content(set2);
1131 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001132}
1133
1134/**
1135 * @brief Insert a node into a set. Context position aware.
1136 *
1137 * @param[in] set Set to use.
1138 * @param[in] node Node to insert to @p set.
1139 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1140 * @param[in] node_type Node type of @p node.
1141 * @param[in] idx Index in @p set to insert into.
1142 */
1143static void
1144set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1145{
Michal Vaskod3678892020-05-21 10:06:58 +02001146 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001147
Michal Vaskod3678892020-05-21 10:06:58 +02001148 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001149 /* first item */
1150 if (idx) {
1151 /* no real harm done, but it is a bug */
1152 LOGINT(set->ctx);
1153 idx = 0;
1154 }
1155 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1156 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1157 set->type = LYXP_SET_NODE_SET;
1158 set->used = 0;
1159 set->size = LYXP_SET_SIZE_START;
1160 set->ctx_pos = 1;
1161 set->ctx_size = 1;
1162 set->ht = NULL;
1163 } else {
1164 /* not an empty set */
1165 if (set->used == set->size) {
1166
1167 /* set is full */
1168 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1169 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1170 set->size += LYXP_SET_SIZE_STEP;
1171 }
1172
1173 if (idx > set->used) {
1174 LOGINT(set->ctx);
1175 idx = set->used;
1176 }
1177
1178 /* make space for the new node */
1179 if (idx < set->used) {
1180 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1181 }
1182 }
1183
1184 /* finally assign the value */
1185 set->val.nodes[idx].node = (struct lyd_node *)node;
1186 set->val.nodes[idx].type = node_type;
1187 set->val.nodes[idx].pos = pos;
1188 ++set->used;
1189
Michal Vasko2caefc12019-11-14 16:07:56 +01001190 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1191 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1192 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001193}
1194
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001195LY_ERR
1196lyxp_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 +02001197{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001198 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001199
1200 assert(set->type == LYXP_SET_SCNODE_SET);
1201
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001202 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001203 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001204 } else {
1205 if (set->used == set->size) {
1206 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 +02001207 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001208 set->size += LYXP_SET_SIZE_STEP;
1209 }
1210
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001211 index = set->used;
1212 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1213 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001214 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001215 ++set->used;
1216 }
1217
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001218 if (index_p) {
1219 *index_p = index;
1220 }
1221
1222 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001223}
1224
1225/**
1226 * @brief Replace a node in a set with another. Context position aware.
1227 *
1228 * @param[in] set Set to use.
1229 * @param[in] node Node to insert to @p set.
1230 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1231 * @param[in] node_type Node type of @p node.
1232 * @param[in] idx Index in @p set of the node to replace.
1233 */
1234static void
1235set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1236{
1237 assert(set && (idx < set->used));
1238
Michal Vasko2caefc12019-11-14 16:07:56 +01001239 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1240 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1241 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001242 set->val.nodes[idx].node = (struct lyd_node *)node;
1243 set->val.nodes[idx].type = node_type;
1244 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001245 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1246 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1247 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001248}
1249
1250/**
1251 * @brief Set all nodes with ctx 1 to a new unique context value.
1252 *
1253 * @param[in] set Set to modify.
1254 * @return New context value.
1255 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001256static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001257set_scnode_new_in_ctx(struct lyxp_set *set)
1258{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001259 uint32_t i;
1260 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001261
1262 assert(set->type == LYXP_SET_SCNODE_SET);
1263
Radek Krejcif13b87b2020-12-01 22:02:17 +01001264 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001265retry:
1266 for (i = 0; i < set->used; ++i) {
1267 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1268 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1269 goto retry;
1270 }
1271 }
1272 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001273 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001274 set->val.scnodes[i].in_ctx = ret_ctx;
1275 }
1276 }
1277
1278 return ret_ctx;
1279}
1280
1281/**
1282 * @brief Get unique @p node position in the data.
1283 *
1284 * @param[in] node Node to find.
1285 * @param[in] node_type Node type of @p node.
1286 * @param[in] root Root node.
1287 * @param[in] root_type Type of the XPath @p root node.
1288 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1289 * be used to increase efficiency and start the DFS from this node.
1290 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1291 * @return Node position.
1292 */
1293static uint32_t
1294get_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 +02001295 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001296{
Michal Vasko56daf732020-08-10 10:57:18 +02001297 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001298 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001299 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001300
1301 assert(prev && prev_pos && !root->prev->next);
1302
1303 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1304 return 0;
1305 }
1306
1307 if (*prev) {
1308 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001309 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001310 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001311 goto dfs_search;
1312 }
1313
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001314 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001315 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001316dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001317 if (*prev && !elem) {
1318 /* resume previous DFS */
1319 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1320 LYD_TREE_DFS_continue = 0;
1321 }
1322
Michal Vasko03ff5a72019-09-11 13:49:33 +02001323 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001324 /* skip */
1325 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001326 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001327 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001328 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001329 break;
1330 }
Michal Vasko56daf732020-08-10 10:57:18 +02001331 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001332 }
Michal Vasko56daf732020-08-10 10:57:18 +02001333
1334 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001335 }
1336
1337 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001338 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001339 break;
1340 }
1341 }
1342
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001343 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001344 if (!(*prev)) {
1345 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001346 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001347 return 0;
1348 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001349 /* start the search again from the beginning */
1350 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001351
Michal Vasko56daf732020-08-10 10:57:18 +02001352 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001353 pos = 1;
1354 goto dfs_search;
1355 }
1356 }
1357
1358 /* remember the last found node for next time */
1359 *prev = node;
1360 *prev_pos = pos;
1361
1362 return pos;
1363}
1364
1365/**
1366 * @brief Assign (fill) missing node positions.
1367 *
1368 * @param[in] set Set to fill positions in.
1369 * @param[in] root Context root node.
1370 * @param[in] root_type Context root type.
1371 * @return LY_ERR
1372 */
1373static LY_ERR
1374set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1375{
1376 const struct lyd_node *prev = NULL, *tmp_node;
1377 uint32_t i, tmp_pos = 0;
1378
1379 for (i = 0; i < set->used; ++i) {
1380 if (!set->val.nodes[i].pos) {
1381 tmp_node = NULL;
1382 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001383 case LYXP_NODE_META:
1384 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001385 if (!tmp_node) {
1386 LOGINT_RET(root->schema->module->ctx);
1387 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001388 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001389 case LYXP_NODE_ELEM:
1390 case LYXP_NODE_TEXT:
1391 if (!tmp_node) {
1392 tmp_node = set->val.nodes[i].node;
1393 }
1394 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1395 break;
1396 default:
1397 /* all roots have position 0 */
1398 break;
1399 }
1400 }
1401 }
1402
1403 return LY_SUCCESS;
1404}
1405
1406/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001407 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001408 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001409 * @param[in] meta Metadata to use.
1410 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001411 */
1412static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001413get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001414{
1415 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001416 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001417
Michal Vasko9f96a052020-03-10 09:41:45 +01001418 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001419 ++pos;
1420 }
1421
Michal Vasko9f96a052020-03-10 09:41:45 +01001422 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001423 return pos;
1424}
1425
1426/**
1427 * @brief Compare 2 nodes in respect to XPath document order.
1428 *
1429 * @param[in] item1 1st node.
1430 * @param[in] item2 2nd node.
1431 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1432 */
1433static int
1434set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1435{
Michal Vasko9f96a052020-03-10 09:41:45 +01001436 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001437
1438 if (item1->pos < item2->pos) {
1439 return -1;
1440 }
1441
1442 if (item1->pos > item2->pos) {
1443 return 1;
1444 }
1445
1446 /* node positions are equal, the fun case */
1447
1448 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1449 /* special case since text nodes are actually saved as their parents */
1450 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1451 if (item1->type == LYXP_NODE_ELEM) {
1452 assert(item2->type == LYXP_NODE_TEXT);
1453 return -1;
1454 } else {
1455 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1456 return 1;
1457 }
1458 }
1459
Michal Vasko9f96a052020-03-10 09:41:45 +01001460 /* we need meta positions now */
1461 if (item1->type == LYXP_NODE_META) {
1462 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001463 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001464 if (item2->type == LYXP_NODE_META) {
1465 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001466 }
1467
Michal Vasko9f96a052020-03-10 09:41:45 +01001468 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001469 /* check for duplicates */
1470 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001471 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001472 return 0;
1473 }
1474
Michal Vasko9f96a052020-03-10 09:41:45 +01001475 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001476 /* elem is always first, 2nd node is after it */
1477 if (item1->type == LYXP_NODE_ELEM) {
1478 assert(item2->type != LYXP_NODE_ELEM);
1479 return -1;
1480 }
1481
Michal Vasko9f96a052020-03-10 09:41:45 +01001482 /* 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 +02001483 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001484 if (((item1->type == LYXP_NODE_TEXT) &&
1485 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1486 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1487 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1488 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001489 return 1;
1490 }
1491
Michal Vasko9f96a052020-03-10 09:41:45 +01001492 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001493 /* 2nd is after 1st */
1494 return -1;
1495}
1496
1497/**
1498 * @brief Set cast for comparisons.
1499 *
1500 * @param[in] trg Target set to cast source into.
1501 * @param[in] src Source set.
1502 * @param[in] type Target set type.
1503 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001504 * @return LY_ERR
1505 */
1506static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001507set_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 +02001508{
1509 assert(src->type == LYXP_SET_NODE_SET);
1510
1511 set_init(trg, src);
1512
1513 /* insert node into target set */
1514 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1515
1516 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001517 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001518}
1519
Michal Vasko4c7763f2020-07-27 17:40:37 +02001520/**
1521 * @brief Set content canonization for comparisons.
1522 *
1523 * @param[in] trg Target set to put the canononized source into.
1524 * @param[in] src Source set.
1525 * @param[in] xp_node Source XPath node/meta to use for canonization.
1526 * @return LY_ERR
1527 */
1528static LY_ERR
1529set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1530{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001531 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001532 struct lyd_value val;
1533 struct ly_err_item *err = NULL;
1534 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001535 LY_ERR rc;
1536
1537 /* is there anything to canonize even? */
1538 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1539 /* do we have a type to use for canonization? */
1540 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1541 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1542 } else if (xp_node->type == LYXP_NODE_META) {
1543 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1544 }
1545 }
1546 if (!type) {
1547 goto fill;
1548 }
1549
1550 if (src->type == LYXP_SET_NUMBER) {
1551 /* canonize number */
1552 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1553 LOGMEM(src->ctx);
1554 return LY_EMEM;
1555 }
1556 } else {
1557 /* canonize string */
1558 str = strdup(src->val.str);
1559 }
1560
1561 /* ignore errors, the value may not satisfy schema constraints */
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001562 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 +01001563 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001564 ly_err_free(err);
1565 if (rc) {
1566 /* invalid value */
1567 free(str);
1568 goto fill;
1569 }
1570
Michal Vasko4c7763f2020-07-27 17:40:37 +02001571 /* use the canonized value */
1572 set_init(trg, src);
1573 trg->type = src->type;
1574 if (src->type == LYXP_SET_NUMBER) {
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001575 trg->val.num = strtold(val.canonical, &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001576 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1577 } else {
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001578 trg->val.str = strdup(val.canonical);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001579 }
1580 type->plugin->free(src->ctx, &val);
1581 return LY_SUCCESS;
1582
1583fill:
1584 /* no canonization needed/possible */
1585 set_fill_set(trg, src);
1586 return LY_SUCCESS;
1587}
1588
Michal Vasko03ff5a72019-09-11 13:49:33 +02001589#ifndef NDEBUG
1590
1591/**
1592 * @brief Bubble sort @p set into XPath document order.
1593 * Context position aware. Unused in the 'Release' build target.
1594 *
1595 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001596 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1597 */
1598static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001599set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001600{
1601 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001602 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001603 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001604 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001605 struct lyxp_set_node item;
1606 struct lyxp_set_hash_node hnode;
1607 uint64_t hash;
1608
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001609 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001610 return 0;
1611 }
1612
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001613 /* find first top-level node to be used as anchor for positions */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001614 for (root = set->cur_node; root->parent; root = (const struct lyd_node *)root->parent) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001615 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001616
1617 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001618 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001619 return -1;
1620 }
1621
1622 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1623 print_set_debug(set);
1624
1625 for (i = 0; i < set->used; ++i) {
1626 inverted = 0;
1627 change = 0;
1628
1629 for (j = 1; j < set->used - i; ++j) {
1630 /* compare node positions */
1631 if (inverted) {
1632 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1633 } else {
1634 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1635 }
1636
1637 /* swap if needed */
1638 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1639 change = 1;
1640
1641 item = set->val.nodes[j - 1];
1642 set->val.nodes[j - 1] = set->val.nodes[j];
1643 set->val.nodes[j] = item;
1644 } else {
1645 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1646 inverted = !inverted;
1647 }
1648 }
1649
1650 ++ret;
1651
1652 if (!change) {
1653 break;
1654 }
1655 }
1656
1657 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1658 print_set_debug(set);
1659
1660 /* check node hashes */
1661 if (set->used >= LYD_HT_MIN_ITEMS) {
1662 assert(set->ht);
1663 for (i = 0; i < set->used; ++i) {
1664 hnode.node = set->val.nodes[i].node;
1665 hnode.type = set->val.nodes[i].type;
1666
1667 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1668 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1669 hash = dict_hash_multi(hash, NULL, 0);
1670
1671 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1672 }
1673 }
1674
1675 return ret - 1;
1676}
1677
1678/**
1679 * @brief Remove duplicate entries in a sorted node set.
1680 *
1681 * @param[in] set Sorted set to check.
1682 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1683 */
1684static LY_ERR
1685set_sorted_dup_node_clean(struct lyxp_set *set)
1686{
1687 uint32_t i = 0;
1688 LY_ERR ret = LY_SUCCESS;
1689
1690 if (set->used > 1) {
1691 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001692 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1693 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001694 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001695 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001696 }
Michal Vasko57eab132019-09-24 11:46:26 +02001697 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001698 }
1699 }
1700
Michal Vasko2caefc12019-11-14 16:07:56 +01001701 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001702 return ret;
1703}
1704
1705#endif
1706
1707/**
1708 * @brief Merge 2 sorted sets into one.
1709 *
1710 * @param[in,out] trg Set to merge into. Duplicates are removed.
1711 * @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 +02001712 * @return LY_ERR
1713 */
1714static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001715set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001716{
1717 uint32_t i, j, k, count, dup_count;
1718 int cmp;
1719 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001720
Michal Vaskod3678892020-05-21 10:06:58 +02001721 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001722 return LY_EINVAL;
1723 }
1724
Michal Vaskod3678892020-05-21 10:06:58 +02001725 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001726 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001727 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001728 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001729 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001730 return LY_SUCCESS;
1731 }
1732
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001733 /* find first top-level node to be used as anchor for positions */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001734 for (root = trg->cur_node; root->parent; root = (const struct lyd_node *)root->parent) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001735 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001736
1737 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001738 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001739 return LY_EINT;
1740 }
1741
1742#ifndef NDEBUG
1743 LOGDBG(LY_LDGXPATH, "MERGE target");
1744 print_set_debug(trg);
1745 LOGDBG(LY_LDGXPATH, "MERGE source");
1746 print_set_debug(src);
1747#endif
1748
1749 /* make memory for the merge (duplicates are not detected yet, so space
1750 * will likely be wasted on them, too bad) */
1751 if (trg->size - trg->used < src->used) {
1752 trg->size = trg->used + src->used;
1753
1754 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1755 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1756 }
1757
1758 i = 0;
1759 j = 0;
1760 count = 0;
1761 dup_count = 0;
1762 do {
1763 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1764 if (!cmp) {
1765 if (!count) {
1766 /* duplicate, just skip it */
1767 ++i;
1768 ++j;
1769 } else {
1770 /* we are copying something already, so let's copy the duplicate too,
1771 * we are hoping that afterwards there are some more nodes to
1772 * copy and this way we can copy them all together */
1773 ++count;
1774 ++dup_count;
1775 ++i;
1776 ++j;
1777 }
1778 } else if (cmp < 0) {
1779 /* inserting src node into trg, just remember it for now */
1780 ++count;
1781 ++i;
1782
1783 /* insert the hash now */
1784 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1785 } else if (count) {
1786copy_nodes:
1787 /* time to actually copy the nodes, we have found the largest block of nodes */
1788 memmove(&trg->val.nodes[j + (count - dup_count)],
1789 &trg->val.nodes[j],
1790 (trg->used - j) * sizeof *trg->val.nodes);
1791 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1792
1793 trg->used += count - dup_count;
1794 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1795 j += count - dup_count;
1796
1797 count = 0;
1798 dup_count = 0;
1799 } else {
1800 ++j;
1801 }
1802 } while ((i < src->used) && (j < trg->used));
1803
1804 if ((i < src->used) || count) {
1805 /* insert all the hashes first */
1806 for (k = i; k < src->used; ++k) {
1807 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1808 }
1809
1810 /* loop ended, but we need to copy something at trg end */
1811 count += src->used - i;
1812 i = src->used;
1813 goto copy_nodes;
1814 }
1815
1816 /* we are inserting hashes before the actual node insert, which causes
1817 * situations when there were initially not enough items for a hash table,
1818 * but even after some were inserted, hash table was not created (during
1819 * insertion the number of items is not updated yet) */
1820 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1821 set_insert_node_hash(trg, NULL, 0);
1822 }
1823
1824#ifndef NDEBUG
1825 LOGDBG(LY_LDGXPATH, "MERGE result");
1826 print_set_debug(trg);
1827#endif
1828
Michal Vaskod3678892020-05-21 10:06:58 +02001829 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001830 return LY_SUCCESS;
1831}
1832
1833/*
1834 * (re)parse functions
1835 *
1836 * Parse functions parse the expression into
1837 * tokens (syntactic analysis).
1838 *
1839 * Reparse functions perform semantic analysis
1840 * (do not save the result, just a check) of
1841 * the expression and fill repeat indices.
1842 */
1843
Michal Vasko14676352020-05-29 11:35:55 +02001844LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001845lyxp_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 +02001846{
Michal Vasko004d3152020-06-11 19:59:22 +02001847 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001848 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001849 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001850 }
Michal Vasko14676352020-05-29 11:35:55 +02001851 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001852 }
1853
Michal Vasko004d3152020-06-11 19:59:22 +02001854 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001855 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001856 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001857 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001858 }
Michal Vasko14676352020-05-29 11:35:55 +02001859 return LY_ENOT;
1860 }
1861
1862 return LY_SUCCESS;
1863}
1864
Michal Vasko004d3152020-06-11 19:59:22 +02001865LY_ERR
1866lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1867{
1868 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1869
1870 /* skip the token */
1871 ++(*tok_idx);
1872
1873 return LY_SUCCESS;
1874}
1875
Michal Vasko14676352020-05-29 11:35:55 +02001876/* just like lyxp_check_token() but tests for 2 tokens */
1877static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001878exp_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 +02001879 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001880{
Michal Vasko004d3152020-06-11 19:59:22 +02001881 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001882 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001883 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001884 }
1885 return LY_EINCOMPLETE;
1886 }
1887
Michal Vasko004d3152020-06-11 19:59:22 +02001888 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001889 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001890 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001891 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001892 }
1893 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001894 }
1895
1896 return LY_SUCCESS;
1897}
1898
1899/**
1900 * @brief Stack operation push on the repeat array.
1901 *
1902 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001903 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001904 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1905 */
1906static void
Michal Vasko004d3152020-06-11 19:59:22 +02001907exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001908{
1909 uint16_t i;
1910
Michal Vasko004d3152020-06-11 19:59:22 +02001911 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001912 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001913 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1914 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1915 exp->repeat[tok_idx][i] = repeat_op_idx;
1916 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001917 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001918 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1919 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1920 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001921 }
1922}
1923
1924/**
1925 * @brief Reparse Predicate. Logs directly on error.
1926 *
1927 * [7] Predicate ::= '[' Expr ']'
1928 *
1929 * @param[in] ctx Context for logging.
1930 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001931 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001932 * @return LY_ERR
1933 */
1934static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001935reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001936{
1937 LY_ERR rc;
1938
Michal Vasko004d3152020-06-11 19:59:22 +02001939 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001940 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001941 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001942
Michal Vasko004d3152020-06-11 19:59:22 +02001943 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001944 LY_CHECK_RET(rc);
1945
Michal Vasko004d3152020-06-11 19:59:22 +02001946 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001947 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001948 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001949
1950 return LY_SUCCESS;
1951}
1952
1953/**
1954 * @brief Reparse RelativeLocationPath. Logs directly on error.
1955 *
1956 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1957 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1958 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1959 *
1960 * @param[in] ctx Context for logging.
1961 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001962 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001963 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1964 */
1965static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001966reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001967{
1968 LY_ERR rc;
1969
Michal Vasko004d3152020-06-11 19:59:22 +02001970 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001971 LY_CHECK_RET(rc);
1972
1973 goto step;
1974 do {
1975 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001976 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001977
Michal Vasko004d3152020-06-11 19:59:22 +02001978 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001979 LY_CHECK_RET(rc);
1980step:
1981 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001982 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001983 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001984 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001985 break;
1986
1987 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001988 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001989 break;
1990
1991 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02001992 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001993
Michal Vasko004d3152020-06-11 19:59:22 +02001994 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001995 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001996 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001997 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 +02001998 return LY_EVALID;
1999 }
Radek Krejci0f969882020-08-21 16:56:47 +02002000 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002001 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002002 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002003 goto reparse_predicate;
2004 break;
2005
2006 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002007 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002008
2009 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002010 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002011 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002012 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002013
2014 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002015 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002016 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002017 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002018
2019reparse_predicate:
2020 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002021 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2022 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002023 LY_CHECK_RET(rc);
2024 }
2025 break;
2026 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002027 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 +02002028 return LY_EVALID;
2029 }
Michal Vasko004d3152020-06-11 19:59:22 +02002030 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002031
2032 return LY_SUCCESS;
2033}
2034
2035/**
2036 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2037 *
2038 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2039 *
2040 * @param[in] ctx Context for logging.
2041 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002042 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002043 * @return LY_ERR
2044 */
2045static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002046reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002047{
2048 LY_ERR rc;
2049
Michal Vasko004d3152020-06-11 19:59:22 +02002050 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 +02002051
2052 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002053 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002054 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002055 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002056
Michal Vasko004d3152020-06-11 19:59:22 +02002057 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002058 return LY_SUCCESS;
2059 }
Michal Vasko004d3152020-06-11 19:59:22 +02002060 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002061 case LYXP_TOKEN_DOT:
2062 case LYXP_TOKEN_DDOT:
2063 case LYXP_TOKEN_AT:
2064 case LYXP_TOKEN_NAMETEST:
2065 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002066 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002067 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002068 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002069 default:
2070 break;
2071 }
2072
Michal Vasko03ff5a72019-09-11 13:49:33 +02002073 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002074 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002075 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002076
Michal Vasko004d3152020-06-11 19:59:22 +02002077 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002078 LY_CHECK_RET(rc);
2079 }
2080
2081 return LY_SUCCESS;
2082}
2083
2084/**
2085 * @brief Reparse FunctionCall. Logs directly on error.
2086 *
2087 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2088 *
2089 * @param[in] ctx Context for logging.
2090 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002091 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002092 * @return LY_ERR
2093 */
2094static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002095reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002096{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002097 int8_t min_arg_count = -1;
2098 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002099 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002100 LY_ERR rc;
2101
Michal Vasko004d3152020-06-11 19:59:22 +02002102 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002103 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002104 func_tok_idx = *tok_idx;
2105 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002106 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002107 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002108 min_arg_count = 1;
2109 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002110 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002111 min_arg_count = 1;
2112 max_arg_count = 1;
2113 }
2114 break;
2115 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002116 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002117 min_arg_count = 1;
2118 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002119 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002120 min_arg_count = 0;
2121 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002122 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002123 min_arg_count = 0;
2124 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002125 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002126 min_arg_count = 0;
2127 max_arg_count = 0;
2128 }
2129 break;
2130 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002131 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002132 min_arg_count = 1;
2133 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002134 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002135 min_arg_count = 0;
2136 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002137 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002138 min_arg_count = 1;
2139 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002140 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002141 min_arg_count = 1;
2142 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002143 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002144 min_arg_count = 1;
2145 max_arg_count = 1;
2146 }
2147 break;
2148 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002149 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002150 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002151 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002152 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002153 min_arg_count = 0;
2154 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002155 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002156 min_arg_count = 0;
2157 max_arg_count = 1;
2158 }
2159 break;
2160 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002161 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002162 min_arg_count = 1;
2163 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002164 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002165 min_arg_count = 1;
2166 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002167 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002168 min_arg_count = 0;
2169 max_arg_count = 0;
2170 }
2171 break;
2172 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002173 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002174 min_arg_count = 2;
2175 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002176 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002177 min_arg_count = 0;
2178 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002179 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002180 min_arg_count = 2;
2181 max_arg_count = 2;
2182 }
2183 break;
2184 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002185 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002186 min_arg_count = 2;
2187 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002188 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002189 min_arg_count = 3;
2190 max_arg_count = 3;
2191 }
2192 break;
2193 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002194 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002195 min_arg_count = 0;
2196 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002197 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002198 min_arg_count = 1;
2199 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002200 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002201 min_arg_count = 2;
2202 max_arg_count = 2;
2203 }
2204 break;
2205 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002206 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002207 min_arg_count = 2;
2208 max_arg_count = 2;
2209 }
2210 break;
2211 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002212 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002213 min_arg_count = 2;
2214 max_arg_count = 2;
2215 }
2216 break;
2217 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002218 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002219 min_arg_count = 0;
2220 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002221 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002222 min_arg_count = 0;
2223 max_arg_count = 1;
2224 }
2225 break;
2226 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002227 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002228 min_arg_count = 0;
2229 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002230 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002231 min_arg_count = 2;
2232 max_arg_count = 2;
2233 }
2234 break;
2235 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002236 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002237 min_arg_count = 2;
2238 max_arg_count = 2;
2239 }
2240 break;
2241 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002242 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002243 min_arg_count = 2;
2244 max_arg_count = 2;
2245 }
2246 break;
2247 }
2248 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002249 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 +02002250 return LY_EINVAL;
2251 }
Michal Vasko004d3152020-06-11 19:59:22 +02002252 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002253
2254 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002255 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002256 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002257 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002258
2259 /* ( Expr ( ',' Expr )* )? */
2260 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002261 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002262 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002263 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002264 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002265 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002266 LY_CHECK_RET(rc);
2267 }
Michal Vasko004d3152020-06-11 19:59:22 +02002268 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2269 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002270
2271 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002272 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002273 LY_CHECK_RET(rc);
2274 }
2275
2276 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002277 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002278 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002279 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002280
Radek Krejci857189e2020-09-01 13:26:36 +02002281 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002282 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 +02002283 return LY_EVALID;
2284 }
2285
2286 return LY_SUCCESS;
2287}
2288
2289/**
2290 * @brief Reparse PathExpr. Logs directly on error.
2291 *
2292 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2293 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2294 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2295 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2296 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2297 *
2298 * @param[in] ctx Context for logging.
2299 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002300 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002301 * @return LY_ERR
2302 */
2303static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002304reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002305{
2306 LY_ERR rc;
2307
Michal Vasko004d3152020-06-11 19:59:22 +02002308 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002309 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002310 }
2311
Michal Vasko004d3152020-06-11 19:59:22 +02002312 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002313 case LYXP_TOKEN_PAR1:
2314 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002315 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002316
Michal Vasko004d3152020-06-11 19:59:22 +02002317 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002318 LY_CHECK_RET(rc);
2319
Michal Vasko004d3152020-06-11 19:59:22 +02002320 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002321 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002322 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002323 goto predicate;
2324 break;
2325 case LYXP_TOKEN_DOT:
2326 case LYXP_TOKEN_DDOT:
2327 case LYXP_TOKEN_AT:
2328 case LYXP_TOKEN_NAMETEST:
2329 case LYXP_TOKEN_NODETYPE:
2330 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002331 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002332 LY_CHECK_RET(rc);
2333 break;
2334 case LYXP_TOKEN_FUNCNAME:
2335 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002336 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002337 LY_CHECK_RET(rc);
2338 goto predicate;
2339 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002340 case LYXP_TOKEN_OPER_PATH:
2341 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002342 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002343 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002344 LY_CHECK_RET(rc);
2345 break;
2346 case LYXP_TOKEN_LITERAL:
2347 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002348 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002349 goto predicate;
2350 break;
2351 case LYXP_TOKEN_NUMBER:
2352 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002353 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002354 goto predicate;
2355 break;
2356 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002357 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 +02002358 return LY_EVALID;
2359 }
2360
2361 return LY_SUCCESS;
2362
2363predicate:
2364 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002365 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2366 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002367 LY_CHECK_RET(rc);
2368 }
2369
2370 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002371 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002372
2373 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002374 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002375
Michal Vasko004d3152020-06-11 19:59:22 +02002376 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002377 LY_CHECK_RET(rc);
2378 }
2379
2380 return LY_SUCCESS;
2381}
2382
2383/**
2384 * @brief Reparse UnaryExpr. Logs directly on error.
2385 *
2386 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2387 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2388 *
2389 * @param[in] ctx Context for logging.
2390 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002391 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002392 * @return LY_ERR
2393 */
2394static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002395reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002396{
2397 uint16_t prev_exp;
2398 LY_ERR rc;
2399
2400 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002401 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002402 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2403 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002404 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002405 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002406 }
2407
2408 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002409 prev_exp = *tok_idx;
2410 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002411 LY_CHECK_RET(rc);
2412
2413 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002414 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002415 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002416 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002417
Michal Vasko004d3152020-06-11 19:59:22 +02002418 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002419 LY_CHECK_RET(rc);
2420 }
2421
2422 return LY_SUCCESS;
2423}
2424
2425/**
2426 * @brief Reparse AdditiveExpr. Logs directly on error.
2427 *
2428 * [15] AdditiveExpr ::= MultiplicativeExpr
2429 * | AdditiveExpr '+' MultiplicativeExpr
2430 * | AdditiveExpr '-' MultiplicativeExpr
2431 * [16] MultiplicativeExpr ::= UnaryExpr
2432 * | MultiplicativeExpr '*' UnaryExpr
2433 * | MultiplicativeExpr 'div' UnaryExpr
2434 * | MultiplicativeExpr 'mod' UnaryExpr
2435 *
2436 * @param[in] ctx Context for logging.
2437 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002438 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002439 * @return LY_ERR
2440 */
2441static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002442reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002443{
2444 uint16_t prev_add_exp, prev_mul_exp;
2445 LY_ERR rc;
2446
Michal Vasko004d3152020-06-11 19:59:22 +02002447 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002448 goto reparse_multiplicative_expr;
2449
2450 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002451 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2452 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002453 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002454 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002455
2456reparse_multiplicative_expr:
2457 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002458 prev_mul_exp = *tok_idx;
2459 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002460 LY_CHECK_RET(rc);
2461
2462 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002463 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2464 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002465 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002466 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002467
Michal Vasko004d3152020-06-11 19:59:22 +02002468 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002469 LY_CHECK_RET(rc);
2470 }
2471 }
2472
2473 return LY_SUCCESS;
2474}
2475
2476/**
2477 * @brief Reparse EqualityExpr. Logs directly on error.
2478 *
2479 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2480 * | EqualityExpr '!=' RelationalExpr
2481 * [14] RelationalExpr ::= AdditiveExpr
2482 * | RelationalExpr '<' AdditiveExpr
2483 * | RelationalExpr '>' AdditiveExpr
2484 * | RelationalExpr '<=' AdditiveExpr
2485 * | RelationalExpr '>=' AdditiveExpr
2486 *
2487 * @param[in] ctx Context for logging.
2488 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002489 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002490 * @return LY_ERR
2491 */
2492static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002493reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002494{
2495 uint16_t prev_eq_exp, prev_rel_exp;
2496 LY_ERR rc;
2497
Michal Vasko004d3152020-06-11 19:59:22 +02002498 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002499 goto reparse_additive_expr;
2500
2501 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002502 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002503 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002504 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002505
2506reparse_additive_expr:
2507 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002508 prev_rel_exp = *tok_idx;
2509 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002510 LY_CHECK_RET(rc);
2511
2512 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002513 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002514 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002515 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002516
Michal Vasko004d3152020-06-11 19:59:22 +02002517 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002518 LY_CHECK_RET(rc);
2519 }
2520 }
2521
2522 return LY_SUCCESS;
2523}
2524
2525/**
2526 * @brief Reparse OrExpr. Logs directly on error.
2527 *
2528 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2529 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2530 *
2531 * @param[in] ctx Context for logging.
2532 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002533 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002534 * @return LY_ERR
2535 */
2536static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002537reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002538{
2539 uint16_t prev_or_exp, prev_and_exp;
2540 LY_ERR rc;
2541
Michal Vasko004d3152020-06-11 19:59:22 +02002542 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002543 goto reparse_equality_expr;
2544
2545 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002546 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 +02002547 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002548 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002549
2550reparse_equality_expr:
2551 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002552 prev_and_exp = *tok_idx;
2553 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002554 LY_CHECK_RET(rc);
2555
2556 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002557 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 +02002558 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002559 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002560
Michal Vasko004d3152020-06-11 19:59:22 +02002561 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002562 LY_CHECK_RET(rc);
2563 }
2564 }
2565
2566 return LY_SUCCESS;
2567}
Radek Krejcib1646a92018-11-02 16:08:26 +01002568
2569/**
2570 * @brief Parse NCName.
2571 *
2572 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002573 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002574 */
Radek Krejcid4270262019-01-07 15:07:25 +01002575static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002576parse_ncname(const char *ncname)
2577{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002578 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002579 size_t size;
2580 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002581
2582 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2583 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2584 return len;
2585 }
2586
2587 do {
2588 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002589 if (!*ncname) {
2590 break;
2591 }
Radek Krejcid4270262019-01-07 15:07:25 +01002592 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002593 } while (is_xmlqnamechar(uc) && (uc != ':'));
2594
2595 return len;
2596}
2597
2598/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002599 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002600 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002601 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002602 * @param[in] exp Expression to use.
2603 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002604 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002605 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002606 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002607 */
2608static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002609exp_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 +01002610{
2611 uint32_t prev;
2612
2613 if (exp->used == exp->size) {
2614 prev = exp->size;
2615 exp->size += LYXP_EXPR_SIZE_STEP;
2616 if (prev > exp->size) {
2617 LOGINT(ctx);
2618 return LY_EINT;
2619 }
2620
2621 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2622 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2623 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2624 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2625 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2626 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2627 }
2628
2629 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002630 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002631 exp->tok_len[exp->used] = tok_len;
2632 ++exp->used;
2633 return LY_SUCCESS;
2634}
2635
2636void
Michal Vasko14676352020-05-29 11:35:55 +02002637lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002638{
2639 uint16_t i;
2640
2641 if (!expr) {
2642 return;
2643 }
2644
2645 lydict_remove(ctx, expr->expr);
2646 free(expr->tokens);
2647 free(expr->tok_pos);
2648 free(expr->tok_len);
2649 if (expr->repeat) {
2650 for (i = 0; i < expr->used; ++i) {
2651 free(expr->repeat[i]);
2652 }
2653 }
2654 free(expr->repeat);
2655 free(expr);
2656}
2657
Radek Krejcif03a9e22020-09-18 20:09:31 +02002658LY_ERR
2659lyxp_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 +01002660{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002661 LY_ERR ret = LY_SUCCESS;
2662 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002663 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002664 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002665 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002666 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002667
Radek Krejcif03a9e22020-09-18 20:09:31 +02002668 assert(expr_p);
2669
2670 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002671 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002672 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002673 }
2674
2675 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002676 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002677 }
2678 if (expr_len > UINT16_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002679 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002680 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002681 }
2682
2683 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002684 expr = calloc(1, sizeof *expr);
2685 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2686 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2687 expr->used = 0;
2688 expr->size = LYXP_EXPR_SIZE_START;
2689 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2690 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002691
Radek Krejcif03a9e22020-09-18 20:09:31 +02002692 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2693 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002694
Radek Krejcif03a9e22020-09-18 20:09:31 +02002695 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2696 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002697
Michal Vasko004d3152020-06-11 19:59:22 +02002698 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002699 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002700
Radek Krejcif03a9e22020-09-18 20:09:31 +02002701 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002702 ++parsed;
2703 }
2704
2705 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002706 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002707
2708 /* '(' */
2709 tok_len = 1;
2710 tok_type = LYXP_TOKEN_PAR1;
2711
Radek Krejcif03a9e22020-09-18 20:09:31 +02002712 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002713 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002714 if (((expr->tok_len[expr->used - 1] == 4) &&
2715 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2716 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2717 ((expr->tok_len[expr->used - 1] == 7) &&
2718 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002719 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002720 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002721 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002722 }
2723 prev_function_check = 0;
2724 }
2725
Radek Krejcif03a9e22020-09-18 20:09:31 +02002726 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002727
2728 /* ')' */
2729 tok_len = 1;
2730 tok_type = LYXP_TOKEN_PAR2;
2731
Radek Krejcif03a9e22020-09-18 20:09:31 +02002732 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002733
2734 /* '[' */
2735 tok_len = 1;
2736 tok_type = LYXP_TOKEN_BRACK1;
2737
Radek Krejcif03a9e22020-09-18 20:09:31 +02002738 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002739
2740 /* ']' */
2741 tok_len = 1;
2742 tok_type = LYXP_TOKEN_BRACK2;
2743
Radek Krejcif03a9e22020-09-18 20:09:31 +02002744 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002745
2746 /* '..' */
2747 tok_len = 2;
2748 tok_type = LYXP_TOKEN_DDOT;
2749
Radek Krejcif03a9e22020-09-18 20:09:31 +02002750 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002751
2752 /* '.' */
2753 tok_len = 1;
2754 tok_type = LYXP_TOKEN_DOT;
2755
Radek Krejcif03a9e22020-09-18 20:09:31 +02002756 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002757
2758 /* '@' */
2759 tok_len = 1;
2760 tok_type = LYXP_TOKEN_AT;
2761
Radek Krejcif03a9e22020-09-18 20:09:31 +02002762 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002763
2764 /* ',' */
2765 tok_len = 1;
2766 tok_type = LYXP_TOKEN_COMMA;
2767
Radek Krejcif03a9e22020-09-18 20:09:31 +02002768 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002769
2770 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002771 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2772 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002773 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002774 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002775 ++tok_len;
2776 tok_type = LYXP_TOKEN_LITERAL;
2777
Radek Krejcif03a9e22020-09-18 20:09:31 +02002778 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002779
2780 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002781 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2782 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002783 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002784 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002785 ++tok_len;
2786 tok_type = LYXP_TOKEN_LITERAL;
2787
Radek Krejcif03a9e22020-09-18 20:09:31 +02002788 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002789
2790 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002791 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2792 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002793 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002794 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002795 }
2796 tok_type = LYXP_TOKEN_NUMBER;
2797
Radek Krejcif03a9e22020-09-18 20:09:31 +02002798 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002799
2800 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002801 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002802 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002803 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002804 } else {
2805 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002806 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002807 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002808
Radek Krejcif03a9e22020-09-18 20:09:31 +02002809 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002810
Michal Vasko3e48bf32020-06-01 08:39:07 +02002811 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002812 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002813 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2814
Radek Krejcif03a9e22020-09-18 20:09:31 +02002815 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002816
2817 /* Operator '<=', '>=' */
2818 tok_len = 2;
2819 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002820
Radek Krejcif03a9e22020-09-18 20:09:31 +02002821 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002822
2823 /* Operator '|' */
2824 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002825 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002826
Radek Krejcif03a9e22020-09-18 20:09:31 +02002827 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002828
2829 /* Operator '+', '-' */
2830 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002831 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002832
Radek Krejcif03a9e22020-09-18 20:09:31 +02002833 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002834
Michal Vasko3e48bf32020-06-01 08:39:07 +02002835 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002836 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002837 tok_type = LYXP_TOKEN_OPER_EQUAL;
2838
Radek Krejcif03a9e22020-09-18 20:09:31 +02002839 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002840
2841 /* Operator '<', '>' */
2842 tok_len = 1;
2843 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002844
Michal Vasko69730152020-10-09 16:30:07 +02002845 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2846 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2847 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2848 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2849 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2850 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2851 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2852 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2853 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2854 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2855 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2856 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002857
2858 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002859 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002860 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002861 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002862
Radek Krejcif03a9e22020-09-18 20:09:31 +02002863 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002864 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002865 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002866
Radek Krejcif03a9e22020-09-18 20:09:31 +02002867 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002868 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002869 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002870
Radek Krejcif03a9e22020-09-18 20:09:31 +02002871 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002872 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002873 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002874
2875 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002876 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 +02002877 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 +02002878 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002879 goto error;
2880 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002881 LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002882 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002883 goto error;
2884 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002885 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002886
2887 /* NameTest '*' */
2888 tok_len = 1;
2889 tok_type = LYXP_TOKEN_NAMETEST;
2890
2891 } else {
2892
2893 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002894 long int ncname_len = parse_ncname(&expr_str[parsed]);
2895 LY_CHECK_ERR_GOTO(ncname_len < 0,
Radek Krejci2efc45b2020-12-22 16:25:44 +01002896 LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002897 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002898 tok_len = ncname_len;
2899
Radek Krejcif03a9e22020-09-18 20:09:31 +02002900 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002901 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002902 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002903 ++tok_len;
2904 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002905 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
2906 LY_CHECK_ERR_GOTO(ncname_len < 0,
Radek Krejci2efc45b2020-12-22 16:25:44 +01002907 LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002908 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002909 tok_len += ncname_len;
2910 }
2911 /* remove old flag to prevent ambiguities */
2912 prev_function_check = 0;
2913 tok_type = LYXP_TOKEN_NAMETEST;
2914 } else {
2915 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2916 prev_function_check = 1;
2917 tok_type = LYXP_TOKEN_NAMETEST;
2918 }
2919 }
2920
2921 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002922 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002923 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002924 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002925 ++parsed;
2926 }
2927
Radek Krejcif03a9e22020-09-18 20:09:31 +02002928 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002929
Michal Vasko004d3152020-06-11 19:59:22 +02002930 if (reparse) {
2931 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002932 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2933 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002934
Michal Vasko004d3152020-06-11 19:59:22 +02002935 /* fill repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002936 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error);
2937 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002938 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002939 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002940 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002941 goto error;
2942 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002943 }
2944
Radek Krejcif03a9e22020-09-18 20:09:31 +02002945 print_expr_struct_debug(expr);
2946 *expr_p = expr;
2947 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002948
2949error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002950 lyxp_expr_free(ctx, expr);
2951 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002952}
2953
Michal Vasko1734be92020-09-22 08:55:10 +02002954LY_ERR
2955lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02002956{
Michal Vasko1734be92020-09-22 08:55:10 +02002957 LY_ERR ret = LY_SUCCESS;
2958 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02002959 uint32_t i, j;
2960
Michal Vasko7f45cf22020-10-01 12:49:44 +02002961 if (!exp) {
2962 goto cleanup;
2963 }
2964
Michal Vasko004d3152020-06-11 19:59:22 +02002965 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02002966 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002967
2968 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
Michal Vasko1734be92020-09-22 08:55:10 +02002969 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002970 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2971
2972 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
Michal Vasko1734be92020-09-22 08:55:10 +02002973 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002974 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2975
2976 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
Michal Vasko1734be92020-09-22 08:55:10 +02002977 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002978 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2979
2980 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002981 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002982 for (i = 0; i < exp->used; ++i) {
2983 if (!exp->repeat[i]) {
2984 dup->repeat[i] = NULL;
2985 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02002986 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002987 /* the ending 0 as well */
2988 ++j;
2989
Michal Vasko99c71642020-07-03 13:33:36 +02002990 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002991 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002992 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
2993 dup->repeat[i][j - 1] = 0;
2994 }
2995 }
2996
2997 dup->used = exp->used;
2998 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02002999 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003000
Michal Vasko1734be92020-09-22 08:55:10 +02003001cleanup:
3002 if (ret) {
3003 lyxp_expr_free(ctx, dup);
3004 } else {
3005 *dup_p = dup;
3006 }
3007 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003008}
3009
Michal Vasko03ff5a72019-09-11 13:49:33 +02003010/*
3011 * warn functions
3012 *
3013 * Warn functions check specific reasonable conditions for schema XPath
3014 * and print a warning if they are not satisfied.
3015 */
3016
3017/**
3018 * @brief Get the last-added schema node that is currently in the context.
3019 *
3020 * @param[in] set Set to search in.
3021 * @return Last-added schema context node, NULL if no node is in context.
3022 */
3023static struct lysc_node *
3024warn_get_scnode_in_ctx(struct lyxp_set *set)
3025{
3026 uint32_t i;
3027
3028 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3029 return NULL;
3030 }
3031
3032 i = set->used;
3033 do {
3034 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003035 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003036 /* if there are more, simply return the first found (last added) */
3037 return set->val.scnodes[i].scnode;
3038 }
3039 } while (i);
3040
3041 return NULL;
3042}
3043
3044/**
3045 * @brief Test whether a type is numeric - integer type or decimal64.
3046 *
3047 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003048 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003049 */
Radek Krejci857189e2020-09-01 13:26:36 +02003050static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003051warn_is_numeric_type(struct lysc_type *type)
3052{
3053 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003054 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003055 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003056
3057 switch (type->basetype) {
3058 case LY_TYPE_DEC64:
3059 case LY_TYPE_INT8:
3060 case LY_TYPE_UINT8:
3061 case LY_TYPE_INT16:
3062 case LY_TYPE_UINT16:
3063 case LY_TYPE_INT32:
3064 case LY_TYPE_UINT32:
3065 case LY_TYPE_INT64:
3066 case LY_TYPE_UINT64:
3067 return 1;
3068 case LY_TYPE_UNION:
3069 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003070 LY_ARRAY_FOR(uni->types, u) {
3071 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003072 if (ret) {
3073 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003074 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003075 }
3076 }
3077 /* did not find any suitable type */
3078 return 0;
3079 case LY_TYPE_LEAFREF:
3080 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3081 default:
3082 return 0;
3083 }
3084}
3085
3086/**
3087 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3088 *
3089 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003090 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003091 */
Radek Krejci857189e2020-09-01 13:26:36 +02003092static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003093warn_is_string_type(struct lysc_type *type)
3094{
3095 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003096 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003097 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003098
3099 switch (type->basetype) {
3100 case LY_TYPE_BITS:
3101 case LY_TYPE_ENUM:
3102 case LY_TYPE_IDENT:
3103 case LY_TYPE_INST:
3104 case LY_TYPE_STRING:
3105 return 1;
3106 case LY_TYPE_UNION:
3107 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003108 LY_ARRAY_FOR(uni->types, u) {
3109 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003110 if (ret) {
3111 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003112 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003113 }
3114 }
3115 /* did not find any suitable type */
3116 return 0;
3117 case LY_TYPE_LEAFREF:
3118 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3119 default:
3120 return 0;
3121 }
3122}
3123
3124/**
3125 * @brief Test whether a type is one specific type.
3126 *
3127 * @param[in] type Type to test.
3128 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003129 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003130 */
Radek Krejci857189e2020-09-01 13:26:36 +02003131static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003132warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3133{
3134 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003135 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003136 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003137
3138 if (type->basetype == base) {
3139 return 1;
3140 } else if (type->basetype == LY_TYPE_UNION) {
3141 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003142 LY_ARRAY_FOR(uni->types, u) {
3143 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003144 if (ret) {
3145 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003146 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003147 }
3148 }
3149 /* did not find any suitable type */
3150 return 0;
3151 } else if (type->basetype == LY_TYPE_LEAFREF) {
3152 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3153 }
3154
3155 return 0;
3156}
3157
3158/**
3159 * @brief Get next type of a (union) type.
3160 *
3161 * @param[in] type Base type.
3162 * @param[in] prev_type Previously returned type.
3163 * @return Next type or NULL.
3164 */
3165static struct lysc_type *
3166warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3167{
3168 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003169 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003170 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003171
3172 switch (type->basetype) {
3173 case LY_TYPE_UNION:
3174 uni = (struct lysc_type_union *)type;
3175 if (!prev_type) {
3176 return uni->types[0];
3177 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003178 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003179 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003180 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003181 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003182 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003183 found = 1;
3184 }
3185 }
3186 return NULL;
3187 default:
3188 if (prev_type) {
3189 assert(type == prev_type);
3190 return NULL;
3191 } else {
3192 return type;
3193 }
3194 }
3195}
3196
3197/**
3198 * @brief Test whether 2 types have a common type.
3199 *
3200 * @param[in] type1 First type.
3201 * @param[in] type2 Second type.
3202 * @return 1 if they do, 0 otherwise.
3203 */
3204static int
3205warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3206{
3207 struct lysc_type *t1, *rt1, *t2, *rt2;
3208
3209 t1 = NULL;
3210 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3211 if (t1->basetype == LY_TYPE_LEAFREF) {
3212 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3213 } else {
3214 rt1 = t1;
3215 }
3216
3217 t2 = NULL;
3218 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3219 if (t2->basetype == LY_TYPE_LEAFREF) {
3220 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3221 } else {
3222 rt2 = t2;
3223 }
3224
3225 if (rt2->basetype == rt1->basetype) {
3226 /* match found */
3227 return 1;
3228 }
3229 }
3230 }
3231
3232 return 0;
3233}
3234
3235/**
3236 * @brief Check both operands of comparison operators.
3237 *
3238 * @param[in] ctx Context for errors.
3239 * @param[in] set1 First operand set.
3240 * @param[in] set2 Second operand set.
3241 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3242 * @param[in] expr Start of the expression to print with the warning.
3243 * @param[in] tok_pos Token position.
3244 */
3245static void
Radek Krejci857189e2020-09-01 13:26:36 +02003246warn_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 +02003247{
3248 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003249 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003250
3251 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3252 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3253
3254 if (!node1 && !node2) {
3255 /* no node-sets involved, nothing to do */
3256 return;
3257 }
3258
3259 if (node1) {
3260 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3261 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3262 warning = 1;
3263 leaves = 0;
3264 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3265 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3266 warning = 1;
3267 }
3268 }
3269
3270 if (node2) {
3271 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3272 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3273 warning = 1;
3274 leaves = 0;
3275 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3276 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3277 warning = 1;
3278 }
3279 }
3280
3281 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003282 if ((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_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3285 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003286 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3287 warning = 1;
3288 }
3289 }
3290
3291 if (warning) {
3292 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3293 }
3294}
3295
3296/**
3297 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3298 *
3299 * @param[in] exp Parsed XPath expression.
3300 * @param[in] set Set with the leaf/leaf-list.
3301 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3302 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3303 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3304 */
3305static void
Michal Vasko40308e72020-10-20 16:38:40 +02003306warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3307 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003308{
3309 struct lysc_node *scnode;
3310 struct lysc_type *type;
3311 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003312 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003313 LY_ERR rc;
3314 struct ly_err_item *err = NULL;
3315
Michal Vasko69730152020-10-09 16:30:07 +02003316 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3317 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003318 /* check that the node can have the specified value */
3319 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3320 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3321 } else {
3322 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3323 }
3324 if (!value) {
3325 LOGMEM(set->ctx);
3326 return;
3327 }
3328
3329 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3330 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003331 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003332 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003333 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003334 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003335 }
3336
3337 type = ((struct lysc_node_leaf *)scnode)->type;
3338 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003339 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003340 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003341 if (rc == LY_EINCOMPLETE) {
3342 rc = LY_SUCCESS;
3343 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003344
3345 if (err) {
3346 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3347 ly_err_free(err);
3348 } else if (rc != LY_SUCCESS) {
3349 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3350 }
3351 if (rc != LY_SUCCESS) {
3352 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003353 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003354 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003355 } else {
3356 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003357 }
3358 }
3359 free(value);
3360 }
3361}
3362
3363/*
3364 * XPath functions
3365 */
3366
3367/**
3368 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3369 * depending on whether the first node bit value from the second argument is set.
3370 *
3371 * @param[in] args Array of arguments.
3372 * @param[in] arg_count Count of elements in @p args.
3373 * @param[in,out] set Context and result set at the same time.
3374 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003375 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003376 */
3377static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003378xpath_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 +02003379{
3380 struct lyd_node_term *leaf;
3381 struct lysc_node_leaf *sleaf;
3382 struct lysc_type_bits *bits;
3383 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003384 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003385
3386 if (options & LYXP_SCNODE_ALL) {
3387 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3388 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003389 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3390 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 +02003391 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3392 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003393 }
3394
3395 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3396 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3397 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 +02003398 } else if (!warn_is_string_type(sleaf->type)) {
3399 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003400 }
3401 }
3402 set_scnode_clear_ctx(set);
3403 return rc;
3404 }
3405
Michal Vaskod3678892020-05-21 10:06:58 +02003406 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003407 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 +02003408 return LY_EVALID;
3409 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003410 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003411 LY_CHECK_RET(rc);
3412
3413 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003414 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003415 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003416 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3417 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003418 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003419 LY_ARRAY_FOR(bits->bits, u) {
3420 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003421 set_fill_boolean(set, 1);
3422 break;
3423 }
3424 }
3425 }
3426 }
3427
3428 return LY_SUCCESS;
3429}
3430
3431/**
3432 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3433 * with the argument converted to boolean.
3434 *
3435 * @param[in] args Array of arguments.
3436 * @param[in] arg_count Count of elements in @p args.
3437 * @param[in,out] set Context and result set at the same time.
3438 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003439 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003440 */
3441static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003442xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003443{
3444 LY_ERR rc;
3445
3446 if (options & LYXP_SCNODE_ALL) {
3447 set_scnode_clear_ctx(set);
3448 return LY_SUCCESS;
3449 }
3450
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003451 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003452 LY_CHECK_RET(rc);
3453 set_fill_set(set, args[0]);
3454
3455 return LY_SUCCESS;
3456}
3457
3458/**
3459 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3460 * with the first argument rounded up to the nearest integer.
3461 *
3462 * @param[in] args Array of arguments.
3463 * @param[in] arg_count Count of elements in @p args.
3464 * @param[in,out] set Context and result set at the same time.
3465 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003466 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003467 */
3468static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003469xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003470{
3471 struct lysc_node_leaf *sleaf;
3472 LY_ERR rc = LY_SUCCESS;
3473
3474 if (options & LYXP_SCNODE_ALL) {
3475 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3476 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003477 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3478 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 +02003479 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3480 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003481 }
3482 set_scnode_clear_ctx(set);
3483 return rc;
3484 }
3485
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003486 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003487 LY_CHECK_RET(rc);
3488 if ((long long)args[0]->val.num != args[0]->val.num) {
3489 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3490 } else {
3491 set_fill_number(set, args[0]->val.num);
3492 }
3493
3494 return LY_SUCCESS;
3495}
3496
3497/**
3498 * @brief Execute the XPath concat(string, string, string*) function.
3499 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3500 *
3501 * @param[in] args Array of arguments.
3502 * @param[in] arg_count Count of elements in @p args.
3503 * @param[in,out] set Context and result set at the same time.
3504 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003505 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003506 */
3507static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003508xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003509{
3510 uint16_t i;
3511 char *str = NULL;
3512 size_t used = 1;
3513 LY_ERR rc = LY_SUCCESS;
3514 struct lysc_node_leaf *sleaf;
3515
3516 if (options & LYXP_SCNODE_ALL) {
3517 for (i = 0; i < arg_count; ++i) {
3518 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3519 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3520 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003521 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003522 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003523 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 +02003524 }
3525 }
3526 }
3527 set_scnode_clear_ctx(set);
3528 return rc;
3529 }
3530
3531 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003532 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003533 if (rc != LY_SUCCESS) {
3534 free(str);
3535 return rc;
3536 }
3537
3538 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3539 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3540 strcpy(str + used - 1, args[i]->val.str);
3541 used += strlen(args[i]->val.str);
3542 }
3543
3544 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003545 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003546 set->type = LYXP_SET_STRING;
3547 set->val.str = str;
3548
3549 return LY_SUCCESS;
3550}
3551
3552/**
3553 * @brief Execute the XPath contains(string, string) function.
3554 * Returns LYXP_SET_BOOLEAN whether the second argument can
3555 * be found in the first or not.
3556 *
3557 * @param[in] args Array of arguments.
3558 * @param[in] arg_count Count of elements in @p args.
3559 * @param[in,out] set Context and result set at the same time.
3560 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003561 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003562 */
3563static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003564xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003565{
3566 struct lysc_node_leaf *sleaf;
3567 LY_ERR rc = LY_SUCCESS;
3568
3569 if (options & LYXP_SCNODE_ALL) {
3570 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3571 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3572 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 +02003573 } else if (!warn_is_string_type(sleaf->type)) {
3574 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003575 }
3576 }
3577
3578 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3579 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3580 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 +02003581 } else if (!warn_is_string_type(sleaf->type)) {
3582 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003583 }
3584 }
3585 set_scnode_clear_ctx(set);
3586 return rc;
3587 }
3588
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003589 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003590 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003591 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003592 LY_CHECK_RET(rc);
3593
3594 if (strstr(args[0]->val.str, args[1]->val.str)) {
3595 set_fill_boolean(set, 1);
3596 } else {
3597 set_fill_boolean(set, 0);
3598 }
3599
3600 return LY_SUCCESS;
3601}
3602
3603/**
3604 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3605 * with the size of the node-set from the argument.
3606 *
3607 * @param[in] args Array of arguments.
3608 * @param[in] arg_count Count of elements in @p args.
3609 * @param[in,out] set Context and result set at the same time.
3610 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003611 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003612 */
3613static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003614xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003615{
3616 struct lysc_node *scnode = NULL;
3617 LY_ERR rc = LY_SUCCESS;
3618
3619 if (options & LYXP_SCNODE_ALL) {
3620 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3621 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003622 }
3623 set_scnode_clear_ctx(set);
3624 return rc;
3625 }
3626
Michal Vasko03ff5a72019-09-11 13:49:33 +02003627 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003628 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003629 return LY_EVALID;
3630 }
3631
3632 set_fill_number(set, args[0]->used);
3633 return LY_SUCCESS;
3634}
3635
3636/**
3637 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3638 * with the context with the intial node.
3639 *
3640 * @param[in] args Array of arguments.
3641 * @param[in] arg_count Count of elements in @p args.
3642 * @param[in,out] set Context and result set at the same time.
3643 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003644 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003645 */
3646static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003647xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003648{
3649 if (arg_count || args) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003650 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003651 return LY_EVALID;
3652 }
3653
3654 if (options & LYXP_SCNODE_ALL) {
3655 set_scnode_clear_ctx(set);
3656
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003657 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003658 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003659 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003660
3661 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003662 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003663 }
3664
3665 return LY_SUCCESS;
3666}
3667
3668/**
3669 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3670 * leafref or instance-identifier target node(s).
3671 *
3672 * @param[in] args Array of arguments.
3673 * @param[in] arg_count Count of elements in @p args.
3674 * @param[in,out] set Context and result set at the same time.
3675 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003676 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003677 */
3678static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003679xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003680{
3681 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003682 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003683 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003684 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003685 struct ly_path *p;
3686 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003687 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003688 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003689 LY_ERR rc = LY_SUCCESS;
3690
3691 if (options & LYXP_SCNODE_ALL) {
3692 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3693 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003694 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3695 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 +02003696 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3697 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
Michal Vasko69730152020-10-09 16:30:07 +02003698 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003699 }
3700 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003701 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003702 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003703 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003704
3705 /* it was already evaluated on schema, it must succeed */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003706 rc = ly_path_compile(set->ctx, lref->cur_mod, &sleaf->node, lref->path, LY_PATH_LREF_TRUE, oper,
3707 LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, NULL, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003708 assert(!rc);
3709
3710 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003711 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003712 ly_path_free(set->ctx, p);
3713
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003714 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003715 }
3716
Michal Vasko03ff5a72019-09-11 13:49:33 +02003717 return rc;
3718 }
3719
Michal Vaskod3678892020-05-21 10:06:58 +02003720 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003721 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003722 return LY_EVALID;
3723 }
3724
Michal Vaskod3678892020-05-21 10:06:58 +02003725 lyxp_set_free_content(set);
3726 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003727 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3728 sleaf = (struct lysc_node_leaf *)leaf->schema;
3729 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3730 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3731 /* find leafref target */
Michal Vasko004d3152020-06-11 19:59:22 +02003732 if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf,
Michal Vasko69730152020-10-09 16:30:07 +02003733 &leaf->value, set->tree, &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003734 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003735 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003736 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003737 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003738 } else {
3739 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003740 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003741 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Michal Vasko69730152020-10-09 16:30:07 +02003742 LYD_CANON_VALUE(leaf));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003743 return LY_EVALID;
3744 }
3745 }
Michal Vasko004d3152020-06-11 19:59:22 +02003746
3747 /* insert it */
3748 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003749 }
3750 }
3751
3752 return LY_SUCCESS;
3753}
3754
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003755static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003756xpath_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 +02003757{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01003758 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003759 LY_ARRAY_COUNT_TYPE u;
3760 struct lyd_node_term *leaf;
3761 struct lysc_node_leaf *sleaf;
3762 struct lyd_meta *meta;
3763 struct lyd_value data = {0}, *val;
3764 struct ly_err_item *err = NULL;
3765 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003766 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003767
3768 if (options & LYXP_SCNODE_ALL) {
3769 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3770 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
3771 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3772 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003773 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003774 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3775 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3776 }
3777
3778 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3779 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3780 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003781 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003782 } else if (!warn_is_string_type(sleaf->type)) {
3783 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3784 }
3785 }
3786 set_scnode_clear_ctx(set);
3787 return rc;
3788 }
3789
3790 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003791 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 +02003792 return LY_EVALID;
3793 }
3794 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3795 LY_CHECK_RET(rc);
3796
3797 set_fill_boolean(set, 0);
3798 found = 0;
3799 for (i = 0; i < args[0]->used; ++i) {
3800 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3801 continue;
3802 }
3803
3804 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3805 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3806 sleaf = (struct lysc_node_leaf *)leaf->schema;
3807 val = &leaf->value;
3808 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3809 /* uninteresting */
3810 continue;
3811 }
3812
3813 /* store args[1] as ident */
3814 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 +01003815 0, set->format, set->prefix_data, LYD_HINT_DATA, leaf->schema, &data, NULL, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003816 } else {
3817 meta = args[0]->val.meta[i].meta;
3818 val = &meta->value;
3819 if (val->realtype->basetype != LY_TYPE_IDENT) {
3820 /* uninteresting */
3821 continue;
3822 }
3823
3824 /* store args[1] as ident */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003825 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 +01003826 set->format, set->prefix_data, LYD_HINT_DATA, meta->parent->schema, &data, NULL, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003827 }
3828
3829 if (err) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01003830 ly_err_print(set->ctx, err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003831 ly_err_free(err);
3832 }
3833 LY_CHECK_RET(rc);
3834
3835 /* finally check the identity itself */
3836 if (self_match && (data.ident == val->ident)) {
3837 set_fill_boolean(set, 1);
3838 found = 1;
3839 }
3840 if (!found) {
3841 LY_ARRAY_FOR(data.ident->derived, u) {
3842 if (data.ident->derived[u] == val->ident) {
3843 set_fill_boolean(set, 1);
3844 found = 1;
3845 break;
3846 }
3847 }
3848 }
3849
3850 /* free temporary value */
3851 val->realtype->plugin->free(set->ctx, &data);
3852 if (found) {
3853 break;
3854 }
3855 }
3856
3857 return LY_SUCCESS;
3858}
3859
Michal Vasko03ff5a72019-09-11 13:49:33 +02003860/**
3861 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3862 * on whether the first argument nodes contain a node of an identity derived from the second
3863 * argument identity.
3864 *
3865 * @param[in] args Array of arguments.
3866 * @param[in] arg_count Count of elements in @p args.
3867 * @param[in,out] set Context and result set at the same time.
3868 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003869 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003870 */
3871static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003872xpath_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 +02003873{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003874 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003875}
3876
3877/**
3878 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3879 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3880 * the second argument identity.
3881 *
3882 * @param[in] args Array of arguments.
3883 * @param[in] arg_count Count of elements in @p args.
3884 * @param[in,out] set Context and result set at the same time.
3885 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003886 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003887 */
3888static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003889xpath_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 +02003890{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003891 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003892}
3893
3894/**
3895 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3896 * with the integer value of the first node's enum value, otherwise NaN.
3897 *
3898 * @param[in] args Array of arguments.
3899 * @param[in] arg_count Count of elements in @p args.
3900 * @param[in,out] set Context and result set at the same time.
3901 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003902 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003903 */
3904static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003905xpath_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 +02003906{
3907 struct lyd_node_term *leaf;
3908 struct lysc_node_leaf *sleaf;
3909 LY_ERR rc = LY_SUCCESS;
3910
3911 if (options & LYXP_SCNODE_ALL) {
3912 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3913 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003914 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3915 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 +02003916 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3917 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003918 }
3919 set_scnode_clear_ctx(set);
3920 return rc;
3921 }
3922
Michal Vaskod3678892020-05-21 10:06:58 +02003923 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003924 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003925 return LY_EVALID;
3926 }
3927
3928 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003929 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003930 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3931 sleaf = (struct lysc_node_leaf *)leaf->schema;
3932 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3933 set_fill_number(set, leaf->value.enum_item->value);
3934 }
3935 }
3936
3937 return LY_SUCCESS;
3938}
3939
3940/**
3941 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3942 * with false value.
3943 *
3944 * @param[in] args Array of arguments.
3945 * @param[in] arg_count Count of elements in @p args.
3946 * @param[in,out] set Context and result set at the same time.
3947 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003948 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003949 */
3950static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003951xpath_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 +02003952{
3953 if (options & LYXP_SCNODE_ALL) {
3954 set_scnode_clear_ctx(set);
3955 return LY_SUCCESS;
3956 }
3957
3958 set_fill_boolean(set, 0);
3959 return LY_SUCCESS;
3960}
3961
3962/**
3963 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3964 * with the first argument floored (truncated).
3965 *
3966 * @param[in] args Array of arguments.
3967 * @param[in] arg_count Count of elements in @p args.
3968 * @param[in,out] set Context and result set at the same time.
3969 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003970 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003971 */
3972static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003973xpath_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 +02003974{
3975 LY_ERR rc;
3976
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003977 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003978 LY_CHECK_RET(rc);
3979 if (isfinite(args[0]->val.num)) {
3980 set_fill_number(set, (long long)args[0]->val.num);
3981 }
3982
3983 return LY_SUCCESS;
3984}
3985
3986/**
3987 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3988 * whether the language of the text matches the one from the argument.
3989 *
3990 * @param[in] args Array of arguments.
3991 * @param[in] arg_count Count of elements in @p args.
3992 * @param[in,out] set Context and result set at the same time.
3993 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003994 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003995 */
3996static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003997xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003998{
3999 const struct lyd_node *node;
4000 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004001 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004002 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004003 LY_ERR rc = LY_SUCCESS;
4004
4005 if (options & LYXP_SCNODE_ALL) {
4006 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4007 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4008 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 +02004009 } else if (!warn_is_string_type(sleaf->type)) {
4010 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004011 }
4012 }
4013 set_scnode_clear_ctx(set);
4014 return rc;
4015 }
4016
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004017 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004018 LY_CHECK_RET(rc);
4019
Michal Vasko03ff5a72019-09-11 13:49:33 +02004020 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004021 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004022 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004023 } else if (!set->used) {
4024 set_fill_boolean(set, 0);
4025 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004026 }
4027
4028 switch (set->val.nodes[0].type) {
4029 case LYXP_NODE_ELEM:
4030 case LYXP_NODE_TEXT:
4031 node = set->val.nodes[0].node;
4032 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004033 case LYXP_NODE_META:
4034 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004035 break;
4036 default:
4037 /* nothing to do with roots */
4038 set_fill_boolean(set, 0);
4039 return LY_SUCCESS;
4040 }
4041
Michal Vasko9f96a052020-03-10 09:41:45 +01004042 /* find lang metadata */
Michal Vaskod989ba02020-08-24 10:59:24 +02004043 for ( ; node; node = (struct lyd_node *)node->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004044 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004045 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004046 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004047 break;
4048 }
4049 }
4050
Michal Vasko9f96a052020-03-10 09:41:45 +01004051 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004052 break;
4053 }
4054 }
4055
4056 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004057 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004058 set_fill_boolean(set, 0);
4059 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004060 uint64_t i;
4061
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004062 val = meta->value.canonical;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004063 for (i = 0; args[0]->val.str[i]; ++i) {
4064 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4065 set_fill_boolean(set, 0);
4066 break;
4067 }
4068 }
4069 if (!args[0]->val.str[i]) {
4070 if (!val[i] || (val[i] == '-')) {
4071 set_fill_boolean(set, 1);
4072 } else {
4073 set_fill_boolean(set, 0);
4074 }
4075 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004076 }
4077
4078 return LY_SUCCESS;
4079}
4080
4081/**
4082 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4083 * with the context size.
4084 *
4085 * @param[in] args Array of arguments.
4086 * @param[in] arg_count Count of elements in @p args.
4087 * @param[in,out] set Context and result set at the same time.
4088 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004089 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004090 */
4091static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004092xpath_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 +02004093{
4094 if (options & LYXP_SCNODE_ALL) {
4095 set_scnode_clear_ctx(set);
4096 return LY_SUCCESS;
4097 }
4098
Michal Vasko03ff5a72019-09-11 13:49:33 +02004099 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004100 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004101 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004102 } else if (!set->used) {
4103 set_fill_number(set, 0);
4104 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004105 }
4106
4107 set_fill_number(set, set->ctx_size);
4108 return LY_SUCCESS;
4109}
4110
4111/**
4112 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4113 * with the node name without namespace from the argument or the context.
4114 *
4115 * @param[in] args Array of arguments.
4116 * @param[in] arg_count Count of elements in @p args.
4117 * @param[in,out] set Context and result set at the same time.
4118 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004119 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004120 */
4121static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004122xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004123{
4124 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004125
Michal Vasko03ff5a72019-09-11 13:49:33 +02004126 /* suppress unused variable warning */
4127 (void)options;
4128
4129 if (options & LYXP_SCNODE_ALL) {
4130 set_scnode_clear_ctx(set);
4131 return LY_SUCCESS;
4132 }
4133
4134 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004135 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004136 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004137 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004138 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004139 } else if (!args[0]->used) {
4140 set_fill_string(set, "", 0);
4141 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004142 }
4143
4144 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004145 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004146
4147 item = &args[0]->val.nodes[0];
4148 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004149 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004150 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004151 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004152 } else if (!set->used) {
4153 set_fill_string(set, "", 0);
4154 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004155 }
4156
4157 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004158 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004159
4160 item = &set->val.nodes[0];
4161 }
4162
4163 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004164 case LYXP_NODE_NONE:
4165 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004166 case LYXP_NODE_ROOT:
4167 case LYXP_NODE_ROOT_CONFIG:
4168 case LYXP_NODE_TEXT:
4169 set_fill_string(set, "", 0);
4170 break;
4171 case LYXP_NODE_ELEM:
4172 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4173 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004174 case LYXP_NODE_META:
4175 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004176 break;
4177 }
4178
4179 return LY_SUCCESS;
4180}
4181
4182/**
4183 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4184 * with the node name fully qualified (with namespace) from the argument or the context.
4185 *
4186 * @param[in] args Array of arguments.
4187 * @param[in] arg_count Count of elements in @p args.
4188 * @param[in,out] set Context and result set at the same time.
4189 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004190 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004191 */
4192static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004193xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004194{
4195 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004196 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004197 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004198 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004199
4200 if (options & LYXP_SCNODE_ALL) {
4201 set_scnode_clear_ctx(set);
4202 return LY_SUCCESS;
4203 }
4204
4205 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004206 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004207 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004208 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004209 } else if (!args[0]->used) {
4210 set_fill_string(set, "", 0);
4211 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004212 }
4213
4214 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004215 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004216
4217 item = &args[0]->val.nodes[0];
4218 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004219 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004220 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004221 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004222 } else if (!set->used) {
4223 set_fill_string(set, "", 0);
4224 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004225 }
4226
4227 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004228 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004229
4230 item = &set->val.nodes[0];
4231 }
4232
4233 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004234 case LYXP_NODE_NONE:
4235 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004236 case LYXP_NODE_ROOT:
4237 case LYXP_NODE_ROOT_CONFIG:
4238 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004239 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004240 break;
4241 case LYXP_NODE_ELEM:
4242 mod = item->node->schema->module;
4243 name = item->node->schema->name;
4244 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004245 case LYXP_NODE_META:
4246 mod = ((struct lyd_meta *)item->node)->annotation->module;
4247 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004248 break;
4249 }
4250
4251 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004252 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004253 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4254 set_fill_string(set, str, strlen(str));
4255 free(str);
4256 } else {
4257 set_fill_string(set, "", 0);
4258 }
4259
4260 return LY_SUCCESS;
4261}
4262
4263/**
4264 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4265 * with the namespace of the node from the argument or the context.
4266 *
4267 * @param[in] args Array of arguments.
4268 * @param[in] arg_count Count of elements in @p args.
4269 * @param[in,out] set Context and result set at the same time.
4270 * @param[in] options XPath options.
4271 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4272 */
4273static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004274xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004275{
4276 struct lyxp_set_node *item;
4277 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004278
Michal Vasko03ff5a72019-09-11 13:49:33 +02004279 /* suppress unused variable warning */
4280 (void)options;
4281
4282 if (options & LYXP_SCNODE_ALL) {
4283 set_scnode_clear_ctx(set);
4284 return LY_SUCCESS;
4285 }
4286
4287 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004288 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004289 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004290 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004291 return LY_EVALID;
4292 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004293 set_fill_string(set, "", 0);
4294 return LY_SUCCESS;
4295 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004296
4297 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004298 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004299
4300 item = &args[0]->val.nodes[0];
4301 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004302 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004303 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004304 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004305 } else if (!set->used) {
4306 set_fill_string(set, "", 0);
4307 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004308 }
4309
4310 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004311 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004312
4313 item = &set->val.nodes[0];
4314 }
4315
4316 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004317 case LYXP_NODE_NONE:
4318 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004319 case LYXP_NODE_ROOT:
4320 case LYXP_NODE_ROOT_CONFIG:
4321 case LYXP_NODE_TEXT:
4322 set_fill_string(set, "", 0);
4323 break;
4324 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004325 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004326 if (item->type == LYXP_NODE_ELEM) {
4327 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004328 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004329 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004330 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004331 }
4332
4333 set_fill_string(set, mod->ns, strlen(mod->ns));
4334 break;
4335 }
4336
4337 return LY_SUCCESS;
4338}
4339
4340/**
4341 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4342 * with only nodes from the context. In practice it either leaves the context
4343 * as it is or returns an empty node set.
4344 *
4345 * @param[in] args Array of arguments.
4346 * @param[in] arg_count Count of elements in @p args.
4347 * @param[in,out] set Context and result set at the same time.
4348 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004349 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004350 */
4351static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004352xpath_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 +02004353{
4354 if (options & LYXP_SCNODE_ALL) {
4355 set_scnode_clear_ctx(set);
4356 return LY_SUCCESS;
4357 }
4358
4359 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004360 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004361 }
4362 return LY_SUCCESS;
4363}
4364
4365/**
4366 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4367 * with normalized value (no leading, trailing, double white spaces) of the node
4368 * from the argument or the context.
4369 *
4370 * @param[in] args Array of arguments.
4371 * @param[in] arg_count Count of elements in @p args.
4372 * @param[in,out] set Context and result set at the same time.
4373 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004374 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004375 */
4376static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004377xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004378{
4379 uint16_t i, new_used;
4380 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004381 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004382 struct lysc_node_leaf *sleaf;
4383 LY_ERR rc = LY_SUCCESS;
4384
4385 if (options & LYXP_SCNODE_ALL) {
4386 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4387 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4388 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 +02004389 } else if (!warn_is_string_type(sleaf->type)) {
4390 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004391 }
4392 }
4393 set_scnode_clear_ctx(set);
4394 return rc;
4395 }
4396
4397 if (arg_count) {
4398 set_fill_set(set, args[0]);
4399 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004400 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004401 LY_CHECK_RET(rc);
4402
4403 /* is there any normalization necessary? */
4404 for (i = 0; set->val.str[i]; ++i) {
4405 if (is_xmlws(set->val.str[i])) {
4406 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4407 have_spaces = 1;
4408 break;
4409 }
4410 space_before = 1;
4411 } else {
4412 space_before = 0;
4413 }
4414 }
4415
4416 /* yep, there is */
4417 if (have_spaces) {
4418 /* it's enough, at least one character will go, makes space for ending '\0' */
4419 new = malloc(strlen(set->val.str) * sizeof(char));
4420 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4421 new_used = 0;
4422
4423 space_before = 0;
4424 for (i = 0; set->val.str[i]; ++i) {
4425 if (is_xmlws(set->val.str[i])) {
4426 if ((i == 0) || space_before) {
4427 space_before = 1;
4428 continue;
4429 } else {
4430 space_before = 1;
4431 }
4432 } else {
4433 space_before = 0;
4434 }
4435
4436 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4437 ++new_used;
4438 }
4439
4440 /* at worst there is one trailing space now */
4441 if (new_used && is_xmlws(new[new_used - 1])) {
4442 --new_used;
4443 }
4444
4445 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4446 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4447 new[new_used] = '\0';
4448
4449 free(set->val.str);
4450 set->val.str = new;
4451 }
4452
4453 return LY_SUCCESS;
4454}
4455
4456/**
4457 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4458 * with the argument converted to boolean and logically inverted.
4459 *
4460 * @param[in] args Array of arguments.
4461 * @param[in] arg_count Count of elements in @p args.
4462 * @param[in,out] set Context and result set at the same time.
4463 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004464 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004465 */
4466static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004467xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004468{
4469 if (options & LYXP_SCNODE_ALL) {
4470 set_scnode_clear_ctx(set);
4471 return LY_SUCCESS;
4472 }
4473
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004474 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004475 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004476 set_fill_boolean(set, 0);
4477 } else {
4478 set_fill_boolean(set, 1);
4479 }
4480
4481 return LY_SUCCESS;
4482}
4483
4484/**
4485 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4486 * with the number representation of either the argument or the context.
4487 *
4488 * @param[in] args Array of arguments.
4489 * @param[in] arg_count Count of elements in @p args.
4490 * @param[in,out] set Context and result set at the same time.
4491 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004492 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004493 */
4494static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004495xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004496{
4497 LY_ERR rc;
4498
4499 if (options & LYXP_SCNODE_ALL) {
4500 set_scnode_clear_ctx(set);
4501 return LY_SUCCESS;
4502 }
4503
4504 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004505 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004506 LY_CHECK_RET(rc);
4507 set_fill_set(set, args[0]);
4508 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004509 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004510 LY_CHECK_RET(rc);
4511 }
4512
4513 return LY_SUCCESS;
4514}
4515
4516/**
4517 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4518 * with the context position.
4519 *
4520 * @param[in] args Array of arguments.
4521 * @param[in] arg_count Count of elements in @p args.
4522 * @param[in,out] set Context and result set at the same time.
4523 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004524 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004525 */
4526static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004527xpath_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 +02004528{
4529 if (options & LYXP_SCNODE_ALL) {
4530 set_scnode_clear_ctx(set);
4531 return LY_SUCCESS;
4532 }
4533
Michal Vasko03ff5a72019-09-11 13:49:33 +02004534 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004535 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004536 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004537 } else if (!set->used) {
4538 set_fill_number(set, 0);
4539 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004540 }
4541
4542 set_fill_number(set, set->ctx_pos);
4543
4544 /* UNUSED in 'Release' build type */
4545 (void)options;
4546 return LY_SUCCESS;
4547}
4548
4549/**
4550 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4551 * depending on whether the second argument regex matches the first argument string. For details refer to
4552 * YANG 1.1 RFC section 10.2.1.
4553 *
4554 * @param[in] args Array of arguments.
4555 * @param[in] arg_count Count of elements in @p args.
4556 * @param[in,out] set Context and result set at the same time.
4557 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004558 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004559 */
4560static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004561xpath_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 +02004562{
4563 struct lysc_pattern **patterns = NULL, **pattern;
4564 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004565 LY_ERR rc = LY_SUCCESS;
4566 struct ly_err_item *err;
4567
4568 if (options & LYXP_SCNODE_ALL) {
4569 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4570 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4571 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 +02004572 } else if (!warn_is_string_type(sleaf->type)) {
4573 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004574 }
4575 }
4576
4577 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4578 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4579 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 +02004580 } else if (!warn_is_string_type(sleaf->type)) {
4581 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004582 }
4583 }
4584 set_scnode_clear_ctx(set);
4585 return rc;
4586 }
4587
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004588 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004589 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004590 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004591 LY_CHECK_RET(rc);
4592
4593 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4594 *pattern = malloc(sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004595 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004596 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Radek Krejciddace2c2021-01-08 11:30:56 +01004597 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004598 if (rc != LY_SUCCESS) {
4599 LY_ARRAY_FREE(patterns);
4600 return rc;
4601 }
4602
4603 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4604 pcre2_code_free((*pattern)->code);
4605 free(*pattern);
4606 LY_ARRAY_FREE(patterns);
4607 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004608 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004609 ly_err_free(err);
4610 return rc;
4611 }
4612
4613 if (rc == LY_EVALID) {
4614 ly_err_free(err);
4615 set_fill_boolean(set, 0);
4616 } else {
4617 set_fill_boolean(set, 1);
4618 }
4619
4620 return LY_SUCCESS;
4621}
4622
4623/**
4624 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4625 * with the rounded first argument. For details refer to
4626 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4627 *
4628 * @param[in] args Array of arguments.
4629 * @param[in] arg_count Count of elements in @p args.
4630 * @param[in,out] set Context and result set at the same time.
4631 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004632 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004633 */
4634static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004635xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004636{
4637 struct lysc_node_leaf *sleaf;
4638 LY_ERR rc = LY_SUCCESS;
4639
4640 if (options & LYXP_SCNODE_ALL) {
4641 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4642 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004643 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4644 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 +02004645 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4646 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004647 }
4648 set_scnode_clear_ctx(set);
4649 return rc;
4650 }
4651
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004652 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004653 LY_CHECK_RET(rc);
4654
4655 /* cover only the cases where floor can't be used */
4656 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4657 set_fill_number(set, -0.0f);
4658 } else {
4659 args[0]->val.num += 0.5;
4660 rc = xpath_floor(args, 1, args[0], options);
4661 LY_CHECK_RET(rc);
4662 set_fill_number(set, args[0]->val.num);
4663 }
4664
4665 return LY_SUCCESS;
4666}
4667
4668/**
4669 * @brief Execute the XPath starts-with(string, string) function.
4670 * Returns LYXP_SET_BOOLEAN whether the second argument is
4671 * the prefix of the first or not.
4672 *
4673 * @param[in] args Array of arguments.
4674 * @param[in] arg_count Count of elements in @p args.
4675 * @param[in,out] set Context and result set at the same time.
4676 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004677 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004678 */
4679static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004680xpath_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 +02004681{
4682 struct lysc_node_leaf *sleaf;
4683 LY_ERR rc = LY_SUCCESS;
4684
4685 if (options & LYXP_SCNODE_ALL) {
4686 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4687 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4688 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 +02004689 } else if (!warn_is_string_type(sleaf->type)) {
4690 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004691 }
4692 }
4693
4694 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4695 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4696 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 +02004697 } else if (!warn_is_string_type(sleaf->type)) {
4698 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004699 }
4700 }
4701 set_scnode_clear_ctx(set);
4702 return rc;
4703 }
4704
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004705 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004706 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004707 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004708 LY_CHECK_RET(rc);
4709
4710 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4711 set_fill_boolean(set, 0);
4712 } else {
4713 set_fill_boolean(set, 1);
4714 }
4715
4716 return LY_SUCCESS;
4717}
4718
4719/**
4720 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4721 * with the string representation of either the argument or the context.
4722 *
4723 * @param[in] args Array of arguments.
4724 * @param[in] arg_count Count of elements in @p args.
4725 * @param[in,out] set Context and result set at the same time.
4726 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004727 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004728 */
4729static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004730xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004731{
4732 LY_ERR rc;
4733
4734 if (options & LYXP_SCNODE_ALL) {
4735 set_scnode_clear_ctx(set);
4736 return LY_SUCCESS;
4737 }
4738
4739 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004740 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004741 LY_CHECK_RET(rc);
4742 set_fill_set(set, args[0]);
4743 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004744 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004745 LY_CHECK_RET(rc);
4746 }
4747
4748 return LY_SUCCESS;
4749}
4750
4751/**
4752 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4753 * with the length of the string in either the argument or the context.
4754 *
4755 * @param[in] args Array of arguments.
4756 * @param[in] arg_count Count of elements in @p args.
4757 * @param[in,out] set Context and result set at the same time.
4758 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004759 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004760 */
4761static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004762xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004763{
4764 struct lysc_node_leaf *sleaf;
4765 LY_ERR rc = LY_SUCCESS;
4766
4767 if (options & LYXP_SCNODE_ALL) {
4768 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4769 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4770 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 +02004771 } else if (!warn_is_string_type(sleaf->type)) {
4772 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004773 }
4774 }
4775 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4776 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4777 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 +02004778 } else if (!warn_is_string_type(sleaf->type)) {
4779 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004780 }
4781 }
4782 set_scnode_clear_ctx(set);
4783 return rc;
4784 }
4785
4786 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004787 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004788 LY_CHECK_RET(rc);
4789 set_fill_number(set, strlen(args[0]->val.str));
4790 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004791 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004792 LY_CHECK_RET(rc);
4793 set_fill_number(set, strlen(set->val.str));
4794 }
4795
4796 return LY_SUCCESS;
4797}
4798
4799/**
4800 * @brief Execute the XPath substring(string, number, number?) function.
4801 * Returns LYXP_SET_STRING substring of the first argument starting
4802 * on the second argument index ending on the third argument index,
4803 * indexed from 1. For exact definition refer to
4804 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4805 *
4806 * @param[in] args Array of arguments.
4807 * @param[in] arg_count Count of elements in @p args.
4808 * @param[in,out] set Context and result set at the same time.
4809 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004810 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004811 */
4812static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004813xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004814{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004815 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004816 uint16_t str_start, str_len, pos;
4817 struct lysc_node_leaf *sleaf;
4818 LY_ERR rc = LY_SUCCESS;
4819
4820 if (options & LYXP_SCNODE_ALL) {
4821 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4822 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4823 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 +02004824 } else if (!warn_is_string_type(sleaf->type)) {
4825 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004826 }
4827 }
4828
4829 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4830 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4831 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 +02004832 } else if (!warn_is_numeric_type(sleaf->type)) {
4833 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004834 }
4835 }
4836
Michal Vasko69730152020-10-09 16:30:07 +02004837 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4838 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004839 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4840 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 +02004841 } else if (!warn_is_numeric_type(sleaf->type)) {
4842 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004843 }
4844 }
4845 set_scnode_clear_ctx(set);
4846 return rc;
4847 }
4848
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004849 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004850 LY_CHECK_RET(rc);
4851
4852 /* start */
4853 if (xpath_round(&args[1], 1, args[1], options)) {
4854 return -1;
4855 }
4856 if (isfinite(args[1]->val.num)) {
4857 start = args[1]->val.num - 1;
4858 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004859 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004860 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004861 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004862 }
4863
4864 /* len */
4865 if (arg_count == 3) {
4866 rc = xpath_round(&args[2], 1, args[2], options);
4867 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004868 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004869 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004870 } else if (isfinite(args[2]->val.num)) {
4871 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004872 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004873 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004874 }
4875 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004876 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004877 }
4878
4879 /* find matching character positions */
4880 str_start = 0;
4881 str_len = 0;
4882 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4883 if (pos < start) {
4884 ++str_start;
4885 } else if (pos < start + len) {
4886 ++str_len;
4887 } else {
4888 break;
4889 }
4890 }
4891
4892 set_fill_string(set, args[0]->val.str + str_start, str_len);
4893 return LY_SUCCESS;
4894}
4895
4896/**
4897 * @brief Execute the XPath substring-after(string, string) function.
4898 * Returns LYXP_SET_STRING with the string succeeding the occurance
4899 * of the second argument in the first or an empty string.
4900 *
4901 * @param[in] args Array of arguments.
4902 * @param[in] arg_count Count of elements in @p args.
4903 * @param[in,out] set Context and result set at the same time.
4904 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004905 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004906 */
4907static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004908xpath_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 +02004909{
4910 char *ptr;
4911 struct lysc_node_leaf *sleaf;
4912 LY_ERR rc = LY_SUCCESS;
4913
4914 if (options & LYXP_SCNODE_ALL) {
4915 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4916 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4917 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 +02004918 } else if (!warn_is_string_type(sleaf->type)) {
4919 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004920 }
4921 }
4922
4923 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4924 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4925 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 +02004926 } else if (!warn_is_string_type(sleaf->type)) {
4927 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004928 }
4929 }
4930 set_scnode_clear_ctx(set);
4931 return rc;
4932 }
4933
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004934 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004935 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004936 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004937 LY_CHECK_RET(rc);
4938
4939 ptr = strstr(args[0]->val.str, args[1]->val.str);
4940 if (ptr) {
4941 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4942 } else {
4943 set_fill_string(set, "", 0);
4944 }
4945
4946 return LY_SUCCESS;
4947}
4948
4949/**
4950 * @brief Execute the XPath substring-before(string, string) function.
4951 * Returns LYXP_SET_STRING with the string preceding the occurance
4952 * of the second argument in the first or an empty string.
4953 *
4954 * @param[in] args Array of arguments.
4955 * @param[in] arg_count Count of elements in @p args.
4956 * @param[in,out] set Context and result set at the same time.
4957 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004958 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004959 */
4960static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004961xpath_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 +02004962{
4963 char *ptr;
4964 struct lysc_node_leaf *sleaf;
4965 LY_ERR rc = LY_SUCCESS;
4966
4967 if (options & LYXP_SCNODE_ALL) {
4968 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4969 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4970 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 +02004971 } else if (!warn_is_string_type(sleaf->type)) {
4972 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004973 }
4974 }
4975
4976 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4977 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4978 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 +02004979 } else if (!warn_is_string_type(sleaf->type)) {
4980 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004981 }
4982 }
4983 set_scnode_clear_ctx(set);
4984 return rc;
4985 }
4986
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004987 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004988 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004989 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004990 LY_CHECK_RET(rc);
4991
4992 ptr = strstr(args[0]->val.str, args[1]->val.str);
4993 if (ptr) {
4994 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4995 } else {
4996 set_fill_string(set, "", 0);
4997 }
4998
4999 return LY_SUCCESS;
5000}
5001
5002/**
5003 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5004 * with the sum of all the nodes in the context.
5005 *
5006 * @param[in] args Array of arguments.
5007 * @param[in] arg_count Count of elements in @p args.
5008 * @param[in,out] set Context and result set at the same time.
5009 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005010 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005011 */
5012static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005013xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005014{
5015 long double num;
5016 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005017 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005018 struct lyxp_set set_item;
5019 struct lysc_node_leaf *sleaf;
5020 LY_ERR rc = LY_SUCCESS;
5021
5022 if (options & LYXP_SCNODE_ALL) {
5023 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5024 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005025 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005026 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5027 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5028 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005029 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005030 } else if (!warn_is_numeric_type(sleaf->type)) {
5031 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005032 }
5033 }
5034 }
5035 }
5036 set_scnode_clear_ctx(set);
5037 return rc;
5038 }
5039
5040 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005041
5042 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005043 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005044 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005045 } else if (!args[0]->used) {
5046 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005047 }
5048
Michal Vasko5c4e5892019-11-14 12:31:38 +01005049 set_init(&set_item, set);
5050
Michal Vasko03ff5a72019-09-11 13:49:33 +02005051 set_item.type = LYXP_SET_NODE_SET;
5052 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5053 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5054
5055 set_item.used = 1;
5056 set_item.size = 1;
5057
5058 for (i = 0; i < args[0]->used; ++i) {
5059 set_item.val.nodes[0] = args[0]->val.nodes[i];
5060
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005061 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005062 LY_CHECK_RET(rc);
5063 num = cast_string_to_number(str);
5064 free(str);
5065 set->val.num += num;
5066 }
5067
5068 free(set_item.val.nodes);
5069
5070 return LY_SUCCESS;
5071}
5072
5073/**
5074 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5075 * with the text content of the nodes in the context.
5076 *
5077 * @param[in] args Array of arguments.
5078 * @param[in] arg_count Count of elements in @p args.
5079 * @param[in,out] set Context and result set at the same time.
5080 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005081 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005082 */
5083static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005084xpath_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 +02005085{
5086 uint32_t i;
5087
5088 if (options & LYXP_SCNODE_ALL) {
5089 set_scnode_clear_ctx(set);
5090 return LY_SUCCESS;
5091 }
5092
Michal Vasko03ff5a72019-09-11 13:49:33 +02005093 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005094 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005095 return LY_EVALID;
5096 }
5097
Michal Vaskod989ba02020-08-24 10:59:24 +02005098 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005099 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005100 case LYXP_NODE_NONE:
5101 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005102 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005103 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5104 set->val.nodes[i].type = LYXP_NODE_TEXT;
5105 ++i;
5106 break;
5107 }
Radek Krejci0f969882020-08-21 16:56:47 +02005108 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005109 case LYXP_NODE_ROOT:
5110 case LYXP_NODE_ROOT_CONFIG:
5111 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005112 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005113 set_remove_node(set, i);
5114 break;
5115 }
5116 }
5117
5118 return LY_SUCCESS;
5119}
5120
5121/**
5122 * @brief Execute the XPath translate(string, string, string) function.
5123 * Returns LYXP_SET_STRING with the first argument with the characters
5124 * from the second argument replaced by those on the corresponding
5125 * positions in the third argument.
5126 *
5127 * @param[in] args Array of arguments.
5128 * @param[in] arg_count Count of elements in @p args.
5129 * @param[in,out] set Context and result set at the same time.
5130 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005131 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005132 */
5133static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005134xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005135{
5136 uint16_t i, j, new_used;
5137 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005138 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005139 struct lysc_node_leaf *sleaf;
5140 LY_ERR rc = LY_SUCCESS;
5141
5142 if (options & LYXP_SCNODE_ALL) {
5143 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5144 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5145 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 +02005146 } else if (!warn_is_string_type(sleaf->type)) {
5147 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005148 }
5149 }
5150
5151 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5152 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5153 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 +02005154 } else if (!warn_is_string_type(sleaf->type)) {
5155 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005156 }
5157 }
5158
5159 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5160 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5161 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 +02005162 } else if (!warn_is_string_type(sleaf->type)) {
5163 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005164 }
5165 }
5166 set_scnode_clear_ctx(set);
5167 return rc;
5168 }
5169
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005170 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005171 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005172 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005173 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005174 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005175 LY_CHECK_RET(rc);
5176
5177 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5178 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5179 new_used = 0;
5180
5181 have_removed = 0;
5182 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005183 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005184
5185 for (j = 0; args[1]->val.str[j]; ++j) {
5186 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5187 /* removing this char */
5188 if (j >= strlen(args[2]->val.str)) {
5189 have_removed = 1;
5190 found = 1;
5191 break;
5192 }
5193 /* replacing this char */
5194 new[new_used] = args[2]->val.str[j];
5195 ++new_used;
5196 found = 1;
5197 break;
5198 }
5199 }
5200
5201 /* copying this char */
5202 if (!found) {
5203 new[new_used] = args[0]->val.str[i];
5204 ++new_used;
5205 }
5206 }
5207
5208 if (have_removed) {
5209 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5210 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5211 }
5212 new[new_used] = '\0';
5213
Michal Vaskod3678892020-05-21 10:06:58 +02005214 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005215 set->type = LYXP_SET_STRING;
5216 set->val.str = new;
5217
5218 return LY_SUCCESS;
5219}
5220
5221/**
5222 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5223 * with true value.
5224 *
5225 * @param[in] args Array of arguments.
5226 * @param[in] arg_count Count of elements in @p args.
5227 * @param[in,out] set Context and result set at the same time.
5228 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005229 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005230 */
5231static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005232xpath_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 +02005233{
5234 if (options & LYXP_SCNODE_ALL) {
5235 set_scnode_clear_ctx(set);
5236 return LY_SUCCESS;
5237 }
5238
5239 set_fill_boolean(set, 1);
5240 return LY_SUCCESS;
5241}
5242
5243/*
5244 * moveto functions
5245 *
5246 * They and only they actually change the context (set).
5247 */
5248
5249/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005250 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005251 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005252 * XPath @p set is expected to be a (sc)node set!
5253 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005254 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5255 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005256 * @param[in] set Set with general XPath context.
5257 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005258 * @param[out] moveto_mod Expected module of a matching node.
5259 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005260 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005261static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005262moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5263 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005264{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005265 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005266 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005267 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005268
Michal Vasko2104e9f2020-03-06 08:23:25 +01005269 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5270
Michal Vasko6346ece2019-09-24 13:12:53 +02005271 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5272 /* specific module */
5273 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005274 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005275
Michal Vasko004d3152020-06-11 19:59:22 +02005276 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005277 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005278 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005279 return LY_EVALID;
5280 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005281
Michal Vasko6346ece2019-09-24 13:12:53 +02005282 *qname += pref_len + 1;
5283 *qname_len -= pref_len + 1;
5284 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5285 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005286 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005287 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005288 switch (set->format) {
5289 case LY_PREF_SCHEMA:
5290 case LY_PREF_SCHEMA_RESOLVED:
5291 /* current module */
5292 mod = set->cur_mod;
5293 break;
5294 case LY_PREF_JSON:
5295 /* inherit parent (context node) module */
5296 if (ctx_scnode) {
5297 mod = ctx_scnode->module;
5298 } else {
5299 mod = NULL;
5300 }
5301 break;
5302 case LY_PREF_XML:
5303 /* not defined */
5304 LOGINT_RET(set->ctx);
5305 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005306 }
5307
Michal Vasko6346ece2019-09-24 13:12:53 +02005308 *moveto_mod = mod;
5309 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005310}
5311
5312/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005313 * @brief Move context @p set to the root. Handles absolute path.
5314 * Result is LYXP_SET_NODE_SET.
5315 *
5316 * @param[in,out] set Set to use.
5317 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005318 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005319 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005320static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005321moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005322{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005323 if (!set) {
Michal Vaskob0099a92020-08-31 14:55:23 +02005324 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005325 }
5326
5327 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005328 set_scnode_clear_ctx(set);
Michal Vaskob0099a92020-08-31 14:55:23 +02005329 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005330 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005331 set->type = LYXP_SET_NODE_SET;
5332 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005333 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005334 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005335
5336 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005337}
5338
5339/**
5340 * @brief Check @p node as a part of NameTest processing.
5341 *
5342 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005343 * @param[in] ctx_node Context node.
5344 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005345 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005346 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005347 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005348 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5349 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005350 */
5351static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005352moveto_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 +01005353 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005354{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005355 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5356 switch (set->format) {
5357 case LY_PREF_SCHEMA:
5358 case LY_PREF_SCHEMA_RESOLVED:
5359 /* use current module */
5360 moveto_mod = set->cur_mod;
5361 break;
5362 case LY_PREF_JSON:
5363 /* inherit module of the context node, if any */
5364 if (ctx_node) {
5365 moveto_mod = ctx_node->schema->module;
5366 }
5367 break;
5368 case LY_PREF_XML:
5369 /* not defined */
5370 LOGINT(set->ctx);
5371 return LY_EINVAL;
5372 }
5373 }
5374
Michal Vasko03ff5a72019-09-11 13:49:33 +02005375 /* module check */
5376 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005377 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005378 }
5379
Michal Vasko5c4e5892019-11-14 12:31:38 +01005380 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005381 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005382 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005383 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5384 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005385 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005386 }
5387
5388 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005389 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005390 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005391 }
5392
Michal Vaskoa1424542019-11-14 16:08:52 +01005393 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005394 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005395 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005396 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005397
5398 /* match */
5399 return LY_SUCCESS;
5400}
5401
5402/**
5403 * @brief Check @p node as a part of schema NameTest processing.
5404 *
5405 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005406 * @param[in] ctx_scnode Context node.
5407 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005408 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005409 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005410 * @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 +02005411 */
5412static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005413moveto_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 +02005414 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005415{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005416 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5417 switch (set->format) {
5418 case LY_PREF_SCHEMA:
5419 case LY_PREF_SCHEMA_RESOLVED:
5420 /* use current module */
5421 moveto_mod = set->cur_mod;
5422 break;
5423 case LY_PREF_JSON:
5424 /* inherit module of the context node, if any */
5425 if (ctx_scnode) {
5426 moveto_mod = ctx_scnode->module;
5427 }
5428 break;
5429 case LY_PREF_XML:
5430 /* not defined */
5431 LOGINT(set->ctx);
5432 return LY_EINVAL;
5433 }
5434 }
5435
Michal Vasko03ff5a72019-09-11 13:49:33 +02005436 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005437 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005438 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005439 }
5440
5441 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005442 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005443 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005444 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005445 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005446 }
5447
5448 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005449 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005450 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005451 }
5452
5453 /* match */
5454 return LY_SUCCESS;
5455}
5456
5457/**
Michal Vaskod3678892020-05-21 10:06:58 +02005458 * @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 +02005459 *
5460 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005461 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005462 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005463 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005464 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5465 */
5466static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005467moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005468{
Michal Vaskof03ed032020-03-04 13:31:44 +01005469 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005470 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005471 LY_ERR rc;
5472
Michal Vaskod3678892020-05-21 10:06:58 +02005473 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005474 return LY_SUCCESS;
5475 }
5476
5477 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005478 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005479 return LY_EVALID;
5480 }
5481
Michal Vasko03ff5a72019-09-11 13:49:33 +02005482 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005483 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005484
5485 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 +01005486 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005487
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005488 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005489 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005490 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005491 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005492 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005493 ctx_node = set->val.nodes[i].node;
5494 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005495 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005496
Michal Vaskod3678892020-05-21 10:06:58 +02005497 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005498 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005499 if (rc == LY_SUCCESS) {
5500 if (!replaced) {
5501 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5502 replaced = 1;
5503 } else {
5504 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005505 }
Michal Vaskod3678892020-05-21 10:06:58 +02005506 ++i;
5507 } else if (rc == LY_EINCOMPLETE) {
5508 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005509 }
5510 }
5511
5512 if (!replaced) {
5513 /* no match */
5514 set_remove_node(set, i);
5515 }
5516 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005517
5518 return LY_SUCCESS;
5519}
5520
5521/**
Michal Vaskod3678892020-05-21 10:06:58 +02005522 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5523 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005524 *
5525 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005526 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005527 * @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 +01005528 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005529 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5530 */
5531static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005532moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5533 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005534{
Michal Vasko004d3152020-06-11 19:59:22 +02005535 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005536 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005537 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005538 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005539
Michal Vasko004d3152020-06-11 19:59:22 +02005540 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005541
5542 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005543 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005544 }
5545
5546 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005547 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005548 ret = LY_EVALID;
5549 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005550 }
5551
5552 /* context check for all the nodes since we have the schema node */
5553 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5554 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005555 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005556 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5557 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005558 lyxp_set_free_content(set);
5559 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005560 }
5561
5562 /* create specific data instance if needed */
5563 if (scnode->nodetype == LYS_LIST) {
5564 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5565 } else if (scnode->nodetype == LYS_LEAFLIST) {
5566 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005567 }
5568
5569 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005570 siblings = NULL;
5571
5572 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5573 assert(!set->val.nodes[i].node);
5574
5575 /* search in all the trees */
5576 siblings = set->tree;
5577 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5578 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005579 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005580 }
5581
5582 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005583 if (inst) {
5584 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005585 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005586 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005587 }
5588
5589 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005590 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005591 ret = LY_EINCOMPLETE;
5592 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005593 }
5594
5595 if (sub) {
5596 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005597 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005598 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005599 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005600 /* no match */
5601 set_remove_node(set, i);
5602 }
5603 }
5604
Michal Vasko004d3152020-06-11 19:59:22 +02005605cleanup:
5606 lyd_free_tree(inst);
5607 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005608}
5609
5610/**
5611 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5612 *
5613 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005614 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005615 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005616 * @param[in] options XPath options.
5617 * @return LY_ERR
5618 */
5619static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005620moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005621{
Radek Krejci857189e2020-09-01 13:26:36 +02005622 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005623 uint32_t getnext_opts;
5624 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005625 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005626 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005627 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005628
Michal Vaskod3678892020-05-21 10:06:58 +02005629 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005630 return LY_SUCCESS;
5631 }
5632
5633 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005634 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005635 return LY_EVALID;
5636 }
5637
Michal Vaskocafad9d2019-11-07 15:20:03 +01005638 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005639 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005640 if (options & LYXP_SCNODE_OUTPUT) {
5641 getnext_opts |= LYS_GETNEXT_OUTPUT;
5642 }
5643
Michal Vasko03ff5a72019-09-11 13:49:33 +02005644 orig_used = set->used;
5645 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005646 uint32_t idx;
5647
Radek Krejcif13b87b2020-12-01 22:02:17 +01005648 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5649 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005650 continue;
5651 }
5652
5653 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005654 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005655 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005656 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005657 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005658
5659 start_parent = set->val.scnodes[i].scnode;
5660
5661 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 +02005662 /* 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 +02005663 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005664 mod_idx = 0;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005665 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005666 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005667 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005668 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005669 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005670 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5671
Michal Vasko03ff5a72019-09-11 13:49:33 +02005672 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005673 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005674 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005675 temp_ctx = 1;
5676 }
5677 }
5678 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005679 }
5680
Michal Vasko519fd602020-05-26 12:17:39 +02005681 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5682 iter = NULL;
5683 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005684 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005685 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5686
5687 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005688 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005689 temp_ctx = 1;
5690 }
5691 }
5692 }
5693 }
5694 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005695
5696 /* correct temporary in_ctx values */
5697 if (temp_ctx) {
5698 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005699 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5700 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005701 }
5702 }
5703 }
5704
5705 return LY_SUCCESS;
5706}
5707
5708/**
Michal Vaskod3678892020-05-21 10:06:58 +02005709 * @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 +02005710 * Context position aware.
5711 *
5712 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005713 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005714 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005715 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005716 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5717 */
5718static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005719moveto_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 +02005720{
5721 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005722 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005723 struct lyxp_set ret_set;
5724 LY_ERR rc;
5725
Michal Vaskod3678892020-05-21 10:06:58 +02005726 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005727 return LY_SUCCESS;
5728 }
5729
5730 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005731 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005732 return LY_EVALID;
5733 }
5734
Michal Vasko9f96a052020-03-10 09:41:45 +01005735 /* 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 +01005736 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005737 LY_CHECK_RET(rc);
5738
Michal Vasko6346ece2019-09-24 13:12:53 +02005739 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005740 set_init(&ret_set, set);
5741 for (i = 0; i < set->used; ++i) {
5742
5743 /* TREE DFS */
5744 start = set->val.nodes[i].node;
5745 for (elem = next = start; elem; elem = next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005746 rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005747 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005748 /* add matching node into result set */
5749 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5750 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5751 /* the node is a duplicate, we'll process it later in the set */
5752 goto skip_children;
5753 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005754 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005755 return rc;
5756 } else if (rc == LY_EINVAL) {
5757 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005758 }
5759
5760 /* TREE DFS NEXT ELEM */
5761 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005762 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005763 if (!next) {
5764skip_children:
5765 /* no children, so try siblings, but only if it's not the start,
5766 * that is considered to be the root and it's siblings are not traversed */
5767 if (elem != start) {
5768 next = elem->next;
5769 } else {
5770 break;
5771 }
5772 }
5773 while (!next) {
5774 /* no siblings, go back through the parents */
5775 if ((struct lyd_node *)elem->parent == start) {
5776 /* we are done, no next element to process */
5777 break;
5778 }
5779 /* parent is already processed, go to its sibling */
5780 elem = (struct lyd_node *)elem->parent;
5781 next = elem->next;
5782 }
5783 }
5784 }
5785
5786 /* make the temporary set the current one */
5787 ret_set.ctx_pos = set->ctx_pos;
5788 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005789 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005790 memcpy(set, &ret_set, sizeof *set);
5791
5792 return LY_SUCCESS;
5793}
5794
5795/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005796 * @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 +02005797 *
5798 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005799 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005800 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005801 * @param[in] options XPath options.
5802 * @return LY_ERR
5803 */
5804static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005805moveto_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 +02005806{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005807 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005808 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005809 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005810
Michal Vaskod3678892020-05-21 10:06:58 +02005811 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005812 return LY_SUCCESS;
5813 }
5814
5815 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005816 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005817 return LY_EVALID;
5818 }
5819
Michal Vasko03ff5a72019-09-11 13:49:33 +02005820 orig_used = set->used;
5821 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005822 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5823 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005824 continue;
5825 }
5826
5827 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005828 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005829 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005830 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005831 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005832
5833 /* TREE DFS */
5834 start = set->val.scnodes[i].scnode;
5835 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005836 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5837 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005838 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005839 }
5840
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005841 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005842 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005843 uint32_t idx;
5844
5845 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005846 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005847 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005848 /* we will process it later in the set */
5849 goto skip_children;
5850 }
5851 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005852 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005853 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005854 } else if (rc == LY_EINVAL) {
5855 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005856 }
5857
5858next_iter:
5859 /* TREE DFS NEXT ELEM */
5860 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005861 next = lysc_node_child(elem);
5862 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5863 next = next->next;
5864 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5865 next = next->next;
5866 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005867 if (!next) {
5868skip_children:
5869 /* no children, so try siblings, but only if it's not the start,
5870 * that is considered to be the root and it's siblings are not traversed */
5871 if (elem != start) {
5872 next = elem->next;
5873 } else {
5874 break;
5875 }
5876 }
5877 while (!next) {
5878 /* no siblings, go back through the parents */
5879 if (elem->parent == start) {
5880 /* we are done, no next element to process */
5881 break;
5882 }
5883 /* parent is already processed, go to its sibling */
5884 elem = elem->parent;
5885 next = elem->next;
5886 }
5887 }
5888 }
5889
5890 return LY_SUCCESS;
5891}
5892
5893/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005894 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005895 * Indirectly context position aware.
5896 *
5897 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005898 * @param[in] mod Matching metadata module, NULL for any.
5899 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005900 * @return LY_ERR
5901 */
5902static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005903moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005904{
Michal Vasko9f96a052020-03-10 09:41:45 +01005905 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005906
Michal Vaskod3678892020-05-21 10:06:58 +02005907 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005908 return LY_SUCCESS;
5909 }
5910
5911 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005912 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005913 return LY_EVALID;
5914 }
5915
Radek Krejci1deb5be2020-08-26 16:43:36 +02005916 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005917 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005918
5919 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5920 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005921 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005922 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005923
5924 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005925 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005926 continue;
5927 }
5928
Michal Vaskod3678892020-05-21 10:06:58 +02005929 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005930 /* match */
5931 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005932 set->val.meta[i].meta = sub;
5933 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005934 /* pos does not change */
5935 replaced = 1;
5936 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005937 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 +02005938 }
5939 ++i;
5940 }
5941 }
5942 }
5943
5944 if (!replaced) {
5945 /* no match */
5946 set_remove_node(set, i);
5947 }
5948 }
5949
5950 return LY_SUCCESS;
5951}
5952
5953/**
5954 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005955 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005956 *
5957 * @param[in,out] set1 Set to use for the result.
5958 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005959 * @return LY_ERR
5960 */
5961static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005962moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005963{
5964 LY_ERR rc;
5965
Michal Vaskod3678892020-05-21 10:06:58 +02005966 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005967 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005968 return LY_EVALID;
5969 }
5970
5971 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005972 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005973 return LY_SUCCESS;
5974 }
5975
Michal Vaskod3678892020-05-21 10:06:58 +02005976 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005977 memcpy(set1, set2, sizeof *set1);
5978 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005979 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005980 return LY_SUCCESS;
5981 }
5982
5983 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005984 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005985
5986 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005987 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005988 LY_CHECK_RET(rc);
5989
5990 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005991 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005992
5993 return LY_SUCCESS;
5994}
5995
5996/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005997 * @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 +02005998 * Context position aware.
5999 *
6000 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006001 * @param[in] mod Matching metadata module, NULL for any.
6002 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006003 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006004 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6005 */
6006static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006007moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006008{
Michal Vasko9f96a052020-03-10 09:41:45 +01006009 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006010 struct lyxp_set *set_all_desc = NULL;
6011 LY_ERR rc;
6012
Michal Vaskod3678892020-05-21 10:06:58 +02006013 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006014 return LY_SUCCESS;
6015 }
6016
6017 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006018 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006019 return LY_EVALID;
6020 }
6021
Michal Vasko03ff5a72019-09-11 13:49:33 +02006022 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6023 * but it likely won't be used much, so it's a waste of time */
6024 /* copy the context */
6025 set_all_desc = set_copy(set);
6026 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006027 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006028 if (rc != LY_SUCCESS) {
6029 lyxp_set_free(set_all_desc);
6030 return rc;
6031 }
6032 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006033 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006034 if (rc != LY_SUCCESS) {
6035 lyxp_set_free(set_all_desc);
6036 return rc;
6037 }
6038 lyxp_set_free(set_all_desc);
6039
Radek Krejci1deb5be2020-08-26 16:43:36 +02006040 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006041 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006042
6043 /* only attributes of an elem can be in the result, skip all the rest,
6044 * we have all attributes qualified in lyd tree */
6045 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006046 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006047 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006048 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006049 continue;
6050 }
6051
Michal Vaskod3678892020-05-21 10:06:58 +02006052 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006053 /* match */
6054 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006055 set->val.meta[i].meta = sub;
6056 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006057 /* pos does not change */
6058 replaced = 1;
6059 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006060 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 +02006061 }
6062 ++i;
6063 }
6064 }
6065 }
6066
6067 if (!replaced) {
6068 /* no match */
6069 set_remove_node(set, i);
6070 }
6071 }
6072
6073 return LY_SUCCESS;
6074}
6075
6076/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006077 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6078 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006079 *
6080 * @param[in] parent Current parent.
6081 * @param[in] parent_pos Position of @p parent.
6082 * @param[in] parent_type Node type of @p parent.
6083 * @param[in,out] to_set Set to use.
6084 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006085 * @param[in] options XPath options.
6086 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6087 */
6088static LY_ERR
6089moveto_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 +02006090 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006091{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006092 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006093 LY_ERR rc;
6094
6095 switch (parent_type) {
6096 case LYXP_NODE_ROOT:
6097 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006098 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006099
Michal Vasko61ac2f62020-05-25 12:39:51 +02006100 /* add all top-level nodes as elements */
6101 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006102 break;
6103 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006104 /* add just the text node of this term element node */
6105 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006106 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6107 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6108 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006109 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006110 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006111
6112 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006113 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006114 break;
6115 default:
6116 LOGINT_RET(parent->schema->module->ctx);
6117 }
6118
Michal Vasko61ac2f62020-05-25 12:39:51 +02006119 /* add all top-level nodes as elements */
6120 LY_LIST_FOR(first, iter) {
6121 /* context check */
6122 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6123 continue;
6124 }
6125
6126 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006127 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006128 return LY_EINCOMPLETE;
6129 }
6130
6131 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6132 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6133
6134 /* also add all the children of this node, recursively */
6135 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6136 LY_CHECK_RET(rc);
6137 }
6138 }
6139
Michal Vasko03ff5a72019-09-11 13:49:33 +02006140 return LY_SUCCESS;
6141}
6142
6143/**
6144 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6145 * (or LYXP_SET_EMPTY). Context position aware.
6146 *
6147 * @param[in,out] set Set to use.
6148 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6149 * @param[in] options XPath options.
6150 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6151 */
6152static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006153moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006154{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006155 struct lyxp_set ret_set;
6156 LY_ERR rc;
6157
Michal Vaskod3678892020-05-21 10:06:58 +02006158 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006159 return LY_SUCCESS;
6160 }
6161
6162 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006163 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006164 return LY_EVALID;
6165 }
6166
6167 /* nothing to do */
6168 if (!all_desc) {
6169 return LY_SUCCESS;
6170 }
6171
Michal Vasko03ff5a72019-09-11 13:49:33 +02006172 /* add all the children, they get added recursively */
6173 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006174 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006175 /* copy the current node to tmp */
6176 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6177
6178 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006179 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006180 continue;
6181 }
6182
Michal Vasko03ff5a72019-09-11 13:49:33 +02006183 /* add all the children */
6184 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 +02006185 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006186 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006187 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006188 return rc;
6189 }
6190 }
6191
6192 /* use the temporary set as the current one */
6193 ret_set.ctx_pos = set->ctx_pos;
6194 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006195 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006196 memcpy(set, &ret_set, sizeof *set);
6197
6198 return LY_SUCCESS;
6199}
6200
6201/**
6202 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6203 * (or LYXP_SET_EMPTY).
6204 *
6205 * @param[in,out] set Set to use.
6206 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6207 * @param[in] options XPath options.
6208 * @return LY_ERR
6209 */
6210static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006211moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006212{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006213 uint32_t getnext_opts;
6214 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006215 const struct lysc_node *iter, *start_parent;
6216 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006217
Michal Vaskod3678892020-05-21 10:06:58 +02006218 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006219 return LY_SUCCESS;
6220 }
6221
6222 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006223 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006224 return LY_EVALID;
6225 }
6226
Michal Vasko519fd602020-05-26 12:17:39 +02006227 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006228 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006229 if (options & LYXP_SCNODE_OUTPUT) {
6230 getnext_opts |= LYS_GETNEXT_OUTPUT;
6231 }
6232
6233 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006234 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006235 if (!all_desc) {
6236 /* traverse the start node */
6237 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6238 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6239 }
6240 continue;
6241 }
6242
Radek Krejcif13b87b2020-12-01 22:02:17 +01006243 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6244 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006245 continue;
6246 }
6247
Michal Vasko519fd602020-05-26 12:17:39 +02006248 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006249 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006250 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006251 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006252 }
6253
Michal Vasko519fd602020-05-26 12:17:39 +02006254 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006255
Michal Vasko519fd602020-05-26 12:17:39 +02006256 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6257 /* it can actually be in any module, it's all <running> */
6258 mod_idx = 0;
6259 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6260 iter = NULL;
6261 /* module may not be implemented */
6262 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6263 /* context check */
6264 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6265 continue;
6266 }
6267
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006268 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006269 /* throw away the insert index, we want to consider that node again, recursively */
6270 }
6271 }
6272
6273 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6274 iter = NULL;
6275 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006276 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006277 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006278 continue;
6279 }
6280
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006281 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006282 }
6283 }
6284 }
6285
6286 return LY_SUCCESS;
6287}
6288
6289/**
6290 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6291 * (or LYXP_SET_EMPTY). Context position aware.
6292 *
6293 * @param[in] set Set to use.
6294 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6295 * @param[in] options XPath options.
6296 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6297 */
6298static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006299moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006300{
6301 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006302 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006303 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006304
Michal Vaskod3678892020-05-21 10:06:58 +02006305 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006306 return LY_SUCCESS;
6307 }
6308
6309 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006310 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006311 return LY_EVALID;
6312 }
6313
6314 if (all_desc) {
6315 /* <path>//.. == <path>//./.. */
6316 rc = moveto_self(set, 1, options);
6317 LY_CHECK_RET(rc);
6318 }
6319
Radek Krejci1deb5be2020-08-26 16:43:36 +02006320 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006321 node = set->val.nodes[i].node;
6322
6323 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6324 new_node = (struct lyd_node *)node->parent;
6325 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6326 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006327 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6328 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006329 if (!new_node) {
6330 LOGINT_RET(set->ctx);
6331 }
6332 } else {
6333 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006334 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006335 continue;
6336 }
6337
Michal Vaskoa1424542019-11-14 16:08:52 +01006338 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006339 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 +02006340 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006341 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006342
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006343 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006344 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006345 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006346
Michal Vasko03ff5a72019-09-11 13:49:33 +02006347 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006348 /* 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 +02006349 new_type = LYXP_NODE_ELEM;
6350 }
6351
Michal Vasko03ff5a72019-09-11 13:49:33 +02006352 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006353 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006354 } else {
6355 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006356 }
6357 }
6358
Michal Vasko2caefc12019-11-14 16:07:56 +01006359 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006360 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006361
6362 return LY_SUCCESS;
6363}
6364
6365/**
6366 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6367 * (or LYXP_SET_EMPTY).
6368 *
6369 * @param[in] set Set to use.
6370 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6371 * @param[in] options XPath options.
6372 * @return LY_ERR
6373 */
6374static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006375moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006376{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006377 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006378 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006379 const struct lysc_node *node, *new_node;
6380 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006381
Michal Vaskod3678892020-05-21 10:06:58 +02006382 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006383 return LY_SUCCESS;
6384 }
6385
6386 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006387 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006388 return LY_EVALID;
6389 }
6390
6391 if (all_desc) {
6392 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006393 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006394 }
6395
Michal Vasko03ff5a72019-09-11 13:49:33 +02006396 orig_used = set->used;
6397 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006398 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6399 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006400 continue;
6401 }
6402
6403 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006404 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006405 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006406 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006407 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006408
6409 node = set->val.scnodes[i].scnode;
6410
6411 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006412 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006413 } else {
6414 /* root does not have a parent */
6415 continue;
6416 }
6417
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006418 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006419 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006420 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006421
Michal Vasko03ff5a72019-09-11 13:49:33 +02006422 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006423 /* 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 +02006424 new_type = LYXP_NODE_ELEM;
6425 }
6426
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006427 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6428 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006429 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006430 temp_ctx = 1;
6431 }
6432 }
6433
6434 if (temp_ctx) {
6435 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006436 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6437 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006438 }
6439 }
6440 }
6441
6442 return LY_SUCCESS;
6443}
6444
6445/**
6446 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6447 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6448 *
6449 * @param[in,out] set1 Set to use for the result.
6450 * @param[in] set2 Set acting as the second operand for @p op.
6451 * @param[in] op Comparison operator to process.
6452 * @param[in] options XPath options.
6453 * @return LY_ERR
6454 */
6455static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006456moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006457{
6458 /*
6459 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6460 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6461 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6462 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6463 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6464 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6465 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6466 *
6467 * '=' or '!='
6468 * BOOLEAN + BOOLEAN
6469 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6470 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6471 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6472 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6473 * NUMBER + NUMBER
6474 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6475 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6476 * STRING + STRING
6477 *
6478 * '<=', '<', '>=', '>'
6479 * NUMBER + NUMBER
6480 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6481 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6482 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6483 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6484 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6485 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6486 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6487 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6488 */
6489 struct lyxp_set iter1, iter2;
6490 int result;
6491 int64_t i;
6492 LY_ERR rc;
6493
Michal Vasko004d3152020-06-11 19:59:22 +02006494 memset(&iter1, 0, sizeof iter1);
6495 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006496
6497 /* iterative evaluation with node-sets */
6498 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6499 if (set1->type == LYXP_SET_NODE_SET) {
6500 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006501 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006502 switch (set2->type) {
6503 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006504 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006505 break;
6506 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006507 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006508 break;
6509 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006510 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006511 break;
6512 }
6513 LY_CHECK_RET(rc);
6514
Michal Vasko4c7763f2020-07-27 17:40:37 +02006515 /* canonize set2 */
6516 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6517
6518 /* compare recursively */
6519 rc = moveto_op_comp(&iter1, &iter2, op, options);
6520 lyxp_set_free_content(&iter2);
6521 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006522
6523 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006524 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006525 set_fill_boolean(set1, 1);
6526 return LY_SUCCESS;
6527 }
6528 }
6529 } else {
6530 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006531 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006532 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006533 case LYXP_SET_NUMBER:
6534 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6535 break;
6536 case LYXP_SET_BOOLEAN:
6537 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6538 break;
6539 default:
6540 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6541 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006542 }
6543 LY_CHECK_RET(rc);
6544
Michal Vasko4c7763f2020-07-27 17:40:37 +02006545 /* canonize set1 */
6546 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 +02006547
Michal Vasko4c7763f2020-07-27 17:40:37 +02006548 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006549 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006550 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006551 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006552
6553 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006554 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006555 set_fill_boolean(set1, 1);
6556 return LY_SUCCESS;
6557 }
6558 }
6559 }
6560
6561 /* false for all nodes */
6562 set_fill_boolean(set1, 0);
6563 return LY_SUCCESS;
6564 }
6565
6566 /* first convert properly */
6567 if ((op[0] == '=') || (op[0] == '!')) {
6568 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006569 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6570 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006571 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006572 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006573 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006574 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006575 LY_CHECK_RET(rc);
6576 } /* else we have 2 strings */
6577 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006578 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006579 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006580 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006581 LY_CHECK_RET(rc);
6582 }
6583
6584 assert(set1->type == set2->type);
6585
6586 /* compute result */
6587 if (op[0] == '=') {
6588 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006589 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006590 } else if (set1->type == LYXP_SET_NUMBER) {
6591 result = (set1->val.num == set2->val.num);
6592 } else {
6593 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006594 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006595 }
6596 } else if (op[0] == '!') {
6597 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006598 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006599 } else if (set1->type == LYXP_SET_NUMBER) {
6600 result = (set1->val.num != set2->val.num);
6601 } else {
6602 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006603 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006604 }
6605 } else {
6606 assert(set1->type == LYXP_SET_NUMBER);
6607 if (op[0] == '<') {
6608 if (op[1] == '=') {
6609 result = (set1->val.num <= set2->val.num);
6610 } else {
6611 result = (set1->val.num < set2->val.num);
6612 }
6613 } else {
6614 if (op[1] == '=') {
6615 result = (set1->val.num >= set2->val.num);
6616 } else {
6617 result = (set1->val.num > set2->val.num);
6618 }
6619 }
6620 }
6621
6622 /* assign result */
6623 if (result) {
6624 set_fill_boolean(set1, 1);
6625 } else {
6626 set_fill_boolean(set1, 0);
6627 }
6628
6629 return LY_SUCCESS;
6630}
6631
6632/**
6633 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6634 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6635 *
6636 * @param[in,out] set1 Set to use for the result.
6637 * @param[in] set2 Set acting as the second operand for @p op.
6638 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006639 * @return LY_ERR
6640 */
6641static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006642moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006643{
6644 LY_ERR rc;
6645
6646 /* unary '-' */
6647 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006648 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006649 LY_CHECK_RET(rc);
6650 set1->val.num *= -1;
6651 lyxp_set_free(set2);
6652 return LY_SUCCESS;
6653 }
6654
6655 assert(set1 && set2);
6656
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006657 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006658 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006659 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006660 LY_CHECK_RET(rc);
6661
6662 switch (op[0]) {
6663 /* '+' */
6664 case '+':
6665 set1->val.num += set2->val.num;
6666 break;
6667
6668 /* '-' */
6669 case '-':
6670 set1->val.num -= set2->val.num;
6671 break;
6672
6673 /* '*' */
6674 case '*':
6675 set1->val.num *= set2->val.num;
6676 break;
6677
6678 /* 'div' */
6679 case 'd':
6680 set1->val.num /= set2->val.num;
6681 break;
6682
6683 /* 'mod' */
6684 case 'm':
6685 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6686 break;
6687
6688 default:
6689 LOGINT_RET(set1->ctx);
6690 }
6691
6692 return LY_SUCCESS;
6693}
6694
6695/*
6696 * eval functions
6697 *
6698 * They execute a parsed XPath expression on some data subtree.
6699 */
6700
6701/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006702 * @brief Evaluate Predicate. Logs directly on error.
6703 *
Michal Vaskod3678892020-05-21 10:06:58 +02006704 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006705 *
6706 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006707 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006708 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6709 * @param[in] options XPath options.
6710 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6711 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6712 */
6713static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006714eval_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 +02006715{
6716 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006717 uint16_t orig_exp;
6718 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006719 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006720 struct lyxp_set set2;
6721 struct lyd_node *orig_parent;
6722
6723 /* '[' */
6724 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006725 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006726 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006727
6728 if (!set) {
6729only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006730 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006731 LY_CHECK_RET(rc);
6732 } else if (set->type == LYXP_SET_NODE_SET) {
6733 /* 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 +01006734 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006735
6736 /* empty set, nothing to evaluate */
6737 if (!set->used) {
6738 goto only_parse;
6739 }
6740
Michal Vasko004d3152020-06-11 19:59:22 +02006741 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006742 orig_pos = 0;
6743 orig_size = set->used;
6744 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006745 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006746 set_init(&set2, set);
6747 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6748 /* remember the node context position for position() and context size for last(),
6749 * predicates should always be evaluated with respect to the child axis (since we do
6750 * not support explicit axes) so we assign positions based on their parents */
6751 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6752 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6753 orig_pos = 1;
6754 } else {
6755 ++orig_pos;
6756 }
6757
6758 set2.ctx_pos = orig_pos;
6759 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006760 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006761
Michal Vasko004d3152020-06-11 19:59:22 +02006762 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006763 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006764 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006765 return rc;
6766 }
6767
6768 /* number is a position */
6769 if (set2.type == LYXP_SET_NUMBER) {
6770 if ((long long)set2.val.num == orig_pos) {
6771 set2.val.num = 1;
6772 } else {
6773 set2.val.num = 0;
6774 }
6775 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006776 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006777
6778 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006779 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006780 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006781 }
6782 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006783 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006784
6785 } else if (set->type == LYXP_SET_SCNODE_SET) {
6786 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006787 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006788 /* there is a currently-valid node */
6789 break;
6790 }
6791 }
6792 /* empty set, nothing to evaluate */
6793 if (i == set->used) {
6794 goto only_parse;
6795 }
6796
Michal Vasko004d3152020-06-11 19:59:22 +02006797 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006798
Michal Vasko03ff5a72019-09-11 13:49:33 +02006799 /* set special in_ctx to all the valid snodes */
6800 pred_in_ctx = set_scnode_new_in_ctx(set);
6801
6802 /* use the valid snodes one-by-one */
6803 for (i = 0; i < set->used; ++i) {
6804 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6805 continue;
6806 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006807 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006808
Michal Vasko004d3152020-06-11 19:59:22 +02006809 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006810
Michal Vasko004d3152020-06-11 19:59:22 +02006811 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006812 LY_CHECK_RET(rc);
6813
6814 set->val.scnodes[i].in_ctx = pred_in_ctx;
6815 }
6816
6817 /* restore the state as it was before the predicate */
6818 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006819 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
6820 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006821 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006822 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006823 }
6824 }
6825
6826 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006827 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006828 set_fill_set(&set2, set);
6829
Michal Vasko004d3152020-06-11 19:59:22 +02006830 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006831 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006832 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006833 return rc;
6834 }
6835
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006836 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006837 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006838 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006839 }
Michal Vaskod3678892020-05-21 10:06:58 +02006840 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006841 }
6842
6843 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006844 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006845 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006846 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006847 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006848
6849 return LY_SUCCESS;
6850}
6851
6852/**
Michal Vaskod3678892020-05-21 10:06:58 +02006853 * @brief Evaluate Literal. Logs directly on error.
6854 *
6855 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006856 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006857 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6858 */
6859static void
Michal Vasko40308e72020-10-20 16:38:40 +02006860eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006861{
6862 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006863 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006864 set_fill_string(set, "", 0);
6865 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006866 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 +02006867 }
6868 }
6869 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006870 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006871 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006872}
6873
6874/**
Michal Vasko004d3152020-06-11 19:59:22 +02006875 * @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 +02006876 *
Michal Vasko004d3152020-06-11 19:59:22 +02006877 * @param[in] exp Full parsed XPath expression.
6878 * @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 +02006879 * @param[in] ctx_node Found schema node as the context for the predicate.
6880 * @param[in] cur_mod Current module for the expression.
6881 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006882 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006883 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006884 * @param[out] predicates Parsed predicates.
6885 * @param[out] pred_type Type of @p predicates.
6886 * @return LY_SUCCESS on success,
6887 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006888 */
6889static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006890eval_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 +02006891 const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_PREFIX_FORMAT format, void *prefix_data,
6892 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006893{
Michal Vasko004d3152020-06-11 19:59:22 +02006894 LY_ERR ret = LY_SUCCESS;
6895 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006896 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006897 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006898 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006899 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006900
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006901 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006902
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006903 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006904 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006905 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006906 return LY_EINVAL;
6907 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006908 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 +02006909 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006910
Michal Vasko004d3152020-06-11 19:59:22 +02006911 /* learn where the predicates end */
6912 e_idx = *tok_idx;
6913 while (key_count) {
6914 /* '[' */
6915 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6916 return LY_EINVAL;
6917 }
6918 ++e_idx;
6919
6920 /* ']' */
6921 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6922 ++e_idx;
6923 }
6924 ++e_idx;
6925
6926 /* another presumably key predicate parsed */
6927 --key_count;
6928 }
Michal Vasko004d3152020-06-11 19:59:22 +02006929 } else {
6930 /* learn just where this single predicate ends */
6931 e_idx = *tok_idx;
6932
Michal Vaskod3678892020-05-21 10:06:58 +02006933 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006934 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6935 return LY_EINVAL;
6936 }
6937 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006938
6939 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006940 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6941 ++e_idx;
6942 }
6943 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006944 }
6945
Michal Vasko004d3152020-06-11 19:59:22 +02006946 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6947 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6948
6949 /* turn logging off */
6950 prev_lo = ly_log_options(0);
6951
6952 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006953 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
6954 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02006955
6956 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006957 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
6958 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006959 LY_CHECK_GOTO(ret, cleanup);
6960
6961 /* success, the predicate must include all the needed information for hash-based search */
6962 *tok_idx = e_idx;
6963
6964cleanup:
6965 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006966 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02006967 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006968}
6969
6970/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006971 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
6972 * data node(s) could be found using a single hash-based search.
6973 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01006974 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006975 * @param[in] node Next context node to check.
6976 * @param[in] name Expected node name.
6977 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01006978 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006979 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006980 * @param[in] format Prefix format.
6981 * @param[in,out] found Previously found node, is updated.
6982 * @return LY_SUCCESS on success,
6983 * @return LY_ENOT if the whole check failed and hashes cannot be used.
6984 */
6985static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01006986eval_name_test_with_predicate_get_scnode(const struct ly_ctx *ctx, const struct lyd_node *node, const char *name,
6987 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 +02006988 const struct lysc_node **found)
6989{
6990 const struct lysc_node *scnode;
6991 const struct lys_module *mod;
6992 uint32_t idx = 0;
6993
Michal Vaskoc71c37b2021-01-11 13:40:02 +01006994 assert((format == LY_PREF_JSON) || moveto_mod);
6995
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006996continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02006997 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006998 if (!node) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01006999 if ((format == LY_PREF_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007000 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007001 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007002 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7003 if (scnode) {
7004 /* we have found a match */
7005 break;
7006 }
7007 }
7008
Michal Vasko7d1d0912020-10-16 08:38:30 +02007009 if (!scnode) {
7010 /* all modules searched */
7011 idx = 0;
7012 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007013 } else {
7014 /* search in top-level */
7015 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7016 }
7017 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007018 if ((format == LY_PREF_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007019 /* we must adjust the module to inherit the one from the context node */
7020 moveto_mod = node->schema->module;
7021 }
7022
7023 /* search in children, do not repeat the same search */
7024 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007025 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007026
7027 /* additional context check */
7028 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7029 scnode = NULL;
7030 }
7031
7032 if (scnode) {
7033 if (*found) {
7034 /* we found a schema node with the same name but at different level, give up, too complicated
7035 * (more hash-based searches would be required, not supported) */
7036 return LY_ENOT;
7037 } else {
7038 /* remember the found schema node and continue to make sure it can be used */
7039 *found = scnode;
7040 }
7041 }
7042
7043 if (idx) {
7044 /* continue searching all the following models */
7045 goto continue_search;
7046 }
7047
7048 return LY_SUCCESS;
7049}
7050
7051/**
Michal Vaskod3678892020-05-21 10:06:58 +02007052 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7053 *
7054 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7055 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7056 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7057 *
7058 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007059 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007060 * @param[in] attr_axis Whether to search attributes or standard nodes.
7061 * @param[in] all_desc Whether to search all the descendants or children only.
7062 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7063 * @param[in] options XPath options.
7064 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7065 */
7066static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007067eval_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 +02007068 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007069{
Michal Vaskod3678892020-05-21 10:06:58 +02007070 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007071 const char *ncname, *ncname_dict = NULL;
7072 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007073 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007074 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007075 struct ly_path_predicate *predicates = NULL;
7076 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007077 LY_ERR rc = LY_SUCCESS;
7078
7079 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007080 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007081 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007082
7083 if (!set) {
7084 goto moveto;
7085 }
7086
Michal Vasko004d3152020-06-11 19:59:22 +02007087 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7088 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007089
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007090 if ((ncname[0] == '*') && (ncname_len == 1)) {
7091 /* all nodes will match */
7092 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007093 }
7094
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007095 /* parse (and skip) module name */
7096 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007097 LY_CHECK_GOTO(rc, cleanup);
7098
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007099 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 +02007100 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007101 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007102 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7103 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007104 /* check failed */
7105 scnode = NULL;
7106 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007107 }
7108 }
7109
Michal Vasko004d3152020-06-11 19:59:22 +02007110 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7111 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007112 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7113 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007114 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007115 scnode = NULL;
7116 }
7117 }
7118 }
7119
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007120 if (!scnode) {
7121 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007122 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007123 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007124 }
7125
7126moveto:
7127 /* move to the attribute(s), data node(s), or schema node(s) */
7128 if (attr_axis) {
7129 if (set && (options & LYXP_SCNODE_ALL)) {
7130 set_scnode_clear_ctx(set);
7131 } else {
7132 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007133 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007134 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007135 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007136 }
7137 LY_CHECK_GOTO(rc, cleanup);
7138 }
7139 } else {
7140 if (set && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007141 int64_t i;
7142
Michal Vaskod3678892020-05-21 10:06:58 +02007143 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007144 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007145 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007146 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007147 }
7148 LY_CHECK_GOTO(rc, cleanup);
7149
7150 for (i = set->used - 1; i > -1; --i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007151 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM) {
Michal Vaskod3678892020-05-21 10:06:58 +02007152 break;
7153 }
7154 }
7155 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007156 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vasko90f12dc2020-12-03 14:20:42 +01007157 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".",
7158 ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007159 free(path);
7160 }
7161 } else {
7162 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007163 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007164 } else {
7165 if (scnode) {
7166 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007167 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007168 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007169 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007170 }
7171 }
7172 LY_CHECK_GOTO(rc, cleanup);
7173 }
7174 }
7175
7176 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007177 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7178 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007179 LY_CHECK_RET(rc);
7180 }
7181
7182cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007183 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007184 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007185 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007186 }
Michal Vaskod3678892020-05-21 10:06:58 +02007187 return rc;
7188}
7189
7190/**
7191 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7192 *
7193 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7194 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7195 * [8] NodeType ::= 'text' | 'node'
7196 *
7197 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007198 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007199 * @param[in] attr_axis Whether to search attributes or standard nodes.
7200 * @param[in] all_desc Whether to search all the descendants or children only.
7201 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7202 * @param[in] options XPath options.
7203 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7204 */
7205static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007206eval_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 +02007207 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007208{
7209 LY_ERR rc;
7210
7211 /* TODO */
7212 (void)attr_axis;
7213 (void)all_desc;
7214
7215 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007216 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007217 if (set->type == LYXP_SET_SCNODE_SET) {
7218 set_scnode_clear_ctx(set);
7219 /* just for the debug message below */
7220 set = NULL;
7221 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007222 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007223 rc = xpath_node(NULL, 0, set, options);
7224 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007225 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007226 rc = xpath_text(NULL, 0, set, options);
7227 }
7228 LY_CHECK_RET(rc);
7229 }
7230 }
7231 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007232 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007233 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007234
7235 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007236 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007237 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007238 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007239 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007240
7241 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007242 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007243 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007244 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007245 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007246
7247 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007248 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7249 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007250 LY_CHECK_RET(rc);
7251 }
7252
7253 return LY_SUCCESS;
7254}
7255
7256/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007257 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7258 *
7259 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7260 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007261 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007262 *
7263 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007264 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007265 * @param[in] all_desc Whether to search all the descendants or children only.
7266 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7267 * @param[in] options XPath options.
7268 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7269 */
7270static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007271eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7272 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007273{
Radek Krejci857189e2020-09-01 13:26:36 +02007274 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007275 LY_ERR rc;
7276
7277 goto step;
7278 do {
7279 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007280 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007281 all_desc = 0;
7282 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007283 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007284 all_desc = 1;
7285 }
7286 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007287 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007288 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007289
7290step:
Michal Vaskod3678892020-05-21 10:06:58 +02007291 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007292 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007293 attr_axis = 1;
7294
7295 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007296 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007297 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007298 } else {
7299 attr_axis = 0;
7300 }
7301
Michal Vasko03ff5a72019-09-11 13:49:33 +02007302 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007303 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007304 case LYXP_TOKEN_DOT:
7305 /* evaluate '.' */
7306 if (set && (options & LYXP_SCNODE_ALL)) {
7307 rc = moveto_scnode_self(set, all_desc, options);
7308 } else {
7309 rc = moveto_self(set, all_desc, options);
7310 }
7311 LY_CHECK_RET(rc);
7312 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007313 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007314 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007315 break;
7316
7317 case LYXP_TOKEN_DDOT:
7318 /* evaluate '..' */
7319 if (set && (options & LYXP_SCNODE_ALL)) {
7320 rc = moveto_scnode_parent(set, all_desc, options);
7321 } else {
7322 rc = moveto_parent(set, all_desc, options);
7323 }
7324 LY_CHECK_RET(rc);
7325 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007326 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007327 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007328 break;
7329
Michal Vasko03ff5a72019-09-11 13:49:33 +02007330 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007331 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007332 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007333 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007334 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007335
Michal Vaskod3678892020-05-21 10:06:58 +02007336 case LYXP_TOKEN_NODETYPE:
7337 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007338 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007339 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007340 break;
7341
7342 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007343 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007344 }
Michal Vasko004d3152020-06-11 19:59:22 +02007345 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007346
7347 return LY_SUCCESS;
7348}
7349
7350/**
7351 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7352 *
7353 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7354 *
7355 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007356 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007357 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7358 * @param[in] options XPath options.
7359 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7360 */
7361static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007362eval_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 +02007363{
Radek Krejci857189e2020-09-01 13:26:36 +02007364 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007365
7366 if (set) {
7367 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007368 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007369 }
7370
7371 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007372 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007373 /* evaluate '/' - deferred */
7374 all_desc = 0;
7375 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007376 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007377 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007378
Michal Vasko004d3152020-06-11 19:59:22 +02007379 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007380 return LY_SUCCESS;
7381 }
Michal Vasko004d3152020-06-11 19:59:22 +02007382 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007383 case LYXP_TOKEN_DOT:
7384 case LYXP_TOKEN_DDOT:
7385 case LYXP_TOKEN_AT:
7386 case LYXP_TOKEN_NAMETEST:
7387 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007388 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007389 break;
7390 default:
7391 break;
7392 }
7393
Michal Vasko03ff5a72019-09-11 13:49:33 +02007394 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007395 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007396 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7397 all_desc = 1;
7398 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007399 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007400 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007401
Michal Vaskob0099a92020-08-31 14:55:23 +02007402 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007403 }
7404
7405 return LY_SUCCESS;
7406}
7407
7408/**
7409 * @brief Evaluate FunctionCall. Logs directly on error.
7410 *
Michal Vaskod3678892020-05-21 10:06:58 +02007411 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007412 *
7413 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007414 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007415 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7416 * @param[in] options XPath options.
7417 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7418 */
7419static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007420eval_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 +02007421{
7422 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007423
Radek Krejci1deb5be2020-08-26 16:43:36 +02007424 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007425 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007426 struct lyxp_set **args = NULL, **args_aux;
7427
7428 if (set) {
7429 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007430 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007431 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007432 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007433 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007434 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007435 xpath_func = &xpath_sum;
7436 }
7437 break;
7438 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007439 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007440 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007441 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007442 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007443 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007444 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007445 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007446 xpath_func = &xpath_true;
7447 }
7448 break;
7449 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007450 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007451 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007452 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007453 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007454 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007455 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007456 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007457 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007458 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007459 xpath_func = &xpath_deref;
7460 }
7461 break;
7462 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007463 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007464 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007465 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007466 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007467 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007468 xpath_func = &xpath_string;
7469 }
7470 break;
7471 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007472 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007473 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007474 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007475 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007476 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007477 xpath_func = &xpath_current;
7478 }
7479 break;
7480 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007481 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007482 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007483 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007484 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007485 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007486 xpath_func = &xpath_re_match;
7487 }
7488 break;
7489 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007490 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007491 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007492 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007493 xpath_func = &xpath_translate;
7494 }
7495 break;
7496 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007497 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007498 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007499 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007500 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007501 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007502 xpath_func = &xpath_bit_is_set;
7503 }
7504 break;
7505 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007506 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007507 xpath_func = &xpath_starts_with;
7508 }
7509 break;
7510 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007511 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007512 xpath_func = &xpath_derived_from;
7513 }
7514 break;
7515 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007516 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007517 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007518 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007519 xpath_func = &xpath_string_length;
7520 }
7521 break;
7522 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007523 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007524 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007525 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007526 xpath_func = &xpath_substring_after;
7527 }
7528 break;
7529 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007530 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007531 xpath_func = &xpath_substring_before;
7532 }
7533 break;
7534 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007535 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007536 xpath_func = &xpath_derived_from_or_self;
7537 }
7538 break;
7539 }
7540
7541 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007542 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 +02007543 return LY_EVALID;
7544 }
7545 }
7546
7547 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007548 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007549 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007550
7551 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007552 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007553 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007554 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007555 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007556
7557 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007558 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007559 if (set) {
7560 args = malloc(sizeof *args);
7561 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7562 arg_count = 1;
7563 args[0] = set_copy(set);
7564 if (!args[0]) {
7565 rc = LY_EMEM;
7566 goto cleanup;
7567 }
7568
Michal Vasko004d3152020-06-11 19:59:22 +02007569 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570 LY_CHECK_GOTO(rc, cleanup);
7571 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007572 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007573 LY_CHECK_GOTO(rc, cleanup);
7574 }
7575 }
Michal Vasko004d3152020-06-11 19:59:22 +02007576 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007577 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007578 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007579 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007580
7581 if (set) {
7582 ++arg_count;
7583 args_aux = realloc(args, arg_count * sizeof *args);
7584 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7585 args = args_aux;
7586 args[arg_count - 1] = set_copy(set);
7587 if (!args[arg_count - 1]) {
7588 rc = LY_EMEM;
7589 goto cleanup;
7590 }
7591
Michal Vasko004d3152020-06-11 19:59:22 +02007592 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007593 LY_CHECK_GOTO(rc, cleanup);
7594 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007595 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007596 LY_CHECK_GOTO(rc, cleanup);
7597 }
7598 }
7599
7600 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007601 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007602 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007603 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007604 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007605
7606 if (set) {
7607 /* evaluate function */
7608 rc = xpath_func(args, arg_count, set, options);
7609
7610 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007611 /* merge all nodes from arg evaluations */
7612 for (i = 0; i < arg_count; ++i) {
7613 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007614 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007615 }
7616 }
7617 } else {
7618 rc = LY_SUCCESS;
7619 }
7620
7621cleanup:
7622 for (i = 0; i < arg_count; ++i) {
7623 lyxp_set_free(args[i]);
7624 }
7625 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007626 return rc;
7627}
7628
7629/**
7630 * @brief Evaluate Number. Logs directly on error.
7631 *
7632 * @param[in] ctx Context for errors.
7633 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007634 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007635 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7636 * @return LY_ERR
7637 */
7638static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007639eval_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 +02007640{
7641 long double num;
7642 char *endptr;
7643
7644 if (set) {
7645 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007646 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007647 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007648 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7649 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007650 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007651 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007652 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007653 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7654 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007655 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007656 return LY_EVALID;
7657 }
7658
7659 set_fill_number(set, num);
7660 }
7661
7662 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007663 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007664 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007665 return LY_SUCCESS;
7666}
7667
7668/**
7669 * @brief Evaluate PathExpr. Logs directly on error.
7670 *
Michal Vaskod3678892020-05-21 10:06:58 +02007671 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007672 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7673 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7674 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007675 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007676 *
7677 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007678 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007679 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7680 * @param[in] options XPath options.
7681 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7682 */
7683static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007684eval_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 +02007685{
Radek Krejci857189e2020-09-01 13:26:36 +02007686 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007687 LY_ERR rc;
7688
Michal Vasko004d3152020-06-11 19:59:22 +02007689 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007690 case LYXP_TOKEN_PAR1:
7691 /* '(' Expr ')' */
7692
7693 /* '(' */
7694 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007695 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007696 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007697
7698 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007699 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007700 LY_CHECK_RET(rc);
7701
7702 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007703 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007704 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007705 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007706 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007707
7708 parent_pos_pred = 0;
7709 goto predicate;
7710
7711 case LYXP_TOKEN_DOT:
7712 case LYXP_TOKEN_DDOT:
7713 case LYXP_TOKEN_AT:
7714 case LYXP_TOKEN_NAMETEST:
7715 case LYXP_TOKEN_NODETYPE:
7716 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007717 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007718 LY_CHECK_RET(rc);
7719 break;
7720
7721 case LYXP_TOKEN_FUNCNAME:
7722 /* FunctionCall */
7723 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007724 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007725 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007726 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007727 }
7728 LY_CHECK_RET(rc);
7729
7730 parent_pos_pred = 1;
7731 goto predicate;
7732
Michal Vasko3e48bf32020-06-01 08:39:07 +02007733 case LYXP_TOKEN_OPER_PATH:
7734 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007735 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007736 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007737 LY_CHECK_RET(rc);
7738 break;
7739
7740 case LYXP_TOKEN_LITERAL:
7741 /* Literal */
7742 if (!set || (options & LYXP_SCNODE_ALL)) {
7743 if (set) {
7744 set_scnode_clear_ctx(set);
7745 }
Michal Vasko004d3152020-06-11 19:59:22 +02007746 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007747 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007748 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007749 }
7750
7751 parent_pos_pred = 1;
7752 goto predicate;
7753
7754 case LYXP_TOKEN_NUMBER:
7755 /* Number */
7756 if (!set || (options & LYXP_SCNODE_ALL)) {
7757 if (set) {
7758 set_scnode_clear_ctx(set);
7759 }
Michal Vasko004d3152020-06-11 19:59:22 +02007760 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007761 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007762 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007763 }
7764 LY_CHECK_RET(rc);
7765
7766 parent_pos_pred = 1;
7767 goto predicate;
7768
7769 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01007770 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 +02007771 return LY_EVALID;
7772 }
7773
7774 return LY_SUCCESS;
7775
7776predicate:
7777 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007778 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7779 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007780 LY_CHECK_RET(rc);
7781 }
7782
7783 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007784 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007785
7786 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007787 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007788 all_desc = 0;
7789 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007790 all_desc = 1;
7791 }
7792
7793 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007794 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007795 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007796
Michal Vasko004d3152020-06-11 19:59:22 +02007797 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007798 LY_CHECK_RET(rc);
7799 }
7800
7801 return LY_SUCCESS;
7802}
7803
7804/**
7805 * @brief Evaluate UnionExpr. Logs directly on error.
7806 *
Michal Vaskod3678892020-05-21 10:06:58 +02007807 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007808 *
7809 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007810 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007811 * @param[in] repeat How many times this expression is repeated.
7812 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7813 * @param[in] options XPath options.
7814 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7815 */
7816static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007817eval_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 +02007818{
7819 LY_ERR rc = LY_SUCCESS;
7820 struct lyxp_set orig_set, set2;
7821 uint16_t i;
7822
7823 assert(repeat);
7824
7825 set_init(&orig_set, set);
7826 set_init(&set2, set);
7827
7828 set_fill_set(&orig_set, set);
7829
Michal Vasko004d3152020-06-11 19:59:22 +02007830 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007831 LY_CHECK_GOTO(rc, cleanup);
7832
7833 /* ('|' PathExpr)* */
7834 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007835 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007836 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007837 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007838 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007839
7840 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007841 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007842 LY_CHECK_GOTO(rc, cleanup);
7843 continue;
7844 }
7845
7846 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007847 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007848 LY_CHECK_GOTO(rc, cleanup);
7849
7850 /* eval */
7851 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007852 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007853 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007854 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007855 LY_CHECK_GOTO(rc, cleanup);
7856 }
7857 }
7858
7859cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007860 lyxp_set_free_content(&orig_set);
7861 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007862 return rc;
7863}
7864
7865/**
7866 * @brief Evaluate UnaryExpr. Logs directly on error.
7867 *
Michal Vaskod3678892020-05-21 10:06:58 +02007868 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007869 *
7870 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007871 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007872 * @param[in] repeat How many times this expression is repeated.
7873 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7874 * @param[in] options XPath options.
7875 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7876 */
7877static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007878eval_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 +02007879{
7880 LY_ERR rc;
7881 uint16_t this_op, i;
7882
7883 assert(repeat);
7884
7885 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007886 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007887 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007888 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 +02007889
7890 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007891 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007892 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007893 }
7894
Michal Vasko004d3152020-06-11 19:59:22 +02007895 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007896 LY_CHECK_RET(rc);
7897
7898 if (set && (repeat % 2)) {
7899 if (options & LYXP_SCNODE_ALL) {
7900 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7901 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007902 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007903 LY_CHECK_RET(rc);
7904 }
7905 }
7906
7907 return LY_SUCCESS;
7908}
7909
7910/**
7911 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7912 *
Michal Vaskod3678892020-05-21 10:06:58 +02007913 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007914 * | MultiplicativeExpr '*' UnaryExpr
7915 * | MultiplicativeExpr 'div' UnaryExpr
7916 * | MultiplicativeExpr 'mod' UnaryExpr
7917 *
7918 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007919 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007920 * @param[in] repeat How many times this expression is repeated.
7921 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7922 * @param[in] options XPath options.
7923 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7924 */
7925static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007926eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
7927 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007928{
7929 LY_ERR rc;
7930 uint16_t this_op;
7931 struct lyxp_set orig_set, set2;
7932 uint16_t i;
7933
7934 assert(repeat);
7935
7936 set_init(&orig_set, set);
7937 set_init(&set2, set);
7938
7939 set_fill_set(&orig_set, set);
7940
Michal Vasko004d3152020-06-11 19:59:22 +02007941 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007942 LY_CHECK_GOTO(rc, cleanup);
7943
7944 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7945 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007946 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007947
Michal Vasko004d3152020-06-11 19:59:22 +02007948 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007949 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007950 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007951 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007952
7953 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007954 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007955 LY_CHECK_GOTO(rc, cleanup);
7956 continue;
7957 }
7958
7959 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007960 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007961 LY_CHECK_GOTO(rc, cleanup);
7962
7963 /* eval */
7964 if (options & LYXP_SCNODE_ALL) {
7965 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007966 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007967 set_scnode_clear_ctx(set);
7968 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007969 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007970 LY_CHECK_GOTO(rc, cleanup);
7971 }
7972 }
7973
7974cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007975 lyxp_set_free_content(&orig_set);
7976 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007977 return rc;
7978}
7979
7980/**
7981 * @brief Evaluate AdditiveExpr. Logs directly on error.
7982 *
Michal Vaskod3678892020-05-21 10:06:58 +02007983 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007984 * | AdditiveExpr '+' MultiplicativeExpr
7985 * | AdditiveExpr '-' MultiplicativeExpr
7986 *
7987 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007988 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007989 * @param[in] repeat How many times this expression is repeated.
7990 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7991 * @param[in] options XPath options.
7992 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7993 */
7994static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007995eval_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 +02007996{
7997 LY_ERR rc;
7998 uint16_t this_op;
7999 struct lyxp_set orig_set, set2;
8000 uint16_t i;
8001
8002 assert(repeat);
8003
8004 set_init(&orig_set, set);
8005 set_init(&set2, set);
8006
8007 set_fill_set(&orig_set, set);
8008
Michal Vasko004d3152020-06-11 19:59:22 +02008009 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008010 LY_CHECK_GOTO(rc, cleanup);
8011
8012 /* ('+' / '-' MultiplicativeExpr)* */
8013 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008014 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008015
Michal Vasko004d3152020-06-11 19:59:22 +02008016 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008017 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008018 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008019 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008020
8021 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008022 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008023 LY_CHECK_GOTO(rc, cleanup);
8024 continue;
8025 }
8026
8027 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008028 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008029 LY_CHECK_GOTO(rc, cleanup);
8030
8031 /* eval */
8032 if (options & LYXP_SCNODE_ALL) {
8033 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008034 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008035 set_scnode_clear_ctx(set);
8036 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008037 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008038 LY_CHECK_GOTO(rc, cleanup);
8039 }
8040 }
8041
8042cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008043 lyxp_set_free_content(&orig_set);
8044 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008045 return rc;
8046}
8047
8048/**
8049 * @brief Evaluate RelationalExpr. Logs directly on error.
8050 *
Michal Vaskod3678892020-05-21 10:06:58 +02008051 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008052 * | RelationalExpr '<' AdditiveExpr
8053 * | RelationalExpr '>' AdditiveExpr
8054 * | RelationalExpr '<=' AdditiveExpr
8055 * | RelationalExpr '>=' AdditiveExpr
8056 *
8057 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008058 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008059 * @param[in] repeat How many times this expression is repeated.
8060 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8061 * @param[in] options XPath options.
8062 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8063 */
8064static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008065eval_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 +02008066{
8067 LY_ERR rc;
8068 uint16_t this_op;
8069 struct lyxp_set orig_set, set2;
8070 uint16_t i;
8071
8072 assert(repeat);
8073
8074 set_init(&orig_set, set);
8075 set_init(&set2, set);
8076
8077 set_fill_set(&orig_set, set);
8078
Michal Vasko004d3152020-06-11 19:59:22 +02008079 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008080 LY_CHECK_GOTO(rc, cleanup);
8081
8082 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8083 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008084 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008085
Michal Vasko004d3152020-06-11 19:59:22 +02008086 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008087 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008088 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008089 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008090
8091 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008092 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008093 LY_CHECK_GOTO(rc, cleanup);
8094 continue;
8095 }
8096
8097 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008098 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008099 LY_CHECK_GOTO(rc, cleanup);
8100
8101 /* eval */
8102 if (options & LYXP_SCNODE_ALL) {
8103 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008104 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008105 set_scnode_clear_ctx(set);
8106 } else {
8107 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8108 LY_CHECK_GOTO(rc, cleanup);
8109 }
8110 }
8111
8112cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008113 lyxp_set_free_content(&orig_set);
8114 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008115 return rc;
8116}
8117
8118/**
8119 * @brief Evaluate EqualityExpr. Logs directly on error.
8120 *
Michal Vaskod3678892020-05-21 10:06:58 +02008121 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008122 * | EqualityExpr '!=' RelationalExpr
8123 *
8124 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008125 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008126 * @param[in] repeat How many times this expression is repeated.
8127 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8128 * @param[in] options XPath options.
8129 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8130 */
8131static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008132eval_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 +02008133{
8134 LY_ERR rc;
8135 uint16_t this_op;
8136 struct lyxp_set orig_set, set2;
8137 uint16_t i;
8138
8139 assert(repeat);
8140
8141 set_init(&orig_set, set);
8142 set_init(&set2, set);
8143
8144 set_fill_set(&orig_set, set);
8145
Michal Vasko004d3152020-06-11 19:59:22 +02008146 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008147 LY_CHECK_GOTO(rc, cleanup);
8148
8149 /* ('=' / '!=' RelationalExpr)* */
8150 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008151 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008152
Michal Vasko004d3152020-06-11 19:59:22 +02008153 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008154 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008155 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008156 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008157
8158 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008159 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008160 LY_CHECK_GOTO(rc, cleanup);
8161 continue;
8162 }
8163
8164 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008165 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008166 LY_CHECK_GOTO(rc, cleanup);
8167
8168 /* eval */
8169 if (options & LYXP_SCNODE_ALL) {
8170 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008171 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8172 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008173 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008174 set_scnode_clear_ctx(set);
8175 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008176 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8177 LY_CHECK_GOTO(rc, cleanup);
8178 }
8179 }
8180
8181cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008182 lyxp_set_free_content(&orig_set);
8183 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008184 return rc;
8185}
8186
8187/**
8188 * @brief Evaluate AndExpr. Logs directly on error.
8189 *
Michal Vaskod3678892020-05-21 10:06:58 +02008190 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008191 *
8192 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008193 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008194 * @param[in] repeat How many times this expression is repeated.
8195 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8196 * @param[in] options XPath options.
8197 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8198 */
8199static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008200eval_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 +02008201{
8202 LY_ERR rc;
8203 struct lyxp_set orig_set, set2;
8204 uint16_t i;
8205
8206 assert(repeat);
8207
8208 set_init(&orig_set, set);
8209 set_init(&set2, set);
8210
8211 set_fill_set(&orig_set, set);
8212
Michal Vasko004d3152020-06-11 19:59:22 +02008213 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008214 LY_CHECK_GOTO(rc, cleanup);
8215
8216 /* cast to boolean, we know that will be the final result */
8217 if (set && (options & LYXP_SCNODE_ALL)) {
8218 set_scnode_clear_ctx(set);
8219 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008220 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008221 }
8222
8223 /* ('and' EqualityExpr)* */
8224 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008225 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8226 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008227 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008228 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008229
8230 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008231 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8232 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008233 LY_CHECK_GOTO(rc, cleanup);
8234 continue;
8235 }
8236
8237 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008238 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008239 LY_CHECK_GOTO(rc, cleanup);
8240
8241 /* eval - just get boolean value actually */
8242 if (set->type == LYXP_SET_SCNODE_SET) {
8243 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008244 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008245 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008246 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008247 set_fill_set(set, &set2);
8248 }
8249 }
8250
8251cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008252 lyxp_set_free_content(&orig_set);
8253 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008254 return rc;
8255}
8256
8257/**
8258 * @brief Evaluate OrExpr. Logs directly on error.
8259 *
Michal Vaskod3678892020-05-21 10:06:58 +02008260 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008261 *
8262 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008263 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008264 * @param[in] repeat How many times this expression is repeated.
8265 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8266 * @param[in] options XPath options.
8267 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8268 */
8269static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008270eval_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 +02008271{
8272 LY_ERR rc;
8273 struct lyxp_set orig_set, set2;
8274 uint16_t i;
8275
8276 assert(repeat);
8277
8278 set_init(&orig_set, set);
8279 set_init(&set2, set);
8280
8281 set_fill_set(&orig_set, set);
8282
Michal Vasko004d3152020-06-11 19:59:22 +02008283 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008284 LY_CHECK_GOTO(rc, cleanup);
8285
8286 /* cast to boolean, we know that will be the final result */
8287 if (set && (options & LYXP_SCNODE_ALL)) {
8288 set_scnode_clear_ctx(set);
8289 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008290 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008291 }
8292
8293 /* ('or' AndExpr)* */
8294 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008295 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8296 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008297 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008298 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008299
8300 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008301 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8302 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008303 LY_CHECK_GOTO(rc, cleanup);
8304 continue;
8305 }
8306
8307 set_fill_set(&set2, &orig_set);
8308 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8309 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008310 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008311 LY_CHECK_GOTO(rc, cleanup);
8312
8313 /* eval - just get boolean value actually */
8314 if (set->type == LYXP_SET_SCNODE_SET) {
8315 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008316 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008317 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008318 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008319 set_fill_set(set, &set2);
8320 }
8321 }
8322
8323cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008324 lyxp_set_free_content(&orig_set);
8325 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008326 return rc;
8327}
8328
8329/**
Michal Vasko004d3152020-06-11 19:59:22 +02008330 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008331 *
8332 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008333 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008334 * @param[in] etype Expression type to evaluate.
8335 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8336 * @param[in] options XPath options.
8337 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8338 */
8339static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008340eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8341 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008342{
8343 uint16_t i, count;
8344 enum lyxp_expr_type next_etype;
8345 LY_ERR rc;
8346
8347 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008348 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008349 next_etype = LYXP_EXPR_NONE;
8350 } else {
8351 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008352 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008353
8354 /* select one-priority lower because etype expression called us */
8355 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008356 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008357 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008358 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008359 } else {
8360 next_etype = LYXP_EXPR_NONE;
8361 }
8362 }
8363
8364 /* decide what expression are we parsing based on the repeat */
8365 switch (next_etype) {
8366 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008367 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008368 break;
8369 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008370 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008371 break;
8372 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008373 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008374 break;
8375 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008376 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008377 break;
8378 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008379 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008380 break;
8381 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008382 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008383 break;
8384 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008385 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008386 break;
8387 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008388 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008389 break;
8390 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008391 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008392 break;
8393 default:
8394 LOGINT_RET(set->ctx);
8395 }
8396
8397 return rc;
8398}
8399
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008400/**
8401 * @brief Get root type.
8402 *
8403 * @param[in] ctx_node Context node.
8404 * @param[in] ctx_scnode Schema context node.
8405 * @param[in] options XPath options.
8406 * @return Root type.
8407 */
8408static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008409lyxp_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 +01008410{
Michal Vasko6b26e742020-07-17 15:02:10 +02008411 const struct lysc_node *op;
8412
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008413 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008414 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008415 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008416
8417 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008418 /* general root that can access everything */
8419 return LYXP_NODE_ROOT;
8420 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8421 /* root context node can access only config data (because we said so, it is unspecified) */
8422 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008423 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008424 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008425 }
8426
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008427 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008428 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008429 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008430
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008431 if (op || !(options & LYXP_SCHEMA)) {
8432 /* general root that can access everything */
8433 return LYXP_NODE_ROOT;
8434 } else if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008435 /* root context node can access only config data (because we said so, it is unspecified) */
8436 return LYXP_NODE_ROOT_CONFIG;
8437 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008438 return LYXP_NODE_ROOT;
8439}
8440
Michal Vasko03ff5a72019-09-11 13:49:33 +02008441LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008442lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
8443 LY_PREFIX_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node, const struct lyd_node *tree,
8444 struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008445{
Michal Vasko004d3152020-06-11 19:59:22 +02008446 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008447 LY_ERR rc;
8448
Michal Vasko400e9672021-01-11 13:39:17 +01008449 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008450 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8451 LOGARG(NULL, "Current module must be set if schema format is used.");
8452 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008453 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008454
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008455 if (tree) {
8456 /* adjust the pointer to be the first top-level sibling */
8457 while (tree->parent) {
8458 tree = lyd_parent(tree);
8459 }
8460 tree = lyd_first_sibling(tree);
8461 }
8462
Michal Vasko03ff5a72019-09-11 13:49:33 +02008463 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008464 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008465 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008466 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8467 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8468
Michal Vasko400e9672021-01-11 13:39:17 +01008469 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008470 set->cur_node = ctx_node;
8471 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008472 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8473 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008474 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008475 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008476 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008477 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008478
Radek Krejciddace2c2021-01-08 11:30:56 +01008479 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008480
Michal Vasko03ff5a72019-09-11 13:49:33 +02008481 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008482 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008483 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008484 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008485 }
8486
Radek Krejciddace2c2021-01-08 11:30:56 +01008487 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008488 return rc;
8489}
8490
8491#if 0
8492
8493/* full xml printing of set elements, not used currently */
8494
8495void
8496lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8497{
8498 uint32_t i;
8499 char *str_num;
8500 struct lyout out;
8501
8502 memset(&out, 0, sizeof out);
8503
8504 out.type = LYOUT_STREAM;
8505 out.method.f = f;
8506
8507 switch (set->type) {
8508 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008509 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008510 break;
8511 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008512 ly_print_(&out, "Boolean XPath set:\n");
8513 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008514 break;
8515 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008516 ly_print_(&out, "String XPath set:\n");
8517 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008518 break;
8519 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008520 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008521
8522 if (isnan(set->value.num)) {
8523 str_num = strdup("NaN");
8524 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8525 str_num = strdup("0");
8526 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8527 str_num = strdup("Infinity");
8528 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8529 str_num = strdup("-Infinity");
8530 } else if ((long long)set->value.num == set->value.num) {
8531 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8532 str_num = NULL;
8533 }
8534 } else {
8535 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8536 str_num = NULL;
8537 }
8538 }
8539 if (!str_num) {
8540 LOGMEM;
8541 return;
8542 }
Michal Vasko5233e962020-08-14 14:26:20 +02008543 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008544 free(str_num);
8545 break;
8546 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008547 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008548
8549 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008550 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008551 switch (set->node_type[i]) {
8552 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008553 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008554 break;
8555 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008556 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008557 break;
8558 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008559 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008560 break;
8561 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008562 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008563 break;
8564 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008565 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008566 break;
8567 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008568 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008569 break;
8570 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008571 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008572 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008573 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008574 break;
8575 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008576 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 +02008577 break;
8578 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008579 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 +02008580 break;
8581 }
8582 }
8583 break;
8584 }
8585}
8586
8587#endif
8588
8589LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008590lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008591{
8592 long double num;
8593 char *str;
8594 LY_ERR rc;
8595
8596 if (!set || (set->type == target)) {
8597 return LY_SUCCESS;
8598 }
8599
8600 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008601 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008602
8603 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008604 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008605 return LY_EINVAL;
8606 }
8607
8608 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008609 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008610 switch (set->type) {
8611 case LYXP_SET_NUMBER:
8612 if (isnan(set->val.num)) {
8613 set->val.str = strdup("NaN");
8614 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8615 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8616 set->val.str = strdup("0");
8617 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8618 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8619 set->val.str = strdup("Infinity");
8620 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8621 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8622 set->val.str = strdup("-Infinity");
8623 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8624 } else if ((long long)set->val.num == set->val.num) {
8625 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8626 LOGMEM_RET(set->ctx);
8627 }
8628 set->val.str = str;
8629 } else {
8630 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8631 LOGMEM_RET(set->ctx);
8632 }
8633 set->val.str = str;
8634 }
8635 break;
8636 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008637 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008638 set->val.str = strdup("true");
8639 } else {
8640 set->val.str = strdup("false");
8641 }
8642 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8643 break;
8644 case LYXP_SET_NODE_SET:
8645 assert(set->used);
8646
8647 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008648 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008649
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008650 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008651 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008652 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008653 set->val.str = str;
8654 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008655 default:
8656 LOGINT_RET(set->ctx);
8657 }
8658 set->type = LYXP_SET_STRING;
8659 }
8660
8661 /* to NUMBER */
8662 if (target == LYXP_SET_NUMBER) {
8663 switch (set->type) {
8664 case LYXP_SET_STRING:
8665 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008666 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008667 set->val.num = num;
8668 break;
8669 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008670 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008671 set->val.num = 1;
8672 } else {
8673 set->val.num = 0;
8674 }
8675 break;
8676 default:
8677 LOGINT_RET(set->ctx);
8678 }
8679 set->type = LYXP_SET_NUMBER;
8680 }
8681
8682 /* to BOOLEAN */
8683 if (target == LYXP_SET_BOOLEAN) {
8684 switch (set->type) {
8685 case LYXP_SET_NUMBER:
8686 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008687 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008688 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008689 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008690 }
8691 break;
8692 case LYXP_SET_STRING:
8693 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008694 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008695 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008696 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008697 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008698 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008699 }
8700 break;
8701 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008702 if (set->used) {
8703 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008704 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008705 } else {
8706 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008707 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008708 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008709 break;
8710 default:
8711 LOGINT_RET(set->ctx);
8712 }
8713 set->type = LYXP_SET_BOOLEAN;
8714 }
8715
Michal Vasko03ff5a72019-09-11 13:49:33 +02008716 return LY_SUCCESS;
8717}
8718
8719LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008720lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
8721 LY_PREFIX_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
8722 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008723{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008724 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008725 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008726
Michal Vasko400e9672021-01-11 13:39:17 +01008727 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008728 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8729 LOGARG(NULL, "Current module must be set if schema format is used.");
8730 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008731 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008732
8733 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008734 memset(set, 0, sizeof *set);
8735 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008736 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8737 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 +01008738 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008739
Michal Vasko400e9672021-01-11 13:39:17 +01008740 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008741 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008742 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008743 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8744 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008745 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008746 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008747 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008748
Radek Krejciddace2c2021-01-08 11:30:56 +01008749 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008750
Michal Vasko03ff5a72019-09-11 13:49:33 +02008751 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01008752 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
8753
Radek Krejciddace2c2021-01-08 11:30:56 +01008754 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008755 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008756}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008757
8758API const char *
8759lyxp_get_expr(const struct lyxp_expr *path)
8760{
8761 if (!path) {
8762 return NULL;
8763 }
8764
8765 return path->expr;
8766}