blob: aa060fce5b904a5fe19945f19974d821ec5fbe96 [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
15
16/* needed by libmath functions isfinite(), isinf(), isnan(), signbit(), ... */
17#define _ISOC99_SOURCE
Radek Krejcib1646a92018-11-02 16:08:26 +010018
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010020
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010022#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010026#include <stdio.h>
27#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010028#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010029
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020031#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020032#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020033#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020034#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010035#include "out.h"
Radek Krejci7931b192020-06-25 17:05:03 +020036#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020037#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020038#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020039#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020040#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020041#include "tree.h"
42#include "tree_data_internal.h"
43#include "tree_schema_internal.h"
44#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020045
Michal Vasko004d3152020-06-11 19:59:22 +020046static 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 +020047static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype,
48 struct lyxp_set *set, uint32_t options);
Michal Vasko03ff5a72019-09-11 13:49:33 +020049
50/**
51 * @brief Print the type of an XPath \p set.
52 *
53 * @param[in] set Set to use.
54 * @return Set type string.
55 */
56static const char *
57print_set_type(struct lyxp_set *set)
58{
59 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020060 case LYXP_SET_NODE_SET:
61 return "node set";
62 case LYXP_SET_SCNODE_SET:
63 return "schema node set";
64 case LYXP_SET_BOOLEAN:
65 return "boolean";
66 case LYXP_SET_NUMBER:
67 return "number";
68 case LYXP_SET_STRING:
69 return "string";
70 }
71
72 return NULL;
73}
74
Michal Vasko24cddf82020-06-01 08:17:01 +020075const char *
76lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020077{
78 switch (tok) {
79 case LYXP_TOKEN_PAR1:
80 return "(";
81 case LYXP_TOKEN_PAR2:
82 return ")";
83 case LYXP_TOKEN_BRACK1:
84 return "[";
85 case LYXP_TOKEN_BRACK2:
86 return "]";
87 case LYXP_TOKEN_DOT:
88 return ".";
89 case LYXP_TOKEN_DDOT:
90 return "..";
91 case LYXP_TOKEN_AT:
92 return "@";
93 case LYXP_TOKEN_COMMA:
94 return ",";
95 case LYXP_TOKEN_NAMETEST:
96 return "NameTest";
97 case LYXP_TOKEN_NODETYPE:
98 return "NodeType";
99 case LYXP_TOKEN_FUNCNAME:
100 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200101 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200102 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200103 case LYXP_TOKEN_OPER_EQUAL:
104 return "Operator(Equal)";
105 case LYXP_TOKEN_OPER_NEQUAL:
106 return "Operator(Non-equal)";
107 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200108 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200109 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200110 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200111 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200112 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200113 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200114 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200115 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200116 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200117 case LYXP_TOKEN_LITERAL:
118 return "Literal";
119 case LYXP_TOKEN_NUMBER:
120 return "Number";
121 default:
122 LOGINT(NULL);
123 return "";
124 }
125}
126
127/**
128 * @brief Print the whole expression \p exp to debug output.
129 *
130 * @param[in] exp Expression to use.
131 */
132static void
Michal Vasko40308e72020-10-20 16:38:40 +0200133print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200134{
135 uint16_t i, j;
136 char tmp[128];
137
Radek Krejci52b6d512020-10-12 12:33:17 +0200138 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200139 return;
140 }
141
142 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
143 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200144 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200145 &exp->expr[exp->tok_pos[i]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200146 if (exp->repeat[i]) {
147 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
148 for (j = 1; exp->repeat[i][j]; ++j) {
149 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
150 }
151 strcat(tmp, ")");
152 }
153 LOGDBG(LY_LDGXPATH, tmp);
154 }
155}
156
157#ifndef NDEBUG
158
159/**
160 * @brief Print XPath set content to debug output.
161 *
162 * @param[in] set Set to print.
163 */
164static void
165print_set_debug(struct lyxp_set *set)
166{
167 uint32_t i;
168 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200169 struct lyxp_set_node *item;
170 struct lyxp_set_scnode *sitem;
171
Radek Krejci52b6d512020-10-12 12:33:17 +0200172 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200173 return;
174 }
175
176 switch (set->type) {
177 case LYXP_SET_NODE_SET:
178 LOGDBG(LY_LDGXPATH, "set NODE SET:");
179 for (i = 0; i < set->used; ++i) {
180 item = &set->val.nodes[i];
181
182 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100183 case LYXP_NODE_NONE:
184 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
185 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200186 case LYXP_NODE_ROOT:
187 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
188 break;
189 case LYXP_NODE_ROOT_CONFIG:
190 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
191 break;
192 case LYXP_NODE_ELEM:
Michal Vasko69730152020-10-09 16:30:07 +0200193 if ((item->node->schema->nodetype == LYS_LIST) &&
194 (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200195 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200196 item->node->schema->name, LYD_CANON_VALUE(lyd_child(item->node)));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200197 } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) {
198 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200199 item->node->schema->name, LYD_CANON_VALUE(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200200 } else {
201 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
202 }
203 break;
204 case LYXP_NODE_TEXT:
205 if (item->node->schema->nodetype & LYS_ANYDATA) {
206 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200207 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200208 } else {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200209 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 +0200210 }
211 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100212 case LYXP_NODE_META:
213 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 +0200214 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200215 break;
216 }
217 }
218 break;
219
220 case LYXP_SET_SCNODE_SET:
221 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
222 for (i = 0; i < set->used; ++i) {
223 sitem = &set->val.scnodes[i];
224
225 switch (sitem->type) {
226 case LYXP_NODE_ROOT:
227 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
228 break;
229 case LYXP_NODE_ROOT_CONFIG:
230 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
231 break;
232 case LYXP_NODE_ELEM:
233 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
234 break;
235 default:
236 LOGINT(NULL);
237 break;
238 }
239 }
240 break;
241
Michal Vasko03ff5a72019-09-11 13:49:33 +0200242 case LYXP_SET_BOOLEAN:
243 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200244 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200245 break;
246
247 case LYXP_SET_STRING:
248 LOGDBG(LY_LDGXPATH, "set STRING");
249 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
250 break;
251
252 case LYXP_SET_NUMBER:
253 LOGDBG(LY_LDGXPATH, "set NUMBER");
254
255 if (isnan(set->val.num)) {
256 str = strdup("NaN");
257 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
258 str = strdup("0");
259 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
260 str = strdup("Infinity");
261 } else if (isinf(set->val.num) && signbit(set->val.num)) {
262 str = strdup("-Infinity");
263 } else if ((long long)set->val.num == set->val.num) {
264 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
265 str = NULL;
266 }
267 } else {
268 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
269 str = NULL;
270 }
271 }
272 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
273
274 LOGDBG(LY_LDGXPATH, "\t%s", str);
275 free(str);
276 }
277}
278
279#endif
280
281/**
282 * @brief Realloc the string \p str.
283 *
284 * @param[in] ctx libyang context for logging.
285 * @param[in] needed How much free space is required.
286 * @param[in,out] str Pointer to the string to use.
287 * @param[in,out] used Used bytes in \p str.
288 * @param[in,out] size Allocated bytes in \p str.
289 * @return LY_ERR
290 */
291static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100292cast_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 +0200293{
294 if (*size - *used < needed) {
295 do {
296 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
297 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
298 return LY_EINVAL;
299 }
300 *size += LYXP_STRING_CAST_SIZE_STEP;
301 } while (*size - *used < needed);
302 *str = ly_realloc(*str, *size * sizeof(char));
303 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
304 }
305
306 return LY_SUCCESS;
307}
308
309/**
310 * @brief Cast nodes recursively to one string @p str.
311 *
312 * @param[in] node Node to cast.
313 * @param[in] fake_cont Whether to put the data into a "fake" container.
314 * @param[in] root_type Type of the XPath root.
315 * @param[in] indent Current indent.
316 * @param[in,out] str Resulting string.
317 * @param[in,out] used Used bytes in @p str.
318 * @param[in,out] size Allocated bytes in @p str.
319 * @return LY_ERR
320 */
321static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200322cast_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 +0200323 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200324{
Radek Krejci7f769d72020-07-11 23:13:56 +0200325 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200326 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200327 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200328 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200329 struct lyd_node_any *any;
330 LY_ERR rc;
331
332 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
333 return LY_SUCCESS;
334 }
335
336 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200337 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200338 LY_CHECK_RET(rc);
339 strcpy(*str + (*used - 1), "\n");
340 ++(*used);
341
342 ++indent;
343 }
344
345 switch (node->schema->nodetype) {
346 case LYS_CONTAINER:
347 case LYS_LIST:
348 case LYS_RPC:
349 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200350 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200351 LY_CHECK_RET(rc);
352 strcpy(*str + (*used - 1), "\n");
353 ++(*used);
354
Radek Krejcia1c1e542020-09-29 16:06:52 +0200355 for (child = lyd_child(node); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200356 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
357 LY_CHECK_RET(rc);
358 }
359
360 break;
361
362 case LYS_LEAF:
363 case LYS_LEAFLIST:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200364 value_str = LYD_CANON_VALUE(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200365
366 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200367 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 +0200368 memset(*str + (*used - 1), ' ', indent * 2);
369 *used += indent * 2;
370
371 /* print value */
372 if (*used == 1) {
373 sprintf(*str + (*used - 1), "%s", value_str);
374 *used += strlen(value_str);
375 } else {
376 sprintf(*str + (*used - 1), "%s\n", value_str);
377 *used += strlen(value_str) + 1;
378 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200379
380 break;
381
382 case LYS_ANYXML:
383 case LYS_ANYDATA:
384 any = (struct lyd_node_any *)node;
385 if (!(void *)any->value.tree) {
386 /* no content */
387 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200388 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200389 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200390 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100391
Michal Vasko60ea6352020-06-29 13:39:39 +0200392 if (any->value_type == LYD_ANYDATA_LYB) {
393 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200394 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 +0200395 /* successfully parsed */
396 free(any->value.mem);
397 any->value.tree = tree;
398 any->value_type = LYD_ANYDATA_DATATREE;
399 }
Radek Krejci7931b192020-06-25 17:05:03 +0200400 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200401 }
402
Michal Vasko03ff5a72019-09-11 13:49:33 +0200403 switch (any->value_type) {
404 case LYD_ANYDATA_STRING:
405 case LYD_ANYDATA_XML:
406 case LYD_ANYDATA_JSON:
407 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200408 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200409 break;
410 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200411 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200412 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200413 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100414 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200415 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200416 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200417 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200418 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200419 }
420 }
421
422 line = strtok_r(buf, "\n", &ptr);
423 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200424 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200425 if (rc != LY_SUCCESS) {
426 free(buf);
427 return rc;
428 }
429 memset(*str + (*used - 1), ' ', indent * 2);
430 *used += indent * 2;
431
432 strcpy(*str + (*used - 1), line);
433 *used += strlen(line);
434
435 strcpy(*str + (*used - 1), "\n");
436 *used += 1;
437 } while ((line = strtok_r(NULL, "\n", &ptr)));
438
439 free(buf);
440 break;
441
442 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200443 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200444 }
445
446 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200447 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200448 LY_CHECK_RET(rc);
449 strcpy(*str + (*used - 1), "\n");
450 ++(*used);
451
452 --indent;
453 }
454
455 return LY_SUCCESS;
456}
457
458/**
459 * @brief Cast an element into a string.
460 *
461 * @param[in] node Node to cast.
462 * @param[in] fake_cont Whether to put the data into a "fake" container.
463 * @param[in] root_type Type of the XPath root.
464 * @param[out] str Element cast to dynamically-allocated string.
465 * @return LY_ERR
466 */
467static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200468cast_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 +0200469{
470 uint16_t used, size;
471 LY_ERR rc;
472
473 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200474 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200475 (*str)[0] = '\0';
476 used = 1;
477 size = LYXP_STRING_CAST_SIZE_START;
478
479 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
480 if (rc != LY_SUCCESS) {
481 free(*str);
482 return rc;
483 }
484
485 if (size > used) {
486 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200487 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200488 }
489 return LY_SUCCESS;
490}
491
492/**
493 * @brief Cast a LYXP_SET_NODE_SET set into a string.
494 * Context position aware.
495 *
496 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200497 * @param[out] str Cast dynamically-allocated string.
498 * @return LY_ERR
499 */
500static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100501cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200502{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200503 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100504 case LYXP_NODE_NONE:
505 /* invalid */
506 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200507 case LYXP_NODE_ROOT:
508 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100509 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200510 case LYXP_NODE_ELEM:
511 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100512 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100513 case LYXP_NODE_META:
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200514 *str = strdup(set->val.meta[0].meta->value.canonical);
515 if (!*str) {
516 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200517 }
518 return LY_SUCCESS;
519 }
520
521 LOGINT_RET(set->ctx);
522}
523
524/**
525 * @brief Cast a string into an XPath number.
526 *
527 * @param[in] str String to use.
528 * @return Cast number.
529 */
530static long double
531cast_string_to_number(const char *str)
532{
533 long double num;
534 char *ptr;
535
536 errno = 0;
537 num = strtold(str, &ptr);
538 if (errno || *ptr) {
539 num = NAN;
540 }
541 return num;
542}
543
544/**
545 * @brief Callback for checking value equality.
546 *
Radek Krejci857189e2020-09-01 13:26:36 +0200547 * Implementation of ::values_equal_cb.
548 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200549 * @param[in] val1_p First value.
550 * @param[in] val2_p Second value.
551 * @param[in] mod Whether hash table is being modified.
552 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200553 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200554 */
Radek Krejci857189e2020-09-01 13:26:36 +0200555static ly_bool
556set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200557{
558 struct lyxp_set_hash_node *val1, *val2;
559
560 val1 = (struct lyxp_set_hash_node *)val1_p;
561 val2 = (struct lyxp_set_hash_node *)val2_p;
562
563 if ((val1->node == val2->node) && (val1->type == val2->type)) {
564 return 1;
565 }
566
567 return 0;
568}
569
570/**
571 * @brief Insert node and its hash into set.
572 *
573 * @param[in] set et to insert to.
574 * @param[in] node Node with hash.
575 * @param[in] type Node type.
576 */
577static void
578set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
579{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200580 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200581 uint32_t i, hash;
582 struct lyxp_set_hash_node hnode;
583
584 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
585 /* create hash table and add all the nodes */
586 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
587 for (i = 0; i < set->used; ++i) {
588 hnode.node = set->val.nodes[i].node;
589 hnode.type = set->val.nodes[i].type;
590
591 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
592 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
593 hash = dict_hash_multi(hash, NULL, 0);
594
595 r = lyht_insert(set->ht, &hnode, hash, NULL);
596 assert(!r);
597 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200598
Michal Vasko9d6befd2019-12-11 14:56:56 +0100599 if (hnode.node == node) {
600 /* it was just added, do not add it twice */
601 node = NULL;
602 }
603 }
604 }
605
606 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200607 /* add the new node into hash table */
608 hnode.node = node;
609 hnode.type = type;
610
611 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
612 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
613 hash = dict_hash_multi(hash, NULL, 0);
614
615 r = lyht_insert(set->ht, &hnode, hash, NULL);
616 assert(!r);
617 (void)r;
618 }
619}
620
621/**
622 * @brief Remove node and its hash from set.
623 *
624 * @param[in] set Set to remove from.
625 * @param[in] node Node to remove.
626 * @param[in] type Node type.
627 */
628static void
629set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
630{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200631 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200632 struct lyxp_set_hash_node hnode;
633 uint32_t hash;
634
635 if (set->ht) {
636 hnode.node = node;
637 hnode.type = type;
638
639 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
640 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
641 hash = dict_hash_multi(hash, NULL, 0);
642
643 r = lyht_remove(set->ht, &hnode, hash);
644 assert(!r);
645 (void)r;
646
647 if (!set->ht->used) {
648 lyht_free(set->ht);
649 set->ht = NULL;
650 }
651 }
652}
653
654/**
655 * @brief Check whether node is in set based on its hash.
656 *
657 * @param[in] set Set to search in.
658 * @param[in] node Node to search for.
659 * @param[in] type Node type.
660 * @param[in] skip_idx Index in @p set to skip.
661 * @return LY_ERR
662 */
663static LY_ERR
664set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
665{
666 struct lyxp_set_hash_node hnode, *match_p;
667 uint32_t hash;
668
669 hnode.node = node;
670 hnode.type = type;
671
672 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
673 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
674 hash = dict_hash_multi(hash, NULL, 0);
675
676 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
677 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
678 /* we found it on the index that should be skipped, find another */
679 hnode = *match_p;
680 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
681 /* none other found */
682 return LY_SUCCESS;
683 }
684 }
685
686 return LY_EEXIST;
687 }
688
689 /* not found */
690 return LY_SUCCESS;
691}
692
Michal Vaskod3678892020-05-21 10:06:58 +0200693void
694lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200695{
696 if (!set) {
697 return;
698 }
699
700 if (set->type == LYXP_SET_NODE_SET) {
701 free(set->val.nodes);
702 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200703 } else if (set->type == LYXP_SET_SCNODE_SET) {
704 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200705 lyht_free(set->ht);
706 } else {
707 if (set->type == LYXP_SET_STRING) {
708 free(set->val.str);
709 }
710 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200711 }
Michal Vaskod3678892020-05-21 10:06:58 +0200712
713 set->val.nodes = NULL;
714 set->used = 0;
715 set->size = 0;
716 set->ht = NULL;
717 set->ctx_pos = 0;
718 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200719}
720
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100721/**
722 * @brief Free dynamically-allocated set.
723 *
724 * @param[in] set Set to free.
725 */
726static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200727lyxp_set_free(struct lyxp_set *set)
728{
729 if (!set) {
730 return;
731 }
732
Michal Vaskod3678892020-05-21 10:06:58 +0200733 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200734 free(set);
735}
736
737/**
738 * @brief Initialize set context.
739 *
740 * @param[in] new Set to initialize.
741 * @param[in] set Arbitrary initialized set.
742 */
743static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200744set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200745{
746 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200747 if (set) {
748 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200749 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100750 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200751 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100752 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200753 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200754 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200755 new->prefix_data = set->prefix_data;
Michal Vasko02a77382019-09-12 11:47:35 +0200756 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200757}
758
759/**
760 * @brief Create a deep copy of a set.
761 *
762 * @param[in] set Set to copy.
763 * @return Copy of @p set.
764 */
765static struct lyxp_set *
766set_copy(struct lyxp_set *set)
767{
768 struct lyxp_set *ret;
769 uint16_t i;
770
771 if (!set) {
772 return NULL;
773 }
774
775 ret = malloc(sizeof *ret);
776 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
777 set_init(ret, set);
778
779 if (set->type == LYXP_SET_SCNODE_SET) {
780 ret->type = set->type;
781
782 for (i = 0; i < set->used; ++i) {
Michal Vaskoba716542019-12-16 10:01:58 +0100783 if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200784 uint32_t idx;
785 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
786 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100787 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200788 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200789 lyxp_set_free(ret);
790 return NULL;
791 }
Michal Vaskoba716542019-12-16 10:01:58 +0100792 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200793 }
794 }
795 } else if (set->type == LYXP_SET_NODE_SET) {
796 ret->type = set->type;
797 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
798 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
799 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
800
801 ret->used = ret->size = set->used;
802 ret->ctx_pos = set->ctx_pos;
803 ret->ctx_size = set->ctx_size;
804 ret->ht = lyht_dup(set->ht);
805 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200806 memcpy(ret, set, sizeof *ret);
807 if (set->type == LYXP_SET_STRING) {
808 ret->val.str = strdup(set->val.str);
809 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
810 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200811 }
812
813 return ret;
814}
815
816/**
817 * @brief Fill XPath set with a string. Any current data are disposed of.
818 *
819 * @param[in] set Set to fill.
820 * @param[in] string String to fill into \p set.
821 * @param[in] str_len Length of \p string. 0 is a valid value!
822 */
823static void
824set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
825{
Michal Vaskod3678892020-05-21 10:06:58 +0200826 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200827
828 set->type = LYXP_SET_STRING;
829 if ((str_len == 0) && (string[0] != '\0')) {
830 string = "";
831 }
832 set->val.str = strndup(string, str_len);
833}
834
835/**
836 * @brief Fill XPath set with a number. Any current data are disposed of.
837 *
838 * @param[in] set Set to fill.
839 * @param[in] number Number to fill into \p set.
840 */
841static void
842set_fill_number(struct lyxp_set *set, long double number)
843{
Michal Vaskod3678892020-05-21 10:06:58 +0200844 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200845
846 set->type = LYXP_SET_NUMBER;
847 set->val.num = number;
848}
849
850/**
851 * @brief Fill XPath set with a boolean. Any current data are disposed of.
852 *
853 * @param[in] set Set to fill.
854 * @param[in] boolean Boolean to fill into \p set.
855 */
856static void
Radek Krejci857189e2020-09-01 13:26:36 +0200857set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200858{
Michal Vaskod3678892020-05-21 10:06:58 +0200859 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200860
861 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200862 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200863}
864
865/**
866 * @brief Fill XPath set with the value from another set (deep assign).
867 * Any current data are disposed of.
868 *
869 * @param[in] trg Set to fill.
870 * @param[in] src Source set to copy into \p trg.
871 */
872static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200873set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200874{
875 if (!trg || !src) {
876 return;
877 }
878
879 if (trg->type == LYXP_SET_NODE_SET) {
880 free(trg->val.nodes);
881 } else if (trg->type == LYXP_SET_STRING) {
882 free(trg->val.str);
883 }
884 set_init(trg, src);
885
886 if (src->type == LYXP_SET_SCNODE_SET) {
887 trg->type = LYXP_SET_SCNODE_SET;
888 trg->used = src->used;
889 trg->size = src->used;
890
891 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
892 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
893 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
894 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200895 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200896 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200897 set_fill_number(trg, src->val.num);
898 } else if (src->type == LYXP_SET_STRING) {
899 set_fill_string(trg, src->val.str, strlen(src->val.str));
900 } else {
901 if (trg->type == LYXP_SET_NODE_SET) {
902 free(trg->val.nodes);
903 } else if (trg->type == LYXP_SET_STRING) {
904 free(trg->val.str);
905 }
906
Michal Vaskod3678892020-05-21 10:06:58 +0200907 assert(src->type == LYXP_SET_NODE_SET);
908
909 trg->type = LYXP_SET_NODE_SET;
910 trg->used = src->used;
911 trg->size = src->used;
912 trg->ctx_pos = src->ctx_pos;
913 trg->ctx_size = src->ctx_size;
914
915 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
916 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
917 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
918 if (src->ht) {
919 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200920 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200921 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200922 }
923 }
924}
925
926/**
927 * @brief Clear context of all schema nodes.
928 *
929 * @param[in] set Set to clear.
930 */
931static void
932set_scnode_clear_ctx(struct lyxp_set *set)
933{
934 uint32_t i;
935
936 for (i = 0; i < set->used; ++i) {
937 if (set->val.scnodes[i].in_ctx == 1) {
938 set->val.scnodes[i].in_ctx = 0;
Michal Vasko5c4e5892019-11-14 12:31:38 +0100939 } else if (set->val.scnodes[i].in_ctx == -2) {
940 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200941 }
942 }
943}
944
945/**
946 * @brief Remove a node from a set. Removing last node changes
947 * set into LYXP_SET_EMPTY. Context position aware.
948 *
949 * @param[in] set Set to use.
950 * @param[in] idx Index from @p set of the node to be removed.
951 */
952static void
953set_remove_node(struct lyxp_set *set, uint32_t idx)
954{
955 assert(set && (set->type == LYXP_SET_NODE_SET));
956 assert(idx < set->used);
957
958 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
959
960 --set->used;
961 if (set->used) {
962 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
963 (set->used - idx) * sizeof *set->val.nodes);
964 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200965 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200966 }
967}
968
969/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100970 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200971 *
972 * @param[in] set Set to use.
973 * @param[in] idx Index from @p set of the node to be removed.
974 */
975static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100976set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200977{
978 assert(set && (set->type == LYXP_SET_NODE_SET));
979 assert(idx < set->used);
980
Michal Vasko2caefc12019-11-14 16:07:56 +0100981 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
982 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
983 }
984 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +0200985}
986
987/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100988 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +0200989 * set into LYXP_SET_EMPTY. Context position aware.
990 *
991 * @param[in] set Set to consolidate.
992 */
993static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100994set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +0200995{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +0200996 uint16_t i, orig_used, end = 0;
Michal Vasko57eab132019-09-24 11:46:26 +0200997 int32_t start;
998
Michal Vaskod3678892020-05-21 10:06:58 +0200999 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001000
1001 orig_used = set->used;
1002 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001003 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001004 start = -1;
1005 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001006 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001007 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001008 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001009 end = i;
1010 ++i;
1011 break;
1012 }
1013
1014 ++i;
1015 if (i == orig_used) {
1016 end = i;
1017 }
1018 } while (i < orig_used);
1019
1020 if (start > -1) {
1021 /* move the whole chunk of valid nodes together */
1022 if (set->used != (unsigned)start) {
1023 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1024 }
1025 set->used += end - start;
1026 }
1027 }
Michal Vasko57eab132019-09-24 11:46:26 +02001028}
1029
1030/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001031 * @brief Check for duplicates in a node set.
1032 *
1033 * @param[in] set Set to check.
1034 * @param[in] node Node to look for in @p set.
1035 * @param[in] node_type Type of @p node.
1036 * @param[in] skip_idx Index from @p set to skip.
1037 * @return LY_ERR
1038 */
1039static LY_ERR
1040set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1041{
1042 uint32_t i;
1043
Michal Vasko2caefc12019-11-14 16:07:56 +01001044 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001045 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1046 }
1047
1048 for (i = 0; i < set->used; ++i) {
1049 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1050 continue;
1051 }
1052
1053 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1054 return LY_EEXIST;
1055 }
1056 }
1057
1058 return LY_SUCCESS;
1059}
1060
Radek Krejci857189e2020-09-01 13:26:36 +02001061ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001062lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1063 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001064{
1065 uint32_t i;
1066
1067 for (i = 0; i < set->used; ++i) {
1068 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1069 continue;
1070 }
1071
1072 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001073 if (index_p) {
1074 *index_p = i;
1075 }
1076 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001077 }
1078 }
1079
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001080 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001081}
1082
Michal Vaskoecd62de2019-11-13 12:35:11 +01001083void
1084lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001085{
1086 uint32_t orig_used, i, j;
1087
Michal Vaskod3678892020-05-21 10:06:58 +02001088 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001089
Michal Vaskod3678892020-05-21 10:06:58 +02001090 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001091 return;
1092 }
1093
Michal Vaskod3678892020-05-21 10:06:58 +02001094 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001095 memcpy(set1, set2, sizeof *set1);
1096 return;
1097 }
1098
1099 if (set1->used + set2->used > set1->size) {
1100 set1->size = set1->used + set2->used;
1101 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1102 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1103 }
1104
1105 orig_used = set1->used;
1106
1107 for (i = 0; i < set2->used; ++i) {
1108 for (j = 0; j < orig_used; ++j) {
1109 /* detect duplicities */
1110 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1111 break;
1112 }
1113 }
1114
1115 if (j == orig_used) {
1116 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1117 ++set1->used;
1118 }
1119 }
1120
Michal Vaskod3678892020-05-21 10:06:58 +02001121 lyxp_set_free_content(set2);
1122 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001123}
1124
1125/**
1126 * @brief Insert a node into a set. Context position aware.
1127 *
1128 * @param[in] set Set to use.
1129 * @param[in] node Node to insert to @p set.
1130 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1131 * @param[in] node_type Node type of @p node.
1132 * @param[in] idx Index in @p set to insert into.
1133 */
1134static void
1135set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1136{
Michal Vaskod3678892020-05-21 10:06:58 +02001137 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001138
Michal Vaskod3678892020-05-21 10:06:58 +02001139 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001140 /* first item */
1141 if (idx) {
1142 /* no real harm done, but it is a bug */
1143 LOGINT(set->ctx);
1144 idx = 0;
1145 }
1146 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1147 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1148 set->type = LYXP_SET_NODE_SET;
1149 set->used = 0;
1150 set->size = LYXP_SET_SIZE_START;
1151 set->ctx_pos = 1;
1152 set->ctx_size = 1;
1153 set->ht = NULL;
1154 } else {
1155 /* not an empty set */
1156 if (set->used == set->size) {
1157
1158 /* set is full */
1159 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1160 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1161 set->size += LYXP_SET_SIZE_STEP;
1162 }
1163
1164 if (idx > set->used) {
1165 LOGINT(set->ctx);
1166 idx = set->used;
1167 }
1168
1169 /* make space for the new node */
1170 if (idx < set->used) {
1171 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1172 }
1173 }
1174
1175 /* finally assign the value */
1176 set->val.nodes[idx].node = (struct lyd_node *)node;
1177 set->val.nodes[idx].type = node_type;
1178 set->val.nodes[idx].pos = pos;
1179 ++set->used;
1180
Michal Vasko2caefc12019-11-14 16:07:56 +01001181 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1182 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1183 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001184}
1185
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001186LY_ERR
1187lyxp_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 +02001188{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001189 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001190
1191 assert(set->type == LYXP_SET_SCNODE_SET);
1192
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001193 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
1194 set->val.scnodes[index].in_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001195 } else {
1196 if (set->used == set->size) {
1197 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 +02001198 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001199 set->size += LYXP_SET_SIZE_STEP;
1200 }
1201
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001202 index = set->used;
1203 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1204 set->val.scnodes[index].type = node_type;
1205 set->val.scnodes[index].in_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001206 ++set->used;
1207 }
1208
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001209 if (index_p) {
1210 *index_p = index;
1211 }
1212
1213 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001214}
1215
1216/**
1217 * @brief Replace a node in a set with another. Context position aware.
1218 *
1219 * @param[in] set Set to use.
1220 * @param[in] node Node to insert to @p set.
1221 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1222 * @param[in] node_type Node type of @p node.
1223 * @param[in] idx Index in @p set of the node to replace.
1224 */
1225static void
1226set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1227{
1228 assert(set && (idx < set->used));
1229
Michal Vasko2caefc12019-11-14 16:07:56 +01001230 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1231 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1232 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001233 set->val.nodes[idx].node = (struct lyd_node *)node;
1234 set->val.nodes[idx].type = node_type;
1235 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001236 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1237 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1238 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001239}
1240
1241/**
1242 * @brief Set all nodes with ctx 1 to a new unique context value.
1243 *
1244 * @param[in] set Set to modify.
1245 * @return New context value.
1246 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001247static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001248set_scnode_new_in_ctx(struct lyxp_set *set)
1249{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001250 uint32_t i;
1251 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001252
1253 assert(set->type == LYXP_SET_SCNODE_SET);
1254
1255 ret_ctx = 3;
1256retry:
1257 for (i = 0; i < set->used; ++i) {
1258 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1259 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1260 goto retry;
1261 }
1262 }
1263 for (i = 0; i < set->used; ++i) {
1264 if (set->val.scnodes[i].in_ctx == 1) {
1265 set->val.scnodes[i].in_ctx = ret_ctx;
1266 }
1267 }
1268
1269 return ret_ctx;
1270}
1271
1272/**
1273 * @brief Get unique @p node position in the data.
1274 *
1275 * @param[in] node Node to find.
1276 * @param[in] node_type Node type of @p node.
1277 * @param[in] root Root node.
1278 * @param[in] root_type Type of the XPath @p root node.
1279 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1280 * be used to increase efficiency and start the DFS from this node.
1281 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1282 * @return Node position.
1283 */
1284static uint32_t
1285get_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 +02001286 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001287{
Michal Vasko56daf732020-08-10 10:57:18 +02001288 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001289 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001290 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001291
1292 assert(prev && prev_pos && !root->prev->next);
1293
1294 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1295 return 0;
1296 }
1297
1298 if (*prev) {
1299 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001300 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001301 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001302 goto dfs_search;
1303 }
1304
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001305 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001306 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001307dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001308 if (*prev && !elem) {
1309 /* resume previous DFS */
1310 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1311 LYD_TREE_DFS_continue = 0;
1312 }
1313
Michal Vasko03ff5a72019-09-11 13:49:33 +02001314 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001315 /* skip */
1316 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001317 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001318 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001319 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001320 break;
1321 }
Michal Vasko56daf732020-08-10 10:57:18 +02001322 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001323 }
Michal Vasko56daf732020-08-10 10:57:18 +02001324
1325 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001326 }
1327
1328 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001329 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001330 break;
1331 }
1332 }
1333
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001334 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001335 if (!(*prev)) {
1336 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001337 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001338 return 0;
1339 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001340 /* start the search again from the beginning */
1341 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001342
Michal Vasko56daf732020-08-10 10:57:18 +02001343 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001344 pos = 1;
1345 goto dfs_search;
1346 }
1347 }
1348
1349 /* remember the last found node for next time */
1350 *prev = node;
1351 *prev_pos = pos;
1352
1353 return pos;
1354}
1355
1356/**
1357 * @brief Assign (fill) missing node positions.
1358 *
1359 * @param[in] set Set to fill positions in.
1360 * @param[in] root Context root node.
1361 * @param[in] root_type Context root type.
1362 * @return LY_ERR
1363 */
1364static LY_ERR
1365set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1366{
1367 const struct lyd_node *prev = NULL, *tmp_node;
1368 uint32_t i, tmp_pos = 0;
1369
1370 for (i = 0; i < set->used; ++i) {
1371 if (!set->val.nodes[i].pos) {
1372 tmp_node = NULL;
1373 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001374 case LYXP_NODE_META:
1375 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001376 if (!tmp_node) {
1377 LOGINT_RET(root->schema->module->ctx);
1378 }
Radek Krejci0f969882020-08-21 16:56:47 +02001379 /* fallthrough */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001380 case LYXP_NODE_ELEM:
1381 case LYXP_NODE_TEXT:
1382 if (!tmp_node) {
1383 tmp_node = set->val.nodes[i].node;
1384 }
1385 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1386 break;
1387 default:
1388 /* all roots have position 0 */
1389 break;
1390 }
1391 }
1392 }
1393
1394 return LY_SUCCESS;
1395}
1396
1397/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001398 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001399 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001400 * @param[in] meta Metadata to use.
1401 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001402 */
1403static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001404get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001405{
1406 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001407 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001408
Michal Vasko9f96a052020-03-10 09:41:45 +01001409 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001410 ++pos;
1411 }
1412
Michal Vasko9f96a052020-03-10 09:41:45 +01001413 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001414 return pos;
1415}
1416
1417/**
1418 * @brief Compare 2 nodes in respect to XPath document order.
1419 *
1420 * @param[in] item1 1st node.
1421 * @param[in] item2 2nd node.
1422 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1423 */
1424static int
1425set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1426{
Michal Vasko9f96a052020-03-10 09:41:45 +01001427 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001428
1429 if (item1->pos < item2->pos) {
1430 return -1;
1431 }
1432
1433 if (item1->pos > item2->pos) {
1434 return 1;
1435 }
1436
1437 /* node positions are equal, the fun case */
1438
1439 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1440 /* special case since text nodes are actually saved as their parents */
1441 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1442 if (item1->type == LYXP_NODE_ELEM) {
1443 assert(item2->type == LYXP_NODE_TEXT);
1444 return -1;
1445 } else {
1446 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1447 return 1;
1448 }
1449 }
1450
Michal Vasko9f96a052020-03-10 09:41:45 +01001451 /* we need meta positions now */
1452 if (item1->type == LYXP_NODE_META) {
1453 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001454 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001455 if (item2->type == LYXP_NODE_META) {
1456 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001457 }
1458
Michal Vasko9f96a052020-03-10 09:41:45 +01001459 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001460 /* check for duplicates */
1461 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001462 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001463 return 0;
1464 }
1465
Michal Vasko9f96a052020-03-10 09:41:45 +01001466 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001467 /* elem is always first, 2nd node is after it */
1468 if (item1->type == LYXP_NODE_ELEM) {
1469 assert(item2->type != LYXP_NODE_ELEM);
1470 return -1;
1471 }
1472
Michal Vasko9f96a052020-03-10 09:41:45 +01001473 /* 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 +02001474 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001475 if (((item1->type == LYXP_NODE_TEXT) &&
1476 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1477 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1478 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1479 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001480 return 1;
1481 }
1482
Michal Vasko9f96a052020-03-10 09:41:45 +01001483 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001484 /* 2nd is after 1st */
1485 return -1;
1486}
1487
1488/**
1489 * @brief Set cast for comparisons.
1490 *
1491 * @param[in] trg Target set to cast source into.
1492 * @param[in] src Source set.
1493 * @param[in] type Target set type.
1494 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001495 * @return LY_ERR
1496 */
1497static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001498set_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 +02001499{
1500 assert(src->type == LYXP_SET_NODE_SET);
1501
1502 set_init(trg, src);
1503
1504 /* insert node into target set */
1505 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1506
1507 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001508 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001509}
1510
Michal Vasko4c7763f2020-07-27 17:40:37 +02001511/**
1512 * @brief Set content canonization for comparisons.
1513 *
1514 * @param[in] trg Target set to put the canononized source into.
1515 * @param[in] src Source set.
1516 * @param[in] xp_node Source XPath node/meta to use for canonization.
1517 * @return LY_ERR
1518 */
1519static LY_ERR
1520set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1521{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001522 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001523 struct lyd_value val;
1524 struct ly_err_item *err = NULL;
1525 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001526 LY_ERR rc;
1527
1528 /* is there anything to canonize even? */
1529 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1530 /* do we have a type to use for canonization? */
1531 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1532 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1533 } else if (xp_node->type == LYXP_NODE_META) {
1534 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1535 }
1536 }
1537 if (!type) {
1538 goto fill;
1539 }
1540
1541 if (src->type == LYXP_SET_NUMBER) {
1542 /* canonize number */
1543 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1544 LOGMEM(src->ctx);
1545 return LY_EMEM;
1546 }
1547 } else {
1548 /* canonize string */
1549 str = strdup(src->val.str);
1550 }
1551
1552 /* ignore errors, the value may not satisfy schema constraints */
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001553 rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_STORE_DYNAMIC, src->format, src->prefix_data,
1554 LYD_HINT_DATA, xp_node->node->schema, &val, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001555 ly_err_free(err);
1556 if (rc) {
1557 /* invalid value */
1558 free(str);
1559 goto fill;
1560 }
1561
Michal Vasko4c7763f2020-07-27 17:40:37 +02001562 /* use the canonized value */
1563 set_init(trg, src);
1564 trg->type = src->type;
1565 if (src->type == LYXP_SET_NUMBER) {
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001566 trg->val.num = strtold(val.canonical, &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001567 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1568 } else {
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001569 trg->val.str = strdup(val.canonical);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001570 }
1571 type->plugin->free(src->ctx, &val);
1572 return LY_SUCCESS;
1573
1574fill:
1575 /* no canonization needed/possible */
1576 set_fill_set(trg, src);
1577 return LY_SUCCESS;
1578}
1579
Michal Vasko03ff5a72019-09-11 13:49:33 +02001580#ifndef NDEBUG
1581
1582/**
1583 * @brief Bubble sort @p set into XPath document order.
1584 * Context position aware. Unused in the 'Release' build target.
1585 *
1586 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001587 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1588 */
1589static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001590set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001591{
1592 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001593 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001594 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001595 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001596 struct lyxp_set_node item;
1597 struct lyxp_set_hash_node hnode;
1598 uint64_t hash;
1599
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001600 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001601 return 0;
1602 }
1603
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001604 /* find first top-level node to be used as anchor for positions */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001605 for (root = set->cur_node; root->parent; root = (const struct lyd_node *)root->parent) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001606 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001607
1608 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001609 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001610 return -1;
1611 }
1612
1613 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1614 print_set_debug(set);
1615
1616 for (i = 0; i < set->used; ++i) {
1617 inverted = 0;
1618 change = 0;
1619
1620 for (j = 1; j < set->used - i; ++j) {
1621 /* compare node positions */
1622 if (inverted) {
1623 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1624 } else {
1625 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1626 }
1627
1628 /* swap if needed */
1629 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1630 change = 1;
1631
1632 item = set->val.nodes[j - 1];
1633 set->val.nodes[j - 1] = set->val.nodes[j];
1634 set->val.nodes[j] = item;
1635 } else {
1636 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1637 inverted = !inverted;
1638 }
1639 }
1640
1641 ++ret;
1642
1643 if (!change) {
1644 break;
1645 }
1646 }
1647
1648 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1649 print_set_debug(set);
1650
1651 /* check node hashes */
1652 if (set->used >= LYD_HT_MIN_ITEMS) {
1653 assert(set->ht);
1654 for (i = 0; i < set->used; ++i) {
1655 hnode.node = set->val.nodes[i].node;
1656 hnode.type = set->val.nodes[i].type;
1657
1658 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1659 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1660 hash = dict_hash_multi(hash, NULL, 0);
1661
1662 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1663 }
1664 }
1665
1666 return ret - 1;
1667}
1668
1669/**
1670 * @brief Remove duplicate entries in a sorted node set.
1671 *
1672 * @param[in] set Sorted set to check.
1673 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1674 */
1675static LY_ERR
1676set_sorted_dup_node_clean(struct lyxp_set *set)
1677{
1678 uint32_t i = 0;
1679 LY_ERR ret = LY_SUCCESS;
1680
1681 if (set->used > 1) {
1682 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001683 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1684 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001685 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001686 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001687 }
Michal Vasko57eab132019-09-24 11:46:26 +02001688 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001689 }
1690 }
1691
Michal Vasko2caefc12019-11-14 16:07:56 +01001692 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001693 return ret;
1694}
1695
1696#endif
1697
1698/**
1699 * @brief Merge 2 sorted sets into one.
1700 *
1701 * @param[in,out] trg Set to merge into. Duplicates are removed.
1702 * @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 +02001703 * @return LY_ERR
1704 */
1705static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001706set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001707{
1708 uint32_t i, j, k, count, dup_count;
1709 int cmp;
1710 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001711
Michal Vaskod3678892020-05-21 10:06:58 +02001712 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001713 return LY_EINVAL;
1714 }
1715
Michal Vaskod3678892020-05-21 10:06:58 +02001716 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001717 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001718 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001719 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001720 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001721 return LY_SUCCESS;
1722 }
1723
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001724 /* find first top-level node to be used as anchor for positions */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001725 for (root = trg->cur_node; root->parent; root = (const struct lyd_node *)root->parent) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001726 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001727
1728 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001729 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001730 return LY_EINT;
1731 }
1732
1733#ifndef NDEBUG
1734 LOGDBG(LY_LDGXPATH, "MERGE target");
1735 print_set_debug(trg);
1736 LOGDBG(LY_LDGXPATH, "MERGE source");
1737 print_set_debug(src);
1738#endif
1739
1740 /* make memory for the merge (duplicates are not detected yet, so space
1741 * will likely be wasted on them, too bad) */
1742 if (trg->size - trg->used < src->used) {
1743 trg->size = trg->used + src->used;
1744
1745 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1746 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1747 }
1748
1749 i = 0;
1750 j = 0;
1751 count = 0;
1752 dup_count = 0;
1753 do {
1754 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1755 if (!cmp) {
1756 if (!count) {
1757 /* duplicate, just skip it */
1758 ++i;
1759 ++j;
1760 } else {
1761 /* we are copying something already, so let's copy the duplicate too,
1762 * we are hoping that afterwards there are some more nodes to
1763 * copy and this way we can copy them all together */
1764 ++count;
1765 ++dup_count;
1766 ++i;
1767 ++j;
1768 }
1769 } else if (cmp < 0) {
1770 /* inserting src node into trg, just remember it for now */
1771 ++count;
1772 ++i;
1773
1774 /* insert the hash now */
1775 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1776 } else if (count) {
1777copy_nodes:
1778 /* time to actually copy the nodes, we have found the largest block of nodes */
1779 memmove(&trg->val.nodes[j + (count - dup_count)],
1780 &trg->val.nodes[j],
1781 (trg->used - j) * sizeof *trg->val.nodes);
1782 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1783
1784 trg->used += count - dup_count;
1785 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1786 j += count - dup_count;
1787
1788 count = 0;
1789 dup_count = 0;
1790 } else {
1791 ++j;
1792 }
1793 } while ((i < src->used) && (j < trg->used));
1794
1795 if ((i < src->used) || count) {
1796 /* insert all the hashes first */
1797 for (k = i; k < src->used; ++k) {
1798 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1799 }
1800
1801 /* loop ended, but we need to copy something at trg end */
1802 count += src->used - i;
1803 i = src->used;
1804 goto copy_nodes;
1805 }
1806
1807 /* we are inserting hashes before the actual node insert, which causes
1808 * situations when there were initially not enough items for a hash table,
1809 * but even after some were inserted, hash table was not created (during
1810 * insertion the number of items is not updated yet) */
1811 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1812 set_insert_node_hash(trg, NULL, 0);
1813 }
1814
1815#ifndef NDEBUG
1816 LOGDBG(LY_LDGXPATH, "MERGE result");
1817 print_set_debug(trg);
1818#endif
1819
Michal Vaskod3678892020-05-21 10:06:58 +02001820 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001821 return LY_SUCCESS;
1822}
1823
1824/*
1825 * (re)parse functions
1826 *
1827 * Parse functions parse the expression into
1828 * tokens (syntactic analysis).
1829 *
1830 * Reparse functions perform semantic analysis
1831 * (do not save the result, just a check) of
1832 * the expression and fill repeat indices.
1833 */
1834
Michal Vasko14676352020-05-29 11:35:55 +02001835LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001836lyxp_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 +02001837{
Michal Vasko004d3152020-06-11 19:59:22 +02001838 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001839 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001840 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1841 }
Michal Vasko14676352020-05-29 11:35:55 +02001842 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001843 }
1844
Michal Vasko004d3152020-06-11 19:59:22 +02001845 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001846 if (ctx) {
Michal Vasko0b468e62020-10-19 09:33:04 +02001847 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
1848 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001849 }
Michal Vasko14676352020-05-29 11:35:55 +02001850 return LY_ENOT;
1851 }
1852
1853 return LY_SUCCESS;
1854}
1855
Michal Vasko004d3152020-06-11 19:59:22 +02001856LY_ERR
1857lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1858{
1859 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1860
1861 /* skip the token */
1862 ++(*tok_idx);
1863
1864 return LY_SUCCESS;
1865}
1866
Michal Vasko14676352020-05-29 11:35:55 +02001867/* just like lyxp_check_token() but tests for 2 tokens */
1868static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001869exp_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 +02001870 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001871{
Michal Vasko004d3152020-06-11 19:59:22 +02001872 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001873 if (ctx) {
1874 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1875 }
1876 return LY_EINCOMPLETE;
1877 }
1878
Michal Vasko004d3152020-06-11 19:59:22 +02001879 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001880 if (ctx) {
Michal Vasko0b468e62020-10-19 09:33:04 +02001881 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
1882 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001883 }
1884 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001885 }
1886
1887 return LY_SUCCESS;
1888}
1889
1890/**
1891 * @brief Stack operation push on the repeat array.
1892 *
1893 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001894 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001895 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1896 */
1897static void
Michal Vasko004d3152020-06-11 19:59:22 +02001898exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001899{
1900 uint16_t i;
1901
Michal Vasko004d3152020-06-11 19:59:22 +02001902 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001903 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001904 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1905 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1906 exp->repeat[tok_idx][i] = repeat_op_idx;
1907 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001908 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001909 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1910 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1911 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001912 }
1913}
1914
1915/**
1916 * @brief Reparse Predicate. Logs directly on error.
1917 *
1918 * [7] Predicate ::= '[' Expr ']'
1919 *
1920 * @param[in] ctx Context for logging.
1921 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001922 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001923 * @return LY_ERR
1924 */
1925static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001926reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001927{
1928 LY_ERR rc;
1929
Michal Vasko004d3152020-06-11 19:59:22 +02001930 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001931 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001932 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001933
Michal Vasko004d3152020-06-11 19:59:22 +02001934 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001935 LY_CHECK_RET(rc);
1936
Michal Vasko004d3152020-06-11 19:59:22 +02001937 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001938 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001939 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001940
1941 return LY_SUCCESS;
1942}
1943
1944/**
1945 * @brief Reparse RelativeLocationPath. Logs directly on error.
1946 *
1947 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1948 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1949 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1950 *
1951 * @param[in] ctx Context for logging.
1952 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001953 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001954 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1955 */
1956static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001957reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001958{
1959 LY_ERR rc;
1960
Michal Vasko004d3152020-06-11 19:59:22 +02001961 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001962 LY_CHECK_RET(rc);
1963
1964 goto step;
1965 do {
1966 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001967 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001968
Michal Vasko004d3152020-06-11 19:59:22 +02001969 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001970 LY_CHECK_RET(rc);
1971step:
1972 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001973 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001974 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001975 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001976 break;
1977
1978 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001979 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001980 break;
1981
1982 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02001983 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001984
Michal Vasko004d3152020-06-11 19:59:22 +02001985 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001986 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001987 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001988 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko69730152020-10-09 16:30:07 +02001989 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001990 return LY_EVALID;
1991 }
Radek Krejci0f969882020-08-21 16:56:47 +02001992 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001993 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02001994 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001995 goto reparse_predicate;
1996 break;
1997
1998 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02001999 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002000
2001 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002002 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002003 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002004 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002005
2006 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002007 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002008 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002009 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002010
2011reparse_predicate:
2012 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002013 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2014 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002015 LY_CHECK_RET(rc);
2016 }
2017 break;
2018 default:
2019 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko69730152020-10-09 16:30:07 +02002020 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002021 return LY_EVALID;
2022 }
Michal Vasko004d3152020-06-11 19:59:22 +02002023 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002024
2025 return LY_SUCCESS;
2026}
2027
2028/**
2029 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2030 *
2031 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2032 *
2033 * @param[in] ctx Context for logging.
2034 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002035 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002036 * @return LY_ERR
2037 */
2038static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002039reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002040{
2041 LY_ERR rc;
2042
Michal Vasko004d3152020-06-11 19:59:22 +02002043 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 +02002044
2045 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002046 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002047 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002048 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002049
Michal Vasko004d3152020-06-11 19:59:22 +02002050 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002051 return LY_SUCCESS;
2052 }
Michal Vasko004d3152020-06-11 19:59:22 +02002053 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002054 case LYXP_TOKEN_DOT:
2055 case LYXP_TOKEN_DDOT:
2056 case LYXP_TOKEN_AT:
2057 case LYXP_TOKEN_NAMETEST:
2058 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002059 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002060 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002061 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002062 default:
2063 break;
2064 }
2065
Michal Vasko03ff5a72019-09-11 13:49:33 +02002066 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002067 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002068 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002069
Michal Vasko004d3152020-06-11 19:59:22 +02002070 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002071 LY_CHECK_RET(rc);
2072 }
2073
2074 return LY_SUCCESS;
2075}
2076
2077/**
2078 * @brief Reparse FunctionCall. Logs directly on error.
2079 *
2080 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2081 *
2082 * @param[in] ctx Context for logging.
2083 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002084 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002085 * @return LY_ERR
2086 */
2087static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002088reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002089{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002090 int8_t min_arg_count = -1;
2091 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002092 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002093 LY_ERR rc;
2094
Michal Vasko004d3152020-06-11 19:59:22 +02002095 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002096 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002097 func_tok_idx = *tok_idx;
2098 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002099 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002100 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002101 min_arg_count = 1;
2102 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002103 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002104 min_arg_count = 1;
2105 max_arg_count = 1;
2106 }
2107 break;
2108 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002109 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
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]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002113 min_arg_count = 0;
2114 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002115 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002116 min_arg_count = 0;
2117 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002118 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002119 min_arg_count = 0;
2120 max_arg_count = 0;
2121 }
2122 break;
2123 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002124 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002125 min_arg_count = 1;
2126 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002127 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002128 min_arg_count = 0;
2129 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002130 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002131 min_arg_count = 1;
2132 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002133 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 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]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002137 min_arg_count = 1;
2138 max_arg_count = 1;
2139 }
2140 break;
2141 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002142 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002143 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002144 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002145 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002146 min_arg_count = 0;
2147 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002148 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002149 min_arg_count = 0;
2150 max_arg_count = 1;
2151 }
2152 break;
2153 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002154 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002155 min_arg_count = 1;
2156 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002157 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002158 min_arg_count = 1;
2159 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002160 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002161 min_arg_count = 0;
2162 max_arg_count = 0;
2163 }
2164 break;
2165 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002166 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002167 min_arg_count = 2;
2168 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002169 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002170 min_arg_count = 0;
2171 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002172 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002173 min_arg_count = 2;
2174 max_arg_count = 2;
2175 }
2176 break;
2177 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002178 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002179 min_arg_count = 2;
2180 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002181 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002182 min_arg_count = 3;
2183 max_arg_count = 3;
2184 }
2185 break;
2186 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002187 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002188 min_arg_count = 0;
2189 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002190 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002191 min_arg_count = 1;
2192 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002193 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002194 min_arg_count = 2;
2195 max_arg_count = 2;
2196 }
2197 break;
2198 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002199 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002200 min_arg_count = 2;
2201 max_arg_count = 2;
2202 }
2203 break;
2204 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002205 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002206 min_arg_count = 2;
2207 max_arg_count = 2;
2208 }
2209 break;
2210 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002211 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002212 min_arg_count = 0;
2213 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002214 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002215 min_arg_count = 0;
2216 max_arg_count = 1;
2217 }
2218 break;
2219 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002220 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
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]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002224 min_arg_count = 2;
2225 max_arg_count = 2;
2226 }
2227 break;
2228 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002229 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002230 min_arg_count = 2;
2231 max_arg_count = 2;
2232 }
2233 break;
2234 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002235 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002236 min_arg_count = 2;
2237 max_arg_count = 2;
2238 }
2239 break;
2240 }
2241 if (min_arg_count == -1) {
Michal Vasko004d3152020-06-11 19:59:22 +02002242 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 +02002243 return LY_EINVAL;
2244 }
Michal Vasko004d3152020-06-11 19:59:22 +02002245 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002246
2247 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002248 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002249 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002250 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002251
2252 /* ( Expr ( ',' Expr )* )? */
2253 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002254 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002255 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002256 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002257 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002258 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002259 LY_CHECK_RET(rc);
2260 }
Michal Vasko004d3152020-06-11 19:59:22 +02002261 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2262 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002263
2264 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002265 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002266 LY_CHECK_RET(rc);
2267 }
2268
2269 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002270 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002271 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002272 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002273
Radek Krejci857189e2020-09-01 13:26:36 +02002274 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Michal Vasko004d3152020-06-11 19:59:22 +02002275 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 +02002276 &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002277 return LY_EVALID;
2278 }
2279
2280 return LY_SUCCESS;
2281}
2282
2283/**
2284 * @brief Reparse PathExpr. Logs directly on error.
2285 *
2286 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2287 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2288 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2289 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2290 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2291 *
2292 * @param[in] ctx Context for logging.
2293 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002294 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002295 * @return LY_ERR
2296 */
2297static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002298reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002299{
2300 LY_ERR rc;
2301
Michal Vasko004d3152020-06-11 19:59:22 +02002302 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002303 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002304 }
2305
Michal Vasko004d3152020-06-11 19:59:22 +02002306 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002307 case LYXP_TOKEN_PAR1:
2308 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002309 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002310
Michal Vasko004d3152020-06-11 19:59:22 +02002311 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002312 LY_CHECK_RET(rc);
2313
Michal Vasko004d3152020-06-11 19:59:22 +02002314 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002315 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002316 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002317 goto predicate;
2318 break;
2319 case LYXP_TOKEN_DOT:
2320 case LYXP_TOKEN_DDOT:
2321 case LYXP_TOKEN_AT:
2322 case LYXP_TOKEN_NAMETEST:
2323 case LYXP_TOKEN_NODETYPE:
2324 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002325 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002326 LY_CHECK_RET(rc);
2327 break;
2328 case LYXP_TOKEN_FUNCNAME:
2329 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002330 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002331 LY_CHECK_RET(rc);
2332 goto predicate;
2333 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002334 case LYXP_TOKEN_OPER_PATH:
2335 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002336 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002337 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002338 LY_CHECK_RET(rc);
2339 break;
2340 case LYXP_TOKEN_LITERAL:
2341 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002342 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002343 goto predicate;
2344 break;
2345 case LYXP_TOKEN_NUMBER:
2346 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002347 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002348 goto predicate;
2349 break;
2350 default:
2351 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko69730152020-10-09 16:30:07 +02002352 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002353 return LY_EVALID;
2354 }
2355
2356 return LY_SUCCESS;
2357
2358predicate:
2359 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002360 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2361 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002362 LY_CHECK_RET(rc);
2363 }
2364
2365 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002366 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002367
2368 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002369 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002370
Michal Vasko004d3152020-06-11 19:59:22 +02002371 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002372 LY_CHECK_RET(rc);
2373 }
2374
2375 return LY_SUCCESS;
2376}
2377
2378/**
2379 * @brief Reparse UnaryExpr. Logs directly on error.
2380 *
2381 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2382 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2383 *
2384 * @param[in] ctx Context for logging.
2385 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002386 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002387 * @return LY_ERR
2388 */
2389static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002390reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002391{
2392 uint16_t prev_exp;
2393 LY_ERR rc;
2394
2395 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002396 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002397 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2398 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002399 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002400 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002401 }
2402
2403 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002404 prev_exp = *tok_idx;
2405 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002406 LY_CHECK_RET(rc);
2407
2408 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002409 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002410 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002411 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002412
Michal Vasko004d3152020-06-11 19:59:22 +02002413 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002414 LY_CHECK_RET(rc);
2415 }
2416
2417 return LY_SUCCESS;
2418}
2419
2420/**
2421 * @brief Reparse AdditiveExpr. Logs directly on error.
2422 *
2423 * [15] AdditiveExpr ::= MultiplicativeExpr
2424 * | AdditiveExpr '+' MultiplicativeExpr
2425 * | AdditiveExpr '-' MultiplicativeExpr
2426 * [16] MultiplicativeExpr ::= UnaryExpr
2427 * | MultiplicativeExpr '*' UnaryExpr
2428 * | MultiplicativeExpr 'div' UnaryExpr
2429 * | MultiplicativeExpr 'mod' UnaryExpr
2430 *
2431 * @param[in] ctx Context for logging.
2432 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002433 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002434 * @return LY_ERR
2435 */
2436static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002437reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002438{
2439 uint16_t prev_add_exp, prev_mul_exp;
2440 LY_ERR rc;
2441
Michal Vasko004d3152020-06-11 19:59:22 +02002442 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002443 goto reparse_multiplicative_expr;
2444
2445 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002446 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2447 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002448 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002449 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002450
2451reparse_multiplicative_expr:
2452 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002453 prev_mul_exp = *tok_idx;
2454 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002455 LY_CHECK_RET(rc);
2456
2457 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002458 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2459 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002460 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002461 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002462
Michal Vasko004d3152020-06-11 19:59:22 +02002463 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002464 LY_CHECK_RET(rc);
2465 }
2466 }
2467
2468 return LY_SUCCESS;
2469}
2470
2471/**
2472 * @brief Reparse EqualityExpr. Logs directly on error.
2473 *
2474 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2475 * | EqualityExpr '!=' RelationalExpr
2476 * [14] RelationalExpr ::= AdditiveExpr
2477 * | RelationalExpr '<' AdditiveExpr
2478 * | RelationalExpr '>' AdditiveExpr
2479 * | RelationalExpr '<=' AdditiveExpr
2480 * | RelationalExpr '>=' AdditiveExpr
2481 *
2482 * @param[in] ctx Context for logging.
2483 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002484 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002485 * @return LY_ERR
2486 */
2487static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002488reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002489{
2490 uint16_t prev_eq_exp, prev_rel_exp;
2491 LY_ERR rc;
2492
Michal Vasko004d3152020-06-11 19:59:22 +02002493 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002494 goto reparse_additive_expr;
2495
2496 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002497 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002498 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002499 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002500
2501reparse_additive_expr:
2502 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002503 prev_rel_exp = *tok_idx;
2504 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002505 LY_CHECK_RET(rc);
2506
2507 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002508 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002509 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002510 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002511
Michal Vasko004d3152020-06-11 19:59:22 +02002512 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002513 LY_CHECK_RET(rc);
2514 }
2515 }
2516
2517 return LY_SUCCESS;
2518}
2519
2520/**
2521 * @brief Reparse OrExpr. Logs directly on error.
2522 *
2523 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2524 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2525 *
2526 * @param[in] ctx Context for logging.
2527 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002528 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002529 * @return LY_ERR
2530 */
2531static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002532reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002533{
2534 uint16_t prev_or_exp, prev_and_exp;
2535 LY_ERR rc;
2536
Michal Vasko004d3152020-06-11 19:59:22 +02002537 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002538 goto reparse_equality_expr;
2539
2540 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002541 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 +02002542 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002543 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002544
2545reparse_equality_expr:
2546 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002547 prev_and_exp = *tok_idx;
2548 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002549 LY_CHECK_RET(rc);
2550
2551 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002552 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 +02002553 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002554 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002555
Michal Vasko004d3152020-06-11 19:59:22 +02002556 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002557 LY_CHECK_RET(rc);
2558 }
2559 }
2560
2561 return LY_SUCCESS;
2562}
Radek Krejcib1646a92018-11-02 16:08:26 +01002563
2564/**
2565 * @brief Parse NCName.
2566 *
2567 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002568 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002569 */
Radek Krejcid4270262019-01-07 15:07:25 +01002570static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002571parse_ncname(const char *ncname)
2572{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002573 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002574 size_t size;
2575 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002576
2577 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2578 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2579 return len;
2580 }
2581
2582 do {
2583 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002584 if (!*ncname) {
2585 break;
2586 }
Radek Krejcid4270262019-01-07 15:07:25 +01002587 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002588 } while (is_xmlqnamechar(uc) && (uc != ':'));
2589
2590 return len;
2591}
2592
2593/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002594 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002595 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002596 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002597 * @param[in] exp Expression to use.
2598 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002599 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002600 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002601 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002602 */
2603static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002604exp_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 +01002605{
2606 uint32_t prev;
2607
2608 if (exp->used == exp->size) {
2609 prev = exp->size;
2610 exp->size += LYXP_EXPR_SIZE_STEP;
2611 if (prev > exp->size) {
2612 LOGINT(ctx);
2613 return LY_EINT;
2614 }
2615
2616 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2617 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2618 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2619 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2620 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2621 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2622 }
2623
2624 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002625 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002626 exp->tok_len[exp->used] = tok_len;
2627 ++exp->used;
2628 return LY_SUCCESS;
2629}
2630
2631void
Michal Vasko14676352020-05-29 11:35:55 +02002632lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002633{
2634 uint16_t i;
2635
2636 if (!expr) {
2637 return;
2638 }
2639
2640 lydict_remove(ctx, expr->expr);
2641 free(expr->tokens);
2642 free(expr->tok_pos);
2643 free(expr->tok_len);
2644 if (expr->repeat) {
2645 for (i = 0; i < expr->used; ++i) {
2646 free(expr->repeat[i]);
2647 }
2648 }
2649 free(expr->repeat);
2650 free(expr);
2651}
2652
Radek Krejcif03a9e22020-09-18 20:09:31 +02002653LY_ERR
2654lyxp_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 +01002655{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002656 LY_ERR ret = LY_SUCCESS;
2657 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002658 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002659 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002660 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002661 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002662
Radek Krejcif03a9e22020-09-18 20:09:31 +02002663 assert(expr_p);
2664
2665 if (!expr_str[0]) {
Michal Vasko004d3152020-06-11 19:59:22 +02002666 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002667 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002668 }
2669
2670 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002671 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002672 }
2673 if (expr_len > UINT16_MAX) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002674 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
2675 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002676 }
2677
2678 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002679 expr = calloc(1, sizeof *expr);
2680 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2681 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2682 expr->used = 0;
2683 expr->size = LYXP_EXPR_SIZE_START;
2684 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2685 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002686
Radek Krejcif03a9e22020-09-18 20:09:31 +02002687 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2688 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002689
Radek Krejcif03a9e22020-09-18 20:09:31 +02002690 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2691 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002692
Michal Vasko004d3152020-06-11 19:59:22 +02002693 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002694 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002695
Radek Krejcif03a9e22020-09-18 20:09:31 +02002696 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002697 ++parsed;
2698 }
2699
2700 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002701 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002702
2703 /* '(' */
2704 tok_len = 1;
2705 tok_type = LYXP_TOKEN_PAR1;
2706
Radek Krejcif03a9e22020-09-18 20:09:31 +02002707 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002708 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002709 if (((expr->tok_len[expr->used - 1] == 4) &&
2710 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2711 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2712 ((expr->tok_len[expr->used - 1] == 7) &&
2713 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002714 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002715 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002716 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002717 }
2718 prev_function_check = 0;
2719 }
2720
Radek Krejcif03a9e22020-09-18 20:09:31 +02002721 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002722
2723 /* ')' */
2724 tok_len = 1;
2725 tok_type = LYXP_TOKEN_PAR2;
2726
Radek Krejcif03a9e22020-09-18 20:09:31 +02002727 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002728
2729 /* '[' */
2730 tok_len = 1;
2731 tok_type = LYXP_TOKEN_BRACK1;
2732
Radek Krejcif03a9e22020-09-18 20:09:31 +02002733 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002734
2735 /* ']' */
2736 tok_len = 1;
2737 tok_type = LYXP_TOKEN_BRACK2;
2738
Radek Krejcif03a9e22020-09-18 20:09:31 +02002739 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002740
2741 /* '..' */
2742 tok_len = 2;
2743 tok_type = LYXP_TOKEN_DDOT;
2744
Radek Krejcif03a9e22020-09-18 20:09:31 +02002745 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002746
2747 /* '.' */
2748 tok_len = 1;
2749 tok_type = LYXP_TOKEN_DOT;
2750
Radek Krejcif03a9e22020-09-18 20:09:31 +02002751 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002752
2753 /* '@' */
2754 tok_len = 1;
2755 tok_type = LYXP_TOKEN_AT;
2756
Radek Krejcif03a9e22020-09-18 20:09:31 +02002757 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002758
2759 /* ',' */
2760 tok_len = 1;
2761 tok_type = LYXP_TOKEN_COMMA;
2762
Radek Krejcif03a9e22020-09-18 20:09:31 +02002763 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002764
2765 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002766 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2767 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Michal Vasko69730152020-10-09 16:30:07 +02002768 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
2769 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002770 ++tok_len;
2771 tok_type = LYXP_TOKEN_LITERAL;
2772
Radek Krejcif03a9e22020-09-18 20:09:31 +02002773 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002774
2775 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002776 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2777 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Michal Vasko69730152020-10-09 16:30:07 +02002778 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
2779 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002780 ++tok_len;
2781 tok_type = LYXP_TOKEN_LITERAL;
2782
Radek Krejcif03a9e22020-09-18 20:09:31 +02002783 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002784
2785 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002786 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2787 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002788 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002789 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002790 }
2791 tok_type = LYXP_TOKEN_NUMBER;
2792
Radek Krejcif03a9e22020-09-18 20:09:31 +02002793 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002794
2795 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002796 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002797 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002798 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002799 } else {
2800 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002801 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002802 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002803
Radek Krejcif03a9e22020-09-18 20:09:31 +02002804 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002805
Michal Vasko3e48bf32020-06-01 08:39:07 +02002806 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002807 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002808 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2809
Radek Krejcif03a9e22020-09-18 20:09:31 +02002810 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002811
2812 /* Operator '<=', '>=' */
2813 tok_len = 2;
2814 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002815
Radek Krejcif03a9e22020-09-18 20:09:31 +02002816 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002817
2818 /* Operator '|' */
2819 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002820 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002821
Radek Krejcif03a9e22020-09-18 20:09:31 +02002822 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002823
2824 /* Operator '+', '-' */
2825 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002826 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002827
Radek Krejcif03a9e22020-09-18 20:09:31 +02002828 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002829
Michal Vasko3e48bf32020-06-01 08:39:07 +02002830 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002831 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002832 tok_type = LYXP_TOKEN_OPER_EQUAL;
2833
Radek Krejcif03a9e22020-09-18 20:09:31 +02002834 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002835
2836 /* Operator '<', '>' */
2837 tok_len = 1;
2838 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002839
Michal Vasko69730152020-10-09 16:30:07 +02002840 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2841 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2842 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2843 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2844 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2845 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2846 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2847 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2848 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2849 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2850 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2851 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002852
2853 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002854 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002855 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002856 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002857
Radek Krejcif03a9e22020-09-18 20:09:31 +02002858 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002859 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002860 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002861
Radek Krejcif03a9e22020-09-18 20:09:31 +02002862 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002863 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002864 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002865
Radek Krejcif03a9e22020-09-18 20:09:31 +02002866 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002867 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002868 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002869
2870 } else if (prev_function_check) {
Michal Vasko53078572019-05-24 08:50:15 +02002871 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 +02002872 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 +02002873 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002874 goto error;
2875 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002876 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr_str);
2877 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002878 goto error;
2879 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002880 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002881
2882 /* NameTest '*' */
2883 tok_len = 1;
2884 tok_type = LYXP_TOKEN_NAMETEST;
2885
2886 } else {
2887
2888 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002889 long int ncname_len = parse_ncname(&expr_str[parsed]);
2890 LY_CHECK_ERR_GOTO(ncname_len < 0,
Michal Vasko69730152020-10-09 16:30:07 +02002891 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
2892 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002893 tok_len = ncname_len;
2894
Radek Krejcif03a9e22020-09-18 20:09:31 +02002895 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002896 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002897 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002898 ++tok_len;
2899 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002900 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
2901 LY_CHECK_ERR_GOTO(ncname_len < 0,
Michal Vasko69730152020-10-09 16:30:07 +02002902 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
2903 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002904 tok_len += ncname_len;
2905 }
2906 /* remove old flag to prevent ambiguities */
2907 prev_function_check = 0;
2908 tok_type = LYXP_TOKEN_NAMETEST;
2909 } else {
2910 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2911 prev_function_check = 1;
2912 tok_type = LYXP_TOKEN_NAMETEST;
2913 }
2914 }
2915
2916 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002917 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002918 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002919 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002920 ++parsed;
2921 }
2922
Radek Krejcif03a9e22020-09-18 20:09:31 +02002923 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002924
Michal Vasko004d3152020-06-11 19:59:22 +02002925 if (reparse) {
2926 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002927 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2928 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002929
Michal Vasko004d3152020-06-11 19:59:22 +02002930 /* fill repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002931 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error);
2932 if (expr->used > tok_idx) {
Michal Vasko004d3152020-06-11 19:59:22 +02002933 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 +02002934 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002935 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002936 goto error;
2937 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002938 }
2939
Radek Krejcif03a9e22020-09-18 20:09:31 +02002940 print_expr_struct_debug(expr);
2941 *expr_p = expr;
2942 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002943
2944error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002945 lyxp_expr_free(ctx, expr);
2946 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002947}
2948
Michal Vasko1734be92020-09-22 08:55:10 +02002949LY_ERR
2950lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02002951{
Michal Vasko1734be92020-09-22 08:55:10 +02002952 LY_ERR ret = LY_SUCCESS;
2953 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02002954 uint32_t i, j;
2955
Michal Vasko7f45cf22020-10-01 12:49:44 +02002956 if (!exp) {
2957 goto cleanup;
2958 }
2959
Michal Vasko004d3152020-06-11 19:59:22 +02002960 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02002961 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002962
2963 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
Michal Vasko1734be92020-09-22 08:55:10 +02002964 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002965 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2966
2967 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
Michal Vasko1734be92020-09-22 08:55:10 +02002968 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002969 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2970
2971 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
Michal Vasko1734be92020-09-22 08:55:10 +02002972 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002973 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2974
2975 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002976 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002977 for (i = 0; i < exp->used; ++i) {
2978 if (!exp->repeat[i]) {
2979 dup->repeat[i] = NULL;
2980 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02002981 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002982 /* the ending 0 as well */
2983 ++j;
2984
Michal Vasko99c71642020-07-03 13:33:36 +02002985 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002986 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002987 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
2988 dup->repeat[i][j - 1] = 0;
2989 }
2990 }
2991
2992 dup->used = exp->used;
2993 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02002994 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002995
Michal Vasko1734be92020-09-22 08:55:10 +02002996cleanup:
2997 if (ret) {
2998 lyxp_expr_free(ctx, dup);
2999 } else {
3000 *dup_p = dup;
3001 }
3002 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003003}
3004
Michal Vasko03ff5a72019-09-11 13:49:33 +02003005/*
3006 * warn functions
3007 *
3008 * Warn functions check specific reasonable conditions for schema XPath
3009 * and print a warning if they are not satisfied.
3010 */
3011
3012/**
3013 * @brief Get the last-added schema node that is currently in the context.
3014 *
3015 * @param[in] set Set to search in.
3016 * @return Last-added schema context node, NULL if no node is in context.
3017 */
3018static struct lysc_node *
3019warn_get_scnode_in_ctx(struct lyxp_set *set)
3020{
3021 uint32_t i;
3022
3023 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3024 return NULL;
3025 }
3026
3027 i = set->used;
3028 do {
3029 --i;
3030 if (set->val.scnodes[i].in_ctx == 1) {
3031 /* if there are more, simply return the first found (last added) */
3032 return set->val.scnodes[i].scnode;
3033 }
3034 } while (i);
3035
3036 return NULL;
3037}
3038
3039/**
3040 * @brief Test whether a type is numeric - integer type or decimal64.
3041 *
3042 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003043 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003044 */
Radek Krejci857189e2020-09-01 13:26:36 +02003045static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003046warn_is_numeric_type(struct lysc_type *type)
3047{
3048 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003049 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003050 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003051
3052 switch (type->basetype) {
3053 case LY_TYPE_DEC64:
3054 case LY_TYPE_INT8:
3055 case LY_TYPE_UINT8:
3056 case LY_TYPE_INT16:
3057 case LY_TYPE_UINT16:
3058 case LY_TYPE_INT32:
3059 case LY_TYPE_UINT32:
3060 case LY_TYPE_INT64:
3061 case LY_TYPE_UINT64:
3062 return 1;
3063 case LY_TYPE_UNION:
3064 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003065 LY_ARRAY_FOR(uni->types, u) {
3066 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003067 if (ret) {
3068 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003069 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003070 }
3071 }
3072 /* did not find any suitable type */
3073 return 0;
3074 case LY_TYPE_LEAFREF:
3075 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3076 default:
3077 return 0;
3078 }
3079}
3080
3081/**
3082 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3083 *
3084 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003085 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003086 */
Radek Krejci857189e2020-09-01 13:26:36 +02003087static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003088warn_is_string_type(struct lysc_type *type)
3089{
3090 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003091 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003092 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003093
3094 switch (type->basetype) {
3095 case LY_TYPE_BITS:
3096 case LY_TYPE_ENUM:
3097 case LY_TYPE_IDENT:
3098 case LY_TYPE_INST:
3099 case LY_TYPE_STRING:
3100 return 1;
3101 case LY_TYPE_UNION:
3102 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003103 LY_ARRAY_FOR(uni->types, u) {
3104 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003105 if (ret) {
3106 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003107 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003108 }
3109 }
3110 /* did not find any suitable type */
3111 return 0;
3112 case LY_TYPE_LEAFREF:
3113 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3114 default:
3115 return 0;
3116 }
3117}
3118
3119/**
3120 * @brief Test whether a type is one specific type.
3121 *
3122 * @param[in] type Type to test.
3123 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003124 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003125 */
Radek Krejci857189e2020-09-01 13:26:36 +02003126static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003127warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3128{
3129 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003130 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003131 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003132
3133 if (type->basetype == base) {
3134 return 1;
3135 } else if (type->basetype == LY_TYPE_UNION) {
3136 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003137 LY_ARRAY_FOR(uni->types, u) {
3138 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003139 if (ret) {
3140 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003141 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003142 }
3143 }
3144 /* did not find any suitable type */
3145 return 0;
3146 } else if (type->basetype == LY_TYPE_LEAFREF) {
3147 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3148 }
3149
3150 return 0;
3151}
3152
3153/**
3154 * @brief Get next type of a (union) type.
3155 *
3156 * @param[in] type Base type.
3157 * @param[in] prev_type Previously returned type.
3158 * @return Next type or NULL.
3159 */
3160static struct lysc_type *
3161warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3162{
3163 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003164 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003165 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003166
3167 switch (type->basetype) {
3168 case LY_TYPE_UNION:
3169 uni = (struct lysc_type_union *)type;
3170 if (!prev_type) {
3171 return uni->types[0];
3172 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003173 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003174 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003175 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003176 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003177 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003178 found = 1;
3179 }
3180 }
3181 return NULL;
3182 default:
3183 if (prev_type) {
3184 assert(type == prev_type);
3185 return NULL;
3186 } else {
3187 return type;
3188 }
3189 }
3190}
3191
3192/**
3193 * @brief Test whether 2 types have a common type.
3194 *
3195 * @param[in] type1 First type.
3196 * @param[in] type2 Second type.
3197 * @return 1 if they do, 0 otherwise.
3198 */
3199static int
3200warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3201{
3202 struct lysc_type *t1, *rt1, *t2, *rt2;
3203
3204 t1 = NULL;
3205 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3206 if (t1->basetype == LY_TYPE_LEAFREF) {
3207 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3208 } else {
3209 rt1 = t1;
3210 }
3211
3212 t2 = NULL;
3213 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3214 if (t2->basetype == LY_TYPE_LEAFREF) {
3215 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3216 } else {
3217 rt2 = t2;
3218 }
3219
3220 if (rt2->basetype == rt1->basetype) {
3221 /* match found */
3222 return 1;
3223 }
3224 }
3225 }
3226
3227 return 0;
3228}
3229
3230/**
3231 * @brief Check both operands of comparison operators.
3232 *
3233 * @param[in] ctx Context for errors.
3234 * @param[in] set1 First operand set.
3235 * @param[in] set2 Second operand set.
3236 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3237 * @param[in] expr Start of the expression to print with the warning.
3238 * @param[in] tok_pos Token position.
3239 */
3240static void
Radek Krejci857189e2020-09-01 13:26:36 +02003241warn_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 +02003242{
3243 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003244 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003245
3246 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3247 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3248
3249 if (!node1 && !node2) {
3250 /* no node-sets involved, nothing to do */
3251 return;
3252 }
3253
3254 if (node1) {
3255 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3256 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3257 warning = 1;
3258 leaves = 0;
3259 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3260 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3261 warning = 1;
3262 }
3263 }
3264
3265 if (node2) {
3266 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3267 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3268 warning = 1;
3269 leaves = 0;
3270 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3271 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3272 warning = 1;
3273 }
3274 }
3275
3276 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003277 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3278 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3279 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3280 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003281 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3282 warning = 1;
3283 }
3284 }
3285
3286 if (warning) {
3287 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3288 }
3289}
3290
3291/**
3292 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3293 *
3294 * @param[in] exp Parsed XPath expression.
3295 * @param[in] set Set with the leaf/leaf-list.
3296 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3297 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3298 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3299 */
3300static void
Michal Vasko40308e72020-10-20 16:38:40 +02003301warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3302 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003303{
3304 struct lysc_node *scnode;
3305 struct lysc_type *type;
3306 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003307 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003308 LY_ERR rc;
3309 struct ly_err_item *err = NULL;
3310
Michal Vasko69730152020-10-09 16:30:07 +02003311 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3312 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003313 /* check that the node can have the specified value */
3314 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3315 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3316 } else {
3317 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3318 }
3319 if (!value) {
3320 LOGMEM(set->ctx);
3321 return;
3322 }
3323
3324 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3325 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003326 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003327 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003328 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003329 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003330 }
3331
3332 type = ((struct lysc_node_leaf *)scnode)->type;
3333 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003334 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003335 LYD_HINT_DATA, scnode, &storage, &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003336
3337 if (err) {
3338 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3339 ly_err_free(err);
3340 } else if (rc != LY_SUCCESS) {
3341 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3342 }
3343 if (rc != LY_SUCCESS) {
3344 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003345 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003346 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003347 } else {
3348 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003349 }
3350 }
3351 free(value);
3352 }
3353}
3354
3355/*
3356 * XPath functions
3357 */
3358
3359/**
3360 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3361 * depending on whether the first node bit value from the second argument is set.
3362 *
3363 * @param[in] args Array of arguments.
3364 * @param[in] arg_count Count of elements in @p args.
3365 * @param[in,out] set Context and result set at the same time.
3366 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003367 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003368 */
3369static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003370xpath_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 +02003371{
3372 struct lyd_node_term *leaf;
3373 struct lysc_node_leaf *sleaf;
3374 struct lysc_type_bits *bits;
3375 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003376 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003377
3378 if (options & LYXP_SCNODE_ALL) {
3379 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3380 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003381 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3382 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 +02003383 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3384 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003385 }
3386
3387 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3388 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3389 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 +02003390 } else if (!warn_is_string_type(sleaf->type)) {
3391 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003392 }
3393 }
3394 set_scnode_clear_ctx(set);
3395 return rc;
3396 }
3397
Michal Vaskod3678892020-05-21 10:06:58 +02003398 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003399 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3400 "bit-is-set(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003401 return LY_EVALID;
3402 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003403 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003404 LY_CHECK_RET(rc);
3405
3406 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003407 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003408 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003409 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3410 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003411 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003412 LY_ARRAY_FOR(bits->bits, u) {
3413 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003414 set_fill_boolean(set, 1);
3415 break;
3416 }
3417 }
3418 }
3419 }
3420
3421 return LY_SUCCESS;
3422}
3423
3424/**
3425 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3426 * with the argument converted to boolean.
3427 *
3428 * @param[in] args Array of arguments.
3429 * @param[in] arg_count Count of elements in @p args.
3430 * @param[in,out] set Context and result set at the same time.
3431 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003432 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003433 */
3434static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003435xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003436{
3437 LY_ERR rc;
3438
3439 if (options & LYXP_SCNODE_ALL) {
3440 set_scnode_clear_ctx(set);
3441 return LY_SUCCESS;
3442 }
3443
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003444 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003445 LY_CHECK_RET(rc);
3446 set_fill_set(set, args[0]);
3447
3448 return LY_SUCCESS;
3449}
3450
3451/**
3452 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3453 * with the first argument rounded up to the nearest integer.
3454 *
3455 * @param[in] args Array of arguments.
3456 * @param[in] arg_count Count of elements in @p args.
3457 * @param[in,out] set Context and result set at the same time.
3458 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003459 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003460 */
3461static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003462xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003463{
3464 struct lysc_node_leaf *sleaf;
3465 LY_ERR rc = LY_SUCCESS;
3466
3467 if (options & LYXP_SCNODE_ALL) {
3468 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3469 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003470 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3471 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 +02003472 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3473 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003474 }
3475 set_scnode_clear_ctx(set);
3476 return rc;
3477 }
3478
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003479 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003480 LY_CHECK_RET(rc);
3481 if ((long long)args[0]->val.num != args[0]->val.num) {
3482 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3483 } else {
3484 set_fill_number(set, args[0]->val.num);
3485 }
3486
3487 return LY_SUCCESS;
3488}
3489
3490/**
3491 * @brief Execute the XPath concat(string, string, string*) function.
3492 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3493 *
3494 * @param[in] args Array of arguments.
3495 * @param[in] arg_count Count of elements in @p args.
3496 * @param[in,out] set Context and result set at the same time.
3497 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003498 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003499 */
3500static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003501xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003502{
3503 uint16_t i;
3504 char *str = NULL;
3505 size_t used = 1;
3506 LY_ERR rc = LY_SUCCESS;
3507 struct lysc_node_leaf *sleaf;
3508
3509 if (options & LYXP_SCNODE_ALL) {
3510 for (i = 0; i < arg_count; ++i) {
3511 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3512 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3513 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003514 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003515 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003516 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 +02003517 }
3518 }
3519 }
3520 set_scnode_clear_ctx(set);
3521 return rc;
3522 }
3523
3524 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003525 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003526 if (rc != LY_SUCCESS) {
3527 free(str);
3528 return rc;
3529 }
3530
3531 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3532 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3533 strcpy(str + used - 1, args[i]->val.str);
3534 used += strlen(args[i]->val.str);
3535 }
3536
3537 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003538 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003539 set->type = LYXP_SET_STRING;
3540 set->val.str = str;
3541
3542 return LY_SUCCESS;
3543}
3544
3545/**
3546 * @brief Execute the XPath contains(string, string) function.
3547 * Returns LYXP_SET_BOOLEAN whether the second argument can
3548 * be found in the first or not.
3549 *
3550 * @param[in] args Array of arguments.
3551 * @param[in] arg_count Count of elements in @p args.
3552 * @param[in,out] set Context and result set at the same time.
3553 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003554 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003555 */
3556static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003557xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003558{
3559 struct lysc_node_leaf *sleaf;
3560 LY_ERR rc = LY_SUCCESS;
3561
3562 if (options & LYXP_SCNODE_ALL) {
3563 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3564 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3565 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 +02003566 } else if (!warn_is_string_type(sleaf->type)) {
3567 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003568 }
3569 }
3570
3571 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3572 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3573 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 +02003574 } else if (!warn_is_string_type(sleaf->type)) {
3575 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003576 }
3577 }
3578 set_scnode_clear_ctx(set);
3579 return rc;
3580 }
3581
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003582 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003583 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003584 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003585 LY_CHECK_RET(rc);
3586
3587 if (strstr(args[0]->val.str, args[1]->val.str)) {
3588 set_fill_boolean(set, 1);
3589 } else {
3590 set_fill_boolean(set, 0);
3591 }
3592
3593 return LY_SUCCESS;
3594}
3595
3596/**
3597 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3598 * with the size of the node-set from the argument.
3599 *
3600 * @param[in] args Array of arguments.
3601 * @param[in] arg_count Count of elements in @p args.
3602 * @param[in,out] set Context and result set at the same time.
3603 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003604 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003605 */
3606static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003607xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003608{
3609 struct lysc_node *scnode = NULL;
3610 LY_ERR rc = LY_SUCCESS;
3611
3612 if (options & LYXP_SCNODE_ALL) {
3613 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3614 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003615 }
3616 set_scnode_clear_ctx(set);
3617 return rc;
3618 }
3619
Michal Vasko03ff5a72019-09-11 13:49:33 +02003620 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003621 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 +02003622 return LY_EVALID;
3623 }
3624
3625 set_fill_number(set, args[0]->used);
3626 return LY_SUCCESS;
3627}
3628
3629/**
3630 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3631 * with the context with the intial node.
3632 *
3633 * @param[in] args Array of arguments.
3634 * @param[in] arg_count Count of elements in @p args.
3635 * @param[in,out] set Context and result set at the same time.
3636 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003637 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003638 */
3639static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003640xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003641{
3642 if (arg_count || args) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003643 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003644 return LY_EVALID;
3645 }
3646
3647 if (options & LYXP_SCNODE_ALL) {
3648 set_scnode_clear_ctx(set);
3649
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003650 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003651 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003652 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003653
3654 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003655 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003656 }
3657
3658 return LY_SUCCESS;
3659}
3660
3661/**
3662 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3663 * leafref or instance-identifier target node(s).
3664 *
3665 * @param[in] args Array of arguments.
3666 * @param[in] arg_count Count of elements in @p args.
3667 * @param[in,out] set Context and result set at the same time.
3668 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003669 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003670 */
3671static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003672xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003673{
3674 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003675 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003676 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003677 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003678 struct ly_path *p;
3679 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003680 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003681 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003682 LY_ERR rc = LY_SUCCESS;
3683
3684 if (options & LYXP_SCNODE_ALL) {
3685 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3686 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003687 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3688 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 +02003689 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3690 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
Michal Vasko69730152020-10-09 16:30:07 +02003691 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003692 }
3693 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003694 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003695 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vasko00cbf532020-06-15 13:58:47 +02003696 oper = lysc_is_output((struct lysc_node *)sleaf) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003697
3698 /* it was already evaluated on schema, it must succeed */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003699 rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path,
3700 LY_PATH_LREF_TRUE, oper, LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003701 assert(!rc);
3702
3703 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003704 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003705 ly_path_free(set->ctx, p);
3706
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003707 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003708 }
3709
Michal Vasko03ff5a72019-09-11 13:49:33 +02003710 return rc;
3711 }
3712
Michal Vaskod3678892020-05-21 10:06:58 +02003713 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003714 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 +02003715 return LY_EVALID;
3716 }
3717
Michal Vaskod3678892020-05-21 10:06:58 +02003718 lyxp_set_free_content(set);
3719 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003720 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3721 sleaf = (struct lysc_node_leaf *)leaf->schema;
3722 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3723 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3724 /* find leafref target */
Michal Vasko004d3152020-06-11 19:59:22 +02003725 if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf,
Michal Vasko69730152020-10-09 16:30:07 +02003726 &leaf->value, set->tree, &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003727 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003728 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003729 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003730 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003731 } else {
3732 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003733 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003734 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Michal Vasko69730152020-10-09 16:30:07 +02003735 LYD_CANON_VALUE(leaf));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003736 return LY_EVALID;
3737 }
3738 }
Michal Vasko004d3152020-06-11 19:59:22 +02003739
3740 /* insert it */
3741 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003742 }
3743 }
3744
3745 return LY_SUCCESS;
3746}
3747
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003748static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003749xpath_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 +02003750{
3751 uint16_t i;
3752 LY_ARRAY_COUNT_TYPE u;
3753 struct lyd_node_term *leaf;
3754 struct lysc_node_leaf *sleaf;
3755 struct lyd_meta *meta;
3756 struct lyd_value data = {0}, *val;
3757 struct ly_err_item *err = NULL;
3758 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003759 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003760
3761 if (options & LYXP_SCNODE_ALL) {
3762 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3763 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
3764 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3765 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003766 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003767 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3768 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3769 }
3770
3771 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3772 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3773 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003774 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003775 } else if (!warn_is_string_type(sleaf->type)) {
3776 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3777 }
3778 }
3779 set_scnode_clear_ctx(set);
3780 return rc;
3781 }
3782
3783 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003784 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 +02003785 "derived-from(-or-self)(node-set, string)");
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003786 return LY_EVALID;
3787 }
3788 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3789 LY_CHECK_RET(rc);
3790
3791 set_fill_boolean(set, 0);
3792 found = 0;
3793 for (i = 0; i < args[0]->used; ++i) {
3794 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3795 continue;
3796 }
3797
3798 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3799 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3800 sleaf = (struct lysc_node_leaf *)leaf->schema;
3801 val = &leaf->value;
3802 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3803 /* uninteresting */
3804 continue;
3805 }
3806
3807 /* store args[1] as ident */
3808 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003809 0, set->format, set->prefix_data, LYD_HINT_DATA, leaf->schema, &data, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003810 } else {
3811 meta = args[0]->val.meta[i].meta;
3812 val = &meta->value;
3813 if (val->realtype->basetype != LY_TYPE_IDENT) {
3814 /* uninteresting */
3815 continue;
3816 }
3817
3818 /* store args[1] as ident */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003819 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), 0,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003820 set->format, set->prefix_data, LYD_HINT_DATA, meta->parent->schema, &data, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003821 }
3822
3823 if (err) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01003824 ly_err_print(set->ctx, err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003825 ly_err_free(err);
3826 }
3827 LY_CHECK_RET(rc);
3828
3829 /* finally check the identity itself */
3830 if (self_match && (data.ident == val->ident)) {
3831 set_fill_boolean(set, 1);
3832 found = 1;
3833 }
3834 if (!found) {
3835 LY_ARRAY_FOR(data.ident->derived, u) {
3836 if (data.ident->derived[u] == val->ident) {
3837 set_fill_boolean(set, 1);
3838 found = 1;
3839 break;
3840 }
3841 }
3842 }
3843
3844 /* free temporary value */
3845 val->realtype->plugin->free(set->ctx, &data);
3846 if (found) {
3847 break;
3848 }
3849 }
3850
3851 return LY_SUCCESS;
3852}
3853
Michal Vasko03ff5a72019-09-11 13:49:33 +02003854/**
3855 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3856 * on whether the first argument nodes contain a node of an identity derived from the second
3857 * argument identity.
3858 *
3859 * @param[in] args Array of arguments.
3860 * @param[in] arg_count Count of elements in @p args.
3861 * @param[in,out] set Context and result set at the same time.
3862 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003863 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003864 */
3865static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003866xpath_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 +02003867{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003868 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003869}
3870
3871/**
3872 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3873 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3874 * the second argument identity.
3875 *
3876 * @param[in] args Array of arguments.
3877 * @param[in] arg_count Count of elements in @p args.
3878 * @param[in,out] set Context and result set at the same time.
3879 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003880 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003881 */
3882static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003883xpath_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 +02003884{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003885 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003886}
3887
3888/**
3889 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3890 * with the integer value of the first node's enum value, otherwise NaN.
3891 *
3892 * @param[in] args Array of arguments.
3893 * @param[in] arg_count Count of elements in @p args.
3894 * @param[in,out] set Context and result set at the same time.
3895 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003896 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003897 */
3898static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003899xpath_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 +02003900{
3901 struct lyd_node_term *leaf;
3902 struct lysc_node_leaf *sleaf;
3903 LY_ERR rc = LY_SUCCESS;
3904
3905 if (options & LYXP_SCNODE_ALL) {
3906 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3907 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003908 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3909 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 +02003910 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3911 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003912 }
3913 set_scnode_clear_ctx(set);
3914 return rc;
3915 }
3916
Michal Vaskod3678892020-05-21 10:06:58 +02003917 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003918 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 +02003919 return LY_EVALID;
3920 }
3921
3922 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003923 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003924 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3925 sleaf = (struct lysc_node_leaf *)leaf->schema;
3926 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3927 set_fill_number(set, leaf->value.enum_item->value);
3928 }
3929 }
3930
3931 return LY_SUCCESS;
3932}
3933
3934/**
3935 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3936 * with false value.
3937 *
3938 * @param[in] args Array of arguments.
3939 * @param[in] arg_count Count of elements in @p args.
3940 * @param[in,out] set Context and result set at the same time.
3941 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003942 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003943 */
3944static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003945xpath_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 +02003946{
3947 if (options & LYXP_SCNODE_ALL) {
3948 set_scnode_clear_ctx(set);
3949 return LY_SUCCESS;
3950 }
3951
3952 set_fill_boolean(set, 0);
3953 return LY_SUCCESS;
3954}
3955
3956/**
3957 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3958 * with the first argument floored (truncated).
3959 *
3960 * @param[in] args Array of arguments.
3961 * @param[in] arg_count Count of elements in @p args.
3962 * @param[in,out] set Context and result set at the same time.
3963 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003964 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003965 */
3966static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003967xpath_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 +02003968{
3969 LY_ERR rc;
3970
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003971 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003972 LY_CHECK_RET(rc);
3973 if (isfinite(args[0]->val.num)) {
3974 set_fill_number(set, (long long)args[0]->val.num);
3975 }
3976
3977 return LY_SUCCESS;
3978}
3979
3980/**
3981 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3982 * whether the language of the text matches the one from the argument.
3983 *
3984 * @param[in] args Array of arguments.
3985 * @param[in] arg_count Count of elements in @p args.
3986 * @param[in,out] set Context and result set at the same time.
3987 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003988 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003989 */
3990static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003991xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003992{
3993 const struct lyd_node *node;
3994 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01003995 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003996 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003997 LY_ERR rc = LY_SUCCESS;
3998
3999 if (options & LYXP_SCNODE_ALL) {
4000 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4001 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4002 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 +02004003 } else if (!warn_is_string_type(sleaf->type)) {
4004 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004005 }
4006 }
4007 set_scnode_clear_ctx(set);
4008 return rc;
4009 }
4010
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004011 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004012 LY_CHECK_RET(rc);
4013
Michal Vasko03ff5a72019-09-11 13:49:33 +02004014 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004015 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 +02004016 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004017 } else if (!set->used) {
4018 set_fill_boolean(set, 0);
4019 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004020 }
4021
4022 switch (set->val.nodes[0].type) {
4023 case LYXP_NODE_ELEM:
4024 case LYXP_NODE_TEXT:
4025 node = set->val.nodes[0].node;
4026 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004027 case LYXP_NODE_META:
4028 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004029 break;
4030 default:
4031 /* nothing to do with roots */
4032 set_fill_boolean(set, 0);
4033 return LY_SUCCESS;
4034 }
4035
Michal Vasko9f96a052020-03-10 09:41:45 +01004036 /* find lang metadata */
Michal Vaskod989ba02020-08-24 10:59:24 +02004037 for ( ; node; node = (struct lyd_node *)node->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004038 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004039 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004040 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004041 break;
4042 }
4043 }
4044
Michal Vasko9f96a052020-03-10 09:41:45 +01004045 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004046 break;
4047 }
4048 }
4049
4050 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004051 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004052 set_fill_boolean(set, 0);
4053 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004054 uint64_t i;
4055
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004056 val = meta->value.canonical;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004057 for (i = 0; args[0]->val.str[i]; ++i) {
4058 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4059 set_fill_boolean(set, 0);
4060 break;
4061 }
4062 }
4063 if (!args[0]->val.str[i]) {
4064 if (!val[i] || (val[i] == '-')) {
4065 set_fill_boolean(set, 1);
4066 } else {
4067 set_fill_boolean(set, 0);
4068 }
4069 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004070 }
4071
4072 return LY_SUCCESS;
4073}
4074
4075/**
4076 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4077 * with the context size.
4078 *
4079 * @param[in] args Array of arguments.
4080 * @param[in] arg_count Count of elements in @p args.
4081 * @param[in,out] set Context and result set at the same time.
4082 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004083 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004084 */
4085static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004086xpath_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 +02004087{
4088 if (options & LYXP_SCNODE_ALL) {
4089 set_scnode_clear_ctx(set);
4090 return LY_SUCCESS;
4091 }
4092
Michal Vasko03ff5a72019-09-11 13:49:33 +02004093 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004094 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 +02004095 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004096 } else if (!set->used) {
4097 set_fill_number(set, 0);
4098 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004099 }
4100
4101 set_fill_number(set, set->ctx_size);
4102 return LY_SUCCESS;
4103}
4104
4105/**
4106 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4107 * with the node name without namespace from the argument or the context.
4108 *
4109 * @param[in] args Array of arguments.
4110 * @param[in] arg_count Count of elements in @p args.
4111 * @param[in,out] set Context and result set at the same time.
4112 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004113 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004114 */
4115static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004116xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004117{
4118 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004119
Michal Vasko03ff5a72019-09-11 13:49:33 +02004120 /* suppress unused variable warning */
4121 (void)options;
4122
4123 if (options & LYXP_SCNODE_ALL) {
4124 set_scnode_clear_ctx(set);
4125 return LY_SUCCESS;
4126 }
4127
4128 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004129 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004130 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
4131 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004132 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004133 } else if (!args[0]->used) {
4134 set_fill_string(set, "", 0);
4135 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004136 }
4137
4138 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004139 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004140
4141 item = &args[0]->val.nodes[0];
4142 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004143 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004144 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 +02004145 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004146 } else if (!set->used) {
4147 set_fill_string(set, "", 0);
4148 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004149 }
4150
4151 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004152 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004153
4154 item = &set->val.nodes[0];
4155 }
4156
4157 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004158 case LYXP_NODE_NONE:
4159 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004160 case LYXP_NODE_ROOT:
4161 case LYXP_NODE_ROOT_CONFIG:
4162 case LYXP_NODE_TEXT:
4163 set_fill_string(set, "", 0);
4164 break;
4165 case LYXP_NODE_ELEM:
4166 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4167 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004168 case LYXP_NODE_META:
4169 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004170 break;
4171 }
4172
4173 return LY_SUCCESS;
4174}
4175
4176/**
4177 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4178 * with the node name fully qualified (with namespace) from the argument or the context.
4179 *
4180 * @param[in] args Array of arguments.
4181 * @param[in] arg_count Count of elements in @p args.
4182 * @param[in,out] set Context and result set at the same time.
4183 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004184 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004185 */
4186static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004187xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004188{
4189 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004190 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004191 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004192 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004193
4194 if (options & LYXP_SCNODE_ALL) {
4195 set_scnode_clear_ctx(set);
4196 return LY_SUCCESS;
4197 }
4198
4199 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004200 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004201 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 +02004202 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004203 } else if (!args[0]->used) {
4204 set_fill_string(set, "", 0);
4205 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004206 }
4207
4208 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004209 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004210
4211 item = &args[0]->val.nodes[0];
4212 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004213 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004214 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 +02004215 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004216 } else if (!set->used) {
4217 set_fill_string(set, "", 0);
4218 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004219 }
4220
4221 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004222 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004223
4224 item = &set->val.nodes[0];
4225 }
4226
4227 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004228 case LYXP_NODE_NONE:
4229 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004230 case LYXP_NODE_ROOT:
4231 case LYXP_NODE_ROOT_CONFIG:
4232 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004233 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004234 break;
4235 case LYXP_NODE_ELEM:
4236 mod = item->node->schema->module;
4237 name = item->node->schema->name;
4238 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004239 case LYXP_NODE_META:
4240 mod = ((struct lyd_meta *)item->node)->annotation->module;
4241 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004242 break;
4243 }
4244
4245 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004246 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004247 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4248 set_fill_string(set, str, strlen(str));
4249 free(str);
4250 } else {
4251 set_fill_string(set, "", 0);
4252 }
4253
4254 return LY_SUCCESS;
4255}
4256
4257/**
4258 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4259 * with the namespace of the node from the argument or the context.
4260 *
4261 * @param[in] args Array of arguments.
4262 * @param[in] arg_count Count of elements in @p args.
4263 * @param[in,out] set Context and result set at the same time.
4264 * @param[in] options XPath options.
4265 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4266 */
4267static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004268xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004269{
4270 struct lyxp_set_node *item;
4271 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004272
Michal Vasko03ff5a72019-09-11 13:49:33 +02004273 /* suppress unused variable warning */
4274 (void)options;
4275
4276 if (options & LYXP_SCNODE_ALL) {
4277 set_scnode_clear_ctx(set);
4278 return LY_SUCCESS;
4279 }
4280
4281 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004282 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004283 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 +02004284 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004285 return LY_EVALID;
4286 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004287 set_fill_string(set, "", 0);
4288 return LY_SUCCESS;
4289 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004290
4291 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004292 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004293
4294 item = &args[0]->val.nodes[0];
4295 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004296 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004297 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 +02004298 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004299 } else if (!set->used) {
4300 set_fill_string(set, "", 0);
4301 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004302 }
4303
4304 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004305 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004306
4307 item = &set->val.nodes[0];
4308 }
4309
4310 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004311 case LYXP_NODE_NONE:
4312 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004313 case LYXP_NODE_ROOT:
4314 case LYXP_NODE_ROOT_CONFIG:
4315 case LYXP_NODE_TEXT:
4316 set_fill_string(set, "", 0);
4317 break;
4318 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004319 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004320 if (item->type == LYXP_NODE_ELEM) {
4321 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004322 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004323 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004324 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004325 }
4326
4327 set_fill_string(set, mod->ns, strlen(mod->ns));
4328 break;
4329 }
4330
4331 return LY_SUCCESS;
4332}
4333
4334/**
4335 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4336 * with only nodes from the context. In practice it either leaves the context
4337 * as it is or returns an empty node set.
4338 *
4339 * @param[in] args Array of arguments.
4340 * @param[in] arg_count Count of elements in @p args.
4341 * @param[in,out] set Context and result set at the same time.
4342 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004343 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004344 */
4345static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004346xpath_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 +02004347{
4348 if (options & LYXP_SCNODE_ALL) {
4349 set_scnode_clear_ctx(set);
4350 return LY_SUCCESS;
4351 }
4352
4353 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004354 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004355 }
4356 return LY_SUCCESS;
4357}
4358
4359/**
4360 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4361 * with normalized value (no leading, trailing, double white spaces) of the node
4362 * from the argument or the context.
4363 *
4364 * @param[in] args Array of arguments.
4365 * @param[in] arg_count Count of elements in @p args.
4366 * @param[in,out] set Context and result set at the same time.
4367 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004368 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004369 */
4370static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004371xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004372{
4373 uint16_t i, new_used;
4374 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004375 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004376 struct lysc_node_leaf *sleaf;
4377 LY_ERR rc = LY_SUCCESS;
4378
4379 if (options & LYXP_SCNODE_ALL) {
4380 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4381 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4382 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 +02004383 } else if (!warn_is_string_type(sleaf->type)) {
4384 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004385 }
4386 }
4387 set_scnode_clear_ctx(set);
4388 return rc;
4389 }
4390
4391 if (arg_count) {
4392 set_fill_set(set, args[0]);
4393 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004394 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004395 LY_CHECK_RET(rc);
4396
4397 /* is there any normalization necessary? */
4398 for (i = 0; set->val.str[i]; ++i) {
4399 if (is_xmlws(set->val.str[i])) {
4400 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4401 have_spaces = 1;
4402 break;
4403 }
4404 space_before = 1;
4405 } else {
4406 space_before = 0;
4407 }
4408 }
4409
4410 /* yep, there is */
4411 if (have_spaces) {
4412 /* it's enough, at least one character will go, makes space for ending '\0' */
4413 new = malloc(strlen(set->val.str) * sizeof(char));
4414 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4415 new_used = 0;
4416
4417 space_before = 0;
4418 for (i = 0; set->val.str[i]; ++i) {
4419 if (is_xmlws(set->val.str[i])) {
4420 if ((i == 0) || space_before) {
4421 space_before = 1;
4422 continue;
4423 } else {
4424 space_before = 1;
4425 }
4426 } else {
4427 space_before = 0;
4428 }
4429
4430 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4431 ++new_used;
4432 }
4433
4434 /* at worst there is one trailing space now */
4435 if (new_used && is_xmlws(new[new_used - 1])) {
4436 --new_used;
4437 }
4438
4439 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4440 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4441 new[new_used] = '\0';
4442
4443 free(set->val.str);
4444 set->val.str = new;
4445 }
4446
4447 return LY_SUCCESS;
4448}
4449
4450/**
4451 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4452 * with the argument converted to boolean and logically inverted.
4453 *
4454 * @param[in] args Array of arguments.
4455 * @param[in] arg_count Count of elements in @p args.
4456 * @param[in,out] set Context and result set at the same time.
4457 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004458 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004459 */
4460static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004461xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004462{
4463 if (options & LYXP_SCNODE_ALL) {
4464 set_scnode_clear_ctx(set);
4465 return LY_SUCCESS;
4466 }
4467
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004468 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004469 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004470 set_fill_boolean(set, 0);
4471 } else {
4472 set_fill_boolean(set, 1);
4473 }
4474
4475 return LY_SUCCESS;
4476}
4477
4478/**
4479 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4480 * with the number representation of either the argument or the context.
4481 *
4482 * @param[in] args Array of arguments.
4483 * @param[in] arg_count Count of elements in @p args.
4484 * @param[in,out] set Context and result set at the same time.
4485 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004486 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004487 */
4488static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004489xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004490{
4491 LY_ERR rc;
4492
4493 if (options & LYXP_SCNODE_ALL) {
4494 set_scnode_clear_ctx(set);
4495 return LY_SUCCESS;
4496 }
4497
4498 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004499 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004500 LY_CHECK_RET(rc);
4501 set_fill_set(set, args[0]);
4502 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004503 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004504 LY_CHECK_RET(rc);
4505 }
4506
4507 return LY_SUCCESS;
4508}
4509
4510/**
4511 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4512 * with the context position.
4513 *
4514 * @param[in] args Array of arguments.
4515 * @param[in] arg_count Count of elements in @p args.
4516 * @param[in,out] set Context and result set at the same time.
4517 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004518 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004519 */
4520static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004521xpath_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 +02004522{
4523 if (options & LYXP_SCNODE_ALL) {
4524 set_scnode_clear_ctx(set);
4525 return LY_SUCCESS;
4526 }
4527
Michal Vasko03ff5a72019-09-11 13:49:33 +02004528 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004529 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 +02004530 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004531 } else if (!set->used) {
4532 set_fill_number(set, 0);
4533 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004534 }
4535
4536 set_fill_number(set, set->ctx_pos);
4537
4538 /* UNUSED in 'Release' build type */
4539 (void)options;
4540 return LY_SUCCESS;
4541}
4542
4543/**
4544 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4545 * depending on whether the second argument regex matches the first argument string. For details refer to
4546 * YANG 1.1 RFC section 10.2.1.
4547 *
4548 * @param[in] args Array of arguments.
4549 * @param[in] arg_count Count of elements in @p args.
4550 * @param[in,out] set Context and result set at the same time.
4551 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004552 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004553 */
4554static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004555xpath_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 +02004556{
4557 struct lysc_pattern **patterns = NULL, **pattern;
4558 struct lysc_node_leaf *sleaf;
4559 char *path;
4560 LY_ERR rc = LY_SUCCESS;
4561 struct ly_err_item *err;
4562
4563 if (options & LYXP_SCNODE_ALL) {
4564 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4565 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4566 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 +02004567 } else if (!warn_is_string_type(sleaf->type)) {
4568 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004569 }
4570 }
4571
4572 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4573 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4574 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 +02004575 } else if (!warn_is_string_type(sleaf->type)) {
4576 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004577 }
4578 }
4579 set_scnode_clear_ctx(set);
4580 return rc;
4581 }
4582
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004583 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004584 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004585 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004586 LY_CHECK_RET(rc);
4587
4588 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4589 *pattern = malloc(sizeof **pattern);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004590 path = lyd_path(set->cur_node, LYD_PATH_LOG, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004591 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4592 free(path);
4593 if (rc != LY_SUCCESS) {
4594 LY_ARRAY_FREE(patterns);
4595 return rc;
4596 }
4597
4598 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4599 pcre2_code_free((*pattern)->code);
4600 free(*pattern);
4601 LY_ARRAY_FREE(patterns);
4602 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004603 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004604 ly_err_free(err);
4605 return rc;
4606 }
4607
4608 if (rc == LY_EVALID) {
4609 ly_err_free(err);
4610 set_fill_boolean(set, 0);
4611 } else {
4612 set_fill_boolean(set, 1);
4613 }
4614
4615 return LY_SUCCESS;
4616}
4617
4618/**
4619 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4620 * with the rounded first argument. For details refer to
4621 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4622 *
4623 * @param[in] args Array of arguments.
4624 * @param[in] arg_count Count of elements in @p args.
4625 * @param[in,out] set Context and result set at the same time.
4626 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004627 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004628 */
4629static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004630xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004631{
4632 struct lysc_node_leaf *sleaf;
4633 LY_ERR rc = LY_SUCCESS;
4634
4635 if (options & LYXP_SCNODE_ALL) {
4636 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4637 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004638 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4639 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 +02004640 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4641 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004642 }
4643 set_scnode_clear_ctx(set);
4644 return rc;
4645 }
4646
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004647 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004648 LY_CHECK_RET(rc);
4649
4650 /* cover only the cases where floor can't be used */
4651 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4652 set_fill_number(set, -0.0f);
4653 } else {
4654 args[0]->val.num += 0.5;
4655 rc = xpath_floor(args, 1, args[0], options);
4656 LY_CHECK_RET(rc);
4657 set_fill_number(set, args[0]->val.num);
4658 }
4659
4660 return LY_SUCCESS;
4661}
4662
4663/**
4664 * @brief Execute the XPath starts-with(string, string) function.
4665 * Returns LYXP_SET_BOOLEAN whether the second argument is
4666 * the prefix of the first or not.
4667 *
4668 * @param[in] args Array of arguments.
4669 * @param[in] arg_count Count of elements in @p args.
4670 * @param[in,out] set Context and result set at the same time.
4671 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004672 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004673 */
4674static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004675xpath_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 +02004676{
4677 struct lysc_node_leaf *sleaf;
4678 LY_ERR rc = LY_SUCCESS;
4679
4680 if (options & LYXP_SCNODE_ALL) {
4681 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4682 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4683 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 +02004684 } else if (!warn_is_string_type(sleaf->type)) {
4685 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004686 }
4687 }
4688
4689 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4690 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4691 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 +02004692 } else if (!warn_is_string_type(sleaf->type)) {
4693 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004694 }
4695 }
4696 set_scnode_clear_ctx(set);
4697 return rc;
4698 }
4699
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004700 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004701 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004702 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004703 LY_CHECK_RET(rc);
4704
4705 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4706 set_fill_boolean(set, 0);
4707 } else {
4708 set_fill_boolean(set, 1);
4709 }
4710
4711 return LY_SUCCESS;
4712}
4713
4714/**
4715 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4716 * with the string representation of either the argument or the context.
4717 *
4718 * @param[in] args Array of arguments.
4719 * @param[in] arg_count Count of elements in @p args.
4720 * @param[in,out] set Context and result set at the same time.
4721 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004722 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004723 */
4724static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004725xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004726{
4727 LY_ERR rc;
4728
4729 if (options & LYXP_SCNODE_ALL) {
4730 set_scnode_clear_ctx(set);
4731 return LY_SUCCESS;
4732 }
4733
4734 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004735 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004736 LY_CHECK_RET(rc);
4737 set_fill_set(set, args[0]);
4738 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004739 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004740 LY_CHECK_RET(rc);
4741 }
4742
4743 return LY_SUCCESS;
4744}
4745
4746/**
4747 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4748 * with the length of the string in either the argument or the context.
4749 *
4750 * @param[in] args Array of arguments.
4751 * @param[in] arg_count Count of elements in @p args.
4752 * @param[in,out] set Context and result set at the same time.
4753 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004754 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004755 */
4756static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004757xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004758{
4759 struct lysc_node_leaf *sleaf;
4760 LY_ERR rc = LY_SUCCESS;
4761
4762 if (options & LYXP_SCNODE_ALL) {
4763 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4764 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4765 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 +02004766 } else if (!warn_is_string_type(sleaf->type)) {
4767 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004768 }
4769 }
4770 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4771 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4772 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 +02004773 } else if (!warn_is_string_type(sleaf->type)) {
4774 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004775 }
4776 }
4777 set_scnode_clear_ctx(set);
4778 return rc;
4779 }
4780
4781 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004782 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004783 LY_CHECK_RET(rc);
4784 set_fill_number(set, strlen(args[0]->val.str));
4785 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004786 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004787 LY_CHECK_RET(rc);
4788 set_fill_number(set, strlen(set->val.str));
4789 }
4790
4791 return LY_SUCCESS;
4792}
4793
4794/**
4795 * @brief Execute the XPath substring(string, number, number?) function.
4796 * Returns LYXP_SET_STRING substring of the first argument starting
4797 * on the second argument index ending on the third argument index,
4798 * indexed from 1. For exact definition refer to
4799 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4800 *
4801 * @param[in] args Array of arguments.
4802 * @param[in] arg_count Count of elements in @p args.
4803 * @param[in,out] set Context and result set at the same time.
4804 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004805 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004806 */
4807static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004808xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004809{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004810 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004811 uint16_t str_start, str_len, pos;
4812 struct lysc_node_leaf *sleaf;
4813 LY_ERR rc = LY_SUCCESS;
4814
4815 if (options & LYXP_SCNODE_ALL) {
4816 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4817 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4818 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 +02004819 } else if (!warn_is_string_type(sleaf->type)) {
4820 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004821 }
4822 }
4823
4824 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4825 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4826 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 +02004827 } else if (!warn_is_numeric_type(sleaf->type)) {
4828 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004829 }
4830 }
4831
Michal Vasko69730152020-10-09 16:30:07 +02004832 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4833 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004834 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4835 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 +02004836 } else if (!warn_is_numeric_type(sleaf->type)) {
4837 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004838 }
4839 }
4840 set_scnode_clear_ctx(set);
4841 return rc;
4842 }
4843
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004844 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004845 LY_CHECK_RET(rc);
4846
4847 /* start */
4848 if (xpath_round(&args[1], 1, args[1], options)) {
4849 return -1;
4850 }
4851 if (isfinite(args[1]->val.num)) {
4852 start = args[1]->val.num - 1;
4853 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004854 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004855 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004856 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004857 }
4858
4859 /* len */
4860 if (arg_count == 3) {
4861 rc = xpath_round(&args[2], 1, args[2], options);
4862 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004863 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004864 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004865 } else if (isfinite(args[2]->val.num)) {
4866 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004867 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004868 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004869 }
4870 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004871 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004872 }
4873
4874 /* find matching character positions */
4875 str_start = 0;
4876 str_len = 0;
4877 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4878 if (pos < start) {
4879 ++str_start;
4880 } else if (pos < start + len) {
4881 ++str_len;
4882 } else {
4883 break;
4884 }
4885 }
4886
4887 set_fill_string(set, args[0]->val.str + str_start, str_len);
4888 return LY_SUCCESS;
4889}
4890
4891/**
4892 * @brief Execute the XPath substring-after(string, string) function.
4893 * Returns LYXP_SET_STRING with the string succeeding the occurance
4894 * of the second argument in the first or an empty string.
4895 *
4896 * @param[in] args Array of arguments.
4897 * @param[in] arg_count Count of elements in @p args.
4898 * @param[in,out] set Context and result set at the same time.
4899 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004900 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004901 */
4902static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004903xpath_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 +02004904{
4905 char *ptr;
4906 struct lysc_node_leaf *sleaf;
4907 LY_ERR rc = LY_SUCCESS;
4908
4909 if (options & LYXP_SCNODE_ALL) {
4910 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4911 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4912 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 +02004913 } else if (!warn_is_string_type(sleaf->type)) {
4914 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004915 }
4916 }
4917
4918 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4919 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4920 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 +02004921 } else if (!warn_is_string_type(sleaf->type)) {
4922 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004923 }
4924 }
4925 set_scnode_clear_ctx(set);
4926 return rc;
4927 }
4928
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004929 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004930 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004931 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004932 LY_CHECK_RET(rc);
4933
4934 ptr = strstr(args[0]->val.str, args[1]->val.str);
4935 if (ptr) {
4936 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4937 } else {
4938 set_fill_string(set, "", 0);
4939 }
4940
4941 return LY_SUCCESS;
4942}
4943
4944/**
4945 * @brief Execute the XPath substring-before(string, string) function.
4946 * Returns LYXP_SET_STRING with the string preceding the occurance
4947 * of the second argument in the first or an empty string.
4948 *
4949 * @param[in] args Array of arguments.
4950 * @param[in] arg_count Count of elements in @p args.
4951 * @param[in,out] set Context and result set at the same time.
4952 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004953 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004954 */
4955static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004956xpath_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 +02004957{
4958 char *ptr;
4959 struct lysc_node_leaf *sleaf;
4960 LY_ERR rc = LY_SUCCESS;
4961
4962 if (options & LYXP_SCNODE_ALL) {
4963 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4964 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4965 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 +02004966 } else if (!warn_is_string_type(sleaf->type)) {
4967 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004968 }
4969 }
4970
4971 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4972 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4973 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 +02004974 } else if (!warn_is_string_type(sleaf->type)) {
4975 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004976 }
4977 }
4978 set_scnode_clear_ctx(set);
4979 return rc;
4980 }
4981
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004982 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004983 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004984 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004985 LY_CHECK_RET(rc);
4986
4987 ptr = strstr(args[0]->val.str, args[1]->val.str);
4988 if (ptr) {
4989 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4990 } else {
4991 set_fill_string(set, "", 0);
4992 }
4993
4994 return LY_SUCCESS;
4995}
4996
4997/**
4998 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
4999 * with the sum of all the nodes in the context.
5000 *
5001 * @param[in] args Array of arguments.
5002 * @param[in] arg_count Count of elements in @p args.
5003 * @param[in,out] set Context and result set at the same time.
5004 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005005 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005006 */
5007static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005008xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005009{
5010 long double num;
5011 char *str;
5012 uint16_t i;
5013 struct lyxp_set set_item;
5014 struct lysc_node_leaf *sleaf;
5015 LY_ERR rc = LY_SUCCESS;
5016
5017 if (options & LYXP_SCNODE_ALL) {
5018 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5019 for (i = 0; i < args[0]->used; ++i) {
5020 if (args[0]->val.scnodes[i].in_ctx == 1) {
5021 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5022 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5023 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005024 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005025 } else if (!warn_is_numeric_type(sleaf->type)) {
5026 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005027 }
5028 }
5029 }
5030 }
5031 set_scnode_clear_ctx(set);
5032 return rc;
5033 }
5034
5035 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005036
5037 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005038 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 +02005039 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005040 } else if (!args[0]->used) {
5041 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005042 }
5043
Michal Vasko5c4e5892019-11-14 12:31:38 +01005044 set_init(&set_item, set);
5045
Michal Vasko03ff5a72019-09-11 13:49:33 +02005046 set_item.type = LYXP_SET_NODE_SET;
5047 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5048 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5049
5050 set_item.used = 1;
5051 set_item.size = 1;
5052
5053 for (i = 0; i < args[0]->used; ++i) {
5054 set_item.val.nodes[0] = args[0]->val.nodes[i];
5055
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005056 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005057 LY_CHECK_RET(rc);
5058 num = cast_string_to_number(str);
5059 free(str);
5060 set->val.num += num;
5061 }
5062
5063 free(set_item.val.nodes);
5064
5065 return LY_SUCCESS;
5066}
5067
5068/**
5069 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5070 * with the text content of the nodes in the context.
5071 *
5072 * @param[in] args Array of arguments.
5073 * @param[in] arg_count Count of elements in @p args.
5074 * @param[in,out] set Context and result set at the same time.
5075 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005076 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005077 */
5078static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005079xpath_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 +02005080{
5081 uint32_t i;
5082
5083 if (options & LYXP_SCNODE_ALL) {
5084 set_scnode_clear_ctx(set);
5085 return LY_SUCCESS;
5086 }
5087
Michal Vasko03ff5a72019-09-11 13:49:33 +02005088 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005089 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 +02005090 return LY_EVALID;
5091 }
5092
Michal Vaskod989ba02020-08-24 10:59:24 +02005093 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005094 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005095 case LYXP_NODE_NONE:
5096 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005097 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005098 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5099 set->val.nodes[i].type = LYXP_NODE_TEXT;
5100 ++i;
5101 break;
5102 }
Radek Krejci0f969882020-08-21 16:56:47 +02005103 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005104 case LYXP_NODE_ROOT:
5105 case LYXP_NODE_ROOT_CONFIG:
5106 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005107 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005108 set_remove_node(set, i);
5109 break;
5110 }
5111 }
5112
5113 return LY_SUCCESS;
5114}
5115
5116/**
5117 * @brief Execute the XPath translate(string, string, string) function.
5118 * Returns LYXP_SET_STRING with the first argument with the characters
5119 * from the second argument replaced by those on the corresponding
5120 * positions in the third argument.
5121 *
5122 * @param[in] args Array of arguments.
5123 * @param[in] arg_count Count of elements in @p args.
5124 * @param[in,out] set Context and result set at the same time.
5125 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005126 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005127 */
5128static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005129xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005130{
5131 uint16_t i, j, new_used;
5132 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005133 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005134 struct lysc_node_leaf *sleaf;
5135 LY_ERR rc = LY_SUCCESS;
5136
5137 if (options & LYXP_SCNODE_ALL) {
5138 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5139 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5140 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 +02005141 } else if (!warn_is_string_type(sleaf->type)) {
5142 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005143 }
5144 }
5145
5146 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5147 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5148 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 +02005149 } else if (!warn_is_string_type(sleaf->type)) {
5150 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005151 }
5152 }
5153
5154 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5155 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5156 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 +02005157 } else if (!warn_is_string_type(sleaf->type)) {
5158 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005159 }
5160 }
5161 set_scnode_clear_ctx(set);
5162 return rc;
5163 }
5164
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005165 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005166 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005167 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005168 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005169 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005170 LY_CHECK_RET(rc);
5171
5172 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5173 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5174 new_used = 0;
5175
5176 have_removed = 0;
5177 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005178 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005179
5180 for (j = 0; args[1]->val.str[j]; ++j) {
5181 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5182 /* removing this char */
5183 if (j >= strlen(args[2]->val.str)) {
5184 have_removed = 1;
5185 found = 1;
5186 break;
5187 }
5188 /* replacing this char */
5189 new[new_used] = args[2]->val.str[j];
5190 ++new_used;
5191 found = 1;
5192 break;
5193 }
5194 }
5195
5196 /* copying this char */
5197 if (!found) {
5198 new[new_used] = args[0]->val.str[i];
5199 ++new_used;
5200 }
5201 }
5202
5203 if (have_removed) {
5204 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5205 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5206 }
5207 new[new_used] = '\0';
5208
Michal Vaskod3678892020-05-21 10:06:58 +02005209 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005210 set->type = LYXP_SET_STRING;
5211 set->val.str = new;
5212
5213 return LY_SUCCESS;
5214}
5215
5216/**
5217 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5218 * with true value.
5219 *
5220 * @param[in] args Array of arguments.
5221 * @param[in] arg_count Count of elements in @p args.
5222 * @param[in,out] set Context and result set at the same time.
5223 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005224 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005225 */
5226static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005227xpath_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 +02005228{
5229 if (options & LYXP_SCNODE_ALL) {
5230 set_scnode_clear_ctx(set);
5231 return LY_SUCCESS;
5232 }
5233
5234 set_fill_boolean(set, 1);
5235 return LY_SUCCESS;
5236}
5237
5238/*
5239 * moveto functions
5240 *
5241 * They and only they actually change the context (set).
5242 */
5243
5244/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005245 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005246 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005247 * XPath @p set is expected to be a (sc)node set!
5248 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005249 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5250 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005251 * @param[in] set Set with general XPath context.
5252 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005253 * @param[out] moveto_mod Expected module of a matching node.
5254 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005255 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005256static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005257moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5258 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005259{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005260 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005261 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005262 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005263
Michal Vasko2104e9f2020-03-06 08:23:25 +01005264 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5265
Michal Vasko6346ece2019-09-24 13:12:53 +02005266 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5267 /* specific module */
5268 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005269 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005270
Michal Vasko004d3152020-06-11 19:59:22 +02005271 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005272 if (!mod || !mod->implemented) {
Michal Vasko2104e9f2020-03-06 08:23:25 +01005273 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005274 LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko2104e9f2020-03-06 08:23:25 +01005275 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005276 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko2104e9f2020-03-06 08:23:25 +01005277 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005278 return LY_EVALID;
5279 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005280
Michal Vasko6346ece2019-09-24 13:12:53 +02005281 *qname += pref_len + 1;
5282 *qname_len -= pref_len + 1;
5283 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5284 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005285 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005286 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005287 switch (set->format) {
5288 case LY_PREF_SCHEMA:
5289 case LY_PREF_SCHEMA_RESOLVED:
5290 /* current module */
5291 mod = set->cur_mod;
5292 break;
5293 case LY_PREF_JSON:
5294 /* inherit parent (context node) module */
5295 if (ctx_scnode) {
5296 mod = ctx_scnode->module;
5297 } else {
5298 mod = NULL;
5299 }
5300 break;
5301 case LY_PREF_XML:
5302 /* not defined */
5303 LOGINT_RET(set->ctx);
5304 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005305 }
5306
Michal Vasko6346ece2019-09-24 13:12:53 +02005307 *moveto_mod = mod;
5308 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005309}
5310
5311/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005312 * @brief Move context @p set to the root. Handles absolute path.
5313 * Result is LYXP_SET_NODE_SET.
5314 *
5315 * @param[in,out] set Set to use.
5316 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005317 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005318 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005319static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005320moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005321{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005322 if (!set) {
Michal Vaskob0099a92020-08-31 14:55:23 +02005323 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005324 }
5325
5326 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005327 set_scnode_clear_ctx(set);
Michal Vaskob0099a92020-08-31 14:55:23 +02005328 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005329 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005330 set->type = LYXP_SET_NODE_SET;
5331 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005332 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005333 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005334
5335 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005336}
5337
5338/**
5339 * @brief Check @p node as a part of NameTest processing.
5340 *
5341 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005342 * @param[in] ctx_node Context node.
5343 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005344 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005345 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005346 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005347 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5348 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005349 */
5350static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005351moveto_node_check(const struct lyd_node *node, const struct lyd_node *ctx_node, const struct lyxp_set *set,
Michal Vaskocdad7122020-11-09 21:04:44 +01005352 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005353{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005354 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5355 switch (set->format) {
5356 case LY_PREF_SCHEMA:
5357 case LY_PREF_SCHEMA_RESOLVED:
5358 /* use current module */
5359 moveto_mod = set->cur_mod;
5360 break;
5361 case LY_PREF_JSON:
5362 /* inherit module of the context node, if any */
5363 if (ctx_node) {
5364 moveto_mod = ctx_node->schema->module;
5365 }
5366 break;
5367 case LY_PREF_XML:
5368 /* not defined */
5369 LOGINT(set->ctx);
5370 return LY_EINVAL;
5371 }
5372 }
5373
Michal Vasko03ff5a72019-09-11 13:49:33 +02005374 /* module check */
5375 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005376 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005377 }
5378
Michal Vasko5c4e5892019-11-14 12:31:38 +01005379 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005380 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005381 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005382 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5383 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005384 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005385 }
5386
5387 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005388 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005389 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005390 }
5391
Michal Vaskoa1424542019-11-14 16:08:52 +01005392 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005393 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005394 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005395 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005396
5397 /* match */
5398 return LY_SUCCESS;
5399}
5400
5401/**
5402 * @brief Check @p node as a part of schema NameTest processing.
5403 *
5404 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005405 * @param[in] ctx_scnode Context node.
5406 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005407 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005408 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005409 * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005410 */
5411static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005412moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set,
Radek Krejci0f969882020-08-21 16:56:47 +02005413 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005414{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005415 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5416 switch (set->format) {
5417 case LY_PREF_SCHEMA:
5418 case LY_PREF_SCHEMA_RESOLVED:
5419 /* use current module */
5420 moveto_mod = set->cur_mod;
5421 break;
5422 case LY_PREF_JSON:
5423 /* inherit module of the context node, if any */
5424 if (ctx_scnode) {
5425 moveto_mod = ctx_scnode->module;
5426 }
5427 break;
5428 case LY_PREF_XML:
5429 /* not defined */
5430 LOGINT(set->ctx);
5431 return LY_EINVAL;
5432 }
5433 }
5434
Michal Vasko03ff5a72019-09-11 13:49:33 +02005435 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005436 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005437 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005438 }
5439
5440 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005441 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005442 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005443 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005444 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005445 }
5446
5447 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005448 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005449 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005450 }
5451
5452 /* match */
5453 return LY_SUCCESS;
5454}
5455
5456/**
Michal Vaskod3678892020-05-21 10:06:58 +02005457 * @brief Move context @p set to a node. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005458 *
5459 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005460 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005461 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005462 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005463 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5464 */
5465static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005466moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005467{
Michal Vaskof03ed032020-03-04 13:31:44 +01005468 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005469 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005470 LY_ERR rc;
5471
Michal Vaskod3678892020-05-21 10:06:58 +02005472 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005473 return LY_SUCCESS;
5474 }
5475
5476 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005477 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 +02005478 return LY_EVALID;
5479 }
5480
Michal Vasko03ff5a72019-09-11 13:49:33 +02005481 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005482 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005483
5484 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005485 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005486
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005487 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005488 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005489 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005490 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005491 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005492 ctx_node = set->val.nodes[i].node;
5493 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005494 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005495
Michal Vaskod3678892020-05-21 10:06:58 +02005496 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005497 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005498 if (rc == LY_SUCCESS) {
5499 if (!replaced) {
5500 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5501 replaced = 1;
5502 } else {
5503 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005504 }
Michal Vaskod3678892020-05-21 10:06:58 +02005505 ++i;
5506 } else if (rc == LY_EINCOMPLETE) {
5507 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005508 }
5509 }
5510
5511 if (!replaced) {
5512 /* no match */
5513 set_remove_node(set, i);
5514 }
5515 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005516
5517 return LY_SUCCESS;
5518}
5519
5520/**
Michal Vaskod3678892020-05-21 10:06:58 +02005521 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5522 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005523 *
5524 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005525 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005526 * @param[in] predicates If @p scnode is ::LYS_LIST or ::LYS_LEAFLIST, the predicates specifying a single instance.
Michal Vaskocdad7122020-11-09 21:04:44 +01005527 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005528 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5529 */
5530static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005531moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5532 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005533{
Michal Vasko004d3152020-06-11 19:59:22 +02005534 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005535 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005536 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005537 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005538
Michal Vasko004d3152020-06-11 19:59:22 +02005539 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005540
5541 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005542 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005543 }
5544
5545 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005546 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 +02005547 ret = LY_EVALID;
5548 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005549 }
5550
5551 /* context check for all the nodes since we have the schema node */
5552 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5553 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005554 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005555 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5556 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005557 lyxp_set_free_content(set);
5558 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005559 }
5560
5561 /* create specific data instance if needed */
5562 if (scnode->nodetype == LYS_LIST) {
5563 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5564 } else if (scnode->nodetype == LYS_LEAFLIST) {
5565 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005566 }
5567
5568 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005569 siblings = NULL;
5570
5571 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5572 assert(!set->val.nodes[i].node);
5573
5574 /* search in all the trees */
5575 siblings = set->tree;
5576 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5577 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005578 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005579 }
5580
5581 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005582 if (inst) {
5583 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005584 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005585 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005586 }
5587
5588 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005589 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005590 ret = LY_EINCOMPLETE;
5591 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005592 }
5593
5594 if (sub) {
5595 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005596 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005597 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005598 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005599 /* no match */
5600 set_remove_node(set, i);
5601 }
5602 }
5603
Michal Vasko004d3152020-06-11 19:59:22 +02005604cleanup:
5605 lyd_free_tree(inst);
5606 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005607}
5608
5609/**
5610 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5611 *
5612 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005613 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005614 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005615 * @param[in] options XPath options.
5616 * @return LY_ERR
5617 */
5618static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005619moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005620{
Radek Krejci857189e2020-09-01 13:26:36 +02005621 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005622 uint32_t getnext_opts;
5623 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005624 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005625 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005626 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005627
Michal Vaskod3678892020-05-21 10:06:58 +02005628 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005629 return LY_SUCCESS;
5630 }
5631
5632 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005633 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 +02005634 return LY_EVALID;
5635 }
5636
Michal Vaskocafad9d2019-11-07 15:20:03 +01005637 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005638 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005639 if (options & LYXP_SCNODE_OUTPUT) {
5640 getnext_opts |= LYS_GETNEXT_OUTPUT;
5641 }
5642
Michal Vasko03ff5a72019-09-11 13:49:33 +02005643 orig_used = set->used;
5644 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005645 uint32_t idx;
5646
Michal Vasko03ff5a72019-09-11 13:49:33 +02005647 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005648 if (set->val.scnodes[i].in_ctx != -2) {
5649 continue;
5650 }
5651
5652 /* remember context node */
5653 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005654 } else {
5655 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005656 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005657
5658 start_parent = set->val.scnodes[i].scnode;
5659
5660 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005661 /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set),
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005662 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005663 mod_idx = 0;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005664 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005665 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005666 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005667 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005668 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005669 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5670
Michal Vasko03ff5a72019-09-11 13:49:33 +02005671 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005672 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005673 set->val.scnodes[idx].in_ctx = 2;
5674 temp_ctx = 1;
5675 }
5676 }
5677 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005678 }
5679
Michal Vasko519fd602020-05-26 12:17:39 +02005680 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5681 iter = NULL;
5682 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005683 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005684 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5685
5686 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005687 set->val.scnodes[idx].in_ctx = 2;
5688 temp_ctx = 1;
5689 }
5690 }
5691 }
5692 }
5693 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005694
5695 /* correct temporary in_ctx values */
5696 if (temp_ctx) {
5697 for (i = 0; i < orig_used; ++i) {
5698 if (set->val.scnodes[i].in_ctx == 2) {
5699 set->val.scnodes[i].in_ctx = 1;
5700 }
5701 }
5702 }
5703
5704 return LY_SUCCESS;
5705}
5706
5707/**
Michal Vaskod3678892020-05-21 10:06:58 +02005708 * @brief Move context @p set to a node and all its descendants. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
Michal Vasko03ff5a72019-09-11 13:49:33 +02005709 * Context position aware.
5710 *
5711 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005712 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005713 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005714 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005715 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5716 */
5717static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005718moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005719{
5720 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005721 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005722 struct lyxp_set ret_set;
5723 LY_ERR rc;
5724
Michal Vaskod3678892020-05-21 10:06:58 +02005725 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005726 return LY_SUCCESS;
5727 }
5728
5729 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005730 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 +02005731 return LY_EVALID;
5732 }
5733
Michal Vasko9f96a052020-03-10 09:41:45 +01005734 /* replace the original nodes (and throws away all text and meta nodes, root is replaced by a child) */
Michal Vaskocdad7122020-11-09 21:04:44 +01005735 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005736 LY_CHECK_RET(rc);
5737
Michal Vasko6346ece2019-09-24 13:12:53 +02005738 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005739 set_init(&ret_set, set);
5740 for (i = 0; i < set->used; ++i) {
5741
5742 /* TREE DFS */
5743 start = set->val.nodes[i].node;
5744 for (elem = next = start; elem; elem = next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005745 rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005746 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005747 /* add matching node into result set */
5748 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5749 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5750 /* the node is a duplicate, we'll process it later in the set */
5751 goto skip_children;
5752 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005753 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005754 return rc;
5755 } else if (rc == LY_EINVAL) {
5756 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005757 }
5758
5759 /* TREE DFS NEXT ELEM */
5760 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005761 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005762 if (!next) {
5763skip_children:
5764 /* no children, so try siblings, but only if it's not the start,
5765 * that is considered to be the root and it's siblings are not traversed */
5766 if (elem != start) {
5767 next = elem->next;
5768 } else {
5769 break;
5770 }
5771 }
5772 while (!next) {
5773 /* no siblings, go back through the parents */
5774 if ((struct lyd_node *)elem->parent == start) {
5775 /* we are done, no next element to process */
5776 break;
5777 }
5778 /* parent is already processed, go to its sibling */
5779 elem = (struct lyd_node *)elem->parent;
5780 next = elem->next;
5781 }
5782 }
5783 }
5784
5785 /* make the temporary set the current one */
5786 ret_set.ctx_pos = set->ctx_pos;
5787 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005788 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005789 memcpy(set, &ret_set, sizeof *set);
5790
5791 return LY_SUCCESS;
5792}
5793
5794/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005795 * @brief Move context @p set to a schema node and all its descendants. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005796 *
5797 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005798 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005799 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005800 * @param[in] options XPath options.
5801 * @return LY_ERR
5802 */
5803static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005804moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005805{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005806 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005807 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005808 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005809
Michal Vaskod3678892020-05-21 10:06:58 +02005810 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005811 return LY_SUCCESS;
5812 }
5813
5814 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005815 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 +02005816 return LY_EVALID;
5817 }
5818
Michal Vasko03ff5a72019-09-11 13:49:33 +02005819 orig_used = set->used;
5820 for (i = 0; i < orig_used; ++i) {
5821 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005822 if (set->val.scnodes[i].in_ctx != -2) {
5823 continue;
5824 }
5825
5826 /* remember context node */
5827 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005828 } else {
5829 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005830 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005831
5832 /* TREE DFS */
5833 start = set->val.scnodes[i].scnode;
5834 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005835 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5836 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005837 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005838 }
5839
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005840 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005841 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005842 uint32_t idx;
5843
5844 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005845 set->val.scnodes[idx].in_ctx = 1;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005846 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005847 /* we will process it later in the set */
5848 goto skip_children;
5849 }
5850 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005851 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005852 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005853 } else if (rc == LY_EINVAL) {
5854 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005855 }
5856
5857next_iter:
5858 /* TREE DFS NEXT ELEM */
5859 /* select element for the next run - children first */
5860 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005861 if (!next) {
5862skip_children:
5863 /* no children, so try siblings, but only if it's not the start,
5864 * that is considered to be the root and it's siblings are not traversed */
5865 if (elem != start) {
5866 next = elem->next;
5867 } else {
5868 break;
5869 }
5870 }
5871 while (!next) {
5872 /* no siblings, go back through the parents */
5873 if (elem->parent == start) {
5874 /* we are done, no next element to process */
5875 break;
5876 }
5877 /* parent is already processed, go to its sibling */
5878 elem = elem->parent;
5879 next = elem->next;
5880 }
5881 }
5882 }
5883
5884 return LY_SUCCESS;
5885}
5886
5887/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005888 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005889 * Indirectly context position aware.
5890 *
5891 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005892 * @param[in] mod Matching metadata module, NULL for any.
5893 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005894 * @return LY_ERR
5895 */
5896static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005897moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005898{
Michal Vasko9f96a052020-03-10 09:41:45 +01005899 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005900
Michal Vaskod3678892020-05-21 10:06:58 +02005901 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005902 return LY_SUCCESS;
5903 }
5904
5905 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005906 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 +02005907 return LY_EVALID;
5908 }
5909
Radek Krejci1deb5be2020-08-26 16:43:36 +02005910 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005911 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005912
5913 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5914 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005915 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005916 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005917
5918 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005919 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005920 continue;
5921 }
5922
Michal Vaskod3678892020-05-21 10:06:58 +02005923 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005924 /* match */
5925 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005926 set->val.meta[i].meta = sub;
5927 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005928 /* pos does not change */
5929 replaced = 1;
5930 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005931 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 +02005932 }
5933 ++i;
5934 }
5935 }
5936 }
5937
5938 if (!replaced) {
5939 /* no match */
5940 set_remove_node(set, i);
5941 }
5942 }
5943
5944 return LY_SUCCESS;
5945}
5946
5947/**
5948 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005949 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005950 *
5951 * @param[in,out] set1 Set to use for the result.
5952 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005953 * @return LY_ERR
5954 */
5955static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005956moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005957{
5958 LY_ERR rc;
5959
Michal Vaskod3678892020-05-21 10:06:58 +02005960 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005961 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 +02005962 return LY_EVALID;
5963 }
5964
5965 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005966 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005967 return LY_SUCCESS;
5968 }
5969
Michal Vaskod3678892020-05-21 10:06:58 +02005970 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005971 memcpy(set1, set2, sizeof *set1);
5972 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005973 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005974 return LY_SUCCESS;
5975 }
5976
5977 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005978 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005979
5980 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005981 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005982 LY_CHECK_RET(rc);
5983
5984 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005985 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005986
5987 return LY_SUCCESS;
5988}
5989
5990/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005991 * @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 +02005992 * Context position aware.
5993 *
5994 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005995 * @param[in] mod Matching metadata module, NULL for any.
5996 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005997 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005998 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5999 */
6000static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006001moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006002{
Michal Vasko9f96a052020-03-10 09:41:45 +01006003 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006004 struct lyxp_set *set_all_desc = NULL;
6005 LY_ERR rc;
6006
Michal Vaskod3678892020-05-21 10:06:58 +02006007 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006008 return LY_SUCCESS;
6009 }
6010
6011 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006012 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 +02006013 return LY_EVALID;
6014 }
6015
Michal Vasko03ff5a72019-09-11 13:49:33 +02006016 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6017 * but it likely won't be used much, so it's a waste of time */
6018 /* copy the context */
6019 set_all_desc = set_copy(set);
6020 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006021 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006022 if (rc != LY_SUCCESS) {
6023 lyxp_set_free(set_all_desc);
6024 return rc;
6025 }
6026 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006027 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006028 if (rc != LY_SUCCESS) {
6029 lyxp_set_free(set_all_desc);
6030 return rc;
6031 }
6032 lyxp_set_free(set_all_desc);
6033
Radek Krejci1deb5be2020-08-26 16:43:36 +02006034 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006035 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006036
6037 /* only attributes of an elem can be in the result, skip all the rest,
6038 * we have all attributes qualified in lyd tree */
6039 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006040 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006041 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006042 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006043 continue;
6044 }
6045
Michal Vaskod3678892020-05-21 10:06:58 +02006046 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006047 /* match */
6048 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006049 set->val.meta[i].meta = sub;
6050 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006051 /* pos does not change */
6052 replaced = 1;
6053 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006054 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 +02006055 }
6056 ++i;
6057 }
6058 }
6059 }
6060
6061 if (!replaced) {
6062 /* no match */
6063 set_remove_node(set, i);
6064 }
6065 }
6066
6067 return LY_SUCCESS;
6068}
6069
6070/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006071 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6072 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006073 *
6074 * @param[in] parent Current parent.
6075 * @param[in] parent_pos Position of @p parent.
6076 * @param[in] parent_type Node type of @p parent.
6077 * @param[in,out] to_set Set to use.
6078 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006079 * @param[in] options XPath options.
6080 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6081 */
6082static LY_ERR
6083moveto_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 +02006084 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006085{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006086 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006087 LY_ERR rc;
6088
6089 switch (parent_type) {
6090 case LYXP_NODE_ROOT:
6091 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006092 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006093
Michal Vasko61ac2f62020-05-25 12:39:51 +02006094 /* add all top-level nodes as elements */
6095 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006096 break;
6097 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006098 /* add just the text node of this term element node */
6099 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006100 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6101 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6102 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006103 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006104 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006105
6106 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006107 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006108 break;
6109 default:
6110 LOGINT_RET(parent->schema->module->ctx);
6111 }
6112
Michal Vasko61ac2f62020-05-25 12:39:51 +02006113 /* add all top-level nodes as elements */
6114 LY_LIST_FOR(first, iter) {
6115 /* context check */
6116 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6117 continue;
6118 }
6119
6120 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006121 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006122 return LY_EINCOMPLETE;
6123 }
6124
6125 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6126 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6127
6128 /* also add all the children of this node, recursively */
6129 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6130 LY_CHECK_RET(rc);
6131 }
6132 }
6133
Michal Vasko03ff5a72019-09-11 13:49:33 +02006134 return LY_SUCCESS;
6135}
6136
6137/**
6138 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6139 * (or LYXP_SET_EMPTY). Context position aware.
6140 *
6141 * @param[in,out] set Set to use.
6142 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6143 * @param[in] options XPath options.
6144 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6145 */
6146static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006147moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006148{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006149 struct lyxp_set ret_set;
6150 LY_ERR rc;
6151
Michal Vaskod3678892020-05-21 10:06:58 +02006152 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006153 return LY_SUCCESS;
6154 }
6155
6156 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006157 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 +02006158 return LY_EVALID;
6159 }
6160
6161 /* nothing to do */
6162 if (!all_desc) {
6163 return LY_SUCCESS;
6164 }
6165
Michal Vasko03ff5a72019-09-11 13:49:33 +02006166 /* add all the children, they get added recursively */
6167 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006168 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006169 /* copy the current node to tmp */
6170 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6171
6172 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006173 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006174 continue;
6175 }
6176
Michal Vasko03ff5a72019-09-11 13:49:33 +02006177 /* add all the children */
6178 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 +02006179 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006180 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006181 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006182 return rc;
6183 }
6184 }
6185
6186 /* use the temporary set as the current one */
6187 ret_set.ctx_pos = set->ctx_pos;
6188 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006189 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006190 memcpy(set, &ret_set, sizeof *set);
6191
6192 return LY_SUCCESS;
6193}
6194
6195/**
6196 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6197 * (or LYXP_SET_EMPTY).
6198 *
6199 * @param[in,out] set Set to use.
6200 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6201 * @param[in] options XPath options.
6202 * @return LY_ERR
6203 */
6204static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006205moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006206{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006207 uint32_t getnext_opts;
6208 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006209 const struct lysc_node *iter, *start_parent;
6210 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006211
Michal Vaskod3678892020-05-21 10:06:58 +02006212 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006213 return LY_SUCCESS;
6214 }
6215
6216 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006217 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 +02006218 return LY_EVALID;
6219 }
6220
6221 /* nothing to do */
6222 if (!all_desc) {
6223 return LY_SUCCESS;
6224 }
6225
Michal Vasko519fd602020-05-26 12:17:39 +02006226 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006227 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006228 if (options & LYXP_SCNODE_OUTPUT) {
6229 getnext_opts |= LYS_GETNEXT_OUTPUT;
6230 }
6231
6232 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006233 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006234 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006235 if (set->val.scnodes[i].in_ctx != -2) {
6236 continue;
6237 }
6238
Michal Vasko519fd602020-05-26 12:17:39 +02006239 /* remember context node */
6240 set->val.scnodes[i].in_ctx = -1;
6241 } else {
6242 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006243 }
6244
Michal Vasko519fd602020-05-26 12:17:39 +02006245 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006246
Michal Vasko519fd602020-05-26 12:17:39 +02006247 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6248 /* it can actually be in any module, it's all <running> */
6249 mod_idx = 0;
6250 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6251 iter = NULL;
6252 /* module may not be implemented */
6253 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6254 /* context check */
6255 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6256 continue;
6257 }
6258
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006259 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006260 /* throw away the insert index, we want to consider that node again, recursively */
6261 }
6262 }
6263
6264 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6265 iter = NULL;
6266 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006267 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006268 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006269 continue;
6270 }
6271
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006272 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006273 }
6274 }
6275 }
6276
6277 return LY_SUCCESS;
6278}
6279
6280/**
6281 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6282 * (or LYXP_SET_EMPTY). Context position aware.
6283 *
6284 * @param[in] set Set to use.
6285 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6286 * @param[in] options XPath options.
6287 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6288 */
6289static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006290moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006291{
6292 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006293 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006294 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006295
Michal Vaskod3678892020-05-21 10:06:58 +02006296 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006297 return LY_SUCCESS;
6298 }
6299
6300 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006301 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 +02006302 return LY_EVALID;
6303 }
6304
6305 if (all_desc) {
6306 /* <path>//.. == <path>//./.. */
6307 rc = moveto_self(set, 1, options);
6308 LY_CHECK_RET(rc);
6309 }
6310
Radek Krejci1deb5be2020-08-26 16:43:36 +02006311 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006312 node = set->val.nodes[i].node;
6313
6314 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6315 new_node = (struct lyd_node *)node->parent;
6316 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6317 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006318 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6319 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006320 if (!new_node) {
6321 LOGINT_RET(set->ctx);
6322 }
6323 } else {
6324 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006325 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006326 continue;
6327 }
6328
Michal Vaskoa1424542019-11-14 16:08:52 +01006329 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006330 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 +02006331 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006332 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006333
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006334 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006335 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006336 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006337
Michal Vasko03ff5a72019-09-11 13:49:33 +02006338 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006339 /* 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 +02006340 new_type = LYXP_NODE_ELEM;
6341 }
6342
Michal Vasko03ff5a72019-09-11 13:49:33 +02006343 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006344 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006345 } else {
6346 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006347 }
6348 }
6349
Michal Vasko2caefc12019-11-14 16:07:56 +01006350 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006351 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006352
6353 return LY_SUCCESS;
6354}
6355
6356/**
6357 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6358 * (or LYXP_SET_EMPTY).
6359 *
6360 * @param[in] set Set to use.
6361 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6362 * @param[in] options XPath options.
6363 * @return LY_ERR
6364 */
6365static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006366moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006367{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006368 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006369 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006370 const struct lysc_node *node, *new_node;
6371 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006372
Michal Vaskod3678892020-05-21 10:06:58 +02006373 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006374 return LY_SUCCESS;
6375 }
6376
6377 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006378 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 +02006379 return LY_EVALID;
6380 }
6381
6382 if (all_desc) {
6383 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006384 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006385 }
6386
Michal Vasko03ff5a72019-09-11 13:49:33 +02006387 orig_used = set->used;
6388 for (i = 0; i < orig_used; ++i) {
6389 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006390 if (set->val.scnodes[i].in_ctx != -2) {
6391 continue;
6392 }
6393
6394 /* remember context node */
6395 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01006396 } else {
6397 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006398 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006399
6400 node = set->val.scnodes[i].scnode;
6401
6402 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006403 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006404 } else {
6405 /* root does not have a parent */
6406 continue;
6407 }
6408
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006409 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006410 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006411 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006412
Michal Vasko03ff5a72019-09-11 13:49:33 +02006413 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006414 /* 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 +02006415 new_type = LYXP_NODE_ELEM;
6416 }
6417
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006418 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6419 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006420 set->val.scnodes[idx].in_ctx = 2;
6421 temp_ctx = 1;
6422 }
6423 }
6424
6425 if (temp_ctx) {
6426 for (i = 0; i < orig_used; ++i) {
6427 if (set->val.scnodes[i].in_ctx == 2) {
6428 set->val.scnodes[i].in_ctx = 1;
6429 }
6430 }
6431 }
6432
6433 return LY_SUCCESS;
6434}
6435
6436/**
6437 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6438 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6439 *
6440 * @param[in,out] set1 Set to use for the result.
6441 * @param[in] set2 Set acting as the second operand for @p op.
6442 * @param[in] op Comparison operator to process.
6443 * @param[in] options XPath options.
6444 * @return LY_ERR
6445 */
6446static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006447moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006448{
6449 /*
6450 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6451 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6452 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6453 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6454 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6455 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6456 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6457 *
6458 * '=' or '!='
6459 * BOOLEAN + BOOLEAN
6460 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6461 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6462 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6463 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6464 * NUMBER + NUMBER
6465 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6466 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6467 * STRING + STRING
6468 *
6469 * '<=', '<', '>=', '>'
6470 * NUMBER + NUMBER
6471 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6472 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6473 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6474 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6475 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6476 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6477 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6478 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6479 */
6480 struct lyxp_set iter1, iter2;
6481 int result;
6482 int64_t i;
6483 LY_ERR rc;
6484
Michal Vasko004d3152020-06-11 19:59:22 +02006485 memset(&iter1, 0, sizeof iter1);
6486 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006487
6488 /* iterative evaluation with node-sets */
6489 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6490 if (set1->type == LYXP_SET_NODE_SET) {
6491 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006492 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006493 switch (set2->type) {
6494 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006495 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006496 break;
6497 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006498 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006499 break;
6500 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006501 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006502 break;
6503 }
6504 LY_CHECK_RET(rc);
6505
Michal Vasko4c7763f2020-07-27 17:40:37 +02006506 /* canonize set2 */
6507 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6508
6509 /* compare recursively */
6510 rc = moveto_op_comp(&iter1, &iter2, op, options);
6511 lyxp_set_free_content(&iter2);
6512 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006513
6514 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006515 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006516 set_fill_boolean(set1, 1);
6517 return LY_SUCCESS;
6518 }
6519 }
6520 } else {
6521 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006522 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006523 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006524 case LYXP_SET_NUMBER:
6525 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6526 break;
6527 case LYXP_SET_BOOLEAN:
6528 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6529 break;
6530 default:
6531 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6532 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006533 }
6534 LY_CHECK_RET(rc);
6535
Michal Vasko4c7763f2020-07-27 17:40:37 +02006536 /* canonize set1 */
6537 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 +02006538
Michal Vasko4c7763f2020-07-27 17:40:37 +02006539 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006540 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006541 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006542 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006543
6544 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006545 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006546 set_fill_boolean(set1, 1);
6547 return LY_SUCCESS;
6548 }
6549 }
6550 }
6551
6552 /* false for all nodes */
6553 set_fill_boolean(set1, 0);
6554 return LY_SUCCESS;
6555 }
6556
6557 /* first convert properly */
6558 if ((op[0] == '=') || (op[0] == '!')) {
6559 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006560 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6561 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006562 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006563 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006564 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006565 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006566 LY_CHECK_RET(rc);
6567 } /* else we have 2 strings */
6568 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006569 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006570 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006571 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006572 LY_CHECK_RET(rc);
6573 }
6574
6575 assert(set1->type == set2->type);
6576
6577 /* compute result */
6578 if (op[0] == '=') {
6579 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006580 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006581 } else if (set1->type == LYXP_SET_NUMBER) {
6582 result = (set1->val.num == set2->val.num);
6583 } else {
6584 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006585 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006586 }
6587 } else if (op[0] == '!') {
6588 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006589 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006590 } else if (set1->type == LYXP_SET_NUMBER) {
6591 result = (set1->val.num != set2->val.num);
6592 } else {
6593 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006594 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006595 }
6596 } else {
6597 assert(set1->type == LYXP_SET_NUMBER);
6598 if (op[0] == '<') {
6599 if (op[1] == '=') {
6600 result = (set1->val.num <= set2->val.num);
6601 } else {
6602 result = (set1->val.num < set2->val.num);
6603 }
6604 } else {
6605 if (op[1] == '=') {
6606 result = (set1->val.num >= set2->val.num);
6607 } else {
6608 result = (set1->val.num > set2->val.num);
6609 }
6610 }
6611 }
6612
6613 /* assign result */
6614 if (result) {
6615 set_fill_boolean(set1, 1);
6616 } else {
6617 set_fill_boolean(set1, 0);
6618 }
6619
6620 return LY_SUCCESS;
6621}
6622
6623/**
6624 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6625 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6626 *
6627 * @param[in,out] set1 Set to use for the result.
6628 * @param[in] set2 Set acting as the second operand for @p op.
6629 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006630 * @return LY_ERR
6631 */
6632static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006633moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006634{
6635 LY_ERR rc;
6636
6637 /* unary '-' */
6638 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006639 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006640 LY_CHECK_RET(rc);
6641 set1->val.num *= -1;
6642 lyxp_set_free(set2);
6643 return LY_SUCCESS;
6644 }
6645
6646 assert(set1 && set2);
6647
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006648 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006649 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006650 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006651 LY_CHECK_RET(rc);
6652
6653 switch (op[0]) {
6654 /* '+' */
6655 case '+':
6656 set1->val.num += set2->val.num;
6657 break;
6658
6659 /* '-' */
6660 case '-':
6661 set1->val.num -= set2->val.num;
6662 break;
6663
6664 /* '*' */
6665 case '*':
6666 set1->val.num *= set2->val.num;
6667 break;
6668
6669 /* 'div' */
6670 case 'd':
6671 set1->val.num /= set2->val.num;
6672 break;
6673
6674 /* 'mod' */
6675 case 'm':
6676 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6677 break;
6678
6679 default:
6680 LOGINT_RET(set1->ctx);
6681 }
6682
6683 return LY_SUCCESS;
6684}
6685
6686/*
6687 * eval functions
6688 *
6689 * They execute a parsed XPath expression on some data subtree.
6690 */
6691
6692/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006693 * @brief Evaluate Predicate. Logs directly on error.
6694 *
Michal Vaskod3678892020-05-21 10:06:58 +02006695 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006696 *
6697 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006698 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006699 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6700 * @param[in] options XPath options.
6701 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6702 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6703 */
6704static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006705eval_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 +02006706{
6707 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006708 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006709 uint32_t orig_pos, orig_size;
6710 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006711 struct lyxp_set set2;
6712 struct lyd_node *orig_parent;
6713
6714 /* '[' */
6715 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006716 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006717 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006718
6719 if (!set) {
6720only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006721 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006722 LY_CHECK_RET(rc);
6723 } else if (set->type == LYXP_SET_NODE_SET) {
6724 /* 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 +01006725 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006726
6727 /* empty set, nothing to evaluate */
6728 if (!set->used) {
6729 goto only_parse;
6730 }
6731
Michal Vasko004d3152020-06-11 19:59:22 +02006732 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006733 orig_pos = 0;
6734 orig_size = set->used;
6735 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006736 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006737 set_init(&set2, set);
6738 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6739 /* remember the node context position for position() and context size for last(),
6740 * predicates should always be evaluated with respect to the child axis (since we do
6741 * not support explicit axes) so we assign positions based on their parents */
6742 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6743 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6744 orig_pos = 1;
6745 } else {
6746 ++orig_pos;
6747 }
6748
6749 set2.ctx_pos = orig_pos;
6750 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006751 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006752
Michal Vasko004d3152020-06-11 19:59:22 +02006753 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006754 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006755 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006756 return rc;
6757 }
6758
6759 /* number is a position */
6760 if (set2.type == LYXP_SET_NUMBER) {
6761 if ((long long)set2.val.num == orig_pos) {
6762 set2.val.num = 1;
6763 } else {
6764 set2.val.num = 0;
6765 }
6766 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006767 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006768
6769 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006770 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006771 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006772 }
6773 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006774 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006775
6776 } else if (set->type == LYXP_SET_SCNODE_SET) {
6777 for (i = 0; i < set->used; ++i) {
6778 if (set->val.scnodes[i].in_ctx == 1) {
6779 /* there is a currently-valid node */
6780 break;
6781 }
6782 }
6783 /* empty set, nothing to evaluate */
6784 if (i == set->used) {
6785 goto only_parse;
6786 }
6787
Michal Vasko004d3152020-06-11 19:59:22 +02006788 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006789
Michal Vasko03ff5a72019-09-11 13:49:33 +02006790 /* set special in_ctx to all the valid snodes */
6791 pred_in_ctx = set_scnode_new_in_ctx(set);
6792
6793 /* use the valid snodes one-by-one */
6794 for (i = 0; i < set->used; ++i) {
6795 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6796 continue;
6797 }
6798 set->val.scnodes[i].in_ctx = 1;
6799
Michal Vasko004d3152020-06-11 19:59:22 +02006800 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006801
Michal Vasko004d3152020-06-11 19:59:22 +02006802 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006803 LY_CHECK_RET(rc);
6804
6805 set->val.scnodes[i].in_ctx = pred_in_ctx;
6806 }
6807
6808 /* restore the state as it was before the predicate */
6809 for (i = 0; i < set->used; ++i) {
6810 if (set->val.scnodes[i].in_ctx == 1) {
6811 set->val.scnodes[i].in_ctx = 0;
6812 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6813 set->val.scnodes[i].in_ctx = 1;
6814 }
6815 }
6816
6817 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006818 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006819 set_fill_set(&set2, set);
6820
Michal Vasko004d3152020-06-11 19:59:22 +02006821 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006822 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006823 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006824 return rc;
6825 }
6826
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006827 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006828 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006829 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006830 }
Michal Vaskod3678892020-05-21 10:06:58 +02006831 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006832 }
6833
6834 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006835 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006836 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006837 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006838 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006839
6840 return LY_SUCCESS;
6841}
6842
6843/**
Michal Vaskod3678892020-05-21 10:06:58 +02006844 * @brief Evaluate Literal. Logs directly on error.
6845 *
6846 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006847 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006848 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6849 */
6850static void
Michal Vasko40308e72020-10-20 16:38:40 +02006851eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006852{
6853 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006854 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006855 set_fill_string(set, "", 0);
6856 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006857 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 +02006858 }
6859 }
6860 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006861 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006862 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006863}
6864
6865/**
Michal Vasko004d3152020-06-11 19:59:22 +02006866 * @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 +02006867 *
Michal Vasko004d3152020-06-11 19:59:22 +02006868 * @param[in] exp Full parsed XPath expression.
6869 * @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 +02006870 * @param[in] ctx_node Found schema node as the context for the predicate.
6871 * @param[in] cur_mod Current module for the expression.
6872 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006873 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006874 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006875 * @param[out] predicates Parsed predicates.
6876 * @param[out] pred_type Type of @p predicates.
6877 * @return LY_SUCCESS on success,
6878 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006879 */
6880static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006881eval_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 +02006882 const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_PREFIX_FORMAT format, void *prefix_data,
6883 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006884{
Michal Vasko004d3152020-06-11 19:59:22 +02006885 LY_ERR ret = LY_SUCCESS;
6886 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006887 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006888 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006889 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006890 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006891
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006892 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006893
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006894 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006895 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006896 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006897 return LY_EINVAL;
6898 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006899 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 +02006900 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006901
Michal Vasko004d3152020-06-11 19:59:22 +02006902 /* learn where the predicates end */
6903 e_idx = *tok_idx;
6904 while (key_count) {
6905 /* '[' */
6906 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6907 return LY_EINVAL;
6908 }
6909 ++e_idx;
6910
6911 /* ']' */
6912 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6913 ++e_idx;
6914 }
6915 ++e_idx;
6916
6917 /* another presumably key predicate parsed */
6918 --key_count;
6919 }
Michal Vasko004d3152020-06-11 19:59:22 +02006920 } else {
6921 /* learn just where this single predicate ends */
6922 e_idx = *tok_idx;
6923
Michal Vaskod3678892020-05-21 10:06:58 +02006924 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006925 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6926 return LY_EINVAL;
6927 }
6928 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006929
6930 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006931 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6932 ++e_idx;
6933 }
6934 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006935 }
6936
Michal Vasko004d3152020-06-11 19:59:22 +02006937 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6938 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6939
6940 /* turn logging off */
6941 prev_lo = ly_log_options(0);
6942
6943 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006944 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
6945 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02006946
6947 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006948 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
6949 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006950 LY_CHECK_GOTO(ret, cleanup);
6951
6952 /* success, the predicate must include all the needed information for hash-based search */
6953 *tok_idx = e_idx;
6954
6955cleanup:
6956 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006957 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02006958 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006959}
6960
6961/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006962 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
6963 * data node(s) could be found using a single hash-based search.
6964 *
6965 * @param[in] node Next context node to check.
6966 * @param[in] name Expected node name.
6967 * @param[in] name_len Length of @p name.
6968 * @param[in] moveto_mod Expected node module.
6969 * @param[in] root_type XPath root type.
6970 * @param[in] no_prefix Whether the node name was unprefixed.
6971 * @param[in] format Prefix format.
6972 * @param[in,out] found Previously found node, is updated.
6973 * @return LY_SUCCESS on success,
6974 * @return LY_ENOT if the whole check failed and hashes cannot be used.
6975 */
6976static LY_ERR
6977eval_name_test_with_predicate_get_scnode(const struct lyd_node *node, const char *name, uint16_t name_len,
6978 const struct lys_module *moveto_mod, enum lyxp_node_type root_type, ly_bool no_prefix, LY_PREFIX_FORMAT format,
6979 const struct lysc_node **found)
6980{
6981 const struct lysc_node *scnode;
6982 const struct lys_module *mod;
6983 uint32_t idx = 0;
6984
6985continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02006986 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006987 if (!node) {
6988 if (no_prefix && (format == LY_PREF_JSON)) {
6989 /* search all modules for a single match */
6990 while ((mod = ly_ctx_get_module_iter(moveto_mod->ctx, &idx))) {
6991 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
6992 if (scnode) {
6993 /* we have found a match */
6994 break;
6995 }
6996 }
6997
Michal Vasko7d1d0912020-10-16 08:38:30 +02006998 if (!scnode) {
6999 /* all modules searched */
7000 idx = 0;
7001 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007002 } else {
7003 /* search in top-level */
7004 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7005 }
7006 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
7007 if (no_prefix && (format == LY_PREF_JSON)) {
7008 /* we must adjust the module to inherit the one from the context node */
7009 moveto_mod = node->schema->module;
7010 }
7011
7012 /* search in children, do not repeat the same search */
7013 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007014 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007015
7016 /* additional context check */
7017 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7018 scnode = NULL;
7019 }
7020
7021 if (scnode) {
7022 if (*found) {
7023 /* we found a schema node with the same name but at different level, give up, too complicated
7024 * (more hash-based searches would be required, not supported) */
7025 return LY_ENOT;
7026 } else {
7027 /* remember the found schema node and continue to make sure it can be used */
7028 *found = scnode;
7029 }
7030 }
7031
7032 if (idx) {
7033 /* continue searching all the following models */
7034 goto continue_search;
7035 }
7036
7037 return LY_SUCCESS;
7038}
7039
7040/**
Michal Vaskod3678892020-05-21 10:06:58 +02007041 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7042 *
7043 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7044 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7045 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7046 *
7047 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007048 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007049 * @param[in] attr_axis Whether to search attributes or standard nodes.
7050 * @param[in] all_desc Whether to search all the descendants or children only.
7051 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7052 * @param[in] options XPath options.
7053 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7054 */
7055static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007056eval_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 +02007057 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007058{
Michal Vaskod3678892020-05-21 10:06:58 +02007059 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007060 const char *ncname, *ncname_dict = NULL;
7061 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007062 const struct lys_module *moveto_mod = NULL;
7063 const struct lysc_node *scnode = NULL, *ctx_scnode;
Michal Vasko004d3152020-06-11 19:59:22 +02007064 struct ly_path_predicate *predicates = NULL;
7065 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007066 LY_ERR rc = LY_SUCCESS;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007067 ly_bool no_prefix;
Michal Vaskod3678892020-05-21 10:06:58 +02007068
7069 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007070 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007071 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007072
7073 if (!set) {
7074 goto moveto;
7075 }
7076
Michal Vasko004d3152020-06-11 19:59:22 +02007077 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7078 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007079
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007080 /* get the first schema context node */
7081 if (set->used) {
7082 if (set->type == LYXP_SET_NODE_SET) {
7083 ctx_scnode = set->val.nodes[0].node ? set->val.nodes[0].node->schema : NULL;
7084 } else {
7085 ctx_scnode = set->val.scnodes[0].scnode;
7086 }
7087 } else {
7088 ctx_scnode = NULL;
7089 }
7090
7091 /* parse (and skip) module name, use any context node (if available) just so we get some moveto_mod if there
7092 * is no prefix for ::LY_PREF_JSON */
7093 rc = moveto_resolve_model(&ncname, &ncname_len, set, ctx_scnode, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007094 LY_CHECK_GOTO(rc, cleanup);
7095
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007096 if (ncname_len == exp->tok_len[*tok_idx - 1]) {
7097 /* remember that there is no prefix */
7098 no_prefix = 1;
7099 } else {
7100 no_prefix = 0;
7101 }
7102
Michal Vaskod3678892020-05-21 10:06:58 +02007103 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
7104 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007105 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007106 if (eval_name_test_with_predicate_get_scnode(set->val.nodes[i].node, ncname, ncname_len, moveto_mod,
7107 set->root_type, no_prefix, set->format, &scnode)) {
7108 /* check failed */
7109 scnode = NULL;
7110 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007111 }
7112 }
7113
Michal Vasko004d3152020-06-11 19:59:22 +02007114 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7115 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007116 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7117 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007118 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007119 scnode = NULL;
7120 }
7121 }
7122 }
7123
Michal Vasko004d3152020-06-11 19:59:22 +02007124 if (!scnode && moveto_mod) {
7125 /* we are not able to match based on a schema node and not all the modules match,
7126 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007127 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007128 }
7129
7130moveto:
7131 /* move to the attribute(s), data node(s), or schema node(s) */
7132 if (attr_axis) {
7133 if (set && (options & LYXP_SCNODE_ALL)) {
7134 set_scnode_clear_ctx(set);
7135 } else {
7136 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007137 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007138 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007139 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007140 }
7141 LY_CHECK_GOTO(rc, cleanup);
7142 }
7143 } else {
7144 if (set && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007145 int64_t i;
7146
Michal Vaskod3678892020-05-21 10:06:58 +02007147 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007148 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007149 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007150 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007151 }
7152 LY_CHECK_GOTO(rc, cleanup);
7153
7154 for (i = set->used - 1; i > -1; --i) {
7155 if (set->val.scnodes[i].in_ctx > 0) {
7156 break;
7157 }
7158 }
7159 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007160 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02007161 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02007162 ncname_len, ncname, ncname - exp->expr, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007163 free(path);
7164 }
7165 } else {
7166 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007167 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007168 } else {
7169 if (scnode) {
7170 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007171 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007172 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007173 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007174 }
7175 }
7176 LY_CHECK_GOTO(rc, cleanup);
7177 }
7178 }
7179
7180 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007181 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7182 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007183 LY_CHECK_RET(rc);
7184 }
7185
7186cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007187 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007188 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007189 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007190 }
Michal Vaskod3678892020-05-21 10:06:58 +02007191 return rc;
7192}
7193
7194/**
7195 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7196 *
7197 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7198 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7199 * [8] NodeType ::= 'text' | 'node'
7200 *
7201 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007202 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007203 * @param[in] attr_axis Whether to search attributes or standard nodes.
7204 * @param[in] all_desc Whether to search all the descendants or children only.
7205 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7206 * @param[in] options XPath options.
7207 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7208 */
7209static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007210eval_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 +02007211 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007212{
7213 LY_ERR rc;
7214
7215 /* TODO */
7216 (void)attr_axis;
7217 (void)all_desc;
7218
7219 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007220 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007221 if (set->type == LYXP_SET_SCNODE_SET) {
7222 set_scnode_clear_ctx(set);
7223 /* just for the debug message below */
7224 set = NULL;
7225 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007226 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007227 rc = xpath_node(NULL, 0, set, options);
7228 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007229 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007230 rc = xpath_text(NULL, 0, set, options);
7231 }
7232 LY_CHECK_RET(rc);
7233 }
7234 }
7235 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007236 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007237 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007238
7239 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007240 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007241 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007242 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007243 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007244
7245 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007246 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007247 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007248 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007249 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007250
7251 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007252 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7253 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007254 LY_CHECK_RET(rc);
7255 }
7256
7257 return LY_SUCCESS;
7258}
7259
7260/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007261 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7262 *
7263 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7264 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007265 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007266 *
7267 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007268 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007269 * @param[in] all_desc Whether to search all the descendants or children only.
7270 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7271 * @param[in] options XPath options.
7272 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7273 */
7274static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007275eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7276 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007277{
Radek Krejci857189e2020-09-01 13:26:36 +02007278 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007279 LY_ERR rc;
7280
7281 goto step;
7282 do {
7283 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007284 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007285 all_desc = 0;
7286 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007287 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007288 all_desc = 1;
7289 }
7290 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007291 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007292 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007293
7294step:
Michal Vaskod3678892020-05-21 10:06:58 +02007295 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007296 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007297 attr_axis = 1;
7298
7299 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007300 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007301 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007302 } else {
7303 attr_axis = 0;
7304 }
7305
Michal Vasko03ff5a72019-09-11 13:49:33 +02007306 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007307 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007308 case LYXP_TOKEN_DOT:
7309 /* evaluate '.' */
7310 if (set && (options & LYXP_SCNODE_ALL)) {
7311 rc = moveto_scnode_self(set, all_desc, options);
7312 } else {
7313 rc = moveto_self(set, all_desc, options);
7314 }
7315 LY_CHECK_RET(rc);
7316 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007317 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007318 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007319 break;
7320
7321 case LYXP_TOKEN_DDOT:
7322 /* evaluate '..' */
7323 if (set && (options & LYXP_SCNODE_ALL)) {
7324 rc = moveto_scnode_parent(set, all_desc, options);
7325 } else {
7326 rc = moveto_parent(set, all_desc, options);
7327 }
7328 LY_CHECK_RET(rc);
7329 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007330 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007331 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007332 break;
7333
Michal Vasko03ff5a72019-09-11 13:49:33 +02007334 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007335 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007336 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007337 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007338 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007339
Michal Vaskod3678892020-05-21 10:06:58 +02007340 case LYXP_TOKEN_NODETYPE:
7341 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007342 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007343 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007344 break;
7345
7346 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007347 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007348 }
Michal Vasko004d3152020-06-11 19:59:22 +02007349 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007350
7351 return LY_SUCCESS;
7352}
7353
7354/**
7355 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7356 *
7357 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7358 *
7359 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007360 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007361 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7362 * @param[in] options XPath options.
7363 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7364 */
7365static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007366eval_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 +02007367{
Radek Krejci857189e2020-09-01 13:26:36 +02007368 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007369
7370 if (set) {
7371 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007372 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007373 }
7374
7375 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007376 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007377 /* evaluate '/' - deferred */
7378 all_desc = 0;
7379 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007380 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007381 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007382
Michal Vasko004d3152020-06-11 19:59:22 +02007383 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007384 return LY_SUCCESS;
7385 }
Michal Vasko004d3152020-06-11 19:59:22 +02007386 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007387 case LYXP_TOKEN_DOT:
7388 case LYXP_TOKEN_DDOT:
7389 case LYXP_TOKEN_AT:
7390 case LYXP_TOKEN_NAMETEST:
7391 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007392 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007393 break;
7394 default:
7395 break;
7396 }
7397
Michal Vasko03ff5a72019-09-11 13:49:33 +02007398 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007399 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007400 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7401 all_desc = 1;
7402 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007403 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007404 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007405
Michal Vaskob0099a92020-08-31 14:55:23 +02007406 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007407 }
7408
7409 return LY_SUCCESS;
7410}
7411
7412/**
7413 * @brief Evaluate FunctionCall. Logs directly on error.
7414 *
Michal Vaskod3678892020-05-21 10:06:58 +02007415 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007416 *
7417 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007418 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007419 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7420 * @param[in] options XPath options.
7421 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7422 */
7423static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007424eval_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 +02007425{
7426 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007427
Radek Krejci1deb5be2020-08-26 16:43:36 +02007428 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007429 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007430 struct lyxp_set **args = NULL, **args_aux;
7431
7432 if (set) {
7433 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007434 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007435 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007436 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007437 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007438 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007439 xpath_func = &xpath_sum;
7440 }
7441 break;
7442 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007443 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007444 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007445 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007446 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007447 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007448 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007449 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007450 xpath_func = &xpath_true;
7451 }
7452 break;
7453 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007454 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007455 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007456 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007457 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007458 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007459 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007460 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007461 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007462 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007463 xpath_func = &xpath_deref;
7464 }
7465 break;
7466 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007467 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007468 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007469 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007470 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007471 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007472 xpath_func = &xpath_string;
7473 }
7474 break;
7475 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007476 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007477 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007478 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007479 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007480 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007481 xpath_func = &xpath_current;
7482 }
7483 break;
7484 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007485 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007486 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007487 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007488 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007489 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007490 xpath_func = &xpath_re_match;
7491 }
7492 break;
7493 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007494 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007495 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007496 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007497 xpath_func = &xpath_translate;
7498 }
7499 break;
7500 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007501 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007502 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007503 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007504 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007505 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007506 xpath_func = &xpath_bit_is_set;
7507 }
7508 break;
7509 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007510 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007511 xpath_func = &xpath_starts_with;
7512 }
7513 break;
7514 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007515 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007516 xpath_func = &xpath_derived_from;
7517 }
7518 break;
7519 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007520 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007521 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007522 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007523 xpath_func = &xpath_string_length;
7524 }
7525 break;
7526 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007527 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007528 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007529 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007530 xpath_func = &xpath_substring_after;
7531 }
7532 break;
7533 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007534 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007535 xpath_func = &xpath_substring_before;
7536 }
7537 break;
7538 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007539 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007540 xpath_func = &xpath_derived_from_or_self;
7541 }
7542 break;
7543 }
7544
7545 if (!xpath_func) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007546 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 +02007547 return LY_EVALID;
7548 }
7549 }
7550
7551 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007552 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007553 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007554
7555 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007556 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007557 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007558 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007559 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007560
7561 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007562 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007563 if (set) {
7564 args = malloc(sizeof *args);
7565 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7566 arg_count = 1;
7567 args[0] = set_copy(set);
7568 if (!args[0]) {
7569 rc = LY_EMEM;
7570 goto cleanup;
7571 }
7572
Michal Vasko004d3152020-06-11 19:59:22 +02007573 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007574 LY_CHECK_GOTO(rc, cleanup);
7575 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007576 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007577 LY_CHECK_GOTO(rc, cleanup);
7578 }
7579 }
Michal Vasko004d3152020-06-11 19:59:22 +02007580 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007581 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007582 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007583 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007584
7585 if (set) {
7586 ++arg_count;
7587 args_aux = realloc(args, arg_count * sizeof *args);
7588 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7589 args = args_aux;
7590 args[arg_count - 1] = set_copy(set);
7591 if (!args[arg_count - 1]) {
7592 rc = LY_EMEM;
7593 goto cleanup;
7594 }
7595
Michal Vasko004d3152020-06-11 19:59:22 +02007596 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007597 LY_CHECK_GOTO(rc, cleanup);
7598 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007599 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007600 LY_CHECK_GOTO(rc, cleanup);
7601 }
7602 }
7603
7604 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007605 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007606 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007607 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007608 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007609
7610 if (set) {
7611 /* evaluate function */
7612 rc = xpath_func(args, arg_count, set, options);
7613
7614 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007615 /* merge all nodes from arg evaluations */
7616 for (i = 0; i < arg_count; ++i) {
7617 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007618 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007619 }
7620 }
7621 } else {
7622 rc = LY_SUCCESS;
7623 }
7624
7625cleanup:
7626 for (i = 0; i < arg_count; ++i) {
7627 lyxp_set_free(args[i]);
7628 }
7629 free(args);
7630
7631 return rc;
7632}
7633
7634/**
7635 * @brief Evaluate Number. Logs directly on error.
7636 *
7637 * @param[in] ctx Context for errors.
7638 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007639 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007640 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7641 * @return LY_ERR
7642 */
7643static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007644eval_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 +02007645{
7646 long double num;
7647 char *endptr;
7648
7649 if (set) {
7650 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007651 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007652 if (errno) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007653 LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7654 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 +02007655 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007656 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007657 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007658 LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7659 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 +02007660 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007661 return LY_EVALID;
7662 }
7663
7664 set_fill_number(set, num);
7665 }
7666
7667 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007668 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007669 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007670 return LY_SUCCESS;
7671}
7672
7673/**
7674 * @brief Evaluate PathExpr. Logs directly on error.
7675 *
Michal Vaskod3678892020-05-21 10:06:58 +02007676 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007677 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7678 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7679 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007680 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007681 *
7682 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007683 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007684 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7685 * @param[in] options XPath options.
7686 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7687 */
7688static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007689eval_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 +02007690{
Radek Krejci857189e2020-09-01 13:26:36 +02007691 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007692 LY_ERR rc;
7693
Michal Vasko004d3152020-06-11 19:59:22 +02007694 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007695 case LYXP_TOKEN_PAR1:
7696 /* '(' Expr ')' */
7697
7698 /* '(' */
7699 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007700 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007701 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007702
7703 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007704 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007705 LY_CHECK_RET(rc);
7706
7707 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007708 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007709 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007710 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007711 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007712
7713 parent_pos_pred = 0;
7714 goto predicate;
7715
7716 case LYXP_TOKEN_DOT:
7717 case LYXP_TOKEN_DDOT:
7718 case LYXP_TOKEN_AT:
7719 case LYXP_TOKEN_NAMETEST:
7720 case LYXP_TOKEN_NODETYPE:
7721 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007722 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007723 LY_CHECK_RET(rc);
7724 break;
7725
7726 case LYXP_TOKEN_FUNCNAME:
7727 /* FunctionCall */
7728 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007729 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007730 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007731 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007732 }
7733 LY_CHECK_RET(rc);
7734
7735 parent_pos_pred = 1;
7736 goto predicate;
7737
Michal Vasko3e48bf32020-06-01 08:39:07 +02007738 case LYXP_TOKEN_OPER_PATH:
7739 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007740 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007741 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007742 LY_CHECK_RET(rc);
7743 break;
7744
7745 case LYXP_TOKEN_LITERAL:
7746 /* Literal */
7747 if (!set || (options & LYXP_SCNODE_ALL)) {
7748 if (set) {
7749 set_scnode_clear_ctx(set);
7750 }
Michal Vasko004d3152020-06-11 19:59:22 +02007751 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007752 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007753 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007754 }
7755
7756 parent_pos_pred = 1;
7757 goto predicate;
7758
7759 case LYXP_TOKEN_NUMBER:
7760 /* Number */
7761 if (!set || (options & LYXP_SCNODE_ALL)) {
7762 if (set) {
7763 set_scnode_clear_ctx(set);
7764 }
Michal Vasko004d3152020-06-11 19:59:22 +02007765 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007766 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007767 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007768 }
7769 LY_CHECK_RET(rc);
7770
7771 parent_pos_pred = 1;
7772 goto predicate;
7773
7774 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007775 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 +02007776 &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007777 return LY_EVALID;
7778 }
7779
7780 return LY_SUCCESS;
7781
7782predicate:
7783 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007784 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7785 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007786 LY_CHECK_RET(rc);
7787 }
7788
7789 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007790 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007791
7792 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007793 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007794 all_desc = 0;
7795 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007796 all_desc = 1;
7797 }
7798
7799 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007800 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007801 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007802
Michal Vasko004d3152020-06-11 19:59:22 +02007803 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007804 LY_CHECK_RET(rc);
7805 }
7806
7807 return LY_SUCCESS;
7808}
7809
7810/**
7811 * @brief Evaluate UnionExpr. Logs directly on error.
7812 *
Michal Vaskod3678892020-05-21 10:06:58 +02007813 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007814 *
7815 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007816 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007817 * @param[in] repeat How many times this expression is repeated.
7818 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7819 * @param[in] options XPath options.
7820 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7821 */
7822static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007823eval_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 +02007824{
7825 LY_ERR rc = LY_SUCCESS;
7826 struct lyxp_set orig_set, set2;
7827 uint16_t i;
7828
7829 assert(repeat);
7830
7831 set_init(&orig_set, set);
7832 set_init(&set2, set);
7833
7834 set_fill_set(&orig_set, set);
7835
Michal Vasko004d3152020-06-11 19:59:22 +02007836 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007837 LY_CHECK_GOTO(rc, cleanup);
7838
7839 /* ('|' PathExpr)* */
7840 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007841 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007842 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007843 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007844 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007845
7846 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007847 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007848 LY_CHECK_GOTO(rc, cleanup);
7849 continue;
7850 }
7851
7852 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007853 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007854 LY_CHECK_GOTO(rc, cleanup);
7855
7856 /* eval */
7857 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007858 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007859 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007860 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007861 LY_CHECK_GOTO(rc, cleanup);
7862 }
7863 }
7864
7865cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007866 lyxp_set_free_content(&orig_set);
7867 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007868 return rc;
7869}
7870
7871/**
7872 * @brief Evaluate UnaryExpr. Logs directly on error.
7873 *
Michal Vaskod3678892020-05-21 10:06:58 +02007874 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007875 *
7876 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007877 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007878 * @param[in] repeat How many times this expression is repeated.
7879 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7880 * @param[in] options XPath options.
7881 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7882 */
7883static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007884eval_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 +02007885{
7886 LY_ERR rc;
7887 uint16_t this_op, i;
7888
7889 assert(repeat);
7890
7891 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007892 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007893 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007894 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 +02007895
7896 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007897 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007898 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007899 }
7900
Michal Vasko004d3152020-06-11 19:59:22 +02007901 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007902 LY_CHECK_RET(rc);
7903
7904 if (set && (repeat % 2)) {
7905 if (options & LYXP_SCNODE_ALL) {
7906 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7907 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007908 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007909 LY_CHECK_RET(rc);
7910 }
7911 }
7912
7913 return LY_SUCCESS;
7914}
7915
7916/**
7917 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7918 *
Michal Vaskod3678892020-05-21 10:06:58 +02007919 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007920 * | MultiplicativeExpr '*' UnaryExpr
7921 * | MultiplicativeExpr 'div' UnaryExpr
7922 * | MultiplicativeExpr 'mod' UnaryExpr
7923 *
7924 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007925 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007926 * @param[in] repeat How many times this expression is repeated.
7927 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7928 * @param[in] options XPath options.
7929 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7930 */
7931static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007932eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
7933 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007934{
7935 LY_ERR rc;
7936 uint16_t this_op;
7937 struct lyxp_set orig_set, set2;
7938 uint16_t i;
7939
7940 assert(repeat);
7941
7942 set_init(&orig_set, set);
7943 set_init(&set2, set);
7944
7945 set_fill_set(&orig_set, set);
7946
Michal Vasko004d3152020-06-11 19:59:22 +02007947 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007948 LY_CHECK_GOTO(rc, cleanup);
7949
7950 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7951 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007952 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007953
Michal Vasko004d3152020-06-11 19:59:22 +02007954 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007955 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007956 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007957 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007958
7959 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007960 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007961 LY_CHECK_GOTO(rc, cleanup);
7962 continue;
7963 }
7964
7965 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007966 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007967 LY_CHECK_GOTO(rc, cleanup);
7968
7969 /* eval */
7970 if (options & LYXP_SCNODE_ALL) {
7971 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007972 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007973 set_scnode_clear_ctx(set);
7974 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007975 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007976 LY_CHECK_GOTO(rc, cleanup);
7977 }
7978 }
7979
7980cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007981 lyxp_set_free_content(&orig_set);
7982 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007983 return rc;
7984}
7985
7986/**
7987 * @brief Evaluate AdditiveExpr. Logs directly on error.
7988 *
Michal Vaskod3678892020-05-21 10:06:58 +02007989 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007990 * | AdditiveExpr '+' MultiplicativeExpr
7991 * | AdditiveExpr '-' MultiplicativeExpr
7992 *
7993 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007994 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007995 * @param[in] repeat How many times this expression is repeated.
7996 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7997 * @param[in] options XPath options.
7998 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7999 */
8000static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008001eval_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 +02008002{
8003 LY_ERR rc;
8004 uint16_t this_op;
8005 struct lyxp_set orig_set, set2;
8006 uint16_t i;
8007
8008 assert(repeat);
8009
8010 set_init(&orig_set, set);
8011 set_init(&set2, set);
8012
8013 set_fill_set(&orig_set, set);
8014
Michal Vasko004d3152020-06-11 19:59:22 +02008015 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008016 LY_CHECK_GOTO(rc, cleanup);
8017
8018 /* ('+' / '-' MultiplicativeExpr)* */
8019 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008020 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008021
Michal Vasko004d3152020-06-11 19:59:22 +02008022 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008023 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008024 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008025 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008026
8027 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008028 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008029 LY_CHECK_GOTO(rc, cleanup);
8030 continue;
8031 }
8032
8033 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008034 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008035 LY_CHECK_GOTO(rc, cleanup);
8036
8037 /* eval */
8038 if (options & LYXP_SCNODE_ALL) {
8039 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008040 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008041 set_scnode_clear_ctx(set);
8042 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008043 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008044 LY_CHECK_GOTO(rc, cleanup);
8045 }
8046 }
8047
8048cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008049 lyxp_set_free_content(&orig_set);
8050 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008051 return rc;
8052}
8053
8054/**
8055 * @brief Evaluate RelationalExpr. Logs directly on error.
8056 *
Michal Vaskod3678892020-05-21 10:06:58 +02008057 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008058 * | RelationalExpr '<' AdditiveExpr
8059 * | RelationalExpr '>' AdditiveExpr
8060 * | RelationalExpr '<=' AdditiveExpr
8061 * | RelationalExpr '>=' AdditiveExpr
8062 *
8063 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008064 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008065 * @param[in] repeat How many times this expression is repeated.
8066 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8067 * @param[in] options XPath options.
8068 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8069 */
8070static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008071eval_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 +02008072{
8073 LY_ERR rc;
8074 uint16_t this_op;
8075 struct lyxp_set orig_set, set2;
8076 uint16_t i;
8077
8078 assert(repeat);
8079
8080 set_init(&orig_set, set);
8081 set_init(&set2, set);
8082
8083 set_fill_set(&orig_set, set);
8084
Michal Vasko004d3152020-06-11 19:59:22 +02008085 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008086 LY_CHECK_GOTO(rc, cleanup);
8087
8088 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8089 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008090 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008091
Michal Vasko004d3152020-06-11 19:59:22 +02008092 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008093 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008094 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008095 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008096
8097 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008098 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008099 LY_CHECK_GOTO(rc, cleanup);
8100 continue;
8101 }
8102
8103 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008104 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008105 LY_CHECK_GOTO(rc, cleanup);
8106
8107 /* eval */
8108 if (options & LYXP_SCNODE_ALL) {
8109 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008110 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008111 set_scnode_clear_ctx(set);
8112 } else {
8113 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8114 LY_CHECK_GOTO(rc, cleanup);
8115 }
8116 }
8117
8118cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008119 lyxp_set_free_content(&orig_set);
8120 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008121 return rc;
8122}
8123
8124/**
8125 * @brief Evaluate EqualityExpr. Logs directly on error.
8126 *
Michal Vaskod3678892020-05-21 10:06:58 +02008127 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008128 * | EqualityExpr '!=' RelationalExpr
8129 *
8130 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008131 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008132 * @param[in] repeat How many times this expression is repeated.
8133 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8134 * @param[in] options XPath options.
8135 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8136 */
8137static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008138eval_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 +02008139{
8140 LY_ERR rc;
8141 uint16_t this_op;
8142 struct lyxp_set orig_set, set2;
8143 uint16_t i;
8144
8145 assert(repeat);
8146
8147 set_init(&orig_set, set);
8148 set_init(&set2, set);
8149
8150 set_fill_set(&orig_set, set);
8151
Michal Vasko004d3152020-06-11 19:59:22 +02008152 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008153 LY_CHECK_GOTO(rc, cleanup);
8154
8155 /* ('=' / '!=' RelationalExpr)* */
8156 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008157 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008158
Michal Vasko004d3152020-06-11 19:59:22 +02008159 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008160 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008161 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008162 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008163
8164 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008165 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008166 LY_CHECK_GOTO(rc, cleanup);
8167 continue;
8168 }
8169
8170 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008171 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008172 LY_CHECK_GOTO(rc, cleanup);
8173
8174 /* eval */
8175 if (options & LYXP_SCNODE_ALL) {
8176 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008177 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8178 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008179 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008180 set_scnode_clear_ctx(set);
8181 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008182 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8183 LY_CHECK_GOTO(rc, cleanup);
8184 }
8185 }
8186
8187cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008188 lyxp_set_free_content(&orig_set);
8189 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008190 return rc;
8191}
8192
8193/**
8194 * @brief Evaluate AndExpr. Logs directly on error.
8195 *
Michal Vaskod3678892020-05-21 10:06:58 +02008196 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008197 *
8198 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008199 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008200 * @param[in] repeat How many times this expression is repeated.
8201 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8202 * @param[in] options XPath options.
8203 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8204 */
8205static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008206eval_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 +02008207{
8208 LY_ERR rc;
8209 struct lyxp_set orig_set, set2;
8210 uint16_t i;
8211
8212 assert(repeat);
8213
8214 set_init(&orig_set, set);
8215 set_init(&set2, set);
8216
8217 set_fill_set(&orig_set, set);
8218
Michal Vasko004d3152020-06-11 19:59:22 +02008219 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008220 LY_CHECK_GOTO(rc, cleanup);
8221
8222 /* cast to boolean, we know that will be the final result */
8223 if (set && (options & LYXP_SCNODE_ALL)) {
8224 set_scnode_clear_ctx(set);
8225 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008226 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008227 }
8228
8229 /* ('and' EqualityExpr)* */
8230 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008231 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8232 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008233 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008234 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008235
8236 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008237 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8238 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008239 LY_CHECK_GOTO(rc, cleanup);
8240 continue;
8241 }
8242
8243 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008244 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008245 LY_CHECK_GOTO(rc, cleanup);
8246
8247 /* eval - just get boolean value actually */
8248 if (set->type == LYXP_SET_SCNODE_SET) {
8249 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008250 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008251 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008252 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008253 set_fill_set(set, &set2);
8254 }
8255 }
8256
8257cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008258 lyxp_set_free_content(&orig_set);
8259 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008260 return rc;
8261}
8262
8263/**
8264 * @brief Evaluate OrExpr. Logs directly on error.
8265 *
Michal Vaskod3678892020-05-21 10:06:58 +02008266 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008267 *
8268 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008269 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008270 * @param[in] repeat How many times this expression is repeated.
8271 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8272 * @param[in] options XPath options.
8273 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8274 */
8275static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008276eval_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 +02008277{
8278 LY_ERR rc;
8279 struct lyxp_set orig_set, set2;
8280 uint16_t i;
8281
8282 assert(repeat);
8283
8284 set_init(&orig_set, set);
8285 set_init(&set2, set);
8286
8287 set_fill_set(&orig_set, set);
8288
Michal Vasko004d3152020-06-11 19:59:22 +02008289 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008290 LY_CHECK_GOTO(rc, cleanup);
8291
8292 /* cast to boolean, we know that will be the final result */
8293 if (set && (options & LYXP_SCNODE_ALL)) {
8294 set_scnode_clear_ctx(set);
8295 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008296 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008297 }
8298
8299 /* ('or' AndExpr)* */
8300 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008301 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8302 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008303 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008304 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008305
8306 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008307 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8308 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008309 LY_CHECK_GOTO(rc, cleanup);
8310 continue;
8311 }
8312
8313 set_fill_set(&set2, &orig_set);
8314 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8315 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008316 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008317 LY_CHECK_GOTO(rc, cleanup);
8318
8319 /* eval - just get boolean value actually */
8320 if (set->type == LYXP_SET_SCNODE_SET) {
8321 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008322 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008323 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008324 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008325 set_fill_set(set, &set2);
8326 }
8327 }
8328
8329cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008330 lyxp_set_free_content(&orig_set);
8331 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008332 return rc;
8333}
8334
8335/**
Michal Vasko004d3152020-06-11 19:59:22 +02008336 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008337 *
8338 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008339 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008340 * @param[in] etype Expression type to evaluate.
8341 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8342 * @param[in] options XPath options.
8343 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8344 */
8345static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008346eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8347 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008348{
8349 uint16_t i, count;
8350 enum lyxp_expr_type next_etype;
8351 LY_ERR rc;
8352
8353 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008354 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008355 next_etype = LYXP_EXPR_NONE;
8356 } else {
8357 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008358 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008359
8360 /* select one-priority lower because etype expression called us */
8361 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008362 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008363 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008364 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008365 } else {
8366 next_etype = LYXP_EXPR_NONE;
8367 }
8368 }
8369
8370 /* decide what expression are we parsing based on the repeat */
8371 switch (next_etype) {
8372 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008373 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008374 break;
8375 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008376 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008377 break;
8378 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008379 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008380 break;
8381 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008382 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008383 break;
8384 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008385 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008386 break;
8387 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008388 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008389 break;
8390 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008391 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008392 break;
8393 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008394 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008395 break;
8396 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008397 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008398 break;
8399 default:
8400 LOGINT_RET(set->ctx);
8401 }
8402
8403 return rc;
8404}
8405
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008406/**
8407 * @brief Get root type.
8408 *
8409 * @param[in] ctx_node Context node.
8410 * @param[in] ctx_scnode Schema context node.
8411 * @param[in] options XPath options.
8412 * @return Root type.
8413 */
8414static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008415lyxp_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 +01008416{
Michal Vasko6b26e742020-07-17 15:02:10 +02008417 const struct lysc_node *op;
8418
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008419 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008420 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008421 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008422
8423 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008424 /* general root that can access everything */
8425 return LYXP_NODE_ROOT;
8426 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8427 /* root context node can access only config data (because we said so, it is unspecified) */
8428 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008429 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008430 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008431 }
8432
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008433 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008434 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008435 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008436
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008437 if (op || !(options & LYXP_SCHEMA)) {
8438 /* general root that can access everything */
8439 return LYXP_NODE_ROOT;
8440 } else if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008441 /* root context node can access only config data (because we said so, it is unspecified) */
8442 return LYXP_NODE_ROOT_CONFIG;
8443 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008444 return LYXP_NODE_ROOT;
8445}
8446
Michal Vasko03ff5a72019-09-11 13:49:33 +02008447LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008448lyxp_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 +02008449 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 +02008450{
Michal Vasko004d3152020-06-11 19:59:22 +02008451 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008452 LY_ERR rc;
8453
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008454 LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL);
8455 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8456 LOGARG(NULL, "Current module must be set if schema format is used.");
8457 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008458 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008459
8460 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008461 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008462 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008463 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8464 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8465
8466 set->ctx = (struct ly_ctx *)(cur_mod ? cur_mod->ctx : (ctx_node ? LYD_CTX(ctx_node) : (tree ? LYD_CTX(tree) : NULL)));
8467 set->cur_node = ctx_node;
8468 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008469 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8470 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008471 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008472 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008473 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008474 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008475
8476 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008477 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008478 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008479 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008480 }
8481
Michal Vasko03ff5a72019-09-11 13:49:33 +02008482 return rc;
8483}
8484
8485#if 0
8486
8487/* full xml printing of set elements, not used currently */
8488
8489void
8490lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8491{
8492 uint32_t i;
8493 char *str_num;
8494 struct lyout out;
8495
8496 memset(&out, 0, sizeof out);
8497
8498 out.type = LYOUT_STREAM;
8499 out.method.f = f;
8500
8501 switch (set->type) {
8502 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008503 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008504 break;
8505 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008506 ly_print_(&out, "Boolean XPath set:\n");
8507 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008508 break;
8509 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008510 ly_print_(&out, "String XPath set:\n");
8511 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008512 break;
8513 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008514 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008515
8516 if (isnan(set->value.num)) {
8517 str_num = strdup("NaN");
8518 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8519 str_num = strdup("0");
8520 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8521 str_num = strdup("Infinity");
8522 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8523 str_num = strdup("-Infinity");
8524 } else if ((long long)set->value.num == set->value.num) {
8525 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8526 str_num = NULL;
8527 }
8528 } else {
8529 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8530 str_num = NULL;
8531 }
8532 }
8533 if (!str_num) {
8534 LOGMEM;
8535 return;
8536 }
Michal Vasko5233e962020-08-14 14:26:20 +02008537 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008538 free(str_num);
8539 break;
8540 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008541 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008542
8543 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008544 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008545 switch (set->node_type[i]) {
8546 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008547 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008548 break;
8549 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008550 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008551 break;
8552 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008553 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008554 break;
8555 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008556 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008557 break;
8558 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008559 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008560 break;
8561 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008562 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008563 break;
8564 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008565 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008566 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008567 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008568 break;
8569 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008570 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 +02008571 break;
8572 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008573 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 +02008574 break;
8575 }
8576 }
8577 break;
8578 }
8579}
8580
8581#endif
8582
8583LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008584lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008585{
8586 long double num;
8587 char *str;
8588 LY_ERR rc;
8589
8590 if (!set || (set->type == target)) {
8591 return LY_SUCCESS;
8592 }
8593
8594 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008595 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008596
8597 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008598 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008599 return LY_EINVAL;
8600 }
8601
8602 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008603 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008604 switch (set->type) {
8605 case LYXP_SET_NUMBER:
8606 if (isnan(set->val.num)) {
8607 set->val.str = strdup("NaN");
8608 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8609 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8610 set->val.str = strdup("0");
8611 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8612 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8613 set->val.str = strdup("Infinity");
8614 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8615 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8616 set->val.str = strdup("-Infinity");
8617 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8618 } else if ((long long)set->val.num == set->val.num) {
8619 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8620 LOGMEM_RET(set->ctx);
8621 }
8622 set->val.str = str;
8623 } else {
8624 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8625 LOGMEM_RET(set->ctx);
8626 }
8627 set->val.str = str;
8628 }
8629 break;
8630 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008631 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008632 set->val.str = strdup("true");
8633 } else {
8634 set->val.str = strdup("false");
8635 }
8636 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8637 break;
8638 case LYXP_SET_NODE_SET:
8639 assert(set->used);
8640
8641 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008642 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008643
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008644 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008645 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008646 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008647 set->val.str = str;
8648 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008649 default:
8650 LOGINT_RET(set->ctx);
8651 }
8652 set->type = LYXP_SET_STRING;
8653 }
8654
8655 /* to NUMBER */
8656 if (target == LYXP_SET_NUMBER) {
8657 switch (set->type) {
8658 case LYXP_SET_STRING:
8659 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008660 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008661 set->val.num = num;
8662 break;
8663 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008664 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008665 set->val.num = 1;
8666 } else {
8667 set->val.num = 0;
8668 }
8669 break;
8670 default:
8671 LOGINT_RET(set->ctx);
8672 }
8673 set->type = LYXP_SET_NUMBER;
8674 }
8675
8676 /* to BOOLEAN */
8677 if (target == LYXP_SET_BOOLEAN) {
8678 switch (set->type) {
8679 case LYXP_SET_NUMBER:
8680 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008681 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008682 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008683 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008684 }
8685 break;
8686 case LYXP_SET_STRING:
8687 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008688 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008689 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008690 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008691 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008692 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008693 }
8694 break;
8695 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008696 if (set->used) {
8697 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008698 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008699 } else {
8700 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008701 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008702 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008703 break;
8704 default:
8705 LOGINT_RET(set->ctx);
8706 }
8707 set->type = LYXP_SET_BOOLEAN;
8708 }
8709
Michal Vasko03ff5a72019-09-11 13:49:33 +02008710 return LY_SUCCESS;
8711}
8712
8713LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008714lyxp_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 +02008715 const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008716{
Michal Vasko004d3152020-06-11 19:59:22 +02008717 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008718
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008719 LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL);
8720 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8721 LOGARG(NULL, "Current module must be set if schema format is used.");
8722 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008723 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008724
8725 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008726 memset(set, 0, sizeof *set);
8727 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008728 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8729 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode ? LYXP_NODE_ELEM : set->root_type, NULL));
Michal Vasko5c4e5892019-11-14 12:31:38 +01008730 set->val.scnodes[0].in_ctx = -2;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008731
8732 set->ctx = cur_mod ? cur_mod->ctx : (ctx_scnode ? ctx_scnode->module->ctx : NULL);
8733 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008734 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008735 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8736 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008737 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008738 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008739 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008740
8741 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008742 return eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008743}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008744
8745API const char *
8746lyxp_get_expr(const struct lyxp_expr *path)
8747{
8748 if (!path) {
8749 return NULL;
8750 }
8751
8752 return path->expr;
8753}