blob: 7eb2e43da675ee378b21e562168c9895817b9c13 [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;
1290
1291 assert(prev && prev_pos && !root->prev->next);
1292
1293 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1294 return 0;
1295 }
1296
1297 if (*prev) {
1298 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001299 pos = *prev_pos;
Radek Krejci1e008d22020-08-17 11:37:37 +02001300 for (top_sibling = *prev; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001301 goto dfs_search;
1302 }
1303
1304 for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) {
Michal Vasko56daf732020-08-10 10:57:18 +02001305 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001306dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001307 if (*prev && !elem) {
1308 /* resume previous DFS */
1309 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1310 LYD_TREE_DFS_continue = 0;
1311 }
1312
Michal Vasko03ff5a72019-09-11 13:49:33 +02001313 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001314 /* skip */
1315 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001316 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001317 if (elem == node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001318 break;
1319 }
Michal Vasko56daf732020-08-10 10:57:18 +02001320 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001321 }
Michal Vasko56daf732020-08-10 10:57:18 +02001322
1323 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001324 }
1325
1326 /* node found */
1327 if (elem) {
1328 break;
1329 }
1330 }
1331
1332 if (!elem) {
1333 if (!(*prev)) {
1334 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001335 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001336 return 0;
1337 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001338 /* start the search again from the beginning */
1339 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001340
Michal Vasko56daf732020-08-10 10:57:18 +02001341 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001342 pos = 1;
1343 goto dfs_search;
1344 }
1345 }
1346
1347 /* remember the last found node for next time */
1348 *prev = node;
1349 *prev_pos = pos;
1350
1351 return pos;
1352}
1353
1354/**
1355 * @brief Assign (fill) missing node positions.
1356 *
1357 * @param[in] set Set to fill positions in.
1358 * @param[in] root Context root node.
1359 * @param[in] root_type Context root type.
1360 * @return LY_ERR
1361 */
1362static LY_ERR
1363set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1364{
1365 const struct lyd_node *prev = NULL, *tmp_node;
1366 uint32_t i, tmp_pos = 0;
1367
1368 for (i = 0; i < set->used; ++i) {
1369 if (!set->val.nodes[i].pos) {
1370 tmp_node = NULL;
1371 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001372 case LYXP_NODE_META:
1373 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001374 if (!tmp_node) {
1375 LOGINT_RET(root->schema->module->ctx);
1376 }
Radek Krejci0f969882020-08-21 16:56:47 +02001377 /* fallthrough */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001378 case LYXP_NODE_ELEM:
1379 case LYXP_NODE_TEXT:
1380 if (!tmp_node) {
1381 tmp_node = set->val.nodes[i].node;
1382 }
1383 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1384 break;
1385 default:
1386 /* all roots have position 0 */
1387 break;
1388 }
1389 }
1390 }
1391
1392 return LY_SUCCESS;
1393}
1394
1395/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001396 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001397 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001398 * @param[in] meta Metadata to use.
1399 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001400 */
1401static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001402get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001403{
1404 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001405 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001406
Michal Vasko9f96a052020-03-10 09:41:45 +01001407 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001408 ++pos;
1409 }
1410
Michal Vasko9f96a052020-03-10 09:41:45 +01001411 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001412 return pos;
1413}
1414
1415/**
1416 * @brief Compare 2 nodes in respect to XPath document order.
1417 *
1418 * @param[in] item1 1st node.
1419 * @param[in] item2 2nd node.
1420 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1421 */
1422static int
1423set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1424{
Michal Vasko9f96a052020-03-10 09:41:45 +01001425 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001426
1427 if (item1->pos < item2->pos) {
1428 return -1;
1429 }
1430
1431 if (item1->pos > item2->pos) {
1432 return 1;
1433 }
1434
1435 /* node positions are equal, the fun case */
1436
1437 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1438 /* special case since text nodes are actually saved as their parents */
1439 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1440 if (item1->type == LYXP_NODE_ELEM) {
1441 assert(item2->type == LYXP_NODE_TEXT);
1442 return -1;
1443 } else {
1444 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1445 return 1;
1446 }
1447 }
1448
Michal Vasko9f96a052020-03-10 09:41:45 +01001449 /* we need meta positions now */
1450 if (item1->type == LYXP_NODE_META) {
1451 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001452 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001453 if (item2->type == LYXP_NODE_META) {
1454 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001455 }
1456
Michal Vasko9f96a052020-03-10 09:41:45 +01001457 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001458 /* check for duplicates */
1459 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001460 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001461 return 0;
1462 }
1463
Michal Vasko9f96a052020-03-10 09:41:45 +01001464 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001465 /* elem is always first, 2nd node is after it */
1466 if (item1->type == LYXP_NODE_ELEM) {
1467 assert(item2->type != LYXP_NODE_ELEM);
1468 return -1;
1469 }
1470
Michal Vasko9f96a052020-03-10 09:41:45 +01001471 /* 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 +02001472 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001473 if (((item1->type == LYXP_NODE_TEXT) &&
1474 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1475 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1476 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1477 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001478 return 1;
1479 }
1480
Michal Vasko9f96a052020-03-10 09:41:45 +01001481 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001482 /* 2nd is after 1st */
1483 return -1;
1484}
1485
1486/**
1487 * @brief Set cast for comparisons.
1488 *
1489 * @param[in] trg Target set to cast source into.
1490 * @param[in] src Source set.
1491 * @param[in] type Target set type.
1492 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001493 * @return LY_ERR
1494 */
1495static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001496set_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 +02001497{
1498 assert(src->type == LYXP_SET_NODE_SET);
1499
1500 set_init(trg, src);
1501
1502 /* insert node into target set */
1503 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1504
1505 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001506 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001507}
1508
Michal Vasko4c7763f2020-07-27 17:40:37 +02001509/**
1510 * @brief Set content canonization for comparisons.
1511 *
1512 * @param[in] trg Target set to put the canononized source into.
1513 * @param[in] src Source set.
1514 * @param[in] xp_node Source XPath node/meta to use for canonization.
1515 * @return LY_ERR
1516 */
1517static LY_ERR
1518set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1519{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001520 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001521 struct lyd_value val;
1522 struct ly_err_item *err = NULL;
1523 char *str, *ptr;
Radek Krejci857189e2020-09-01 13:26:36 +02001524 ly_bool dynamic;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001525 LY_ERR rc;
1526
1527 /* is there anything to canonize even? */
1528 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1529 /* do we have a type to use for canonization? */
1530 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1531 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1532 } else if (xp_node->type == LYXP_NODE_META) {
1533 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1534 }
1535 }
1536 if (!type) {
1537 goto fill;
1538 }
1539
1540 if (src->type == LYXP_SET_NUMBER) {
1541 /* canonize number */
1542 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1543 LOGMEM(src->ctx);
1544 return LY_EMEM;
1545 }
1546 } else {
1547 /* canonize string */
1548 str = strdup(src->val.str);
1549 }
1550
1551 /* ignore errors, the value may not satisfy schema constraints */
Michal Vasko25d6ad02020-10-22 12:20:22 +02001552 rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_STORE_DYNAMIC, LY_PREF_JSON, NULL, LYD_HINT_DATA,
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001553 xp_node->node->schema, &val, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001554 ly_err_free(err);
1555 if (rc) {
1556 /* invalid value */
1557 free(str);
1558 goto fill;
1559 }
1560
1561 /* storing successful, now print the canonical value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001562 str = (char *)type->plugin->print(&val, LY_PREF_JSON, NULL, &dynamic);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001563
1564 /* use the canonized value */
1565 set_init(trg, src);
1566 trg->type = src->type;
1567 if (src->type == LYXP_SET_NUMBER) {
1568 trg->val.num = strtold(str, &ptr);
1569 if (dynamic) {
1570 free(str);
1571 }
1572 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1573 } else {
1574 trg->val.str = (dynamic ? str : strdup(str));
1575 }
1576 type->plugin->free(src->ctx, &val);
1577 return LY_SUCCESS;
1578
1579fill:
1580 /* no canonization needed/possible */
1581 set_fill_set(trg, src);
1582 return LY_SUCCESS;
1583}
1584
Michal Vasko03ff5a72019-09-11 13:49:33 +02001585#ifndef NDEBUG
1586
1587/**
1588 * @brief Bubble sort @p set into XPath document order.
1589 * Context position aware. Unused in the 'Release' build target.
1590 *
1591 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001592 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1593 */
1594static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001595set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001596{
1597 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001598 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001599 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001600 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001601 struct lyxp_set_node item;
1602 struct lyxp_set_hash_node hnode;
1603 uint64_t hash;
1604
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001605 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001606 return 0;
1607 }
1608
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001609 /* find first top-level node to be used as anchor for positions */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001610 for (root = set->cur_node; root->parent; root = (const struct lyd_node *)root->parent) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001611 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001612
1613 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001614 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001615 return -1;
1616 }
1617
1618 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1619 print_set_debug(set);
1620
1621 for (i = 0; i < set->used; ++i) {
1622 inverted = 0;
1623 change = 0;
1624
1625 for (j = 1; j < set->used - i; ++j) {
1626 /* compare node positions */
1627 if (inverted) {
1628 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1629 } else {
1630 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1631 }
1632
1633 /* swap if needed */
1634 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1635 change = 1;
1636
1637 item = set->val.nodes[j - 1];
1638 set->val.nodes[j - 1] = set->val.nodes[j];
1639 set->val.nodes[j] = item;
1640 } else {
1641 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1642 inverted = !inverted;
1643 }
1644 }
1645
1646 ++ret;
1647
1648 if (!change) {
1649 break;
1650 }
1651 }
1652
1653 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1654 print_set_debug(set);
1655
1656 /* check node hashes */
1657 if (set->used >= LYD_HT_MIN_ITEMS) {
1658 assert(set->ht);
1659 for (i = 0; i < set->used; ++i) {
1660 hnode.node = set->val.nodes[i].node;
1661 hnode.type = set->val.nodes[i].type;
1662
1663 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1664 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1665 hash = dict_hash_multi(hash, NULL, 0);
1666
1667 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1668 }
1669 }
1670
1671 return ret - 1;
1672}
1673
1674/**
1675 * @brief Remove duplicate entries in a sorted node set.
1676 *
1677 * @param[in] set Sorted set to check.
1678 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1679 */
1680static LY_ERR
1681set_sorted_dup_node_clean(struct lyxp_set *set)
1682{
1683 uint32_t i = 0;
1684 LY_ERR ret = LY_SUCCESS;
1685
1686 if (set->used > 1) {
1687 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001688 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1689 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001690 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001691 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001692 }
Michal Vasko57eab132019-09-24 11:46:26 +02001693 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001694 }
1695 }
1696
Michal Vasko2caefc12019-11-14 16:07:56 +01001697 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001698 return ret;
1699}
1700
1701#endif
1702
1703/**
1704 * @brief Merge 2 sorted sets into one.
1705 *
1706 * @param[in,out] trg Set to merge into. Duplicates are removed.
1707 * @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 +02001708 * @return LY_ERR
1709 */
1710static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001711set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001712{
1713 uint32_t i, j, k, count, dup_count;
1714 int cmp;
1715 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001716
Michal Vaskod3678892020-05-21 10:06:58 +02001717 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001718 return LY_EINVAL;
1719 }
1720
Michal Vaskod3678892020-05-21 10:06:58 +02001721 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001722 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001723 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001724 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001725 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001726 return LY_SUCCESS;
1727 }
1728
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001729 /* find first top-level node to be used as anchor for positions */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001730 for (root = trg->cur_node; root->parent; root = (const struct lyd_node *)root->parent) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001731 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001732
1733 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001734 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001735 return LY_EINT;
1736 }
1737
1738#ifndef NDEBUG
1739 LOGDBG(LY_LDGXPATH, "MERGE target");
1740 print_set_debug(trg);
1741 LOGDBG(LY_LDGXPATH, "MERGE source");
1742 print_set_debug(src);
1743#endif
1744
1745 /* make memory for the merge (duplicates are not detected yet, so space
1746 * will likely be wasted on them, too bad) */
1747 if (trg->size - trg->used < src->used) {
1748 trg->size = trg->used + src->used;
1749
1750 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1751 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1752 }
1753
1754 i = 0;
1755 j = 0;
1756 count = 0;
1757 dup_count = 0;
1758 do {
1759 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1760 if (!cmp) {
1761 if (!count) {
1762 /* duplicate, just skip it */
1763 ++i;
1764 ++j;
1765 } else {
1766 /* we are copying something already, so let's copy the duplicate too,
1767 * we are hoping that afterwards there are some more nodes to
1768 * copy and this way we can copy them all together */
1769 ++count;
1770 ++dup_count;
1771 ++i;
1772 ++j;
1773 }
1774 } else if (cmp < 0) {
1775 /* inserting src node into trg, just remember it for now */
1776 ++count;
1777 ++i;
1778
1779 /* insert the hash now */
1780 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1781 } else if (count) {
1782copy_nodes:
1783 /* time to actually copy the nodes, we have found the largest block of nodes */
1784 memmove(&trg->val.nodes[j + (count - dup_count)],
1785 &trg->val.nodes[j],
1786 (trg->used - j) * sizeof *trg->val.nodes);
1787 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1788
1789 trg->used += count - dup_count;
1790 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1791 j += count - dup_count;
1792
1793 count = 0;
1794 dup_count = 0;
1795 } else {
1796 ++j;
1797 }
1798 } while ((i < src->used) && (j < trg->used));
1799
1800 if ((i < src->used) || count) {
1801 /* insert all the hashes first */
1802 for (k = i; k < src->used; ++k) {
1803 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1804 }
1805
1806 /* loop ended, but we need to copy something at trg end */
1807 count += src->used - i;
1808 i = src->used;
1809 goto copy_nodes;
1810 }
1811
1812 /* we are inserting hashes before the actual node insert, which causes
1813 * situations when there were initially not enough items for a hash table,
1814 * but even after some were inserted, hash table was not created (during
1815 * insertion the number of items is not updated yet) */
1816 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1817 set_insert_node_hash(trg, NULL, 0);
1818 }
1819
1820#ifndef NDEBUG
1821 LOGDBG(LY_LDGXPATH, "MERGE result");
1822 print_set_debug(trg);
1823#endif
1824
Michal Vaskod3678892020-05-21 10:06:58 +02001825 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001826 return LY_SUCCESS;
1827}
1828
1829/*
1830 * (re)parse functions
1831 *
1832 * Parse functions parse the expression into
1833 * tokens (syntactic analysis).
1834 *
1835 * Reparse functions perform semantic analysis
1836 * (do not save the result, just a check) of
1837 * the expression and fill repeat indices.
1838 */
1839
Michal Vasko14676352020-05-29 11:35:55 +02001840LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001841lyxp_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 +02001842{
Michal Vasko004d3152020-06-11 19:59:22 +02001843 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001844 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001845 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1846 }
Michal Vasko14676352020-05-29 11:35:55 +02001847 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001848 }
1849
Michal Vasko004d3152020-06-11 19:59:22 +02001850 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001851 if (ctx) {
Michal Vasko0b468e62020-10-19 09:33:04 +02001852 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
1853 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001854 }
Michal Vasko14676352020-05-29 11:35:55 +02001855 return LY_ENOT;
1856 }
1857
1858 return LY_SUCCESS;
1859}
1860
Michal Vasko004d3152020-06-11 19:59:22 +02001861LY_ERR
1862lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1863{
1864 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1865
1866 /* skip the token */
1867 ++(*tok_idx);
1868
1869 return LY_SUCCESS;
1870}
1871
Michal Vasko14676352020-05-29 11:35:55 +02001872/* just like lyxp_check_token() but tests for 2 tokens */
1873static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001874exp_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 +02001875 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001876{
Michal Vasko004d3152020-06-11 19:59:22 +02001877 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001878 if (ctx) {
1879 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1880 }
1881 return LY_EINCOMPLETE;
1882 }
1883
Michal Vasko004d3152020-06-11 19:59:22 +02001884 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001885 if (ctx) {
Michal Vasko0b468e62020-10-19 09:33:04 +02001886 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
1887 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001888 }
1889 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001890 }
1891
1892 return LY_SUCCESS;
1893}
1894
1895/**
1896 * @brief Stack operation push on the repeat array.
1897 *
1898 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001899 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001900 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1901 */
1902static void
Michal Vasko004d3152020-06-11 19:59:22 +02001903exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001904{
1905 uint16_t i;
1906
Michal Vasko004d3152020-06-11 19:59:22 +02001907 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001908 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001909 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1910 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1911 exp->repeat[tok_idx][i] = repeat_op_idx;
1912 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001913 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001914 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1915 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1916 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001917 }
1918}
1919
1920/**
1921 * @brief Reparse Predicate. Logs directly on error.
1922 *
1923 * [7] Predicate ::= '[' Expr ']'
1924 *
1925 * @param[in] ctx Context for logging.
1926 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001927 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001928 * @return LY_ERR
1929 */
1930static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001931reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001932{
1933 LY_ERR rc;
1934
Michal Vasko004d3152020-06-11 19:59:22 +02001935 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001936 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001937 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001938
Michal Vasko004d3152020-06-11 19:59:22 +02001939 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001940 LY_CHECK_RET(rc);
1941
Michal Vasko004d3152020-06-11 19:59:22 +02001942 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001943 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001944 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001945
1946 return LY_SUCCESS;
1947}
1948
1949/**
1950 * @brief Reparse RelativeLocationPath. Logs directly on error.
1951 *
1952 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1953 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1954 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1955 *
1956 * @param[in] ctx Context for logging.
1957 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001958 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001959 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1960 */
1961static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001962reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001963{
1964 LY_ERR rc;
1965
Michal Vasko004d3152020-06-11 19:59:22 +02001966 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001967 LY_CHECK_RET(rc);
1968
1969 goto step;
1970 do {
1971 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001972 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001973
Michal Vasko004d3152020-06-11 19:59:22 +02001974 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001975 LY_CHECK_RET(rc);
1976step:
1977 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001978 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001979 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001980 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001981 break;
1982
1983 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001984 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001985 break;
1986
1987 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02001988 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001989
Michal Vasko004d3152020-06-11 19:59:22 +02001990 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001991 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001992 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001993 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko69730152020-10-09 16:30:07 +02001994 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001995 return LY_EVALID;
1996 }
Radek Krejci0f969882020-08-21 16:56:47 +02001997 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001998 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02001999 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002000 goto reparse_predicate;
2001 break;
2002
2003 case LYXP_TOKEN_NODETYPE:
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_PAR1);
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
2011 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002012 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002013 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002014 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002015
2016reparse_predicate:
2017 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002018 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2019 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002020 LY_CHECK_RET(rc);
2021 }
2022 break;
2023 default:
2024 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko69730152020-10-09 16:30:07 +02002025 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002026 return LY_EVALID;
2027 }
Michal Vasko004d3152020-06-11 19:59:22 +02002028 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002029
2030 return LY_SUCCESS;
2031}
2032
2033/**
2034 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2035 *
2036 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2037 *
2038 * @param[in] ctx Context for logging.
2039 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002040 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002041 * @return LY_ERR
2042 */
2043static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002044reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002045{
2046 LY_ERR rc;
2047
Michal Vasko004d3152020-06-11 19:59:22 +02002048 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 +02002049
2050 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002051 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002052 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002053 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002054
Michal Vasko004d3152020-06-11 19:59:22 +02002055 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002056 return LY_SUCCESS;
2057 }
Michal Vasko004d3152020-06-11 19:59:22 +02002058 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002059 case LYXP_TOKEN_DOT:
2060 case LYXP_TOKEN_DDOT:
2061 case LYXP_TOKEN_AT:
2062 case LYXP_TOKEN_NAMETEST:
2063 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002064 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002065 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002066 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002067 default:
2068 break;
2069 }
2070
Michal Vasko03ff5a72019-09-11 13:49:33 +02002071 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002072 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002073 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002074
Michal Vasko004d3152020-06-11 19:59:22 +02002075 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002076 LY_CHECK_RET(rc);
2077 }
2078
2079 return LY_SUCCESS;
2080}
2081
2082/**
2083 * @brief Reparse FunctionCall. Logs directly on error.
2084 *
2085 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2086 *
2087 * @param[in] ctx Context for logging.
2088 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002089 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002090 * @return LY_ERR
2091 */
2092static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002093reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002094{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002095 int8_t min_arg_count = -1;
2096 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002097 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002098 LY_ERR rc;
2099
Michal Vasko004d3152020-06-11 19:59:22 +02002100 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002101 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002102 func_tok_idx = *tok_idx;
2103 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002104 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002105 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002106 min_arg_count = 1;
2107 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002108 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002109 min_arg_count = 1;
2110 max_arg_count = 1;
2111 }
2112 break;
2113 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002114 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002115 min_arg_count = 1;
2116 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002117 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002118 min_arg_count = 0;
2119 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002120 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002121 min_arg_count = 0;
2122 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002123 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002124 min_arg_count = 0;
2125 max_arg_count = 0;
2126 }
2127 break;
2128 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002129 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002130 min_arg_count = 1;
2131 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002132 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002133 min_arg_count = 0;
2134 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002135 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002136 min_arg_count = 1;
2137 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002138 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002139 min_arg_count = 1;
2140 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002141 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002142 min_arg_count = 1;
2143 max_arg_count = 1;
2144 }
2145 break;
2146 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002147 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002148 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002149 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002150 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002151 min_arg_count = 0;
2152 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002153 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002154 min_arg_count = 0;
2155 max_arg_count = 1;
2156 }
2157 break;
2158 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002159 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002160 min_arg_count = 1;
2161 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002162 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002163 min_arg_count = 1;
2164 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002165 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002166 min_arg_count = 0;
2167 max_arg_count = 0;
2168 }
2169 break;
2170 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002171 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002172 min_arg_count = 2;
2173 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002174 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002175 min_arg_count = 0;
2176 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002177 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002178 min_arg_count = 2;
2179 max_arg_count = 2;
2180 }
2181 break;
2182 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002183 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002184 min_arg_count = 2;
2185 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002186 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002187 min_arg_count = 3;
2188 max_arg_count = 3;
2189 }
2190 break;
2191 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002192 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002193 min_arg_count = 0;
2194 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002195 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002196 min_arg_count = 1;
2197 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002198 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002199 min_arg_count = 2;
2200 max_arg_count = 2;
2201 }
2202 break;
2203 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002204 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002205 min_arg_count = 2;
2206 max_arg_count = 2;
2207 }
2208 break;
2209 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002210 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002211 min_arg_count = 2;
2212 max_arg_count = 2;
2213 }
2214 break;
2215 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002216 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002217 min_arg_count = 0;
2218 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002219 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002220 min_arg_count = 0;
2221 max_arg_count = 1;
2222 }
2223 break;
2224 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002225 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002226 min_arg_count = 0;
2227 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002228 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002229 min_arg_count = 2;
2230 max_arg_count = 2;
2231 }
2232 break;
2233 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002234 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002235 min_arg_count = 2;
2236 max_arg_count = 2;
2237 }
2238 break;
2239 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002240 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002241 min_arg_count = 2;
2242 max_arg_count = 2;
2243 }
2244 break;
2245 }
2246 if (min_arg_count == -1) {
Michal Vasko004d3152020-06-11 19:59:22 +02002247 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 +02002248 return LY_EINVAL;
2249 }
Michal Vasko004d3152020-06-11 19:59:22 +02002250 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002251
2252 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002253 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002254 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002255 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002256
2257 /* ( Expr ( ',' Expr )* )? */
2258 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002259 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002260 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002261 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002262 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002263 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002264 LY_CHECK_RET(rc);
2265 }
Michal Vasko004d3152020-06-11 19:59:22 +02002266 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2267 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002268
2269 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002270 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002271 LY_CHECK_RET(rc);
2272 }
2273
2274 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002275 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002276 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002277 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002278
Radek Krejci857189e2020-09-01 13:26:36 +02002279 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Michal Vasko004d3152020-06-11 19:59:22 +02002280 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 +02002281 &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002282 return LY_EVALID;
2283 }
2284
2285 return LY_SUCCESS;
2286}
2287
2288/**
2289 * @brief Reparse PathExpr. Logs directly on error.
2290 *
2291 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2292 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2293 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2294 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2295 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2296 *
2297 * @param[in] ctx Context for logging.
2298 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002299 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002300 * @return LY_ERR
2301 */
2302static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002303reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002304{
2305 LY_ERR rc;
2306
Michal Vasko004d3152020-06-11 19:59:22 +02002307 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002308 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002309 }
2310
Michal Vasko004d3152020-06-11 19:59:22 +02002311 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002312 case LYXP_TOKEN_PAR1:
2313 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002314 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002315
Michal Vasko004d3152020-06-11 19:59:22 +02002316 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002317 LY_CHECK_RET(rc);
2318
Michal Vasko004d3152020-06-11 19:59:22 +02002319 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002320 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002321 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002322 goto predicate;
2323 break;
2324 case LYXP_TOKEN_DOT:
2325 case LYXP_TOKEN_DDOT:
2326 case LYXP_TOKEN_AT:
2327 case LYXP_TOKEN_NAMETEST:
2328 case LYXP_TOKEN_NODETYPE:
2329 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002330 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002331 LY_CHECK_RET(rc);
2332 break;
2333 case LYXP_TOKEN_FUNCNAME:
2334 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002335 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002336 LY_CHECK_RET(rc);
2337 goto predicate;
2338 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002339 case LYXP_TOKEN_OPER_PATH:
2340 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002341 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002342 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002343 LY_CHECK_RET(rc);
2344 break;
2345 case LYXP_TOKEN_LITERAL:
2346 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002347 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002348 goto predicate;
2349 break;
2350 case LYXP_TOKEN_NUMBER:
2351 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002352 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002353 goto predicate;
2354 break;
2355 default:
2356 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko69730152020-10-09 16:30:07 +02002357 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002358 return LY_EVALID;
2359 }
2360
2361 return LY_SUCCESS;
2362
2363predicate:
2364 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002365 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2366 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002367 LY_CHECK_RET(rc);
2368 }
2369
2370 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002371 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002372
2373 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002374 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002375
Michal Vasko004d3152020-06-11 19:59:22 +02002376 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002377 LY_CHECK_RET(rc);
2378 }
2379
2380 return LY_SUCCESS;
2381}
2382
2383/**
2384 * @brief Reparse UnaryExpr. Logs directly on error.
2385 *
2386 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2387 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2388 *
2389 * @param[in] ctx Context for logging.
2390 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002391 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002392 * @return LY_ERR
2393 */
2394static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002395reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002396{
2397 uint16_t prev_exp;
2398 LY_ERR rc;
2399
2400 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002401 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002402 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2403 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002404 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002405 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002406 }
2407
2408 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002409 prev_exp = *tok_idx;
2410 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002411 LY_CHECK_RET(rc);
2412
2413 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002414 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002415 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002416 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002417
Michal Vasko004d3152020-06-11 19:59:22 +02002418 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002419 LY_CHECK_RET(rc);
2420 }
2421
2422 return LY_SUCCESS;
2423}
2424
2425/**
2426 * @brief Reparse AdditiveExpr. Logs directly on error.
2427 *
2428 * [15] AdditiveExpr ::= MultiplicativeExpr
2429 * | AdditiveExpr '+' MultiplicativeExpr
2430 * | AdditiveExpr '-' MultiplicativeExpr
2431 * [16] MultiplicativeExpr ::= UnaryExpr
2432 * | MultiplicativeExpr '*' UnaryExpr
2433 * | MultiplicativeExpr 'div' UnaryExpr
2434 * | MultiplicativeExpr 'mod' UnaryExpr
2435 *
2436 * @param[in] ctx Context for logging.
2437 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002438 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002439 * @return LY_ERR
2440 */
2441static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002442reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002443{
2444 uint16_t prev_add_exp, prev_mul_exp;
2445 LY_ERR rc;
2446
Michal Vasko004d3152020-06-11 19:59:22 +02002447 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002448 goto reparse_multiplicative_expr;
2449
2450 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002451 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2452 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002453 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002454 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002455
2456reparse_multiplicative_expr:
2457 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002458 prev_mul_exp = *tok_idx;
2459 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002460 LY_CHECK_RET(rc);
2461
2462 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002463 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2464 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002465 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002466 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002467
Michal Vasko004d3152020-06-11 19:59:22 +02002468 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002469 LY_CHECK_RET(rc);
2470 }
2471 }
2472
2473 return LY_SUCCESS;
2474}
2475
2476/**
2477 * @brief Reparse EqualityExpr. Logs directly on error.
2478 *
2479 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2480 * | EqualityExpr '!=' RelationalExpr
2481 * [14] RelationalExpr ::= AdditiveExpr
2482 * | RelationalExpr '<' AdditiveExpr
2483 * | RelationalExpr '>' AdditiveExpr
2484 * | RelationalExpr '<=' AdditiveExpr
2485 * | RelationalExpr '>=' AdditiveExpr
2486 *
2487 * @param[in] ctx Context for logging.
2488 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002489 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002490 * @return LY_ERR
2491 */
2492static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002493reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002494{
2495 uint16_t prev_eq_exp, prev_rel_exp;
2496 LY_ERR rc;
2497
Michal Vasko004d3152020-06-11 19:59:22 +02002498 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002499 goto reparse_additive_expr;
2500
2501 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002502 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002503 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002504 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002505
2506reparse_additive_expr:
2507 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002508 prev_rel_exp = *tok_idx;
2509 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002510 LY_CHECK_RET(rc);
2511
2512 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002513 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002514 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002515 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002516
Michal Vasko004d3152020-06-11 19:59:22 +02002517 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002518 LY_CHECK_RET(rc);
2519 }
2520 }
2521
2522 return LY_SUCCESS;
2523}
2524
2525/**
2526 * @brief Reparse OrExpr. Logs directly on error.
2527 *
2528 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2529 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2530 *
2531 * @param[in] ctx Context for logging.
2532 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002533 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002534 * @return LY_ERR
2535 */
2536static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002537reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002538{
2539 uint16_t prev_or_exp, prev_and_exp;
2540 LY_ERR rc;
2541
Michal Vasko004d3152020-06-11 19:59:22 +02002542 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002543 goto reparse_equality_expr;
2544
2545 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002546 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002547 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002548 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002549
2550reparse_equality_expr:
2551 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002552 prev_and_exp = *tok_idx;
2553 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002554 LY_CHECK_RET(rc);
2555
2556 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002557 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002558 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002559 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002560
Michal Vasko004d3152020-06-11 19:59:22 +02002561 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002562 LY_CHECK_RET(rc);
2563 }
2564 }
2565
2566 return LY_SUCCESS;
2567}
Radek Krejcib1646a92018-11-02 16:08:26 +01002568
2569/**
2570 * @brief Parse NCName.
2571 *
2572 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002573 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002574 */
Radek Krejcid4270262019-01-07 15:07:25 +01002575static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002576parse_ncname(const char *ncname)
2577{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002578 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002579 size_t size;
2580 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002581
2582 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2583 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2584 return len;
2585 }
2586
2587 do {
2588 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002589 if (!*ncname) {
2590 break;
2591 }
Radek Krejcid4270262019-01-07 15:07:25 +01002592 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002593 } while (is_xmlqnamechar(uc) && (uc != ':'));
2594
2595 return len;
2596}
2597
2598/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002599 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002600 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002601 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002602 * @param[in] exp Expression to use.
2603 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002604 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002605 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002606 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002607 */
2608static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002609exp_add_token(const struct ly_ctx *ctx, struct lyxp_expr *exp, enum lyxp_token token, uint16_t tok_pos, uint16_t tok_len)
Radek Krejcib1646a92018-11-02 16:08:26 +01002610{
2611 uint32_t prev;
2612
2613 if (exp->used == exp->size) {
2614 prev = exp->size;
2615 exp->size += LYXP_EXPR_SIZE_STEP;
2616 if (prev > exp->size) {
2617 LOGINT(ctx);
2618 return LY_EINT;
2619 }
2620
2621 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2622 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2623 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2624 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2625 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2626 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2627 }
2628
2629 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002630 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002631 exp->tok_len[exp->used] = tok_len;
2632 ++exp->used;
2633 return LY_SUCCESS;
2634}
2635
2636void
Michal Vasko14676352020-05-29 11:35:55 +02002637lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002638{
2639 uint16_t i;
2640
2641 if (!expr) {
2642 return;
2643 }
2644
2645 lydict_remove(ctx, expr->expr);
2646 free(expr->tokens);
2647 free(expr->tok_pos);
2648 free(expr->tok_len);
2649 if (expr->repeat) {
2650 for (i = 0; i < expr->used; ++i) {
2651 free(expr->repeat[i]);
2652 }
2653 }
2654 free(expr->repeat);
2655 free(expr);
2656}
2657
Radek Krejcif03a9e22020-09-18 20:09:31 +02002658LY_ERR
2659lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr_str, size_t expr_len, ly_bool reparse, struct lyxp_expr **expr_p)
Radek Krejcib1646a92018-11-02 16:08:26 +01002660{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002661 LY_ERR ret = LY_SUCCESS;
2662 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002663 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002664 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002665 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002666 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002667
Radek Krejcif03a9e22020-09-18 20:09:31 +02002668 assert(expr_p);
2669
2670 if (!expr_str[0]) {
Michal Vasko004d3152020-06-11 19:59:22 +02002671 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002672 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002673 }
2674
2675 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002676 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002677 }
2678 if (expr_len > UINT16_MAX) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002679 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
2680 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002681 }
2682
2683 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002684 expr = calloc(1, sizeof *expr);
2685 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2686 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2687 expr->used = 0;
2688 expr->size = LYXP_EXPR_SIZE_START;
2689 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2690 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002691
Radek Krejcif03a9e22020-09-18 20:09:31 +02002692 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2693 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002694
Radek Krejcif03a9e22020-09-18 20:09:31 +02002695 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2696 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002697
Michal Vasko004d3152020-06-11 19:59:22 +02002698 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002699 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002700
Radek Krejcif03a9e22020-09-18 20:09:31 +02002701 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002702 ++parsed;
2703 }
2704
2705 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002706 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002707
2708 /* '(' */
2709 tok_len = 1;
2710 tok_type = LYXP_TOKEN_PAR1;
2711
Radek Krejcif03a9e22020-09-18 20:09:31 +02002712 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002713 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002714 if (((expr->tok_len[expr->used - 1] == 4) &&
2715 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2716 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2717 ((expr->tok_len[expr->used - 1] == 7) &&
2718 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002719 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002720 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002721 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002722 }
2723 prev_function_check = 0;
2724 }
2725
Radek Krejcif03a9e22020-09-18 20:09:31 +02002726 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002727
2728 /* ')' */
2729 tok_len = 1;
2730 tok_type = LYXP_TOKEN_PAR2;
2731
Radek Krejcif03a9e22020-09-18 20:09:31 +02002732 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002733
2734 /* '[' */
2735 tok_len = 1;
2736 tok_type = LYXP_TOKEN_BRACK1;
2737
Radek Krejcif03a9e22020-09-18 20:09:31 +02002738 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002739
2740 /* ']' */
2741 tok_len = 1;
2742 tok_type = LYXP_TOKEN_BRACK2;
2743
Radek Krejcif03a9e22020-09-18 20:09:31 +02002744 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002745
2746 /* '..' */
2747 tok_len = 2;
2748 tok_type = LYXP_TOKEN_DDOT;
2749
Radek Krejcif03a9e22020-09-18 20:09:31 +02002750 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002751
2752 /* '.' */
2753 tok_len = 1;
2754 tok_type = LYXP_TOKEN_DOT;
2755
Radek Krejcif03a9e22020-09-18 20:09:31 +02002756 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002757
2758 /* '@' */
2759 tok_len = 1;
2760 tok_type = LYXP_TOKEN_AT;
2761
Radek Krejcif03a9e22020-09-18 20:09:31 +02002762 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002763
2764 /* ',' */
2765 tok_len = 1;
2766 tok_type = LYXP_TOKEN_COMMA;
2767
Radek Krejcif03a9e22020-09-18 20:09:31 +02002768 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002769
2770 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002771 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2772 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Michal Vasko69730152020-10-09 16:30:07 +02002773 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
2774 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002775 ++tok_len;
2776 tok_type = LYXP_TOKEN_LITERAL;
2777
Radek Krejcif03a9e22020-09-18 20:09:31 +02002778 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002779
2780 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002781 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2782 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Michal Vasko69730152020-10-09 16:30:07 +02002783 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
2784 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002785 ++tok_len;
2786 tok_type = LYXP_TOKEN_LITERAL;
2787
Radek Krejcif03a9e22020-09-18 20:09:31 +02002788 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002789
2790 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002791 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2792 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002793 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002794 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002795 }
2796 tok_type = LYXP_TOKEN_NUMBER;
2797
Radek Krejcif03a9e22020-09-18 20:09:31 +02002798 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002799
2800 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002801 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002802 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002803 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002804 } else {
2805 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002806 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002807 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002808
Radek Krejcif03a9e22020-09-18 20:09:31 +02002809 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002810
Michal Vasko3e48bf32020-06-01 08:39:07 +02002811 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002812 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002813 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2814
Radek Krejcif03a9e22020-09-18 20:09:31 +02002815 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002816
2817 /* Operator '<=', '>=' */
2818 tok_len = 2;
2819 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002820
Radek Krejcif03a9e22020-09-18 20:09:31 +02002821 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002822
2823 /* Operator '|' */
2824 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002825 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002826
Radek Krejcif03a9e22020-09-18 20:09:31 +02002827 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002828
2829 /* Operator '+', '-' */
2830 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002831 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002832
Radek Krejcif03a9e22020-09-18 20:09:31 +02002833 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002834
Michal Vasko3e48bf32020-06-01 08:39:07 +02002835 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002836 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002837 tok_type = LYXP_TOKEN_OPER_EQUAL;
2838
Radek Krejcif03a9e22020-09-18 20:09:31 +02002839 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002840
2841 /* Operator '<', '>' */
2842 tok_len = 1;
2843 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002844
Michal Vasko69730152020-10-09 16:30:07 +02002845 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2846 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2847 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2848 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2849 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2850 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2851 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2852 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2853 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2854 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2855 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2856 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002857
2858 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002859 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002860 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002861 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002862
Radek Krejcif03a9e22020-09-18 20:09:31 +02002863 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002864 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002865 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002866
Radek Krejcif03a9e22020-09-18 20:09:31 +02002867 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002868 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002869 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002870
Radek Krejcif03a9e22020-09-18 20:09:31 +02002871 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002872 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002873 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002874
2875 } else if (prev_function_check) {
Michal Vasko53078572019-05-24 08:50:15 +02002876 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 +02002877 expr_str[parsed], expr_str[parsed], expr->tok_len[expr->used - 1], &expr->expr[expr->tok_pos[expr->used - 1]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002878 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002879 goto error;
2880 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002881 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr_str);
2882 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002883 goto error;
2884 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002885 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002886
2887 /* NameTest '*' */
2888 tok_len = 1;
2889 tok_type = LYXP_TOKEN_NAMETEST;
2890
2891 } else {
2892
2893 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002894 long int ncname_len = parse_ncname(&expr_str[parsed]);
2895 LY_CHECK_ERR_GOTO(ncname_len < 0,
Michal Vasko69730152020-10-09 16:30:07 +02002896 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
2897 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002898 tok_len = ncname_len;
2899
Radek Krejcif03a9e22020-09-18 20:09:31 +02002900 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002901 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002902 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002903 ++tok_len;
2904 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002905 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
2906 LY_CHECK_ERR_GOTO(ncname_len < 0,
Michal Vasko69730152020-10-09 16:30:07 +02002907 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
2908 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002909 tok_len += ncname_len;
2910 }
2911 /* remove old flag to prevent ambiguities */
2912 prev_function_check = 0;
2913 tok_type = LYXP_TOKEN_NAMETEST;
2914 } else {
2915 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2916 prev_function_check = 1;
2917 tok_type = LYXP_TOKEN_NAMETEST;
2918 }
2919 }
2920
2921 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002922 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002923 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002924 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002925 ++parsed;
2926 }
2927
Radek Krejcif03a9e22020-09-18 20:09:31 +02002928 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002929
Michal Vasko004d3152020-06-11 19:59:22 +02002930 if (reparse) {
2931 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002932 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2933 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002934
Michal Vasko004d3152020-06-11 19:59:22 +02002935 /* fill repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002936 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error);
2937 if (expr->used > tok_idx) {
Michal Vasko004d3152020-06-11 19:59:22 +02002938 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 +02002939 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002940 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002941 goto error;
2942 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002943 }
2944
Radek Krejcif03a9e22020-09-18 20:09:31 +02002945 print_expr_struct_debug(expr);
2946 *expr_p = expr;
2947 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002948
2949error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002950 lyxp_expr_free(ctx, expr);
2951 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002952}
2953
Michal Vasko1734be92020-09-22 08:55:10 +02002954LY_ERR
2955lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02002956{
Michal Vasko1734be92020-09-22 08:55:10 +02002957 LY_ERR ret = LY_SUCCESS;
2958 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02002959 uint32_t i, j;
2960
Michal Vasko7f45cf22020-10-01 12:49:44 +02002961 if (!exp) {
2962 goto cleanup;
2963 }
2964
Michal Vasko004d3152020-06-11 19:59:22 +02002965 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02002966 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002967
2968 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
Michal Vasko1734be92020-09-22 08:55:10 +02002969 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002970 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2971
2972 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
Michal Vasko1734be92020-09-22 08:55:10 +02002973 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002974 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2975
2976 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
Michal Vasko1734be92020-09-22 08:55:10 +02002977 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002978 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2979
2980 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002981 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002982 for (i = 0; i < exp->used; ++i) {
2983 if (!exp->repeat[i]) {
2984 dup->repeat[i] = NULL;
2985 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02002986 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002987 /* the ending 0 as well */
2988 ++j;
2989
Michal Vasko99c71642020-07-03 13:33:36 +02002990 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002991 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002992 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
2993 dup->repeat[i][j - 1] = 0;
2994 }
2995 }
2996
2997 dup->used = exp->used;
2998 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02002999 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003000
Michal Vasko1734be92020-09-22 08:55:10 +02003001cleanup:
3002 if (ret) {
3003 lyxp_expr_free(ctx, dup);
3004 } else {
3005 *dup_p = dup;
3006 }
3007 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003008}
3009
Michal Vasko03ff5a72019-09-11 13:49:33 +02003010/*
3011 * warn functions
3012 *
3013 * Warn functions check specific reasonable conditions for schema XPath
3014 * and print a warning if they are not satisfied.
3015 */
3016
3017/**
3018 * @brief Get the last-added schema node that is currently in the context.
3019 *
3020 * @param[in] set Set to search in.
3021 * @return Last-added schema context node, NULL if no node is in context.
3022 */
3023static struct lysc_node *
3024warn_get_scnode_in_ctx(struct lyxp_set *set)
3025{
3026 uint32_t i;
3027
3028 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3029 return NULL;
3030 }
3031
3032 i = set->used;
3033 do {
3034 --i;
3035 if (set->val.scnodes[i].in_ctx == 1) {
3036 /* if there are more, simply return the first found (last added) */
3037 return set->val.scnodes[i].scnode;
3038 }
3039 } while (i);
3040
3041 return NULL;
3042}
3043
3044/**
3045 * @brief Test whether a type is numeric - integer type or decimal64.
3046 *
3047 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003048 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003049 */
Radek Krejci857189e2020-09-01 13:26:36 +02003050static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003051warn_is_numeric_type(struct lysc_type *type)
3052{
3053 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003054 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003055 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003056
3057 switch (type->basetype) {
3058 case LY_TYPE_DEC64:
3059 case LY_TYPE_INT8:
3060 case LY_TYPE_UINT8:
3061 case LY_TYPE_INT16:
3062 case LY_TYPE_UINT16:
3063 case LY_TYPE_INT32:
3064 case LY_TYPE_UINT32:
3065 case LY_TYPE_INT64:
3066 case LY_TYPE_UINT64:
3067 return 1;
3068 case LY_TYPE_UNION:
3069 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003070 LY_ARRAY_FOR(uni->types, u) {
3071 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003072 if (ret) {
3073 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003074 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003075 }
3076 }
3077 /* did not find any suitable type */
3078 return 0;
3079 case LY_TYPE_LEAFREF:
3080 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3081 default:
3082 return 0;
3083 }
3084}
3085
3086/**
3087 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3088 *
3089 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003090 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003091 */
Radek Krejci857189e2020-09-01 13:26:36 +02003092static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003093warn_is_string_type(struct lysc_type *type)
3094{
3095 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003096 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003097 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003098
3099 switch (type->basetype) {
3100 case LY_TYPE_BITS:
3101 case LY_TYPE_ENUM:
3102 case LY_TYPE_IDENT:
3103 case LY_TYPE_INST:
3104 case LY_TYPE_STRING:
3105 return 1;
3106 case LY_TYPE_UNION:
3107 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003108 LY_ARRAY_FOR(uni->types, u) {
3109 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003110 if (ret) {
3111 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003112 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003113 }
3114 }
3115 /* did not find any suitable type */
3116 return 0;
3117 case LY_TYPE_LEAFREF:
3118 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3119 default:
3120 return 0;
3121 }
3122}
3123
3124/**
3125 * @brief Test whether a type is one specific type.
3126 *
3127 * @param[in] type Type to test.
3128 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003129 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003130 */
Radek Krejci857189e2020-09-01 13:26:36 +02003131static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003132warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3133{
3134 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003135 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003136 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003137
3138 if (type->basetype == base) {
3139 return 1;
3140 } else if (type->basetype == LY_TYPE_UNION) {
3141 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003142 LY_ARRAY_FOR(uni->types, u) {
3143 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003144 if (ret) {
3145 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003146 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003147 }
3148 }
3149 /* did not find any suitable type */
3150 return 0;
3151 } else if (type->basetype == LY_TYPE_LEAFREF) {
3152 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3153 }
3154
3155 return 0;
3156}
3157
3158/**
3159 * @brief Get next type of a (union) type.
3160 *
3161 * @param[in] type Base type.
3162 * @param[in] prev_type Previously returned type.
3163 * @return Next type or NULL.
3164 */
3165static struct lysc_type *
3166warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3167{
3168 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003169 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003170 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003171
3172 switch (type->basetype) {
3173 case LY_TYPE_UNION:
3174 uni = (struct lysc_type_union *)type;
3175 if (!prev_type) {
3176 return uni->types[0];
3177 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003178 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003179 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003180 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003181 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003182 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003183 found = 1;
3184 }
3185 }
3186 return NULL;
3187 default:
3188 if (prev_type) {
3189 assert(type == prev_type);
3190 return NULL;
3191 } else {
3192 return type;
3193 }
3194 }
3195}
3196
3197/**
3198 * @brief Test whether 2 types have a common type.
3199 *
3200 * @param[in] type1 First type.
3201 * @param[in] type2 Second type.
3202 * @return 1 if they do, 0 otherwise.
3203 */
3204static int
3205warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3206{
3207 struct lysc_type *t1, *rt1, *t2, *rt2;
3208
3209 t1 = NULL;
3210 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3211 if (t1->basetype == LY_TYPE_LEAFREF) {
3212 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3213 } else {
3214 rt1 = t1;
3215 }
3216
3217 t2 = NULL;
3218 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3219 if (t2->basetype == LY_TYPE_LEAFREF) {
3220 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3221 } else {
3222 rt2 = t2;
3223 }
3224
3225 if (rt2->basetype == rt1->basetype) {
3226 /* match found */
3227 return 1;
3228 }
3229 }
3230 }
3231
3232 return 0;
3233}
3234
3235/**
3236 * @brief Check both operands of comparison operators.
3237 *
3238 * @param[in] ctx Context for errors.
3239 * @param[in] set1 First operand set.
3240 * @param[in] set2 Second operand set.
3241 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3242 * @param[in] expr Start of the expression to print with the warning.
3243 * @param[in] tok_pos Token position.
3244 */
3245static void
Radek Krejci857189e2020-09-01 13:26:36 +02003246warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr, uint16_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003247{
3248 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003249 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003250
3251 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3252 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3253
3254 if (!node1 && !node2) {
3255 /* no node-sets involved, nothing to do */
3256 return;
3257 }
3258
3259 if (node1) {
3260 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3261 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3262 warning = 1;
3263 leaves = 0;
3264 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3265 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3266 warning = 1;
3267 }
3268 }
3269
3270 if (node2) {
3271 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3272 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3273 warning = 1;
3274 leaves = 0;
3275 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3276 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3277 warning = 1;
3278 }
3279 }
3280
3281 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003282 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3283 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3284 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3285 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003286 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3287 warning = 1;
3288 }
3289 }
3290
3291 if (warning) {
3292 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3293 }
3294}
3295
3296/**
3297 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3298 *
3299 * @param[in] exp Parsed XPath expression.
3300 * @param[in] set Set with the leaf/leaf-list.
3301 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3302 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3303 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3304 */
3305static void
Michal Vasko40308e72020-10-20 16:38:40 +02003306warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3307 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003308{
3309 struct lysc_node *scnode;
3310 struct lysc_type *type;
3311 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003312 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003313 LY_ERR rc;
3314 struct ly_err_item *err = NULL;
3315
Michal Vasko69730152020-10-09 16:30:07 +02003316 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3317 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003318 /* check that the node can have the specified value */
3319 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3320 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3321 } else {
3322 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3323 }
3324 if (!value) {
3325 LOGMEM(set->ctx);
3326 return;
3327 }
3328
3329 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3330 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003331 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003332 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003333 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003334 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003335 }
3336
3337 type = ((struct lysc_node_leaf *)scnode)->type;
3338 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003339 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003340 LYD_HINT_DATA, scnode, &storage, &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003341
3342 if (err) {
3343 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3344 ly_err_free(err);
3345 } else if (rc != LY_SUCCESS) {
3346 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3347 }
3348 if (rc != LY_SUCCESS) {
3349 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003350 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003351 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003352 } else {
3353 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003354 }
3355 }
3356 free(value);
3357 }
3358}
3359
3360/*
3361 * XPath functions
3362 */
3363
3364/**
3365 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3366 * depending on whether the first node bit value from the second argument is set.
3367 *
3368 * @param[in] args Array of arguments.
3369 * @param[in] arg_count Count of elements in @p args.
3370 * @param[in,out] set Context and result set at the same time.
3371 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003372 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003373 */
3374static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003375xpath_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 +02003376{
3377 struct lyd_node_term *leaf;
3378 struct lysc_node_leaf *sleaf;
3379 struct lysc_type_bits *bits;
3380 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003381 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003382
3383 if (options & LYXP_SCNODE_ALL) {
3384 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3385 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003386 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3387 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 +02003388 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3389 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003390 }
3391
3392 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3393 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3394 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 +02003395 } else if (!warn_is_string_type(sleaf->type)) {
3396 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003397 }
3398 }
3399 set_scnode_clear_ctx(set);
3400 return rc;
3401 }
3402
Michal Vaskod3678892020-05-21 10:06:58 +02003403 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003404 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3405 "bit-is-set(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003406 return LY_EVALID;
3407 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003408 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003409 LY_CHECK_RET(rc);
3410
3411 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003412 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003413 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003414 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3415 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003416 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003417 LY_ARRAY_FOR(bits->bits, u) {
3418 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003419 set_fill_boolean(set, 1);
3420 break;
3421 }
3422 }
3423 }
3424 }
3425
3426 return LY_SUCCESS;
3427}
3428
3429/**
3430 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3431 * with the argument converted to boolean.
3432 *
3433 * @param[in] args Array of arguments.
3434 * @param[in] arg_count Count of elements in @p args.
3435 * @param[in,out] set Context and result set at the same time.
3436 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003437 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003438 */
3439static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003440xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003441{
3442 LY_ERR rc;
3443
3444 if (options & LYXP_SCNODE_ALL) {
3445 set_scnode_clear_ctx(set);
3446 return LY_SUCCESS;
3447 }
3448
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003449 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003450 LY_CHECK_RET(rc);
3451 set_fill_set(set, args[0]);
3452
3453 return LY_SUCCESS;
3454}
3455
3456/**
3457 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3458 * with the first argument rounded up to the nearest integer.
3459 *
3460 * @param[in] args Array of arguments.
3461 * @param[in] arg_count Count of elements in @p args.
3462 * @param[in,out] set Context and result set at the same time.
3463 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003464 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003465 */
3466static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003467xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003468{
3469 struct lysc_node_leaf *sleaf;
3470 LY_ERR rc = LY_SUCCESS;
3471
3472 if (options & LYXP_SCNODE_ALL) {
3473 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3474 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003475 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3476 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 +02003477 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3478 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003479 }
3480 set_scnode_clear_ctx(set);
3481 return rc;
3482 }
3483
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003484 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003485 LY_CHECK_RET(rc);
3486 if ((long long)args[0]->val.num != args[0]->val.num) {
3487 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3488 } else {
3489 set_fill_number(set, args[0]->val.num);
3490 }
3491
3492 return LY_SUCCESS;
3493}
3494
3495/**
3496 * @brief Execute the XPath concat(string, string, string*) function.
3497 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3498 *
3499 * @param[in] args Array of arguments.
3500 * @param[in] arg_count Count of elements in @p args.
3501 * @param[in,out] set Context and result set at the same time.
3502 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003503 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003504 */
3505static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003506xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003507{
3508 uint16_t i;
3509 char *str = NULL;
3510 size_t used = 1;
3511 LY_ERR rc = LY_SUCCESS;
3512 struct lysc_node_leaf *sleaf;
3513
3514 if (options & LYXP_SCNODE_ALL) {
3515 for (i = 0; i < arg_count; ++i) {
3516 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3517 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3518 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003519 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003520 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003521 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 +02003522 }
3523 }
3524 }
3525 set_scnode_clear_ctx(set);
3526 return rc;
3527 }
3528
3529 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003530 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003531 if (rc != LY_SUCCESS) {
3532 free(str);
3533 return rc;
3534 }
3535
3536 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3537 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3538 strcpy(str + used - 1, args[i]->val.str);
3539 used += strlen(args[i]->val.str);
3540 }
3541
3542 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003543 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003544 set->type = LYXP_SET_STRING;
3545 set->val.str = str;
3546
3547 return LY_SUCCESS;
3548}
3549
3550/**
3551 * @brief Execute the XPath contains(string, string) function.
3552 * Returns LYXP_SET_BOOLEAN whether the second argument can
3553 * be found in the first or not.
3554 *
3555 * @param[in] args Array of arguments.
3556 * @param[in] arg_count Count of elements in @p args.
3557 * @param[in,out] set Context and result set at the same time.
3558 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003559 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003560 */
3561static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003562xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003563{
3564 struct lysc_node_leaf *sleaf;
3565 LY_ERR rc = LY_SUCCESS;
3566
3567 if (options & LYXP_SCNODE_ALL) {
3568 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3569 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3570 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 +02003571 } else if (!warn_is_string_type(sleaf->type)) {
3572 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003573 }
3574 }
3575
3576 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3577 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3578 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 +02003579 } else if (!warn_is_string_type(sleaf->type)) {
3580 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003581 }
3582 }
3583 set_scnode_clear_ctx(set);
3584 return rc;
3585 }
3586
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003587 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003588 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003589 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003590 LY_CHECK_RET(rc);
3591
3592 if (strstr(args[0]->val.str, args[1]->val.str)) {
3593 set_fill_boolean(set, 1);
3594 } else {
3595 set_fill_boolean(set, 0);
3596 }
3597
3598 return LY_SUCCESS;
3599}
3600
3601/**
3602 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3603 * with the size of the node-set from the argument.
3604 *
3605 * @param[in] args Array of arguments.
3606 * @param[in] arg_count Count of elements in @p args.
3607 * @param[in,out] set Context and result set at the same time.
3608 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003609 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003610 */
3611static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003612xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003613{
3614 struct lysc_node *scnode = NULL;
3615 LY_ERR rc = LY_SUCCESS;
3616
3617 if (options & LYXP_SCNODE_ALL) {
3618 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3619 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003620 }
3621 set_scnode_clear_ctx(set);
3622 return rc;
3623 }
3624
Michal Vasko03ff5a72019-09-11 13:49:33 +02003625 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003626 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 +02003627 return LY_EVALID;
3628 }
3629
3630 set_fill_number(set, args[0]->used);
3631 return LY_SUCCESS;
3632}
3633
3634/**
3635 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3636 * with the context with the intial node.
3637 *
3638 * @param[in] args Array of arguments.
3639 * @param[in] arg_count Count of elements in @p args.
3640 * @param[in,out] set Context and result set at the same time.
3641 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003642 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003643 */
3644static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003645xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003646{
3647 if (arg_count || args) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003648 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003649 return LY_EVALID;
3650 }
3651
3652 if (options & LYXP_SCNODE_ALL) {
3653 set_scnode_clear_ctx(set);
3654
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003655 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003656 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003657 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003658
3659 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003660 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003661 }
3662
3663 return LY_SUCCESS;
3664}
3665
3666/**
3667 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3668 * leafref or instance-identifier target node(s).
3669 *
3670 * @param[in] args Array of arguments.
3671 * @param[in] arg_count Count of elements in @p args.
3672 * @param[in,out] set Context and result set at the same time.
3673 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003674 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003675 */
3676static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003677xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003678{
3679 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003680 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003681 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003682 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003683 struct ly_path *p;
3684 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003685 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003686 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003687 LY_ERR rc = LY_SUCCESS;
3688
3689 if (options & LYXP_SCNODE_ALL) {
3690 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3691 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003692 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3693 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 +02003694 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3695 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
Michal Vasko69730152020-10-09 16:30:07 +02003696 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003697 }
3698 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003699 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003700 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vasko00cbf532020-06-15 13:58:47 +02003701 oper = lysc_is_output((struct lysc_node *)sleaf) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003702
3703 /* it was already evaluated on schema, it must succeed */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003704 rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path,
3705 LY_PATH_LREF_TRUE, oper, LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003706 assert(!rc);
3707
3708 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003709 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003710 ly_path_free(set->ctx, p);
3711
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003712 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003713 }
3714
Michal Vasko03ff5a72019-09-11 13:49:33 +02003715 return rc;
3716 }
3717
Michal Vaskod3678892020-05-21 10:06:58 +02003718 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003719 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 +02003720 return LY_EVALID;
3721 }
3722
Michal Vaskod3678892020-05-21 10:06:58 +02003723 lyxp_set_free_content(set);
3724 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003725 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3726 sleaf = (struct lysc_node_leaf *)leaf->schema;
3727 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3728 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3729 /* find leafref target */
Michal Vasko004d3152020-06-11 19:59:22 +02003730 if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf,
Michal Vasko69730152020-10-09 16:30:07 +02003731 &leaf->value, set->tree, &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003732 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003733 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003734 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003735 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003736 } else {
3737 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003738 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003739 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Michal Vasko69730152020-10-09 16:30:07 +02003740 LYD_CANON_VALUE(leaf));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003741 return LY_EVALID;
3742 }
3743 }
Michal Vasko004d3152020-06-11 19:59:22 +02003744
3745 /* insert it */
3746 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003747 }
3748 }
3749
3750 return LY_SUCCESS;
3751}
3752
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003753static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003754xpath_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 +02003755{
3756 uint16_t i;
3757 LY_ARRAY_COUNT_TYPE u;
3758 struct lyd_node_term *leaf;
3759 struct lysc_node_leaf *sleaf;
3760 struct lyd_meta *meta;
3761 struct lyd_value data = {0}, *val;
3762 struct ly_err_item *err = NULL;
3763 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003764 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003765
3766 if (options & LYXP_SCNODE_ALL) {
3767 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3768 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
3769 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3770 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003771 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003772 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3773 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3774 }
3775
3776 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3777 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3778 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003779 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003780 } else if (!warn_is_string_type(sleaf->type)) {
3781 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3782 }
3783 }
3784 set_scnode_clear_ctx(set);
3785 return rc;
3786 }
3787
3788 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003789 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 +02003790 "derived-from(-or-self)(node-set, string)");
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003791 return LY_EVALID;
3792 }
3793 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3794 LY_CHECK_RET(rc);
3795
3796 set_fill_boolean(set, 0);
3797 found = 0;
3798 for (i = 0; i < args[0]->used; ++i) {
3799 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3800 continue;
3801 }
3802
3803 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3804 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3805 sleaf = (struct lysc_node_leaf *)leaf->schema;
3806 val = &leaf->value;
3807 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3808 /* uninteresting */
3809 continue;
3810 }
3811
3812 /* store args[1] as ident */
3813 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003814 0, set->format, set->prefix_data, LYD_HINT_DATA, leaf->schema, &data, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003815 } else {
3816 meta = args[0]->val.meta[i].meta;
3817 val = &meta->value;
3818 if (val->realtype->basetype != LY_TYPE_IDENT) {
3819 /* uninteresting */
3820 continue;
3821 }
3822
3823 /* store args[1] as ident */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003824 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), 0,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003825 set->format, set->prefix_data, LYD_HINT_DATA, meta->parent->schema, &data, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003826 }
3827
3828 if (err) {
3829 ly_err_print(err);
3830 ly_err_free(err);
3831 }
3832 LY_CHECK_RET(rc);
3833
3834 /* finally check the identity itself */
3835 if (self_match && (data.ident == val->ident)) {
3836 set_fill_boolean(set, 1);
3837 found = 1;
3838 }
3839 if (!found) {
3840 LY_ARRAY_FOR(data.ident->derived, u) {
3841 if (data.ident->derived[u] == val->ident) {
3842 set_fill_boolean(set, 1);
3843 found = 1;
3844 break;
3845 }
3846 }
3847 }
3848
3849 /* free temporary value */
3850 val->realtype->plugin->free(set->ctx, &data);
3851 if (found) {
3852 break;
3853 }
3854 }
3855
3856 return LY_SUCCESS;
3857}
3858
Michal Vasko03ff5a72019-09-11 13:49:33 +02003859/**
3860 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3861 * on whether the first argument nodes contain a node of an identity derived from the second
3862 * argument identity.
3863 *
3864 * @param[in] args Array of arguments.
3865 * @param[in] arg_count Count of elements in @p args.
3866 * @param[in,out] set Context and result set at the same time.
3867 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003868 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003869 */
3870static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003871xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003872{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003873 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003874}
3875
3876/**
3877 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3878 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3879 * the second argument identity.
3880 *
3881 * @param[in] args Array of arguments.
3882 * @param[in] arg_count Count of elements in @p args.
3883 * @param[in,out] set Context and result set at the same time.
3884 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003885 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003886 */
3887static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003888xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003889{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003890 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003891}
3892
3893/**
3894 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3895 * with the integer value of the first node's enum value, otherwise NaN.
3896 *
3897 * @param[in] args Array of arguments.
3898 * @param[in] arg_count Count of elements in @p args.
3899 * @param[in,out] set Context and result set at the same time.
3900 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003901 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003902 */
3903static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003904xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003905{
3906 struct lyd_node_term *leaf;
3907 struct lysc_node_leaf *sleaf;
3908 LY_ERR rc = LY_SUCCESS;
3909
3910 if (options & LYXP_SCNODE_ALL) {
3911 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3912 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003913 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3914 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003915 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3916 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003917 }
3918 set_scnode_clear_ctx(set);
3919 return rc;
3920 }
3921
Michal Vaskod3678892020-05-21 10:06:58 +02003922 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003923 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 +02003924 return LY_EVALID;
3925 }
3926
3927 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003928 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003929 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3930 sleaf = (struct lysc_node_leaf *)leaf->schema;
3931 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3932 set_fill_number(set, leaf->value.enum_item->value);
3933 }
3934 }
3935
3936 return LY_SUCCESS;
3937}
3938
3939/**
3940 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3941 * with false value.
3942 *
3943 * @param[in] args Array of arguments.
3944 * @param[in] arg_count Count of elements in @p args.
3945 * @param[in,out] set Context and result set at the same time.
3946 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003947 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003948 */
3949static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003950xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003951{
3952 if (options & LYXP_SCNODE_ALL) {
3953 set_scnode_clear_ctx(set);
3954 return LY_SUCCESS;
3955 }
3956
3957 set_fill_boolean(set, 0);
3958 return LY_SUCCESS;
3959}
3960
3961/**
3962 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3963 * with the first argument floored (truncated).
3964 *
3965 * @param[in] args Array of arguments.
3966 * @param[in] arg_count Count of elements in @p args.
3967 * @param[in,out] set Context and result set at the same time.
3968 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003969 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003970 */
3971static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003972xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t UNUSED(options))
Michal Vasko03ff5a72019-09-11 13:49:33 +02003973{
3974 LY_ERR rc;
3975
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003976 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003977 LY_CHECK_RET(rc);
3978 if (isfinite(args[0]->val.num)) {
3979 set_fill_number(set, (long long)args[0]->val.num);
3980 }
3981
3982 return LY_SUCCESS;
3983}
3984
3985/**
3986 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3987 * whether the language of the text matches the one from the argument.
3988 *
3989 * @param[in] args Array of arguments.
3990 * @param[in] arg_count Count of elements in @p args.
3991 * @param[in,out] set Context and result set at the same time.
3992 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003993 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003994 */
3995static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003996xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003997{
3998 const struct lyd_node *node;
3999 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004000 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004001 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004002 LY_ERR rc = LY_SUCCESS;
4003
4004 if (options & LYXP_SCNODE_ALL) {
4005 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4006 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4007 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004008 } else if (!warn_is_string_type(sleaf->type)) {
4009 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004010 }
4011 }
4012 set_scnode_clear_ctx(set);
4013 return rc;
4014 }
4015
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004016 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004017 LY_CHECK_RET(rc);
4018
Michal Vasko03ff5a72019-09-11 13:49:33 +02004019 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004020 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 +02004021 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004022 } else if (!set->used) {
4023 set_fill_boolean(set, 0);
4024 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004025 }
4026
4027 switch (set->val.nodes[0].type) {
4028 case LYXP_NODE_ELEM:
4029 case LYXP_NODE_TEXT:
4030 node = set->val.nodes[0].node;
4031 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004032 case LYXP_NODE_META:
4033 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004034 break;
4035 default:
4036 /* nothing to do with roots */
4037 set_fill_boolean(set, 0);
4038 return LY_SUCCESS;
4039 }
4040
Michal Vasko9f96a052020-03-10 09:41:45 +01004041 /* find lang metadata */
Michal Vaskod989ba02020-08-24 10:59:24 +02004042 for ( ; node; node = (struct lyd_node *)node->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004043 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004044 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004045 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004046 break;
4047 }
4048 }
4049
Michal Vasko9f96a052020-03-10 09:41:45 +01004050 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004051 break;
4052 }
4053 }
4054
4055 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004056 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004057 set_fill_boolean(set, 0);
4058 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004059 uint64_t i;
4060
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004061 val = meta->value.canonical;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004062 for (i = 0; args[0]->val.str[i]; ++i) {
4063 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4064 set_fill_boolean(set, 0);
4065 break;
4066 }
4067 }
4068 if (!args[0]->val.str[i]) {
4069 if (!val[i] || (val[i] == '-')) {
4070 set_fill_boolean(set, 1);
4071 } else {
4072 set_fill_boolean(set, 0);
4073 }
4074 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004075 }
4076
4077 return LY_SUCCESS;
4078}
4079
4080/**
4081 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4082 * with the context size.
4083 *
4084 * @param[in] args Array of arguments.
4085 * @param[in] arg_count Count of elements in @p args.
4086 * @param[in,out] set Context and result set at the same time.
4087 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004088 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004089 */
4090static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004091xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004092{
4093 if (options & LYXP_SCNODE_ALL) {
4094 set_scnode_clear_ctx(set);
4095 return LY_SUCCESS;
4096 }
4097
Michal Vasko03ff5a72019-09-11 13:49:33 +02004098 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004099 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 +02004100 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004101 } else if (!set->used) {
4102 set_fill_number(set, 0);
4103 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004104 }
4105
4106 set_fill_number(set, set->ctx_size);
4107 return LY_SUCCESS;
4108}
4109
4110/**
4111 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4112 * with the node name without namespace from the argument or the context.
4113 *
4114 * @param[in] args Array of arguments.
4115 * @param[in] arg_count Count of elements in @p args.
4116 * @param[in,out] set Context and result set at the same time.
4117 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004118 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004119 */
4120static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004121xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004122{
4123 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004124
Michal Vasko03ff5a72019-09-11 13:49:33 +02004125 /* suppress unused variable warning */
4126 (void)options;
4127
4128 if (options & LYXP_SCNODE_ALL) {
4129 set_scnode_clear_ctx(set);
4130 return LY_SUCCESS;
4131 }
4132
4133 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004134 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004135 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
4136 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004137 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004138 } else if (!args[0]->used) {
4139 set_fill_string(set, "", 0);
4140 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004141 }
4142
4143 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004144 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004145
4146 item = &args[0]->val.nodes[0];
4147 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004148 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004149 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 +02004150 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004151 } else if (!set->used) {
4152 set_fill_string(set, "", 0);
4153 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004154 }
4155
4156 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004157 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004158
4159 item = &set->val.nodes[0];
4160 }
4161
4162 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004163 case LYXP_NODE_NONE:
4164 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004165 case LYXP_NODE_ROOT:
4166 case LYXP_NODE_ROOT_CONFIG:
4167 case LYXP_NODE_TEXT:
4168 set_fill_string(set, "", 0);
4169 break;
4170 case LYXP_NODE_ELEM:
4171 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4172 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004173 case LYXP_NODE_META:
4174 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004175 break;
4176 }
4177
4178 return LY_SUCCESS;
4179}
4180
4181/**
4182 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4183 * with the node name fully qualified (with namespace) from the argument or the context.
4184 *
4185 * @param[in] args Array of arguments.
4186 * @param[in] arg_count Count of elements in @p args.
4187 * @param[in,out] set Context and result set at the same time.
4188 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004189 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004190 */
4191static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004192xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004193{
4194 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004195 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004196 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004197 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004198
4199 if (options & LYXP_SCNODE_ALL) {
4200 set_scnode_clear_ctx(set);
4201 return LY_SUCCESS;
4202 }
4203
4204 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004205 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004206 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 +02004207 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004208 } else if (!args[0]->used) {
4209 set_fill_string(set, "", 0);
4210 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004211 }
4212
4213 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004214 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004215
4216 item = &args[0]->val.nodes[0];
4217 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004218 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004219 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 +02004220 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004221 } else if (!set->used) {
4222 set_fill_string(set, "", 0);
4223 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004224 }
4225
4226 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004227 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004228
4229 item = &set->val.nodes[0];
4230 }
4231
4232 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004233 case LYXP_NODE_NONE:
4234 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004235 case LYXP_NODE_ROOT:
4236 case LYXP_NODE_ROOT_CONFIG:
4237 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004238 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004239 break;
4240 case LYXP_NODE_ELEM:
4241 mod = item->node->schema->module;
4242 name = item->node->schema->name;
4243 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004244 case LYXP_NODE_META:
4245 mod = ((struct lyd_meta *)item->node)->annotation->module;
4246 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004247 break;
4248 }
4249
4250 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004251 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004252 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4253 set_fill_string(set, str, strlen(str));
4254 free(str);
4255 } else {
4256 set_fill_string(set, "", 0);
4257 }
4258
4259 return LY_SUCCESS;
4260}
4261
4262/**
4263 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4264 * with the namespace of the node from the argument or the context.
4265 *
4266 * @param[in] args Array of arguments.
4267 * @param[in] arg_count Count of elements in @p args.
4268 * @param[in,out] set Context and result set at the same time.
4269 * @param[in] options XPath options.
4270 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4271 */
4272static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004273xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004274{
4275 struct lyxp_set_node *item;
4276 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004277
Michal Vasko03ff5a72019-09-11 13:49:33 +02004278 /* suppress unused variable warning */
4279 (void)options;
4280
4281 if (options & LYXP_SCNODE_ALL) {
4282 set_scnode_clear_ctx(set);
4283 return LY_SUCCESS;
4284 }
4285
4286 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004287 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004288 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 +02004289 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004290 return LY_EVALID;
4291 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004292 set_fill_string(set, "", 0);
4293 return LY_SUCCESS;
4294 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004295
4296 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004297 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004298
4299 item = &args[0]->val.nodes[0];
4300 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004301 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004302 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 +02004303 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004304 } else if (!set->used) {
4305 set_fill_string(set, "", 0);
4306 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004307 }
4308
4309 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004310 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004311
4312 item = &set->val.nodes[0];
4313 }
4314
4315 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004316 case LYXP_NODE_NONE:
4317 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004318 case LYXP_NODE_ROOT:
4319 case LYXP_NODE_ROOT_CONFIG:
4320 case LYXP_NODE_TEXT:
4321 set_fill_string(set, "", 0);
4322 break;
4323 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004324 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004325 if (item->type == LYXP_NODE_ELEM) {
4326 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004327 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004328 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004329 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004330 }
4331
4332 set_fill_string(set, mod->ns, strlen(mod->ns));
4333 break;
4334 }
4335
4336 return LY_SUCCESS;
4337}
4338
4339/**
4340 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4341 * with only nodes from the context. In practice it either leaves the context
4342 * as it is or returns an empty node set.
4343 *
4344 * @param[in] args Array of arguments.
4345 * @param[in] arg_count Count of elements in @p args.
4346 * @param[in,out] set Context and result set at the same time.
4347 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004348 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004349 */
4350static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004351xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004352{
4353 if (options & LYXP_SCNODE_ALL) {
4354 set_scnode_clear_ctx(set);
4355 return LY_SUCCESS;
4356 }
4357
4358 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004359 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004360 }
4361 return LY_SUCCESS;
4362}
4363
4364/**
4365 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4366 * with normalized value (no leading, trailing, double white spaces) of the node
4367 * from the argument or the context.
4368 *
4369 * @param[in] args Array of arguments.
4370 * @param[in] arg_count Count of elements in @p args.
4371 * @param[in,out] set Context and result set at the same time.
4372 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004373 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004374 */
4375static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004376xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004377{
4378 uint16_t i, new_used;
4379 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004380 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004381 struct lysc_node_leaf *sleaf;
4382 LY_ERR rc = LY_SUCCESS;
4383
4384 if (options & LYXP_SCNODE_ALL) {
4385 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4386 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4387 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004388 } else if (!warn_is_string_type(sleaf->type)) {
4389 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004390 }
4391 }
4392 set_scnode_clear_ctx(set);
4393 return rc;
4394 }
4395
4396 if (arg_count) {
4397 set_fill_set(set, args[0]);
4398 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004399 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004400 LY_CHECK_RET(rc);
4401
4402 /* is there any normalization necessary? */
4403 for (i = 0; set->val.str[i]; ++i) {
4404 if (is_xmlws(set->val.str[i])) {
4405 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4406 have_spaces = 1;
4407 break;
4408 }
4409 space_before = 1;
4410 } else {
4411 space_before = 0;
4412 }
4413 }
4414
4415 /* yep, there is */
4416 if (have_spaces) {
4417 /* it's enough, at least one character will go, makes space for ending '\0' */
4418 new = malloc(strlen(set->val.str) * sizeof(char));
4419 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4420 new_used = 0;
4421
4422 space_before = 0;
4423 for (i = 0; set->val.str[i]; ++i) {
4424 if (is_xmlws(set->val.str[i])) {
4425 if ((i == 0) || space_before) {
4426 space_before = 1;
4427 continue;
4428 } else {
4429 space_before = 1;
4430 }
4431 } else {
4432 space_before = 0;
4433 }
4434
4435 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4436 ++new_used;
4437 }
4438
4439 /* at worst there is one trailing space now */
4440 if (new_used && is_xmlws(new[new_used - 1])) {
4441 --new_used;
4442 }
4443
4444 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4445 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4446 new[new_used] = '\0';
4447
4448 free(set->val.str);
4449 set->val.str = new;
4450 }
4451
4452 return LY_SUCCESS;
4453}
4454
4455/**
4456 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4457 * with the argument converted to boolean and logically inverted.
4458 *
4459 * @param[in] args Array of arguments.
4460 * @param[in] arg_count Count of elements in @p args.
4461 * @param[in,out] set Context and result set at the same time.
4462 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004463 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004464 */
4465static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004466xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004467{
4468 if (options & LYXP_SCNODE_ALL) {
4469 set_scnode_clear_ctx(set);
4470 return LY_SUCCESS;
4471 }
4472
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004473 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004474 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004475 set_fill_boolean(set, 0);
4476 } else {
4477 set_fill_boolean(set, 1);
4478 }
4479
4480 return LY_SUCCESS;
4481}
4482
4483/**
4484 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4485 * with the number representation of either the argument or the context.
4486 *
4487 * @param[in] args Array of arguments.
4488 * @param[in] arg_count Count of elements in @p args.
4489 * @param[in,out] set Context and result set at the same time.
4490 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004491 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004492 */
4493static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004494xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004495{
4496 LY_ERR rc;
4497
4498 if (options & LYXP_SCNODE_ALL) {
4499 set_scnode_clear_ctx(set);
4500 return LY_SUCCESS;
4501 }
4502
4503 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004504 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004505 LY_CHECK_RET(rc);
4506 set_fill_set(set, args[0]);
4507 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004508 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004509 LY_CHECK_RET(rc);
4510 }
4511
4512 return LY_SUCCESS;
4513}
4514
4515/**
4516 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4517 * with the context position.
4518 *
4519 * @param[in] args Array of arguments.
4520 * @param[in] arg_count Count of elements in @p args.
4521 * @param[in,out] set Context and result set at the same time.
4522 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004523 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004524 */
4525static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004526xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004527{
4528 if (options & LYXP_SCNODE_ALL) {
4529 set_scnode_clear_ctx(set);
4530 return LY_SUCCESS;
4531 }
4532
Michal Vasko03ff5a72019-09-11 13:49:33 +02004533 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004534 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 +02004535 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004536 } else if (!set->used) {
4537 set_fill_number(set, 0);
4538 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004539 }
4540
4541 set_fill_number(set, set->ctx_pos);
4542
4543 /* UNUSED in 'Release' build type */
4544 (void)options;
4545 return LY_SUCCESS;
4546}
4547
4548/**
4549 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4550 * depending on whether the second argument regex matches the first argument string. For details refer to
4551 * YANG 1.1 RFC section 10.2.1.
4552 *
4553 * @param[in] args Array of arguments.
4554 * @param[in] arg_count Count of elements in @p args.
4555 * @param[in,out] set Context and result set at the same time.
4556 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004557 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004558 */
4559static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004560xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004561{
4562 struct lysc_pattern **patterns = NULL, **pattern;
4563 struct lysc_node_leaf *sleaf;
4564 char *path;
4565 LY_ERR rc = LY_SUCCESS;
4566 struct ly_err_item *err;
4567
4568 if (options & LYXP_SCNODE_ALL) {
4569 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4570 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4571 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004572 } else if (!warn_is_string_type(sleaf->type)) {
4573 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004574 }
4575 }
4576
4577 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4578 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4579 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004580 } else if (!warn_is_string_type(sleaf->type)) {
4581 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004582 }
4583 }
4584 set_scnode_clear_ctx(set);
4585 return rc;
4586 }
4587
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004588 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004589 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004590 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004591 LY_CHECK_RET(rc);
4592
4593 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4594 *pattern = malloc(sizeof **pattern);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004595 path = lyd_path(set->cur_node, LYD_PATH_LOG, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004596 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4597 free(path);
4598 if (rc != LY_SUCCESS) {
4599 LY_ARRAY_FREE(patterns);
4600 return rc;
4601 }
4602
4603 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4604 pcre2_code_free((*pattern)->code);
4605 free(*pattern);
4606 LY_ARRAY_FREE(patterns);
4607 if (rc && (rc != LY_EVALID)) {
4608 ly_err_print(err);
4609 ly_err_free(err);
4610 return rc;
4611 }
4612
4613 if (rc == LY_EVALID) {
4614 ly_err_free(err);
4615 set_fill_boolean(set, 0);
4616 } else {
4617 set_fill_boolean(set, 1);
4618 }
4619
4620 return LY_SUCCESS;
4621}
4622
4623/**
4624 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4625 * with the rounded first argument. For details refer to
4626 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4627 *
4628 * @param[in] args Array of arguments.
4629 * @param[in] arg_count Count of elements in @p args.
4630 * @param[in,out] set Context and result set at the same time.
4631 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004632 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004633 */
4634static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004635xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004636{
4637 struct lysc_node_leaf *sleaf;
4638 LY_ERR rc = LY_SUCCESS;
4639
4640 if (options & LYXP_SCNODE_ALL) {
4641 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4642 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004643 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4644 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004645 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4646 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004647 }
4648 set_scnode_clear_ctx(set);
4649 return rc;
4650 }
4651
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004652 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004653 LY_CHECK_RET(rc);
4654
4655 /* cover only the cases where floor can't be used */
4656 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4657 set_fill_number(set, -0.0f);
4658 } else {
4659 args[0]->val.num += 0.5;
4660 rc = xpath_floor(args, 1, args[0], options);
4661 LY_CHECK_RET(rc);
4662 set_fill_number(set, args[0]->val.num);
4663 }
4664
4665 return LY_SUCCESS;
4666}
4667
4668/**
4669 * @brief Execute the XPath starts-with(string, string) function.
4670 * Returns LYXP_SET_BOOLEAN whether the second argument is
4671 * the prefix of the first or not.
4672 *
4673 * @param[in] args Array of arguments.
4674 * @param[in] arg_count Count of elements in @p args.
4675 * @param[in,out] set Context and result set at the same time.
4676 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004677 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004678 */
4679static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004680xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004681{
4682 struct lysc_node_leaf *sleaf;
4683 LY_ERR rc = LY_SUCCESS;
4684
4685 if (options & LYXP_SCNODE_ALL) {
4686 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4687 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4688 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004689 } else if (!warn_is_string_type(sleaf->type)) {
4690 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004691 }
4692 }
4693
4694 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4695 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4696 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004697 } else if (!warn_is_string_type(sleaf->type)) {
4698 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004699 }
4700 }
4701 set_scnode_clear_ctx(set);
4702 return rc;
4703 }
4704
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004705 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004706 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004707 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004708 LY_CHECK_RET(rc);
4709
4710 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4711 set_fill_boolean(set, 0);
4712 } else {
4713 set_fill_boolean(set, 1);
4714 }
4715
4716 return LY_SUCCESS;
4717}
4718
4719/**
4720 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4721 * with the string representation of either the argument or the context.
4722 *
4723 * @param[in] args Array of arguments.
4724 * @param[in] arg_count Count of elements in @p args.
4725 * @param[in,out] set Context and result set at the same time.
4726 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004727 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004728 */
4729static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004730xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004731{
4732 LY_ERR rc;
4733
4734 if (options & LYXP_SCNODE_ALL) {
4735 set_scnode_clear_ctx(set);
4736 return LY_SUCCESS;
4737 }
4738
4739 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004740 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004741 LY_CHECK_RET(rc);
4742 set_fill_set(set, args[0]);
4743 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004744 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004745 LY_CHECK_RET(rc);
4746 }
4747
4748 return LY_SUCCESS;
4749}
4750
4751/**
4752 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4753 * with the length of the string in either the argument or the context.
4754 *
4755 * @param[in] args Array of arguments.
4756 * @param[in] arg_count Count of elements in @p args.
4757 * @param[in,out] set Context and result set at the same time.
4758 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004759 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004760 */
4761static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004762xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004763{
4764 struct lysc_node_leaf *sleaf;
4765 LY_ERR rc = LY_SUCCESS;
4766
4767 if (options & LYXP_SCNODE_ALL) {
4768 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4769 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4770 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004771 } else if (!warn_is_string_type(sleaf->type)) {
4772 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004773 }
4774 }
4775 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4776 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4777 LOGWRN(set->ctx, "Argument #0 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004778 } else if (!warn_is_string_type(sleaf->type)) {
4779 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004780 }
4781 }
4782 set_scnode_clear_ctx(set);
4783 return rc;
4784 }
4785
4786 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004787 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004788 LY_CHECK_RET(rc);
4789 set_fill_number(set, strlen(args[0]->val.str));
4790 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004791 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004792 LY_CHECK_RET(rc);
4793 set_fill_number(set, strlen(set->val.str));
4794 }
4795
4796 return LY_SUCCESS;
4797}
4798
4799/**
4800 * @brief Execute the XPath substring(string, number, number?) function.
4801 * Returns LYXP_SET_STRING substring of the first argument starting
4802 * on the second argument index ending on the third argument index,
4803 * indexed from 1. For exact definition refer to
4804 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4805 *
4806 * @param[in] args Array of arguments.
4807 * @param[in] arg_count Count of elements in @p args.
4808 * @param[in,out] set Context and result set at the same time.
4809 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004810 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004811 */
4812static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004813xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004814{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004815 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004816 uint16_t str_start, str_len, pos;
4817 struct lysc_node_leaf *sleaf;
4818 LY_ERR rc = LY_SUCCESS;
4819
4820 if (options & LYXP_SCNODE_ALL) {
4821 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4822 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4823 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004824 } else if (!warn_is_string_type(sleaf->type)) {
4825 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004826 }
4827 }
4828
4829 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4830 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4831 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004832 } else if (!warn_is_numeric_type(sleaf->type)) {
4833 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004834 }
4835 }
4836
Michal Vasko69730152020-10-09 16:30:07 +02004837 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4838 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004839 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4840 LOGWRN(set->ctx, "Argument #3 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004841 } else if (!warn_is_numeric_type(sleaf->type)) {
4842 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004843 }
4844 }
4845 set_scnode_clear_ctx(set);
4846 return rc;
4847 }
4848
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004849 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004850 LY_CHECK_RET(rc);
4851
4852 /* start */
4853 if (xpath_round(&args[1], 1, args[1], options)) {
4854 return -1;
4855 }
4856 if (isfinite(args[1]->val.num)) {
4857 start = args[1]->val.num - 1;
4858 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004859 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004860 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004861 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004862 }
4863
4864 /* len */
4865 if (arg_count == 3) {
4866 rc = xpath_round(&args[2], 1, args[2], options);
4867 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004868 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004869 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004870 } else if (isfinite(args[2]->val.num)) {
4871 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004872 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004873 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004874 }
4875 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004876 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004877 }
4878
4879 /* find matching character positions */
4880 str_start = 0;
4881 str_len = 0;
4882 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4883 if (pos < start) {
4884 ++str_start;
4885 } else if (pos < start + len) {
4886 ++str_len;
4887 } else {
4888 break;
4889 }
4890 }
4891
4892 set_fill_string(set, args[0]->val.str + str_start, str_len);
4893 return LY_SUCCESS;
4894}
4895
4896/**
4897 * @brief Execute the XPath substring-after(string, string) function.
4898 * Returns LYXP_SET_STRING with the string succeeding the occurance
4899 * of the second argument in the first or an empty string.
4900 *
4901 * @param[in] args Array of arguments.
4902 * @param[in] arg_count Count of elements in @p args.
4903 * @param[in,out] set Context and result set at the same time.
4904 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004905 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004906 */
4907static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004908xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004909{
4910 char *ptr;
4911 struct lysc_node_leaf *sleaf;
4912 LY_ERR rc = LY_SUCCESS;
4913
4914 if (options & LYXP_SCNODE_ALL) {
4915 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4916 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4917 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004918 } else if (!warn_is_string_type(sleaf->type)) {
4919 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004920 }
4921 }
4922
4923 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4924 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4925 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004926 } else if (!warn_is_string_type(sleaf->type)) {
4927 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004928 }
4929 }
4930 set_scnode_clear_ctx(set);
4931 return rc;
4932 }
4933
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004934 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004935 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004936 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004937 LY_CHECK_RET(rc);
4938
4939 ptr = strstr(args[0]->val.str, args[1]->val.str);
4940 if (ptr) {
4941 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4942 } else {
4943 set_fill_string(set, "", 0);
4944 }
4945
4946 return LY_SUCCESS;
4947}
4948
4949/**
4950 * @brief Execute the XPath substring-before(string, string) function.
4951 * Returns LYXP_SET_STRING with the string preceding the occurance
4952 * of the second argument in the first or an empty string.
4953 *
4954 * @param[in] args Array of arguments.
4955 * @param[in] arg_count Count of elements in @p args.
4956 * @param[in,out] set Context and result set at the same time.
4957 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004958 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004959 */
4960static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004961xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004962{
4963 char *ptr;
4964 struct lysc_node_leaf *sleaf;
4965 LY_ERR rc = LY_SUCCESS;
4966
4967 if (options & LYXP_SCNODE_ALL) {
4968 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4969 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4970 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004971 } else if (!warn_is_string_type(sleaf->type)) {
4972 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004973 }
4974 }
4975
4976 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4977 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4978 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004979 } else if (!warn_is_string_type(sleaf->type)) {
4980 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004981 }
4982 }
4983 set_scnode_clear_ctx(set);
4984 return rc;
4985 }
4986
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004987 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004988 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004989 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004990 LY_CHECK_RET(rc);
4991
4992 ptr = strstr(args[0]->val.str, args[1]->val.str);
4993 if (ptr) {
4994 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4995 } else {
4996 set_fill_string(set, "", 0);
4997 }
4998
4999 return LY_SUCCESS;
5000}
5001
5002/**
5003 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5004 * with the sum of all the nodes in the context.
5005 *
5006 * @param[in] args Array of arguments.
5007 * @param[in] arg_count Count of elements in @p args.
5008 * @param[in,out] set Context and result set at the same time.
5009 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005010 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005011 */
5012static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005013xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005014{
5015 long double num;
5016 char *str;
5017 uint16_t i;
5018 struct lyxp_set set_item;
5019 struct lysc_node_leaf *sleaf;
5020 LY_ERR rc = LY_SUCCESS;
5021
5022 if (options & LYXP_SCNODE_ALL) {
5023 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5024 for (i = 0; i < args[0]->used; ++i) {
5025 if (args[0]->val.scnodes[i].in_ctx == 1) {
5026 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5027 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5028 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005029 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005030 } else if (!warn_is_numeric_type(sleaf->type)) {
5031 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005032 }
5033 }
5034 }
5035 }
5036 set_scnode_clear_ctx(set);
5037 return rc;
5038 }
5039
5040 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005041
5042 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005043 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 +02005044 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005045 } else if (!args[0]->used) {
5046 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005047 }
5048
Michal Vasko5c4e5892019-11-14 12:31:38 +01005049 set_init(&set_item, set);
5050
Michal Vasko03ff5a72019-09-11 13:49:33 +02005051 set_item.type = LYXP_SET_NODE_SET;
5052 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5053 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5054
5055 set_item.used = 1;
5056 set_item.size = 1;
5057
5058 for (i = 0; i < args[0]->used; ++i) {
5059 set_item.val.nodes[0] = args[0]->val.nodes[i];
5060
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005061 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005062 LY_CHECK_RET(rc);
5063 num = cast_string_to_number(str);
5064 free(str);
5065 set->val.num += num;
5066 }
5067
5068 free(set_item.val.nodes);
5069
5070 return LY_SUCCESS;
5071}
5072
5073/**
5074 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5075 * with the text content of the nodes in the context.
5076 *
5077 * @param[in] args Array of arguments.
5078 * @param[in] arg_count Count of elements in @p args.
5079 * @param[in,out] set Context and result set at the same time.
5080 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005081 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005082 */
5083static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005084xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005085{
5086 uint32_t i;
5087
5088 if (options & LYXP_SCNODE_ALL) {
5089 set_scnode_clear_ctx(set);
5090 return LY_SUCCESS;
5091 }
5092
Michal Vasko03ff5a72019-09-11 13:49:33 +02005093 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005094 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 +02005095 return LY_EVALID;
5096 }
5097
Michal Vaskod989ba02020-08-24 10:59:24 +02005098 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005099 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005100 case LYXP_NODE_NONE:
5101 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005102 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005103 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5104 set->val.nodes[i].type = LYXP_NODE_TEXT;
5105 ++i;
5106 break;
5107 }
Radek Krejci0f969882020-08-21 16:56:47 +02005108 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005109 case LYXP_NODE_ROOT:
5110 case LYXP_NODE_ROOT_CONFIG:
5111 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005112 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005113 set_remove_node(set, i);
5114 break;
5115 }
5116 }
5117
5118 return LY_SUCCESS;
5119}
5120
5121/**
5122 * @brief Execute the XPath translate(string, string, string) function.
5123 * Returns LYXP_SET_STRING with the first argument with the characters
5124 * from the second argument replaced by those on the corresponding
5125 * positions in the third argument.
5126 *
5127 * @param[in] args Array of arguments.
5128 * @param[in] arg_count Count of elements in @p args.
5129 * @param[in,out] set Context and result set at the same time.
5130 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005131 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005132 */
5133static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005134xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005135{
5136 uint16_t i, j, new_used;
5137 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005138 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005139 struct lysc_node_leaf *sleaf;
5140 LY_ERR rc = LY_SUCCESS;
5141
5142 if (options & LYXP_SCNODE_ALL) {
5143 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5144 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5145 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005146 } else if (!warn_is_string_type(sleaf->type)) {
5147 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005148 }
5149 }
5150
5151 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5152 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5153 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005154 } else if (!warn_is_string_type(sleaf->type)) {
5155 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005156 }
5157 }
5158
5159 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5160 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5161 LOGWRN(set->ctx, "Argument #3 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005162 } else if (!warn_is_string_type(sleaf->type)) {
5163 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005164 }
5165 }
5166 set_scnode_clear_ctx(set);
5167 return rc;
5168 }
5169
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005170 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005171 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005172 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005173 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005174 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005175 LY_CHECK_RET(rc);
5176
5177 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5178 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5179 new_used = 0;
5180
5181 have_removed = 0;
5182 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005183 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005184
5185 for (j = 0; args[1]->val.str[j]; ++j) {
5186 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5187 /* removing this char */
5188 if (j >= strlen(args[2]->val.str)) {
5189 have_removed = 1;
5190 found = 1;
5191 break;
5192 }
5193 /* replacing this char */
5194 new[new_used] = args[2]->val.str[j];
5195 ++new_used;
5196 found = 1;
5197 break;
5198 }
5199 }
5200
5201 /* copying this char */
5202 if (!found) {
5203 new[new_used] = args[0]->val.str[i];
5204 ++new_used;
5205 }
5206 }
5207
5208 if (have_removed) {
5209 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5210 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5211 }
5212 new[new_used] = '\0';
5213
Michal Vaskod3678892020-05-21 10:06:58 +02005214 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005215 set->type = LYXP_SET_STRING;
5216 set->val.str = new;
5217
5218 return LY_SUCCESS;
5219}
5220
5221/**
5222 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5223 * with true value.
5224 *
5225 * @param[in] args Array of arguments.
5226 * @param[in] arg_count Count of elements in @p args.
5227 * @param[in,out] set Context and result set at the same time.
5228 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005229 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005230 */
5231static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005232xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005233{
5234 if (options & LYXP_SCNODE_ALL) {
5235 set_scnode_clear_ctx(set);
5236 return LY_SUCCESS;
5237 }
5238
5239 set_fill_boolean(set, 1);
5240 return LY_SUCCESS;
5241}
5242
5243/*
5244 * moveto functions
5245 *
5246 * They and only they actually change the context (set).
5247 */
5248
5249/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005250 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005251 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005252 * XPath @p set is expected to be a (sc)node set!
5253 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005254 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5255 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005256 * @param[in] set Set with general XPath context.
5257 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005258 * @param[out] moveto_mod Expected module of a matching node.
5259 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005260 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005261static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005262moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5263 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005264{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005265 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005266 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005267 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005268
Michal Vasko2104e9f2020-03-06 08:23:25 +01005269 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5270
Michal Vasko6346ece2019-09-24 13:12:53 +02005271 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5272 /* specific module */
5273 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005274 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005275
Michal Vasko004d3152020-06-11 19:59:22 +02005276 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005277 if (!mod || !mod->implemented) {
Michal Vasko2104e9f2020-03-06 08:23:25 +01005278 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005279 LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko2104e9f2020-03-06 08:23:25 +01005280 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005281 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko2104e9f2020-03-06 08:23:25 +01005282 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005283 return LY_EVALID;
5284 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005285
Michal Vasko6346ece2019-09-24 13:12:53 +02005286 *qname += pref_len + 1;
5287 *qname_len -= pref_len + 1;
5288 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5289 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005290 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005291 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005292 switch (set->format) {
5293 case LY_PREF_SCHEMA:
5294 case LY_PREF_SCHEMA_RESOLVED:
5295 /* current module */
5296 mod = set->cur_mod;
5297 break;
5298 case LY_PREF_JSON:
5299 /* inherit parent (context node) module */
5300 if (ctx_scnode) {
5301 mod = ctx_scnode->module;
5302 } else {
5303 mod = NULL;
5304 }
5305 break;
5306 case LY_PREF_XML:
5307 /* not defined */
5308 LOGINT_RET(set->ctx);
5309 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005310 }
5311
Michal Vasko6346ece2019-09-24 13:12:53 +02005312 *moveto_mod = mod;
5313 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005314}
5315
5316/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005317 * @brief Move context @p set to the root. Handles absolute path.
5318 * Result is LYXP_SET_NODE_SET.
5319 *
5320 * @param[in,out] set Set to use.
5321 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005322 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005323 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005324static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005325moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005326{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005327 if (!set) {
Michal Vaskob0099a92020-08-31 14:55:23 +02005328 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005329 }
5330
5331 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005332 set_scnode_clear_ctx(set);
Michal Vaskob0099a92020-08-31 14:55:23 +02005333 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005334 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005335 set->type = LYXP_SET_NODE_SET;
5336 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005337 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005338 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005339
5340 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005341}
5342
5343/**
Michal Vaskoa1424542019-11-14 16:08:52 +01005344 * @brief Check whether a node has some unresolved "when".
5345 *
5346 * @param[in] node Node to check.
5347 * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when")
5348 */
5349static LY_ERR
5350moveto_when_check(const struct lyd_node *node)
5351{
5352 const struct lysc_node *schema;
5353
5354 if (!node) {
5355 return LY_SUCCESS;
5356 }
5357
5358 schema = node->schema;
5359 do {
5360 if (schema->when && !(node->flags & LYD_WHEN_TRUE)) {
5361 return LY_EINCOMPLETE;
5362 }
5363 schema = schema->parent;
5364 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
5365
5366 return LY_SUCCESS;
5367}
5368
5369/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005370 * @brief Check @p node as a part of NameTest processing.
5371 *
5372 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005373 * @param[in] ctx_node Context node.
5374 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005375 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005376 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005377 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5378 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005379 */
5380static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005381moveto_node_check(const struct lyd_node *node, const struct lyd_node *ctx_node, const struct lyxp_set *set,
Radek Krejci0f969882020-08-21 16:56:47 +02005382 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005383{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005384 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5385 switch (set->format) {
5386 case LY_PREF_SCHEMA:
5387 case LY_PREF_SCHEMA_RESOLVED:
5388 /* use current module */
5389 moveto_mod = set->cur_mod;
5390 break;
5391 case LY_PREF_JSON:
5392 /* inherit module of the context node, if any */
5393 if (ctx_node) {
5394 moveto_mod = ctx_node->schema->module;
5395 }
5396 break;
5397 case LY_PREF_XML:
5398 /* not defined */
5399 LOGINT(set->ctx);
5400 return LY_EINVAL;
5401 }
5402 }
5403
Michal Vasko03ff5a72019-09-11 13:49:33 +02005404 /* module check */
5405 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005406 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005407 }
5408
Michal Vasko5c4e5892019-11-14 12:31:38 +01005409 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005410 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005411 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005412 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5413 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005414 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005415 }
5416
5417 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005418 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005419 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005420 }
5421
Michal Vaskoa1424542019-11-14 16:08:52 +01005422 /* when check */
5423 if (moveto_when_check(node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005424 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005425 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005426
5427 /* match */
5428 return LY_SUCCESS;
5429}
5430
5431/**
5432 * @brief Check @p node as a part of schema NameTest processing.
5433 *
5434 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005435 * @param[in] ctx_scnode Context node.
5436 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005437 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005438 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005439 * @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 +02005440 */
5441static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005442moveto_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 +02005443 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005444{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005445 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5446 switch (set->format) {
5447 case LY_PREF_SCHEMA:
5448 case LY_PREF_SCHEMA_RESOLVED:
5449 /* use current module */
5450 moveto_mod = set->cur_mod;
5451 break;
5452 case LY_PREF_JSON:
5453 /* inherit module of the context node, if any */
5454 if (ctx_scnode) {
5455 moveto_mod = ctx_scnode->module;
5456 }
5457 break;
5458 case LY_PREF_XML:
5459 /* not defined */
5460 LOGINT(set->ctx);
5461 return LY_EINVAL;
5462 }
5463 }
5464
Michal Vasko03ff5a72019-09-11 13:49:33 +02005465 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005466 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005467 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005468 }
5469
5470 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005471 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005472 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005473 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005474 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005475 }
5476
5477 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005478 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005479 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005480 }
5481
5482 /* match */
5483 return LY_SUCCESS;
5484}
5485
5486/**
Michal Vaskod3678892020-05-21 10:06:58 +02005487 * @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 +02005488 *
5489 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005490 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005491 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005492 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5493 */
5494static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005495moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005496{
Michal Vaskof03ed032020-03-04 13:31:44 +01005497 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005498 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005499 LY_ERR rc;
5500
Michal Vaskod3678892020-05-21 10:06:58 +02005501 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005502 return LY_SUCCESS;
5503 }
5504
5505 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005506 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 +02005507 return LY_EVALID;
5508 }
5509
Michal Vasko03ff5a72019-09-11 13:49:33 +02005510 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005511 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005512
5513 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 +01005514 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005515
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005516 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005517 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005518 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005519 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005520 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005521 ctx_node = set->val.nodes[i].node;
5522 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005523 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005524
Michal Vaskod3678892020-05-21 10:06:58 +02005525 for (sub = siblings; sub; sub = sub->next) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005526 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02005527 if (rc == LY_SUCCESS) {
5528 if (!replaced) {
5529 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5530 replaced = 1;
5531 } else {
5532 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005533 }
Michal Vaskod3678892020-05-21 10:06:58 +02005534 ++i;
5535 } else if (rc == LY_EINCOMPLETE) {
5536 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005537 }
5538 }
5539
5540 if (!replaced) {
5541 /* no match */
5542 set_remove_node(set, i);
5543 }
5544 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005545
5546 return LY_SUCCESS;
5547}
5548
5549/**
Michal Vaskod3678892020-05-21 10:06:58 +02005550 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5551 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005552 *
5553 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005554 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005555 * @param[in] predicates If @p scnode is ::LYS_LIST or ::LYS_LEAFLIST, the predicates specifying a single instance.
Michal Vaskod3678892020-05-21 10:06:58 +02005556 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5557 */
5558static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02005559moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates)
Michal Vaskod3678892020-05-21 10:06:58 +02005560{
Michal Vasko004d3152020-06-11 19:59:22 +02005561 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005562 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005563 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005564 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005565
Michal Vasko004d3152020-06-11 19:59:22 +02005566 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005567
5568 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005569 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005570 }
5571
5572 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005573 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 +02005574 ret = LY_EVALID;
5575 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005576 }
5577
5578 /* context check for all the nodes since we have the schema node */
5579 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5580 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005581 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005582 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5583 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005584 lyxp_set_free_content(set);
5585 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005586 }
5587
5588 /* create specific data instance if needed */
5589 if (scnode->nodetype == LYS_LIST) {
5590 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5591 } else if (scnode->nodetype == LYS_LEAFLIST) {
5592 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005593 }
5594
5595 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005596 siblings = NULL;
5597
5598 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5599 assert(!set->val.nodes[i].node);
5600
5601 /* search in all the trees */
5602 siblings = set->tree;
5603 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5604 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005605 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005606 }
5607
5608 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005609 if (inst) {
5610 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005611 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005612 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005613 }
5614
5615 /* when check */
5616 if (sub && moveto_when_check(sub)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005617 ret = LY_EINCOMPLETE;
5618 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005619 }
5620
5621 if (sub) {
5622 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005623 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005624 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005625 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005626 /* no match */
5627 set_remove_node(set, i);
5628 }
5629 }
5630
Michal Vasko004d3152020-06-11 19:59:22 +02005631cleanup:
5632 lyd_free_tree(inst);
5633 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005634}
5635
5636/**
5637 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5638 *
5639 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005640 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005641 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005642 * @param[in] options XPath options.
5643 * @return LY_ERR
5644 */
5645static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005646moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005647{
Radek Krejci857189e2020-09-01 13:26:36 +02005648 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005649 uint32_t getnext_opts;
5650 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005651 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005652 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005653 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005654
Michal Vaskod3678892020-05-21 10:06:58 +02005655 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005656 return LY_SUCCESS;
5657 }
5658
5659 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005660 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 +02005661 return LY_EVALID;
5662 }
5663
Michal Vaskocafad9d2019-11-07 15:20:03 +01005664 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005665 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005666 if (options & LYXP_SCNODE_OUTPUT) {
5667 getnext_opts |= LYS_GETNEXT_OUTPUT;
5668 }
5669
Michal Vasko03ff5a72019-09-11 13:49:33 +02005670 orig_used = set->used;
5671 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005672 uint32_t idx;
5673
Michal Vasko03ff5a72019-09-11 13:49:33 +02005674 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005675 if (set->val.scnodes[i].in_ctx != -2) {
5676 continue;
5677 }
5678
5679 /* remember context node */
5680 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005681 } else {
5682 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005683 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005684
5685 start_parent = set->val.scnodes[i].scnode;
5686
5687 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 +02005688 /* 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 +02005689 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005690 mod_idx = 0;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005691 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005692 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005693 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005694 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005695 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005696 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5697
Michal Vasko03ff5a72019-09-11 13:49:33 +02005698 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005699 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005700 set->val.scnodes[idx].in_ctx = 2;
5701 temp_ctx = 1;
5702 }
5703 }
5704 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005705 }
5706
Michal Vasko519fd602020-05-26 12:17:39 +02005707 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5708 iter = NULL;
5709 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005710 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005711 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5712
5713 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005714 set->val.scnodes[idx].in_ctx = 2;
5715 temp_ctx = 1;
5716 }
5717 }
5718 }
5719 }
5720 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005721
5722 /* correct temporary in_ctx values */
5723 if (temp_ctx) {
5724 for (i = 0; i < orig_used; ++i) {
5725 if (set->val.scnodes[i].in_ctx == 2) {
5726 set->val.scnodes[i].in_ctx = 1;
5727 }
5728 }
5729 }
5730
5731 return LY_SUCCESS;
5732}
5733
5734/**
Michal Vaskod3678892020-05-21 10:06:58 +02005735 * @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 +02005736 * Context position aware.
5737 *
5738 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005739 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005740 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005741 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5742 */
5743static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005744moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005745{
5746 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005747 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005748 struct lyxp_set ret_set;
5749 LY_ERR rc;
5750
Michal Vaskod3678892020-05-21 10:06:58 +02005751 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005752 return LY_SUCCESS;
5753 }
5754
5755 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005756 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 +02005757 return LY_EVALID;
5758 }
5759
Michal Vasko9f96a052020-03-10 09:41:45 +01005760 /* replace the original nodes (and throws away all text and meta nodes, root is replaced by a child) */
Michal Vaskod3678892020-05-21 10:06:58 +02005761 rc = moveto_node(set, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005762 LY_CHECK_RET(rc);
5763
Michal Vasko6346ece2019-09-24 13:12:53 +02005764 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005765 set_init(&ret_set, set);
5766 for (i = 0; i < set->used; ++i) {
5767
5768 /* TREE DFS */
5769 start = set->val.nodes[i].node;
5770 for (elem = next = start; elem; elem = next) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005771 rc = moveto_node_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005772 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005773 /* add matching node into result set */
5774 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5775 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5776 /* the node is a duplicate, we'll process it later in the set */
5777 goto skip_children;
5778 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005779 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005780 return rc;
5781 } else if (rc == LY_EINVAL) {
5782 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005783 }
5784
5785 /* TREE DFS NEXT ELEM */
5786 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005787 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005788 if (!next) {
5789skip_children:
5790 /* no children, so try siblings, but only if it's not the start,
5791 * that is considered to be the root and it's siblings are not traversed */
5792 if (elem != start) {
5793 next = elem->next;
5794 } else {
5795 break;
5796 }
5797 }
5798 while (!next) {
5799 /* no siblings, go back through the parents */
5800 if ((struct lyd_node *)elem->parent == start) {
5801 /* we are done, no next element to process */
5802 break;
5803 }
5804 /* parent is already processed, go to its sibling */
5805 elem = (struct lyd_node *)elem->parent;
5806 next = elem->next;
5807 }
5808 }
5809 }
5810
5811 /* make the temporary set the current one */
5812 ret_set.ctx_pos = set->ctx_pos;
5813 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005814 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005815 memcpy(set, &ret_set, sizeof *set);
5816
5817 return LY_SUCCESS;
5818}
5819
5820/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005821 * @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 +02005822 *
5823 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005824 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005825 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005826 * @param[in] options XPath options.
5827 * @return LY_ERR
5828 */
5829static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005830moveto_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 +02005831{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005832 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005833 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005834 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005835
Michal Vaskod3678892020-05-21 10:06:58 +02005836 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005837 return LY_SUCCESS;
5838 }
5839
5840 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005841 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 +02005842 return LY_EVALID;
5843 }
5844
Michal Vasko03ff5a72019-09-11 13:49:33 +02005845 orig_used = set->used;
5846 for (i = 0; i < orig_used; ++i) {
5847 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005848 if (set->val.scnodes[i].in_ctx != -2) {
5849 continue;
5850 }
5851
5852 /* remember context node */
5853 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005854 } else {
5855 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005856 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005857
5858 /* TREE DFS */
5859 start = set->val.scnodes[i].scnode;
5860 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005861 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5862 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005863 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005864 }
5865
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005866 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005867 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005868 uint32_t idx;
5869
5870 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005871 set->val.scnodes[idx].in_ctx = 1;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005872 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005873 /* we will process it later in the set */
5874 goto skip_children;
5875 }
5876 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005877 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005878 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005879 } else if (rc == LY_EINVAL) {
5880 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005881 }
5882
5883next_iter:
5884 /* TREE DFS NEXT ELEM */
5885 /* select element for the next run - children first */
5886 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005887 if (!next) {
5888skip_children:
5889 /* no children, so try siblings, but only if it's not the start,
5890 * that is considered to be the root and it's siblings are not traversed */
5891 if (elem != start) {
5892 next = elem->next;
5893 } else {
5894 break;
5895 }
5896 }
5897 while (!next) {
5898 /* no siblings, go back through the parents */
5899 if (elem->parent == start) {
5900 /* we are done, no next element to process */
5901 break;
5902 }
5903 /* parent is already processed, go to its sibling */
5904 elem = elem->parent;
5905 next = elem->next;
5906 }
5907 }
5908 }
5909
5910 return LY_SUCCESS;
5911}
5912
5913/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005914 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005915 * Indirectly context position aware.
5916 *
5917 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005918 * @param[in] mod Matching metadata module, NULL for any.
5919 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005920 * @return LY_ERR
5921 */
5922static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005923moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005924{
Michal Vasko9f96a052020-03-10 09:41:45 +01005925 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005926
Michal Vaskod3678892020-05-21 10:06:58 +02005927 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005928 return LY_SUCCESS;
5929 }
5930
5931 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005932 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 +02005933 return LY_EVALID;
5934 }
5935
Radek Krejci1deb5be2020-08-26 16:43:36 +02005936 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005937 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005938
5939 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5940 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005941 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005942 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005943
5944 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005945 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005946 continue;
5947 }
5948
Michal Vaskod3678892020-05-21 10:06:58 +02005949 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005950 /* match */
5951 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005952 set->val.meta[i].meta = sub;
5953 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005954 /* pos does not change */
5955 replaced = 1;
5956 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005957 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 +02005958 }
5959 ++i;
5960 }
5961 }
5962 }
5963
5964 if (!replaced) {
5965 /* no match */
5966 set_remove_node(set, i);
5967 }
5968 }
5969
5970 return LY_SUCCESS;
5971}
5972
5973/**
5974 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005975 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005976 *
5977 * @param[in,out] set1 Set to use for the result.
5978 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005979 * @return LY_ERR
5980 */
5981static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005982moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005983{
5984 LY_ERR rc;
5985
Michal Vaskod3678892020-05-21 10:06:58 +02005986 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005987 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 +02005988 return LY_EVALID;
5989 }
5990
5991 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005992 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005993 return LY_SUCCESS;
5994 }
5995
Michal Vaskod3678892020-05-21 10:06:58 +02005996 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005997 memcpy(set1, set2, sizeof *set1);
5998 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005999 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006000 return LY_SUCCESS;
6001 }
6002
6003 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006004 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006005
6006 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006007 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006008 LY_CHECK_RET(rc);
6009
6010 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006011 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006012
6013 return LY_SUCCESS;
6014}
6015
6016/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006017 * @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 +02006018 * Context position aware.
6019 *
6020 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006021 * @param[in] mod Matching metadata module, NULL for any.
6022 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006023 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6024 */
6025static int
Michal Vaskod3678892020-05-21 10:06:58 +02006026moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006027{
Michal Vasko9f96a052020-03-10 09:41:45 +01006028 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006029 struct lyxp_set *set_all_desc = NULL;
6030 LY_ERR rc;
6031
Michal Vaskod3678892020-05-21 10:06:58 +02006032 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006033 return LY_SUCCESS;
6034 }
6035
6036 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006037 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 +02006038 return LY_EVALID;
6039 }
6040
Michal Vasko03ff5a72019-09-11 13:49:33 +02006041 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6042 * but it likely won't be used much, so it's a waste of time */
6043 /* copy the context */
6044 set_all_desc = set_copy(set);
6045 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskod3678892020-05-21 10:06:58 +02006046 rc = moveto_node_alldesc(set_all_desc, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006047 if (rc != LY_SUCCESS) {
6048 lyxp_set_free(set_all_desc);
6049 return rc;
6050 }
6051 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006052 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006053 if (rc != LY_SUCCESS) {
6054 lyxp_set_free(set_all_desc);
6055 return rc;
6056 }
6057 lyxp_set_free(set_all_desc);
6058
Radek Krejci1deb5be2020-08-26 16:43:36 +02006059 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006060 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006061
6062 /* only attributes of an elem can be in the result, skip all the rest,
6063 * we have all attributes qualified in lyd tree */
6064 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006065 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006066 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006067 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006068 continue;
6069 }
6070
Michal Vaskod3678892020-05-21 10:06:58 +02006071 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006072 /* match */
6073 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006074 set->val.meta[i].meta = sub;
6075 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006076 /* pos does not change */
6077 replaced = 1;
6078 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006079 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 +02006080 }
6081 ++i;
6082 }
6083 }
6084 }
6085
6086 if (!replaced) {
6087 /* no match */
6088 set_remove_node(set, i);
6089 }
6090 }
6091
6092 return LY_SUCCESS;
6093}
6094
6095/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006096 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6097 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006098 *
6099 * @param[in] parent Current parent.
6100 * @param[in] parent_pos Position of @p parent.
6101 * @param[in] parent_type Node type of @p parent.
6102 * @param[in,out] to_set Set to use.
6103 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006104 * @param[in] options XPath options.
6105 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6106 */
6107static LY_ERR
6108moveto_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 +02006109 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006110{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006111 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006112 LY_ERR rc;
6113
6114 switch (parent_type) {
6115 case LYXP_NODE_ROOT:
6116 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006117 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006118
Michal Vasko61ac2f62020-05-25 12:39:51 +02006119 /* add all top-level nodes as elements */
6120 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006121 break;
6122 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006123 /* add just the text node of this term element node */
6124 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006125 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6126 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6127 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006128 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006129 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006130
6131 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006132 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006133 break;
6134 default:
6135 LOGINT_RET(parent->schema->module->ctx);
6136 }
6137
Michal Vasko61ac2f62020-05-25 12:39:51 +02006138 /* add all top-level nodes as elements */
6139 LY_LIST_FOR(first, iter) {
6140 /* context check */
6141 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6142 continue;
6143 }
6144
6145 /* when check */
6146 if (moveto_when_check(iter)) {
6147 return LY_EINCOMPLETE;
6148 }
6149
6150 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6151 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6152
6153 /* also add all the children of this node, recursively */
6154 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6155 LY_CHECK_RET(rc);
6156 }
6157 }
6158
Michal Vasko03ff5a72019-09-11 13:49:33 +02006159 return LY_SUCCESS;
6160}
6161
6162/**
6163 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6164 * (or LYXP_SET_EMPTY). Context position aware.
6165 *
6166 * @param[in,out] set Set to use.
6167 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6168 * @param[in] options XPath options.
6169 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6170 */
6171static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006172moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006173{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006174 struct lyxp_set ret_set;
6175 LY_ERR rc;
6176
Michal Vaskod3678892020-05-21 10:06:58 +02006177 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006178 return LY_SUCCESS;
6179 }
6180
6181 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006182 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 +02006183 return LY_EVALID;
6184 }
6185
6186 /* nothing to do */
6187 if (!all_desc) {
6188 return LY_SUCCESS;
6189 }
6190
Michal Vasko03ff5a72019-09-11 13:49:33 +02006191 /* add all the children, they get added recursively */
6192 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006193 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006194 /* copy the current node to tmp */
6195 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6196
6197 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006198 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006199 continue;
6200 }
6201
Michal Vasko03ff5a72019-09-11 13:49:33 +02006202 /* add all the children */
6203 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 +02006204 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006205 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006206 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006207 return rc;
6208 }
6209 }
6210
6211 /* use the temporary set as the current one */
6212 ret_set.ctx_pos = set->ctx_pos;
6213 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006214 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006215 memcpy(set, &ret_set, sizeof *set);
6216
6217 return LY_SUCCESS;
6218}
6219
6220/**
6221 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6222 * (or LYXP_SET_EMPTY).
6223 *
6224 * @param[in,out] set Set to use.
6225 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6226 * @param[in] options XPath options.
6227 * @return LY_ERR
6228 */
6229static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006230moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006231{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006232 uint32_t getnext_opts;
6233 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006234 const struct lysc_node *iter, *start_parent;
6235 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006236
Michal Vaskod3678892020-05-21 10:06:58 +02006237 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006238 return LY_SUCCESS;
6239 }
6240
6241 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006242 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 +02006243 return LY_EVALID;
6244 }
6245
6246 /* nothing to do */
6247 if (!all_desc) {
6248 return LY_SUCCESS;
6249 }
6250
Michal Vasko519fd602020-05-26 12:17:39 +02006251 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006252 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006253 if (options & LYXP_SCNODE_OUTPUT) {
6254 getnext_opts |= LYS_GETNEXT_OUTPUT;
6255 }
6256
6257 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006258 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006259 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006260 if (set->val.scnodes[i].in_ctx != -2) {
6261 continue;
6262 }
6263
Michal Vasko519fd602020-05-26 12:17:39 +02006264 /* remember context node */
6265 set->val.scnodes[i].in_ctx = -1;
6266 } else {
6267 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006268 }
6269
Michal Vasko519fd602020-05-26 12:17:39 +02006270 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006271
Michal Vasko519fd602020-05-26 12:17:39 +02006272 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6273 /* it can actually be in any module, it's all <running> */
6274 mod_idx = 0;
6275 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6276 iter = NULL;
6277 /* module may not be implemented */
6278 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6279 /* context check */
6280 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6281 continue;
6282 }
6283
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006284 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006285 /* throw away the insert index, we want to consider that node again, recursively */
6286 }
6287 }
6288
6289 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6290 iter = NULL;
6291 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006292 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006293 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006294 continue;
6295 }
6296
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006297 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006298 }
6299 }
6300 }
6301
6302 return LY_SUCCESS;
6303}
6304
6305/**
6306 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6307 * (or LYXP_SET_EMPTY). Context position aware.
6308 *
6309 * @param[in] set Set to use.
6310 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6311 * @param[in] options XPath options.
6312 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6313 */
6314static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006315moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006316{
6317 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006318 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006319 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006320
Michal Vaskod3678892020-05-21 10:06:58 +02006321 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006322 return LY_SUCCESS;
6323 }
6324
6325 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006326 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 +02006327 return LY_EVALID;
6328 }
6329
6330 if (all_desc) {
6331 /* <path>//.. == <path>//./.. */
6332 rc = moveto_self(set, 1, options);
6333 LY_CHECK_RET(rc);
6334 }
6335
Radek Krejci1deb5be2020-08-26 16:43:36 +02006336 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006337 node = set->val.nodes[i].node;
6338
6339 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6340 new_node = (struct lyd_node *)node->parent;
6341 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6342 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006343 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6344 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006345 if (!new_node) {
6346 LOGINT_RET(set->ctx);
6347 }
6348 } else {
6349 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006350 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006351 continue;
6352 }
6353
Michal Vaskoa1424542019-11-14 16:08:52 +01006354 /* when check */
6355 if (moveto_when_check(new_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006356 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006357 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006358
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006359 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006360 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006361 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006362
Michal Vasko03ff5a72019-09-11 13:49:33 +02006363 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006364 /* 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 +02006365 new_type = LYXP_NODE_ELEM;
6366 }
6367
Michal Vasko03ff5a72019-09-11 13:49:33 +02006368 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006369 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006370 } else {
6371 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006372 }
6373 }
6374
Michal Vasko2caefc12019-11-14 16:07:56 +01006375 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006376 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006377
6378 return LY_SUCCESS;
6379}
6380
6381/**
6382 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6383 * (or LYXP_SET_EMPTY).
6384 *
6385 * @param[in] set Set to use.
6386 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6387 * @param[in] options XPath options.
6388 * @return LY_ERR
6389 */
6390static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006391moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006392{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006393 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006394 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006395 const struct lysc_node *node, *new_node;
6396 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006397
Michal Vaskod3678892020-05-21 10:06:58 +02006398 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006399 return LY_SUCCESS;
6400 }
6401
6402 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006403 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 +02006404 return LY_EVALID;
6405 }
6406
6407 if (all_desc) {
6408 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006409 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006410 }
6411
Michal Vasko03ff5a72019-09-11 13:49:33 +02006412 orig_used = set->used;
6413 for (i = 0; i < orig_used; ++i) {
6414 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006415 if (set->val.scnodes[i].in_ctx != -2) {
6416 continue;
6417 }
6418
6419 /* remember context node */
6420 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01006421 } else {
6422 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006423 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006424
6425 node = set->val.scnodes[i].scnode;
6426
6427 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006428 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006429 } else {
6430 /* root does not have a parent */
6431 continue;
6432 }
6433
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006434 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006435 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006436 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006437
Michal Vasko03ff5a72019-09-11 13:49:33 +02006438 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006439 /* 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 +02006440 new_type = LYXP_NODE_ELEM;
6441 }
6442
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006443 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6444 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006445 set->val.scnodes[idx].in_ctx = 2;
6446 temp_ctx = 1;
6447 }
6448 }
6449
6450 if (temp_ctx) {
6451 for (i = 0; i < orig_used; ++i) {
6452 if (set->val.scnodes[i].in_ctx == 2) {
6453 set->val.scnodes[i].in_ctx = 1;
6454 }
6455 }
6456 }
6457
6458 return LY_SUCCESS;
6459}
6460
6461/**
6462 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6463 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6464 *
6465 * @param[in,out] set1 Set to use for the result.
6466 * @param[in] set2 Set acting as the second operand for @p op.
6467 * @param[in] op Comparison operator to process.
6468 * @param[in] options XPath options.
6469 * @return LY_ERR
6470 */
6471static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006472moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006473{
6474 /*
6475 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6476 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6477 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6478 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6479 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6480 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6481 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6482 *
6483 * '=' or '!='
6484 * BOOLEAN + BOOLEAN
6485 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6486 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6487 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6488 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6489 * NUMBER + NUMBER
6490 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6491 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6492 * STRING + STRING
6493 *
6494 * '<=', '<', '>=', '>'
6495 * NUMBER + NUMBER
6496 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6497 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6498 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6499 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6500 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6501 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6502 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6503 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6504 */
6505 struct lyxp_set iter1, iter2;
6506 int result;
6507 int64_t i;
6508 LY_ERR rc;
6509
Michal Vasko004d3152020-06-11 19:59:22 +02006510 memset(&iter1, 0, sizeof iter1);
6511 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006512
6513 /* iterative evaluation with node-sets */
6514 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6515 if (set1->type == LYXP_SET_NODE_SET) {
6516 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006517 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006518 switch (set2->type) {
6519 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006520 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006521 break;
6522 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006523 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006524 break;
6525 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006526 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006527 break;
6528 }
6529 LY_CHECK_RET(rc);
6530
Michal Vasko4c7763f2020-07-27 17:40:37 +02006531 /* canonize set2 */
6532 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6533
6534 /* compare recursively */
6535 rc = moveto_op_comp(&iter1, &iter2, op, options);
6536 lyxp_set_free_content(&iter2);
6537 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006538
6539 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006540 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006541 set_fill_boolean(set1, 1);
6542 return LY_SUCCESS;
6543 }
6544 }
6545 } else {
6546 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006547 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006548 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006549 case LYXP_SET_NUMBER:
6550 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6551 break;
6552 case LYXP_SET_BOOLEAN:
6553 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6554 break;
6555 default:
6556 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6557 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006558 }
6559 LY_CHECK_RET(rc);
6560
Michal Vasko4c7763f2020-07-27 17:40:37 +02006561 /* canonize set1 */
6562 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 +02006563
Michal Vasko4c7763f2020-07-27 17:40:37 +02006564 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006565 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006566 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006567 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006568
6569 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006570 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006571 set_fill_boolean(set1, 1);
6572 return LY_SUCCESS;
6573 }
6574 }
6575 }
6576
6577 /* false for all nodes */
6578 set_fill_boolean(set1, 0);
6579 return LY_SUCCESS;
6580 }
6581
6582 /* first convert properly */
6583 if ((op[0] == '=') || (op[0] == '!')) {
6584 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006585 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6586 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006587 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006588 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006589 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006590 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006591 LY_CHECK_RET(rc);
6592 } /* else we have 2 strings */
6593 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006594 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006595 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006596 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006597 LY_CHECK_RET(rc);
6598 }
6599
6600 assert(set1->type == set2->type);
6601
6602 /* compute result */
6603 if (op[0] == '=') {
6604 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006605 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006606 } else if (set1->type == LYXP_SET_NUMBER) {
6607 result = (set1->val.num == set2->val.num);
6608 } else {
6609 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006610 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006611 }
6612 } else if (op[0] == '!') {
6613 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006614 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006615 } else if (set1->type == LYXP_SET_NUMBER) {
6616 result = (set1->val.num != set2->val.num);
6617 } else {
6618 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006619 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006620 }
6621 } else {
6622 assert(set1->type == LYXP_SET_NUMBER);
6623 if (op[0] == '<') {
6624 if (op[1] == '=') {
6625 result = (set1->val.num <= set2->val.num);
6626 } else {
6627 result = (set1->val.num < set2->val.num);
6628 }
6629 } else {
6630 if (op[1] == '=') {
6631 result = (set1->val.num >= set2->val.num);
6632 } else {
6633 result = (set1->val.num > set2->val.num);
6634 }
6635 }
6636 }
6637
6638 /* assign result */
6639 if (result) {
6640 set_fill_boolean(set1, 1);
6641 } else {
6642 set_fill_boolean(set1, 0);
6643 }
6644
6645 return LY_SUCCESS;
6646}
6647
6648/**
6649 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6650 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6651 *
6652 * @param[in,out] set1 Set to use for the result.
6653 * @param[in] set2 Set acting as the second operand for @p op.
6654 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006655 * @return LY_ERR
6656 */
6657static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006658moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006659{
6660 LY_ERR rc;
6661
6662 /* unary '-' */
6663 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006664 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006665 LY_CHECK_RET(rc);
6666 set1->val.num *= -1;
6667 lyxp_set_free(set2);
6668 return LY_SUCCESS;
6669 }
6670
6671 assert(set1 && set2);
6672
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006673 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006674 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006675 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006676 LY_CHECK_RET(rc);
6677
6678 switch (op[0]) {
6679 /* '+' */
6680 case '+':
6681 set1->val.num += set2->val.num;
6682 break;
6683
6684 /* '-' */
6685 case '-':
6686 set1->val.num -= set2->val.num;
6687 break;
6688
6689 /* '*' */
6690 case '*':
6691 set1->val.num *= set2->val.num;
6692 break;
6693
6694 /* 'div' */
6695 case 'd':
6696 set1->val.num /= set2->val.num;
6697 break;
6698
6699 /* 'mod' */
6700 case 'm':
6701 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6702 break;
6703
6704 default:
6705 LOGINT_RET(set1->ctx);
6706 }
6707
6708 return LY_SUCCESS;
6709}
6710
6711/*
6712 * eval functions
6713 *
6714 * They execute a parsed XPath expression on some data subtree.
6715 */
6716
6717/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006718 * @brief Evaluate Predicate. Logs directly on error.
6719 *
Michal Vaskod3678892020-05-21 10:06:58 +02006720 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006721 *
6722 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006723 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006724 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6725 * @param[in] options XPath options.
6726 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6727 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6728 */
6729static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006730eval_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 +02006731{
6732 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006733 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006734 uint32_t orig_pos, orig_size;
6735 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006736 struct lyxp_set set2;
6737 struct lyd_node *orig_parent;
6738
6739 /* '[' */
6740 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006741 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006742 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006743
6744 if (!set) {
6745only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006746 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006747 LY_CHECK_RET(rc);
6748 } else if (set->type == LYXP_SET_NODE_SET) {
6749 /* 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 +01006750 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006751
6752 /* empty set, nothing to evaluate */
6753 if (!set->used) {
6754 goto only_parse;
6755 }
6756
Michal Vasko004d3152020-06-11 19:59:22 +02006757 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006758 orig_pos = 0;
6759 orig_size = set->used;
6760 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006761 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006762 set_init(&set2, set);
6763 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6764 /* remember the node context position for position() and context size for last(),
6765 * predicates should always be evaluated with respect to the child axis (since we do
6766 * not support explicit axes) so we assign positions based on their parents */
6767 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6768 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6769 orig_pos = 1;
6770 } else {
6771 ++orig_pos;
6772 }
6773
6774 set2.ctx_pos = orig_pos;
6775 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006776 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006777
Michal Vasko004d3152020-06-11 19:59:22 +02006778 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006779 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006780 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006781 return rc;
6782 }
6783
6784 /* number is a position */
6785 if (set2.type == LYXP_SET_NUMBER) {
6786 if ((long long)set2.val.num == orig_pos) {
6787 set2.val.num = 1;
6788 } else {
6789 set2.val.num = 0;
6790 }
6791 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006792 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006793
6794 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006795 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006796 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006797 }
6798 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006799 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006800
6801 } else if (set->type == LYXP_SET_SCNODE_SET) {
6802 for (i = 0; i < set->used; ++i) {
6803 if (set->val.scnodes[i].in_ctx == 1) {
6804 /* there is a currently-valid node */
6805 break;
6806 }
6807 }
6808 /* empty set, nothing to evaluate */
6809 if (i == set->used) {
6810 goto only_parse;
6811 }
6812
Michal Vasko004d3152020-06-11 19:59:22 +02006813 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006814
Michal Vasko03ff5a72019-09-11 13:49:33 +02006815 /* set special in_ctx to all the valid snodes */
6816 pred_in_ctx = set_scnode_new_in_ctx(set);
6817
6818 /* use the valid snodes one-by-one */
6819 for (i = 0; i < set->used; ++i) {
6820 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6821 continue;
6822 }
6823 set->val.scnodes[i].in_ctx = 1;
6824
Michal Vasko004d3152020-06-11 19:59:22 +02006825 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006826
Michal Vasko004d3152020-06-11 19:59:22 +02006827 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006828 LY_CHECK_RET(rc);
6829
6830 set->val.scnodes[i].in_ctx = pred_in_ctx;
6831 }
6832
6833 /* restore the state as it was before the predicate */
6834 for (i = 0; i < set->used; ++i) {
6835 if (set->val.scnodes[i].in_ctx == 1) {
6836 set->val.scnodes[i].in_ctx = 0;
6837 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6838 set->val.scnodes[i].in_ctx = 1;
6839 }
6840 }
6841
6842 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006843 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006844 set_fill_set(&set2, set);
6845
Michal Vasko004d3152020-06-11 19:59:22 +02006846 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006847 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006848 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006849 return rc;
6850 }
6851
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006852 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006853 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006854 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006855 }
Michal Vaskod3678892020-05-21 10:06:58 +02006856 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006857 }
6858
6859 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006860 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006861 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006862 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006863 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006864
6865 return LY_SUCCESS;
6866}
6867
6868/**
Michal Vaskod3678892020-05-21 10:06:58 +02006869 * @brief Evaluate Literal. Logs directly on error.
6870 *
6871 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006872 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006873 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6874 */
6875static void
Michal Vasko40308e72020-10-20 16:38:40 +02006876eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006877{
6878 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006879 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006880 set_fill_string(set, "", 0);
6881 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006882 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 +02006883 }
6884 }
6885 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006886 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006887 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006888}
6889
6890/**
Michal Vasko004d3152020-06-11 19:59:22 +02006891 * @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 +02006892 *
Michal Vasko004d3152020-06-11 19:59:22 +02006893 * @param[in] exp Full parsed XPath expression.
6894 * @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 +02006895 * @param[in] ctx_node Found schema node as the context for the predicate.
6896 * @param[in] cur_mod Current module for the expression.
6897 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006898 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006899 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006900 * @param[out] predicates Parsed predicates.
6901 * @param[out] pred_type Type of @p predicates.
6902 * @return LY_SUCCESS on success,
6903 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006904 */
6905static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006906eval_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 +02006907 const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_PREFIX_FORMAT format, void *prefix_data,
6908 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006909{
Michal Vasko004d3152020-06-11 19:59:22 +02006910 LY_ERR ret = LY_SUCCESS;
6911 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006912 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006913 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006914 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006915 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006916
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006917 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006918
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006919 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006920 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006921 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006922 return LY_EINVAL;
6923 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006924 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 +02006925 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006926
Michal Vasko004d3152020-06-11 19:59:22 +02006927 /* learn where the predicates end */
6928 e_idx = *tok_idx;
6929 while (key_count) {
6930 /* '[' */
6931 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6932 return LY_EINVAL;
6933 }
6934 ++e_idx;
6935
6936 /* ']' */
6937 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6938 ++e_idx;
6939 }
6940 ++e_idx;
6941
6942 /* another presumably key predicate parsed */
6943 --key_count;
6944 }
Michal Vasko004d3152020-06-11 19:59:22 +02006945 } else {
6946 /* learn just where this single predicate ends */
6947 e_idx = *tok_idx;
6948
Michal Vaskod3678892020-05-21 10:06:58 +02006949 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006950 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6951 return LY_EINVAL;
6952 }
6953 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006954
6955 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006956 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6957 ++e_idx;
6958 }
6959 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006960 }
6961
Michal Vasko004d3152020-06-11 19:59:22 +02006962 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6963 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6964
6965 /* turn logging off */
6966 prev_lo = ly_log_options(0);
6967
6968 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006969 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
6970 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02006971
6972 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006973 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
6974 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006975 LY_CHECK_GOTO(ret, cleanup);
6976
6977 /* success, the predicate must include all the needed information for hash-based search */
6978 *tok_idx = e_idx;
6979
6980cleanup:
6981 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006982 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02006983 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006984}
6985
6986/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006987 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
6988 * data node(s) could be found using a single hash-based search.
6989 *
6990 * @param[in] node Next context node to check.
6991 * @param[in] name Expected node name.
6992 * @param[in] name_len Length of @p name.
6993 * @param[in] moveto_mod Expected node module.
6994 * @param[in] root_type XPath root type.
6995 * @param[in] no_prefix Whether the node name was unprefixed.
6996 * @param[in] format Prefix format.
6997 * @param[in,out] found Previously found node, is updated.
6998 * @return LY_SUCCESS on success,
6999 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7000 */
7001static LY_ERR
7002eval_name_test_with_predicate_get_scnode(const struct lyd_node *node, const char *name, uint16_t name_len,
7003 const struct lys_module *moveto_mod, enum lyxp_node_type root_type, ly_bool no_prefix, LY_PREFIX_FORMAT format,
7004 const struct lysc_node **found)
7005{
7006 const struct lysc_node *scnode;
7007 const struct lys_module *mod;
7008 uint32_t idx = 0;
7009
7010continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007011 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007012 if (!node) {
7013 if (no_prefix && (format == LY_PREF_JSON)) {
7014 /* search all modules for a single match */
7015 while ((mod = ly_ctx_get_module_iter(moveto_mod->ctx, &idx))) {
7016 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7017 if (scnode) {
7018 /* we have found a match */
7019 break;
7020 }
7021 }
7022
Michal Vasko7d1d0912020-10-16 08:38:30 +02007023 if (!scnode) {
7024 /* all modules searched */
7025 idx = 0;
7026 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007027 } else {
7028 /* search in top-level */
7029 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7030 }
7031 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
7032 if (no_prefix && (format == LY_PREF_JSON)) {
7033 /* we must adjust the module to inherit the one from the context node */
7034 moveto_mod = node->schema->module;
7035 }
7036
7037 /* search in children, do not repeat the same search */
7038 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007039 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007040
7041 /* additional context check */
7042 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7043 scnode = NULL;
7044 }
7045
7046 if (scnode) {
7047 if (*found) {
7048 /* we found a schema node with the same name but at different level, give up, too complicated
7049 * (more hash-based searches would be required, not supported) */
7050 return LY_ENOT;
7051 } else {
7052 /* remember the found schema node and continue to make sure it can be used */
7053 *found = scnode;
7054 }
7055 }
7056
7057 if (idx) {
7058 /* continue searching all the following models */
7059 goto continue_search;
7060 }
7061
7062 return LY_SUCCESS;
7063}
7064
7065/**
Michal Vaskod3678892020-05-21 10:06:58 +02007066 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7067 *
7068 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7069 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7070 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7071 *
7072 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007073 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007074 * @param[in] attr_axis Whether to search attributes or standard nodes.
7075 * @param[in] all_desc Whether to search all the descendants or children only.
7076 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7077 * @param[in] options XPath options.
7078 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7079 */
7080static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007081eval_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 +02007082 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007083{
Michal Vaskod3678892020-05-21 10:06:58 +02007084 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007085 const char *ncname, *ncname_dict = NULL;
7086 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007087 const struct lys_module *moveto_mod = NULL;
7088 const struct lysc_node *scnode = NULL, *ctx_scnode;
Michal Vasko004d3152020-06-11 19:59:22 +02007089 struct ly_path_predicate *predicates = NULL;
7090 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007091 LY_ERR rc = LY_SUCCESS;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007092 ly_bool no_prefix;
Michal Vaskod3678892020-05-21 10:06:58 +02007093
7094 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007095 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007096 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007097
7098 if (!set) {
7099 goto moveto;
7100 }
7101
Michal Vasko004d3152020-06-11 19:59:22 +02007102 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7103 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007104
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007105 /* get the first schema context node */
7106 if (set->used) {
7107 if (set->type == LYXP_SET_NODE_SET) {
7108 ctx_scnode = set->val.nodes[0].node ? set->val.nodes[0].node->schema : NULL;
7109 } else {
7110 ctx_scnode = set->val.scnodes[0].scnode;
7111 }
7112 } else {
7113 ctx_scnode = NULL;
7114 }
7115
7116 /* parse (and skip) module name, use any context node (if available) just so we get some moveto_mod if there
7117 * is no prefix for ::LY_PREF_JSON */
7118 rc = moveto_resolve_model(&ncname, &ncname_len, set, ctx_scnode, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007119 LY_CHECK_GOTO(rc, cleanup);
7120
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007121 if (ncname_len == exp->tok_len[*tok_idx - 1]) {
7122 /* remember that there is no prefix */
7123 no_prefix = 1;
7124 } else {
7125 no_prefix = 0;
7126 }
7127
Michal Vaskod3678892020-05-21 10:06:58 +02007128 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
7129 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007130 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007131 if (eval_name_test_with_predicate_get_scnode(set->val.nodes[i].node, ncname, ncname_len, moveto_mod,
7132 set->root_type, no_prefix, set->format, &scnode)) {
7133 /* check failed */
7134 scnode = NULL;
7135 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007136 }
7137 }
7138
Michal Vasko004d3152020-06-11 19:59:22 +02007139 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7140 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007141 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7142 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007143 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007144 scnode = NULL;
7145 }
7146 }
7147 }
7148
Michal Vasko004d3152020-06-11 19:59:22 +02007149 if (!scnode && moveto_mod) {
7150 /* we are not able to match based on a schema node and not all the modules match,
7151 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007152 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007153 }
7154
7155moveto:
7156 /* move to the attribute(s), data node(s), or schema node(s) */
7157 if (attr_axis) {
7158 if (set && (options & LYXP_SCNODE_ALL)) {
7159 set_scnode_clear_ctx(set);
7160 } else {
7161 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007162 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007163 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007164 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007165 }
7166 LY_CHECK_GOTO(rc, cleanup);
7167 }
7168 } else {
7169 if (set && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007170 int64_t i;
7171
Michal Vaskod3678892020-05-21 10:06:58 +02007172 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007173 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007174 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007175 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007176 }
7177 LY_CHECK_GOTO(rc, cleanup);
7178
7179 for (i = set->used - 1; i > -1; --i) {
7180 if (set->val.scnodes[i].in_ctx > 0) {
7181 break;
7182 }
7183 }
7184 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007185 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02007186 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02007187 ncname_len, ncname, ncname - exp->expr, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007188 free(path);
7189 }
7190 } else {
7191 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007192 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007193 } else {
7194 if (scnode) {
7195 /* we can find the nodes using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02007196 rc = moveto_node_hash(set, scnode, predicates);
Michal Vaskod3678892020-05-21 10:06:58 +02007197 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007198 rc = moveto_node(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007199 }
7200 }
7201 LY_CHECK_GOTO(rc, cleanup);
7202 }
7203 }
7204
7205 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007206 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7207 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007208 LY_CHECK_RET(rc);
7209 }
7210
7211cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007212 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007213 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007214 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007215 }
Michal Vaskod3678892020-05-21 10:06:58 +02007216 return rc;
7217}
7218
7219/**
7220 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7221 *
7222 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7223 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7224 * [8] NodeType ::= 'text' | 'node'
7225 *
7226 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007227 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007228 * @param[in] attr_axis Whether to search attributes or standard nodes.
7229 * @param[in] all_desc Whether to search all the descendants or children only.
7230 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7231 * @param[in] options XPath options.
7232 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7233 */
7234static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007235eval_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 +02007236 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007237{
7238 LY_ERR rc;
7239
7240 /* TODO */
7241 (void)attr_axis;
7242 (void)all_desc;
7243
7244 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007245 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007246 if (set->type == LYXP_SET_SCNODE_SET) {
7247 set_scnode_clear_ctx(set);
7248 /* just for the debug message below */
7249 set = NULL;
7250 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007251 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007252 rc = xpath_node(NULL, 0, set, options);
7253 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007254 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007255 rc = xpath_text(NULL, 0, set, options);
7256 }
7257 LY_CHECK_RET(rc);
7258 }
7259 }
7260 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007261 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007262 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007263
7264 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007265 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007266 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007267 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007268 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007269
7270 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007271 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007272 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007273 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007274 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007275
7276 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007277 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7278 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007279 LY_CHECK_RET(rc);
7280 }
7281
7282 return LY_SUCCESS;
7283}
7284
7285/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007286 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7287 *
7288 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7289 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007290 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007291 *
7292 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007293 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007294 * @param[in] all_desc Whether to search all the descendants or children only.
7295 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7296 * @param[in] options XPath options.
7297 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7298 */
7299static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007300eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7301 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007302{
Radek Krejci857189e2020-09-01 13:26:36 +02007303 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007304 LY_ERR rc;
7305
7306 goto step;
7307 do {
7308 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007309 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007310 all_desc = 0;
7311 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007312 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007313 all_desc = 1;
7314 }
7315 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007316 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007317 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007318
7319step:
Michal Vaskod3678892020-05-21 10:06:58 +02007320 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007321 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007322 attr_axis = 1;
7323
7324 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007325 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007326 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007327 } else {
7328 attr_axis = 0;
7329 }
7330
Michal Vasko03ff5a72019-09-11 13:49:33 +02007331 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007332 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007333 case LYXP_TOKEN_DOT:
7334 /* evaluate '.' */
7335 if (set && (options & LYXP_SCNODE_ALL)) {
7336 rc = moveto_scnode_self(set, all_desc, options);
7337 } else {
7338 rc = moveto_self(set, all_desc, options);
7339 }
7340 LY_CHECK_RET(rc);
7341 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007342 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007343 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007344 break;
7345
7346 case LYXP_TOKEN_DDOT:
7347 /* evaluate '..' */
7348 if (set && (options & LYXP_SCNODE_ALL)) {
7349 rc = moveto_scnode_parent(set, all_desc, options);
7350 } else {
7351 rc = moveto_parent(set, all_desc, options);
7352 }
7353 LY_CHECK_RET(rc);
7354 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007355 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007356 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007357 break;
7358
Michal Vasko03ff5a72019-09-11 13:49:33 +02007359 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007360 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007361 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007362 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007363 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007364
Michal Vaskod3678892020-05-21 10:06:58 +02007365 case LYXP_TOKEN_NODETYPE:
7366 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007367 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007368 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007369 break;
7370
7371 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007372 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007373 }
Michal Vasko004d3152020-06-11 19:59:22 +02007374 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007375
7376 return LY_SUCCESS;
7377}
7378
7379/**
7380 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7381 *
7382 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7383 *
7384 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007385 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007386 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7387 * @param[in] options XPath options.
7388 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7389 */
7390static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007391eval_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 +02007392{
Radek Krejci857189e2020-09-01 13:26:36 +02007393 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007394
7395 if (set) {
7396 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007397 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007398 }
7399
7400 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007401 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007402 /* evaluate '/' - deferred */
7403 all_desc = 0;
7404 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007405 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007406 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007407
Michal Vasko004d3152020-06-11 19:59:22 +02007408 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007409 return LY_SUCCESS;
7410 }
Michal Vasko004d3152020-06-11 19:59:22 +02007411 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007412 case LYXP_TOKEN_DOT:
7413 case LYXP_TOKEN_DDOT:
7414 case LYXP_TOKEN_AT:
7415 case LYXP_TOKEN_NAMETEST:
7416 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007417 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007418 break;
7419 default:
7420 break;
7421 }
7422
Michal Vasko03ff5a72019-09-11 13:49:33 +02007423 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007424 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007425 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7426 all_desc = 1;
7427 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007428 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007429 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007430
Michal Vaskob0099a92020-08-31 14:55:23 +02007431 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007432 }
7433
7434 return LY_SUCCESS;
7435}
7436
7437/**
7438 * @brief Evaluate FunctionCall. Logs directly on error.
7439 *
Michal Vaskod3678892020-05-21 10:06:58 +02007440 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007441 *
7442 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007443 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007444 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7445 * @param[in] options XPath options.
7446 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7447 */
7448static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007449eval_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 +02007450{
7451 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007452
Radek Krejci1deb5be2020-08-26 16:43:36 +02007453 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007454 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007455 struct lyxp_set **args = NULL, **args_aux;
7456
7457 if (set) {
7458 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007459 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007460 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007461 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007462 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007463 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007464 xpath_func = &xpath_sum;
7465 }
7466 break;
7467 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007468 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007469 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007470 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007471 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007472 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007473 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007474 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007475 xpath_func = &xpath_true;
7476 }
7477 break;
7478 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007479 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007480 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007481 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007482 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007483 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007484 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007485 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007486 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007487 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007488 xpath_func = &xpath_deref;
7489 }
7490 break;
7491 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007492 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007493 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007494 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007495 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007496 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007497 xpath_func = &xpath_string;
7498 }
7499 break;
7500 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007501 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007502 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007503 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007504 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007505 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007506 xpath_func = &xpath_current;
7507 }
7508 break;
7509 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007510 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007511 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007512 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007513 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007514 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007515 xpath_func = &xpath_re_match;
7516 }
7517 break;
7518 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007519 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007520 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007521 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007522 xpath_func = &xpath_translate;
7523 }
7524 break;
7525 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007526 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007527 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007528 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007529 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007530 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007531 xpath_func = &xpath_bit_is_set;
7532 }
7533 break;
7534 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007535 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007536 xpath_func = &xpath_starts_with;
7537 }
7538 break;
7539 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007540 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007541 xpath_func = &xpath_derived_from;
7542 }
7543 break;
7544 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007545 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007546 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007547 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007548 xpath_func = &xpath_string_length;
7549 }
7550 break;
7551 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007552 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007553 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007554 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007555 xpath_func = &xpath_substring_after;
7556 }
7557 break;
7558 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007559 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007560 xpath_func = &xpath_substring_before;
7561 }
7562 break;
7563 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007564 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007565 xpath_func = &xpath_derived_from_or_self;
7566 }
7567 break;
7568 }
7569
7570 if (!xpath_func) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007571 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 +02007572 return LY_EVALID;
7573 }
7574 }
7575
7576 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007577 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007578 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007579
7580 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007581 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007582 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007583 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007584 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007585
7586 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007587 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007588 if (set) {
7589 args = malloc(sizeof *args);
7590 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7591 arg_count = 1;
7592 args[0] = set_copy(set);
7593 if (!args[0]) {
7594 rc = LY_EMEM;
7595 goto cleanup;
7596 }
7597
Michal Vasko004d3152020-06-11 19:59:22 +02007598 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007599 LY_CHECK_GOTO(rc, cleanup);
7600 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007601 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007602 LY_CHECK_GOTO(rc, cleanup);
7603 }
7604 }
Michal Vasko004d3152020-06-11 19:59:22 +02007605 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
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 ++arg_count;
7612 args_aux = realloc(args, arg_count * sizeof *args);
7613 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7614 args = args_aux;
7615 args[arg_count - 1] = set_copy(set);
7616 if (!args[arg_count - 1]) {
7617 rc = LY_EMEM;
7618 goto cleanup;
7619 }
7620
Michal Vasko004d3152020-06-11 19:59:22 +02007621 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007622 LY_CHECK_GOTO(rc, cleanup);
7623 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007624 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007625 LY_CHECK_GOTO(rc, cleanup);
7626 }
7627 }
7628
7629 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007630 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007631 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007632 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007633 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007634
7635 if (set) {
7636 /* evaluate function */
7637 rc = xpath_func(args, arg_count, set, options);
7638
7639 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007640 /* merge all nodes from arg evaluations */
7641 for (i = 0; i < arg_count; ++i) {
7642 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007643 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007644 }
7645 }
7646 } else {
7647 rc = LY_SUCCESS;
7648 }
7649
7650cleanup:
7651 for (i = 0; i < arg_count; ++i) {
7652 lyxp_set_free(args[i]);
7653 }
7654 free(args);
7655
7656 return rc;
7657}
7658
7659/**
7660 * @brief Evaluate Number. Logs directly on error.
7661 *
7662 * @param[in] ctx Context for errors.
7663 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007664 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007665 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7666 * @return LY_ERR
7667 */
7668static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007669eval_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 +02007670{
7671 long double num;
7672 char *endptr;
7673
7674 if (set) {
7675 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007676 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007677 if (errno) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007678 LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7679 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 +02007680 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007681 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007682 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007683 LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7684 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 +02007685 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007686 return LY_EVALID;
7687 }
7688
7689 set_fill_number(set, num);
7690 }
7691
7692 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007693 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007694 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007695 return LY_SUCCESS;
7696}
7697
7698/**
7699 * @brief Evaluate PathExpr. Logs directly on error.
7700 *
Michal Vaskod3678892020-05-21 10:06:58 +02007701 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007702 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7703 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7704 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007705 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007706 *
7707 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007708 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007709 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7710 * @param[in] options XPath options.
7711 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7712 */
7713static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007714eval_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 +02007715{
Radek Krejci857189e2020-09-01 13:26:36 +02007716 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007717 LY_ERR rc;
7718
Michal Vasko004d3152020-06-11 19:59:22 +02007719 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007720 case LYXP_TOKEN_PAR1:
7721 /* '(' Expr ')' */
7722
7723 /* '(' */
7724 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007725 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007726 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007727
7728 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007729 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007730 LY_CHECK_RET(rc);
7731
7732 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007733 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007734 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007735 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007736 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007737
7738 parent_pos_pred = 0;
7739 goto predicate;
7740
7741 case LYXP_TOKEN_DOT:
7742 case LYXP_TOKEN_DDOT:
7743 case LYXP_TOKEN_AT:
7744 case LYXP_TOKEN_NAMETEST:
7745 case LYXP_TOKEN_NODETYPE:
7746 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007747 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007748 LY_CHECK_RET(rc);
7749 break;
7750
7751 case LYXP_TOKEN_FUNCNAME:
7752 /* FunctionCall */
7753 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007754 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007755 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007756 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007757 }
7758 LY_CHECK_RET(rc);
7759
7760 parent_pos_pred = 1;
7761 goto predicate;
7762
Michal Vasko3e48bf32020-06-01 08:39:07 +02007763 case LYXP_TOKEN_OPER_PATH:
7764 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007765 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007766 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007767 LY_CHECK_RET(rc);
7768 break;
7769
7770 case LYXP_TOKEN_LITERAL:
7771 /* Literal */
7772 if (!set || (options & LYXP_SCNODE_ALL)) {
7773 if (set) {
7774 set_scnode_clear_ctx(set);
7775 }
Michal Vasko004d3152020-06-11 19:59:22 +02007776 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007777 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007778 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007779 }
7780
7781 parent_pos_pred = 1;
7782 goto predicate;
7783
7784 case LYXP_TOKEN_NUMBER:
7785 /* Number */
7786 if (!set || (options & LYXP_SCNODE_ALL)) {
7787 if (set) {
7788 set_scnode_clear_ctx(set);
7789 }
Michal Vasko004d3152020-06-11 19:59:22 +02007790 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007791 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007792 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007793 }
7794 LY_CHECK_RET(rc);
7795
7796 parent_pos_pred = 1;
7797 goto predicate;
7798
7799 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007800 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 +02007801 &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007802 return LY_EVALID;
7803 }
7804
7805 return LY_SUCCESS;
7806
7807predicate:
7808 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007809 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7810 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007811 LY_CHECK_RET(rc);
7812 }
7813
7814 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007815 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007816
7817 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007818 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007819 all_desc = 0;
7820 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007821 all_desc = 1;
7822 }
7823
7824 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007825 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007826 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007827
Michal Vasko004d3152020-06-11 19:59:22 +02007828 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007829 LY_CHECK_RET(rc);
7830 }
7831
7832 return LY_SUCCESS;
7833}
7834
7835/**
7836 * @brief Evaluate UnionExpr. Logs directly on error.
7837 *
Michal Vaskod3678892020-05-21 10:06:58 +02007838 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007839 *
7840 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007841 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007842 * @param[in] repeat How many times this expression is repeated.
7843 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7844 * @param[in] options XPath options.
7845 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7846 */
7847static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007848eval_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 +02007849{
7850 LY_ERR rc = LY_SUCCESS;
7851 struct lyxp_set orig_set, set2;
7852 uint16_t i;
7853
7854 assert(repeat);
7855
7856 set_init(&orig_set, set);
7857 set_init(&set2, set);
7858
7859 set_fill_set(&orig_set, set);
7860
Michal Vasko004d3152020-06-11 19:59:22 +02007861 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007862 LY_CHECK_GOTO(rc, cleanup);
7863
7864 /* ('|' PathExpr)* */
7865 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007866 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007867 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007868 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007869 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007870
7871 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007872 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007873 LY_CHECK_GOTO(rc, cleanup);
7874 continue;
7875 }
7876
7877 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007878 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007879 LY_CHECK_GOTO(rc, cleanup);
7880
7881 /* eval */
7882 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007883 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007884 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007885 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007886 LY_CHECK_GOTO(rc, cleanup);
7887 }
7888 }
7889
7890cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007891 lyxp_set_free_content(&orig_set);
7892 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007893 return rc;
7894}
7895
7896/**
7897 * @brief Evaluate UnaryExpr. Logs directly on error.
7898 *
Michal Vaskod3678892020-05-21 10:06:58 +02007899 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007900 *
7901 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007902 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007903 * @param[in] repeat How many times this expression is repeated.
7904 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7905 * @param[in] options XPath options.
7906 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7907 */
7908static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007909eval_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 +02007910{
7911 LY_ERR rc;
7912 uint16_t this_op, i;
7913
7914 assert(repeat);
7915
7916 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007917 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007918 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007919 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 +02007920
7921 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007922 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007923 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007924 }
7925
Michal Vasko004d3152020-06-11 19:59:22 +02007926 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007927 LY_CHECK_RET(rc);
7928
7929 if (set && (repeat % 2)) {
7930 if (options & LYXP_SCNODE_ALL) {
7931 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7932 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007933 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007934 LY_CHECK_RET(rc);
7935 }
7936 }
7937
7938 return LY_SUCCESS;
7939}
7940
7941/**
7942 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7943 *
Michal Vaskod3678892020-05-21 10:06:58 +02007944 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007945 * | MultiplicativeExpr '*' UnaryExpr
7946 * | MultiplicativeExpr 'div' UnaryExpr
7947 * | MultiplicativeExpr 'mod' UnaryExpr
7948 *
7949 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007950 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007951 * @param[in] repeat How many times this expression is repeated.
7952 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7953 * @param[in] options XPath options.
7954 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7955 */
7956static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007957eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
7958 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007959{
7960 LY_ERR rc;
7961 uint16_t this_op;
7962 struct lyxp_set orig_set, set2;
7963 uint16_t i;
7964
7965 assert(repeat);
7966
7967 set_init(&orig_set, set);
7968 set_init(&set2, set);
7969
7970 set_fill_set(&orig_set, set);
7971
Michal Vasko004d3152020-06-11 19:59:22 +02007972 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007973 LY_CHECK_GOTO(rc, cleanup);
7974
7975 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7976 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007977 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007978
Michal Vasko004d3152020-06-11 19:59:22 +02007979 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007980 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007981 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007982 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007983
7984 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007985 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007986 LY_CHECK_GOTO(rc, cleanup);
7987 continue;
7988 }
7989
7990 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007991 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007992 LY_CHECK_GOTO(rc, cleanup);
7993
7994 /* eval */
7995 if (options & LYXP_SCNODE_ALL) {
7996 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007997 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007998 set_scnode_clear_ctx(set);
7999 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008000 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008001 LY_CHECK_GOTO(rc, cleanup);
8002 }
8003 }
8004
8005cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008006 lyxp_set_free_content(&orig_set);
8007 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008008 return rc;
8009}
8010
8011/**
8012 * @brief Evaluate AdditiveExpr. Logs directly on error.
8013 *
Michal Vaskod3678892020-05-21 10:06:58 +02008014 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008015 * | AdditiveExpr '+' MultiplicativeExpr
8016 * | AdditiveExpr '-' MultiplicativeExpr
8017 *
8018 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008019 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008020 * @param[in] repeat How many times this expression is repeated.
8021 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8022 * @param[in] options XPath options.
8023 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8024 */
8025static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008026eval_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 +02008027{
8028 LY_ERR rc;
8029 uint16_t this_op;
8030 struct lyxp_set orig_set, set2;
8031 uint16_t i;
8032
8033 assert(repeat);
8034
8035 set_init(&orig_set, set);
8036 set_init(&set2, set);
8037
8038 set_fill_set(&orig_set, set);
8039
Michal Vasko004d3152020-06-11 19:59:22 +02008040 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008041 LY_CHECK_GOTO(rc, cleanup);
8042
8043 /* ('+' / '-' MultiplicativeExpr)* */
8044 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008045 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008046
Michal Vasko004d3152020-06-11 19:59:22 +02008047 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008048 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008049 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008050 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008051
8052 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008053 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008054 LY_CHECK_GOTO(rc, cleanup);
8055 continue;
8056 }
8057
8058 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008059 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008060 LY_CHECK_GOTO(rc, cleanup);
8061
8062 /* eval */
8063 if (options & LYXP_SCNODE_ALL) {
8064 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008065 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008066 set_scnode_clear_ctx(set);
8067 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008068 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008069 LY_CHECK_GOTO(rc, cleanup);
8070 }
8071 }
8072
8073cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008074 lyxp_set_free_content(&orig_set);
8075 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008076 return rc;
8077}
8078
8079/**
8080 * @brief Evaluate RelationalExpr. Logs directly on error.
8081 *
Michal Vaskod3678892020-05-21 10:06:58 +02008082 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008083 * | RelationalExpr '<' AdditiveExpr
8084 * | RelationalExpr '>' AdditiveExpr
8085 * | RelationalExpr '<=' AdditiveExpr
8086 * | RelationalExpr '>=' AdditiveExpr
8087 *
8088 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008089 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008090 * @param[in] repeat How many times this expression is repeated.
8091 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8092 * @param[in] options XPath options.
8093 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8094 */
8095static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008096eval_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 +02008097{
8098 LY_ERR rc;
8099 uint16_t this_op;
8100 struct lyxp_set orig_set, set2;
8101 uint16_t i;
8102
8103 assert(repeat);
8104
8105 set_init(&orig_set, set);
8106 set_init(&set2, set);
8107
8108 set_fill_set(&orig_set, set);
8109
Michal Vasko004d3152020-06-11 19:59:22 +02008110 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008111 LY_CHECK_GOTO(rc, cleanup);
8112
8113 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8114 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008115 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008116
Michal Vasko004d3152020-06-11 19:59:22 +02008117 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008118 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008119 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008120 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008121
8122 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008123 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008124 LY_CHECK_GOTO(rc, cleanup);
8125 continue;
8126 }
8127
8128 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008129 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008130 LY_CHECK_GOTO(rc, cleanup);
8131
8132 /* eval */
8133 if (options & LYXP_SCNODE_ALL) {
8134 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008135 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008136 set_scnode_clear_ctx(set);
8137 } else {
8138 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8139 LY_CHECK_GOTO(rc, cleanup);
8140 }
8141 }
8142
8143cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008144 lyxp_set_free_content(&orig_set);
8145 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008146 return rc;
8147}
8148
8149/**
8150 * @brief Evaluate EqualityExpr. Logs directly on error.
8151 *
Michal Vaskod3678892020-05-21 10:06:58 +02008152 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008153 * | EqualityExpr '!=' RelationalExpr
8154 *
8155 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008156 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008157 * @param[in] repeat How many times this expression is repeated.
8158 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8159 * @param[in] options XPath options.
8160 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8161 */
8162static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008163eval_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 +02008164{
8165 LY_ERR rc;
8166 uint16_t this_op;
8167 struct lyxp_set orig_set, set2;
8168 uint16_t i;
8169
8170 assert(repeat);
8171
8172 set_init(&orig_set, set);
8173 set_init(&set2, set);
8174
8175 set_fill_set(&orig_set, set);
8176
Michal Vasko004d3152020-06-11 19:59:22 +02008177 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008178 LY_CHECK_GOTO(rc, cleanup);
8179
8180 /* ('=' / '!=' RelationalExpr)* */
8181 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008182 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008183
Michal Vasko004d3152020-06-11 19:59:22 +02008184 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008185 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008186 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008187 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008188
8189 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008190 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008191 LY_CHECK_GOTO(rc, cleanup);
8192 continue;
8193 }
8194
8195 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008196 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008197 LY_CHECK_GOTO(rc, cleanup);
8198
8199 /* eval */
8200 if (options & LYXP_SCNODE_ALL) {
8201 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008202 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8203 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008204 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008205 set_scnode_clear_ctx(set);
8206 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008207 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8208 LY_CHECK_GOTO(rc, cleanup);
8209 }
8210 }
8211
8212cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008213 lyxp_set_free_content(&orig_set);
8214 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008215 return rc;
8216}
8217
8218/**
8219 * @brief Evaluate AndExpr. Logs directly on error.
8220 *
Michal Vaskod3678892020-05-21 10:06:58 +02008221 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008222 *
8223 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008224 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008225 * @param[in] repeat How many times this expression is repeated.
8226 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8227 * @param[in] options XPath options.
8228 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8229 */
8230static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008231eval_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 +02008232{
8233 LY_ERR rc;
8234 struct lyxp_set orig_set, set2;
8235 uint16_t i;
8236
8237 assert(repeat);
8238
8239 set_init(&orig_set, set);
8240 set_init(&set2, set);
8241
8242 set_fill_set(&orig_set, set);
8243
Michal Vasko004d3152020-06-11 19:59:22 +02008244 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008245 LY_CHECK_GOTO(rc, cleanup);
8246
8247 /* cast to boolean, we know that will be the final result */
8248 if (set && (options & LYXP_SCNODE_ALL)) {
8249 set_scnode_clear_ctx(set);
8250 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008251 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008252 }
8253
8254 /* ('and' EqualityExpr)* */
8255 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008256 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8257 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008258 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008259 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008260
8261 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008262 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8263 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008264 LY_CHECK_GOTO(rc, cleanup);
8265 continue;
8266 }
8267
8268 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008269 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008270 LY_CHECK_GOTO(rc, cleanup);
8271
8272 /* eval - just get boolean value actually */
8273 if (set->type == LYXP_SET_SCNODE_SET) {
8274 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008275 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008276 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008277 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008278 set_fill_set(set, &set2);
8279 }
8280 }
8281
8282cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008283 lyxp_set_free_content(&orig_set);
8284 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008285 return rc;
8286}
8287
8288/**
8289 * @brief Evaluate OrExpr. Logs directly on error.
8290 *
Michal Vaskod3678892020-05-21 10:06:58 +02008291 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008292 *
8293 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008294 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008295 * @param[in] repeat How many times this expression is repeated.
8296 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8297 * @param[in] options XPath options.
8298 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8299 */
8300static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008301eval_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 +02008302{
8303 LY_ERR rc;
8304 struct lyxp_set orig_set, set2;
8305 uint16_t i;
8306
8307 assert(repeat);
8308
8309 set_init(&orig_set, set);
8310 set_init(&set2, set);
8311
8312 set_fill_set(&orig_set, set);
8313
Michal Vasko004d3152020-06-11 19:59:22 +02008314 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008315 LY_CHECK_GOTO(rc, cleanup);
8316
8317 /* cast to boolean, we know that will be the final result */
8318 if (set && (options & LYXP_SCNODE_ALL)) {
8319 set_scnode_clear_ctx(set);
8320 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008321 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008322 }
8323
8324 /* ('or' AndExpr)* */
8325 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008326 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8327 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008328 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008329 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008330
8331 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008332 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8333 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008334 LY_CHECK_GOTO(rc, cleanup);
8335 continue;
8336 }
8337
8338 set_fill_set(&set2, &orig_set);
8339 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8340 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008341 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008342 LY_CHECK_GOTO(rc, cleanup);
8343
8344 /* eval - just get boolean value actually */
8345 if (set->type == LYXP_SET_SCNODE_SET) {
8346 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008347 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008348 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008349 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008350 set_fill_set(set, &set2);
8351 }
8352 }
8353
8354cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008355 lyxp_set_free_content(&orig_set);
8356 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008357 return rc;
8358}
8359
8360/**
Michal Vasko004d3152020-06-11 19:59:22 +02008361 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008362 *
8363 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008364 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008365 * @param[in] etype Expression type to evaluate.
8366 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8367 * @param[in] options XPath options.
8368 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8369 */
8370static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008371eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8372 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008373{
8374 uint16_t i, count;
8375 enum lyxp_expr_type next_etype;
8376 LY_ERR rc;
8377
8378 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008379 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008380 next_etype = LYXP_EXPR_NONE;
8381 } else {
8382 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008383 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008384
8385 /* select one-priority lower because etype expression called us */
8386 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008387 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008388 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008389 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008390 } else {
8391 next_etype = LYXP_EXPR_NONE;
8392 }
8393 }
8394
8395 /* decide what expression are we parsing based on the repeat */
8396 switch (next_etype) {
8397 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008398 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008399 break;
8400 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008401 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008402 break;
8403 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008404 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008405 break;
8406 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008407 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008408 break;
8409 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008410 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008411 break;
8412 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008413 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008414 break;
8415 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008416 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008417 break;
8418 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008419 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008420 break;
8421 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008422 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008423 break;
8424 default:
8425 LOGINT_RET(set->ctx);
8426 }
8427
8428 return rc;
8429}
8430
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008431/**
8432 * @brief Get root type.
8433 *
8434 * @param[in] ctx_node Context node.
8435 * @param[in] ctx_scnode Schema context node.
8436 * @param[in] options XPath options.
8437 * @return Root type.
8438 */
8439static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008440lyxp_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 +01008441{
Michal Vasko6b26e742020-07-17 15:02:10 +02008442 const struct lysc_node *op;
8443
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008444 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008445 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008446 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008447
8448 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008449 /* general root that can access everything */
8450 return LYXP_NODE_ROOT;
8451 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8452 /* root context node can access only config data (because we said so, it is unspecified) */
8453 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008454 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008455 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008456 }
8457
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008458 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008459 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008460 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008461
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008462 if (op || !(options & LYXP_SCHEMA)) {
8463 /* general root that can access everything */
8464 return LYXP_NODE_ROOT;
8465 } else if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008466 /* root context node can access only config data (because we said so, it is unspecified) */
8467 return LYXP_NODE_ROOT_CONFIG;
8468 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008469 return LYXP_NODE_ROOT;
8470}
8471
Michal Vasko03ff5a72019-09-11 13:49:33 +02008472LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008473lyxp_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 +02008474 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 +02008475{
Michal Vasko004d3152020-06-11 19:59:22 +02008476 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008477 LY_ERR rc;
8478
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008479 LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL);
8480 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8481 LOGARG(NULL, "Current module must be set if schema format is used.");
8482 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008483 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008484
8485 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008486 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008487 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008488 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8489 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8490
8491 set->ctx = (struct ly_ctx *)(cur_mod ? cur_mod->ctx : (ctx_node ? LYD_CTX(ctx_node) : (tree ? LYD_CTX(tree) : NULL)));
8492 set->cur_node = ctx_node;
8493 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008494 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8495 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008496 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008497 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008498 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008499 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008500
8501 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008502 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008503 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008504 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008505 }
8506
Michal Vasko03ff5a72019-09-11 13:49:33 +02008507 return rc;
8508}
8509
8510#if 0
8511
8512/* full xml printing of set elements, not used currently */
8513
8514void
8515lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8516{
8517 uint32_t i;
8518 char *str_num;
8519 struct lyout out;
8520
8521 memset(&out, 0, sizeof out);
8522
8523 out.type = LYOUT_STREAM;
8524 out.method.f = f;
8525
8526 switch (set->type) {
8527 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008528 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008529 break;
8530 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008531 ly_print_(&out, "Boolean XPath set:\n");
8532 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008533 break;
8534 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008535 ly_print_(&out, "String XPath set:\n");
8536 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008537 break;
8538 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008539 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008540
8541 if (isnan(set->value.num)) {
8542 str_num = strdup("NaN");
8543 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8544 str_num = strdup("0");
8545 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8546 str_num = strdup("Infinity");
8547 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8548 str_num = strdup("-Infinity");
8549 } else if ((long long)set->value.num == set->value.num) {
8550 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8551 str_num = NULL;
8552 }
8553 } else {
8554 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8555 str_num = NULL;
8556 }
8557 }
8558 if (!str_num) {
8559 LOGMEM;
8560 return;
8561 }
Michal Vasko5233e962020-08-14 14:26:20 +02008562 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008563 free(str_num);
8564 break;
8565 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008566 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008567
8568 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008569 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008570 switch (set->node_type[i]) {
8571 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008572 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008573 break;
8574 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008575 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008576 break;
8577 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008578 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008579 break;
8580 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008581 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008582 break;
8583 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008584 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008585 break;
8586 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008587 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008588 break;
8589 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008590 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008591 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008592 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008593 break;
8594 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008595 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 +02008596 break;
8597 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008598 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 +02008599 break;
8600 }
8601 }
8602 break;
8603 }
8604}
8605
8606#endif
8607
8608LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008609lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008610{
8611 long double num;
8612 char *str;
8613 LY_ERR rc;
8614
8615 if (!set || (set->type == target)) {
8616 return LY_SUCCESS;
8617 }
8618
8619 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008620 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008621
8622 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008623 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008624 return LY_EINVAL;
8625 }
8626
8627 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008628 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008629 switch (set->type) {
8630 case LYXP_SET_NUMBER:
8631 if (isnan(set->val.num)) {
8632 set->val.str = strdup("NaN");
8633 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8634 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8635 set->val.str = strdup("0");
8636 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8637 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8638 set->val.str = strdup("Infinity");
8639 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8640 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8641 set->val.str = strdup("-Infinity");
8642 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8643 } else if ((long long)set->val.num == set->val.num) {
8644 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8645 LOGMEM_RET(set->ctx);
8646 }
8647 set->val.str = str;
8648 } else {
8649 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8650 LOGMEM_RET(set->ctx);
8651 }
8652 set->val.str = str;
8653 }
8654 break;
8655 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008656 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008657 set->val.str = strdup("true");
8658 } else {
8659 set->val.str = strdup("false");
8660 }
8661 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8662 break;
8663 case LYXP_SET_NODE_SET:
8664 assert(set->used);
8665
8666 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008667 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008668
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008669 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008670 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008671 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008672 set->val.str = str;
8673 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008674 default:
8675 LOGINT_RET(set->ctx);
8676 }
8677 set->type = LYXP_SET_STRING;
8678 }
8679
8680 /* to NUMBER */
8681 if (target == LYXP_SET_NUMBER) {
8682 switch (set->type) {
8683 case LYXP_SET_STRING:
8684 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008685 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008686 set->val.num = num;
8687 break;
8688 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008689 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008690 set->val.num = 1;
8691 } else {
8692 set->val.num = 0;
8693 }
8694 break;
8695 default:
8696 LOGINT_RET(set->ctx);
8697 }
8698 set->type = LYXP_SET_NUMBER;
8699 }
8700
8701 /* to BOOLEAN */
8702 if (target == LYXP_SET_BOOLEAN) {
8703 switch (set->type) {
8704 case LYXP_SET_NUMBER:
8705 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008706 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008707 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008708 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008709 }
8710 break;
8711 case LYXP_SET_STRING:
8712 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008713 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008714 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008715 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008716 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008717 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008718 }
8719 break;
8720 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008721 if (set->used) {
8722 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008723 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008724 } else {
8725 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008726 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008727 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008728 break;
8729 default:
8730 LOGINT_RET(set->ctx);
8731 }
8732 set->type = LYXP_SET_BOOLEAN;
8733 }
8734
Michal Vasko03ff5a72019-09-11 13:49:33 +02008735 return LY_SUCCESS;
8736}
8737
8738LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008739lyxp_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 +02008740 const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008741{
Michal Vasko004d3152020-06-11 19:59:22 +02008742 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008743
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008744 LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL);
8745 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8746 LOGARG(NULL, "Current module must be set if schema format is used.");
8747 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008748 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008749
8750 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008751 memset(set, 0, sizeof *set);
8752 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008753 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8754 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 +01008755 set->val.scnodes[0].in_ctx = -2;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008756
8757 set->ctx = cur_mod ? cur_mod->ctx : (ctx_scnode ? ctx_scnode->module->ctx : NULL);
8758 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008759 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008760 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8761 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008762 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008763 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008764 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008765
8766 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008767 return eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008768}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008769
8770API const char *
8771lyxp_get_expr(const struct lyxp_expr *path)
8772{
8773 if (!path) {
8774 return NULL;
8775 }
8776
8777 return path->expr;
8778}