blob: 89aa0d09a30351738cd9478899d851bddc148321 [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 Vasko00cbf532020-06-15 13:58:47 +02003703 oper = lysc_is_output((struct lysc_node *)sleaf) ? 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 Vaskoc0792ca2020-12-01 12:03:21 +01003706 rc = ly_path_compile(set->ctx, lref->cur_mod, (struct lysc_node *)sleaf, lref->path,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003707 LY_PATH_LREF_TRUE, oper, 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 Krejci2efc45b2020-12-22 16:25:44 +01004595 LOG_LOCSET(set->ctx, NULL, set->cur_node, NULL, NULL);
4596 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
4597 LOG_LOCBACK(set->ctx, 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 */
5861 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005862 if (!next) {
5863skip_children:
5864 /* no children, so try siblings, but only if it's not the start,
5865 * that is considered to be the root and it's siblings are not traversed */
5866 if (elem != start) {
5867 next = elem->next;
5868 } else {
5869 break;
5870 }
5871 }
5872 while (!next) {
5873 /* no siblings, go back through the parents */
5874 if (elem->parent == start) {
5875 /* we are done, no next element to process */
5876 break;
5877 }
5878 /* parent is already processed, go to its sibling */
5879 elem = elem->parent;
5880 next = elem->next;
5881 }
5882 }
5883 }
5884
5885 return LY_SUCCESS;
5886}
5887
5888/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005889 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005890 * Indirectly context position aware.
5891 *
5892 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005893 * @param[in] mod Matching metadata module, NULL for any.
5894 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005895 * @return LY_ERR
5896 */
5897static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005898moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005899{
Michal Vasko9f96a052020-03-10 09:41:45 +01005900 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005901
Michal Vaskod3678892020-05-21 10:06:58 +02005902 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005903 return LY_SUCCESS;
5904 }
5905
5906 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005907 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005908 return LY_EVALID;
5909 }
5910
Radek Krejci1deb5be2020-08-26 16:43:36 +02005911 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005912 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005913
5914 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5915 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005916 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005917 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005918
5919 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005920 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005921 continue;
5922 }
5923
Michal Vaskod3678892020-05-21 10:06:58 +02005924 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005925 /* match */
5926 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005927 set->val.meta[i].meta = sub;
5928 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005929 /* pos does not change */
5930 replaced = 1;
5931 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005932 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 +02005933 }
5934 ++i;
5935 }
5936 }
5937 }
5938
5939 if (!replaced) {
5940 /* no match */
5941 set_remove_node(set, i);
5942 }
5943 }
5944
5945 return LY_SUCCESS;
5946}
5947
5948/**
5949 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005950 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005951 *
5952 * @param[in,out] set1 Set to use for the result.
5953 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005954 * @return LY_ERR
5955 */
5956static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005957moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005958{
5959 LY_ERR rc;
5960
Michal Vaskod3678892020-05-21 10:06:58 +02005961 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005962 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005963 return LY_EVALID;
5964 }
5965
5966 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005967 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005968 return LY_SUCCESS;
5969 }
5970
Michal Vaskod3678892020-05-21 10:06:58 +02005971 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005972 memcpy(set1, set2, sizeof *set1);
5973 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005974 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005975 return LY_SUCCESS;
5976 }
5977
5978 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005979 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005980
5981 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005982 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005983 LY_CHECK_RET(rc);
5984
5985 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005986 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005987
5988 return LY_SUCCESS;
5989}
5990
5991/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005992 * @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 +02005993 * Context position aware.
5994 *
5995 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005996 * @param[in] mod Matching metadata module, NULL for any.
5997 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005998 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005999 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6000 */
6001static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006002moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006003{
Michal Vasko9f96a052020-03-10 09:41:45 +01006004 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006005 struct lyxp_set *set_all_desc = NULL;
6006 LY_ERR rc;
6007
Michal Vaskod3678892020-05-21 10:06:58 +02006008 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006009 return LY_SUCCESS;
6010 }
6011
6012 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006013 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006014 return LY_EVALID;
6015 }
6016
Michal Vasko03ff5a72019-09-11 13:49:33 +02006017 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6018 * but it likely won't be used much, so it's a waste of time */
6019 /* copy the context */
6020 set_all_desc = set_copy(set);
6021 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006022 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006023 if (rc != LY_SUCCESS) {
6024 lyxp_set_free(set_all_desc);
6025 return rc;
6026 }
6027 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006028 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006029 if (rc != LY_SUCCESS) {
6030 lyxp_set_free(set_all_desc);
6031 return rc;
6032 }
6033 lyxp_set_free(set_all_desc);
6034
Radek Krejci1deb5be2020-08-26 16:43:36 +02006035 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006036 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006037
6038 /* only attributes of an elem can be in the result, skip all the rest,
6039 * we have all attributes qualified in lyd tree */
6040 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006041 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006042 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006043 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006044 continue;
6045 }
6046
Michal Vaskod3678892020-05-21 10:06:58 +02006047 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006048 /* match */
6049 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006050 set->val.meta[i].meta = sub;
6051 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006052 /* pos does not change */
6053 replaced = 1;
6054 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006055 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 +02006056 }
6057 ++i;
6058 }
6059 }
6060 }
6061
6062 if (!replaced) {
6063 /* no match */
6064 set_remove_node(set, i);
6065 }
6066 }
6067
6068 return LY_SUCCESS;
6069}
6070
6071/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006072 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6073 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006074 *
6075 * @param[in] parent Current parent.
6076 * @param[in] parent_pos Position of @p parent.
6077 * @param[in] parent_type Node type of @p parent.
6078 * @param[in,out] to_set Set to use.
6079 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006080 * @param[in] options XPath options.
6081 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6082 */
6083static LY_ERR
6084moveto_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 +02006085 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006086{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006087 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006088 LY_ERR rc;
6089
6090 switch (parent_type) {
6091 case LYXP_NODE_ROOT:
6092 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006093 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006094
Michal Vasko61ac2f62020-05-25 12:39:51 +02006095 /* add all top-level nodes as elements */
6096 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006097 break;
6098 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006099 /* add just the text node of this term element node */
6100 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006101 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6102 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6103 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006104 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006105 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006106
6107 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006108 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006109 break;
6110 default:
6111 LOGINT_RET(parent->schema->module->ctx);
6112 }
6113
Michal Vasko61ac2f62020-05-25 12:39:51 +02006114 /* add all top-level nodes as elements */
6115 LY_LIST_FOR(first, iter) {
6116 /* context check */
6117 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6118 continue;
6119 }
6120
6121 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006122 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006123 return LY_EINCOMPLETE;
6124 }
6125
6126 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6127 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6128
6129 /* also add all the children of this node, recursively */
6130 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6131 LY_CHECK_RET(rc);
6132 }
6133 }
6134
Michal Vasko03ff5a72019-09-11 13:49:33 +02006135 return LY_SUCCESS;
6136}
6137
6138/**
6139 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6140 * (or LYXP_SET_EMPTY). Context position aware.
6141 *
6142 * @param[in,out] set Set to use.
6143 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6144 * @param[in] options XPath options.
6145 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6146 */
6147static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006148moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006149{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006150 struct lyxp_set ret_set;
6151 LY_ERR rc;
6152
Michal Vaskod3678892020-05-21 10:06:58 +02006153 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006154 return LY_SUCCESS;
6155 }
6156
6157 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006158 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006159 return LY_EVALID;
6160 }
6161
6162 /* nothing to do */
6163 if (!all_desc) {
6164 return LY_SUCCESS;
6165 }
6166
Michal Vasko03ff5a72019-09-11 13:49:33 +02006167 /* add all the children, they get added recursively */
6168 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006169 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006170 /* copy the current node to tmp */
6171 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6172
6173 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006174 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006175 continue;
6176 }
6177
Michal Vasko03ff5a72019-09-11 13:49:33 +02006178 /* add all the children */
6179 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 +02006180 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006181 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006182 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006183 return rc;
6184 }
6185 }
6186
6187 /* use the temporary set as the current one */
6188 ret_set.ctx_pos = set->ctx_pos;
6189 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006190 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006191 memcpy(set, &ret_set, sizeof *set);
6192
6193 return LY_SUCCESS;
6194}
6195
6196/**
6197 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6198 * (or LYXP_SET_EMPTY).
6199 *
6200 * @param[in,out] set Set to use.
6201 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6202 * @param[in] options XPath options.
6203 * @return LY_ERR
6204 */
6205static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006206moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006207{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006208 uint32_t getnext_opts;
6209 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006210 const struct lysc_node *iter, *start_parent;
6211 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006212
Michal Vaskod3678892020-05-21 10:06:58 +02006213 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006214 return LY_SUCCESS;
6215 }
6216
6217 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006218 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006219 return LY_EVALID;
6220 }
6221
Michal Vasko519fd602020-05-26 12:17:39 +02006222 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006223 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006224 if (options & LYXP_SCNODE_OUTPUT) {
6225 getnext_opts |= LYS_GETNEXT_OUTPUT;
6226 }
6227
6228 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006229 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006230 if (!all_desc) {
6231 /* traverse the start node */
6232 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6233 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6234 }
6235 continue;
6236 }
6237
Radek Krejcif13b87b2020-12-01 22:02:17 +01006238 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6239 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006240 continue;
6241 }
6242
Michal Vasko519fd602020-05-26 12:17:39 +02006243 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006244 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006245 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006246 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006247 }
6248
Michal Vasko519fd602020-05-26 12:17:39 +02006249 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006250
Michal Vasko519fd602020-05-26 12:17:39 +02006251 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6252 /* it can actually be in any module, it's all <running> */
6253 mod_idx = 0;
6254 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6255 iter = NULL;
6256 /* module may not be implemented */
6257 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6258 /* context check */
6259 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6260 continue;
6261 }
6262
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006263 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006264 /* throw away the insert index, we want to consider that node again, recursively */
6265 }
6266 }
6267
6268 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6269 iter = NULL;
6270 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006271 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006272 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006273 continue;
6274 }
6275
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006276 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006277 }
6278 }
6279 }
6280
6281 return LY_SUCCESS;
6282}
6283
6284/**
6285 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6286 * (or LYXP_SET_EMPTY). Context position aware.
6287 *
6288 * @param[in] set Set to use.
6289 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6290 * @param[in] options XPath options.
6291 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6292 */
6293static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006294moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006295{
6296 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006297 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006298 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006299
Michal Vaskod3678892020-05-21 10:06:58 +02006300 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006301 return LY_SUCCESS;
6302 }
6303
6304 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006305 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006306 return LY_EVALID;
6307 }
6308
6309 if (all_desc) {
6310 /* <path>//.. == <path>//./.. */
6311 rc = moveto_self(set, 1, options);
6312 LY_CHECK_RET(rc);
6313 }
6314
Radek Krejci1deb5be2020-08-26 16:43:36 +02006315 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006316 node = set->val.nodes[i].node;
6317
6318 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6319 new_node = (struct lyd_node *)node->parent;
6320 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6321 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006322 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6323 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006324 if (!new_node) {
6325 LOGINT_RET(set->ctx);
6326 }
6327 } else {
6328 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006329 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006330 continue;
6331 }
6332
Michal Vaskoa1424542019-11-14 16:08:52 +01006333 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006334 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 +02006335 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006336 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006337
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006338 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006339 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006340 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006341
Michal Vasko03ff5a72019-09-11 13:49:33 +02006342 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006343 /* 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 +02006344 new_type = LYXP_NODE_ELEM;
6345 }
6346
Michal Vasko03ff5a72019-09-11 13:49:33 +02006347 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006348 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006349 } else {
6350 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006351 }
6352 }
6353
Michal Vasko2caefc12019-11-14 16:07:56 +01006354 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006355 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006356
6357 return LY_SUCCESS;
6358}
6359
6360/**
6361 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6362 * (or LYXP_SET_EMPTY).
6363 *
6364 * @param[in] set Set to use.
6365 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6366 * @param[in] options XPath options.
6367 * @return LY_ERR
6368 */
6369static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006370moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006371{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006372 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006373 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006374 const struct lysc_node *node, *new_node;
6375 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006376
Michal Vaskod3678892020-05-21 10:06:58 +02006377 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006378 return LY_SUCCESS;
6379 }
6380
6381 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006382 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006383 return LY_EVALID;
6384 }
6385
6386 if (all_desc) {
6387 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006388 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006389 }
6390
Michal Vasko03ff5a72019-09-11 13:49:33 +02006391 orig_used = set->used;
6392 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006393 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6394 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006395 continue;
6396 }
6397
6398 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006399 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006400 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006401 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006402 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006403
6404 node = set->val.scnodes[i].scnode;
6405
6406 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006407 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006408 } else {
6409 /* root does not have a parent */
6410 continue;
6411 }
6412
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006413 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006414 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006415 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006416
Michal Vasko03ff5a72019-09-11 13:49:33 +02006417 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006418 /* 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 +02006419 new_type = LYXP_NODE_ELEM;
6420 }
6421
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006422 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6423 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006424 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006425 temp_ctx = 1;
6426 }
6427 }
6428
6429 if (temp_ctx) {
6430 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006431 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6432 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006433 }
6434 }
6435 }
6436
6437 return LY_SUCCESS;
6438}
6439
6440/**
6441 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6442 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6443 *
6444 * @param[in,out] set1 Set to use for the result.
6445 * @param[in] set2 Set acting as the second operand for @p op.
6446 * @param[in] op Comparison operator to process.
6447 * @param[in] options XPath options.
6448 * @return LY_ERR
6449 */
6450static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006451moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006452{
6453 /*
6454 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6455 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6456 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6457 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6458 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6459 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6460 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6461 *
6462 * '=' or '!='
6463 * BOOLEAN + BOOLEAN
6464 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6465 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6466 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6467 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6468 * NUMBER + NUMBER
6469 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6470 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6471 * STRING + STRING
6472 *
6473 * '<=', '<', '>=', '>'
6474 * NUMBER + NUMBER
6475 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6476 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6477 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6478 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6479 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6480 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6481 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6482 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6483 */
6484 struct lyxp_set iter1, iter2;
6485 int result;
6486 int64_t i;
6487 LY_ERR rc;
6488
Michal Vasko004d3152020-06-11 19:59:22 +02006489 memset(&iter1, 0, sizeof iter1);
6490 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006491
6492 /* iterative evaluation with node-sets */
6493 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6494 if (set1->type == LYXP_SET_NODE_SET) {
6495 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006496 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006497 switch (set2->type) {
6498 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006499 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006500 break;
6501 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006502 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006503 break;
6504 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006505 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006506 break;
6507 }
6508 LY_CHECK_RET(rc);
6509
Michal Vasko4c7763f2020-07-27 17:40:37 +02006510 /* canonize set2 */
6511 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6512
6513 /* compare recursively */
6514 rc = moveto_op_comp(&iter1, &iter2, op, options);
6515 lyxp_set_free_content(&iter2);
6516 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006517
6518 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006519 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006520 set_fill_boolean(set1, 1);
6521 return LY_SUCCESS;
6522 }
6523 }
6524 } else {
6525 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006526 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006527 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006528 case LYXP_SET_NUMBER:
6529 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6530 break;
6531 case LYXP_SET_BOOLEAN:
6532 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6533 break;
6534 default:
6535 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6536 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006537 }
6538 LY_CHECK_RET(rc);
6539
Michal Vasko4c7763f2020-07-27 17:40:37 +02006540 /* canonize set1 */
6541 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 +02006542
Michal Vasko4c7763f2020-07-27 17:40:37 +02006543 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006544 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006545 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006546 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006547
6548 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006549 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006550 set_fill_boolean(set1, 1);
6551 return LY_SUCCESS;
6552 }
6553 }
6554 }
6555
6556 /* false for all nodes */
6557 set_fill_boolean(set1, 0);
6558 return LY_SUCCESS;
6559 }
6560
6561 /* first convert properly */
6562 if ((op[0] == '=') || (op[0] == '!')) {
6563 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006564 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6565 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006566 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006567 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006568 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006569 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006570 LY_CHECK_RET(rc);
6571 } /* else we have 2 strings */
6572 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006573 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006574 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006575 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006576 LY_CHECK_RET(rc);
6577 }
6578
6579 assert(set1->type == set2->type);
6580
6581 /* compute result */
6582 if (op[0] == '=') {
6583 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006584 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006585 } else if (set1->type == LYXP_SET_NUMBER) {
6586 result = (set1->val.num == set2->val.num);
6587 } else {
6588 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006589 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006590 }
6591 } else if (op[0] == '!') {
6592 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006593 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006594 } else if (set1->type == LYXP_SET_NUMBER) {
6595 result = (set1->val.num != set2->val.num);
6596 } else {
6597 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006598 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006599 }
6600 } else {
6601 assert(set1->type == LYXP_SET_NUMBER);
6602 if (op[0] == '<') {
6603 if (op[1] == '=') {
6604 result = (set1->val.num <= set2->val.num);
6605 } else {
6606 result = (set1->val.num < set2->val.num);
6607 }
6608 } else {
6609 if (op[1] == '=') {
6610 result = (set1->val.num >= set2->val.num);
6611 } else {
6612 result = (set1->val.num > set2->val.num);
6613 }
6614 }
6615 }
6616
6617 /* assign result */
6618 if (result) {
6619 set_fill_boolean(set1, 1);
6620 } else {
6621 set_fill_boolean(set1, 0);
6622 }
6623
6624 return LY_SUCCESS;
6625}
6626
6627/**
6628 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6629 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6630 *
6631 * @param[in,out] set1 Set to use for the result.
6632 * @param[in] set2 Set acting as the second operand for @p op.
6633 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006634 * @return LY_ERR
6635 */
6636static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006637moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006638{
6639 LY_ERR rc;
6640
6641 /* unary '-' */
6642 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006643 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006644 LY_CHECK_RET(rc);
6645 set1->val.num *= -1;
6646 lyxp_set_free(set2);
6647 return LY_SUCCESS;
6648 }
6649
6650 assert(set1 && set2);
6651
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006652 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006653 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006654 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006655 LY_CHECK_RET(rc);
6656
6657 switch (op[0]) {
6658 /* '+' */
6659 case '+':
6660 set1->val.num += set2->val.num;
6661 break;
6662
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 /* 'div' */
6674 case 'd':
6675 set1->val.num /= set2->val.num;
6676 break;
6677
6678 /* 'mod' */
6679 case 'm':
6680 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6681 break;
6682
6683 default:
6684 LOGINT_RET(set1->ctx);
6685 }
6686
6687 return LY_SUCCESS;
6688}
6689
6690/*
6691 * eval functions
6692 *
6693 * They execute a parsed XPath expression on some data subtree.
6694 */
6695
6696/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006697 * @brief Evaluate Predicate. Logs directly on error.
6698 *
Michal Vaskod3678892020-05-21 10:06:58 +02006699 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006700 *
6701 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006702 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006703 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6704 * @param[in] options XPath options.
6705 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6706 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6707 */
6708static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006709eval_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 +02006710{
6711 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006712 uint16_t orig_exp;
6713 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006714 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006715 struct lyxp_set set2;
6716 struct lyd_node *orig_parent;
6717
6718 /* '[' */
6719 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006720 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006721 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006722
6723 if (!set) {
6724only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006725 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006726 LY_CHECK_RET(rc);
6727 } else if (set->type == LYXP_SET_NODE_SET) {
6728 /* 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 +01006729 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006730
6731 /* empty set, nothing to evaluate */
6732 if (!set->used) {
6733 goto only_parse;
6734 }
6735
Michal Vasko004d3152020-06-11 19:59:22 +02006736 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006737 orig_pos = 0;
6738 orig_size = set->used;
6739 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006740 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006741 set_init(&set2, set);
6742 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6743 /* remember the node context position for position() and context size for last(),
6744 * predicates should always be evaluated with respect to the child axis (since we do
6745 * not support explicit axes) so we assign positions based on their parents */
6746 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6747 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6748 orig_pos = 1;
6749 } else {
6750 ++orig_pos;
6751 }
6752
6753 set2.ctx_pos = orig_pos;
6754 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006755 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006756
Michal Vasko004d3152020-06-11 19:59:22 +02006757 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006758 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006759 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006760 return rc;
6761 }
6762
6763 /* number is a position */
6764 if (set2.type == LYXP_SET_NUMBER) {
6765 if ((long long)set2.val.num == orig_pos) {
6766 set2.val.num = 1;
6767 } else {
6768 set2.val.num = 0;
6769 }
6770 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006771 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006772
6773 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006774 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006775 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006776 }
6777 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006778 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006779
6780 } else if (set->type == LYXP_SET_SCNODE_SET) {
6781 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006782 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006783 /* there is a currently-valid node */
6784 break;
6785 }
6786 }
6787 /* empty set, nothing to evaluate */
6788 if (i == set->used) {
6789 goto only_parse;
6790 }
6791
Michal Vasko004d3152020-06-11 19:59:22 +02006792 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006793
Michal Vasko03ff5a72019-09-11 13:49:33 +02006794 /* set special in_ctx to all the valid snodes */
6795 pred_in_ctx = set_scnode_new_in_ctx(set);
6796
6797 /* use the valid snodes one-by-one */
6798 for (i = 0; i < set->used; ++i) {
6799 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6800 continue;
6801 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006802 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006803
Michal Vasko004d3152020-06-11 19:59:22 +02006804 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006805
Michal Vasko004d3152020-06-11 19:59:22 +02006806 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006807 LY_CHECK_RET(rc);
6808
6809 set->val.scnodes[i].in_ctx = pred_in_ctx;
6810 }
6811
6812 /* restore the state as it was before the predicate */
6813 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006814 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
6815 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006816 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006817 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006818 }
6819 }
6820
6821 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006822 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006823 set_fill_set(&set2, set);
6824
Michal Vasko004d3152020-06-11 19:59:22 +02006825 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006826 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006827 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006828 return rc;
6829 }
6830
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006831 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006832 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006833 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006834 }
Michal Vaskod3678892020-05-21 10:06:58 +02006835 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006836 }
6837
6838 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006839 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006840 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006841 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006842 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006843
6844 return LY_SUCCESS;
6845}
6846
6847/**
Michal Vaskod3678892020-05-21 10:06:58 +02006848 * @brief Evaluate Literal. Logs directly on error.
6849 *
6850 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006851 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006852 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6853 */
6854static void
Michal Vasko40308e72020-10-20 16:38:40 +02006855eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006856{
6857 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006858 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006859 set_fill_string(set, "", 0);
6860 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006861 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 +02006862 }
6863 }
6864 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006865 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006866 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006867}
6868
6869/**
Michal Vasko004d3152020-06-11 19:59:22 +02006870 * @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 +02006871 *
Michal Vasko004d3152020-06-11 19:59:22 +02006872 * @param[in] exp Full parsed XPath expression.
6873 * @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 +02006874 * @param[in] ctx_node Found schema node as the context for the predicate.
6875 * @param[in] cur_mod Current module for the expression.
6876 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006877 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006878 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006879 * @param[out] predicates Parsed predicates.
6880 * @param[out] pred_type Type of @p predicates.
6881 * @return LY_SUCCESS on success,
6882 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006883 */
6884static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006885eval_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 +02006886 const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_PREFIX_FORMAT format, void *prefix_data,
6887 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006888{
Michal Vasko004d3152020-06-11 19:59:22 +02006889 LY_ERR ret = LY_SUCCESS;
6890 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006891 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006892 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006893 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006894 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006895
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006896 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006897
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006898 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006899 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006900 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006901 return LY_EINVAL;
6902 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006903 for (key_count = 0, key = lysc_node_children(ctx_node, 0); key && (key->flags & LYS_KEY); key = key->next, ++key_count) {}
Michal Vasko004d3152020-06-11 19:59:22 +02006904 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006905
Michal Vasko004d3152020-06-11 19:59:22 +02006906 /* learn where the predicates end */
6907 e_idx = *tok_idx;
6908 while (key_count) {
6909 /* '[' */
6910 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6911 return LY_EINVAL;
6912 }
6913 ++e_idx;
6914
6915 /* ']' */
6916 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6917 ++e_idx;
6918 }
6919 ++e_idx;
6920
6921 /* another presumably key predicate parsed */
6922 --key_count;
6923 }
Michal Vasko004d3152020-06-11 19:59:22 +02006924 } else {
6925 /* learn just where this single predicate ends */
6926 e_idx = *tok_idx;
6927
Michal Vaskod3678892020-05-21 10:06:58 +02006928 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006929 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6930 return LY_EINVAL;
6931 }
6932 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006933
6934 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006935 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6936 ++e_idx;
6937 }
6938 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006939 }
6940
Michal Vasko004d3152020-06-11 19:59:22 +02006941 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6942 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6943
6944 /* turn logging off */
6945 prev_lo = ly_log_options(0);
6946
6947 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006948 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
6949 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02006950
6951 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006952 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
6953 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006954 LY_CHECK_GOTO(ret, cleanup);
6955
6956 /* success, the predicate must include all the needed information for hash-based search */
6957 *tok_idx = e_idx;
6958
6959cleanup:
6960 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006961 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02006962 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006963}
6964
6965/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006966 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
6967 * data node(s) could be found using a single hash-based search.
6968 *
6969 * @param[in] node Next context node to check.
6970 * @param[in] name Expected node name.
6971 * @param[in] name_len Length of @p name.
6972 * @param[in] moveto_mod Expected node module.
6973 * @param[in] root_type XPath root type.
6974 * @param[in] no_prefix Whether the node name was unprefixed.
6975 * @param[in] format Prefix format.
6976 * @param[in,out] found Previously found node, is updated.
6977 * @return LY_SUCCESS on success,
6978 * @return LY_ENOT if the whole check failed and hashes cannot be used.
6979 */
6980static LY_ERR
6981eval_name_test_with_predicate_get_scnode(const struct lyd_node *node, const char *name, uint16_t name_len,
6982 const struct lys_module *moveto_mod, enum lyxp_node_type root_type, ly_bool no_prefix, LY_PREFIX_FORMAT format,
6983 const struct lysc_node **found)
6984{
6985 const struct lysc_node *scnode;
6986 const struct lys_module *mod;
6987 uint32_t idx = 0;
6988
6989continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02006990 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006991 if (!node) {
6992 if (no_prefix && (format == LY_PREF_JSON)) {
6993 /* search all modules for a single match */
6994 while ((mod = ly_ctx_get_module_iter(moveto_mod->ctx, &idx))) {
6995 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
6996 if (scnode) {
6997 /* we have found a match */
6998 break;
6999 }
7000 }
7001
Michal Vasko7d1d0912020-10-16 08:38:30 +02007002 if (!scnode) {
7003 /* all modules searched */
7004 idx = 0;
7005 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007006 } else {
7007 /* search in top-level */
7008 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7009 }
7010 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
7011 if (no_prefix && (format == LY_PREF_JSON)) {
7012 /* we must adjust the module to inherit the one from the context node */
7013 moveto_mod = node->schema->module;
7014 }
7015
7016 /* search in children, do not repeat the same search */
7017 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007018 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007019
7020 /* additional context check */
7021 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7022 scnode = NULL;
7023 }
7024
7025 if (scnode) {
7026 if (*found) {
7027 /* we found a schema node with the same name but at different level, give up, too complicated
7028 * (more hash-based searches would be required, not supported) */
7029 return LY_ENOT;
7030 } else {
7031 /* remember the found schema node and continue to make sure it can be used */
7032 *found = scnode;
7033 }
7034 }
7035
7036 if (idx) {
7037 /* continue searching all the following models */
7038 goto continue_search;
7039 }
7040
7041 return LY_SUCCESS;
7042}
7043
7044/**
Michal Vaskod3678892020-05-21 10:06:58 +02007045 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7046 *
7047 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7048 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7049 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7050 *
7051 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007052 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007053 * @param[in] attr_axis Whether to search attributes or standard nodes.
7054 * @param[in] all_desc Whether to search all the descendants or children only.
7055 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7056 * @param[in] options XPath options.
7057 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7058 */
7059static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007060eval_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 +02007061 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007062{
Michal Vaskod3678892020-05-21 10:06:58 +02007063 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007064 const char *ncname, *ncname_dict = NULL;
7065 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007066 const struct lys_module *moveto_mod = NULL;
7067 const struct lysc_node *scnode = NULL, *ctx_scnode;
Michal Vasko004d3152020-06-11 19:59:22 +02007068 struct ly_path_predicate *predicates = NULL;
7069 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007070 LY_ERR rc = LY_SUCCESS;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007071 ly_bool no_prefix;
Michal Vaskod3678892020-05-21 10:06:58 +02007072
7073 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007074 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007075 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007076
7077 if (!set) {
7078 goto moveto;
7079 }
7080
Michal Vasko004d3152020-06-11 19:59:22 +02007081 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7082 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007083
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007084 /* get the first schema context node */
7085 if (set->used) {
7086 if (set->type == LYXP_SET_NODE_SET) {
7087 ctx_scnode = set->val.nodes[0].node ? set->val.nodes[0].node->schema : NULL;
7088 } else {
7089 ctx_scnode = set->val.scnodes[0].scnode;
7090 }
7091 } else {
7092 ctx_scnode = NULL;
7093 }
7094
7095 /* parse (and skip) module name, use any context node (if available) just so we get some moveto_mod if there
7096 * is no prefix for ::LY_PREF_JSON */
7097 rc = moveto_resolve_model(&ncname, &ncname_len, set, ctx_scnode, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007098 LY_CHECK_GOTO(rc, cleanup);
7099
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007100 if (ncname_len == exp->tok_len[*tok_idx - 1]) {
7101 /* remember that there is no prefix */
7102 no_prefix = 1;
7103 } else {
7104 no_prefix = 0;
7105 }
7106
Michal Vaskod3678892020-05-21 10:06:58 +02007107 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
7108 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007109 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007110 if (eval_name_test_with_predicate_get_scnode(set->val.nodes[i].node, ncname, ncname_len, moveto_mod,
7111 set->root_type, no_prefix, set->format, &scnode)) {
7112 /* check failed */
7113 scnode = NULL;
7114 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007115 }
7116 }
7117
Michal Vasko004d3152020-06-11 19:59:22 +02007118 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7119 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007120 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7121 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007122 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007123 scnode = NULL;
7124 }
7125 }
7126 }
7127
Michal Vasko004d3152020-06-11 19:59:22 +02007128 if (!scnode && moveto_mod) {
7129 /* we are not able to match based on a schema node and not all the modules match,
7130 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007131 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007132 }
7133
Michal Vasko97ea1632020-11-23 17:00:09 +01007134 if ((set->format == LY_PREF_JSON) && no_prefix) {
7135 /* do not set moveto_mod if there is no explicit prefix for JSON, it should be inherited from
7136 * the specific context node */
7137 moveto_mod = NULL;
7138 }
7139
Michal Vaskod3678892020-05-21 10:06:58 +02007140moveto:
7141 /* move to the attribute(s), data node(s), or schema node(s) */
7142 if (attr_axis) {
7143 if (set && (options & LYXP_SCNODE_ALL)) {
7144 set_scnode_clear_ctx(set);
7145 } else {
7146 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007147 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007148 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007149 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007150 }
7151 LY_CHECK_GOTO(rc, cleanup);
7152 }
7153 } else {
7154 if (set && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007155 int64_t i;
7156
Michal Vaskod3678892020-05-21 10:06:58 +02007157 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007158 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007159 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007160 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007161 }
7162 LY_CHECK_GOTO(rc, cleanup);
7163
7164 for (i = set->used - 1; i > -1; --i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007165 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM) {
Michal Vaskod3678892020-05-21 10:06:58 +02007166 break;
7167 }
7168 }
7169 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007170 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vasko90f12dc2020-12-03 14:20:42 +01007171 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".",
7172 ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007173 free(path);
7174 }
7175 } else {
7176 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007177 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007178 } else {
7179 if (scnode) {
7180 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007181 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007182 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007183 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007184 }
7185 }
7186 LY_CHECK_GOTO(rc, cleanup);
7187 }
7188 }
7189
7190 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007191 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7192 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007193 LY_CHECK_RET(rc);
7194 }
7195
7196cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007197 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007198 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007199 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007200 }
Michal Vaskod3678892020-05-21 10:06:58 +02007201 return rc;
7202}
7203
7204/**
7205 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7206 *
7207 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7208 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7209 * [8] NodeType ::= 'text' | 'node'
7210 *
7211 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007212 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007213 * @param[in] attr_axis Whether to search attributes or standard nodes.
7214 * @param[in] all_desc Whether to search all the descendants or children only.
7215 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7216 * @param[in] options XPath options.
7217 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7218 */
7219static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007220eval_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 +02007221 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007222{
7223 LY_ERR rc;
7224
7225 /* TODO */
7226 (void)attr_axis;
7227 (void)all_desc;
7228
7229 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007230 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007231 if (set->type == LYXP_SET_SCNODE_SET) {
7232 set_scnode_clear_ctx(set);
7233 /* just for the debug message below */
7234 set = NULL;
7235 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007236 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007237 rc = xpath_node(NULL, 0, set, options);
7238 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007239 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007240 rc = xpath_text(NULL, 0, set, options);
7241 }
7242 LY_CHECK_RET(rc);
7243 }
7244 }
7245 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007246 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007247 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007248
7249 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007250 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007251 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007252 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007253 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007254
7255 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007256 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007257 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007258 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007259 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007260
7261 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007262 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7263 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007264 LY_CHECK_RET(rc);
7265 }
7266
7267 return LY_SUCCESS;
7268}
7269
7270/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007271 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7272 *
7273 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7274 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007275 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007276 *
7277 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007278 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007279 * @param[in] all_desc Whether to search all the descendants or children only.
7280 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7281 * @param[in] options XPath options.
7282 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7283 */
7284static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007285eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7286 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007287{
Radek Krejci857189e2020-09-01 13:26:36 +02007288 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007289 LY_ERR rc;
7290
7291 goto step;
7292 do {
7293 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007294 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007295 all_desc = 0;
7296 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007297 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007298 all_desc = 1;
7299 }
7300 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007301 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007302 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007303
7304step:
Michal Vaskod3678892020-05-21 10:06:58 +02007305 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007306 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007307 attr_axis = 1;
7308
7309 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007310 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007311 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007312 } else {
7313 attr_axis = 0;
7314 }
7315
Michal Vasko03ff5a72019-09-11 13:49:33 +02007316 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007317 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007318 case LYXP_TOKEN_DOT:
7319 /* evaluate '.' */
7320 if (set && (options & LYXP_SCNODE_ALL)) {
7321 rc = moveto_scnode_self(set, all_desc, options);
7322 } else {
7323 rc = moveto_self(set, all_desc, options);
7324 }
7325 LY_CHECK_RET(rc);
7326 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007327 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007328 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007329 break;
7330
7331 case LYXP_TOKEN_DDOT:
7332 /* evaluate '..' */
7333 if (set && (options & LYXP_SCNODE_ALL)) {
7334 rc = moveto_scnode_parent(set, all_desc, options);
7335 } else {
7336 rc = moveto_parent(set, all_desc, options);
7337 }
7338 LY_CHECK_RET(rc);
7339 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007340 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007341 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007342 break;
7343
Michal Vasko03ff5a72019-09-11 13:49:33 +02007344 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007345 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007346 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007347 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007348 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007349
Michal Vaskod3678892020-05-21 10:06:58 +02007350 case LYXP_TOKEN_NODETYPE:
7351 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007352 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007353 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007354 break;
7355
7356 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007357 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007358 }
Michal Vasko004d3152020-06-11 19:59:22 +02007359 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007360
7361 return LY_SUCCESS;
7362}
7363
7364/**
7365 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7366 *
7367 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7368 *
7369 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007370 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007371 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7372 * @param[in] options XPath options.
7373 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7374 */
7375static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007376eval_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 +02007377{
Radek Krejci857189e2020-09-01 13:26:36 +02007378 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007379
7380 if (set) {
7381 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007382 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007383 }
7384
7385 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007386 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007387 /* evaluate '/' - deferred */
7388 all_desc = 0;
7389 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007390 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007391 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007392
Michal Vasko004d3152020-06-11 19:59:22 +02007393 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007394 return LY_SUCCESS;
7395 }
Michal Vasko004d3152020-06-11 19:59:22 +02007396 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007397 case LYXP_TOKEN_DOT:
7398 case LYXP_TOKEN_DDOT:
7399 case LYXP_TOKEN_AT:
7400 case LYXP_TOKEN_NAMETEST:
7401 case LYXP_TOKEN_NODETYPE:
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 break;
7404 default:
7405 break;
7406 }
7407
Michal Vasko03ff5a72019-09-11 13:49:33 +02007408 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007409 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007410 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7411 all_desc = 1;
7412 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007413 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007414 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007415
Michal Vaskob0099a92020-08-31 14:55:23 +02007416 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007417 }
7418
7419 return LY_SUCCESS;
7420}
7421
7422/**
7423 * @brief Evaluate FunctionCall. Logs directly on error.
7424 *
Michal Vaskod3678892020-05-21 10:06:58 +02007425 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007426 *
7427 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007428 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007429 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7430 * @param[in] options XPath options.
7431 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7432 */
7433static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007434eval_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 +02007435{
7436 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007437
Radek Krejci1deb5be2020-08-26 16:43:36 +02007438 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007439 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007440 struct lyxp_set **args = NULL, **args_aux;
7441
7442 if (set) {
7443 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007444 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007445 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007446 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007447 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007448 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007449 xpath_func = &xpath_sum;
7450 }
7451 break;
7452 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007453 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007454 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007455 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007456 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007457 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007458 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007459 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007460 xpath_func = &xpath_true;
7461 }
7462 break;
7463 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007464 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007465 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007466 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007467 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007468 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007469 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007470 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007471 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007472 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007473 xpath_func = &xpath_deref;
7474 }
7475 break;
7476 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007477 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007478 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007479 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007480 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007481 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007482 xpath_func = &xpath_string;
7483 }
7484 break;
7485 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007486 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007487 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007488 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007489 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007490 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007491 xpath_func = &xpath_current;
7492 }
7493 break;
7494 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007495 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007496 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007497 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007498 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007499 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007500 xpath_func = &xpath_re_match;
7501 }
7502 break;
7503 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007504 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007505 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007506 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007507 xpath_func = &xpath_translate;
7508 }
7509 break;
7510 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007511 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007512 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007513 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007514 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007515 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007516 xpath_func = &xpath_bit_is_set;
7517 }
7518 break;
7519 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007520 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007521 xpath_func = &xpath_starts_with;
7522 }
7523 break;
7524 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007525 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007526 xpath_func = &xpath_derived_from;
7527 }
7528 break;
7529 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007530 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007531 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007532 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007533 xpath_func = &xpath_string_length;
7534 }
7535 break;
7536 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007537 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007538 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007539 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007540 xpath_func = &xpath_substring_after;
7541 }
7542 break;
7543 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007544 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545 xpath_func = &xpath_substring_before;
7546 }
7547 break;
7548 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007549 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007550 xpath_func = &xpath_derived_from_or_self;
7551 }
7552 break;
7553 }
7554
7555 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007556 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 +02007557 return LY_EVALID;
7558 }
7559 }
7560
7561 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007562 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007563 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007564
7565 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007566 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007567 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007568 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007569 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570
7571 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007572 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007573 if (set) {
7574 args = malloc(sizeof *args);
7575 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7576 arg_count = 1;
7577 args[0] = set_copy(set);
7578 if (!args[0]) {
7579 rc = LY_EMEM;
7580 goto cleanup;
7581 }
7582
Michal Vasko004d3152020-06-11 19:59:22 +02007583 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007584 LY_CHECK_GOTO(rc, cleanup);
7585 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007586 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007587 LY_CHECK_GOTO(rc, cleanup);
7588 }
7589 }
Michal Vasko004d3152020-06-11 19:59:22 +02007590 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007591 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007592 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007593 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007594
7595 if (set) {
7596 ++arg_count;
7597 args_aux = realloc(args, arg_count * sizeof *args);
7598 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7599 args = args_aux;
7600 args[arg_count - 1] = set_copy(set);
7601 if (!args[arg_count - 1]) {
7602 rc = LY_EMEM;
7603 goto cleanup;
7604 }
7605
Michal Vasko004d3152020-06-11 19:59:22 +02007606 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007607 LY_CHECK_GOTO(rc, cleanup);
7608 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007609 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007610 LY_CHECK_GOTO(rc, cleanup);
7611 }
7612 }
7613
7614 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007615 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007616 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007617 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007618 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007619
7620 if (set) {
7621 /* evaluate function */
7622 rc = xpath_func(args, arg_count, set, options);
7623
7624 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007625 /* merge all nodes from arg evaluations */
7626 for (i = 0; i < arg_count; ++i) {
7627 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007628 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007629 }
7630 }
7631 } else {
7632 rc = LY_SUCCESS;
7633 }
7634
7635cleanup:
7636 for (i = 0; i < arg_count; ++i) {
7637 lyxp_set_free(args[i]);
7638 }
7639 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007640 return rc;
7641}
7642
7643/**
7644 * @brief Evaluate Number. Logs directly on error.
7645 *
7646 * @param[in] ctx Context for errors.
7647 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007648 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007649 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7650 * @return LY_ERR
7651 */
7652static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007653eval_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 +02007654{
7655 long double num;
7656 char *endptr;
7657
7658 if (set) {
7659 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007660 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007661 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007662 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7663 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007664 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007665 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007666 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007667 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7668 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007669 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007670 return LY_EVALID;
7671 }
7672
7673 set_fill_number(set, num);
7674 }
7675
7676 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007677 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007678 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007679 return LY_SUCCESS;
7680}
7681
7682/**
7683 * @brief Evaluate PathExpr. Logs directly on error.
7684 *
Michal Vaskod3678892020-05-21 10:06:58 +02007685 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007686 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7687 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7688 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007689 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007690 *
7691 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007692 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007693 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7694 * @param[in] options XPath options.
7695 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7696 */
7697static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007698eval_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 +02007699{
Radek Krejci857189e2020-09-01 13:26:36 +02007700 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007701 LY_ERR rc;
7702
Michal Vasko004d3152020-06-11 19:59:22 +02007703 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007704 case LYXP_TOKEN_PAR1:
7705 /* '(' Expr ')' */
7706
7707 /* '(' */
7708 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007709 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007710 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007711
7712 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007713 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007714 LY_CHECK_RET(rc);
7715
7716 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007717 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007718 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007719 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007720 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007721
7722 parent_pos_pred = 0;
7723 goto predicate;
7724
7725 case LYXP_TOKEN_DOT:
7726 case LYXP_TOKEN_DDOT:
7727 case LYXP_TOKEN_AT:
7728 case LYXP_TOKEN_NAMETEST:
7729 case LYXP_TOKEN_NODETYPE:
7730 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007731 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007732 LY_CHECK_RET(rc);
7733 break;
7734
7735 case LYXP_TOKEN_FUNCNAME:
7736 /* FunctionCall */
7737 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007738 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007739 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007740 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007741 }
7742 LY_CHECK_RET(rc);
7743
7744 parent_pos_pred = 1;
7745 goto predicate;
7746
Michal Vasko3e48bf32020-06-01 08:39:07 +02007747 case LYXP_TOKEN_OPER_PATH:
7748 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007749 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007750 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007751 LY_CHECK_RET(rc);
7752 break;
7753
7754 case LYXP_TOKEN_LITERAL:
7755 /* Literal */
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 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007761 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007762 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007763 }
7764
7765 parent_pos_pred = 1;
7766 goto predicate;
7767
7768 case LYXP_TOKEN_NUMBER:
7769 /* Number */
7770 if (!set || (options & LYXP_SCNODE_ALL)) {
7771 if (set) {
7772 set_scnode_clear_ctx(set);
7773 }
Michal Vasko004d3152020-06-11 19:59:22 +02007774 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007775 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007776 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007777 }
7778 LY_CHECK_RET(rc);
7779
7780 parent_pos_pred = 1;
7781 goto predicate;
7782
7783 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01007784 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 +02007785 return LY_EVALID;
7786 }
7787
7788 return LY_SUCCESS;
7789
7790predicate:
7791 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007792 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7793 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007794 LY_CHECK_RET(rc);
7795 }
7796
7797 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007798 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007799
7800 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007801 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007802 all_desc = 0;
7803 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007804 all_desc = 1;
7805 }
7806
7807 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007808 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007809 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007810
Michal Vasko004d3152020-06-11 19:59:22 +02007811 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007812 LY_CHECK_RET(rc);
7813 }
7814
7815 return LY_SUCCESS;
7816}
7817
7818/**
7819 * @brief Evaluate UnionExpr. Logs directly on error.
7820 *
Michal Vaskod3678892020-05-21 10:06:58 +02007821 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007822 *
7823 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007824 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007825 * @param[in] repeat How many times this expression is repeated.
7826 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7827 * @param[in] options XPath options.
7828 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7829 */
7830static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007831eval_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 +02007832{
7833 LY_ERR rc = LY_SUCCESS;
7834 struct lyxp_set orig_set, set2;
7835 uint16_t i;
7836
7837 assert(repeat);
7838
7839 set_init(&orig_set, set);
7840 set_init(&set2, set);
7841
7842 set_fill_set(&orig_set, set);
7843
Michal Vasko004d3152020-06-11 19:59:22 +02007844 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007845 LY_CHECK_GOTO(rc, cleanup);
7846
7847 /* ('|' PathExpr)* */
7848 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007849 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007850 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007851 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007852 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007853
7854 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007855 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007856 LY_CHECK_GOTO(rc, cleanup);
7857 continue;
7858 }
7859
7860 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007861 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007862 LY_CHECK_GOTO(rc, cleanup);
7863
7864 /* eval */
7865 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007866 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007867 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007868 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007869 LY_CHECK_GOTO(rc, cleanup);
7870 }
7871 }
7872
7873cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007874 lyxp_set_free_content(&orig_set);
7875 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007876 return rc;
7877}
7878
7879/**
7880 * @brief Evaluate UnaryExpr. Logs directly on error.
7881 *
Michal Vaskod3678892020-05-21 10:06:58 +02007882 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007883 *
7884 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007885 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007886 * @param[in] repeat How many times this expression is repeated.
7887 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7888 * @param[in] options XPath options.
7889 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7890 */
7891static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007892eval_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 +02007893{
7894 LY_ERR rc;
7895 uint16_t this_op, i;
7896
7897 assert(repeat);
7898
7899 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007900 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007901 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007902 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 +02007903
7904 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007905 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007906 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007907 }
7908
Michal Vasko004d3152020-06-11 19:59:22 +02007909 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007910 LY_CHECK_RET(rc);
7911
7912 if (set && (repeat % 2)) {
7913 if (options & LYXP_SCNODE_ALL) {
7914 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7915 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007916 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007917 LY_CHECK_RET(rc);
7918 }
7919 }
7920
7921 return LY_SUCCESS;
7922}
7923
7924/**
7925 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7926 *
Michal Vaskod3678892020-05-21 10:06:58 +02007927 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007928 * | MultiplicativeExpr '*' UnaryExpr
7929 * | MultiplicativeExpr 'div' UnaryExpr
7930 * | MultiplicativeExpr 'mod' UnaryExpr
7931 *
7932 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007933 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007934 * @param[in] repeat How many times this expression is repeated.
7935 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7936 * @param[in] options XPath options.
7937 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7938 */
7939static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007940eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
7941 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007942{
7943 LY_ERR rc;
7944 uint16_t this_op;
7945 struct lyxp_set orig_set, set2;
7946 uint16_t i;
7947
7948 assert(repeat);
7949
7950 set_init(&orig_set, set);
7951 set_init(&set2, set);
7952
7953 set_fill_set(&orig_set, set);
7954
Michal Vasko004d3152020-06-11 19:59:22 +02007955 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007956 LY_CHECK_GOTO(rc, cleanup);
7957
7958 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7959 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007960 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007961
Michal Vasko004d3152020-06-11 19:59:22 +02007962 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007963 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007964 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007965 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007966
7967 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007968 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007969 LY_CHECK_GOTO(rc, cleanup);
7970 continue;
7971 }
7972
7973 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007974 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007975 LY_CHECK_GOTO(rc, cleanup);
7976
7977 /* eval */
7978 if (options & LYXP_SCNODE_ALL) {
7979 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007980 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007981 set_scnode_clear_ctx(set);
7982 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007983 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007984 LY_CHECK_GOTO(rc, cleanup);
7985 }
7986 }
7987
7988cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007989 lyxp_set_free_content(&orig_set);
7990 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007991 return rc;
7992}
7993
7994/**
7995 * @brief Evaluate AdditiveExpr. Logs directly on error.
7996 *
Michal Vaskod3678892020-05-21 10:06:58 +02007997 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007998 * | AdditiveExpr '+' MultiplicativeExpr
7999 * | AdditiveExpr '-' MultiplicativeExpr
8000 *
8001 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008002 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008003 * @param[in] repeat How many times this expression is repeated.
8004 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8005 * @param[in] options XPath options.
8006 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8007 */
8008static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008009eval_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 +02008010{
8011 LY_ERR rc;
8012 uint16_t this_op;
8013 struct lyxp_set orig_set, set2;
8014 uint16_t i;
8015
8016 assert(repeat);
8017
8018 set_init(&orig_set, set);
8019 set_init(&set2, set);
8020
8021 set_fill_set(&orig_set, set);
8022
Michal Vasko004d3152020-06-11 19:59:22 +02008023 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008024 LY_CHECK_GOTO(rc, cleanup);
8025
8026 /* ('+' / '-' MultiplicativeExpr)* */
8027 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008028 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008029
Michal Vasko004d3152020-06-11 19:59:22 +02008030 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008031 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008032 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008033 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008034
8035 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008036 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008037 LY_CHECK_GOTO(rc, cleanup);
8038 continue;
8039 }
8040
8041 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008042 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008043 LY_CHECK_GOTO(rc, cleanup);
8044
8045 /* eval */
8046 if (options & LYXP_SCNODE_ALL) {
8047 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008048 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008049 set_scnode_clear_ctx(set);
8050 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008051 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008052 LY_CHECK_GOTO(rc, cleanup);
8053 }
8054 }
8055
8056cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008057 lyxp_set_free_content(&orig_set);
8058 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008059 return rc;
8060}
8061
8062/**
8063 * @brief Evaluate RelationalExpr. Logs directly on error.
8064 *
Michal Vaskod3678892020-05-21 10:06:58 +02008065 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008066 * | RelationalExpr '<' AdditiveExpr
8067 * | RelationalExpr '>' AdditiveExpr
8068 * | RelationalExpr '<=' AdditiveExpr
8069 * | RelationalExpr '>=' AdditiveExpr
8070 *
8071 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008072 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008073 * @param[in] repeat How many times this expression is repeated.
8074 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8075 * @param[in] options XPath options.
8076 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8077 */
8078static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008079eval_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 +02008080{
8081 LY_ERR rc;
8082 uint16_t this_op;
8083 struct lyxp_set orig_set, set2;
8084 uint16_t i;
8085
8086 assert(repeat);
8087
8088 set_init(&orig_set, set);
8089 set_init(&set2, set);
8090
8091 set_fill_set(&orig_set, set);
8092
Michal Vasko004d3152020-06-11 19:59:22 +02008093 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008094 LY_CHECK_GOTO(rc, cleanup);
8095
8096 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8097 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008098 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008099
Michal Vasko004d3152020-06-11 19:59:22 +02008100 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008101 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008102 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008103 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008104
8105 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008106 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008107 LY_CHECK_GOTO(rc, cleanup);
8108 continue;
8109 }
8110
8111 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008112 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008113 LY_CHECK_GOTO(rc, cleanup);
8114
8115 /* eval */
8116 if (options & LYXP_SCNODE_ALL) {
8117 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008118 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008119 set_scnode_clear_ctx(set);
8120 } else {
8121 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8122 LY_CHECK_GOTO(rc, cleanup);
8123 }
8124 }
8125
8126cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008127 lyxp_set_free_content(&orig_set);
8128 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008129 return rc;
8130}
8131
8132/**
8133 * @brief Evaluate EqualityExpr. Logs directly on error.
8134 *
Michal Vaskod3678892020-05-21 10:06:58 +02008135 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008136 * | EqualityExpr '!=' RelationalExpr
8137 *
8138 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008139 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008140 * @param[in] repeat How many times this expression is repeated.
8141 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8142 * @param[in] options XPath options.
8143 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8144 */
8145static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008146eval_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 +02008147{
8148 LY_ERR rc;
8149 uint16_t this_op;
8150 struct lyxp_set orig_set, set2;
8151 uint16_t i;
8152
8153 assert(repeat);
8154
8155 set_init(&orig_set, set);
8156 set_init(&set2, set);
8157
8158 set_fill_set(&orig_set, set);
8159
Michal Vasko004d3152020-06-11 19:59:22 +02008160 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008161 LY_CHECK_GOTO(rc, cleanup);
8162
8163 /* ('=' / '!=' RelationalExpr)* */
8164 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008165 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008166
Michal Vasko004d3152020-06-11 19:59:22 +02008167 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008168 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008169 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008170 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008171
8172 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008173 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008174 LY_CHECK_GOTO(rc, cleanup);
8175 continue;
8176 }
8177
8178 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008179 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008180 LY_CHECK_GOTO(rc, cleanup);
8181
8182 /* eval */
8183 if (options & LYXP_SCNODE_ALL) {
8184 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008185 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8186 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008187 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008188 set_scnode_clear_ctx(set);
8189 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008190 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8191 LY_CHECK_GOTO(rc, cleanup);
8192 }
8193 }
8194
8195cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008196 lyxp_set_free_content(&orig_set);
8197 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008198 return rc;
8199}
8200
8201/**
8202 * @brief Evaluate AndExpr. Logs directly on error.
8203 *
Michal Vaskod3678892020-05-21 10:06:58 +02008204 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008205 *
8206 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008207 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008208 * @param[in] repeat How many times this expression is repeated.
8209 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8210 * @param[in] options XPath options.
8211 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8212 */
8213static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008214eval_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 +02008215{
8216 LY_ERR rc;
8217 struct lyxp_set orig_set, set2;
8218 uint16_t i;
8219
8220 assert(repeat);
8221
8222 set_init(&orig_set, set);
8223 set_init(&set2, set);
8224
8225 set_fill_set(&orig_set, set);
8226
Michal Vasko004d3152020-06-11 19:59:22 +02008227 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008228 LY_CHECK_GOTO(rc, cleanup);
8229
8230 /* cast to boolean, we know that will be the final result */
8231 if (set && (options & LYXP_SCNODE_ALL)) {
8232 set_scnode_clear_ctx(set);
8233 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008234 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008235 }
8236
8237 /* ('and' EqualityExpr)* */
8238 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008239 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8240 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008241 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008242 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008243
8244 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008245 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8246 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008247 LY_CHECK_GOTO(rc, cleanup);
8248 continue;
8249 }
8250
8251 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008252 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008253 LY_CHECK_GOTO(rc, cleanup);
8254
8255 /* eval - just get boolean value actually */
8256 if (set->type == LYXP_SET_SCNODE_SET) {
8257 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008258 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008259 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008260 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008261 set_fill_set(set, &set2);
8262 }
8263 }
8264
8265cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008266 lyxp_set_free_content(&orig_set);
8267 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008268 return rc;
8269}
8270
8271/**
8272 * @brief Evaluate OrExpr. Logs directly on error.
8273 *
Michal Vaskod3678892020-05-21 10:06:58 +02008274 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008275 *
8276 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008277 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008278 * @param[in] repeat How many times this expression is repeated.
8279 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8280 * @param[in] options XPath options.
8281 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8282 */
8283static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008284eval_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 +02008285{
8286 LY_ERR rc;
8287 struct lyxp_set orig_set, set2;
8288 uint16_t i;
8289
8290 assert(repeat);
8291
8292 set_init(&orig_set, set);
8293 set_init(&set2, set);
8294
8295 set_fill_set(&orig_set, set);
8296
Michal Vasko004d3152020-06-11 19:59:22 +02008297 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008298 LY_CHECK_GOTO(rc, cleanup);
8299
8300 /* cast to boolean, we know that will be the final result */
8301 if (set && (options & LYXP_SCNODE_ALL)) {
8302 set_scnode_clear_ctx(set);
8303 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008304 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008305 }
8306
8307 /* ('or' AndExpr)* */
8308 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008309 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8310 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008311 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008312 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008313
8314 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008315 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8316 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008317 LY_CHECK_GOTO(rc, cleanup);
8318 continue;
8319 }
8320
8321 set_fill_set(&set2, &orig_set);
8322 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8323 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008324 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008325 LY_CHECK_GOTO(rc, cleanup);
8326
8327 /* eval - just get boolean value actually */
8328 if (set->type == LYXP_SET_SCNODE_SET) {
8329 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008330 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008331 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008332 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008333 set_fill_set(set, &set2);
8334 }
8335 }
8336
8337cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008338 lyxp_set_free_content(&orig_set);
8339 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008340 return rc;
8341}
8342
8343/**
Michal Vasko004d3152020-06-11 19:59:22 +02008344 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008345 *
8346 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008347 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008348 * @param[in] etype Expression type to evaluate.
8349 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8350 * @param[in] options XPath options.
8351 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8352 */
8353static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008354eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8355 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008356{
8357 uint16_t i, count;
8358 enum lyxp_expr_type next_etype;
8359 LY_ERR rc;
8360
8361 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008362 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008363 next_etype = LYXP_EXPR_NONE;
8364 } else {
8365 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008366 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008367
8368 /* select one-priority lower because etype expression called us */
8369 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008370 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008371 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008372 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008373 } else {
8374 next_etype = LYXP_EXPR_NONE;
8375 }
8376 }
8377
8378 /* decide what expression are we parsing based on the repeat */
8379 switch (next_etype) {
8380 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008381 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008382 break;
8383 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008384 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008385 break;
8386 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008387 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008388 break;
8389 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008390 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008391 break;
8392 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008393 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008394 break;
8395 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008396 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008397 break;
8398 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008399 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008400 break;
8401 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008402 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008403 break;
8404 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008405 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008406 break;
8407 default:
8408 LOGINT_RET(set->ctx);
8409 }
8410
8411 return rc;
8412}
8413
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008414/**
8415 * @brief Get root type.
8416 *
8417 * @param[in] ctx_node Context node.
8418 * @param[in] ctx_scnode Schema context node.
8419 * @param[in] options XPath options.
8420 * @return Root type.
8421 */
8422static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008423lyxp_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 +01008424{
Michal Vasko6b26e742020-07-17 15:02:10 +02008425 const struct lysc_node *op;
8426
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008427 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008428 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008429 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008430
8431 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008432 /* general root that can access everything */
8433 return LYXP_NODE_ROOT;
8434 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8435 /* root context node can access only config data (because we said so, it is unspecified) */
8436 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008437 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008438 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008439 }
8440
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008441 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008442 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008443 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008444
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008445 if (op || !(options & LYXP_SCHEMA)) {
8446 /* general root that can access everything */
8447 return LYXP_NODE_ROOT;
8448 } else if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008449 /* root context node can access only config data (because we said so, it is unspecified) */
8450 return LYXP_NODE_ROOT_CONFIG;
8451 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008452 return LYXP_NODE_ROOT;
8453}
8454
Michal Vasko03ff5a72019-09-11 13:49:33 +02008455LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008456lyxp_eval(const struct lyxp_expr *exp, const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008457 const struct lyd_node *ctx_node, const struct lyd_node *tree, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008458{
Michal Vasko004d3152020-06-11 19:59:22 +02008459 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008460 LY_ERR rc;
8461
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008462 LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL);
8463 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8464 LOGARG(NULL, "Current module must be set if schema format is used.");
8465 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008466 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008467
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008468 if (tree) {
8469 /* adjust the pointer to be the first top-level sibling */
8470 while (tree->parent) {
8471 tree = lyd_parent(tree);
8472 }
8473 tree = lyd_first_sibling(tree);
8474 }
8475
Michal Vasko03ff5a72019-09-11 13:49:33 +02008476 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008477 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008478 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008479 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8480 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8481
8482 set->ctx = (struct ly_ctx *)(cur_mod ? cur_mod->ctx : (ctx_node ? LYD_CTX(ctx_node) : (tree ? LYD_CTX(tree) : NULL)));
8483 set->cur_node = ctx_node;
8484 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008485 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8486 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008487 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008488 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008489 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008490 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008491
Radek Krejci2efc45b2020-12-22 16:25:44 +01008492 LOG_LOCSET(set->ctx, NULL, set->cur_node, NULL, NULL);
8493
Michal Vasko03ff5a72019-09-11 13:49:33 +02008494 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008495 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008496 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008497 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008498 }
8499
Radek Krejci2efc45b2020-12-22 16:25:44 +01008500 LOG_LOCBACK(set->ctx, 0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008501 return rc;
8502}
8503
8504#if 0
8505
8506/* full xml printing of set elements, not used currently */
8507
8508void
8509lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8510{
8511 uint32_t i;
8512 char *str_num;
8513 struct lyout out;
8514
8515 memset(&out, 0, sizeof out);
8516
8517 out.type = LYOUT_STREAM;
8518 out.method.f = f;
8519
8520 switch (set->type) {
8521 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008522 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008523 break;
8524 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008525 ly_print_(&out, "Boolean XPath set:\n");
8526 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008527 break;
8528 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008529 ly_print_(&out, "String XPath set:\n");
8530 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008531 break;
8532 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008533 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008534
8535 if (isnan(set->value.num)) {
8536 str_num = strdup("NaN");
8537 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8538 str_num = strdup("0");
8539 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8540 str_num = strdup("Infinity");
8541 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8542 str_num = strdup("-Infinity");
8543 } else if ((long long)set->value.num == set->value.num) {
8544 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8545 str_num = NULL;
8546 }
8547 } else {
8548 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8549 str_num = NULL;
8550 }
8551 }
8552 if (!str_num) {
8553 LOGMEM;
8554 return;
8555 }
Michal Vasko5233e962020-08-14 14:26:20 +02008556 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008557 free(str_num);
8558 break;
8559 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008560 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008561
8562 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008563 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008564 switch (set->node_type[i]) {
8565 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008566 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008567 break;
8568 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008569 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008570 break;
8571 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008572 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008573 break;
8574 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008575 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008576 break;
8577 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008578 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008579 break;
8580 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008581 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008582 break;
8583 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008584 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008585 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008586 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008587 break;
8588 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008589 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 +02008590 break;
8591 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008592 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 +02008593 break;
8594 }
8595 }
8596 break;
8597 }
8598}
8599
8600#endif
8601
8602LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008603lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008604{
8605 long double num;
8606 char *str;
8607 LY_ERR rc;
8608
8609 if (!set || (set->type == target)) {
8610 return LY_SUCCESS;
8611 }
8612
8613 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008614 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008615
8616 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008617 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008618 return LY_EINVAL;
8619 }
8620
8621 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008622 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008623 switch (set->type) {
8624 case LYXP_SET_NUMBER:
8625 if (isnan(set->val.num)) {
8626 set->val.str = strdup("NaN");
8627 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8628 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8629 set->val.str = strdup("0");
8630 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8631 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8632 set->val.str = strdup("Infinity");
8633 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8634 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8635 set->val.str = strdup("-Infinity");
8636 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8637 } else if ((long long)set->val.num == set->val.num) {
8638 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8639 LOGMEM_RET(set->ctx);
8640 }
8641 set->val.str = str;
8642 } else {
8643 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8644 LOGMEM_RET(set->ctx);
8645 }
8646 set->val.str = str;
8647 }
8648 break;
8649 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008650 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008651 set->val.str = strdup("true");
8652 } else {
8653 set->val.str = strdup("false");
8654 }
8655 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8656 break;
8657 case LYXP_SET_NODE_SET:
8658 assert(set->used);
8659
8660 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008661 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008662
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008663 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008664 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008665 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008666 set->val.str = str;
8667 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008668 default:
8669 LOGINT_RET(set->ctx);
8670 }
8671 set->type = LYXP_SET_STRING;
8672 }
8673
8674 /* to NUMBER */
8675 if (target == LYXP_SET_NUMBER) {
8676 switch (set->type) {
8677 case LYXP_SET_STRING:
8678 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008679 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008680 set->val.num = num;
8681 break;
8682 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008683 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008684 set->val.num = 1;
8685 } else {
8686 set->val.num = 0;
8687 }
8688 break;
8689 default:
8690 LOGINT_RET(set->ctx);
8691 }
8692 set->type = LYXP_SET_NUMBER;
8693 }
8694
8695 /* to BOOLEAN */
8696 if (target == LYXP_SET_BOOLEAN) {
8697 switch (set->type) {
8698 case LYXP_SET_NUMBER:
8699 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008700 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008701 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008702 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008703 }
8704 break;
8705 case LYXP_SET_STRING:
8706 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008707 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008708 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008709 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008710 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008711 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008712 }
8713 break;
8714 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008715 if (set->used) {
8716 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008717 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008718 } else {
8719 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008720 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008721 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008722 break;
8723 default:
8724 LOGINT_RET(set->ctx);
8725 }
8726 set->type = LYXP_SET_BOOLEAN;
8727 }
8728
Michal Vasko03ff5a72019-09-11 13:49:33 +02008729 return LY_SUCCESS;
8730}
8731
8732LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008733lyxp_atomize(const struct lyxp_expr *exp, const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008734 const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008735{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008736 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008737 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008738
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008739 LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL);
8740 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8741 LOGARG(NULL, "Current module must be set if schema format is used.");
8742 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008743 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008744
8745 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008746 memset(set, 0, sizeof *set);
8747 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008748 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8749 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 +01008750 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008751
8752 set->ctx = cur_mod ? cur_mod->ctx : (ctx_scnode ? ctx_scnode->module->ctx : NULL);
8753 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008754 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008755 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8756 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008757 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008758 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008759 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008760
Radek Krejci2efc45b2020-12-22 16:25:44 +01008761 LOG_LOCSET(set->ctx, set->cur_scnode, NULL, NULL, NULL);
8762
Michal Vasko03ff5a72019-09-11 13:49:33 +02008763 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01008764 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
8765
8766 LOG_LOCBACK(set->ctx, 1, 0, 0, 0);
8767 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008768}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008769
8770API const char *
8771lyxp_get_expr(const struct lyxp_expr *path)
8772{
8773 if (!path) {
8774 return NULL;
8775 }
8776
8777 return path->expr;
8778}