blob: 10d0cdf8b0c004c85199b8e663e60cbd49ef268f [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 Vasko03ff5a72019-09-11 13:49:33 +0200138 uint16_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;
772 uint16_t i;
773
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 Vaskoed4fcfe2020-07-08 10:38:56 +02001000 uint16_t i, orig_used, end = 0;
Michal Vasko57eab132019-09-24 11:46:26 +02001001 int32_t start;
1002
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) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001849 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1850 }
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) {
Michal Vasko0b468e62020-10-19 09:33:04 +02001856 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
1857 &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) {
1883 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1884 }
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) {
Michal Vasko0b468e62020-10-19 09:33:04 +02001890 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
1891 &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)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001997 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko69730152020-10-09 16:30:07 +02001998 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001999 return LY_EVALID;
2000 }
Radek Krejci0f969882020-08-21 16:56:47 +02002001 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002002 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002003 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002004 goto reparse_predicate;
2005 break;
2006
2007 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002008 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002009
2010 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002011 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002012 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002013 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002014
2015 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002016 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002017 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002018 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002019
2020reparse_predicate:
2021 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002022 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2023 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002024 LY_CHECK_RET(rc);
2025 }
2026 break;
2027 default:
2028 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko69730152020-10-09 16:30:07 +02002029 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002030 return LY_EVALID;
2031 }
Michal Vasko004d3152020-06-11 19:59:22 +02002032 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002033
2034 return LY_SUCCESS;
2035}
2036
2037/**
2038 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2039 *
2040 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2041 *
2042 * @param[in] ctx Context for logging.
2043 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002044 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002045 * @return LY_ERR
2046 */
2047static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002048reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002049{
2050 LY_ERR rc;
2051
Michal Vasko004d3152020-06-11 19:59:22 +02002052 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 +02002053
2054 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002055 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002056 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002057 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002058
Michal Vasko004d3152020-06-11 19:59:22 +02002059 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002060 return LY_SUCCESS;
2061 }
Michal Vasko004d3152020-06-11 19:59:22 +02002062 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002063 case LYXP_TOKEN_DOT:
2064 case LYXP_TOKEN_DDOT:
2065 case LYXP_TOKEN_AT:
2066 case LYXP_TOKEN_NAMETEST:
2067 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002068 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002069 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002070 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002071 default:
2072 break;
2073 }
2074
Michal Vasko03ff5a72019-09-11 13:49:33 +02002075 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002076 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002077 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002078
Michal Vasko004d3152020-06-11 19:59:22 +02002079 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002080 LY_CHECK_RET(rc);
2081 }
2082
2083 return LY_SUCCESS;
2084}
2085
2086/**
2087 * @brief Reparse FunctionCall. Logs directly on error.
2088 *
2089 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2090 *
2091 * @param[in] ctx Context for logging.
2092 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002093 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002094 * @return LY_ERR
2095 */
2096static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002097reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002098{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002099 int8_t min_arg_count = -1;
2100 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002101 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002102 LY_ERR rc;
2103
Michal Vasko004d3152020-06-11 19:59:22 +02002104 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002105 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002106 func_tok_idx = *tok_idx;
2107 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002108 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002109 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002110 min_arg_count = 1;
2111 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002112 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002113 min_arg_count = 1;
2114 max_arg_count = 1;
2115 }
2116 break;
2117 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002118 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002119 min_arg_count = 1;
2120 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002121 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002122 min_arg_count = 0;
2123 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002124 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002125 min_arg_count = 0;
2126 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002127 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002128 min_arg_count = 0;
2129 max_arg_count = 0;
2130 }
2131 break;
2132 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002133 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002134 min_arg_count = 1;
2135 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002136 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002137 min_arg_count = 0;
2138 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002139 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002140 min_arg_count = 1;
2141 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002142 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002143 min_arg_count = 1;
2144 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002145 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002146 min_arg_count = 1;
2147 max_arg_count = 1;
2148 }
2149 break;
2150 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002151 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002152 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002153 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002154 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002155 min_arg_count = 0;
2156 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002157 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002158 min_arg_count = 0;
2159 max_arg_count = 1;
2160 }
2161 break;
2162 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002163 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002164 min_arg_count = 1;
2165 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002166 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002167 min_arg_count = 1;
2168 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002169 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002170 min_arg_count = 0;
2171 max_arg_count = 0;
2172 }
2173 break;
2174 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002175 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002176 min_arg_count = 2;
2177 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002178 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002179 min_arg_count = 0;
2180 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002181 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002182 min_arg_count = 2;
2183 max_arg_count = 2;
2184 }
2185 break;
2186 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002187 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002188 min_arg_count = 2;
2189 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002190 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002191 min_arg_count = 3;
2192 max_arg_count = 3;
2193 }
2194 break;
2195 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002196 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002197 min_arg_count = 0;
2198 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002199 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002200 min_arg_count = 1;
2201 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002202 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002203 min_arg_count = 2;
2204 max_arg_count = 2;
2205 }
2206 break;
2207 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002208 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002209 min_arg_count = 2;
2210 max_arg_count = 2;
2211 }
2212 break;
2213 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002214 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002215 min_arg_count = 2;
2216 max_arg_count = 2;
2217 }
2218 break;
2219 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002220 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002221 min_arg_count = 0;
2222 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002223 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002224 min_arg_count = 0;
2225 max_arg_count = 1;
2226 }
2227 break;
2228 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002229 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002230 min_arg_count = 0;
2231 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002232 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002233 min_arg_count = 2;
2234 max_arg_count = 2;
2235 }
2236 break;
2237 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002238 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002239 min_arg_count = 2;
2240 max_arg_count = 2;
2241 }
2242 break;
2243 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002244 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002245 min_arg_count = 2;
2246 max_arg_count = 2;
2247 }
2248 break;
2249 }
2250 if (min_arg_count == -1) {
Michal Vasko004d3152020-06-11 19:59:22 +02002251 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002252 return LY_EINVAL;
2253 }
Michal Vasko004d3152020-06-11 19:59:22 +02002254 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002255
2256 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002257 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002258 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002259 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002260
2261 /* ( Expr ( ',' Expr )* )? */
2262 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002263 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002264 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002265 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002266 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002267 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002268 LY_CHECK_RET(rc);
2269 }
Michal Vasko004d3152020-06-11 19:59:22 +02002270 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2271 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002272
2273 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002274 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002275 LY_CHECK_RET(rc);
2276 }
2277
2278 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002279 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002280 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002281 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002282
Radek Krejci857189e2020-09-01 13:26:36 +02002283 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Michal Vasko004d3152020-06-11 19:59:22 +02002284 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx],
Michal Vasko69730152020-10-09 16:30:07 +02002285 &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002286 return LY_EVALID;
2287 }
2288
2289 return LY_SUCCESS;
2290}
2291
2292/**
2293 * @brief Reparse PathExpr. Logs directly on error.
2294 *
2295 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2296 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2297 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2298 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2299 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2300 *
2301 * @param[in] ctx Context for logging.
2302 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002303 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002304 * @return LY_ERR
2305 */
2306static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002307reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002308{
2309 LY_ERR rc;
2310
Michal Vasko004d3152020-06-11 19:59:22 +02002311 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002312 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002313 }
2314
Michal Vasko004d3152020-06-11 19:59:22 +02002315 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002316 case LYXP_TOKEN_PAR1:
2317 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002318 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002319
Michal Vasko004d3152020-06-11 19:59:22 +02002320 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002321 LY_CHECK_RET(rc);
2322
Michal Vasko004d3152020-06-11 19:59:22 +02002323 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002324 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002325 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002326 goto predicate;
2327 break;
2328 case LYXP_TOKEN_DOT:
2329 case LYXP_TOKEN_DDOT:
2330 case LYXP_TOKEN_AT:
2331 case LYXP_TOKEN_NAMETEST:
2332 case LYXP_TOKEN_NODETYPE:
2333 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002334 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002335 LY_CHECK_RET(rc);
2336 break;
2337 case LYXP_TOKEN_FUNCNAME:
2338 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002339 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002340 LY_CHECK_RET(rc);
2341 goto predicate;
2342 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002343 case LYXP_TOKEN_OPER_PATH:
2344 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002345 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002346 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002347 LY_CHECK_RET(rc);
2348 break;
2349 case LYXP_TOKEN_LITERAL:
2350 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002351 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002352 goto predicate;
2353 break;
2354 case LYXP_TOKEN_NUMBER:
2355 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002356 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002357 goto predicate;
2358 break;
2359 default:
2360 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko69730152020-10-09 16:30:07 +02002361 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002362 return LY_EVALID;
2363 }
2364
2365 return LY_SUCCESS;
2366
2367predicate:
2368 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002369 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2370 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002371 LY_CHECK_RET(rc);
2372 }
2373
2374 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002375 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002376
2377 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002378 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002379
Michal Vasko004d3152020-06-11 19:59:22 +02002380 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002381 LY_CHECK_RET(rc);
2382 }
2383
2384 return LY_SUCCESS;
2385}
2386
2387/**
2388 * @brief Reparse UnaryExpr. Logs directly on error.
2389 *
2390 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2391 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2392 *
2393 * @param[in] ctx Context for logging.
2394 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002395 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002396 * @return LY_ERR
2397 */
2398static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002399reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002400{
2401 uint16_t prev_exp;
2402 LY_ERR rc;
2403
2404 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002405 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002406 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2407 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002408 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002409 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002410 }
2411
2412 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002413 prev_exp = *tok_idx;
2414 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002415 LY_CHECK_RET(rc);
2416
2417 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002418 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002419 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002420 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002421
Michal Vasko004d3152020-06-11 19:59:22 +02002422 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002423 LY_CHECK_RET(rc);
2424 }
2425
2426 return LY_SUCCESS;
2427}
2428
2429/**
2430 * @brief Reparse AdditiveExpr. Logs directly on error.
2431 *
2432 * [15] AdditiveExpr ::= MultiplicativeExpr
2433 * | AdditiveExpr '+' MultiplicativeExpr
2434 * | AdditiveExpr '-' MultiplicativeExpr
2435 * [16] MultiplicativeExpr ::= UnaryExpr
2436 * | MultiplicativeExpr '*' UnaryExpr
2437 * | MultiplicativeExpr 'div' UnaryExpr
2438 * | MultiplicativeExpr 'mod' UnaryExpr
2439 *
2440 * @param[in] ctx Context for logging.
2441 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002442 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002443 * @return LY_ERR
2444 */
2445static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002446reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002447{
2448 uint16_t prev_add_exp, prev_mul_exp;
2449 LY_ERR rc;
2450
Michal Vasko004d3152020-06-11 19:59:22 +02002451 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002452 goto reparse_multiplicative_expr;
2453
2454 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002455 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2456 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002457 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002458 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002459
2460reparse_multiplicative_expr:
2461 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002462 prev_mul_exp = *tok_idx;
2463 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002464 LY_CHECK_RET(rc);
2465
2466 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002467 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2468 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002469 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002470 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002471
Michal Vasko004d3152020-06-11 19:59:22 +02002472 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002473 LY_CHECK_RET(rc);
2474 }
2475 }
2476
2477 return LY_SUCCESS;
2478}
2479
2480/**
2481 * @brief Reparse EqualityExpr. Logs directly on error.
2482 *
2483 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2484 * | EqualityExpr '!=' RelationalExpr
2485 * [14] RelationalExpr ::= AdditiveExpr
2486 * | RelationalExpr '<' AdditiveExpr
2487 * | RelationalExpr '>' AdditiveExpr
2488 * | RelationalExpr '<=' AdditiveExpr
2489 * | RelationalExpr '>=' AdditiveExpr
2490 *
2491 * @param[in] ctx Context for logging.
2492 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002493 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002494 * @return LY_ERR
2495 */
2496static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002497reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002498{
2499 uint16_t prev_eq_exp, prev_rel_exp;
2500 LY_ERR rc;
2501
Michal Vasko004d3152020-06-11 19:59:22 +02002502 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002503 goto reparse_additive_expr;
2504
2505 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002506 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002507 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002508 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002509
2510reparse_additive_expr:
2511 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002512 prev_rel_exp = *tok_idx;
2513 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002514 LY_CHECK_RET(rc);
2515
2516 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002517 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002518 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002519 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002520
Michal Vasko004d3152020-06-11 19:59:22 +02002521 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002522 LY_CHECK_RET(rc);
2523 }
2524 }
2525
2526 return LY_SUCCESS;
2527}
2528
2529/**
2530 * @brief Reparse OrExpr. Logs directly on error.
2531 *
2532 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2533 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2534 *
2535 * @param[in] ctx Context for logging.
2536 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002537 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002538 * @return LY_ERR
2539 */
2540static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002541reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002542{
2543 uint16_t prev_or_exp, prev_and_exp;
2544 LY_ERR rc;
2545
Michal Vasko004d3152020-06-11 19:59:22 +02002546 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002547 goto reparse_equality_expr;
2548
2549 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002550 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 +02002551 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002552 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002553
2554reparse_equality_expr:
2555 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002556 prev_and_exp = *tok_idx;
2557 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002558 LY_CHECK_RET(rc);
2559
2560 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002561 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 +02002562 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002563 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002564
Michal Vasko004d3152020-06-11 19:59:22 +02002565 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002566 LY_CHECK_RET(rc);
2567 }
2568 }
2569
2570 return LY_SUCCESS;
2571}
Radek Krejcib1646a92018-11-02 16:08:26 +01002572
2573/**
2574 * @brief Parse NCName.
2575 *
2576 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002577 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002578 */
Radek Krejcid4270262019-01-07 15:07:25 +01002579static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002580parse_ncname(const char *ncname)
2581{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002582 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002583 size_t size;
2584 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002585
2586 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2587 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2588 return len;
2589 }
2590
2591 do {
2592 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002593 if (!*ncname) {
2594 break;
2595 }
Radek Krejcid4270262019-01-07 15:07:25 +01002596 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002597 } while (is_xmlqnamechar(uc) && (uc != ':'));
2598
2599 return len;
2600}
2601
2602/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002603 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002604 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002605 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002606 * @param[in] exp Expression to use.
2607 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002608 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002609 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002610 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002611 */
2612static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002613exp_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 +01002614{
2615 uint32_t prev;
2616
2617 if (exp->used == exp->size) {
2618 prev = exp->size;
2619 exp->size += LYXP_EXPR_SIZE_STEP;
2620 if (prev > exp->size) {
2621 LOGINT(ctx);
2622 return LY_EINT;
2623 }
2624
2625 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2626 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2627 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2628 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2629 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2630 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2631 }
2632
2633 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002634 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002635 exp->tok_len[exp->used] = tok_len;
2636 ++exp->used;
2637 return LY_SUCCESS;
2638}
2639
2640void
Michal Vasko14676352020-05-29 11:35:55 +02002641lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002642{
2643 uint16_t i;
2644
2645 if (!expr) {
2646 return;
2647 }
2648
2649 lydict_remove(ctx, expr->expr);
2650 free(expr->tokens);
2651 free(expr->tok_pos);
2652 free(expr->tok_len);
2653 if (expr->repeat) {
2654 for (i = 0; i < expr->used; ++i) {
2655 free(expr->repeat[i]);
2656 }
2657 }
2658 free(expr->repeat);
2659 free(expr);
2660}
2661
Radek Krejcif03a9e22020-09-18 20:09:31 +02002662LY_ERR
2663lyxp_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 +01002664{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002665 LY_ERR ret = LY_SUCCESS;
2666 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002667 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002668 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002669 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002670 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002671
Radek Krejcif03a9e22020-09-18 20:09:31 +02002672 assert(expr_p);
2673
2674 if (!expr_str[0]) {
Michal Vasko004d3152020-06-11 19:59:22 +02002675 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002676 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002677 }
2678
2679 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002680 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002681 }
2682 if (expr_len > UINT16_MAX) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002683 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
2684 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002685 }
2686
2687 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002688 expr = calloc(1, sizeof *expr);
2689 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2690 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2691 expr->used = 0;
2692 expr->size = LYXP_EXPR_SIZE_START;
2693 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2694 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002695
Radek Krejcif03a9e22020-09-18 20:09:31 +02002696 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2697 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002698
Radek Krejcif03a9e22020-09-18 20:09:31 +02002699 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2700 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002701
Michal Vasko004d3152020-06-11 19:59:22 +02002702 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002703 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002704
Radek Krejcif03a9e22020-09-18 20:09:31 +02002705 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002706 ++parsed;
2707 }
2708
2709 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002710 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002711
2712 /* '(' */
2713 tok_len = 1;
2714 tok_type = LYXP_TOKEN_PAR1;
2715
Radek Krejcif03a9e22020-09-18 20:09:31 +02002716 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002717 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002718 if (((expr->tok_len[expr->used - 1] == 4) &&
2719 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2720 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2721 ((expr->tok_len[expr->used - 1] == 7) &&
2722 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002723 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002724 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002725 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002726 }
2727 prev_function_check = 0;
2728 }
2729
Radek Krejcif03a9e22020-09-18 20:09:31 +02002730 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002731
2732 /* ')' */
2733 tok_len = 1;
2734 tok_type = LYXP_TOKEN_PAR2;
2735
Radek Krejcif03a9e22020-09-18 20:09:31 +02002736 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002737
2738 /* '[' */
2739 tok_len = 1;
2740 tok_type = LYXP_TOKEN_BRACK1;
2741
Radek Krejcif03a9e22020-09-18 20:09:31 +02002742 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002743
2744 /* ']' */
2745 tok_len = 1;
2746 tok_type = LYXP_TOKEN_BRACK2;
2747
Radek Krejcif03a9e22020-09-18 20:09:31 +02002748 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002749
2750 /* '..' */
2751 tok_len = 2;
2752 tok_type = LYXP_TOKEN_DDOT;
2753
Radek Krejcif03a9e22020-09-18 20:09:31 +02002754 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002755
2756 /* '.' */
2757 tok_len = 1;
2758 tok_type = LYXP_TOKEN_DOT;
2759
Radek Krejcif03a9e22020-09-18 20:09:31 +02002760 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002761
2762 /* '@' */
2763 tok_len = 1;
2764 tok_type = LYXP_TOKEN_AT;
2765
Radek Krejcif03a9e22020-09-18 20:09:31 +02002766 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002767
2768 /* ',' */
2769 tok_len = 1;
2770 tok_type = LYXP_TOKEN_COMMA;
2771
Radek Krejcif03a9e22020-09-18 20:09:31 +02002772 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002773
2774 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002775 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2776 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Michal Vasko69730152020-10-09 16:30:07 +02002777 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
2778 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002779 ++tok_len;
2780 tok_type = LYXP_TOKEN_LITERAL;
2781
Radek Krejcif03a9e22020-09-18 20:09:31 +02002782 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002783
2784 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002785 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2786 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Michal Vasko69730152020-10-09 16:30:07 +02002787 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
2788 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002789 ++tok_len;
2790 tok_type = LYXP_TOKEN_LITERAL;
2791
Radek Krejcif03a9e22020-09-18 20:09:31 +02002792 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002793
2794 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002795 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2796 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002797 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002798 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002799 }
2800 tok_type = LYXP_TOKEN_NUMBER;
2801
Radek Krejcif03a9e22020-09-18 20:09:31 +02002802 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002803
2804 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002805 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002806 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002807 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002808 } else {
2809 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002810 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002811 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002812
Radek Krejcif03a9e22020-09-18 20:09:31 +02002813 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002814
Michal Vasko3e48bf32020-06-01 08:39:07 +02002815 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002816 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002817 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2818
Radek Krejcif03a9e22020-09-18 20:09:31 +02002819 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002820
2821 /* Operator '<=', '>=' */
2822 tok_len = 2;
2823 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002824
Radek Krejcif03a9e22020-09-18 20:09:31 +02002825 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002826
2827 /* Operator '|' */
2828 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002829 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002830
Radek Krejcif03a9e22020-09-18 20:09:31 +02002831 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002832
2833 /* Operator '+', '-' */
2834 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002835 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002836
Radek Krejcif03a9e22020-09-18 20:09:31 +02002837 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002838
Michal Vasko3e48bf32020-06-01 08:39:07 +02002839 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002840 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002841 tok_type = LYXP_TOKEN_OPER_EQUAL;
2842
Radek Krejcif03a9e22020-09-18 20:09:31 +02002843 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002844
2845 /* Operator '<', '>' */
2846 tok_len = 1;
2847 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002848
Michal Vasko69730152020-10-09 16:30:07 +02002849 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2850 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2851 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2852 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2853 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2854 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2855 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2856 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2857 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2858 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2859 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2860 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002861
2862 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002863 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002864 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002865 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002866
Radek Krejcif03a9e22020-09-18 20:09:31 +02002867 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002868 tok_len = 2;
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], "and", 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_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002874
Radek Krejcif03a9e22020-09-18 20:09:31 +02002875 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002876 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002877 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002878
2879 } else if (prev_function_check) {
Michal Vasko53078572019-05-24 08:50:15 +02002880 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
Michal Vasko69730152020-10-09 16:30:07 +02002881 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 +02002882 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002883 goto error;
2884 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002885 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr_str);
2886 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002887 goto error;
2888 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002889 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002890
2891 /* NameTest '*' */
2892 tok_len = 1;
2893 tok_type = LYXP_TOKEN_NAMETEST;
2894
2895 } else {
2896
2897 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002898 long int ncname_len = parse_ncname(&expr_str[parsed]);
2899 LY_CHECK_ERR_GOTO(ncname_len < 0,
Michal Vasko69730152020-10-09 16:30:07 +02002900 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
2901 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002902 tok_len = ncname_len;
2903
Radek Krejcif03a9e22020-09-18 20:09:31 +02002904 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002905 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002906 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002907 ++tok_len;
2908 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002909 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
2910 LY_CHECK_ERR_GOTO(ncname_len < 0,
Michal Vasko69730152020-10-09 16:30:07 +02002911 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
2912 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002913 tok_len += ncname_len;
2914 }
2915 /* remove old flag to prevent ambiguities */
2916 prev_function_check = 0;
2917 tok_type = LYXP_TOKEN_NAMETEST;
2918 } else {
2919 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2920 prev_function_check = 1;
2921 tok_type = LYXP_TOKEN_NAMETEST;
2922 }
2923 }
2924
2925 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002926 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002927 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002928 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002929 ++parsed;
2930 }
2931
Radek Krejcif03a9e22020-09-18 20:09:31 +02002932 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002933
Michal Vasko004d3152020-06-11 19:59:22 +02002934 if (reparse) {
2935 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002936 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2937 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002938
Michal Vasko004d3152020-06-11 19:59:22 +02002939 /* fill repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002940 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error);
2941 if (expr->used > tok_idx) {
Michal Vasko004d3152020-06-11 19:59:22 +02002942 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002943 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002944 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002945 goto error;
2946 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002947 }
2948
Radek Krejcif03a9e22020-09-18 20:09:31 +02002949 print_expr_struct_debug(expr);
2950 *expr_p = expr;
2951 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002952
2953error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002954 lyxp_expr_free(ctx, expr);
2955 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002956}
2957
Michal Vasko1734be92020-09-22 08:55:10 +02002958LY_ERR
2959lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02002960{
Michal Vasko1734be92020-09-22 08:55:10 +02002961 LY_ERR ret = LY_SUCCESS;
2962 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02002963 uint32_t i, j;
2964
Michal Vasko7f45cf22020-10-01 12:49:44 +02002965 if (!exp) {
2966 goto cleanup;
2967 }
2968
Michal Vasko004d3152020-06-11 19:59:22 +02002969 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02002970 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002971
2972 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
Michal Vasko1734be92020-09-22 08:55:10 +02002973 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002974 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2975
2976 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
Michal Vasko1734be92020-09-22 08:55:10 +02002977 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002978 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2979
2980 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
Michal Vasko1734be92020-09-22 08:55:10 +02002981 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002982 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2983
2984 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002985 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002986 for (i = 0; i < exp->used; ++i) {
2987 if (!exp->repeat[i]) {
2988 dup->repeat[i] = NULL;
2989 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02002990 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002991 /* the ending 0 as well */
2992 ++j;
2993
Michal Vasko99c71642020-07-03 13:33:36 +02002994 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002995 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002996 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
2997 dup->repeat[i][j - 1] = 0;
2998 }
2999 }
3000
3001 dup->used = exp->used;
3002 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003003 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003004
Michal Vasko1734be92020-09-22 08:55:10 +02003005cleanup:
3006 if (ret) {
3007 lyxp_expr_free(ctx, dup);
3008 } else {
3009 *dup_p = dup;
3010 }
3011 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003012}
3013
Michal Vasko03ff5a72019-09-11 13:49:33 +02003014/*
3015 * warn functions
3016 *
3017 * Warn functions check specific reasonable conditions for schema XPath
3018 * and print a warning if they are not satisfied.
3019 */
3020
3021/**
3022 * @brief Get the last-added schema node that is currently in the context.
3023 *
3024 * @param[in] set Set to search in.
3025 * @return Last-added schema context node, NULL if no node is in context.
3026 */
3027static struct lysc_node *
3028warn_get_scnode_in_ctx(struct lyxp_set *set)
3029{
3030 uint32_t i;
3031
3032 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3033 return NULL;
3034 }
3035
3036 i = set->used;
3037 do {
3038 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003039 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003040 /* if there are more, simply return the first found (last added) */
3041 return set->val.scnodes[i].scnode;
3042 }
3043 } while (i);
3044
3045 return NULL;
3046}
3047
3048/**
3049 * @brief Test whether a type is numeric - integer type or decimal64.
3050 *
3051 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003052 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003053 */
Radek Krejci857189e2020-09-01 13:26:36 +02003054static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003055warn_is_numeric_type(struct lysc_type *type)
3056{
3057 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003058 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003059 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003060
3061 switch (type->basetype) {
3062 case LY_TYPE_DEC64:
3063 case LY_TYPE_INT8:
3064 case LY_TYPE_UINT8:
3065 case LY_TYPE_INT16:
3066 case LY_TYPE_UINT16:
3067 case LY_TYPE_INT32:
3068 case LY_TYPE_UINT32:
3069 case LY_TYPE_INT64:
3070 case LY_TYPE_UINT64:
3071 return 1;
3072 case LY_TYPE_UNION:
3073 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003074 LY_ARRAY_FOR(uni->types, u) {
3075 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003076 if (ret) {
3077 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003078 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003079 }
3080 }
3081 /* did not find any suitable type */
3082 return 0;
3083 case LY_TYPE_LEAFREF:
3084 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3085 default:
3086 return 0;
3087 }
3088}
3089
3090/**
3091 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3092 *
3093 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003094 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003095 */
Radek Krejci857189e2020-09-01 13:26:36 +02003096static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003097warn_is_string_type(struct lysc_type *type)
3098{
3099 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003100 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003101 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003102
3103 switch (type->basetype) {
3104 case LY_TYPE_BITS:
3105 case LY_TYPE_ENUM:
3106 case LY_TYPE_IDENT:
3107 case LY_TYPE_INST:
3108 case LY_TYPE_STRING:
3109 return 1;
3110 case LY_TYPE_UNION:
3111 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003112 LY_ARRAY_FOR(uni->types, u) {
3113 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003114 if (ret) {
3115 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003116 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003117 }
3118 }
3119 /* did not find any suitable type */
3120 return 0;
3121 case LY_TYPE_LEAFREF:
3122 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3123 default:
3124 return 0;
3125 }
3126}
3127
3128/**
3129 * @brief Test whether a type is one specific type.
3130 *
3131 * @param[in] type Type to test.
3132 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003133 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003134 */
Radek Krejci857189e2020-09-01 13:26:36 +02003135static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003136warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3137{
3138 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003139 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003140 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003141
3142 if (type->basetype == base) {
3143 return 1;
3144 } else if (type->basetype == LY_TYPE_UNION) {
3145 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003146 LY_ARRAY_FOR(uni->types, u) {
3147 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003148 if (ret) {
3149 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003150 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003151 }
3152 }
3153 /* did not find any suitable type */
3154 return 0;
3155 } else if (type->basetype == LY_TYPE_LEAFREF) {
3156 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3157 }
3158
3159 return 0;
3160}
3161
3162/**
3163 * @brief Get next type of a (union) type.
3164 *
3165 * @param[in] type Base type.
3166 * @param[in] prev_type Previously returned type.
3167 * @return Next type or NULL.
3168 */
3169static struct lysc_type *
3170warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3171{
3172 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003173 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003174 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003175
3176 switch (type->basetype) {
3177 case LY_TYPE_UNION:
3178 uni = (struct lysc_type_union *)type;
3179 if (!prev_type) {
3180 return uni->types[0];
3181 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003182 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003183 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003184 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003185 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003186 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003187 found = 1;
3188 }
3189 }
3190 return NULL;
3191 default:
3192 if (prev_type) {
3193 assert(type == prev_type);
3194 return NULL;
3195 } else {
3196 return type;
3197 }
3198 }
3199}
3200
3201/**
3202 * @brief Test whether 2 types have a common type.
3203 *
3204 * @param[in] type1 First type.
3205 * @param[in] type2 Second type.
3206 * @return 1 if they do, 0 otherwise.
3207 */
3208static int
3209warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3210{
3211 struct lysc_type *t1, *rt1, *t2, *rt2;
3212
3213 t1 = NULL;
3214 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3215 if (t1->basetype == LY_TYPE_LEAFREF) {
3216 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3217 } else {
3218 rt1 = t1;
3219 }
3220
3221 t2 = NULL;
3222 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3223 if (t2->basetype == LY_TYPE_LEAFREF) {
3224 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3225 } else {
3226 rt2 = t2;
3227 }
3228
3229 if (rt2->basetype == rt1->basetype) {
3230 /* match found */
3231 return 1;
3232 }
3233 }
3234 }
3235
3236 return 0;
3237}
3238
3239/**
3240 * @brief Check both operands of comparison operators.
3241 *
3242 * @param[in] ctx Context for errors.
3243 * @param[in] set1 First operand set.
3244 * @param[in] set2 Second operand set.
3245 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3246 * @param[in] expr Start of the expression to print with the warning.
3247 * @param[in] tok_pos Token position.
3248 */
3249static void
Radek Krejci857189e2020-09-01 13:26:36 +02003250warn_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 +02003251{
3252 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003253 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003254
3255 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3256 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3257
3258 if (!node1 && !node2) {
3259 /* no node-sets involved, nothing to do */
3260 return;
3261 }
3262
3263 if (node1) {
3264 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3265 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3266 warning = 1;
3267 leaves = 0;
3268 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3269 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3270 warning = 1;
3271 }
3272 }
3273
3274 if (node2) {
3275 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3276 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3277 warning = 1;
3278 leaves = 0;
3279 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3280 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3281 warning = 1;
3282 }
3283 }
3284
3285 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003286 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3287 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3288 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3289 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003290 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3291 warning = 1;
3292 }
3293 }
3294
3295 if (warning) {
3296 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3297 }
3298}
3299
3300/**
3301 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3302 *
3303 * @param[in] exp Parsed XPath expression.
3304 * @param[in] set Set with the leaf/leaf-list.
3305 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3306 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3307 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3308 */
3309static void
Michal Vasko40308e72020-10-20 16:38:40 +02003310warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3311 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003312{
3313 struct lysc_node *scnode;
3314 struct lysc_type *type;
3315 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003316 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003317 LY_ERR rc;
3318 struct ly_err_item *err = NULL;
3319
Michal Vasko69730152020-10-09 16:30:07 +02003320 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3321 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003322 /* check that the node can have the specified value */
3323 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3324 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3325 } else {
3326 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3327 }
3328 if (!value) {
3329 LOGMEM(set->ctx);
3330 return;
3331 }
3332
3333 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3334 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003335 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003336 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003337 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003338 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003339 }
3340
3341 type = ((struct lysc_node_leaf *)scnode)->type;
3342 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003343 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003344 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003345 if (rc == LY_EINCOMPLETE) {
3346 rc = LY_SUCCESS;
3347 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003348
3349 if (err) {
3350 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3351 ly_err_free(err);
3352 } else if (rc != LY_SUCCESS) {
3353 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3354 }
3355 if (rc != LY_SUCCESS) {
3356 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003357 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003358 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003359 } else {
3360 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003361 }
3362 }
3363 free(value);
3364 }
3365}
3366
3367/*
3368 * XPath functions
3369 */
3370
3371/**
3372 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3373 * depending on whether the first node bit value from the second argument is set.
3374 *
3375 * @param[in] args Array of arguments.
3376 * @param[in] arg_count Count of elements in @p args.
3377 * @param[in,out] set Context and result set at the same time.
3378 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003379 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003380 */
3381static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003382xpath_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 +02003383{
3384 struct lyd_node_term *leaf;
3385 struct lysc_node_leaf *sleaf;
3386 struct lysc_type_bits *bits;
3387 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003388 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003389
3390 if (options & LYXP_SCNODE_ALL) {
3391 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3392 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003393 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3394 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 +02003395 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3396 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003397 }
3398
3399 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3400 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3401 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 +02003402 } else if (!warn_is_string_type(sleaf->type)) {
3403 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003404 }
3405 }
3406 set_scnode_clear_ctx(set);
3407 return rc;
3408 }
3409
Michal Vaskod3678892020-05-21 10:06:58 +02003410 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003411 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3412 "bit-is-set(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003413 return LY_EVALID;
3414 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003415 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003416 LY_CHECK_RET(rc);
3417
3418 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003419 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003420 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003421 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3422 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003423 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003424 LY_ARRAY_FOR(bits->bits, u) {
3425 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003426 set_fill_boolean(set, 1);
3427 break;
3428 }
3429 }
3430 }
3431 }
3432
3433 return LY_SUCCESS;
3434}
3435
3436/**
3437 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3438 * with the argument converted to boolean.
3439 *
3440 * @param[in] args Array of arguments.
3441 * @param[in] arg_count Count of elements in @p args.
3442 * @param[in,out] set Context and result set at the same time.
3443 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003444 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003445 */
3446static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003447xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003448{
3449 LY_ERR rc;
3450
3451 if (options & LYXP_SCNODE_ALL) {
3452 set_scnode_clear_ctx(set);
3453 return LY_SUCCESS;
3454 }
3455
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003456 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003457 LY_CHECK_RET(rc);
3458 set_fill_set(set, args[0]);
3459
3460 return LY_SUCCESS;
3461}
3462
3463/**
3464 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3465 * with the first argument rounded up to the nearest integer.
3466 *
3467 * @param[in] args Array of arguments.
3468 * @param[in] arg_count Count of elements in @p args.
3469 * @param[in,out] set Context and result set at the same time.
3470 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003471 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003472 */
3473static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003474xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003475{
3476 struct lysc_node_leaf *sleaf;
3477 LY_ERR rc = LY_SUCCESS;
3478
3479 if (options & LYXP_SCNODE_ALL) {
3480 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3481 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003482 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3483 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 +02003484 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3485 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003486 }
3487 set_scnode_clear_ctx(set);
3488 return rc;
3489 }
3490
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003491 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003492 LY_CHECK_RET(rc);
3493 if ((long long)args[0]->val.num != args[0]->val.num) {
3494 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3495 } else {
3496 set_fill_number(set, args[0]->val.num);
3497 }
3498
3499 return LY_SUCCESS;
3500}
3501
3502/**
3503 * @brief Execute the XPath concat(string, string, string*) function.
3504 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3505 *
3506 * @param[in] args Array of arguments.
3507 * @param[in] arg_count Count of elements in @p args.
3508 * @param[in,out] set Context and result set at the same time.
3509 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003510 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003511 */
3512static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003513xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003514{
3515 uint16_t i;
3516 char *str = NULL;
3517 size_t used = 1;
3518 LY_ERR rc = LY_SUCCESS;
3519 struct lysc_node_leaf *sleaf;
3520
3521 if (options & LYXP_SCNODE_ALL) {
3522 for (i = 0; i < arg_count; ++i) {
3523 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3524 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3525 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003526 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003527 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003528 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 +02003529 }
3530 }
3531 }
3532 set_scnode_clear_ctx(set);
3533 return rc;
3534 }
3535
3536 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003537 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003538 if (rc != LY_SUCCESS) {
3539 free(str);
3540 return rc;
3541 }
3542
3543 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3544 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3545 strcpy(str + used - 1, args[i]->val.str);
3546 used += strlen(args[i]->val.str);
3547 }
3548
3549 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003550 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003551 set->type = LYXP_SET_STRING;
3552 set->val.str = str;
3553
3554 return LY_SUCCESS;
3555}
3556
3557/**
3558 * @brief Execute the XPath contains(string, string) function.
3559 * Returns LYXP_SET_BOOLEAN whether the second argument can
3560 * be found in the first or not.
3561 *
3562 * @param[in] args Array of arguments.
3563 * @param[in] arg_count Count of elements in @p args.
3564 * @param[in,out] set Context and result set at the same time.
3565 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003566 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003567 */
3568static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003569xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003570{
3571 struct lysc_node_leaf *sleaf;
3572 LY_ERR rc = LY_SUCCESS;
3573
3574 if (options & LYXP_SCNODE_ALL) {
3575 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3576 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3577 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 +02003578 } else if (!warn_is_string_type(sleaf->type)) {
3579 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003580 }
3581 }
3582
3583 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3584 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3585 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 +02003586 } else if (!warn_is_string_type(sleaf->type)) {
3587 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003588 }
3589 }
3590 set_scnode_clear_ctx(set);
3591 return rc;
3592 }
3593
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003594 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003595 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003596 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003597 LY_CHECK_RET(rc);
3598
3599 if (strstr(args[0]->val.str, args[1]->val.str)) {
3600 set_fill_boolean(set, 1);
3601 } else {
3602 set_fill_boolean(set, 0);
3603 }
3604
3605 return LY_SUCCESS;
3606}
3607
3608/**
3609 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3610 * with the size of the node-set from the argument.
3611 *
3612 * @param[in] args Array of arguments.
3613 * @param[in] arg_count Count of elements in @p args.
3614 * @param[in,out] set Context and result set at the same time.
3615 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003616 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003617 */
3618static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003619xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003620{
3621 struct lysc_node *scnode = NULL;
3622 LY_ERR rc = LY_SUCCESS;
3623
3624 if (options & LYXP_SCNODE_ALL) {
3625 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3626 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003627 }
3628 set_scnode_clear_ctx(set);
3629 return rc;
3630 }
3631
Michal Vasko03ff5a72019-09-11 13:49:33 +02003632 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003633 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003634 return LY_EVALID;
3635 }
3636
3637 set_fill_number(set, args[0]->used);
3638 return LY_SUCCESS;
3639}
3640
3641/**
3642 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3643 * with the context with the intial node.
3644 *
3645 * @param[in] args Array of arguments.
3646 * @param[in] arg_count Count of elements in @p args.
3647 * @param[in,out] set Context and result set at the same time.
3648 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003649 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003650 */
3651static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003652xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003653{
3654 if (arg_count || args) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003655 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003656 return LY_EVALID;
3657 }
3658
3659 if (options & LYXP_SCNODE_ALL) {
3660 set_scnode_clear_ctx(set);
3661
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003662 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003663 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003664 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003665
3666 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003667 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003668 }
3669
3670 return LY_SUCCESS;
3671}
3672
3673/**
3674 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3675 * leafref or instance-identifier target node(s).
3676 *
3677 * @param[in] args Array of arguments.
3678 * @param[in] arg_count Count of elements in @p args.
3679 * @param[in,out] set Context and result set at the same time.
3680 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003681 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003682 */
3683static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003684xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003685{
3686 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003687 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003688 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003689 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003690 struct ly_path *p;
3691 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003692 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003693 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003694 LY_ERR rc = LY_SUCCESS;
3695
3696 if (options & LYXP_SCNODE_ALL) {
3697 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3698 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003699 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3700 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 +02003701 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3702 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
Michal Vasko69730152020-10-09 16:30:07 +02003703 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003704 }
3705 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003706 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003707 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vasko00cbf532020-06-15 13:58:47 +02003708 oper = lysc_is_output((struct lysc_node *)sleaf) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003709
3710 /* it was already evaluated on schema, it must succeed */
Michal Vaskoc0792ca2020-12-01 12:03:21 +01003711 rc = ly_path_compile(set->ctx, lref->cur_mod, (struct lysc_node *)sleaf, lref->path,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003712 LY_PATH_LREF_TRUE, oper, LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, NULL, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003713 assert(!rc);
3714
3715 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003716 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003717 ly_path_free(set->ctx, p);
3718
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003719 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003720 }
3721
Michal Vasko03ff5a72019-09-11 13:49:33 +02003722 return rc;
3723 }
3724
Michal Vaskod3678892020-05-21 10:06:58 +02003725 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003726 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003727 return LY_EVALID;
3728 }
3729
Michal Vaskod3678892020-05-21 10:06:58 +02003730 lyxp_set_free_content(set);
3731 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003732 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3733 sleaf = (struct lysc_node_leaf *)leaf->schema;
3734 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3735 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3736 /* find leafref target */
Michal Vasko004d3152020-06-11 19:59:22 +02003737 if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf,
Michal Vasko69730152020-10-09 16:30:07 +02003738 &leaf->value, set->tree, &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003739 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003740 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003741 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003742 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003743 } else {
3744 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003745 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003746 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Michal Vasko69730152020-10-09 16:30:07 +02003747 LYD_CANON_VALUE(leaf));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003748 return LY_EVALID;
3749 }
3750 }
Michal Vasko004d3152020-06-11 19:59:22 +02003751
3752 /* insert it */
3753 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003754 }
3755 }
3756
3757 return LY_SUCCESS;
3758}
3759
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003760static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003761xpath_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 +02003762{
3763 uint16_t i;
3764 LY_ARRAY_COUNT_TYPE u;
3765 struct lyd_node_term *leaf;
3766 struct lysc_node_leaf *sleaf;
3767 struct lyd_meta *meta;
3768 struct lyd_value data = {0}, *val;
3769 struct ly_err_item *err = NULL;
3770 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003771 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003772
3773 if (options & LYXP_SCNODE_ALL) {
3774 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3775 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
3776 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3777 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003778 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003779 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3780 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3781 }
3782
3783 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3784 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3785 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003786 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003787 } else if (!warn_is_string_type(sleaf->type)) {
3788 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3789 }
3790 }
3791 set_scnode_clear_ctx(set);
3792 return rc;
3793 }
3794
3795 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003796 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02003797 "derived-from(-or-self)(node-set, string)");
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003798 return LY_EVALID;
3799 }
3800 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3801 LY_CHECK_RET(rc);
3802
3803 set_fill_boolean(set, 0);
3804 found = 0;
3805 for (i = 0; i < args[0]->used; ++i) {
3806 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3807 continue;
3808 }
3809
3810 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3811 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3812 sleaf = (struct lysc_node_leaf *)leaf->schema;
3813 val = &leaf->value;
3814 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3815 /* uninteresting */
3816 continue;
3817 }
3818
3819 /* store args[1] as ident */
3820 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 +01003821 0, set->format, set->prefix_data, LYD_HINT_DATA, leaf->schema, &data, NULL, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003822 } else {
3823 meta = args[0]->val.meta[i].meta;
3824 val = &meta->value;
3825 if (val->realtype->basetype != LY_TYPE_IDENT) {
3826 /* uninteresting */
3827 continue;
3828 }
3829
3830 /* store args[1] as ident */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003831 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 +01003832 set->format, set->prefix_data, LYD_HINT_DATA, meta->parent->schema, &data, NULL, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003833 }
3834
3835 if (err) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01003836 ly_err_print(set->ctx, err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003837 ly_err_free(err);
3838 }
3839 LY_CHECK_RET(rc);
3840
3841 /* finally check the identity itself */
3842 if (self_match && (data.ident == val->ident)) {
3843 set_fill_boolean(set, 1);
3844 found = 1;
3845 }
3846 if (!found) {
3847 LY_ARRAY_FOR(data.ident->derived, u) {
3848 if (data.ident->derived[u] == val->ident) {
3849 set_fill_boolean(set, 1);
3850 found = 1;
3851 break;
3852 }
3853 }
3854 }
3855
3856 /* free temporary value */
3857 val->realtype->plugin->free(set->ctx, &data);
3858 if (found) {
3859 break;
3860 }
3861 }
3862
3863 return LY_SUCCESS;
3864}
3865
Michal Vasko03ff5a72019-09-11 13:49:33 +02003866/**
3867 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3868 * on whether the first argument nodes contain a node of an identity derived from the second
3869 * argument identity.
3870 *
3871 * @param[in] args Array of arguments.
3872 * @param[in] arg_count Count of elements in @p args.
3873 * @param[in,out] set Context and result set at the same time.
3874 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003875 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003876 */
3877static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003878xpath_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 +02003879{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003880 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003881}
3882
3883/**
3884 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3885 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3886 * the second argument identity.
3887 *
3888 * @param[in] args Array of arguments.
3889 * @param[in] arg_count Count of elements in @p args.
3890 * @param[in,out] set Context and result set at the same time.
3891 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003892 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003893 */
3894static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003895xpath_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 +02003896{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003897 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003898}
3899
3900/**
3901 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3902 * with the integer value of the first node's enum value, otherwise NaN.
3903 *
3904 * @param[in] args Array of arguments.
3905 * @param[in] arg_count Count of elements in @p args.
3906 * @param[in,out] set Context and result set at the same time.
3907 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003908 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003909 */
3910static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003911xpath_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 +02003912{
3913 struct lyd_node_term *leaf;
3914 struct lysc_node_leaf *sleaf;
3915 LY_ERR rc = LY_SUCCESS;
3916
3917 if (options & LYXP_SCNODE_ALL) {
3918 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3919 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003920 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3921 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 +02003922 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3923 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003924 }
3925 set_scnode_clear_ctx(set);
3926 return rc;
3927 }
3928
Michal Vaskod3678892020-05-21 10:06:58 +02003929 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003930 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003931 return LY_EVALID;
3932 }
3933
3934 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003935 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003936 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3937 sleaf = (struct lysc_node_leaf *)leaf->schema;
3938 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3939 set_fill_number(set, leaf->value.enum_item->value);
3940 }
3941 }
3942
3943 return LY_SUCCESS;
3944}
3945
3946/**
3947 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3948 * with false value.
3949 *
3950 * @param[in] args Array of arguments.
3951 * @param[in] arg_count Count of elements in @p args.
3952 * @param[in,out] set Context and result set at the same time.
3953 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003954 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003955 */
3956static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003957xpath_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 +02003958{
3959 if (options & LYXP_SCNODE_ALL) {
3960 set_scnode_clear_ctx(set);
3961 return LY_SUCCESS;
3962 }
3963
3964 set_fill_boolean(set, 0);
3965 return LY_SUCCESS;
3966}
3967
3968/**
3969 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3970 * with the first argument floored (truncated).
3971 *
3972 * @param[in] args Array of arguments.
3973 * @param[in] arg_count Count of elements in @p args.
3974 * @param[in,out] set Context and result set at the same time.
3975 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003976 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003977 */
3978static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003979xpath_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 +02003980{
3981 LY_ERR rc;
3982
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003983 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003984 LY_CHECK_RET(rc);
3985 if (isfinite(args[0]->val.num)) {
3986 set_fill_number(set, (long long)args[0]->val.num);
3987 }
3988
3989 return LY_SUCCESS;
3990}
3991
3992/**
3993 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3994 * whether the language of the text matches the one from the argument.
3995 *
3996 * @param[in] args Array of arguments.
3997 * @param[in] arg_count Count of elements in @p args.
3998 * @param[in,out] set Context and result set at the same time.
3999 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004000 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004001 */
4002static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004003xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004004{
4005 const struct lyd_node *node;
4006 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004007 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004008 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004009 LY_ERR rc = LY_SUCCESS;
4010
4011 if (options & LYXP_SCNODE_ALL) {
4012 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4013 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4014 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 +02004015 } else if (!warn_is_string_type(sleaf->type)) {
4016 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004017 }
4018 }
4019 set_scnode_clear_ctx(set);
4020 return rc;
4021 }
4022
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004023 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004024 LY_CHECK_RET(rc);
4025
Michal Vasko03ff5a72019-09-11 13:49:33 +02004026 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004027 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004028 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004029 } else if (!set->used) {
4030 set_fill_boolean(set, 0);
4031 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004032 }
4033
4034 switch (set->val.nodes[0].type) {
4035 case LYXP_NODE_ELEM:
4036 case LYXP_NODE_TEXT:
4037 node = set->val.nodes[0].node;
4038 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004039 case LYXP_NODE_META:
4040 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004041 break;
4042 default:
4043 /* nothing to do with roots */
4044 set_fill_boolean(set, 0);
4045 return LY_SUCCESS;
4046 }
4047
Michal Vasko9f96a052020-03-10 09:41:45 +01004048 /* find lang metadata */
Michal Vaskod989ba02020-08-24 10:59:24 +02004049 for ( ; node; node = (struct lyd_node *)node->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004050 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004051 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004052 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004053 break;
4054 }
4055 }
4056
Michal Vasko9f96a052020-03-10 09:41:45 +01004057 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004058 break;
4059 }
4060 }
4061
4062 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004063 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004064 set_fill_boolean(set, 0);
4065 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004066 uint64_t i;
4067
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004068 val = meta->value.canonical;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004069 for (i = 0; args[0]->val.str[i]; ++i) {
4070 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4071 set_fill_boolean(set, 0);
4072 break;
4073 }
4074 }
4075 if (!args[0]->val.str[i]) {
4076 if (!val[i] || (val[i] == '-')) {
4077 set_fill_boolean(set, 1);
4078 } else {
4079 set_fill_boolean(set, 0);
4080 }
4081 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004082 }
4083
4084 return LY_SUCCESS;
4085}
4086
4087/**
4088 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4089 * with the context size.
4090 *
4091 * @param[in] args Array of arguments.
4092 * @param[in] arg_count Count of elements in @p args.
4093 * @param[in,out] set Context and result set at the same time.
4094 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004095 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004096 */
4097static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004098xpath_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 +02004099{
4100 if (options & LYXP_SCNODE_ALL) {
4101 set_scnode_clear_ctx(set);
4102 return LY_SUCCESS;
4103 }
4104
Michal Vasko03ff5a72019-09-11 13:49:33 +02004105 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004106 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004107 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004108 } else if (!set->used) {
4109 set_fill_number(set, 0);
4110 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004111 }
4112
4113 set_fill_number(set, set->ctx_size);
4114 return LY_SUCCESS;
4115}
4116
4117/**
4118 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4119 * with the node name without namespace from the argument or the context.
4120 *
4121 * @param[in] args Array of arguments.
4122 * @param[in] arg_count Count of elements in @p args.
4123 * @param[in,out] set Context and result set at the same time.
4124 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004125 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004126 */
4127static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004128xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004129{
4130 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004131
Michal Vasko03ff5a72019-09-11 13:49:33 +02004132 /* suppress unused variable warning */
4133 (void)options;
4134
4135 if (options & LYXP_SCNODE_ALL) {
4136 set_scnode_clear_ctx(set);
4137 return LY_SUCCESS;
4138 }
4139
4140 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004141 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004142 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
4143 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004144 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004145 } else if (!args[0]->used) {
4146 set_fill_string(set, "", 0);
4147 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004148 }
4149
4150 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004151 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004152
4153 item = &args[0]->val.nodes[0];
4154 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004155 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004156 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004157 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004158 } else if (!set->used) {
4159 set_fill_string(set, "", 0);
4160 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004161 }
4162
4163 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004164 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004165
4166 item = &set->val.nodes[0];
4167 }
4168
4169 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004170 case LYXP_NODE_NONE:
4171 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004172 case LYXP_NODE_ROOT:
4173 case LYXP_NODE_ROOT_CONFIG:
4174 case LYXP_NODE_TEXT:
4175 set_fill_string(set, "", 0);
4176 break;
4177 case LYXP_NODE_ELEM:
4178 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4179 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004180 case LYXP_NODE_META:
4181 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004182 break;
4183 }
4184
4185 return LY_SUCCESS;
4186}
4187
4188/**
4189 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4190 * with the node name fully qualified (with namespace) from the argument or the context.
4191 *
4192 * @param[in] args Array of arguments.
4193 * @param[in] arg_count Count of elements in @p args.
4194 * @param[in,out] set Context and result set at the same time.
4195 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004196 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004197 */
4198static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004199xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004200{
4201 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004202 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004203 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004204 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004205
4206 if (options & LYXP_SCNODE_ALL) {
4207 set_scnode_clear_ctx(set);
4208 return LY_SUCCESS;
4209 }
4210
4211 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004212 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004213 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004214 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004215 } else if (!args[0]->used) {
4216 set_fill_string(set, "", 0);
4217 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004218 }
4219
4220 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004221 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004222
4223 item = &args[0]->val.nodes[0];
4224 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004225 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004226 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004227 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004228 } else if (!set->used) {
4229 set_fill_string(set, "", 0);
4230 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004231 }
4232
4233 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004234 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004235
4236 item = &set->val.nodes[0];
4237 }
4238
4239 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004240 case LYXP_NODE_NONE:
4241 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004242 case LYXP_NODE_ROOT:
4243 case LYXP_NODE_ROOT_CONFIG:
4244 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004245 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004246 break;
4247 case LYXP_NODE_ELEM:
4248 mod = item->node->schema->module;
4249 name = item->node->schema->name;
4250 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004251 case LYXP_NODE_META:
4252 mod = ((struct lyd_meta *)item->node)->annotation->module;
4253 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004254 break;
4255 }
4256
4257 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004258 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004259 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4260 set_fill_string(set, str, strlen(str));
4261 free(str);
4262 } else {
4263 set_fill_string(set, "", 0);
4264 }
4265
4266 return LY_SUCCESS;
4267}
4268
4269/**
4270 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4271 * with the namespace of the node from the argument or the context.
4272 *
4273 * @param[in] args Array of arguments.
4274 * @param[in] arg_count Count of elements in @p args.
4275 * @param[in,out] set Context and result set at the same time.
4276 * @param[in] options XPath options.
4277 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4278 */
4279static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004280xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004281{
4282 struct lyxp_set_node *item;
4283 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004284
Michal Vasko03ff5a72019-09-11 13:49:33 +02004285 /* suppress unused variable warning */
4286 (void)options;
4287
4288 if (options & LYXP_SCNODE_ALL) {
4289 set_scnode_clear_ctx(set);
4290 return LY_SUCCESS;
4291 }
4292
4293 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004294 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004295 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004296 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004297 return LY_EVALID;
4298 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004299 set_fill_string(set, "", 0);
4300 return LY_SUCCESS;
4301 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004302
4303 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004304 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004305
4306 item = &args[0]->val.nodes[0];
4307 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004308 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004309 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004310 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004311 } else if (!set->used) {
4312 set_fill_string(set, "", 0);
4313 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004314 }
4315
4316 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004317 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004318
4319 item = &set->val.nodes[0];
4320 }
4321
4322 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004323 case LYXP_NODE_NONE:
4324 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004325 case LYXP_NODE_ROOT:
4326 case LYXP_NODE_ROOT_CONFIG:
4327 case LYXP_NODE_TEXT:
4328 set_fill_string(set, "", 0);
4329 break;
4330 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004331 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004332 if (item->type == LYXP_NODE_ELEM) {
4333 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004334 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004335 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004336 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004337 }
4338
4339 set_fill_string(set, mod->ns, strlen(mod->ns));
4340 break;
4341 }
4342
4343 return LY_SUCCESS;
4344}
4345
4346/**
4347 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4348 * with only nodes from the context. In practice it either leaves the context
4349 * as it is or returns an empty node set.
4350 *
4351 * @param[in] args Array of arguments.
4352 * @param[in] arg_count Count of elements in @p args.
4353 * @param[in,out] set Context and result set at the same time.
4354 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004355 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004356 */
4357static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004358xpath_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 +02004359{
4360 if (options & LYXP_SCNODE_ALL) {
4361 set_scnode_clear_ctx(set);
4362 return LY_SUCCESS;
4363 }
4364
4365 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004366 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004367 }
4368 return LY_SUCCESS;
4369}
4370
4371/**
4372 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4373 * with normalized value (no leading, trailing, double white spaces) of the node
4374 * from the argument or the context.
4375 *
4376 * @param[in] args Array of arguments.
4377 * @param[in] arg_count Count of elements in @p args.
4378 * @param[in,out] set Context and result set at the same time.
4379 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004380 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004381 */
4382static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004383xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004384{
4385 uint16_t i, new_used;
4386 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004387 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004388 struct lysc_node_leaf *sleaf;
4389 LY_ERR rc = LY_SUCCESS;
4390
4391 if (options & LYXP_SCNODE_ALL) {
4392 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4393 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4394 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 +02004395 } else if (!warn_is_string_type(sleaf->type)) {
4396 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004397 }
4398 }
4399 set_scnode_clear_ctx(set);
4400 return rc;
4401 }
4402
4403 if (arg_count) {
4404 set_fill_set(set, args[0]);
4405 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004406 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004407 LY_CHECK_RET(rc);
4408
4409 /* is there any normalization necessary? */
4410 for (i = 0; set->val.str[i]; ++i) {
4411 if (is_xmlws(set->val.str[i])) {
4412 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4413 have_spaces = 1;
4414 break;
4415 }
4416 space_before = 1;
4417 } else {
4418 space_before = 0;
4419 }
4420 }
4421
4422 /* yep, there is */
4423 if (have_spaces) {
4424 /* it's enough, at least one character will go, makes space for ending '\0' */
4425 new = malloc(strlen(set->val.str) * sizeof(char));
4426 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4427 new_used = 0;
4428
4429 space_before = 0;
4430 for (i = 0; set->val.str[i]; ++i) {
4431 if (is_xmlws(set->val.str[i])) {
4432 if ((i == 0) || space_before) {
4433 space_before = 1;
4434 continue;
4435 } else {
4436 space_before = 1;
4437 }
4438 } else {
4439 space_before = 0;
4440 }
4441
4442 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4443 ++new_used;
4444 }
4445
4446 /* at worst there is one trailing space now */
4447 if (new_used && is_xmlws(new[new_used - 1])) {
4448 --new_used;
4449 }
4450
4451 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4452 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4453 new[new_used] = '\0';
4454
4455 free(set->val.str);
4456 set->val.str = new;
4457 }
4458
4459 return LY_SUCCESS;
4460}
4461
4462/**
4463 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4464 * with the argument converted to boolean and logically inverted.
4465 *
4466 * @param[in] args Array of arguments.
4467 * @param[in] arg_count Count of elements in @p args.
4468 * @param[in,out] set Context and result set at the same time.
4469 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004470 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004471 */
4472static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004473xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004474{
4475 if (options & LYXP_SCNODE_ALL) {
4476 set_scnode_clear_ctx(set);
4477 return LY_SUCCESS;
4478 }
4479
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004480 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004481 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004482 set_fill_boolean(set, 0);
4483 } else {
4484 set_fill_boolean(set, 1);
4485 }
4486
4487 return LY_SUCCESS;
4488}
4489
4490/**
4491 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4492 * with the number representation of either the argument or the context.
4493 *
4494 * @param[in] args Array of arguments.
4495 * @param[in] arg_count Count of elements in @p args.
4496 * @param[in,out] set Context and result set at the same time.
4497 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004498 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004499 */
4500static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004501xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004502{
4503 LY_ERR rc;
4504
4505 if (options & LYXP_SCNODE_ALL) {
4506 set_scnode_clear_ctx(set);
4507 return LY_SUCCESS;
4508 }
4509
4510 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004511 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004512 LY_CHECK_RET(rc);
4513 set_fill_set(set, args[0]);
4514 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004515 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004516 LY_CHECK_RET(rc);
4517 }
4518
4519 return LY_SUCCESS;
4520}
4521
4522/**
4523 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4524 * with the context position.
4525 *
4526 * @param[in] args Array of arguments.
4527 * @param[in] arg_count Count of elements in @p args.
4528 * @param[in,out] set Context and result set at the same time.
4529 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004530 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004531 */
4532static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004533xpath_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 +02004534{
4535 if (options & LYXP_SCNODE_ALL) {
4536 set_scnode_clear_ctx(set);
4537 return LY_SUCCESS;
4538 }
4539
Michal Vasko03ff5a72019-09-11 13:49:33 +02004540 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004541 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004542 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004543 } else if (!set->used) {
4544 set_fill_number(set, 0);
4545 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004546 }
4547
4548 set_fill_number(set, set->ctx_pos);
4549
4550 /* UNUSED in 'Release' build type */
4551 (void)options;
4552 return LY_SUCCESS;
4553}
4554
4555/**
4556 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4557 * depending on whether the second argument regex matches the first argument string. For details refer to
4558 * YANG 1.1 RFC section 10.2.1.
4559 *
4560 * @param[in] args Array of arguments.
4561 * @param[in] arg_count Count of elements in @p args.
4562 * @param[in,out] set Context and result set at the same time.
4563 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004564 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004565 */
4566static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004567xpath_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 +02004568{
4569 struct lysc_pattern **patterns = NULL, **pattern;
4570 struct lysc_node_leaf *sleaf;
4571 char *path;
4572 LY_ERR rc = LY_SUCCESS;
4573 struct ly_err_item *err;
4574
4575 if (options & LYXP_SCNODE_ALL) {
4576 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4577 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4578 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 +02004579 } else if (!warn_is_string_type(sleaf->type)) {
4580 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004581 }
4582 }
4583
4584 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4585 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4586 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 +02004587 } else if (!warn_is_string_type(sleaf->type)) {
4588 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004589 }
4590 }
4591 set_scnode_clear_ctx(set);
4592 return rc;
4593 }
4594
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004595 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004596 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004597 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004598 LY_CHECK_RET(rc);
4599
4600 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4601 *pattern = malloc(sizeof **pattern);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004602 path = lyd_path(set->cur_node, LYD_PATH_LOG, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004603 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4604 free(path);
4605 if (rc != LY_SUCCESS) {
4606 LY_ARRAY_FREE(patterns);
4607 return rc;
4608 }
4609
4610 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4611 pcre2_code_free((*pattern)->code);
4612 free(*pattern);
4613 LY_ARRAY_FREE(patterns);
4614 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004615 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004616 ly_err_free(err);
4617 return rc;
4618 }
4619
4620 if (rc == LY_EVALID) {
4621 ly_err_free(err);
4622 set_fill_boolean(set, 0);
4623 } else {
4624 set_fill_boolean(set, 1);
4625 }
4626
4627 return LY_SUCCESS;
4628}
4629
4630/**
4631 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4632 * with the rounded first argument. For details refer to
4633 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4634 *
4635 * @param[in] args Array of arguments.
4636 * @param[in] arg_count Count of elements in @p args.
4637 * @param[in,out] set Context and result set at the same time.
4638 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004639 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004640 */
4641static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004642xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004643{
4644 struct lysc_node_leaf *sleaf;
4645 LY_ERR rc = LY_SUCCESS;
4646
4647 if (options & LYXP_SCNODE_ALL) {
4648 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4649 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004650 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4651 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 +02004652 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4653 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004654 }
4655 set_scnode_clear_ctx(set);
4656 return rc;
4657 }
4658
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004659 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004660 LY_CHECK_RET(rc);
4661
4662 /* cover only the cases where floor can't be used */
4663 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4664 set_fill_number(set, -0.0f);
4665 } else {
4666 args[0]->val.num += 0.5;
4667 rc = xpath_floor(args, 1, args[0], options);
4668 LY_CHECK_RET(rc);
4669 set_fill_number(set, args[0]->val.num);
4670 }
4671
4672 return LY_SUCCESS;
4673}
4674
4675/**
4676 * @brief Execute the XPath starts-with(string, string) function.
4677 * Returns LYXP_SET_BOOLEAN whether the second argument is
4678 * the prefix of the first or not.
4679 *
4680 * @param[in] args Array of arguments.
4681 * @param[in] arg_count Count of elements in @p args.
4682 * @param[in,out] set Context and result set at the same time.
4683 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004684 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004685 */
4686static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004687xpath_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 +02004688{
4689 struct lysc_node_leaf *sleaf;
4690 LY_ERR rc = LY_SUCCESS;
4691
4692 if (options & LYXP_SCNODE_ALL) {
4693 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4694 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4695 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 +02004696 } else if (!warn_is_string_type(sleaf->type)) {
4697 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004698 }
4699 }
4700
4701 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4702 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4703 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 +02004704 } else if (!warn_is_string_type(sleaf->type)) {
4705 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004706 }
4707 }
4708 set_scnode_clear_ctx(set);
4709 return rc;
4710 }
4711
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004712 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004713 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004714 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004715 LY_CHECK_RET(rc);
4716
4717 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4718 set_fill_boolean(set, 0);
4719 } else {
4720 set_fill_boolean(set, 1);
4721 }
4722
4723 return LY_SUCCESS;
4724}
4725
4726/**
4727 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4728 * with the string representation of either the argument or the context.
4729 *
4730 * @param[in] args Array of arguments.
4731 * @param[in] arg_count Count of elements in @p args.
4732 * @param[in,out] set Context and result set at the same time.
4733 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004734 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004735 */
4736static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004737xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004738{
4739 LY_ERR rc;
4740
4741 if (options & LYXP_SCNODE_ALL) {
4742 set_scnode_clear_ctx(set);
4743 return LY_SUCCESS;
4744 }
4745
4746 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004747 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004748 LY_CHECK_RET(rc);
4749 set_fill_set(set, args[0]);
4750 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004751 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004752 LY_CHECK_RET(rc);
4753 }
4754
4755 return LY_SUCCESS;
4756}
4757
4758/**
4759 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4760 * with the length of the string in either the argument or the context.
4761 *
4762 * @param[in] args Array of arguments.
4763 * @param[in] arg_count Count of elements in @p args.
4764 * @param[in,out] set Context and result set at the same time.
4765 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004766 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004767 */
4768static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004769xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004770{
4771 struct lysc_node_leaf *sleaf;
4772 LY_ERR rc = LY_SUCCESS;
4773
4774 if (options & LYXP_SCNODE_ALL) {
4775 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4776 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4777 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 +02004778 } else if (!warn_is_string_type(sleaf->type)) {
4779 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004780 }
4781 }
4782 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4783 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4784 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 +02004785 } else if (!warn_is_string_type(sleaf->type)) {
4786 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004787 }
4788 }
4789 set_scnode_clear_ctx(set);
4790 return rc;
4791 }
4792
4793 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004794 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004795 LY_CHECK_RET(rc);
4796 set_fill_number(set, strlen(args[0]->val.str));
4797 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004798 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004799 LY_CHECK_RET(rc);
4800 set_fill_number(set, strlen(set->val.str));
4801 }
4802
4803 return LY_SUCCESS;
4804}
4805
4806/**
4807 * @brief Execute the XPath substring(string, number, number?) function.
4808 * Returns LYXP_SET_STRING substring of the first argument starting
4809 * on the second argument index ending on the third argument index,
4810 * indexed from 1. For exact definition refer to
4811 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4812 *
4813 * @param[in] args Array of arguments.
4814 * @param[in] arg_count Count of elements in @p args.
4815 * @param[in,out] set Context and result set at the same time.
4816 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004817 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004818 */
4819static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004820xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004821{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004822 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004823 uint16_t str_start, str_len, pos;
4824 struct lysc_node_leaf *sleaf;
4825 LY_ERR rc = LY_SUCCESS;
4826
4827 if (options & LYXP_SCNODE_ALL) {
4828 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4829 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4830 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 +02004831 } else if (!warn_is_string_type(sleaf->type)) {
4832 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004833 }
4834 }
4835
4836 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4837 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4838 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 +02004839 } else if (!warn_is_numeric_type(sleaf->type)) {
4840 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004841 }
4842 }
4843
Michal Vasko69730152020-10-09 16:30:07 +02004844 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4845 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004846 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4847 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 +02004848 } else if (!warn_is_numeric_type(sleaf->type)) {
4849 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004850 }
4851 }
4852 set_scnode_clear_ctx(set);
4853 return rc;
4854 }
4855
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004856 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004857 LY_CHECK_RET(rc);
4858
4859 /* start */
4860 if (xpath_round(&args[1], 1, args[1], options)) {
4861 return -1;
4862 }
4863 if (isfinite(args[1]->val.num)) {
4864 start = args[1]->val.num - 1;
4865 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004866 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004867 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004868 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004869 }
4870
4871 /* len */
4872 if (arg_count == 3) {
4873 rc = xpath_round(&args[2], 1, args[2], options);
4874 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004875 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004876 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004877 } else if (isfinite(args[2]->val.num)) {
4878 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004879 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004880 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004881 }
4882 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004883 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004884 }
4885
4886 /* find matching character positions */
4887 str_start = 0;
4888 str_len = 0;
4889 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4890 if (pos < start) {
4891 ++str_start;
4892 } else if (pos < start + len) {
4893 ++str_len;
4894 } else {
4895 break;
4896 }
4897 }
4898
4899 set_fill_string(set, args[0]->val.str + str_start, str_len);
4900 return LY_SUCCESS;
4901}
4902
4903/**
4904 * @brief Execute the XPath substring-after(string, string) function.
4905 * Returns LYXP_SET_STRING with the string succeeding the occurance
4906 * of the second argument in the first or an empty string.
4907 *
4908 * @param[in] args Array of arguments.
4909 * @param[in] arg_count Count of elements in @p args.
4910 * @param[in,out] set Context and result set at the same time.
4911 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004912 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004913 */
4914static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004915xpath_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 +02004916{
4917 char *ptr;
4918 struct lysc_node_leaf *sleaf;
4919 LY_ERR rc = LY_SUCCESS;
4920
4921 if (options & LYXP_SCNODE_ALL) {
4922 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4923 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4924 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 +02004925 } else if (!warn_is_string_type(sleaf->type)) {
4926 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004927 }
4928 }
4929
4930 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4931 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4932 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 +02004933 } else if (!warn_is_string_type(sleaf->type)) {
4934 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004935 }
4936 }
4937 set_scnode_clear_ctx(set);
4938 return rc;
4939 }
4940
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004941 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004942 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004943 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004944 LY_CHECK_RET(rc);
4945
4946 ptr = strstr(args[0]->val.str, args[1]->val.str);
4947 if (ptr) {
4948 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4949 } else {
4950 set_fill_string(set, "", 0);
4951 }
4952
4953 return LY_SUCCESS;
4954}
4955
4956/**
4957 * @brief Execute the XPath substring-before(string, string) function.
4958 * Returns LYXP_SET_STRING with the string preceding the occurance
4959 * of the second argument in the first or an empty string.
4960 *
4961 * @param[in] args Array of arguments.
4962 * @param[in] arg_count Count of elements in @p args.
4963 * @param[in,out] set Context and result set at the same time.
4964 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004965 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004966 */
4967static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004968xpath_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 +02004969{
4970 char *ptr;
4971 struct lysc_node_leaf *sleaf;
4972 LY_ERR rc = LY_SUCCESS;
4973
4974 if (options & LYXP_SCNODE_ALL) {
4975 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4976 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4977 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 +02004978 } else if (!warn_is_string_type(sleaf->type)) {
4979 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004980 }
4981 }
4982
4983 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4984 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4985 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 +02004986 } else if (!warn_is_string_type(sleaf->type)) {
4987 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004988 }
4989 }
4990 set_scnode_clear_ctx(set);
4991 return rc;
4992 }
4993
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004994 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004995 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004996 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004997 LY_CHECK_RET(rc);
4998
4999 ptr = strstr(args[0]->val.str, args[1]->val.str);
5000 if (ptr) {
5001 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5002 } else {
5003 set_fill_string(set, "", 0);
5004 }
5005
5006 return LY_SUCCESS;
5007}
5008
5009/**
5010 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5011 * with the sum of all the nodes in the context.
5012 *
5013 * @param[in] args Array of arguments.
5014 * @param[in] arg_count Count of elements in @p args.
5015 * @param[in,out] set Context and result set at the same time.
5016 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005017 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005018 */
5019static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005020xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005021{
5022 long double num;
5023 char *str;
5024 uint16_t i;
5025 struct lyxp_set set_item;
5026 struct lysc_node_leaf *sleaf;
5027 LY_ERR rc = LY_SUCCESS;
5028
5029 if (options & LYXP_SCNODE_ALL) {
5030 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5031 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005032 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005033 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5034 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5035 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005036 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005037 } else if (!warn_is_numeric_type(sleaf->type)) {
5038 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005039 }
5040 }
5041 }
5042 }
5043 set_scnode_clear_ctx(set);
5044 return rc;
5045 }
5046
5047 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005048
5049 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005050 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005051 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005052 } else if (!args[0]->used) {
5053 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005054 }
5055
Michal Vasko5c4e5892019-11-14 12:31:38 +01005056 set_init(&set_item, set);
5057
Michal Vasko03ff5a72019-09-11 13:49:33 +02005058 set_item.type = LYXP_SET_NODE_SET;
5059 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5060 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5061
5062 set_item.used = 1;
5063 set_item.size = 1;
5064
5065 for (i = 0; i < args[0]->used; ++i) {
5066 set_item.val.nodes[0] = args[0]->val.nodes[i];
5067
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005068 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005069 LY_CHECK_RET(rc);
5070 num = cast_string_to_number(str);
5071 free(str);
5072 set->val.num += num;
5073 }
5074
5075 free(set_item.val.nodes);
5076
5077 return LY_SUCCESS;
5078}
5079
5080/**
5081 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5082 * with the text content of the nodes in the context.
5083 *
5084 * @param[in] args Array of arguments.
5085 * @param[in] arg_count Count of elements in @p args.
5086 * @param[in,out] set Context and result set at the same time.
5087 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005088 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005089 */
5090static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005091xpath_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 +02005092{
5093 uint32_t i;
5094
5095 if (options & LYXP_SCNODE_ALL) {
5096 set_scnode_clear_ctx(set);
5097 return LY_SUCCESS;
5098 }
5099
Michal Vasko03ff5a72019-09-11 13:49:33 +02005100 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005101 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005102 return LY_EVALID;
5103 }
5104
Michal Vaskod989ba02020-08-24 10:59:24 +02005105 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005106 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005107 case LYXP_NODE_NONE:
5108 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005109 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005110 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5111 set->val.nodes[i].type = LYXP_NODE_TEXT;
5112 ++i;
5113 break;
5114 }
Radek Krejci0f969882020-08-21 16:56:47 +02005115 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005116 case LYXP_NODE_ROOT:
5117 case LYXP_NODE_ROOT_CONFIG:
5118 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005119 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005120 set_remove_node(set, i);
5121 break;
5122 }
5123 }
5124
5125 return LY_SUCCESS;
5126}
5127
5128/**
5129 * @brief Execute the XPath translate(string, string, string) function.
5130 * Returns LYXP_SET_STRING with the first argument with the characters
5131 * from the second argument replaced by those on the corresponding
5132 * positions in the third argument.
5133 *
5134 * @param[in] args Array of arguments.
5135 * @param[in] arg_count Count of elements in @p args.
5136 * @param[in,out] set Context and result set at the same time.
5137 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005138 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005139 */
5140static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005141xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005142{
5143 uint16_t i, j, new_used;
5144 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005145 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005146 struct lysc_node_leaf *sleaf;
5147 LY_ERR rc = LY_SUCCESS;
5148
5149 if (options & LYXP_SCNODE_ALL) {
5150 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5151 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5152 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 +02005153 } else if (!warn_is_string_type(sleaf->type)) {
5154 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005155 }
5156 }
5157
5158 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5159 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5160 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 +02005161 } else if (!warn_is_string_type(sleaf->type)) {
5162 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005163 }
5164 }
5165
5166 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5167 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5168 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 +02005169 } else if (!warn_is_string_type(sleaf->type)) {
5170 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005171 }
5172 }
5173 set_scnode_clear_ctx(set);
5174 return rc;
5175 }
5176
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005177 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005178 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005179 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005180 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005181 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005182 LY_CHECK_RET(rc);
5183
5184 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5185 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5186 new_used = 0;
5187
5188 have_removed = 0;
5189 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005190 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005191
5192 for (j = 0; args[1]->val.str[j]; ++j) {
5193 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5194 /* removing this char */
5195 if (j >= strlen(args[2]->val.str)) {
5196 have_removed = 1;
5197 found = 1;
5198 break;
5199 }
5200 /* replacing this char */
5201 new[new_used] = args[2]->val.str[j];
5202 ++new_used;
5203 found = 1;
5204 break;
5205 }
5206 }
5207
5208 /* copying this char */
5209 if (!found) {
5210 new[new_used] = args[0]->val.str[i];
5211 ++new_used;
5212 }
5213 }
5214
5215 if (have_removed) {
5216 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5217 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5218 }
5219 new[new_used] = '\0';
5220
Michal Vaskod3678892020-05-21 10:06:58 +02005221 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005222 set->type = LYXP_SET_STRING;
5223 set->val.str = new;
5224
5225 return LY_SUCCESS;
5226}
5227
5228/**
5229 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5230 * with true value.
5231 *
5232 * @param[in] args Array of arguments.
5233 * @param[in] arg_count Count of elements in @p args.
5234 * @param[in,out] set Context and result set at the same time.
5235 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005236 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005237 */
5238static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005239xpath_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 +02005240{
5241 if (options & LYXP_SCNODE_ALL) {
5242 set_scnode_clear_ctx(set);
5243 return LY_SUCCESS;
5244 }
5245
5246 set_fill_boolean(set, 1);
5247 return LY_SUCCESS;
5248}
5249
5250/*
5251 * moveto functions
5252 *
5253 * They and only they actually change the context (set).
5254 */
5255
5256/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005257 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005258 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005259 * XPath @p set is expected to be a (sc)node set!
5260 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005261 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5262 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005263 * @param[in] set Set with general XPath context.
5264 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005265 * @param[out] moveto_mod Expected module of a matching node.
5266 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005267 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005268static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005269moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5270 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005271{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005272 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005273 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005274 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005275
Michal Vasko2104e9f2020-03-06 08:23:25 +01005276 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5277
Michal Vasko6346ece2019-09-24 13:12:53 +02005278 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5279 /* specific module */
5280 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005281 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005282
Michal Vasko004d3152020-06-11 19:59:22 +02005283 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005284 if (!mod || !mod->implemented) {
Michal Vasko2104e9f2020-03-06 08:23:25 +01005285 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005286 LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko2104e9f2020-03-06 08:23:25 +01005287 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005288 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko2104e9f2020-03-06 08:23:25 +01005289 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005290 return LY_EVALID;
5291 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005292
Michal Vasko6346ece2019-09-24 13:12:53 +02005293 *qname += pref_len + 1;
5294 *qname_len -= pref_len + 1;
5295 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5296 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005297 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005298 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005299 switch (set->format) {
5300 case LY_PREF_SCHEMA:
5301 case LY_PREF_SCHEMA_RESOLVED:
5302 /* current module */
5303 mod = set->cur_mod;
5304 break;
5305 case LY_PREF_JSON:
5306 /* inherit parent (context node) module */
5307 if (ctx_scnode) {
5308 mod = ctx_scnode->module;
5309 } else {
5310 mod = NULL;
5311 }
5312 break;
5313 case LY_PREF_XML:
5314 /* not defined */
5315 LOGINT_RET(set->ctx);
5316 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005317 }
5318
Michal Vasko6346ece2019-09-24 13:12:53 +02005319 *moveto_mod = mod;
5320 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005321}
5322
5323/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005324 * @brief Move context @p set to the root. Handles absolute path.
5325 * Result is LYXP_SET_NODE_SET.
5326 *
5327 * @param[in,out] set Set to use.
5328 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005329 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005330 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005331static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005332moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005333{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005334 if (!set) {
Michal Vaskob0099a92020-08-31 14:55:23 +02005335 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005336 }
5337
5338 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005339 set_scnode_clear_ctx(set);
Michal Vaskob0099a92020-08-31 14:55:23 +02005340 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005341 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005342 set->type = LYXP_SET_NODE_SET;
5343 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005344 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005345 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005346
5347 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005348}
5349
5350/**
5351 * @brief Check @p node as a part of NameTest processing.
5352 *
5353 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005354 * @param[in] ctx_node Context node.
5355 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005356 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005357 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005358 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005359 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5360 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005361 */
5362static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005363moveto_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 +01005364 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005365{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005366 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5367 switch (set->format) {
5368 case LY_PREF_SCHEMA:
5369 case LY_PREF_SCHEMA_RESOLVED:
5370 /* use current module */
5371 moveto_mod = set->cur_mod;
5372 break;
5373 case LY_PREF_JSON:
5374 /* inherit module of the context node, if any */
5375 if (ctx_node) {
5376 moveto_mod = ctx_node->schema->module;
5377 }
5378 break;
5379 case LY_PREF_XML:
5380 /* not defined */
5381 LOGINT(set->ctx);
5382 return LY_EINVAL;
5383 }
5384 }
5385
Michal Vasko03ff5a72019-09-11 13:49:33 +02005386 /* module check */
5387 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005388 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005389 }
5390
Michal Vasko5c4e5892019-11-14 12:31:38 +01005391 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005392 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005393 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005394 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5395 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005396 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005397 }
5398
5399 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005400 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005401 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005402 }
5403
Michal Vaskoa1424542019-11-14 16:08:52 +01005404 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005405 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005406 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005407 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005408
5409 /* match */
5410 return LY_SUCCESS;
5411}
5412
5413/**
5414 * @brief Check @p node as a part of schema NameTest processing.
5415 *
5416 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005417 * @param[in] ctx_scnode Context node.
5418 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005419 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005420 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005421 * @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 +02005422 */
5423static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005424moveto_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 +02005425 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005426{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005427 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5428 switch (set->format) {
5429 case LY_PREF_SCHEMA:
5430 case LY_PREF_SCHEMA_RESOLVED:
5431 /* use current module */
5432 moveto_mod = set->cur_mod;
5433 break;
5434 case LY_PREF_JSON:
5435 /* inherit module of the context node, if any */
5436 if (ctx_scnode) {
5437 moveto_mod = ctx_scnode->module;
5438 }
5439 break;
5440 case LY_PREF_XML:
5441 /* not defined */
5442 LOGINT(set->ctx);
5443 return LY_EINVAL;
5444 }
5445 }
5446
Michal Vasko03ff5a72019-09-11 13:49:33 +02005447 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005448 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005449 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005450 }
5451
5452 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005453 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005454 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005455 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005456 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005457 }
5458
5459 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005460 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005461 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005462 }
5463
5464 /* match */
5465 return LY_SUCCESS;
5466}
5467
5468/**
Michal Vaskod3678892020-05-21 10:06:58 +02005469 * @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 +02005470 *
5471 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005472 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005473 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005474 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005475 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5476 */
5477static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005478moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005479{
Michal Vaskof03ed032020-03-04 13:31:44 +01005480 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005481 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005482 LY_ERR rc;
5483
Michal Vaskod3678892020-05-21 10:06:58 +02005484 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005485 return LY_SUCCESS;
5486 }
5487
5488 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005489 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005490 return LY_EVALID;
5491 }
5492
Michal Vasko03ff5a72019-09-11 13:49:33 +02005493 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005494 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005495
5496 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 +01005497 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005498
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005499 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005500 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005501 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005502 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005503 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005504 ctx_node = set->val.nodes[i].node;
5505 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005506 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005507
Michal Vaskod3678892020-05-21 10:06:58 +02005508 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005509 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005510 if (rc == LY_SUCCESS) {
5511 if (!replaced) {
5512 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5513 replaced = 1;
5514 } else {
5515 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005516 }
Michal Vaskod3678892020-05-21 10:06:58 +02005517 ++i;
5518 } else if (rc == LY_EINCOMPLETE) {
5519 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005520 }
5521 }
5522
5523 if (!replaced) {
5524 /* no match */
5525 set_remove_node(set, i);
5526 }
5527 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005528
5529 return LY_SUCCESS;
5530}
5531
5532/**
Michal Vaskod3678892020-05-21 10:06:58 +02005533 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5534 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005535 *
5536 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005537 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005538 * @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 +01005539 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005540 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5541 */
5542static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005543moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5544 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005545{
Michal Vasko004d3152020-06-11 19:59:22 +02005546 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005547 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005548 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005549 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005550
Michal Vasko004d3152020-06-11 19:59:22 +02005551 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005552
5553 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005554 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005555 }
5556
5557 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005558 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005559 ret = LY_EVALID;
5560 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005561 }
5562
5563 /* context check for all the nodes since we have the schema node */
5564 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5565 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005566 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005567 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5568 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005569 lyxp_set_free_content(set);
5570 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005571 }
5572
5573 /* create specific data instance if needed */
5574 if (scnode->nodetype == LYS_LIST) {
5575 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5576 } else if (scnode->nodetype == LYS_LEAFLIST) {
5577 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005578 }
5579
5580 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005581 siblings = NULL;
5582
5583 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5584 assert(!set->val.nodes[i].node);
5585
5586 /* search in all the trees */
5587 siblings = set->tree;
5588 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5589 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005590 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005591 }
5592
5593 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005594 if (inst) {
5595 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005596 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005597 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005598 }
5599
5600 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005601 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005602 ret = LY_EINCOMPLETE;
5603 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005604 }
5605
5606 if (sub) {
5607 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005608 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005609 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005610 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005611 /* no match */
5612 set_remove_node(set, i);
5613 }
5614 }
5615
Michal Vasko004d3152020-06-11 19:59:22 +02005616cleanup:
5617 lyd_free_tree(inst);
5618 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005619}
5620
5621/**
5622 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5623 *
5624 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005625 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005626 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005627 * @param[in] options XPath options.
5628 * @return LY_ERR
5629 */
5630static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005631moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005632{
Radek Krejci857189e2020-09-01 13:26:36 +02005633 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005634 uint32_t getnext_opts;
5635 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005636 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005637 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005638 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005639
Michal Vaskod3678892020-05-21 10:06:58 +02005640 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005641 return LY_SUCCESS;
5642 }
5643
5644 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005645 LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005646 return LY_EVALID;
5647 }
5648
Michal Vaskocafad9d2019-11-07 15:20:03 +01005649 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005650 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005651 if (options & LYXP_SCNODE_OUTPUT) {
5652 getnext_opts |= LYS_GETNEXT_OUTPUT;
5653 }
5654
Michal Vasko03ff5a72019-09-11 13:49:33 +02005655 orig_used = set->used;
5656 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005657 uint32_t idx;
5658
Radek Krejcif13b87b2020-12-01 22:02:17 +01005659 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5660 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005661 continue;
5662 }
5663
5664 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005665 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005666 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005667 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005668 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005669
5670 start_parent = set->val.scnodes[i].scnode;
5671
5672 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 +02005673 /* 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 +02005674 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005675 mod_idx = 0;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005676 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005677 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005678 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005679 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005680 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005681 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5682
Michal Vasko03ff5a72019-09-11 13:49:33 +02005683 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005684 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005685 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005686 temp_ctx = 1;
5687 }
5688 }
5689 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005690 }
5691
Michal Vasko519fd602020-05-26 12:17:39 +02005692 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5693 iter = NULL;
5694 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005695 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005696 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5697
5698 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005699 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005700 temp_ctx = 1;
5701 }
5702 }
5703 }
5704 }
5705 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005706
5707 /* correct temporary in_ctx values */
5708 if (temp_ctx) {
5709 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005710 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5711 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005712 }
5713 }
5714 }
5715
5716 return LY_SUCCESS;
5717}
5718
5719/**
Michal Vaskod3678892020-05-21 10:06:58 +02005720 * @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 +02005721 * Context position aware.
5722 *
5723 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005724 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005725 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005726 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005727 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5728 */
5729static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005730moveto_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 +02005731{
5732 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005733 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005734 struct lyxp_set ret_set;
5735 LY_ERR rc;
5736
Michal Vaskod3678892020-05-21 10:06:58 +02005737 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005738 return LY_SUCCESS;
5739 }
5740
5741 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005742 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005743 return LY_EVALID;
5744 }
5745
Michal Vasko9f96a052020-03-10 09:41:45 +01005746 /* 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 +01005747 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005748 LY_CHECK_RET(rc);
5749
Michal Vasko6346ece2019-09-24 13:12:53 +02005750 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005751 set_init(&ret_set, set);
5752 for (i = 0; i < set->used; ++i) {
5753
5754 /* TREE DFS */
5755 start = set->val.nodes[i].node;
5756 for (elem = next = start; elem; elem = next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005757 rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005758 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005759 /* add matching node into result set */
5760 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5761 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5762 /* the node is a duplicate, we'll process it later in the set */
5763 goto skip_children;
5764 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005765 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005766 return rc;
5767 } else if (rc == LY_EINVAL) {
5768 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005769 }
5770
5771 /* TREE DFS NEXT ELEM */
5772 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005773 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005774 if (!next) {
5775skip_children:
5776 /* no children, so try siblings, but only if it's not the start,
5777 * that is considered to be the root and it's siblings are not traversed */
5778 if (elem != start) {
5779 next = elem->next;
5780 } else {
5781 break;
5782 }
5783 }
5784 while (!next) {
5785 /* no siblings, go back through the parents */
5786 if ((struct lyd_node *)elem->parent == start) {
5787 /* we are done, no next element to process */
5788 break;
5789 }
5790 /* parent is already processed, go to its sibling */
5791 elem = (struct lyd_node *)elem->parent;
5792 next = elem->next;
5793 }
5794 }
5795 }
5796
5797 /* make the temporary set the current one */
5798 ret_set.ctx_pos = set->ctx_pos;
5799 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005800 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005801 memcpy(set, &ret_set, sizeof *set);
5802
5803 return LY_SUCCESS;
5804}
5805
5806/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005807 * @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 +02005808 *
5809 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005810 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005811 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005812 * @param[in] options XPath options.
5813 * @return LY_ERR
5814 */
5815static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005816moveto_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 +02005817{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005818 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005819 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005820 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005821
Michal Vaskod3678892020-05-21 10:06:58 +02005822 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005823 return LY_SUCCESS;
5824 }
5825
5826 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005827 LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005828 return LY_EVALID;
5829 }
5830
Michal Vasko03ff5a72019-09-11 13:49:33 +02005831 orig_used = set->used;
5832 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005833 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5834 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005835 continue;
5836 }
5837
5838 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005839 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005840 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005841 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005842 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005843
5844 /* TREE DFS */
5845 start = set->val.scnodes[i].scnode;
5846 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005847 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5848 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005849 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005850 }
5851
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005852 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005853 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005854 uint32_t idx;
5855
5856 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005857 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005858 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005859 /* we will process it later in the set */
5860 goto skip_children;
5861 }
5862 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005863 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005864 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005865 } else if (rc == LY_EINVAL) {
5866 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005867 }
5868
5869next_iter:
5870 /* TREE DFS NEXT ELEM */
5871 /* select element for the next run - children first */
5872 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005873 if (!next) {
5874skip_children:
5875 /* no children, so try siblings, but only if it's not the start,
5876 * that is considered to be the root and it's siblings are not traversed */
5877 if (elem != start) {
5878 next = elem->next;
5879 } else {
5880 break;
5881 }
5882 }
5883 while (!next) {
5884 /* no siblings, go back through the parents */
5885 if (elem->parent == start) {
5886 /* we are done, no next element to process */
5887 break;
5888 }
5889 /* parent is already processed, go to its sibling */
5890 elem = elem->parent;
5891 next = elem->next;
5892 }
5893 }
5894 }
5895
5896 return LY_SUCCESS;
5897}
5898
5899/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005900 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005901 * Indirectly context position aware.
5902 *
5903 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005904 * @param[in] mod Matching metadata module, NULL for any.
5905 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005906 * @return LY_ERR
5907 */
5908static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005909moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005910{
Michal Vasko9f96a052020-03-10 09:41:45 +01005911 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005912
Michal Vaskod3678892020-05-21 10:06:58 +02005913 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005914 return LY_SUCCESS;
5915 }
5916
5917 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005918 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005919 return LY_EVALID;
5920 }
5921
Radek Krejci1deb5be2020-08-26 16:43:36 +02005922 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005923 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005924
5925 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5926 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005927 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005928 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005929
5930 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005931 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005932 continue;
5933 }
5934
Michal Vaskod3678892020-05-21 10:06:58 +02005935 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005936 /* match */
5937 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005938 set->val.meta[i].meta = sub;
5939 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005940 /* pos does not change */
5941 replaced = 1;
5942 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005943 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 +02005944 }
5945 ++i;
5946 }
5947 }
5948 }
5949
5950 if (!replaced) {
5951 /* no match */
5952 set_remove_node(set, i);
5953 }
5954 }
5955
5956 return LY_SUCCESS;
5957}
5958
5959/**
5960 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005961 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005962 *
5963 * @param[in,out] set1 Set to use for the result.
5964 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005965 * @return LY_ERR
5966 */
5967static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005968moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005969{
5970 LY_ERR rc;
5971
Michal Vaskod3678892020-05-21 10:06:58 +02005972 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005973 LOGVAL(set1->ctx, LY_VLOG_LYD, set1->cur_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005974 return LY_EVALID;
5975 }
5976
5977 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005978 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005979 return LY_SUCCESS;
5980 }
5981
Michal Vaskod3678892020-05-21 10:06:58 +02005982 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005983 memcpy(set1, set2, sizeof *set1);
5984 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005985 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005986 return LY_SUCCESS;
5987 }
5988
5989 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005990 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005991
5992 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005993 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005994 LY_CHECK_RET(rc);
5995
5996 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005997 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005998
5999 return LY_SUCCESS;
6000}
6001
6002/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006003 * @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 +02006004 * Context position aware.
6005 *
6006 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006007 * @param[in] mod Matching metadata module, NULL for any.
6008 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006009 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006010 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6011 */
6012static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006013moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006014{
Michal Vasko9f96a052020-03-10 09:41:45 +01006015 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006016 struct lyxp_set *set_all_desc = NULL;
6017 LY_ERR rc;
6018
Michal Vaskod3678892020-05-21 10:06:58 +02006019 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006020 return LY_SUCCESS;
6021 }
6022
6023 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006024 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006025 return LY_EVALID;
6026 }
6027
Michal Vasko03ff5a72019-09-11 13:49:33 +02006028 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6029 * but it likely won't be used much, so it's a waste of time */
6030 /* copy the context */
6031 set_all_desc = set_copy(set);
6032 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006033 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006034 if (rc != LY_SUCCESS) {
6035 lyxp_set_free(set_all_desc);
6036 return rc;
6037 }
6038 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006039 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006040 if (rc != LY_SUCCESS) {
6041 lyxp_set_free(set_all_desc);
6042 return rc;
6043 }
6044 lyxp_set_free(set_all_desc);
6045
Radek Krejci1deb5be2020-08-26 16:43:36 +02006046 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006047 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006048
6049 /* only attributes of an elem can be in the result, skip all the rest,
6050 * we have all attributes qualified in lyd tree */
6051 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006052 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006053 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006054 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006055 continue;
6056 }
6057
Michal Vaskod3678892020-05-21 10:06:58 +02006058 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006059 /* match */
6060 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006061 set->val.meta[i].meta = sub;
6062 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006063 /* pos does not change */
6064 replaced = 1;
6065 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006066 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 +02006067 }
6068 ++i;
6069 }
6070 }
6071 }
6072
6073 if (!replaced) {
6074 /* no match */
6075 set_remove_node(set, i);
6076 }
6077 }
6078
6079 return LY_SUCCESS;
6080}
6081
6082/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006083 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6084 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006085 *
6086 * @param[in] parent Current parent.
6087 * @param[in] parent_pos Position of @p parent.
6088 * @param[in] parent_type Node type of @p parent.
6089 * @param[in,out] to_set Set to use.
6090 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006091 * @param[in] options XPath options.
6092 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6093 */
6094static LY_ERR
6095moveto_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 +02006096 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006097{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006098 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006099 LY_ERR rc;
6100
6101 switch (parent_type) {
6102 case LYXP_NODE_ROOT:
6103 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006104 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006105
Michal Vasko61ac2f62020-05-25 12:39:51 +02006106 /* add all top-level nodes as elements */
6107 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006108 break;
6109 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006110 /* add just the text node of this term element node */
6111 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006112 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6113 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6114 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006115 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006116 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006117
6118 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006119 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006120 break;
6121 default:
6122 LOGINT_RET(parent->schema->module->ctx);
6123 }
6124
Michal Vasko61ac2f62020-05-25 12:39:51 +02006125 /* add all top-level nodes as elements */
6126 LY_LIST_FOR(first, iter) {
6127 /* context check */
6128 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6129 continue;
6130 }
6131
6132 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006133 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006134 return LY_EINCOMPLETE;
6135 }
6136
6137 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6138 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6139
6140 /* also add all the children of this node, recursively */
6141 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6142 LY_CHECK_RET(rc);
6143 }
6144 }
6145
Michal Vasko03ff5a72019-09-11 13:49:33 +02006146 return LY_SUCCESS;
6147}
6148
6149/**
6150 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6151 * (or LYXP_SET_EMPTY). Context position aware.
6152 *
6153 * @param[in,out] set Set to use.
6154 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6155 * @param[in] options XPath options.
6156 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6157 */
6158static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006159moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006160{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006161 struct lyxp_set ret_set;
6162 LY_ERR rc;
6163
Michal Vaskod3678892020-05-21 10:06:58 +02006164 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006165 return LY_SUCCESS;
6166 }
6167
6168 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006169 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006170 return LY_EVALID;
6171 }
6172
6173 /* nothing to do */
6174 if (!all_desc) {
6175 return LY_SUCCESS;
6176 }
6177
Michal Vasko03ff5a72019-09-11 13:49:33 +02006178 /* add all the children, they get added recursively */
6179 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006180 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006181 /* copy the current node to tmp */
6182 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6183
6184 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006185 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006186 continue;
6187 }
6188
Michal Vasko03ff5a72019-09-11 13:49:33 +02006189 /* add all the children */
6190 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 +02006191 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006192 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006193 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006194 return rc;
6195 }
6196 }
6197
6198 /* use the temporary set as the current one */
6199 ret_set.ctx_pos = set->ctx_pos;
6200 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006201 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006202 memcpy(set, &ret_set, sizeof *set);
6203
6204 return LY_SUCCESS;
6205}
6206
6207/**
6208 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6209 * (or LYXP_SET_EMPTY).
6210 *
6211 * @param[in,out] set Set to use.
6212 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6213 * @param[in] options XPath options.
6214 * @return LY_ERR
6215 */
6216static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006217moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006218{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006219 uint32_t getnext_opts;
6220 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006221 const struct lysc_node *iter, *start_parent;
6222 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006223
Michal Vaskod3678892020-05-21 10:06:58 +02006224 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006225 return LY_SUCCESS;
6226 }
6227
6228 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006229 LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006230 return LY_EVALID;
6231 }
6232
6233 /* nothing to do */
6234 if (!all_desc) {
6235 return LY_SUCCESS;
6236 }
6237
Michal Vasko519fd602020-05-26 12:17:39 +02006238 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006239 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006240 if (options & LYXP_SCNODE_OUTPUT) {
6241 getnext_opts |= LYS_GETNEXT_OUTPUT;
6242 }
6243
6244 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006245 for (uint32_t i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006246 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6247 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006248 continue;
6249 }
6250
Michal Vasko519fd602020-05-26 12:17:39 +02006251 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006252 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006253 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006254 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006255 }
6256
Michal Vasko519fd602020-05-26 12:17:39 +02006257 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006258
Michal Vasko519fd602020-05-26 12:17:39 +02006259 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6260 /* it can actually be in any module, it's all <running> */
6261 mod_idx = 0;
6262 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6263 iter = NULL;
6264 /* module may not be implemented */
6265 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6266 /* context check */
6267 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6268 continue;
6269 }
6270
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006271 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006272 /* throw away the insert index, we want to consider that node again, recursively */
6273 }
6274 }
6275
6276 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6277 iter = NULL;
6278 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006279 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006280 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006281 continue;
6282 }
6283
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006284 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006285 }
6286 }
6287 }
6288
6289 return LY_SUCCESS;
6290}
6291
6292/**
6293 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6294 * (or LYXP_SET_EMPTY). Context position aware.
6295 *
6296 * @param[in] set Set to use.
6297 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6298 * @param[in] options XPath options.
6299 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6300 */
6301static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006302moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006303{
6304 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006305 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006306 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006307
Michal Vaskod3678892020-05-21 10:06:58 +02006308 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006309 return LY_SUCCESS;
6310 }
6311
6312 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006313 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006314 return LY_EVALID;
6315 }
6316
6317 if (all_desc) {
6318 /* <path>//.. == <path>//./.. */
6319 rc = moveto_self(set, 1, options);
6320 LY_CHECK_RET(rc);
6321 }
6322
Radek Krejci1deb5be2020-08-26 16:43:36 +02006323 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006324 node = set->val.nodes[i].node;
6325
6326 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6327 new_node = (struct lyd_node *)node->parent;
6328 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6329 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006330 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6331 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006332 if (!new_node) {
6333 LOGINT_RET(set->ctx);
6334 }
6335 } else {
6336 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006337 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006338 continue;
6339 }
6340
Michal Vaskoa1424542019-11-14 16:08:52 +01006341 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006342 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 +02006343 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006344 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006345
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006346 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006347 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006348 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006349
Michal Vasko03ff5a72019-09-11 13:49:33 +02006350 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006351 /* 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 +02006352 new_type = LYXP_NODE_ELEM;
6353 }
6354
Michal Vasko03ff5a72019-09-11 13:49:33 +02006355 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006356 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006357 } else {
6358 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006359 }
6360 }
6361
Michal Vasko2caefc12019-11-14 16:07:56 +01006362 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006363 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006364
6365 return LY_SUCCESS;
6366}
6367
6368/**
6369 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6370 * (or LYXP_SET_EMPTY).
6371 *
6372 * @param[in] set Set to use.
6373 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6374 * @param[in] options XPath options.
6375 * @return LY_ERR
6376 */
6377static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006378moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006379{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006380 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006381 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006382 const struct lysc_node *node, *new_node;
6383 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006384
Michal Vaskod3678892020-05-21 10:06:58 +02006385 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006386 return LY_SUCCESS;
6387 }
6388
6389 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006390 LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006391 return LY_EVALID;
6392 }
6393
6394 if (all_desc) {
6395 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006396 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006397 }
6398
Michal Vasko03ff5a72019-09-11 13:49:33 +02006399 orig_used = set->used;
6400 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006401 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6402 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006403 continue;
6404 }
6405
6406 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006407 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006408 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006409 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006410 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006411
6412 node = set->val.scnodes[i].scnode;
6413
6414 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006415 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006416 } else {
6417 /* root does not have a parent */
6418 continue;
6419 }
6420
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006421 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006422 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006423 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006424
Michal Vasko03ff5a72019-09-11 13:49:33 +02006425 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006426 /* 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 +02006427 new_type = LYXP_NODE_ELEM;
6428 }
6429
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006430 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6431 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006432 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006433 temp_ctx = 1;
6434 }
6435 }
6436
6437 if (temp_ctx) {
6438 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006439 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6440 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006441 }
6442 }
6443 }
6444
6445 return LY_SUCCESS;
6446}
6447
6448/**
6449 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6450 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6451 *
6452 * @param[in,out] set1 Set to use for the result.
6453 * @param[in] set2 Set acting as the second operand for @p op.
6454 * @param[in] op Comparison operator to process.
6455 * @param[in] options XPath options.
6456 * @return LY_ERR
6457 */
6458static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006459moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006460{
6461 /*
6462 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6463 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6464 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6465 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6466 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6467 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6468 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6469 *
6470 * '=' or '!='
6471 * BOOLEAN + BOOLEAN
6472 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6473 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6474 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6475 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6476 * NUMBER + NUMBER
6477 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6478 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6479 * STRING + STRING
6480 *
6481 * '<=', '<', '>=', '>'
6482 * NUMBER + NUMBER
6483 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6484 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6485 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6486 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6487 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6488 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6489 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6490 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6491 */
6492 struct lyxp_set iter1, iter2;
6493 int result;
6494 int64_t i;
6495 LY_ERR rc;
6496
Michal Vasko004d3152020-06-11 19:59:22 +02006497 memset(&iter1, 0, sizeof iter1);
6498 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006499
6500 /* iterative evaluation with node-sets */
6501 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6502 if (set1->type == LYXP_SET_NODE_SET) {
6503 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006504 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006505 switch (set2->type) {
6506 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006507 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006508 break;
6509 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006510 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006511 break;
6512 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006513 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006514 break;
6515 }
6516 LY_CHECK_RET(rc);
6517
Michal Vasko4c7763f2020-07-27 17:40:37 +02006518 /* canonize set2 */
6519 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6520
6521 /* compare recursively */
6522 rc = moveto_op_comp(&iter1, &iter2, op, options);
6523 lyxp_set_free_content(&iter2);
6524 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006525
6526 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006527 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006528 set_fill_boolean(set1, 1);
6529 return LY_SUCCESS;
6530 }
6531 }
6532 } else {
6533 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006534 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006535 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006536 case LYXP_SET_NUMBER:
6537 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6538 break;
6539 case LYXP_SET_BOOLEAN:
6540 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6541 break;
6542 default:
6543 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6544 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006545 }
6546 LY_CHECK_RET(rc);
6547
Michal Vasko4c7763f2020-07-27 17:40:37 +02006548 /* canonize set1 */
6549 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 +02006550
Michal Vasko4c7763f2020-07-27 17:40:37 +02006551 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006552 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006553 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006554 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006555
6556 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006557 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006558 set_fill_boolean(set1, 1);
6559 return LY_SUCCESS;
6560 }
6561 }
6562 }
6563
6564 /* false for all nodes */
6565 set_fill_boolean(set1, 0);
6566 return LY_SUCCESS;
6567 }
6568
6569 /* first convert properly */
6570 if ((op[0] == '=') || (op[0] == '!')) {
6571 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006572 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6573 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006574 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006575 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006576 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006577 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006578 LY_CHECK_RET(rc);
6579 } /* else we have 2 strings */
6580 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006581 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006582 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006583 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006584 LY_CHECK_RET(rc);
6585 }
6586
6587 assert(set1->type == set2->type);
6588
6589 /* compute result */
6590 if (op[0] == '=') {
6591 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006592 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006593 } else if (set1->type == LYXP_SET_NUMBER) {
6594 result = (set1->val.num == set2->val.num);
6595 } else {
6596 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006597 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006598 }
6599 } else if (op[0] == '!') {
6600 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006601 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006602 } else if (set1->type == LYXP_SET_NUMBER) {
6603 result = (set1->val.num != set2->val.num);
6604 } else {
6605 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006606 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006607 }
6608 } else {
6609 assert(set1->type == LYXP_SET_NUMBER);
6610 if (op[0] == '<') {
6611 if (op[1] == '=') {
6612 result = (set1->val.num <= set2->val.num);
6613 } else {
6614 result = (set1->val.num < set2->val.num);
6615 }
6616 } else {
6617 if (op[1] == '=') {
6618 result = (set1->val.num >= set2->val.num);
6619 } else {
6620 result = (set1->val.num > set2->val.num);
6621 }
6622 }
6623 }
6624
6625 /* assign result */
6626 if (result) {
6627 set_fill_boolean(set1, 1);
6628 } else {
6629 set_fill_boolean(set1, 0);
6630 }
6631
6632 return LY_SUCCESS;
6633}
6634
6635/**
6636 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6637 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6638 *
6639 * @param[in,out] set1 Set to use for the result.
6640 * @param[in] set2 Set acting as the second operand for @p op.
6641 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006642 * @return LY_ERR
6643 */
6644static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006645moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006646{
6647 LY_ERR rc;
6648
6649 /* unary '-' */
6650 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006651 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006652 LY_CHECK_RET(rc);
6653 set1->val.num *= -1;
6654 lyxp_set_free(set2);
6655 return LY_SUCCESS;
6656 }
6657
6658 assert(set1 && set2);
6659
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006660 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006661 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006662 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006663 LY_CHECK_RET(rc);
6664
6665 switch (op[0]) {
6666 /* '+' */
6667 case '+':
6668 set1->val.num += set2->val.num;
6669 break;
6670
6671 /* '-' */
6672 case '-':
6673 set1->val.num -= set2->val.num;
6674 break;
6675
6676 /* '*' */
6677 case '*':
6678 set1->val.num *= set2->val.num;
6679 break;
6680
6681 /* 'div' */
6682 case 'd':
6683 set1->val.num /= set2->val.num;
6684 break;
6685
6686 /* 'mod' */
6687 case 'm':
6688 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6689 break;
6690
6691 default:
6692 LOGINT_RET(set1->ctx);
6693 }
6694
6695 return LY_SUCCESS;
6696}
6697
6698/*
6699 * eval functions
6700 *
6701 * They execute a parsed XPath expression on some data subtree.
6702 */
6703
6704/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006705 * @brief Evaluate Predicate. Logs directly on error.
6706 *
Michal Vaskod3678892020-05-21 10:06:58 +02006707 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006708 *
6709 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006710 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006711 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6712 * @param[in] options XPath options.
6713 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6714 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6715 */
6716static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006717eval_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 +02006718{
6719 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006720 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006721 uint32_t orig_pos, orig_size;
6722 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006723 struct lyxp_set set2;
6724 struct lyd_node *orig_parent;
6725
6726 /* '[' */
6727 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006728 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006729 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006730
6731 if (!set) {
6732only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006733 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006734 LY_CHECK_RET(rc);
6735 } else if (set->type == LYXP_SET_NODE_SET) {
6736 /* 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 +01006737 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006738
6739 /* empty set, nothing to evaluate */
6740 if (!set->used) {
6741 goto only_parse;
6742 }
6743
Michal Vasko004d3152020-06-11 19:59:22 +02006744 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006745 orig_pos = 0;
6746 orig_size = set->used;
6747 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006748 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006749 set_init(&set2, set);
6750 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6751 /* remember the node context position for position() and context size for last(),
6752 * predicates should always be evaluated with respect to the child axis (since we do
6753 * not support explicit axes) so we assign positions based on their parents */
6754 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6755 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6756 orig_pos = 1;
6757 } else {
6758 ++orig_pos;
6759 }
6760
6761 set2.ctx_pos = orig_pos;
6762 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006763 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006764
Michal Vasko004d3152020-06-11 19:59:22 +02006765 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006766 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006767 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006768 return rc;
6769 }
6770
6771 /* number is a position */
6772 if (set2.type == LYXP_SET_NUMBER) {
6773 if ((long long)set2.val.num == orig_pos) {
6774 set2.val.num = 1;
6775 } else {
6776 set2.val.num = 0;
6777 }
6778 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006779 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006780
6781 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006782 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006783 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006784 }
6785 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006786 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006787
6788 } else if (set->type == LYXP_SET_SCNODE_SET) {
6789 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006790 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006791 /* there is a currently-valid node */
6792 break;
6793 }
6794 }
6795 /* empty set, nothing to evaluate */
6796 if (i == set->used) {
6797 goto only_parse;
6798 }
6799
Michal Vasko004d3152020-06-11 19:59:22 +02006800 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006801
Michal Vasko03ff5a72019-09-11 13:49:33 +02006802 /* set special in_ctx to all the valid snodes */
6803 pred_in_ctx = set_scnode_new_in_ctx(set);
6804
6805 /* use the valid snodes one-by-one */
6806 for (i = 0; i < set->used; ++i) {
6807 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6808 continue;
6809 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006810 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006811
Michal Vasko004d3152020-06-11 19:59:22 +02006812 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006813
Michal Vasko004d3152020-06-11 19:59:22 +02006814 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006815 LY_CHECK_RET(rc);
6816
6817 set->val.scnodes[i].in_ctx = pred_in_ctx;
6818 }
6819
6820 /* restore the state as it was before the predicate */
6821 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006822 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
6823 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006824 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006825 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006826 }
6827 }
6828
6829 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006830 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006831 set_fill_set(&set2, set);
6832
Michal Vasko004d3152020-06-11 19:59:22 +02006833 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006834 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006835 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006836 return rc;
6837 }
6838
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006839 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006840 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006841 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006842 }
Michal Vaskod3678892020-05-21 10:06:58 +02006843 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006844 }
6845
6846 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006847 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006848 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006849 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006850 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006851
6852 return LY_SUCCESS;
6853}
6854
6855/**
Michal Vaskod3678892020-05-21 10:06:58 +02006856 * @brief Evaluate Literal. Logs directly on error.
6857 *
6858 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006859 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006860 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6861 */
6862static void
Michal Vasko40308e72020-10-20 16:38:40 +02006863eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006864{
6865 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006866 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006867 set_fill_string(set, "", 0);
6868 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006869 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 +02006870 }
6871 }
6872 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006873 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006874 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006875}
6876
6877/**
Michal Vasko004d3152020-06-11 19:59:22 +02006878 * @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 +02006879 *
Michal Vasko004d3152020-06-11 19:59:22 +02006880 * @param[in] exp Full parsed XPath expression.
6881 * @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 +02006882 * @param[in] ctx_node Found schema node as the context for the predicate.
6883 * @param[in] cur_mod Current module for the expression.
6884 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006885 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006886 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006887 * @param[out] predicates Parsed predicates.
6888 * @param[out] pred_type Type of @p predicates.
6889 * @return LY_SUCCESS on success,
6890 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006891 */
6892static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006893eval_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 +02006894 const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_PREFIX_FORMAT format, void *prefix_data,
6895 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006896{
Michal Vasko004d3152020-06-11 19:59:22 +02006897 LY_ERR ret = LY_SUCCESS;
6898 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006899 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006900 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006901 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006902 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006903
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006904 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006905
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006906 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006907 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006908 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006909 return LY_EINVAL;
6910 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006911 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 +02006912 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006913
Michal Vasko004d3152020-06-11 19:59:22 +02006914 /* learn where the predicates end */
6915 e_idx = *tok_idx;
6916 while (key_count) {
6917 /* '[' */
6918 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6919 return LY_EINVAL;
6920 }
6921 ++e_idx;
6922
6923 /* ']' */
6924 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6925 ++e_idx;
6926 }
6927 ++e_idx;
6928
6929 /* another presumably key predicate parsed */
6930 --key_count;
6931 }
Michal Vasko004d3152020-06-11 19:59:22 +02006932 } else {
6933 /* learn just where this single predicate ends */
6934 e_idx = *tok_idx;
6935
Michal Vaskod3678892020-05-21 10:06:58 +02006936 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006937 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6938 return LY_EINVAL;
6939 }
6940 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006941
6942 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006943 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6944 ++e_idx;
6945 }
6946 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006947 }
6948
Michal Vasko004d3152020-06-11 19:59:22 +02006949 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6950 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6951
6952 /* turn logging off */
6953 prev_lo = ly_log_options(0);
6954
6955 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006956 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
6957 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02006958
6959 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006960 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
6961 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006962 LY_CHECK_GOTO(ret, cleanup);
6963
6964 /* success, the predicate must include all the needed information for hash-based search */
6965 *tok_idx = e_idx;
6966
6967cleanup:
6968 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006969 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02006970 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006971}
6972
6973/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006974 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
6975 * data node(s) could be found using a single hash-based search.
6976 *
6977 * @param[in] node Next context node to check.
6978 * @param[in] name Expected node name.
6979 * @param[in] name_len Length of @p name.
6980 * @param[in] moveto_mod Expected node module.
6981 * @param[in] root_type XPath root type.
6982 * @param[in] no_prefix Whether the node name was unprefixed.
6983 * @param[in] format Prefix format.
6984 * @param[in,out] found Previously found node, is updated.
6985 * @return LY_SUCCESS on success,
6986 * @return LY_ENOT if the whole check failed and hashes cannot be used.
6987 */
6988static LY_ERR
6989eval_name_test_with_predicate_get_scnode(const struct lyd_node *node, const char *name, uint16_t name_len,
6990 const struct lys_module *moveto_mod, enum lyxp_node_type root_type, ly_bool no_prefix, LY_PREFIX_FORMAT format,
6991 const struct lysc_node **found)
6992{
6993 const struct lysc_node *scnode;
6994 const struct lys_module *mod;
6995 uint32_t idx = 0;
6996
6997continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02006998 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006999 if (!node) {
7000 if (no_prefix && (format == LY_PREF_JSON)) {
7001 /* search all modules for a single match */
7002 while ((mod = ly_ctx_get_module_iter(moveto_mod->ctx, &idx))) {
7003 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7004 if (scnode) {
7005 /* we have found a match */
7006 break;
7007 }
7008 }
7009
Michal Vasko7d1d0912020-10-16 08:38:30 +02007010 if (!scnode) {
7011 /* all modules searched */
7012 idx = 0;
7013 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007014 } else {
7015 /* search in top-level */
7016 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7017 }
7018 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
7019 if (no_prefix && (format == LY_PREF_JSON)) {
7020 /* we must adjust the module to inherit the one from the context node */
7021 moveto_mod = node->schema->module;
7022 }
7023
7024 /* search in children, do not repeat the same search */
7025 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007026 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007027
7028 /* additional context check */
7029 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7030 scnode = NULL;
7031 }
7032
7033 if (scnode) {
7034 if (*found) {
7035 /* we found a schema node with the same name but at different level, give up, too complicated
7036 * (more hash-based searches would be required, not supported) */
7037 return LY_ENOT;
7038 } else {
7039 /* remember the found schema node and continue to make sure it can be used */
7040 *found = scnode;
7041 }
7042 }
7043
7044 if (idx) {
7045 /* continue searching all the following models */
7046 goto continue_search;
7047 }
7048
7049 return LY_SUCCESS;
7050}
7051
7052/**
Michal Vaskod3678892020-05-21 10:06:58 +02007053 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7054 *
7055 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7056 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7057 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7058 *
7059 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007060 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007061 * @param[in] attr_axis Whether to search attributes or standard nodes.
7062 * @param[in] all_desc Whether to search all the descendants or children only.
7063 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7064 * @param[in] options XPath options.
7065 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7066 */
7067static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007068eval_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 +02007069 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007070{
Michal Vaskod3678892020-05-21 10:06:58 +02007071 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007072 const char *ncname, *ncname_dict = NULL;
7073 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007074 const struct lys_module *moveto_mod = NULL;
7075 const struct lysc_node *scnode = NULL, *ctx_scnode;
Michal Vasko004d3152020-06-11 19:59:22 +02007076 struct ly_path_predicate *predicates = NULL;
7077 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007078 LY_ERR rc = LY_SUCCESS;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007079 ly_bool no_prefix;
Michal Vaskod3678892020-05-21 10:06:58 +02007080
7081 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007082 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007083 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007084
7085 if (!set) {
7086 goto moveto;
7087 }
7088
Michal Vasko004d3152020-06-11 19:59:22 +02007089 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7090 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007091
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007092 /* get the first schema context node */
7093 if (set->used) {
7094 if (set->type == LYXP_SET_NODE_SET) {
7095 ctx_scnode = set->val.nodes[0].node ? set->val.nodes[0].node->schema : NULL;
7096 } else {
7097 ctx_scnode = set->val.scnodes[0].scnode;
7098 }
7099 } else {
7100 ctx_scnode = NULL;
7101 }
7102
7103 /* parse (and skip) module name, use any context node (if available) just so we get some moveto_mod if there
7104 * is no prefix for ::LY_PREF_JSON */
7105 rc = moveto_resolve_model(&ncname, &ncname_len, set, ctx_scnode, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007106 LY_CHECK_GOTO(rc, cleanup);
7107
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007108 if (ncname_len == exp->tok_len[*tok_idx - 1]) {
7109 /* remember that there is no prefix */
7110 no_prefix = 1;
7111 } else {
7112 no_prefix = 0;
7113 }
7114
Michal Vaskod3678892020-05-21 10:06:58 +02007115 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
7116 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007117 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007118 if (eval_name_test_with_predicate_get_scnode(set->val.nodes[i].node, ncname, ncname_len, moveto_mod,
7119 set->root_type, no_prefix, set->format, &scnode)) {
7120 /* check failed */
7121 scnode = NULL;
7122 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007123 }
7124 }
7125
Michal Vasko004d3152020-06-11 19:59:22 +02007126 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7127 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007128 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7129 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007130 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007131 scnode = NULL;
7132 }
7133 }
7134 }
7135
Michal Vasko004d3152020-06-11 19:59:22 +02007136 if (!scnode && moveto_mod) {
7137 /* we are not able to match based on a schema node and not all the modules match,
7138 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007139 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007140 }
7141
Michal Vasko97ea1632020-11-23 17:00:09 +01007142 if ((set->format == LY_PREF_JSON) && no_prefix) {
7143 /* do not set moveto_mod if there is no explicit prefix for JSON, it should be inherited from
7144 * the specific context node */
7145 moveto_mod = NULL;
7146 }
7147
Michal Vaskod3678892020-05-21 10:06:58 +02007148moveto:
7149 /* move to the attribute(s), data node(s), or schema node(s) */
7150 if (attr_axis) {
7151 if (set && (options & LYXP_SCNODE_ALL)) {
7152 set_scnode_clear_ctx(set);
7153 } else {
7154 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007155 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007156 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007157 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007158 }
7159 LY_CHECK_GOTO(rc, cleanup);
7160 }
7161 } else {
7162 if (set && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007163 int64_t i;
7164
Michal Vaskod3678892020-05-21 10:06:58 +02007165 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007166 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007167 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007168 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007169 }
7170 LY_CHECK_GOTO(rc, cleanup);
7171
7172 for (i = set->used - 1; i > -1; --i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007173 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM) {
Michal Vaskod3678892020-05-21 10:06:58 +02007174 break;
7175 }
7176 }
7177 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007178 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vasko90f12dc2020-12-03 14:20:42 +01007179 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".",
7180 ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007181 free(path);
7182 }
7183 } else {
7184 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007185 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007186 } else {
7187 if (scnode) {
7188 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007189 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007190 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007191 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007192 }
7193 }
7194 LY_CHECK_GOTO(rc, cleanup);
7195 }
7196 }
7197
7198 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007199 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7200 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007201 LY_CHECK_RET(rc);
7202 }
7203
7204cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007205 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007206 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007207 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007208 }
Michal Vaskod3678892020-05-21 10:06:58 +02007209 return rc;
7210}
7211
7212/**
7213 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7214 *
7215 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7216 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7217 * [8] NodeType ::= 'text' | 'node'
7218 *
7219 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007220 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007221 * @param[in] attr_axis Whether to search attributes or standard nodes.
7222 * @param[in] all_desc Whether to search all the descendants or children only.
7223 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7224 * @param[in] options XPath options.
7225 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7226 */
7227static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007228eval_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 +02007229 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007230{
7231 LY_ERR rc;
7232
7233 /* TODO */
7234 (void)attr_axis;
7235 (void)all_desc;
7236
7237 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007238 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007239 if (set->type == LYXP_SET_SCNODE_SET) {
7240 set_scnode_clear_ctx(set);
7241 /* just for the debug message below */
7242 set = NULL;
7243 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007244 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007245 rc = xpath_node(NULL, 0, set, options);
7246 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007247 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007248 rc = xpath_text(NULL, 0, set, options);
7249 }
7250 LY_CHECK_RET(rc);
7251 }
7252 }
7253 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007254 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007255 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007256
7257 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007258 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007259 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007260 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007261 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007262
7263 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007264 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007265 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007266 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007267 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007268
7269 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007270 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7271 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007272 LY_CHECK_RET(rc);
7273 }
7274
7275 return LY_SUCCESS;
7276}
7277
7278/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007279 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7280 *
7281 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7282 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007283 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007284 *
7285 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007286 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007287 * @param[in] all_desc Whether to search all the descendants or children only.
7288 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7289 * @param[in] options XPath options.
7290 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7291 */
7292static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007293eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7294 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007295{
Radek Krejci857189e2020-09-01 13:26:36 +02007296 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007297 LY_ERR rc;
7298
7299 goto step;
7300 do {
7301 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007302 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007303 all_desc = 0;
7304 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007305 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007306 all_desc = 1;
7307 }
7308 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007309 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007310 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007311
7312step:
Michal Vaskod3678892020-05-21 10:06:58 +02007313 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007314 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007315 attr_axis = 1;
7316
7317 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007318 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007319 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007320 } else {
7321 attr_axis = 0;
7322 }
7323
Michal Vasko03ff5a72019-09-11 13:49:33 +02007324 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007325 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007326 case LYXP_TOKEN_DOT:
7327 /* evaluate '.' */
7328 if (set && (options & LYXP_SCNODE_ALL)) {
7329 rc = moveto_scnode_self(set, all_desc, options);
7330 } else {
7331 rc = moveto_self(set, all_desc, options);
7332 }
7333 LY_CHECK_RET(rc);
7334 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007335 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007336 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007337 break;
7338
7339 case LYXP_TOKEN_DDOT:
7340 /* evaluate '..' */
7341 if (set && (options & LYXP_SCNODE_ALL)) {
7342 rc = moveto_scnode_parent(set, all_desc, options);
7343 } else {
7344 rc = moveto_parent(set, all_desc, options);
7345 }
7346 LY_CHECK_RET(rc);
7347 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007348 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007349 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007350 break;
7351
Michal Vasko03ff5a72019-09-11 13:49:33 +02007352 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007353 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007354 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007355 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007356 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007357
Michal Vaskod3678892020-05-21 10:06:58 +02007358 case LYXP_TOKEN_NODETYPE:
7359 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007360 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007361 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007362 break;
7363
7364 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007365 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007366 }
Michal Vasko004d3152020-06-11 19:59:22 +02007367 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007368
7369 return LY_SUCCESS;
7370}
7371
7372/**
7373 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7374 *
7375 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7376 *
7377 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007378 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007379 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7380 * @param[in] options XPath options.
7381 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7382 */
7383static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007384eval_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 +02007385{
Radek Krejci857189e2020-09-01 13:26:36 +02007386 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007387
7388 if (set) {
7389 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007390 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007391 }
7392
7393 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007394 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007395 /* evaluate '/' - deferred */
7396 all_desc = 0;
7397 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007398 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007399 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007400
Michal Vasko004d3152020-06-11 19:59:22 +02007401 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007402 return LY_SUCCESS;
7403 }
Michal Vasko004d3152020-06-11 19:59:22 +02007404 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007405 case LYXP_TOKEN_DOT:
7406 case LYXP_TOKEN_DDOT:
7407 case LYXP_TOKEN_AT:
7408 case LYXP_TOKEN_NAMETEST:
7409 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007410 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007411 break;
7412 default:
7413 break;
7414 }
7415
Michal Vasko03ff5a72019-09-11 13:49:33 +02007416 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007417 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007418 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7419 all_desc = 1;
7420 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007421 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007422 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007423
Michal Vaskob0099a92020-08-31 14:55:23 +02007424 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007425 }
7426
7427 return LY_SUCCESS;
7428}
7429
7430/**
7431 * @brief Evaluate FunctionCall. Logs directly on error.
7432 *
Michal Vaskod3678892020-05-21 10:06:58 +02007433 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007434 *
7435 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007436 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007437 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7438 * @param[in] options XPath options.
7439 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7440 */
7441static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007442eval_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 +02007443{
7444 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007445
Radek Krejci1deb5be2020-08-26 16:43:36 +02007446 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007447 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007448 struct lyxp_set **args = NULL, **args_aux;
7449
7450 if (set) {
7451 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007452 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007453 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007454 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007455 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007456 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007457 xpath_func = &xpath_sum;
7458 }
7459 break;
7460 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007461 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007462 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007463 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007464 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007465 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007466 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007467 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007468 xpath_func = &xpath_true;
7469 }
7470 break;
7471 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007472 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007473 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007474 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007475 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007476 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007477 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007478 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007479 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007480 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007481 xpath_func = &xpath_deref;
7482 }
7483 break;
7484 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007485 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007486 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007487 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007488 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007489 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007490 xpath_func = &xpath_string;
7491 }
7492 break;
7493 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007494 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007495 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007496 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007497 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007498 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007499 xpath_func = &xpath_current;
7500 }
7501 break;
7502 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007503 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007504 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007505 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007506 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007507 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007508 xpath_func = &xpath_re_match;
7509 }
7510 break;
7511 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007512 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007513 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007514 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007515 xpath_func = &xpath_translate;
7516 }
7517 break;
7518 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007519 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007520 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007521 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007522 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007523 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007524 xpath_func = &xpath_bit_is_set;
7525 }
7526 break;
7527 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007528 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007529 xpath_func = &xpath_starts_with;
7530 }
7531 break;
7532 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007533 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007534 xpath_func = &xpath_derived_from;
7535 }
7536 break;
7537 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007538 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007539 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007540 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007541 xpath_func = &xpath_string_length;
7542 }
7543 break;
7544 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007545 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007546 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007547 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007548 xpath_func = &xpath_substring_after;
7549 }
7550 break;
7551 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007552 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007553 xpath_func = &xpath_substring_before;
7554 }
7555 break;
7556 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007557 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007558 xpath_func = &xpath_derived_from_or_self;
7559 }
7560 break;
7561 }
7562
7563 if (!xpath_func) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007564 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007565 return LY_EVALID;
7566 }
7567 }
7568
7569 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007570 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007571 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007572
7573 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007574 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007575 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007576 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007577 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007578
7579 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007580 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007581 if (set) {
7582 args = malloc(sizeof *args);
7583 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7584 arg_count = 1;
7585 args[0] = set_copy(set);
7586 if (!args[0]) {
7587 rc = LY_EMEM;
7588 goto cleanup;
7589 }
7590
Michal Vasko004d3152020-06-11 19:59:22 +02007591 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007592 LY_CHECK_GOTO(rc, cleanup);
7593 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007594 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007595 LY_CHECK_GOTO(rc, cleanup);
7596 }
7597 }
Michal Vasko004d3152020-06-11 19:59:22 +02007598 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007599 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007600 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007601 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007602
7603 if (set) {
7604 ++arg_count;
7605 args_aux = realloc(args, arg_count * sizeof *args);
7606 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7607 args = args_aux;
7608 args[arg_count - 1] = set_copy(set);
7609 if (!args[arg_count - 1]) {
7610 rc = LY_EMEM;
7611 goto cleanup;
7612 }
7613
Michal Vasko004d3152020-06-11 19:59:22 +02007614 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007615 LY_CHECK_GOTO(rc, cleanup);
7616 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007617 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007618 LY_CHECK_GOTO(rc, cleanup);
7619 }
7620 }
7621
7622 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007623 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007624 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007625 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007626 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007627
7628 if (set) {
7629 /* evaluate function */
7630 rc = xpath_func(args, arg_count, set, options);
7631
7632 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007633 /* merge all nodes from arg evaluations */
7634 for (i = 0; i < arg_count; ++i) {
7635 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007636 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007637 }
7638 }
7639 } else {
7640 rc = LY_SUCCESS;
7641 }
7642
7643cleanup:
7644 for (i = 0; i < arg_count; ++i) {
7645 lyxp_set_free(args[i]);
7646 }
7647 free(args);
7648
7649 return rc;
7650}
7651
7652/**
7653 * @brief Evaluate Number. Logs directly on error.
7654 *
7655 * @param[in] ctx Context for errors.
7656 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007657 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007658 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7659 * @return LY_ERR
7660 */
7661static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007662eval_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 +02007663{
7664 long double num;
7665 char *endptr;
7666
7667 if (set) {
7668 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007669 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007670 if (errno) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007671 LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7672 LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007673 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007674 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007675 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007676 LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7677 LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007678 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007679 return LY_EVALID;
7680 }
7681
7682 set_fill_number(set, num);
7683 }
7684
7685 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007686 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007687 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007688 return LY_SUCCESS;
7689}
7690
7691/**
7692 * @brief Evaluate PathExpr. Logs directly on error.
7693 *
Michal Vaskod3678892020-05-21 10:06:58 +02007694 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007695 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7696 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7697 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007698 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007699 *
7700 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007701 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007702 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7703 * @param[in] options XPath options.
7704 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7705 */
7706static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007707eval_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 +02007708{
Radek Krejci857189e2020-09-01 13:26:36 +02007709 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007710 LY_ERR rc;
7711
Michal Vasko004d3152020-06-11 19:59:22 +02007712 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007713 case LYXP_TOKEN_PAR1:
7714 /* '(' Expr ')' */
7715
7716 /* '(' */
7717 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007718 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007719 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007720
7721 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007722 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007723 LY_CHECK_RET(rc);
7724
7725 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007726 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007727 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007728 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007729 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007730
7731 parent_pos_pred = 0;
7732 goto predicate;
7733
7734 case LYXP_TOKEN_DOT:
7735 case LYXP_TOKEN_DDOT:
7736 case LYXP_TOKEN_AT:
7737 case LYXP_TOKEN_NAMETEST:
7738 case LYXP_TOKEN_NODETYPE:
7739 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007740 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007741 LY_CHECK_RET(rc);
7742 break;
7743
7744 case LYXP_TOKEN_FUNCNAME:
7745 /* FunctionCall */
7746 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007747 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007748 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007749 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007750 }
7751 LY_CHECK_RET(rc);
7752
7753 parent_pos_pred = 1;
7754 goto predicate;
7755
Michal Vasko3e48bf32020-06-01 08:39:07 +02007756 case LYXP_TOKEN_OPER_PATH:
7757 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007758 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007759 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007760 LY_CHECK_RET(rc);
7761 break;
7762
7763 case LYXP_TOKEN_LITERAL:
7764 /* Literal */
7765 if (!set || (options & LYXP_SCNODE_ALL)) {
7766 if (set) {
7767 set_scnode_clear_ctx(set);
7768 }
Michal Vasko004d3152020-06-11 19:59:22 +02007769 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007770 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007771 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007772 }
7773
7774 parent_pos_pred = 1;
7775 goto predicate;
7776
7777 case LYXP_TOKEN_NUMBER:
7778 /* Number */
7779 if (!set || (options & LYXP_SCNODE_ALL)) {
7780 if (set) {
7781 set_scnode_clear_ctx(set);
7782 }
Michal Vasko004d3152020-06-11 19:59:22 +02007783 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007784 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007785 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007786 }
7787 LY_CHECK_RET(rc);
7788
7789 parent_pos_pred = 1;
7790 goto predicate;
7791
7792 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007793 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]),
Michal Vasko69730152020-10-09 16:30:07 +02007794 &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007795 return LY_EVALID;
7796 }
7797
7798 return LY_SUCCESS;
7799
7800predicate:
7801 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007802 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7803 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007804 LY_CHECK_RET(rc);
7805 }
7806
7807 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007808 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007809
7810 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007811 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007812 all_desc = 0;
7813 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007814 all_desc = 1;
7815 }
7816
7817 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007818 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007819 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007820
Michal Vasko004d3152020-06-11 19:59:22 +02007821 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007822 LY_CHECK_RET(rc);
7823 }
7824
7825 return LY_SUCCESS;
7826}
7827
7828/**
7829 * @brief Evaluate UnionExpr. Logs directly on error.
7830 *
Michal Vaskod3678892020-05-21 10:06:58 +02007831 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007832 *
7833 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007834 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007835 * @param[in] repeat How many times this expression is repeated.
7836 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7837 * @param[in] options XPath options.
7838 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7839 */
7840static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007841eval_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 +02007842{
7843 LY_ERR rc = LY_SUCCESS;
7844 struct lyxp_set orig_set, set2;
7845 uint16_t i;
7846
7847 assert(repeat);
7848
7849 set_init(&orig_set, set);
7850 set_init(&set2, set);
7851
7852 set_fill_set(&orig_set, set);
7853
Michal Vasko004d3152020-06-11 19:59:22 +02007854 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007855 LY_CHECK_GOTO(rc, cleanup);
7856
7857 /* ('|' PathExpr)* */
7858 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007859 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007860 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007861 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007862 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007863
7864 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007865 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007866 LY_CHECK_GOTO(rc, cleanup);
7867 continue;
7868 }
7869
7870 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007871 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007872 LY_CHECK_GOTO(rc, cleanup);
7873
7874 /* eval */
7875 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007876 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007877 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007878 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007879 LY_CHECK_GOTO(rc, cleanup);
7880 }
7881 }
7882
7883cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007884 lyxp_set_free_content(&orig_set);
7885 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007886 return rc;
7887}
7888
7889/**
7890 * @brief Evaluate UnaryExpr. Logs directly on error.
7891 *
Michal Vaskod3678892020-05-21 10:06:58 +02007892 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007893 *
7894 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007895 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007896 * @param[in] repeat How many times this expression is repeated.
7897 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7898 * @param[in] options XPath options.
7899 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7900 */
7901static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007902eval_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 +02007903{
7904 LY_ERR rc;
7905 uint16_t this_op, i;
7906
7907 assert(repeat);
7908
7909 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007910 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007911 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007912 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 +02007913
7914 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007915 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007916 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007917 }
7918
Michal Vasko004d3152020-06-11 19:59:22 +02007919 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007920 LY_CHECK_RET(rc);
7921
7922 if (set && (repeat % 2)) {
7923 if (options & LYXP_SCNODE_ALL) {
7924 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7925 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007926 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007927 LY_CHECK_RET(rc);
7928 }
7929 }
7930
7931 return LY_SUCCESS;
7932}
7933
7934/**
7935 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7936 *
Michal Vaskod3678892020-05-21 10:06:58 +02007937 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007938 * | MultiplicativeExpr '*' UnaryExpr
7939 * | MultiplicativeExpr 'div' UnaryExpr
7940 * | MultiplicativeExpr 'mod' UnaryExpr
7941 *
7942 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007943 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007944 * @param[in] repeat How many times this expression is repeated.
7945 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7946 * @param[in] options XPath options.
7947 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7948 */
7949static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007950eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
7951 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007952{
7953 LY_ERR rc;
7954 uint16_t this_op;
7955 struct lyxp_set orig_set, set2;
7956 uint16_t i;
7957
7958 assert(repeat);
7959
7960 set_init(&orig_set, set);
7961 set_init(&set2, set);
7962
7963 set_fill_set(&orig_set, set);
7964
Michal Vasko004d3152020-06-11 19:59:22 +02007965 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007966 LY_CHECK_GOTO(rc, cleanup);
7967
7968 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7969 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007970 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007971
Michal Vasko004d3152020-06-11 19:59:22 +02007972 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007973 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007974 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007975 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007976
7977 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007978 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007979 LY_CHECK_GOTO(rc, cleanup);
7980 continue;
7981 }
7982
7983 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007984 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007985 LY_CHECK_GOTO(rc, cleanup);
7986
7987 /* eval */
7988 if (options & LYXP_SCNODE_ALL) {
7989 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007990 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007991 set_scnode_clear_ctx(set);
7992 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007993 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007994 LY_CHECK_GOTO(rc, cleanup);
7995 }
7996 }
7997
7998cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007999 lyxp_set_free_content(&orig_set);
8000 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008001 return rc;
8002}
8003
8004/**
8005 * @brief Evaluate AdditiveExpr. Logs directly on error.
8006 *
Michal Vaskod3678892020-05-21 10:06:58 +02008007 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008008 * | AdditiveExpr '+' MultiplicativeExpr
8009 * | AdditiveExpr '-' MultiplicativeExpr
8010 *
8011 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008012 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008013 * @param[in] repeat How many times this expression is repeated.
8014 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8015 * @param[in] options XPath options.
8016 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8017 */
8018static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008019eval_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 +02008020{
8021 LY_ERR rc;
8022 uint16_t this_op;
8023 struct lyxp_set orig_set, set2;
8024 uint16_t i;
8025
8026 assert(repeat);
8027
8028 set_init(&orig_set, set);
8029 set_init(&set2, set);
8030
8031 set_fill_set(&orig_set, set);
8032
Michal Vasko004d3152020-06-11 19:59:22 +02008033 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008034 LY_CHECK_GOTO(rc, cleanup);
8035
8036 /* ('+' / '-' MultiplicativeExpr)* */
8037 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008038 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008039
Michal Vasko004d3152020-06-11 19:59:22 +02008040 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008041 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008042 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008043 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008044
8045 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008046 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008047 LY_CHECK_GOTO(rc, cleanup);
8048 continue;
8049 }
8050
8051 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008052 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008053 LY_CHECK_GOTO(rc, cleanup);
8054
8055 /* eval */
8056 if (options & LYXP_SCNODE_ALL) {
8057 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008058 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008059 set_scnode_clear_ctx(set);
8060 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008061 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008062 LY_CHECK_GOTO(rc, cleanup);
8063 }
8064 }
8065
8066cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008067 lyxp_set_free_content(&orig_set);
8068 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008069 return rc;
8070}
8071
8072/**
8073 * @brief Evaluate RelationalExpr. Logs directly on error.
8074 *
Michal Vaskod3678892020-05-21 10:06:58 +02008075 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008076 * | RelationalExpr '<' AdditiveExpr
8077 * | RelationalExpr '>' AdditiveExpr
8078 * | RelationalExpr '<=' AdditiveExpr
8079 * | RelationalExpr '>=' AdditiveExpr
8080 *
8081 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008082 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008083 * @param[in] repeat How many times this expression is repeated.
8084 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8085 * @param[in] options XPath options.
8086 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8087 */
8088static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008089eval_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 +02008090{
8091 LY_ERR rc;
8092 uint16_t this_op;
8093 struct lyxp_set orig_set, set2;
8094 uint16_t i;
8095
8096 assert(repeat);
8097
8098 set_init(&orig_set, set);
8099 set_init(&set2, set);
8100
8101 set_fill_set(&orig_set, set);
8102
Michal Vasko004d3152020-06-11 19:59:22 +02008103 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008104 LY_CHECK_GOTO(rc, cleanup);
8105
8106 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8107 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008108 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008109
Michal Vasko004d3152020-06-11 19:59:22 +02008110 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008111 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008112 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008113 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008114
8115 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008116 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008117 LY_CHECK_GOTO(rc, cleanup);
8118 continue;
8119 }
8120
8121 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008122 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008123 LY_CHECK_GOTO(rc, cleanup);
8124
8125 /* eval */
8126 if (options & LYXP_SCNODE_ALL) {
8127 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008128 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008129 set_scnode_clear_ctx(set);
8130 } else {
8131 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8132 LY_CHECK_GOTO(rc, cleanup);
8133 }
8134 }
8135
8136cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008137 lyxp_set_free_content(&orig_set);
8138 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008139 return rc;
8140}
8141
8142/**
8143 * @brief Evaluate EqualityExpr. Logs directly on error.
8144 *
Michal Vaskod3678892020-05-21 10:06:58 +02008145 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008146 * | EqualityExpr '!=' RelationalExpr
8147 *
8148 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008149 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008150 * @param[in] repeat How many times this expression is repeated.
8151 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8152 * @param[in] options XPath options.
8153 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8154 */
8155static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008156eval_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 +02008157{
8158 LY_ERR rc;
8159 uint16_t this_op;
8160 struct lyxp_set orig_set, set2;
8161 uint16_t i;
8162
8163 assert(repeat);
8164
8165 set_init(&orig_set, set);
8166 set_init(&set2, set);
8167
8168 set_fill_set(&orig_set, set);
8169
Michal Vasko004d3152020-06-11 19:59:22 +02008170 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008171 LY_CHECK_GOTO(rc, cleanup);
8172
8173 /* ('=' / '!=' RelationalExpr)* */
8174 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008175 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008176
Michal Vasko004d3152020-06-11 19:59:22 +02008177 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008178 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008179 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008180 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008181
8182 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008183 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008184 LY_CHECK_GOTO(rc, cleanup);
8185 continue;
8186 }
8187
8188 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008189 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008190 LY_CHECK_GOTO(rc, cleanup);
8191
8192 /* eval */
8193 if (options & LYXP_SCNODE_ALL) {
8194 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008195 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8196 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008197 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008198 set_scnode_clear_ctx(set);
8199 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008200 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8201 LY_CHECK_GOTO(rc, cleanup);
8202 }
8203 }
8204
8205cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008206 lyxp_set_free_content(&orig_set);
8207 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008208 return rc;
8209}
8210
8211/**
8212 * @brief Evaluate AndExpr. Logs directly on error.
8213 *
Michal Vaskod3678892020-05-21 10:06:58 +02008214 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008215 *
8216 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008217 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008218 * @param[in] repeat How many times this expression is repeated.
8219 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8220 * @param[in] options XPath options.
8221 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8222 */
8223static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008224eval_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 +02008225{
8226 LY_ERR rc;
8227 struct lyxp_set orig_set, set2;
8228 uint16_t i;
8229
8230 assert(repeat);
8231
8232 set_init(&orig_set, set);
8233 set_init(&set2, set);
8234
8235 set_fill_set(&orig_set, set);
8236
Michal Vasko004d3152020-06-11 19:59:22 +02008237 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008238 LY_CHECK_GOTO(rc, cleanup);
8239
8240 /* cast to boolean, we know that will be the final result */
8241 if (set && (options & LYXP_SCNODE_ALL)) {
8242 set_scnode_clear_ctx(set);
8243 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008244 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008245 }
8246
8247 /* ('and' EqualityExpr)* */
8248 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008249 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8250 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008251 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008252 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008253
8254 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008255 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8256 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008257 LY_CHECK_GOTO(rc, cleanup);
8258 continue;
8259 }
8260
8261 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008262 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008263 LY_CHECK_GOTO(rc, cleanup);
8264
8265 /* eval - just get boolean value actually */
8266 if (set->type == LYXP_SET_SCNODE_SET) {
8267 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008268 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008269 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008270 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008271 set_fill_set(set, &set2);
8272 }
8273 }
8274
8275cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008276 lyxp_set_free_content(&orig_set);
8277 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008278 return rc;
8279}
8280
8281/**
8282 * @brief Evaluate OrExpr. Logs directly on error.
8283 *
Michal Vaskod3678892020-05-21 10:06:58 +02008284 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008285 *
8286 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008287 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008288 * @param[in] repeat How many times this expression is repeated.
8289 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8290 * @param[in] options XPath options.
8291 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8292 */
8293static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008294eval_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 +02008295{
8296 LY_ERR rc;
8297 struct lyxp_set orig_set, set2;
8298 uint16_t i;
8299
8300 assert(repeat);
8301
8302 set_init(&orig_set, set);
8303 set_init(&set2, set);
8304
8305 set_fill_set(&orig_set, set);
8306
Michal Vasko004d3152020-06-11 19:59:22 +02008307 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008308 LY_CHECK_GOTO(rc, cleanup);
8309
8310 /* cast to boolean, we know that will be the final result */
8311 if (set && (options & LYXP_SCNODE_ALL)) {
8312 set_scnode_clear_ctx(set);
8313 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008314 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008315 }
8316
8317 /* ('or' AndExpr)* */
8318 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008319 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8320 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008321 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008322 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008323
8324 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008325 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8326 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008327 LY_CHECK_GOTO(rc, cleanup);
8328 continue;
8329 }
8330
8331 set_fill_set(&set2, &orig_set);
8332 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8333 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008334 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008335 LY_CHECK_GOTO(rc, cleanup);
8336
8337 /* eval - just get boolean value actually */
8338 if (set->type == LYXP_SET_SCNODE_SET) {
8339 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008340 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008341 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008342 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008343 set_fill_set(set, &set2);
8344 }
8345 }
8346
8347cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008348 lyxp_set_free_content(&orig_set);
8349 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008350 return rc;
8351}
8352
8353/**
Michal Vasko004d3152020-06-11 19:59:22 +02008354 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008355 *
8356 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008357 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008358 * @param[in] etype Expression type to evaluate.
8359 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8360 * @param[in] options XPath options.
8361 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8362 */
8363static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008364eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8365 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008366{
8367 uint16_t i, count;
8368 enum lyxp_expr_type next_etype;
8369 LY_ERR rc;
8370
8371 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008372 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008373 next_etype = LYXP_EXPR_NONE;
8374 } else {
8375 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008376 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008377
8378 /* select one-priority lower because etype expression called us */
8379 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008380 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008381 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008382 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008383 } else {
8384 next_etype = LYXP_EXPR_NONE;
8385 }
8386 }
8387
8388 /* decide what expression are we parsing based on the repeat */
8389 switch (next_etype) {
8390 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008391 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008392 break;
8393 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008394 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008395 break;
8396 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008397 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008398 break;
8399 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008400 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008401 break;
8402 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008403 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008404 break;
8405 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008406 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008407 break;
8408 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008409 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008410 break;
8411 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008412 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008413 break;
8414 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008415 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008416 break;
8417 default:
8418 LOGINT_RET(set->ctx);
8419 }
8420
8421 return rc;
8422}
8423
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008424/**
8425 * @brief Get root type.
8426 *
8427 * @param[in] ctx_node Context node.
8428 * @param[in] ctx_scnode Schema context node.
8429 * @param[in] options XPath options.
8430 * @return Root type.
8431 */
8432static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008433lyxp_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 +01008434{
Michal Vasko6b26e742020-07-17 15:02:10 +02008435 const struct lysc_node *op;
8436
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008437 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008438 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008439 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008440
8441 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008442 /* general root that can access everything */
8443 return LYXP_NODE_ROOT;
8444 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8445 /* root context node can access only config data (because we said so, it is unspecified) */
8446 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008447 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008448 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008449 }
8450
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008451 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008452 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008453 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008454
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008455 if (op || !(options & LYXP_SCHEMA)) {
8456 /* general root that can access everything */
8457 return LYXP_NODE_ROOT;
8458 } else if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008459 /* root context node can access only config data (because we said so, it is unspecified) */
8460 return LYXP_NODE_ROOT_CONFIG;
8461 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008462 return LYXP_NODE_ROOT;
8463}
8464
Michal Vasko03ff5a72019-09-11 13:49:33 +02008465LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008466lyxp_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 +02008467 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 +02008468{
Michal Vasko004d3152020-06-11 19:59:22 +02008469 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008470 LY_ERR rc;
8471
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008472 LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL);
8473 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8474 LOGARG(NULL, "Current module must be set if schema format is used.");
8475 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008476 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008477
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008478 if (tree) {
8479 /* adjust the pointer to be the first top-level sibling */
8480 while (tree->parent) {
8481 tree = lyd_parent(tree);
8482 }
8483 tree = lyd_first_sibling(tree);
8484 }
8485
Michal Vasko03ff5a72019-09-11 13:49:33 +02008486 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008487 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008488 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008489 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8490 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8491
8492 set->ctx = (struct ly_ctx *)(cur_mod ? cur_mod->ctx : (ctx_node ? LYD_CTX(ctx_node) : (tree ? LYD_CTX(tree) : NULL)));
8493 set->cur_node = ctx_node;
8494 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008495 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8496 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008497 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008498 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008499 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008500 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008501
8502 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008503 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008504 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008505 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008506 }
8507
Michal Vasko03ff5a72019-09-11 13:49:33 +02008508 return rc;
8509}
8510
8511#if 0
8512
8513/* full xml printing of set elements, not used currently */
8514
8515void
8516lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8517{
8518 uint32_t i;
8519 char *str_num;
8520 struct lyout out;
8521
8522 memset(&out, 0, sizeof out);
8523
8524 out.type = LYOUT_STREAM;
8525 out.method.f = f;
8526
8527 switch (set->type) {
8528 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008529 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008530 break;
8531 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008532 ly_print_(&out, "Boolean XPath set:\n");
8533 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008534 break;
8535 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008536 ly_print_(&out, "String XPath set:\n");
8537 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008538 break;
8539 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008540 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008541
8542 if (isnan(set->value.num)) {
8543 str_num = strdup("NaN");
8544 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8545 str_num = strdup("0");
8546 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8547 str_num = strdup("Infinity");
8548 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8549 str_num = strdup("-Infinity");
8550 } else if ((long long)set->value.num == set->value.num) {
8551 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8552 str_num = NULL;
8553 }
8554 } else {
8555 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8556 str_num = NULL;
8557 }
8558 }
8559 if (!str_num) {
8560 LOGMEM;
8561 return;
8562 }
Michal Vasko5233e962020-08-14 14:26:20 +02008563 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008564 free(str_num);
8565 break;
8566 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008567 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008568
8569 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008570 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008571 switch (set->node_type[i]) {
8572 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008573 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008574 break;
8575 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008576 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008577 break;
8578 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008579 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008580 break;
8581 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008582 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008583 break;
8584 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008585 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008586 break;
8587 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008588 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008589 break;
8590 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008591 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008592 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008593 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008594 break;
8595 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008596 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 +02008597 break;
8598 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008599 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 +02008600 break;
8601 }
8602 }
8603 break;
8604 }
8605}
8606
8607#endif
8608
8609LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008610lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008611{
8612 long double num;
8613 char *str;
8614 LY_ERR rc;
8615
8616 if (!set || (set->type == target)) {
8617 return LY_SUCCESS;
8618 }
8619
8620 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008621 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008622
8623 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008624 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008625 return LY_EINVAL;
8626 }
8627
8628 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008629 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008630 switch (set->type) {
8631 case LYXP_SET_NUMBER:
8632 if (isnan(set->val.num)) {
8633 set->val.str = strdup("NaN");
8634 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8635 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8636 set->val.str = strdup("0");
8637 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8638 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8639 set->val.str = strdup("Infinity");
8640 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8641 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8642 set->val.str = strdup("-Infinity");
8643 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8644 } else if ((long long)set->val.num == set->val.num) {
8645 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8646 LOGMEM_RET(set->ctx);
8647 }
8648 set->val.str = str;
8649 } else {
8650 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8651 LOGMEM_RET(set->ctx);
8652 }
8653 set->val.str = str;
8654 }
8655 break;
8656 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008657 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008658 set->val.str = strdup("true");
8659 } else {
8660 set->val.str = strdup("false");
8661 }
8662 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8663 break;
8664 case LYXP_SET_NODE_SET:
8665 assert(set->used);
8666
8667 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008668 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008669
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008670 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008671 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008672 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008673 set->val.str = str;
8674 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008675 default:
8676 LOGINT_RET(set->ctx);
8677 }
8678 set->type = LYXP_SET_STRING;
8679 }
8680
8681 /* to NUMBER */
8682 if (target == LYXP_SET_NUMBER) {
8683 switch (set->type) {
8684 case LYXP_SET_STRING:
8685 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008686 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008687 set->val.num = num;
8688 break;
8689 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008690 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008691 set->val.num = 1;
8692 } else {
8693 set->val.num = 0;
8694 }
8695 break;
8696 default:
8697 LOGINT_RET(set->ctx);
8698 }
8699 set->type = LYXP_SET_NUMBER;
8700 }
8701
8702 /* to BOOLEAN */
8703 if (target == LYXP_SET_BOOLEAN) {
8704 switch (set->type) {
8705 case LYXP_SET_NUMBER:
8706 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008707 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008708 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008709 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008710 }
8711 break;
8712 case LYXP_SET_STRING:
8713 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008714 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008715 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008716 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008717 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008718 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008719 }
8720 break;
8721 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008722 if (set->used) {
8723 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008724 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008725 } else {
8726 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008727 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008728 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008729 break;
8730 default:
8731 LOGINT_RET(set->ctx);
8732 }
8733 set->type = LYXP_SET_BOOLEAN;
8734 }
8735
Michal Vasko03ff5a72019-09-11 13:49:33 +02008736 return LY_SUCCESS;
8737}
8738
8739LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008740lyxp_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 +02008741 const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008742{
Michal Vasko004d3152020-06-11 19:59:22 +02008743 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008744
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008745 LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL);
8746 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8747 LOGARG(NULL, "Current module must be set if schema format is used.");
8748 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008749 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008750
8751 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008752 memset(set, 0, sizeof *set);
8753 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008754 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8755 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 +01008756 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008757
8758 set->ctx = cur_mod ? cur_mod->ctx : (ctx_scnode ? ctx_scnode->module->ctx : NULL);
8759 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008760 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008761 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8762 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008763 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008764 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008765 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008766
8767 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008768 return eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008769}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008770
8771API const char *
8772lyxp_get_expr(const struct lyxp_expr *path)
8773{
8774 if (!path) {
8775 return NULL;
8776 }
8777
8778 return path->expr;
8779}