blob: cb6af2bb51e69d9066d42c71a5d480378f5839d5 [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>
24#include <limits.h>
25#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010027#include <stdio.h>
28#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010029#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010030
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020032#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020033#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020035#include "hash_table.h"
Radek 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.h"
40#include "printer_data.h"
41#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);
Radek Krejci1deb5be2020-08-26 16:43:36 +020047static LY_ERR eval_expr_select(struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, uint32_t options);
Michal Vasko03ff5a72019-09-11 13:49:33 +020048
49/**
50 * @brief Print the type of an XPath \p set.
51 *
52 * @param[in] set Set to use.
53 * @return Set type string.
54 */
55static const char *
56print_set_type(struct lyxp_set *set)
57{
58 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020059 case LYXP_SET_NODE_SET:
60 return "node set";
61 case LYXP_SET_SCNODE_SET:
62 return "schema node set";
63 case LYXP_SET_BOOLEAN:
64 return "boolean";
65 case LYXP_SET_NUMBER:
66 return "number";
67 case LYXP_SET_STRING:
68 return "string";
69 }
70
71 return NULL;
72}
73
Michal Vasko24cddf82020-06-01 08:17:01 +020074const char *
75lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020076{
77 switch (tok) {
78 case LYXP_TOKEN_PAR1:
79 return "(";
80 case LYXP_TOKEN_PAR2:
81 return ")";
82 case LYXP_TOKEN_BRACK1:
83 return "[";
84 case LYXP_TOKEN_BRACK2:
85 return "]";
86 case LYXP_TOKEN_DOT:
87 return ".";
88 case LYXP_TOKEN_DDOT:
89 return "..";
90 case LYXP_TOKEN_AT:
91 return "@";
92 case LYXP_TOKEN_COMMA:
93 return ",";
94 case LYXP_TOKEN_NAMETEST:
95 return "NameTest";
96 case LYXP_TOKEN_NODETYPE:
97 return "NodeType";
98 case LYXP_TOKEN_FUNCNAME:
99 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200100 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200101 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200102 case LYXP_TOKEN_OPER_EQUAL:
103 return "Operator(Equal)";
104 case LYXP_TOKEN_OPER_NEQUAL:
105 return "Operator(Non-equal)";
106 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200107 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200108 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200109 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200110 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200111 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200112 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200113 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200114 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200115 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200116 case LYXP_TOKEN_LITERAL:
117 return "Literal";
118 case LYXP_TOKEN_NUMBER:
119 return "Number";
120 default:
121 LOGINT(NULL);
122 return "";
123 }
124}
125
126/**
127 * @brief Print the whole expression \p exp to debug output.
128 *
129 * @param[in] exp Expression to use.
130 */
131static void
132print_expr_struct_debug(struct lyxp_expr *exp)
133{
134 uint16_t i, j;
135 char tmp[128];
136
Radek Krejci52b6d512020-10-12 12:33:17 +0200137 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200138 return;
139 }
140
141 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
142 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200143 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200144 &exp->expr[exp->tok_pos[i]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200145 if (exp->repeat[i]) {
146 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
147 for (j = 1; exp->repeat[i][j]; ++j) {
148 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
149 }
150 strcat(tmp, ")");
151 }
152 LOGDBG(LY_LDGXPATH, tmp);
153 }
154}
155
156#ifndef NDEBUG
157
158/**
159 * @brief Print XPath set content to debug output.
160 *
161 * @param[in] set Set to print.
162 */
163static void
164print_set_debug(struct lyxp_set *set)
165{
166 uint32_t i;
167 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200168 struct lyxp_set_node *item;
169 struct lyxp_set_scnode *sitem;
170
Radek Krejci52b6d512020-10-12 12:33:17 +0200171 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200172 return;
173 }
174
175 switch (set->type) {
176 case LYXP_SET_NODE_SET:
177 LOGDBG(LY_LDGXPATH, "set NODE SET:");
178 for (i = 0; i < set->used; ++i) {
179 item = &set->val.nodes[i];
180
181 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100182 case LYXP_NODE_NONE:
183 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
184 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200185 case LYXP_NODE_ROOT:
186 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
187 break;
188 case LYXP_NODE_ROOT_CONFIG:
189 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
190 break;
191 case LYXP_NODE_ELEM:
Michal Vasko69730152020-10-09 16:30:07 +0200192 if ((item->node->schema->nodetype == LYS_LIST) &&
193 (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200194 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200195 item->node->schema->name, LYD_CANON_VALUE(lyd_child(item->node)));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200196 } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) {
197 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200198 item->node->schema->name, LYD_CANON_VALUE(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200199 } else {
200 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
201 }
202 break;
203 case LYXP_NODE_TEXT:
204 if (item->node->schema->nodetype & LYS_ANYDATA) {
205 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200206 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200207 } else {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200208 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 +0200209 }
210 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100211 case LYXP_NODE_META:
212 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 +0200213 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200214 break;
215 }
216 }
217 break;
218
219 case LYXP_SET_SCNODE_SET:
220 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
221 for (i = 0; i < set->used; ++i) {
222 sitem = &set->val.scnodes[i];
223
224 switch (sitem->type) {
225 case LYXP_NODE_ROOT:
226 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
227 break;
228 case LYXP_NODE_ROOT_CONFIG:
229 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
230 break;
231 case LYXP_NODE_ELEM:
232 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
233 break;
234 default:
235 LOGINT(NULL);
236 break;
237 }
238 }
239 break;
240
Michal Vasko03ff5a72019-09-11 13:49:33 +0200241 case LYXP_SET_BOOLEAN:
242 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200243 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200244 break;
245
246 case LYXP_SET_STRING:
247 LOGDBG(LY_LDGXPATH, "set STRING");
248 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
249 break;
250
251 case LYXP_SET_NUMBER:
252 LOGDBG(LY_LDGXPATH, "set NUMBER");
253
254 if (isnan(set->val.num)) {
255 str = strdup("NaN");
256 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
257 str = strdup("0");
258 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
259 str = strdup("Infinity");
260 } else if (isinf(set->val.num) && signbit(set->val.num)) {
261 str = strdup("-Infinity");
262 } else if ((long long)set->val.num == set->val.num) {
263 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
264 str = NULL;
265 }
266 } else {
267 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
268 str = NULL;
269 }
270 }
271 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
272
273 LOGDBG(LY_LDGXPATH, "\t%s", str);
274 free(str);
275 }
276}
277
278#endif
279
280/**
281 * @brief Realloc the string \p str.
282 *
283 * @param[in] ctx libyang context for logging.
284 * @param[in] needed How much free space is required.
285 * @param[in,out] str Pointer to the string to use.
286 * @param[in,out] used Used bytes in \p str.
287 * @param[in,out] size Allocated bytes in \p str.
288 * @return LY_ERR
289 */
290static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100291cast_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 +0200292{
293 if (*size - *used < needed) {
294 do {
295 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
296 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
297 return LY_EINVAL;
298 }
299 *size += LYXP_STRING_CAST_SIZE_STEP;
300 } while (*size - *used < needed);
301 *str = ly_realloc(*str, *size * sizeof(char));
302 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
303 }
304
305 return LY_SUCCESS;
306}
307
308/**
309 * @brief Cast nodes recursively to one string @p str.
310 *
311 * @param[in] node Node to cast.
312 * @param[in] fake_cont Whether to put the data into a "fake" container.
313 * @param[in] root_type Type of the XPath root.
314 * @param[in] indent Current indent.
315 * @param[in,out] str Resulting string.
316 * @param[in,out] used Used bytes in @p str.
317 * @param[in,out] size Allocated bytes in @p str.
318 * @return LY_ERR
319 */
320static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200321cast_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 +0200322 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200323{
Radek Krejci7f769d72020-07-11 23:13:56 +0200324 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200325 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200326 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200327 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200328 struct lyd_node_any *any;
329 LY_ERR rc;
330
331 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
332 return LY_SUCCESS;
333 }
334
335 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200336 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200337 LY_CHECK_RET(rc);
338 strcpy(*str + (*used - 1), "\n");
339 ++(*used);
340
341 ++indent;
342 }
343
344 switch (node->schema->nodetype) {
345 case LYS_CONTAINER:
346 case LYS_LIST:
347 case LYS_RPC:
348 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200349 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200350 LY_CHECK_RET(rc);
351 strcpy(*str + (*used - 1), "\n");
352 ++(*used);
353
Radek Krejcia1c1e542020-09-29 16:06:52 +0200354 for (child = lyd_child(node); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200355 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
356 LY_CHECK_RET(rc);
357 }
358
359 break;
360
361 case LYS_LEAF:
362 case LYS_LEAFLIST:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200363 value_str = LYD_CANON_VALUE(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200364
365 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200366 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 +0200367 memset(*str + (*used - 1), ' ', indent * 2);
368 *used += indent * 2;
369
370 /* print value */
371 if (*used == 1) {
372 sprintf(*str + (*used - 1), "%s", value_str);
373 *used += strlen(value_str);
374 } else {
375 sprintf(*str + (*used - 1), "%s\n", value_str);
376 *used += strlen(value_str) + 1;
377 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200378
379 break;
380
381 case LYS_ANYXML:
382 case LYS_ANYDATA:
383 any = (struct lyd_node_any *)node;
384 if (!(void *)any->value.tree) {
385 /* no content */
386 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200387 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200388 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200389 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100390
Michal Vasko60ea6352020-06-29 13:39:39 +0200391 if (any->value_type == LYD_ANYDATA_LYB) {
392 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200393 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 +0200394 /* successfully parsed */
395 free(any->value.mem);
396 any->value.tree = tree;
397 any->value_type = LYD_ANYDATA_DATATREE;
398 }
Radek Krejci7931b192020-06-25 17:05:03 +0200399 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200400 }
401
Michal Vasko03ff5a72019-09-11 13:49:33 +0200402 switch (any->value_type) {
403 case LYD_ANYDATA_STRING:
404 case LYD_ANYDATA_XML:
405 case LYD_ANYDATA_JSON:
406 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200407 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200408 break;
409 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200410 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200411 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200412 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100413 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200414 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200415 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200416 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200417 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200418 }
419 }
420
421 line = strtok_r(buf, "\n", &ptr);
422 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200423 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200424 if (rc != LY_SUCCESS) {
425 free(buf);
426 return rc;
427 }
428 memset(*str + (*used - 1), ' ', indent * 2);
429 *used += indent * 2;
430
431 strcpy(*str + (*used - 1), line);
432 *used += strlen(line);
433
434 strcpy(*str + (*used - 1), "\n");
435 *used += 1;
436 } while ((line = strtok_r(NULL, "\n", &ptr)));
437
438 free(buf);
439 break;
440
441 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200442 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200443 }
444
445 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200446 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200447 LY_CHECK_RET(rc);
448 strcpy(*str + (*used - 1), "\n");
449 ++(*used);
450
451 --indent;
452 }
453
454 return LY_SUCCESS;
455}
456
457/**
458 * @brief Cast an element into a string.
459 *
460 * @param[in] node Node to cast.
461 * @param[in] fake_cont Whether to put the data into a "fake" container.
462 * @param[in] root_type Type of the XPath root.
463 * @param[out] str Element cast to dynamically-allocated string.
464 * @return LY_ERR
465 */
466static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200467cast_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 +0200468{
469 uint16_t used, size;
470 LY_ERR rc;
471
472 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200473 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200474 (*str)[0] = '\0';
475 used = 1;
476 size = LYXP_STRING_CAST_SIZE_START;
477
478 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
479 if (rc != LY_SUCCESS) {
480 free(*str);
481 return rc;
482 }
483
484 if (size > used) {
485 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200486 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200487 }
488 return LY_SUCCESS;
489}
490
491/**
492 * @brief Cast a LYXP_SET_NODE_SET set into a string.
493 * Context position aware.
494 *
495 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200496 * @param[out] str Cast dynamically-allocated string.
497 * @return LY_ERR
498 */
499static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100500cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200501{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200502 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100503 case LYXP_NODE_NONE:
504 /* invalid */
505 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200506 case LYXP_NODE_ROOT:
507 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100508 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200509 case LYXP_NODE_ELEM:
510 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100511 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100512 case LYXP_NODE_META:
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200513 *str = strdup(set->val.meta[0].meta->value.canonical);
514 if (!*str) {
515 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200516 }
517 return LY_SUCCESS;
518 }
519
520 LOGINT_RET(set->ctx);
521}
522
523/**
524 * @brief Cast a string into an XPath number.
525 *
526 * @param[in] str String to use.
527 * @return Cast number.
528 */
529static long double
530cast_string_to_number(const char *str)
531{
532 long double num;
533 char *ptr;
534
535 errno = 0;
536 num = strtold(str, &ptr);
537 if (errno || *ptr) {
538 num = NAN;
539 }
540 return num;
541}
542
543/**
544 * @brief Callback for checking value equality.
545 *
Radek Krejci857189e2020-09-01 13:26:36 +0200546 * Implementation of ::values_equal_cb.
547 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200548 * @param[in] val1_p First value.
549 * @param[in] val2_p Second value.
550 * @param[in] mod Whether hash table is being modified.
551 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200552 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200553 */
Radek Krejci857189e2020-09-01 13:26:36 +0200554static ly_bool
555set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200556{
557 struct lyxp_set_hash_node *val1, *val2;
558
559 val1 = (struct lyxp_set_hash_node *)val1_p;
560 val2 = (struct lyxp_set_hash_node *)val2_p;
561
562 if ((val1->node == val2->node) && (val1->type == val2->type)) {
563 return 1;
564 }
565
566 return 0;
567}
568
569/**
570 * @brief Insert node and its hash into set.
571 *
572 * @param[in] set et to insert to.
573 * @param[in] node Node with hash.
574 * @param[in] type Node type.
575 */
576static void
577set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
578{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200579 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200580 uint32_t i, hash;
581 struct lyxp_set_hash_node hnode;
582
583 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
584 /* create hash table and add all the nodes */
585 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
586 for (i = 0; i < set->used; ++i) {
587 hnode.node = set->val.nodes[i].node;
588 hnode.type = set->val.nodes[i].type;
589
590 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
591 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
592 hash = dict_hash_multi(hash, NULL, 0);
593
594 r = lyht_insert(set->ht, &hnode, hash, NULL);
595 assert(!r);
596 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200597
Michal Vasko9d6befd2019-12-11 14:56:56 +0100598 if (hnode.node == node) {
599 /* it was just added, do not add it twice */
600 node = NULL;
601 }
602 }
603 }
604
605 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200606 /* add the new node into hash table */
607 hnode.node = node;
608 hnode.type = type;
609
610 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
611 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
612 hash = dict_hash_multi(hash, NULL, 0);
613
614 r = lyht_insert(set->ht, &hnode, hash, NULL);
615 assert(!r);
616 (void)r;
617 }
618}
619
620/**
621 * @brief Remove node and its hash from set.
622 *
623 * @param[in] set Set to remove from.
624 * @param[in] node Node to remove.
625 * @param[in] type Node type.
626 */
627static void
628set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
629{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200630 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200631 struct lyxp_set_hash_node hnode;
632 uint32_t hash;
633
634 if (set->ht) {
635 hnode.node = node;
636 hnode.type = type;
637
638 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
639 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
640 hash = dict_hash_multi(hash, NULL, 0);
641
642 r = lyht_remove(set->ht, &hnode, hash);
643 assert(!r);
644 (void)r;
645
646 if (!set->ht->used) {
647 lyht_free(set->ht);
648 set->ht = NULL;
649 }
650 }
651}
652
653/**
654 * @brief Check whether node is in set based on its hash.
655 *
656 * @param[in] set Set to search in.
657 * @param[in] node Node to search for.
658 * @param[in] type Node type.
659 * @param[in] skip_idx Index in @p set to skip.
660 * @return LY_ERR
661 */
662static LY_ERR
663set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
664{
665 struct lyxp_set_hash_node hnode, *match_p;
666 uint32_t hash;
667
668 hnode.node = node;
669 hnode.type = type;
670
671 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
672 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
673 hash = dict_hash_multi(hash, NULL, 0);
674
675 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
676 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
677 /* we found it on the index that should be skipped, find another */
678 hnode = *match_p;
679 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
680 /* none other found */
681 return LY_SUCCESS;
682 }
683 }
684
685 return LY_EEXIST;
686 }
687
688 /* not found */
689 return LY_SUCCESS;
690}
691
Michal Vaskod3678892020-05-21 10:06:58 +0200692void
693lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200694{
695 if (!set) {
696 return;
697 }
698
699 if (set->type == LYXP_SET_NODE_SET) {
700 free(set->val.nodes);
701 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200702 } else if (set->type == LYXP_SET_SCNODE_SET) {
703 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200704 lyht_free(set->ht);
705 } else {
706 if (set->type == LYXP_SET_STRING) {
707 free(set->val.str);
708 }
709 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200710 }
Michal Vaskod3678892020-05-21 10:06:58 +0200711
712 set->val.nodes = NULL;
713 set->used = 0;
714 set->size = 0;
715 set->ht = NULL;
716 set->ctx_pos = 0;
717 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200718}
719
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100720/**
721 * @brief Free dynamically-allocated set.
722 *
723 * @param[in] set Set to free.
724 */
725static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200726lyxp_set_free(struct lyxp_set *set)
727{
728 if (!set) {
729 return;
730 }
731
Michal Vaskod3678892020-05-21 10:06:58 +0200732 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200733 free(set);
734}
735
736/**
737 * @brief Initialize set context.
738 *
739 * @param[in] new Set to initialize.
740 * @param[in] set Arbitrary initialized set.
741 */
742static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200743set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200744{
745 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200746 if (set) {
747 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200748 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100749 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200750 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100751 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200752 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200753 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200754 new->prefix_data = set->prefix_data;
Michal Vasko02a77382019-09-12 11:47:35 +0200755 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200756}
757
758/**
759 * @brief Create a deep copy of a set.
760 *
761 * @param[in] set Set to copy.
762 * @return Copy of @p set.
763 */
764static struct lyxp_set *
765set_copy(struct lyxp_set *set)
766{
767 struct lyxp_set *ret;
768 uint16_t i;
769
770 if (!set) {
771 return NULL;
772 }
773
774 ret = malloc(sizeof *ret);
775 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
776 set_init(ret, set);
777
778 if (set->type == LYXP_SET_SCNODE_SET) {
779 ret->type = set->type;
780
781 for (i = 0; i < set->used; ++i) {
Michal Vaskoba716542019-12-16 10:01:58 +0100782 if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200783 uint32_t idx;
784 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
785 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100786 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200787 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200788 lyxp_set_free(ret);
789 return NULL;
790 }
Michal Vaskoba716542019-12-16 10:01:58 +0100791 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200792 }
793 }
794 } else if (set->type == LYXP_SET_NODE_SET) {
795 ret->type = set->type;
796 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
797 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
798 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
799
800 ret->used = ret->size = set->used;
801 ret->ctx_pos = set->ctx_pos;
802 ret->ctx_size = set->ctx_size;
803 ret->ht = lyht_dup(set->ht);
804 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200805 memcpy(ret, set, sizeof *ret);
806 if (set->type == LYXP_SET_STRING) {
807 ret->val.str = strdup(set->val.str);
808 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
809 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200810 }
811
812 return ret;
813}
814
815/**
816 * @brief Fill XPath set with a string. Any current data are disposed of.
817 *
818 * @param[in] set Set to fill.
819 * @param[in] string String to fill into \p set.
820 * @param[in] str_len Length of \p string. 0 is a valid value!
821 */
822static void
823set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
824{
Michal Vaskod3678892020-05-21 10:06:58 +0200825 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200826
827 set->type = LYXP_SET_STRING;
828 if ((str_len == 0) && (string[0] != '\0')) {
829 string = "";
830 }
831 set->val.str = strndup(string, str_len);
832}
833
834/**
835 * @brief Fill XPath set with a number. Any current data are disposed of.
836 *
837 * @param[in] set Set to fill.
838 * @param[in] number Number to fill into \p set.
839 */
840static void
841set_fill_number(struct lyxp_set *set, long double number)
842{
Michal Vaskod3678892020-05-21 10:06:58 +0200843 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200844
845 set->type = LYXP_SET_NUMBER;
846 set->val.num = number;
847}
848
849/**
850 * @brief Fill XPath set with a boolean. Any current data are disposed of.
851 *
852 * @param[in] set Set to fill.
853 * @param[in] boolean Boolean to fill into \p set.
854 */
855static void
Radek Krejci857189e2020-09-01 13:26:36 +0200856set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200857{
Michal Vaskod3678892020-05-21 10:06:58 +0200858 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200859
860 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200861 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200862}
863
864/**
865 * @brief Fill XPath set with the value from another set (deep assign).
866 * Any current data are disposed of.
867 *
868 * @param[in] trg Set to fill.
869 * @param[in] src Source set to copy into \p trg.
870 */
871static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200872set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200873{
874 if (!trg || !src) {
875 return;
876 }
877
878 if (trg->type == LYXP_SET_NODE_SET) {
879 free(trg->val.nodes);
880 } else if (trg->type == LYXP_SET_STRING) {
881 free(trg->val.str);
882 }
883 set_init(trg, src);
884
885 if (src->type == LYXP_SET_SCNODE_SET) {
886 trg->type = LYXP_SET_SCNODE_SET;
887 trg->used = src->used;
888 trg->size = src->used;
889
890 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
891 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
892 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
893 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200894 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200895 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200896 set_fill_number(trg, src->val.num);
897 } else if (src->type == LYXP_SET_STRING) {
898 set_fill_string(trg, src->val.str, strlen(src->val.str));
899 } else {
900 if (trg->type == LYXP_SET_NODE_SET) {
901 free(trg->val.nodes);
902 } else if (trg->type == LYXP_SET_STRING) {
903 free(trg->val.str);
904 }
905
Michal Vaskod3678892020-05-21 10:06:58 +0200906 assert(src->type == LYXP_SET_NODE_SET);
907
908 trg->type = LYXP_SET_NODE_SET;
909 trg->used = src->used;
910 trg->size = src->used;
911 trg->ctx_pos = src->ctx_pos;
912 trg->ctx_size = src->ctx_size;
913
914 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
915 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
916 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
917 if (src->ht) {
918 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200919 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200920 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200921 }
922 }
923}
924
925/**
926 * @brief Clear context of all schema nodes.
927 *
928 * @param[in] set Set to clear.
929 */
930static void
931set_scnode_clear_ctx(struct lyxp_set *set)
932{
933 uint32_t i;
934
935 for (i = 0; i < set->used; ++i) {
936 if (set->val.scnodes[i].in_ctx == 1) {
937 set->val.scnodes[i].in_ctx = 0;
Michal Vasko5c4e5892019-11-14 12:31:38 +0100938 } else if (set->val.scnodes[i].in_ctx == -2) {
939 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200940 }
941 }
942}
943
944/**
945 * @brief Remove a node from a set. Removing last node changes
946 * set into LYXP_SET_EMPTY. Context position aware.
947 *
948 * @param[in] set Set to use.
949 * @param[in] idx Index from @p set of the node to be removed.
950 */
951static void
952set_remove_node(struct lyxp_set *set, uint32_t idx)
953{
954 assert(set && (set->type == LYXP_SET_NODE_SET));
955 assert(idx < set->used);
956
957 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
958
959 --set->used;
960 if (set->used) {
961 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
962 (set->used - idx) * sizeof *set->val.nodes);
963 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200964 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200965 }
966}
967
968/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100969 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200970 *
971 * @param[in] set Set to use.
972 * @param[in] idx Index from @p set of the node to be removed.
973 */
974static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100975set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200976{
977 assert(set && (set->type == LYXP_SET_NODE_SET));
978 assert(idx < set->used);
979
Michal Vasko2caefc12019-11-14 16:07:56 +0100980 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
981 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
982 }
983 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +0200984}
985
986/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100987 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +0200988 * set into LYXP_SET_EMPTY. Context position aware.
989 *
990 * @param[in] set Set to consolidate.
991 */
992static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100993set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +0200994{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +0200995 uint16_t i, orig_used, end = 0;
Michal Vasko57eab132019-09-24 11:46:26 +0200996 int32_t start;
997
Michal Vaskod3678892020-05-21 10:06:58 +0200998 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +0200999
1000 orig_used = set->used;
1001 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001002 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001003 start = -1;
1004 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001005 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001006 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001007 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001008 end = i;
1009 ++i;
1010 break;
1011 }
1012
1013 ++i;
1014 if (i == orig_used) {
1015 end = i;
1016 }
1017 } while (i < orig_used);
1018
1019 if (start > -1) {
1020 /* move the whole chunk of valid nodes together */
1021 if (set->used != (unsigned)start) {
1022 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1023 }
1024 set->used += end - start;
1025 }
1026 }
Michal Vasko57eab132019-09-24 11:46:26 +02001027}
1028
1029/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001030 * @brief Check for duplicates in a node set.
1031 *
1032 * @param[in] set Set to check.
1033 * @param[in] node Node to look for in @p set.
1034 * @param[in] node_type Type of @p node.
1035 * @param[in] skip_idx Index from @p set to skip.
1036 * @return LY_ERR
1037 */
1038static LY_ERR
1039set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1040{
1041 uint32_t i;
1042
Michal Vasko2caefc12019-11-14 16:07:56 +01001043 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001044 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1045 }
1046
1047 for (i = 0; i < set->used; ++i) {
1048 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1049 continue;
1050 }
1051
1052 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1053 return LY_EEXIST;
1054 }
1055 }
1056
1057 return LY_SUCCESS;
1058}
1059
Radek Krejci857189e2020-09-01 13:26:36 +02001060ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001061lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1062 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001063{
1064 uint32_t i;
1065
1066 for (i = 0; i < set->used; ++i) {
1067 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1068 continue;
1069 }
1070
1071 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001072 if (index_p) {
1073 *index_p = i;
1074 }
1075 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001076 }
1077 }
1078
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001079 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001080}
1081
Michal Vaskoecd62de2019-11-13 12:35:11 +01001082void
1083lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001084{
1085 uint32_t orig_used, i, j;
1086
Michal Vaskod3678892020-05-21 10:06:58 +02001087 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001088
Michal Vaskod3678892020-05-21 10:06:58 +02001089 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001090 return;
1091 }
1092
Michal Vaskod3678892020-05-21 10:06:58 +02001093 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001094 memcpy(set1, set2, sizeof *set1);
1095 return;
1096 }
1097
1098 if (set1->used + set2->used > set1->size) {
1099 set1->size = set1->used + set2->used;
1100 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1101 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1102 }
1103
1104 orig_used = set1->used;
1105
1106 for (i = 0; i < set2->used; ++i) {
1107 for (j = 0; j < orig_used; ++j) {
1108 /* detect duplicities */
1109 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1110 break;
1111 }
1112 }
1113
1114 if (j == orig_used) {
1115 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1116 ++set1->used;
1117 }
1118 }
1119
Michal Vaskod3678892020-05-21 10:06:58 +02001120 lyxp_set_free_content(set2);
1121 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001122}
1123
1124/**
1125 * @brief Insert a node into a set. Context position aware.
1126 *
1127 * @param[in] set Set to use.
1128 * @param[in] node Node to insert to @p set.
1129 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1130 * @param[in] node_type Node type of @p node.
1131 * @param[in] idx Index in @p set to insert into.
1132 */
1133static void
1134set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1135{
Michal Vaskod3678892020-05-21 10:06:58 +02001136 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001137
Michal Vaskod3678892020-05-21 10:06:58 +02001138 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001139 /* first item */
1140 if (idx) {
1141 /* no real harm done, but it is a bug */
1142 LOGINT(set->ctx);
1143 idx = 0;
1144 }
1145 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1146 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1147 set->type = LYXP_SET_NODE_SET;
1148 set->used = 0;
1149 set->size = LYXP_SET_SIZE_START;
1150 set->ctx_pos = 1;
1151 set->ctx_size = 1;
1152 set->ht = NULL;
1153 } else {
1154 /* not an empty set */
1155 if (set->used == set->size) {
1156
1157 /* set is full */
1158 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1159 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1160 set->size += LYXP_SET_SIZE_STEP;
1161 }
1162
1163 if (idx > set->used) {
1164 LOGINT(set->ctx);
1165 idx = set->used;
1166 }
1167
1168 /* make space for the new node */
1169 if (idx < set->used) {
1170 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1171 }
1172 }
1173
1174 /* finally assign the value */
1175 set->val.nodes[idx].node = (struct lyd_node *)node;
1176 set->val.nodes[idx].type = node_type;
1177 set->val.nodes[idx].pos = pos;
1178 ++set->used;
1179
Michal Vasko2caefc12019-11-14 16:07:56 +01001180 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1181 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1182 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001183}
1184
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001185LY_ERR
1186lyxp_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 +02001187{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001188 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001189
1190 assert(set->type == LYXP_SET_SCNODE_SET);
1191
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001192 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
1193 set->val.scnodes[index].in_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001194 } else {
1195 if (set->used == set->size) {
1196 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 +02001197 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001198 set->size += LYXP_SET_SIZE_STEP;
1199 }
1200
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001201 index = set->used;
1202 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1203 set->val.scnodes[index].type = node_type;
1204 set->val.scnodes[index].in_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001205 ++set->used;
1206 }
1207
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001208 if (index_p) {
1209 *index_p = index;
1210 }
1211
1212 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001213}
1214
1215/**
1216 * @brief Replace a node in a set with another. Context position aware.
1217 *
1218 * @param[in] set Set to use.
1219 * @param[in] node Node to insert to @p set.
1220 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1221 * @param[in] node_type Node type of @p node.
1222 * @param[in] idx Index in @p set of the node to replace.
1223 */
1224static void
1225set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1226{
1227 assert(set && (idx < set->used));
1228
Michal Vasko2caefc12019-11-14 16:07:56 +01001229 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1230 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1231 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001232 set->val.nodes[idx].node = (struct lyd_node *)node;
1233 set->val.nodes[idx].type = node_type;
1234 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001235 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1236 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1237 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001238}
1239
1240/**
1241 * @brief Set all nodes with ctx 1 to a new unique context value.
1242 *
1243 * @param[in] set Set to modify.
1244 * @return New context value.
1245 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001246static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001247set_scnode_new_in_ctx(struct lyxp_set *set)
1248{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001249 uint32_t i;
1250 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001251
1252 assert(set->type == LYXP_SET_SCNODE_SET);
1253
1254 ret_ctx = 3;
1255retry:
1256 for (i = 0; i < set->used; ++i) {
1257 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1258 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1259 goto retry;
1260 }
1261 }
1262 for (i = 0; i < set->used; ++i) {
1263 if (set->val.scnodes[i].in_ctx == 1) {
1264 set->val.scnodes[i].in_ctx = ret_ctx;
1265 }
1266 }
1267
1268 return ret_ctx;
1269}
1270
1271/**
1272 * @brief Get unique @p node position in the data.
1273 *
1274 * @param[in] node Node to find.
1275 * @param[in] node_type Node type of @p node.
1276 * @param[in] root Root node.
1277 * @param[in] root_type Type of the XPath @p root node.
1278 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1279 * be used to increase efficiency and start the DFS from this node.
1280 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1281 * @return Node position.
1282 */
1283static uint32_t
1284get_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 +02001285 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001286{
Michal Vasko56daf732020-08-10 10:57:18 +02001287 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001288 uint32_t pos = 1;
1289
1290 assert(prev && prev_pos && !root->prev->next);
1291
1292 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1293 return 0;
1294 }
1295
1296 if (*prev) {
1297 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001298 pos = *prev_pos;
Radek Krejci1e008d22020-08-17 11:37:37 +02001299 for (top_sibling = *prev; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001300 goto dfs_search;
1301 }
1302
1303 for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) {
Michal Vasko56daf732020-08-10 10:57:18 +02001304 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001305dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001306 if (*prev && !elem) {
1307 /* resume previous DFS */
1308 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1309 LYD_TREE_DFS_continue = 0;
1310 }
1311
Michal Vasko03ff5a72019-09-11 13:49:33 +02001312 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001313 /* skip */
1314 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001315 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001316 if (elem == node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001317 break;
1318 }
Michal Vasko56daf732020-08-10 10:57:18 +02001319 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001320 }
Michal Vasko56daf732020-08-10 10:57:18 +02001321
1322 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001323 }
1324
1325 /* node found */
1326 if (elem) {
1327 break;
1328 }
1329 }
1330
1331 if (!elem) {
1332 if (!(*prev)) {
1333 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001334 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001335 return 0;
1336 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001337 /* start the search again from the beginning */
1338 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001339
Michal Vasko56daf732020-08-10 10:57:18 +02001340 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001341 pos = 1;
1342 goto dfs_search;
1343 }
1344 }
1345
1346 /* remember the last found node for next time */
1347 *prev = node;
1348 *prev_pos = pos;
1349
1350 return pos;
1351}
1352
1353/**
1354 * @brief Assign (fill) missing node positions.
1355 *
1356 * @param[in] set Set to fill positions in.
1357 * @param[in] root Context root node.
1358 * @param[in] root_type Context root type.
1359 * @return LY_ERR
1360 */
1361static LY_ERR
1362set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1363{
1364 const struct lyd_node *prev = NULL, *tmp_node;
1365 uint32_t i, tmp_pos = 0;
1366
1367 for (i = 0; i < set->used; ++i) {
1368 if (!set->val.nodes[i].pos) {
1369 tmp_node = NULL;
1370 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001371 case LYXP_NODE_META:
1372 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001373 if (!tmp_node) {
1374 LOGINT_RET(root->schema->module->ctx);
1375 }
Radek Krejci0f969882020-08-21 16:56:47 +02001376 /* fallthrough */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001377 case LYXP_NODE_ELEM:
1378 case LYXP_NODE_TEXT:
1379 if (!tmp_node) {
1380 tmp_node = set->val.nodes[i].node;
1381 }
1382 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1383 break;
1384 default:
1385 /* all roots have position 0 */
1386 break;
1387 }
1388 }
1389 }
1390
1391 return LY_SUCCESS;
1392}
1393
1394/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001395 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001396 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001397 * @param[in] meta Metadata to use.
1398 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001399 */
1400static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001401get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001402{
1403 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001404 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001405
Michal Vasko9f96a052020-03-10 09:41:45 +01001406 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001407 ++pos;
1408 }
1409
Michal Vasko9f96a052020-03-10 09:41:45 +01001410 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001411 return pos;
1412}
1413
1414/**
1415 * @brief Compare 2 nodes in respect to XPath document order.
1416 *
1417 * @param[in] item1 1st node.
1418 * @param[in] item2 2nd node.
1419 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1420 */
1421static int
1422set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1423{
Michal Vasko9f96a052020-03-10 09:41:45 +01001424 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001425
1426 if (item1->pos < item2->pos) {
1427 return -1;
1428 }
1429
1430 if (item1->pos > item2->pos) {
1431 return 1;
1432 }
1433
1434 /* node positions are equal, the fun case */
1435
1436 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1437 /* special case since text nodes are actually saved as their parents */
1438 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1439 if (item1->type == LYXP_NODE_ELEM) {
1440 assert(item2->type == LYXP_NODE_TEXT);
1441 return -1;
1442 } else {
1443 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1444 return 1;
1445 }
1446 }
1447
Michal Vasko9f96a052020-03-10 09:41:45 +01001448 /* we need meta positions now */
1449 if (item1->type == LYXP_NODE_META) {
1450 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001451 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001452 if (item2->type == LYXP_NODE_META) {
1453 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001454 }
1455
Michal Vasko9f96a052020-03-10 09:41:45 +01001456 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001457 /* check for duplicates */
1458 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001459 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001460 return 0;
1461 }
1462
Michal Vasko9f96a052020-03-10 09:41:45 +01001463 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001464 /* elem is always first, 2nd node is after it */
1465 if (item1->type == LYXP_NODE_ELEM) {
1466 assert(item2->type != LYXP_NODE_ELEM);
1467 return -1;
1468 }
1469
Michal Vasko9f96a052020-03-10 09:41:45 +01001470 /* 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 +02001471 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001472 if (((item1->type == LYXP_NODE_TEXT) &&
1473 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1474 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1475 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1476 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001477 return 1;
1478 }
1479
Michal Vasko9f96a052020-03-10 09:41:45 +01001480 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001481 /* 2nd is after 1st */
1482 return -1;
1483}
1484
1485/**
1486 * @brief Set cast for comparisons.
1487 *
1488 * @param[in] trg Target set to cast source into.
1489 * @param[in] src Source set.
1490 * @param[in] type Target set type.
1491 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001492 * @return LY_ERR
1493 */
1494static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001495set_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 +02001496{
1497 assert(src->type == LYXP_SET_NODE_SET);
1498
1499 set_init(trg, src);
1500
1501 /* insert node into target set */
1502 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1503
1504 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001505 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001506}
1507
Michal Vasko4c7763f2020-07-27 17:40:37 +02001508/**
1509 * @brief Set content canonization for comparisons.
1510 *
1511 * @param[in] trg Target set to put the canononized source into.
1512 * @param[in] src Source set.
1513 * @param[in] xp_node Source XPath node/meta to use for canonization.
1514 * @return LY_ERR
1515 */
1516static LY_ERR
1517set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1518{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001519 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001520 struct lyd_value val;
1521 struct ly_err_item *err = NULL;
1522 char *str, *ptr;
Radek Krejci857189e2020-09-01 13:26:36 +02001523 ly_bool dynamic;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001524 LY_ERR rc;
1525
1526 /* is there anything to canonize even? */
1527 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1528 /* do we have a type to use for canonization? */
1529 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1530 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1531 } else if (xp_node->type == LYXP_NODE_META) {
1532 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1533 }
1534 }
1535 if (!type) {
1536 goto fill;
1537 }
1538
1539 if (src->type == LYXP_SET_NUMBER) {
1540 /* canonize number */
1541 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1542 LOGMEM(src->ctx);
1543 return LY_EMEM;
1544 }
1545 } else {
1546 /* canonize string */
1547 str = strdup(src->val.str);
1548 }
1549
1550 /* ignore errors, the value may not satisfy schema constraints */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001551 rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_OPTS_DYNAMIC, LY_PREF_JSON, NULL, LYD_HINT_DATA,
1552 xp_node->node->schema, &val, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001553 ly_err_free(err);
1554 if (rc) {
1555 /* invalid value */
1556 free(str);
1557 goto fill;
1558 }
1559
1560 /* storing successful, now print the canonical value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001561 str = (char *)type->plugin->print(&val, LY_PREF_JSON, NULL, &dynamic);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001562
1563 /* use the canonized value */
1564 set_init(trg, src);
1565 trg->type = src->type;
1566 if (src->type == LYXP_SET_NUMBER) {
1567 trg->val.num = strtold(str, &ptr);
1568 if (dynamic) {
1569 free(str);
1570 }
1571 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1572 } else {
1573 trg->val.str = (dynamic ? str : strdup(str));
1574 }
1575 type->plugin->free(src->ctx, &val);
1576 return LY_SUCCESS;
1577
1578fill:
1579 /* no canonization needed/possible */
1580 set_fill_set(trg, src);
1581 return LY_SUCCESS;
1582}
1583
Michal Vasko03ff5a72019-09-11 13:49:33 +02001584#ifndef NDEBUG
1585
1586/**
1587 * @brief Bubble sort @p set into XPath document order.
1588 * Context position aware. Unused in the 'Release' build target.
1589 *
1590 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001591 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1592 */
1593static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001594set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001595{
1596 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001597 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001598 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001599 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001600 struct lyxp_set_node item;
1601 struct lyxp_set_hash_node hnode;
1602 uint64_t hash;
1603
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001604 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001605 return 0;
1606 }
1607
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001608 /* find first top-level node to be used as anchor for positions */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001609 for (root = set->cur_node; root->parent; root = (const struct lyd_node *)root->parent) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001610 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001611
1612 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001613 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001614 return -1;
1615 }
1616
1617 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1618 print_set_debug(set);
1619
1620 for (i = 0; i < set->used; ++i) {
1621 inverted = 0;
1622 change = 0;
1623
1624 for (j = 1; j < set->used - i; ++j) {
1625 /* compare node positions */
1626 if (inverted) {
1627 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1628 } else {
1629 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1630 }
1631
1632 /* swap if needed */
1633 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1634 change = 1;
1635
1636 item = set->val.nodes[j - 1];
1637 set->val.nodes[j - 1] = set->val.nodes[j];
1638 set->val.nodes[j] = item;
1639 } else {
1640 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1641 inverted = !inverted;
1642 }
1643 }
1644
1645 ++ret;
1646
1647 if (!change) {
1648 break;
1649 }
1650 }
1651
1652 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1653 print_set_debug(set);
1654
1655 /* check node hashes */
1656 if (set->used >= LYD_HT_MIN_ITEMS) {
1657 assert(set->ht);
1658 for (i = 0; i < set->used; ++i) {
1659 hnode.node = set->val.nodes[i].node;
1660 hnode.type = set->val.nodes[i].type;
1661
1662 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1663 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1664 hash = dict_hash_multi(hash, NULL, 0);
1665
1666 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1667 }
1668 }
1669
1670 return ret - 1;
1671}
1672
1673/**
1674 * @brief Remove duplicate entries in a sorted node set.
1675 *
1676 * @param[in] set Sorted set to check.
1677 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1678 */
1679static LY_ERR
1680set_sorted_dup_node_clean(struct lyxp_set *set)
1681{
1682 uint32_t i = 0;
1683 LY_ERR ret = LY_SUCCESS;
1684
1685 if (set->used > 1) {
1686 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001687 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1688 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001689 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001690 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001691 }
Michal Vasko57eab132019-09-24 11:46:26 +02001692 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001693 }
1694 }
1695
Michal Vasko2caefc12019-11-14 16:07:56 +01001696 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001697 return ret;
1698}
1699
1700#endif
1701
1702/**
1703 * @brief Merge 2 sorted sets into one.
1704 *
1705 * @param[in,out] trg Set to merge into. Duplicates are removed.
1706 * @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 +02001707 * @return LY_ERR
1708 */
1709static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001710set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001711{
1712 uint32_t i, j, k, count, dup_count;
1713 int cmp;
1714 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001715
Michal Vaskod3678892020-05-21 10:06:58 +02001716 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001717 return LY_EINVAL;
1718 }
1719
Michal Vaskod3678892020-05-21 10:06:58 +02001720 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001721 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001722 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001723 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001724 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001725 return LY_SUCCESS;
1726 }
1727
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001728 /* find first top-level node to be used as anchor for positions */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001729 for (root = trg->cur_node; root->parent; root = (const struct lyd_node *)root->parent) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001730 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001731
1732 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001733 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001734 return LY_EINT;
1735 }
1736
1737#ifndef NDEBUG
1738 LOGDBG(LY_LDGXPATH, "MERGE target");
1739 print_set_debug(trg);
1740 LOGDBG(LY_LDGXPATH, "MERGE source");
1741 print_set_debug(src);
1742#endif
1743
1744 /* make memory for the merge (duplicates are not detected yet, so space
1745 * will likely be wasted on them, too bad) */
1746 if (trg->size - trg->used < src->used) {
1747 trg->size = trg->used + src->used;
1748
1749 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1750 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1751 }
1752
1753 i = 0;
1754 j = 0;
1755 count = 0;
1756 dup_count = 0;
1757 do {
1758 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1759 if (!cmp) {
1760 if (!count) {
1761 /* duplicate, just skip it */
1762 ++i;
1763 ++j;
1764 } else {
1765 /* we are copying something already, so let's copy the duplicate too,
1766 * we are hoping that afterwards there are some more nodes to
1767 * copy and this way we can copy them all together */
1768 ++count;
1769 ++dup_count;
1770 ++i;
1771 ++j;
1772 }
1773 } else if (cmp < 0) {
1774 /* inserting src node into trg, just remember it for now */
1775 ++count;
1776 ++i;
1777
1778 /* insert the hash now */
1779 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1780 } else if (count) {
1781copy_nodes:
1782 /* time to actually copy the nodes, we have found the largest block of nodes */
1783 memmove(&trg->val.nodes[j + (count - dup_count)],
1784 &trg->val.nodes[j],
1785 (trg->used - j) * sizeof *trg->val.nodes);
1786 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1787
1788 trg->used += count - dup_count;
1789 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1790 j += count - dup_count;
1791
1792 count = 0;
1793 dup_count = 0;
1794 } else {
1795 ++j;
1796 }
1797 } while ((i < src->used) && (j < trg->used));
1798
1799 if ((i < src->used) || count) {
1800 /* insert all the hashes first */
1801 for (k = i; k < src->used; ++k) {
1802 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1803 }
1804
1805 /* loop ended, but we need to copy something at trg end */
1806 count += src->used - i;
1807 i = src->used;
1808 goto copy_nodes;
1809 }
1810
1811 /* we are inserting hashes before the actual node insert, which causes
1812 * situations when there were initially not enough items for a hash table,
1813 * but even after some were inserted, hash table was not created (during
1814 * insertion the number of items is not updated yet) */
1815 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1816 set_insert_node_hash(trg, NULL, 0);
1817 }
1818
1819#ifndef NDEBUG
1820 LOGDBG(LY_LDGXPATH, "MERGE result");
1821 print_set_debug(trg);
1822#endif
1823
Michal Vaskod3678892020-05-21 10:06:58 +02001824 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001825 return LY_SUCCESS;
1826}
1827
1828/*
1829 * (re)parse functions
1830 *
1831 * Parse functions parse the expression into
1832 * tokens (syntactic analysis).
1833 *
1834 * Reparse functions perform semantic analysis
1835 * (do not save the result, just a check) of
1836 * the expression and fill repeat indices.
1837 */
1838
Michal Vasko14676352020-05-29 11:35:55 +02001839LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001840lyxp_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 +02001841{
Michal Vasko004d3152020-06-11 19:59:22 +02001842 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001843 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001844 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1845 }
Michal Vasko14676352020-05-29 11:35:55 +02001846 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001847 }
1848
Michal Vasko004d3152020-06-11 19:59:22 +02001849 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001850 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001851 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko69730152020-10-09 16:30:07 +02001852 lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001853 }
Michal Vasko14676352020-05-29 11:35:55 +02001854 return LY_ENOT;
1855 }
1856
1857 return LY_SUCCESS;
1858}
1859
Michal Vasko004d3152020-06-11 19:59:22 +02001860LY_ERR
1861lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1862{
1863 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1864
1865 /* skip the token */
1866 ++(*tok_idx);
1867
1868 return LY_SUCCESS;
1869}
1870
Michal Vasko14676352020-05-29 11:35:55 +02001871/* just like lyxp_check_token() but tests for 2 tokens */
1872static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001873exp_check_token2(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok1,
Radek Krejci0f969882020-08-21 16:56:47 +02001874 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001875{
Michal Vasko004d3152020-06-11 19:59:22 +02001876 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001877 if (ctx) {
1878 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1879 }
1880 return LY_EINCOMPLETE;
1881 }
1882
Michal Vasko004d3152020-06-11 19:59:22 +02001883 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001884 if (ctx) {
1885 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko69730152020-10-09 16:30:07 +02001886 lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001887 }
1888 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001889 }
1890
1891 return LY_SUCCESS;
1892}
1893
1894/**
1895 * @brief Stack operation push on the repeat array.
1896 *
1897 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001898 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001899 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1900 */
1901static void
Michal Vasko004d3152020-06-11 19:59:22 +02001902exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001903{
1904 uint16_t i;
1905
Michal Vasko004d3152020-06-11 19:59:22 +02001906 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001907 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001908 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1909 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1910 exp->repeat[tok_idx][i] = repeat_op_idx;
1911 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001912 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001913 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1914 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1915 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001916 }
1917}
1918
1919/**
1920 * @brief Reparse Predicate. Logs directly on error.
1921 *
1922 * [7] Predicate ::= '[' Expr ']'
1923 *
1924 * @param[in] ctx Context for logging.
1925 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001926 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001927 * @return LY_ERR
1928 */
1929static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001930reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001931{
1932 LY_ERR rc;
1933
Michal Vasko004d3152020-06-11 19:59:22 +02001934 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001935 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001936 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001937
Michal Vasko004d3152020-06-11 19:59:22 +02001938 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001939 LY_CHECK_RET(rc);
1940
Michal Vasko004d3152020-06-11 19:59:22 +02001941 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001942 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001943 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001944
1945 return LY_SUCCESS;
1946}
1947
1948/**
1949 * @brief Reparse RelativeLocationPath. Logs directly on error.
1950 *
1951 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1952 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1953 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1954 *
1955 * @param[in] ctx Context for logging.
1956 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001957 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001958 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1959 */
1960static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001961reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001962{
1963 LY_ERR rc;
1964
Michal Vasko004d3152020-06-11 19:59:22 +02001965 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001966 LY_CHECK_RET(rc);
1967
1968 goto step;
1969 do {
1970 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001971 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001972
Michal Vasko004d3152020-06-11 19:59:22 +02001973 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001974 LY_CHECK_RET(rc);
1975step:
1976 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001977 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001978 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001979 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001980 break;
1981
1982 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001983 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001984 break;
1985
1986 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02001987 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001988
Michal Vasko004d3152020-06-11 19:59:22 +02001989 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001990 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001991 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001992 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko69730152020-10-09 16:30:07 +02001993 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001994 return LY_EVALID;
1995 }
Radek Krejci0f969882020-08-21 16:56:47 +02001996 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001997 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02001998 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001999 goto reparse_predicate;
2000 break;
2001
2002 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002003 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002004
2005 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002006 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002007 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002008 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002009
2010 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002011 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002012 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002013 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002014
2015reparse_predicate:
2016 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002017 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2018 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002019 LY_CHECK_RET(rc);
2020 }
2021 break;
2022 default:
2023 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko69730152020-10-09 16:30:07 +02002024 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002025 return LY_EVALID;
2026 }
Michal Vasko004d3152020-06-11 19:59:22 +02002027 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002028
2029 return LY_SUCCESS;
2030}
2031
2032/**
2033 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2034 *
2035 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2036 *
2037 * @param[in] ctx Context for logging.
2038 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002039 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002040 * @return LY_ERR
2041 */
2042static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002043reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002044{
2045 LY_ERR rc;
2046
Michal Vasko004d3152020-06-11 19:59:22 +02002047 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 +02002048
2049 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002050 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002051 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002052 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002053
Michal Vasko004d3152020-06-11 19:59:22 +02002054 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002055 return LY_SUCCESS;
2056 }
Michal Vasko004d3152020-06-11 19:59:22 +02002057 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002058 case LYXP_TOKEN_DOT:
2059 case LYXP_TOKEN_DDOT:
2060 case LYXP_TOKEN_AT:
2061 case LYXP_TOKEN_NAMETEST:
2062 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002063 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002064 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002065 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002066 default:
2067 break;
2068 }
2069
Michal Vasko03ff5a72019-09-11 13:49:33 +02002070 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002071 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002072 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002073
Michal Vasko004d3152020-06-11 19:59:22 +02002074 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002075 LY_CHECK_RET(rc);
2076 }
2077
2078 return LY_SUCCESS;
2079}
2080
2081/**
2082 * @brief Reparse FunctionCall. Logs directly on error.
2083 *
2084 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2085 *
2086 * @param[in] ctx Context for logging.
2087 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002088 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002089 * @return LY_ERR
2090 */
2091static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002092reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002093{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002094 int8_t min_arg_count = -1;
2095 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002096 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002097 LY_ERR rc;
2098
Michal Vasko004d3152020-06-11 19:59:22 +02002099 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002100 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002101 func_tok_idx = *tok_idx;
2102 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002103 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002104 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002105 min_arg_count = 1;
2106 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002107 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002108 min_arg_count = 1;
2109 max_arg_count = 1;
2110 }
2111 break;
2112 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002113 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002114 min_arg_count = 1;
2115 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002116 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002117 min_arg_count = 0;
2118 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002119 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002120 min_arg_count = 0;
2121 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002122 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002123 min_arg_count = 0;
2124 max_arg_count = 0;
2125 }
2126 break;
2127 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002128 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002129 min_arg_count = 1;
2130 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002131 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002132 min_arg_count = 0;
2133 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002134 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002135 min_arg_count = 1;
2136 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002137 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002138 min_arg_count = 1;
2139 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002140 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002141 min_arg_count = 1;
2142 max_arg_count = 1;
2143 }
2144 break;
2145 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002146 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002147 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002148 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002149 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002150 min_arg_count = 0;
2151 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002152 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002153 min_arg_count = 0;
2154 max_arg_count = 1;
2155 }
2156 break;
2157 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002158 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002159 min_arg_count = 1;
2160 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002161 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002162 min_arg_count = 1;
2163 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002164 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002165 min_arg_count = 0;
2166 max_arg_count = 0;
2167 }
2168 break;
2169 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002170 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002171 min_arg_count = 2;
2172 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002173 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002174 min_arg_count = 0;
2175 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002176 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002177 min_arg_count = 2;
2178 max_arg_count = 2;
2179 }
2180 break;
2181 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002182 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002183 min_arg_count = 2;
2184 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002185 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002186 min_arg_count = 3;
2187 max_arg_count = 3;
2188 }
2189 break;
2190 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002191 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002192 min_arg_count = 0;
2193 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002194 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002195 min_arg_count = 1;
2196 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002197 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002198 min_arg_count = 2;
2199 max_arg_count = 2;
2200 }
2201 break;
2202 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002203 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002204 min_arg_count = 2;
2205 max_arg_count = 2;
2206 }
2207 break;
2208 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002209 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002210 min_arg_count = 2;
2211 max_arg_count = 2;
2212 }
2213 break;
2214 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002215 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002216 min_arg_count = 0;
2217 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002218 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002219 min_arg_count = 0;
2220 max_arg_count = 1;
2221 }
2222 break;
2223 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002224 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002225 min_arg_count = 0;
2226 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002227 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002228 min_arg_count = 2;
2229 max_arg_count = 2;
2230 }
2231 break;
2232 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002233 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002234 min_arg_count = 2;
2235 max_arg_count = 2;
2236 }
2237 break;
2238 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002239 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002240 min_arg_count = 2;
2241 max_arg_count = 2;
2242 }
2243 break;
2244 }
2245 if (min_arg_count == -1) {
Michal Vasko004d3152020-06-11 19:59:22 +02002246 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 +02002247 return LY_EINVAL;
2248 }
Michal Vasko004d3152020-06-11 19:59:22 +02002249 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002250
2251 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002252 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002253 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002254 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002255
2256 /* ( Expr ( ',' Expr )* )? */
2257 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002258 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002259 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002260 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002261 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002262 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002263 LY_CHECK_RET(rc);
2264 }
Michal Vasko004d3152020-06-11 19:59:22 +02002265 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2266 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002267
2268 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002269 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002270 LY_CHECK_RET(rc);
2271 }
2272
2273 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002274 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002275 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002276 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002277
Radek Krejci857189e2020-09-01 13:26:36 +02002278 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Michal Vasko004d3152020-06-11 19:59:22 +02002279 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 +02002280 &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002281 return LY_EVALID;
2282 }
2283
2284 return LY_SUCCESS;
2285}
2286
2287/**
2288 * @brief Reparse PathExpr. Logs directly on error.
2289 *
2290 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2291 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2292 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2293 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2294 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2295 *
2296 * @param[in] ctx Context for logging.
2297 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002298 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002299 * @return LY_ERR
2300 */
2301static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002302reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002303{
2304 LY_ERR rc;
2305
Michal Vasko004d3152020-06-11 19:59:22 +02002306 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002307 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002308 }
2309
Michal Vasko004d3152020-06-11 19:59:22 +02002310 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002311 case LYXP_TOKEN_PAR1:
2312 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002313 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002314
Michal Vasko004d3152020-06-11 19:59:22 +02002315 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002316 LY_CHECK_RET(rc);
2317
Michal Vasko004d3152020-06-11 19:59:22 +02002318 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002319 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002320 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002321 goto predicate;
2322 break;
2323 case LYXP_TOKEN_DOT:
2324 case LYXP_TOKEN_DDOT:
2325 case LYXP_TOKEN_AT:
2326 case LYXP_TOKEN_NAMETEST:
2327 case LYXP_TOKEN_NODETYPE:
2328 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002329 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002330 LY_CHECK_RET(rc);
2331 break;
2332 case LYXP_TOKEN_FUNCNAME:
2333 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002334 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002335 LY_CHECK_RET(rc);
2336 goto predicate;
2337 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002338 case LYXP_TOKEN_OPER_PATH:
2339 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002340 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002341 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002342 LY_CHECK_RET(rc);
2343 break;
2344 case LYXP_TOKEN_LITERAL:
2345 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002346 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002347 goto predicate;
2348 break;
2349 case LYXP_TOKEN_NUMBER:
2350 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002351 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002352 goto predicate;
2353 break;
2354 default:
2355 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko69730152020-10-09 16:30:07 +02002356 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002357 return LY_EVALID;
2358 }
2359
2360 return LY_SUCCESS;
2361
2362predicate:
2363 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002364 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2365 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002366 LY_CHECK_RET(rc);
2367 }
2368
2369 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002370 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002371
2372 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002373 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002374
Michal Vasko004d3152020-06-11 19:59:22 +02002375 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002376 LY_CHECK_RET(rc);
2377 }
2378
2379 return LY_SUCCESS;
2380}
2381
2382/**
2383 * @brief Reparse UnaryExpr. Logs directly on error.
2384 *
2385 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2386 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2387 *
2388 * @param[in] ctx Context for logging.
2389 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002390 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002391 * @return LY_ERR
2392 */
2393static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002394reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002395{
2396 uint16_t prev_exp;
2397 LY_ERR rc;
2398
2399 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002400 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002401 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2402 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002403 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002404 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002405 }
2406
2407 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002408 prev_exp = *tok_idx;
2409 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002410 LY_CHECK_RET(rc);
2411
2412 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002413 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002414 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002415 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002416
Michal Vasko004d3152020-06-11 19:59:22 +02002417 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002418 LY_CHECK_RET(rc);
2419 }
2420
2421 return LY_SUCCESS;
2422}
2423
2424/**
2425 * @brief Reparse AdditiveExpr. Logs directly on error.
2426 *
2427 * [15] AdditiveExpr ::= MultiplicativeExpr
2428 * | AdditiveExpr '+' MultiplicativeExpr
2429 * | AdditiveExpr '-' MultiplicativeExpr
2430 * [16] MultiplicativeExpr ::= UnaryExpr
2431 * | MultiplicativeExpr '*' UnaryExpr
2432 * | MultiplicativeExpr 'div' UnaryExpr
2433 * | MultiplicativeExpr 'mod' UnaryExpr
2434 *
2435 * @param[in] ctx Context for logging.
2436 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002437 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002438 * @return LY_ERR
2439 */
2440static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002441reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002442{
2443 uint16_t prev_add_exp, prev_mul_exp;
2444 LY_ERR rc;
2445
Michal Vasko004d3152020-06-11 19:59:22 +02002446 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002447 goto reparse_multiplicative_expr;
2448
2449 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002450 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2451 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002452 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002453 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002454
2455reparse_multiplicative_expr:
2456 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002457 prev_mul_exp = *tok_idx;
2458 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002459 LY_CHECK_RET(rc);
2460
2461 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002462 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2463 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002464 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002465 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002466
Michal Vasko004d3152020-06-11 19:59:22 +02002467 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002468 LY_CHECK_RET(rc);
2469 }
2470 }
2471
2472 return LY_SUCCESS;
2473}
2474
2475/**
2476 * @brief Reparse EqualityExpr. Logs directly on error.
2477 *
2478 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2479 * | EqualityExpr '!=' RelationalExpr
2480 * [14] RelationalExpr ::= AdditiveExpr
2481 * | RelationalExpr '<' AdditiveExpr
2482 * | RelationalExpr '>' AdditiveExpr
2483 * | RelationalExpr '<=' AdditiveExpr
2484 * | RelationalExpr '>=' AdditiveExpr
2485 *
2486 * @param[in] ctx Context for logging.
2487 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002488 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002489 * @return LY_ERR
2490 */
2491static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002492reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002493{
2494 uint16_t prev_eq_exp, prev_rel_exp;
2495 LY_ERR rc;
2496
Michal Vasko004d3152020-06-11 19:59:22 +02002497 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002498 goto reparse_additive_expr;
2499
2500 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002501 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002502 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002503 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002504
2505reparse_additive_expr:
2506 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002507 prev_rel_exp = *tok_idx;
2508 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002509 LY_CHECK_RET(rc);
2510
2511 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002512 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002513 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002514 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002515
Michal Vasko004d3152020-06-11 19:59:22 +02002516 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002517 LY_CHECK_RET(rc);
2518 }
2519 }
2520
2521 return LY_SUCCESS;
2522}
2523
2524/**
2525 * @brief Reparse OrExpr. Logs directly on error.
2526 *
2527 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2528 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2529 *
2530 * @param[in] ctx Context for logging.
2531 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002532 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002533 * @return LY_ERR
2534 */
2535static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002536reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002537{
2538 uint16_t prev_or_exp, prev_and_exp;
2539 LY_ERR rc;
2540
Michal Vasko004d3152020-06-11 19:59:22 +02002541 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002542 goto reparse_equality_expr;
2543
2544 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002545 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 +02002546 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002547 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002548
2549reparse_equality_expr:
2550 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002551 prev_and_exp = *tok_idx;
2552 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002553 LY_CHECK_RET(rc);
2554
2555 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002556 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 +02002557 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002558 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002559
Michal Vasko004d3152020-06-11 19:59:22 +02002560 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002561 LY_CHECK_RET(rc);
2562 }
2563 }
2564
2565 return LY_SUCCESS;
2566}
Radek Krejcib1646a92018-11-02 16:08:26 +01002567
2568/**
2569 * @brief Parse NCName.
2570 *
2571 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002572 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002573 */
Radek Krejcid4270262019-01-07 15:07:25 +01002574static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002575parse_ncname(const char *ncname)
2576{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002577 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002578 size_t size;
2579 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002580
2581 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2582 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2583 return len;
2584 }
2585
2586 do {
2587 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002588 if (!*ncname) {
2589 break;
2590 }
Radek Krejcid4270262019-01-07 15:07:25 +01002591 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002592 } while (is_xmlqnamechar(uc) && (uc != ':'));
2593
2594 return len;
2595}
2596
2597/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002598 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002599 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002600 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002601 * @param[in] exp Expression to use.
2602 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002603 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002604 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002605 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002606 */
2607static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002608exp_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 +01002609{
2610 uint32_t prev;
2611
2612 if (exp->used == exp->size) {
2613 prev = exp->size;
2614 exp->size += LYXP_EXPR_SIZE_STEP;
2615 if (prev > exp->size) {
2616 LOGINT(ctx);
2617 return LY_EINT;
2618 }
2619
2620 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2621 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2622 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2623 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2624 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2625 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2626 }
2627
2628 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002629 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002630 exp->tok_len[exp->used] = tok_len;
2631 ++exp->used;
2632 return LY_SUCCESS;
2633}
2634
2635void
Michal Vasko14676352020-05-29 11:35:55 +02002636lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002637{
2638 uint16_t i;
2639
2640 if (!expr) {
2641 return;
2642 }
2643
2644 lydict_remove(ctx, expr->expr);
2645 free(expr->tokens);
2646 free(expr->tok_pos);
2647 free(expr->tok_len);
2648 if (expr->repeat) {
2649 for (i = 0; i < expr->used; ++i) {
2650 free(expr->repeat[i]);
2651 }
2652 }
2653 free(expr->repeat);
2654 free(expr);
2655}
2656
Radek Krejcif03a9e22020-09-18 20:09:31 +02002657LY_ERR
2658lyxp_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 +01002659{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002660 LY_ERR ret = LY_SUCCESS;
2661 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002662 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002663 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002664 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002665 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002666
Radek Krejcif03a9e22020-09-18 20:09:31 +02002667 assert(expr_p);
2668
2669 if (!expr_str[0]) {
Michal Vasko004d3152020-06-11 19:59:22 +02002670 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002671 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002672 }
2673
2674 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002675 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002676 }
2677 if (expr_len > UINT16_MAX) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002678 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
2679 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002680 }
2681
2682 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002683 expr = calloc(1, sizeof *expr);
2684 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2685 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2686 expr->used = 0;
2687 expr->size = LYXP_EXPR_SIZE_START;
2688 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2689 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002690
Radek Krejcif03a9e22020-09-18 20:09:31 +02002691 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2692 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002693
Radek Krejcif03a9e22020-09-18 20:09:31 +02002694 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2695 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002696
Michal Vasko004d3152020-06-11 19:59:22 +02002697 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002698 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002699
Radek Krejcif03a9e22020-09-18 20:09:31 +02002700 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002701 ++parsed;
2702 }
2703
2704 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002705 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002706
2707 /* '(' */
2708 tok_len = 1;
2709 tok_type = LYXP_TOKEN_PAR1;
2710
Radek Krejcif03a9e22020-09-18 20:09:31 +02002711 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002712 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002713 if (((expr->tok_len[expr->used - 1] == 4) &&
2714 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2715 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2716 ((expr->tok_len[expr->used - 1] == 7) &&
2717 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002718 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002719 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002720 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002721 }
2722 prev_function_check = 0;
2723 }
2724
Radek Krejcif03a9e22020-09-18 20:09:31 +02002725 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002726
2727 /* ')' */
2728 tok_len = 1;
2729 tok_type = LYXP_TOKEN_PAR2;
2730
Radek Krejcif03a9e22020-09-18 20:09:31 +02002731 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002732
2733 /* '[' */
2734 tok_len = 1;
2735 tok_type = LYXP_TOKEN_BRACK1;
2736
Radek Krejcif03a9e22020-09-18 20:09:31 +02002737 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002738
2739 /* ']' */
2740 tok_len = 1;
2741 tok_type = LYXP_TOKEN_BRACK2;
2742
Radek Krejcif03a9e22020-09-18 20:09:31 +02002743 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002744
2745 /* '..' */
2746 tok_len = 2;
2747 tok_type = LYXP_TOKEN_DDOT;
2748
Radek Krejcif03a9e22020-09-18 20:09:31 +02002749 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002750
2751 /* '.' */
2752 tok_len = 1;
2753 tok_type = LYXP_TOKEN_DOT;
2754
Radek Krejcif03a9e22020-09-18 20:09:31 +02002755 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002756
2757 /* '@' */
2758 tok_len = 1;
2759 tok_type = LYXP_TOKEN_AT;
2760
Radek Krejcif03a9e22020-09-18 20:09:31 +02002761 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002762
2763 /* ',' */
2764 tok_len = 1;
2765 tok_type = LYXP_TOKEN_COMMA;
2766
Radek Krejcif03a9e22020-09-18 20:09:31 +02002767 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002768
2769 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002770 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2771 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Michal Vasko69730152020-10-09 16:30:07 +02002772 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
2773 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002774 ++tok_len;
2775 tok_type = LYXP_TOKEN_LITERAL;
2776
Radek Krejcif03a9e22020-09-18 20:09:31 +02002777 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002778
2779 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002780 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2781 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Michal Vasko69730152020-10-09 16:30:07 +02002782 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
2783 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002784 ++tok_len;
2785 tok_type = LYXP_TOKEN_LITERAL;
2786
Radek Krejcif03a9e22020-09-18 20:09:31 +02002787 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002788
2789 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002790 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2791 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002792 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002793 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002794 }
2795 tok_type = LYXP_TOKEN_NUMBER;
2796
Radek Krejcif03a9e22020-09-18 20:09:31 +02002797 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002798
2799 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002800 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002801 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002802 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002803 } else {
2804 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002805 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002806 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002807
Radek Krejcif03a9e22020-09-18 20:09:31 +02002808 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002809
Michal Vasko3e48bf32020-06-01 08:39:07 +02002810 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002811 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002812 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2813
Radek Krejcif03a9e22020-09-18 20:09:31 +02002814 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002815
2816 /* Operator '<=', '>=' */
2817 tok_len = 2;
2818 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002819
Radek Krejcif03a9e22020-09-18 20:09:31 +02002820 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002821
2822 /* Operator '|' */
2823 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002824 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002825
Radek Krejcif03a9e22020-09-18 20:09:31 +02002826 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002827
2828 /* Operator '+', '-' */
2829 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002830 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002831
Radek Krejcif03a9e22020-09-18 20:09:31 +02002832 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002833
Michal Vasko3e48bf32020-06-01 08:39:07 +02002834 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002835 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002836 tok_type = LYXP_TOKEN_OPER_EQUAL;
2837
Radek Krejcif03a9e22020-09-18 20:09:31 +02002838 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002839
2840 /* Operator '<', '>' */
2841 tok_len = 1;
2842 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002843
Michal Vasko69730152020-10-09 16:30:07 +02002844 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2845 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2846 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2847 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2848 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2849 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2850 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2851 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2852 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2853 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2854 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2855 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002856
2857 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002858 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002859 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002860 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002861
Radek Krejcif03a9e22020-09-18 20:09:31 +02002862 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002863 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002864 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002865
Radek Krejcif03a9e22020-09-18 20:09:31 +02002866 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002867 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002868 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002869
Radek Krejcif03a9e22020-09-18 20:09:31 +02002870 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002871 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002872 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002873
2874 } else if (prev_function_check) {
Michal Vasko53078572019-05-24 08:50:15 +02002875 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 +02002876 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 +02002877 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002878 goto error;
2879 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002880 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr_str);
2881 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002882 goto error;
2883 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002884 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002885
2886 /* NameTest '*' */
2887 tok_len = 1;
2888 tok_type = LYXP_TOKEN_NAMETEST;
2889
2890 } else {
2891
2892 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002893 long int ncname_len = parse_ncname(&expr_str[parsed]);
2894 LY_CHECK_ERR_GOTO(ncname_len < 0,
Michal Vasko69730152020-10-09 16:30:07 +02002895 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
2896 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002897 tok_len = ncname_len;
2898
Radek Krejcif03a9e22020-09-18 20:09:31 +02002899 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002900 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002901 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002902 ++tok_len;
2903 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002904 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
2905 LY_CHECK_ERR_GOTO(ncname_len < 0,
Michal Vasko69730152020-10-09 16:30:07 +02002906 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
2907 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002908 tok_len += ncname_len;
2909 }
2910 /* remove old flag to prevent ambiguities */
2911 prev_function_check = 0;
2912 tok_type = LYXP_TOKEN_NAMETEST;
2913 } else {
2914 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2915 prev_function_check = 1;
2916 tok_type = LYXP_TOKEN_NAMETEST;
2917 }
2918 }
2919
2920 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002921 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002922 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002923 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002924 ++parsed;
2925 }
2926
Radek Krejcif03a9e22020-09-18 20:09:31 +02002927 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002928
Michal Vasko004d3152020-06-11 19:59:22 +02002929 if (reparse) {
2930 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002931 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2932 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002933
Michal Vasko004d3152020-06-11 19:59:22 +02002934 /* fill repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002935 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error);
2936 if (expr->used > tok_idx) {
Michal Vasko004d3152020-06-11 19:59:22 +02002937 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 +02002938 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002939 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002940 goto error;
2941 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002942 }
2943
Radek Krejcif03a9e22020-09-18 20:09:31 +02002944 print_expr_struct_debug(expr);
2945 *expr_p = expr;
2946 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002947
2948error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002949 lyxp_expr_free(ctx, expr);
2950 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002951}
2952
Michal Vasko1734be92020-09-22 08:55:10 +02002953LY_ERR
2954lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02002955{
Michal Vasko1734be92020-09-22 08:55:10 +02002956 LY_ERR ret = LY_SUCCESS;
2957 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02002958 uint32_t i, j;
2959
Michal Vasko7f45cf22020-10-01 12:49:44 +02002960 if (!exp) {
2961 goto cleanup;
2962 }
2963
Michal Vasko004d3152020-06-11 19:59:22 +02002964 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02002965 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002966
2967 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
Michal Vasko1734be92020-09-22 08:55:10 +02002968 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002969 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2970
2971 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
Michal Vasko1734be92020-09-22 08:55:10 +02002972 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002973 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2974
2975 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
Michal Vasko1734be92020-09-22 08:55:10 +02002976 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002977 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2978
2979 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002980 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002981 for (i = 0; i < exp->used; ++i) {
2982 if (!exp->repeat[i]) {
2983 dup->repeat[i] = NULL;
2984 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02002985 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002986 /* the ending 0 as well */
2987 ++j;
2988
Michal Vasko99c71642020-07-03 13:33:36 +02002989 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002990 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002991 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
2992 dup->repeat[i][j - 1] = 0;
2993 }
2994 }
2995
2996 dup->used = exp->used;
2997 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02002998 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002999
Michal Vasko1734be92020-09-22 08:55:10 +02003000cleanup:
3001 if (ret) {
3002 lyxp_expr_free(ctx, dup);
3003 } else {
3004 *dup_p = dup;
3005 }
3006 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003007}
3008
Michal Vasko03ff5a72019-09-11 13:49:33 +02003009/*
3010 * warn functions
3011 *
3012 * Warn functions check specific reasonable conditions for schema XPath
3013 * and print a warning if they are not satisfied.
3014 */
3015
3016/**
3017 * @brief Get the last-added schema node that is currently in the context.
3018 *
3019 * @param[in] set Set to search in.
3020 * @return Last-added schema context node, NULL if no node is in context.
3021 */
3022static struct lysc_node *
3023warn_get_scnode_in_ctx(struct lyxp_set *set)
3024{
3025 uint32_t i;
3026
3027 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3028 return NULL;
3029 }
3030
3031 i = set->used;
3032 do {
3033 --i;
3034 if (set->val.scnodes[i].in_ctx == 1) {
3035 /* if there are more, simply return the first found (last added) */
3036 return set->val.scnodes[i].scnode;
3037 }
3038 } while (i);
3039
3040 return NULL;
3041}
3042
3043/**
3044 * @brief Test whether a type is numeric - integer type or decimal64.
3045 *
3046 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003047 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003048 */
Radek Krejci857189e2020-09-01 13:26:36 +02003049static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003050warn_is_numeric_type(struct lysc_type *type)
3051{
3052 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003053 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003054 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003055
3056 switch (type->basetype) {
3057 case LY_TYPE_DEC64:
3058 case LY_TYPE_INT8:
3059 case LY_TYPE_UINT8:
3060 case LY_TYPE_INT16:
3061 case LY_TYPE_UINT16:
3062 case LY_TYPE_INT32:
3063 case LY_TYPE_UINT32:
3064 case LY_TYPE_INT64:
3065 case LY_TYPE_UINT64:
3066 return 1;
3067 case LY_TYPE_UNION:
3068 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003069 LY_ARRAY_FOR(uni->types, u) {
3070 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003071 if (ret) {
3072 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003073 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003074 }
3075 }
3076 /* did not find any suitable type */
3077 return 0;
3078 case LY_TYPE_LEAFREF:
3079 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3080 default:
3081 return 0;
3082 }
3083}
3084
3085/**
3086 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3087 *
3088 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003089 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003090 */
Radek Krejci857189e2020-09-01 13:26:36 +02003091static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003092warn_is_string_type(struct lysc_type *type)
3093{
3094 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003095 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003096 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003097
3098 switch (type->basetype) {
3099 case LY_TYPE_BITS:
3100 case LY_TYPE_ENUM:
3101 case LY_TYPE_IDENT:
3102 case LY_TYPE_INST:
3103 case LY_TYPE_STRING:
3104 return 1;
3105 case LY_TYPE_UNION:
3106 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003107 LY_ARRAY_FOR(uni->types, u) {
3108 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003109 if (ret) {
3110 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003111 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003112 }
3113 }
3114 /* did not find any suitable type */
3115 return 0;
3116 case LY_TYPE_LEAFREF:
3117 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3118 default:
3119 return 0;
3120 }
3121}
3122
3123/**
3124 * @brief Test whether a type is one specific type.
3125 *
3126 * @param[in] type Type to test.
3127 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003128 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003129 */
Radek Krejci857189e2020-09-01 13:26:36 +02003130static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003131warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3132{
3133 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003134 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003135 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003136
3137 if (type->basetype == base) {
3138 return 1;
3139 } else if (type->basetype == LY_TYPE_UNION) {
3140 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003141 LY_ARRAY_FOR(uni->types, u) {
3142 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003143 if (ret) {
3144 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003145 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003146 }
3147 }
3148 /* did not find any suitable type */
3149 return 0;
3150 } else if (type->basetype == LY_TYPE_LEAFREF) {
3151 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3152 }
3153
3154 return 0;
3155}
3156
3157/**
3158 * @brief Get next type of a (union) type.
3159 *
3160 * @param[in] type Base type.
3161 * @param[in] prev_type Previously returned type.
3162 * @return Next type or NULL.
3163 */
3164static struct lysc_type *
3165warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3166{
3167 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003168 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003169 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003170
3171 switch (type->basetype) {
3172 case LY_TYPE_UNION:
3173 uni = (struct lysc_type_union *)type;
3174 if (!prev_type) {
3175 return uni->types[0];
3176 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003177 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003178 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003179 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003180 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003181 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003182 found = 1;
3183 }
3184 }
3185 return NULL;
3186 default:
3187 if (prev_type) {
3188 assert(type == prev_type);
3189 return NULL;
3190 } else {
3191 return type;
3192 }
3193 }
3194}
3195
3196/**
3197 * @brief Test whether 2 types have a common type.
3198 *
3199 * @param[in] type1 First type.
3200 * @param[in] type2 Second type.
3201 * @return 1 if they do, 0 otherwise.
3202 */
3203static int
3204warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3205{
3206 struct lysc_type *t1, *rt1, *t2, *rt2;
3207
3208 t1 = NULL;
3209 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3210 if (t1->basetype == LY_TYPE_LEAFREF) {
3211 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3212 } else {
3213 rt1 = t1;
3214 }
3215
3216 t2 = NULL;
3217 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3218 if (t2->basetype == LY_TYPE_LEAFREF) {
3219 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3220 } else {
3221 rt2 = t2;
3222 }
3223
3224 if (rt2->basetype == rt1->basetype) {
3225 /* match found */
3226 return 1;
3227 }
3228 }
3229 }
3230
3231 return 0;
3232}
3233
3234/**
3235 * @brief Check both operands of comparison operators.
3236 *
3237 * @param[in] ctx Context for errors.
3238 * @param[in] set1 First operand set.
3239 * @param[in] set2 Second operand set.
3240 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3241 * @param[in] expr Start of the expression to print with the warning.
3242 * @param[in] tok_pos Token position.
3243 */
3244static void
Radek Krejci857189e2020-09-01 13:26:36 +02003245warn_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 +02003246{
3247 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003248 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003249
3250 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3251 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3252
3253 if (!node1 && !node2) {
3254 /* no node-sets involved, nothing to do */
3255 return;
3256 }
3257
3258 if (node1) {
3259 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3260 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3261 warning = 1;
3262 leaves = 0;
3263 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3264 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3265 warning = 1;
3266 }
3267 }
3268
3269 if (node2) {
3270 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3271 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3272 warning = 1;
3273 leaves = 0;
3274 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3275 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3276 warning = 1;
3277 }
3278 }
3279
3280 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003281 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3282 (!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_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003285 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3286 warning = 1;
3287 }
3288 }
3289
3290 if (warning) {
3291 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3292 }
3293}
3294
3295/**
3296 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3297 *
3298 * @param[in] exp Parsed XPath expression.
3299 * @param[in] set Set with the leaf/leaf-list.
3300 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3301 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3302 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3303 */
3304static void
3305warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp)
3306{
3307 struct lysc_node *scnode;
3308 struct lysc_type *type;
3309 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003310 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003311 LY_ERR rc;
3312 struct ly_err_item *err = NULL;
3313
Michal Vasko69730152020-10-09 16:30:07 +02003314 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3315 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003316 /* check that the node can have the specified value */
3317 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3318 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3319 } else {
3320 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3321 }
3322 if (!value) {
3323 LOGMEM(set->ctx);
3324 return;
3325 }
3326
3327 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3328 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003329 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003330 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003331 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003332 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003333 }
3334
3335 type = ((struct lysc_node_leaf *)scnode)->type;
3336 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003337 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003338 LYD_HINT_DATA, scnode, &storage, &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003339
3340 if (err) {
3341 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3342 ly_err_free(err);
3343 } else if (rc != LY_SUCCESS) {
3344 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3345 }
3346 if (rc != LY_SUCCESS) {
3347 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003348 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003349 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003350 } else {
3351 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003352 }
3353 }
3354 free(value);
3355 }
3356}
3357
3358/*
3359 * XPath functions
3360 */
3361
3362/**
3363 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3364 * depending on whether the first node bit value from the second argument is set.
3365 *
3366 * @param[in] args Array of arguments.
3367 * @param[in] arg_count Count of elements in @p args.
3368 * @param[in,out] set Context and result set at the same time.
3369 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003370 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003371 */
3372static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003373xpath_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 +02003374{
3375 struct lyd_node_term *leaf;
3376 struct lysc_node_leaf *sleaf;
3377 struct lysc_type_bits *bits;
3378 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003379 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003380
3381 if (options & LYXP_SCNODE_ALL) {
3382 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3383 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003384 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3385 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 +02003386 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3387 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003388 }
3389
3390 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3391 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3392 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 +02003393 } else if (!warn_is_string_type(sleaf->type)) {
3394 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003395 }
3396 }
3397 set_scnode_clear_ctx(set);
3398 return rc;
3399 }
3400
Michal Vaskod3678892020-05-21 10:06:58 +02003401 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003402 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3403 "bit-is-set(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003404 return LY_EVALID;
3405 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003406 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003407 LY_CHECK_RET(rc);
3408
3409 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003410 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003411 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003412 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3413 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003414 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003415 LY_ARRAY_FOR(bits->bits, u) {
3416 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003417 set_fill_boolean(set, 1);
3418 break;
3419 }
3420 }
3421 }
3422 }
3423
3424 return LY_SUCCESS;
3425}
3426
3427/**
3428 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3429 * with the argument converted to boolean.
3430 *
3431 * @param[in] args Array of arguments.
3432 * @param[in] arg_count Count of elements in @p args.
3433 * @param[in,out] set Context and result set at the same time.
3434 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003435 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003436 */
3437static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003438xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003439{
3440 LY_ERR rc;
3441
3442 if (options & LYXP_SCNODE_ALL) {
3443 set_scnode_clear_ctx(set);
3444 return LY_SUCCESS;
3445 }
3446
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003447 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003448 LY_CHECK_RET(rc);
3449 set_fill_set(set, args[0]);
3450
3451 return LY_SUCCESS;
3452}
3453
3454/**
3455 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3456 * with the first argument rounded up to the nearest integer.
3457 *
3458 * @param[in] args Array of arguments.
3459 * @param[in] arg_count Count of elements in @p args.
3460 * @param[in,out] set Context and result set at the same time.
3461 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003462 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003463 */
3464static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003465xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003466{
3467 struct lysc_node_leaf *sleaf;
3468 LY_ERR rc = LY_SUCCESS;
3469
3470 if (options & LYXP_SCNODE_ALL) {
3471 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3472 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003473 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3474 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 +02003475 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3476 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003477 }
3478 set_scnode_clear_ctx(set);
3479 return rc;
3480 }
3481
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003482 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003483 LY_CHECK_RET(rc);
3484 if ((long long)args[0]->val.num != args[0]->val.num) {
3485 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3486 } else {
3487 set_fill_number(set, args[0]->val.num);
3488 }
3489
3490 return LY_SUCCESS;
3491}
3492
3493/**
3494 * @brief Execute the XPath concat(string, string, string*) function.
3495 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3496 *
3497 * @param[in] args Array of arguments.
3498 * @param[in] arg_count Count of elements in @p args.
3499 * @param[in,out] set Context and result set at the same time.
3500 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003501 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003502 */
3503static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003504xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003505{
3506 uint16_t i;
3507 char *str = NULL;
3508 size_t used = 1;
3509 LY_ERR rc = LY_SUCCESS;
3510 struct lysc_node_leaf *sleaf;
3511
3512 if (options & LYXP_SCNODE_ALL) {
3513 for (i = 0; i < arg_count; ++i) {
3514 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3515 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3516 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003517 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003518 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003519 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 +02003520 }
3521 }
3522 }
3523 set_scnode_clear_ctx(set);
3524 return rc;
3525 }
3526
3527 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003528 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003529 if (rc != LY_SUCCESS) {
3530 free(str);
3531 return rc;
3532 }
3533
3534 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3535 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3536 strcpy(str + used - 1, args[i]->val.str);
3537 used += strlen(args[i]->val.str);
3538 }
3539
3540 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003541 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003542 set->type = LYXP_SET_STRING;
3543 set->val.str = str;
3544
3545 return LY_SUCCESS;
3546}
3547
3548/**
3549 * @brief Execute the XPath contains(string, string) function.
3550 * Returns LYXP_SET_BOOLEAN whether the second argument can
3551 * be found in the first or not.
3552 *
3553 * @param[in] args Array of arguments.
3554 * @param[in] arg_count Count of elements in @p args.
3555 * @param[in,out] set Context and result set at the same time.
3556 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003557 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003558 */
3559static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003560xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003561{
3562 struct lysc_node_leaf *sleaf;
3563 LY_ERR rc = LY_SUCCESS;
3564
3565 if (options & LYXP_SCNODE_ALL) {
3566 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3567 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3568 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 +02003569 } else if (!warn_is_string_type(sleaf->type)) {
3570 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003571 }
3572 }
3573
3574 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3575 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3576 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 +02003577 } else if (!warn_is_string_type(sleaf->type)) {
3578 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003579 }
3580 }
3581 set_scnode_clear_ctx(set);
3582 return rc;
3583 }
3584
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003585 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003586 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003587 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003588 LY_CHECK_RET(rc);
3589
3590 if (strstr(args[0]->val.str, args[1]->val.str)) {
3591 set_fill_boolean(set, 1);
3592 } else {
3593 set_fill_boolean(set, 0);
3594 }
3595
3596 return LY_SUCCESS;
3597}
3598
3599/**
3600 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3601 * with the size of the node-set from the argument.
3602 *
3603 * @param[in] args Array of arguments.
3604 * @param[in] arg_count Count of elements in @p args.
3605 * @param[in,out] set Context and result set at the same time.
3606 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003607 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003608 */
3609static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003610xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003611{
3612 struct lysc_node *scnode = NULL;
3613 LY_ERR rc = LY_SUCCESS;
3614
3615 if (options & LYXP_SCNODE_ALL) {
3616 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3617 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003618 }
3619 set_scnode_clear_ctx(set);
3620 return rc;
3621 }
3622
Michal Vasko03ff5a72019-09-11 13:49:33 +02003623 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003624 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 +02003625 return LY_EVALID;
3626 }
3627
3628 set_fill_number(set, args[0]->used);
3629 return LY_SUCCESS;
3630}
3631
3632/**
3633 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3634 * with the context with the intial node.
3635 *
3636 * @param[in] args Array of arguments.
3637 * @param[in] arg_count Count of elements in @p args.
3638 * @param[in,out] set Context and result set at the same time.
3639 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003640 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003641 */
3642static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003643xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003644{
3645 if (arg_count || args) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003646 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003647 return LY_EVALID;
3648 }
3649
3650 if (options & LYXP_SCNODE_ALL) {
3651 set_scnode_clear_ctx(set);
3652
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003653 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003654 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003655 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003656
3657 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003658 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003659 }
3660
3661 return LY_SUCCESS;
3662}
3663
3664/**
3665 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3666 * leafref or instance-identifier target node(s).
3667 *
3668 * @param[in] args Array of arguments.
3669 * @param[in] arg_count Count of elements in @p args.
3670 * @param[in,out] set Context and result set at the same time.
3671 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003672 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003673 */
3674static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003675xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003676{
3677 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003678 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003679 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003680 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003681 struct ly_path *p;
3682 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003683 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003684 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003685 LY_ERR rc = LY_SUCCESS;
3686
3687 if (options & LYXP_SCNODE_ALL) {
3688 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3689 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003690 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3691 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 +02003692 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3693 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
Michal Vasko69730152020-10-09 16:30:07 +02003694 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003695 }
3696 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003697 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003698 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vasko00cbf532020-06-15 13:58:47 +02003699 oper = lysc_is_output((struct lysc_node *)sleaf) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003700
3701 /* it was already evaluated on schema, it must succeed */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003702 rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path,
3703 LY_PATH_LREF_TRUE, oper, LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003704 assert(!rc);
3705
3706 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003707 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003708 ly_path_free(set->ctx, p);
3709
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003710 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003711 }
3712
Michal Vasko03ff5a72019-09-11 13:49:33 +02003713 return rc;
3714 }
3715
Michal Vaskod3678892020-05-21 10:06:58 +02003716 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003717 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 +02003718 return LY_EVALID;
3719 }
3720
Michal Vaskod3678892020-05-21 10:06:58 +02003721 lyxp_set_free_content(set);
3722 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003723 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3724 sleaf = (struct lysc_node_leaf *)leaf->schema;
3725 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3726 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3727 /* find leafref target */
Michal Vasko004d3152020-06-11 19:59:22 +02003728 if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf,
Michal Vasko69730152020-10-09 16:30:07 +02003729 &leaf->value, set->tree, &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003730 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003731 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003732 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003733 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003734 } else {
3735 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003736 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003737 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Michal Vasko69730152020-10-09 16:30:07 +02003738 LYD_CANON_VALUE(leaf));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003739 return LY_EVALID;
3740 }
3741 }
Michal Vasko004d3152020-06-11 19:59:22 +02003742
3743 /* insert it */
3744 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003745 }
3746 }
3747
3748 return LY_SUCCESS;
3749}
3750
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003751static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003752xpath_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 +02003753{
3754 uint16_t i;
3755 LY_ARRAY_COUNT_TYPE u;
3756 struct lyd_node_term *leaf;
3757 struct lysc_node_leaf *sleaf;
3758 struct lyd_meta *meta;
3759 struct lyd_value data = {0}, *val;
3760 struct ly_err_item *err = NULL;
3761 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003762 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003763
3764 if (options & LYXP_SCNODE_ALL) {
3765 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3766 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
3767 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3768 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003769 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003770 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3771 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3772 }
3773
3774 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3775 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3776 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003777 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003778 } else if (!warn_is_string_type(sleaf->type)) {
3779 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3780 }
3781 }
3782 set_scnode_clear_ctx(set);
3783 return rc;
3784 }
3785
3786 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003787 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 +02003788 "derived-from(-or-self)(node-set, string)");
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003789 return LY_EVALID;
3790 }
3791 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3792 LY_CHECK_RET(rc);
3793
3794 set_fill_boolean(set, 0);
3795 found = 0;
3796 for (i = 0; i < args[0]->used; ++i) {
3797 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3798 continue;
3799 }
3800
3801 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3802 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3803 sleaf = (struct lysc_node_leaf *)leaf->schema;
3804 val = &leaf->value;
3805 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3806 /* uninteresting */
3807 continue;
3808 }
3809
3810 /* store args[1] as ident */
3811 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 +02003812 0, set->format, set->prefix_data, LYD_HINT_DATA, leaf->schema, &data, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003813 } else {
3814 meta = args[0]->val.meta[i].meta;
3815 val = &meta->value;
3816 if (val->realtype->basetype != LY_TYPE_IDENT) {
3817 /* uninteresting */
3818 continue;
3819 }
3820
3821 /* store args[1] as ident */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003822 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 +02003823 set->format, set->prefix_data, LYD_HINT_DATA, meta->parent->schema, &data, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003824 }
3825
3826 if (err) {
3827 ly_err_print(err);
3828 ly_err_free(err);
3829 }
3830 LY_CHECK_RET(rc);
3831
3832 /* finally check the identity itself */
3833 if (self_match && (data.ident == val->ident)) {
3834 set_fill_boolean(set, 1);
3835 found = 1;
3836 }
3837 if (!found) {
3838 LY_ARRAY_FOR(data.ident->derived, u) {
3839 if (data.ident->derived[u] == val->ident) {
3840 set_fill_boolean(set, 1);
3841 found = 1;
3842 break;
3843 }
3844 }
3845 }
3846
3847 /* free temporary value */
3848 val->realtype->plugin->free(set->ctx, &data);
3849 if (found) {
3850 break;
3851 }
3852 }
3853
3854 return LY_SUCCESS;
3855}
3856
Michal Vasko03ff5a72019-09-11 13:49:33 +02003857/**
3858 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3859 * on whether the first argument nodes contain a node of an identity derived from the second
3860 * argument identity.
3861 *
3862 * @param[in] args Array of arguments.
3863 * @param[in] arg_count Count of elements in @p args.
3864 * @param[in,out] set Context and result set at the same time.
3865 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003866 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003867 */
3868static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003869xpath_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 +02003870{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003871 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003872}
3873
3874/**
3875 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3876 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3877 * the second argument identity.
3878 *
3879 * @param[in] args Array of arguments.
3880 * @param[in] arg_count Count of elements in @p args.
3881 * @param[in,out] set Context and result set at the same time.
3882 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003883 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003884 */
3885static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003886xpath_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 +02003887{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003888 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003889}
3890
3891/**
3892 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3893 * with the integer value of the first node's enum value, otherwise NaN.
3894 *
3895 * @param[in] args Array of arguments.
3896 * @param[in] arg_count Count of elements in @p args.
3897 * @param[in,out] set Context and result set at the same time.
3898 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003899 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003900 */
3901static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003902xpath_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 +02003903{
3904 struct lyd_node_term *leaf;
3905 struct lysc_node_leaf *sleaf;
3906 LY_ERR rc = LY_SUCCESS;
3907
3908 if (options & LYXP_SCNODE_ALL) {
3909 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3910 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003911 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3912 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 +02003913 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3914 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003915 }
3916 set_scnode_clear_ctx(set);
3917 return rc;
3918 }
3919
Michal Vaskod3678892020-05-21 10:06:58 +02003920 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003921 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 +02003922 return LY_EVALID;
3923 }
3924
3925 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003926 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003927 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3928 sleaf = (struct lysc_node_leaf *)leaf->schema;
3929 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3930 set_fill_number(set, leaf->value.enum_item->value);
3931 }
3932 }
3933
3934 return LY_SUCCESS;
3935}
3936
3937/**
3938 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3939 * with false value.
3940 *
3941 * @param[in] args Array of arguments.
3942 * @param[in] arg_count Count of elements in @p args.
3943 * @param[in,out] set Context and result set at the same time.
3944 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003945 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003946 */
3947static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003948xpath_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 +02003949{
3950 if (options & LYXP_SCNODE_ALL) {
3951 set_scnode_clear_ctx(set);
3952 return LY_SUCCESS;
3953 }
3954
3955 set_fill_boolean(set, 0);
3956 return LY_SUCCESS;
3957}
3958
3959/**
3960 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3961 * with the first argument floored (truncated).
3962 *
3963 * @param[in] args Array of arguments.
3964 * @param[in] arg_count Count of elements in @p args.
3965 * @param[in,out] set Context and result set at the same time.
3966 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003967 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003968 */
3969static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003970xpath_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 +02003971{
3972 LY_ERR rc;
3973
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003974 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003975 LY_CHECK_RET(rc);
3976 if (isfinite(args[0]->val.num)) {
3977 set_fill_number(set, (long long)args[0]->val.num);
3978 }
3979
3980 return LY_SUCCESS;
3981}
3982
3983/**
3984 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3985 * whether the language of the text matches the one from the argument.
3986 *
3987 * @param[in] args Array of arguments.
3988 * @param[in] arg_count Count of elements in @p args.
3989 * @param[in,out] set Context and result set at the same time.
3990 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003991 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003992 */
3993static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003994xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003995{
3996 const struct lyd_node *node;
3997 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01003998 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003999 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004000 LY_ERR rc = LY_SUCCESS;
4001
4002 if (options & LYXP_SCNODE_ALL) {
4003 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4004 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4005 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 +02004006 } else if (!warn_is_string_type(sleaf->type)) {
4007 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004008 }
4009 }
4010 set_scnode_clear_ctx(set);
4011 return rc;
4012 }
4013
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004014 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004015 LY_CHECK_RET(rc);
4016
Michal Vasko03ff5a72019-09-11 13:49:33 +02004017 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004018 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 +02004019 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004020 } else if (!set->used) {
4021 set_fill_boolean(set, 0);
4022 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004023 }
4024
4025 switch (set->val.nodes[0].type) {
4026 case LYXP_NODE_ELEM:
4027 case LYXP_NODE_TEXT:
4028 node = set->val.nodes[0].node;
4029 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004030 case LYXP_NODE_META:
4031 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004032 break;
4033 default:
4034 /* nothing to do with roots */
4035 set_fill_boolean(set, 0);
4036 return LY_SUCCESS;
4037 }
4038
Michal Vasko9f96a052020-03-10 09:41:45 +01004039 /* find lang metadata */
Michal Vaskod989ba02020-08-24 10:59:24 +02004040 for ( ; node; node = (struct lyd_node *)node->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004041 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004042 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004043 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004044 break;
4045 }
4046 }
4047
Michal Vasko9f96a052020-03-10 09:41:45 +01004048 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004049 break;
4050 }
4051 }
4052
4053 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004054 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004055 set_fill_boolean(set, 0);
4056 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004057 uint64_t i;
4058
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004059 val = meta->value.canonical;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004060 for (i = 0; args[0]->val.str[i]; ++i) {
4061 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4062 set_fill_boolean(set, 0);
4063 break;
4064 }
4065 }
4066 if (!args[0]->val.str[i]) {
4067 if (!val[i] || (val[i] == '-')) {
4068 set_fill_boolean(set, 1);
4069 } else {
4070 set_fill_boolean(set, 0);
4071 }
4072 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004073 }
4074
4075 return LY_SUCCESS;
4076}
4077
4078/**
4079 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4080 * with the context size.
4081 *
4082 * @param[in] args Array of arguments.
4083 * @param[in] arg_count Count of elements in @p args.
4084 * @param[in,out] set Context and result set at the same time.
4085 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004086 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004087 */
4088static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004089xpath_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 +02004090{
4091 if (options & LYXP_SCNODE_ALL) {
4092 set_scnode_clear_ctx(set);
4093 return LY_SUCCESS;
4094 }
4095
Michal Vasko03ff5a72019-09-11 13:49:33 +02004096 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004097 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 +02004098 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004099 } else if (!set->used) {
4100 set_fill_number(set, 0);
4101 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004102 }
4103
4104 set_fill_number(set, set->ctx_size);
4105 return LY_SUCCESS;
4106}
4107
4108/**
4109 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4110 * with the node name without namespace from the argument or the context.
4111 *
4112 * @param[in] args Array of arguments.
4113 * @param[in] arg_count Count of elements in @p args.
4114 * @param[in,out] set Context and result set at the same time.
4115 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004116 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004117 */
4118static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004119xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004120{
4121 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004122
Michal Vasko03ff5a72019-09-11 13:49:33 +02004123 /* suppress unused variable warning */
4124 (void)options;
4125
4126 if (options & LYXP_SCNODE_ALL) {
4127 set_scnode_clear_ctx(set);
4128 return LY_SUCCESS;
4129 }
4130
4131 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004132 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004133 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
4134 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004135 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004136 } else if (!args[0]->used) {
4137 set_fill_string(set, "", 0);
4138 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004139 }
4140
4141 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004142 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004143
4144 item = &args[0]->val.nodes[0];
4145 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004146 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004147 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 +02004148 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004149 } else if (!set->used) {
4150 set_fill_string(set, "", 0);
4151 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004152 }
4153
4154 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004155 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004156
4157 item = &set->val.nodes[0];
4158 }
4159
4160 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004161 case LYXP_NODE_NONE:
4162 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004163 case LYXP_NODE_ROOT:
4164 case LYXP_NODE_ROOT_CONFIG:
4165 case LYXP_NODE_TEXT:
4166 set_fill_string(set, "", 0);
4167 break;
4168 case LYXP_NODE_ELEM:
4169 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4170 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004171 case LYXP_NODE_META:
4172 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004173 break;
4174 }
4175
4176 return LY_SUCCESS;
4177}
4178
4179/**
4180 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4181 * with the node name fully qualified (with namespace) from the argument or the context.
4182 *
4183 * @param[in] args Array of arguments.
4184 * @param[in] arg_count Count of elements in @p args.
4185 * @param[in,out] set Context and result set at the same time.
4186 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004187 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004188 */
4189static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004190xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004191{
4192 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004193 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004194 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004195 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004196
4197 if (options & LYXP_SCNODE_ALL) {
4198 set_scnode_clear_ctx(set);
4199 return LY_SUCCESS;
4200 }
4201
4202 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004203 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004204 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 +02004205 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004206 } else if (!args[0]->used) {
4207 set_fill_string(set, "", 0);
4208 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004209 }
4210
4211 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004212 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004213
4214 item = &args[0]->val.nodes[0];
4215 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004216 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004217 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 +02004218 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004219 } else if (!set->used) {
4220 set_fill_string(set, "", 0);
4221 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004222 }
4223
4224 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004225 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004226
4227 item = &set->val.nodes[0];
4228 }
4229
4230 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004231 case LYXP_NODE_NONE:
4232 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004233 case LYXP_NODE_ROOT:
4234 case LYXP_NODE_ROOT_CONFIG:
4235 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004236 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004237 break;
4238 case LYXP_NODE_ELEM:
4239 mod = item->node->schema->module;
4240 name = item->node->schema->name;
4241 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004242 case LYXP_NODE_META:
4243 mod = ((struct lyd_meta *)item->node)->annotation->module;
4244 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004245 break;
4246 }
4247
4248 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004249 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004250 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4251 set_fill_string(set, str, strlen(str));
4252 free(str);
4253 } else {
4254 set_fill_string(set, "", 0);
4255 }
4256
4257 return LY_SUCCESS;
4258}
4259
4260/**
4261 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4262 * with the namespace of the node from the argument or the context.
4263 *
4264 * @param[in] args Array of arguments.
4265 * @param[in] arg_count Count of elements in @p args.
4266 * @param[in,out] set Context and result set at the same time.
4267 * @param[in] options XPath options.
4268 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4269 */
4270static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004271xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004272{
4273 struct lyxp_set_node *item;
4274 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004275
Michal Vasko03ff5a72019-09-11 13:49:33 +02004276 /* suppress unused variable warning */
4277 (void)options;
4278
4279 if (options & LYXP_SCNODE_ALL) {
4280 set_scnode_clear_ctx(set);
4281 return LY_SUCCESS;
4282 }
4283
4284 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004285 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004286 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 +02004287 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004288 return LY_EVALID;
4289 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004290 set_fill_string(set, "", 0);
4291 return LY_SUCCESS;
4292 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004293
4294 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004295 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004296
4297 item = &args[0]->val.nodes[0];
4298 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004299 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004300 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 +02004301 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004302 } else if (!set->used) {
4303 set_fill_string(set, "", 0);
4304 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004305 }
4306
4307 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004308 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004309
4310 item = &set->val.nodes[0];
4311 }
4312
4313 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004314 case LYXP_NODE_NONE:
4315 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004316 case LYXP_NODE_ROOT:
4317 case LYXP_NODE_ROOT_CONFIG:
4318 case LYXP_NODE_TEXT:
4319 set_fill_string(set, "", 0);
4320 break;
4321 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004322 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004323 if (item->type == LYXP_NODE_ELEM) {
4324 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004325 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004326 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004327 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004328 }
4329
4330 set_fill_string(set, mod->ns, strlen(mod->ns));
4331 break;
4332 }
4333
4334 return LY_SUCCESS;
4335}
4336
4337/**
4338 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4339 * with only nodes from the context. In practice it either leaves the context
4340 * as it is or returns an empty node set.
4341 *
4342 * @param[in] args Array of arguments.
4343 * @param[in] arg_count Count of elements in @p args.
4344 * @param[in,out] set Context and result set at the same time.
4345 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004346 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004347 */
4348static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004349xpath_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 +02004350{
4351 if (options & LYXP_SCNODE_ALL) {
4352 set_scnode_clear_ctx(set);
4353 return LY_SUCCESS;
4354 }
4355
4356 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004357 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004358 }
4359 return LY_SUCCESS;
4360}
4361
4362/**
4363 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4364 * with normalized value (no leading, trailing, double white spaces) of the node
4365 * from the argument or the context.
4366 *
4367 * @param[in] args Array of arguments.
4368 * @param[in] arg_count Count of elements in @p args.
4369 * @param[in,out] set Context and result set at the same time.
4370 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004371 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004372 */
4373static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004374xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004375{
4376 uint16_t i, new_used;
4377 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004378 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004379 struct lysc_node_leaf *sleaf;
4380 LY_ERR rc = LY_SUCCESS;
4381
4382 if (options & LYXP_SCNODE_ALL) {
4383 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4384 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4385 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 +02004386 } else if (!warn_is_string_type(sleaf->type)) {
4387 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004388 }
4389 }
4390 set_scnode_clear_ctx(set);
4391 return rc;
4392 }
4393
4394 if (arg_count) {
4395 set_fill_set(set, args[0]);
4396 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004397 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004398 LY_CHECK_RET(rc);
4399
4400 /* is there any normalization necessary? */
4401 for (i = 0; set->val.str[i]; ++i) {
4402 if (is_xmlws(set->val.str[i])) {
4403 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4404 have_spaces = 1;
4405 break;
4406 }
4407 space_before = 1;
4408 } else {
4409 space_before = 0;
4410 }
4411 }
4412
4413 /* yep, there is */
4414 if (have_spaces) {
4415 /* it's enough, at least one character will go, makes space for ending '\0' */
4416 new = malloc(strlen(set->val.str) * sizeof(char));
4417 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4418 new_used = 0;
4419
4420 space_before = 0;
4421 for (i = 0; set->val.str[i]; ++i) {
4422 if (is_xmlws(set->val.str[i])) {
4423 if ((i == 0) || space_before) {
4424 space_before = 1;
4425 continue;
4426 } else {
4427 space_before = 1;
4428 }
4429 } else {
4430 space_before = 0;
4431 }
4432
4433 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4434 ++new_used;
4435 }
4436
4437 /* at worst there is one trailing space now */
4438 if (new_used && is_xmlws(new[new_used - 1])) {
4439 --new_used;
4440 }
4441
4442 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4443 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4444 new[new_used] = '\0';
4445
4446 free(set->val.str);
4447 set->val.str = new;
4448 }
4449
4450 return LY_SUCCESS;
4451}
4452
4453/**
4454 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4455 * with the argument converted to boolean and logically inverted.
4456 *
4457 * @param[in] args Array of arguments.
4458 * @param[in] arg_count Count of elements in @p args.
4459 * @param[in,out] set Context and result set at the same time.
4460 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004461 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004462 */
4463static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004464xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004465{
4466 if (options & LYXP_SCNODE_ALL) {
4467 set_scnode_clear_ctx(set);
4468 return LY_SUCCESS;
4469 }
4470
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004471 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004472 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004473 set_fill_boolean(set, 0);
4474 } else {
4475 set_fill_boolean(set, 1);
4476 }
4477
4478 return LY_SUCCESS;
4479}
4480
4481/**
4482 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4483 * with the number representation of either the argument or the context.
4484 *
4485 * @param[in] args Array of arguments.
4486 * @param[in] arg_count Count of elements in @p args.
4487 * @param[in,out] set Context and result set at the same time.
4488 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004489 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004490 */
4491static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004492xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004493{
4494 LY_ERR rc;
4495
4496 if (options & LYXP_SCNODE_ALL) {
4497 set_scnode_clear_ctx(set);
4498 return LY_SUCCESS;
4499 }
4500
4501 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004502 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004503 LY_CHECK_RET(rc);
4504 set_fill_set(set, args[0]);
4505 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004506 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004507 LY_CHECK_RET(rc);
4508 }
4509
4510 return LY_SUCCESS;
4511}
4512
4513/**
4514 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4515 * with the context position.
4516 *
4517 * @param[in] args Array of arguments.
4518 * @param[in] arg_count Count of elements in @p args.
4519 * @param[in,out] set Context and result set at the same time.
4520 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004521 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004522 */
4523static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004524xpath_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 +02004525{
4526 if (options & LYXP_SCNODE_ALL) {
4527 set_scnode_clear_ctx(set);
4528 return LY_SUCCESS;
4529 }
4530
Michal Vasko03ff5a72019-09-11 13:49:33 +02004531 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004532 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 +02004533 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004534 } else if (!set->used) {
4535 set_fill_number(set, 0);
4536 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004537 }
4538
4539 set_fill_number(set, set->ctx_pos);
4540
4541 /* UNUSED in 'Release' build type */
4542 (void)options;
4543 return LY_SUCCESS;
4544}
4545
4546/**
4547 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4548 * depending on whether the second argument regex matches the first argument string. For details refer to
4549 * YANG 1.1 RFC section 10.2.1.
4550 *
4551 * @param[in] args Array of arguments.
4552 * @param[in] arg_count Count of elements in @p args.
4553 * @param[in,out] set Context and result set at the same time.
4554 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004555 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004556 */
4557static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004558xpath_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 +02004559{
4560 struct lysc_pattern **patterns = NULL, **pattern;
4561 struct lysc_node_leaf *sleaf;
4562 char *path;
4563 LY_ERR rc = LY_SUCCESS;
4564 struct ly_err_item *err;
4565
4566 if (options & LYXP_SCNODE_ALL) {
4567 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4568 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4569 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 +02004570 } else if (!warn_is_string_type(sleaf->type)) {
4571 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004572 }
4573 }
4574
4575 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4576 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4577 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 +02004578 } else if (!warn_is_string_type(sleaf->type)) {
4579 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004580 }
4581 }
4582 set_scnode_clear_ctx(set);
4583 return rc;
4584 }
4585
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004586 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004587 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004588 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004589 LY_CHECK_RET(rc);
4590
4591 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4592 *pattern = malloc(sizeof **pattern);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004593 path = lyd_path(set->cur_node, LYD_PATH_LOG, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004594 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4595 free(path);
4596 if (rc != LY_SUCCESS) {
4597 LY_ARRAY_FREE(patterns);
4598 return rc;
4599 }
4600
4601 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4602 pcre2_code_free((*pattern)->code);
4603 free(*pattern);
4604 LY_ARRAY_FREE(patterns);
4605 if (rc && (rc != LY_EVALID)) {
4606 ly_err_print(err);
4607 ly_err_free(err);
4608 return rc;
4609 }
4610
4611 if (rc == LY_EVALID) {
4612 ly_err_free(err);
4613 set_fill_boolean(set, 0);
4614 } else {
4615 set_fill_boolean(set, 1);
4616 }
4617
4618 return LY_SUCCESS;
4619}
4620
4621/**
4622 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4623 * with the rounded first argument. For details refer to
4624 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4625 *
4626 * @param[in] args Array of arguments.
4627 * @param[in] arg_count Count of elements in @p args.
4628 * @param[in,out] set Context and result set at the same time.
4629 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004630 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004631 */
4632static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004633xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004634{
4635 struct lysc_node_leaf *sleaf;
4636 LY_ERR rc = LY_SUCCESS;
4637
4638 if (options & LYXP_SCNODE_ALL) {
4639 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4640 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004641 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4642 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 +02004643 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4644 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004645 }
4646 set_scnode_clear_ctx(set);
4647 return rc;
4648 }
4649
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004650 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004651 LY_CHECK_RET(rc);
4652
4653 /* cover only the cases where floor can't be used */
4654 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4655 set_fill_number(set, -0.0f);
4656 } else {
4657 args[0]->val.num += 0.5;
4658 rc = xpath_floor(args, 1, args[0], options);
4659 LY_CHECK_RET(rc);
4660 set_fill_number(set, args[0]->val.num);
4661 }
4662
4663 return LY_SUCCESS;
4664}
4665
4666/**
4667 * @brief Execute the XPath starts-with(string, string) function.
4668 * Returns LYXP_SET_BOOLEAN whether the second argument is
4669 * the prefix of the first or not.
4670 *
4671 * @param[in] args Array of arguments.
4672 * @param[in] arg_count Count of elements in @p args.
4673 * @param[in,out] set Context and result set at the same time.
4674 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004675 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004676 */
4677static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004678xpath_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 +02004679{
4680 struct lysc_node_leaf *sleaf;
4681 LY_ERR rc = LY_SUCCESS;
4682
4683 if (options & LYXP_SCNODE_ALL) {
4684 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4685 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4686 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 +02004687 } else if (!warn_is_string_type(sleaf->type)) {
4688 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004689 }
4690 }
4691
4692 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4693 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4694 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 +02004695 } else if (!warn_is_string_type(sleaf->type)) {
4696 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004697 }
4698 }
4699 set_scnode_clear_ctx(set);
4700 return rc;
4701 }
4702
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004703 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004704 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004705 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004706 LY_CHECK_RET(rc);
4707
4708 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4709 set_fill_boolean(set, 0);
4710 } else {
4711 set_fill_boolean(set, 1);
4712 }
4713
4714 return LY_SUCCESS;
4715}
4716
4717/**
4718 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4719 * with the string representation of either the argument or the context.
4720 *
4721 * @param[in] args Array of arguments.
4722 * @param[in] arg_count Count of elements in @p args.
4723 * @param[in,out] set Context and result set at the same time.
4724 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004725 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004726 */
4727static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004728xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004729{
4730 LY_ERR rc;
4731
4732 if (options & LYXP_SCNODE_ALL) {
4733 set_scnode_clear_ctx(set);
4734 return LY_SUCCESS;
4735 }
4736
4737 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004738 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004739 LY_CHECK_RET(rc);
4740 set_fill_set(set, args[0]);
4741 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004742 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004743 LY_CHECK_RET(rc);
4744 }
4745
4746 return LY_SUCCESS;
4747}
4748
4749/**
4750 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4751 * with the length of the string in either the argument or the context.
4752 *
4753 * @param[in] args Array of arguments.
4754 * @param[in] arg_count Count of elements in @p args.
4755 * @param[in,out] set Context and result set at the same time.
4756 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004757 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004758 */
4759static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004760xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004761{
4762 struct lysc_node_leaf *sleaf;
4763 LY_ERR rc = LY_SUCCESS;
4764
4765 if (options & LYXP_SCNODE_ALL) {
4766 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4767 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4768 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 +02004769 } else if (!warn_is_string_type(sleaf->type)) {
4770 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004771 }
4772 }
4773 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4774 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4775 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 +02004776 } else if (!warn_is_string_type(sleaf->type)) {
4777 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004778 }
4779 }
4780 set_scnode_clear_ctx(set);
4781 return rc;
4782 }
4783
4784 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004785 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004786 LY_CHECK_RET(rc);
4787 set_fill_number(set, strlen(args[0]->val.str));
4788 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004789 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004790 LY_CHECK_RET(rc);
4791 set_fill_number(set, strlen(set->val.str));
4792 }
4793
4794 return LY_SUCCESS;
4795}
4796
4797/**
4798 * @brief Execute the XPath substring(string, number, number?) function.
4799 * Returns LYXP_SET_STRING substring of the first argument starting
4800 * on the second argument index ending on the third argument index,
4801 * indexed from 1. For exact definition refer to
4802 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4803 *
4804 * @param[in] args Array of arguments.
4805 * @param[in] arg_count Count of elements in @p args.
4806 * @param[in,out] set Context and result set at the same time.
4807 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004808 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004809 */
4810static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004811xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004812{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004813 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004814 uint16_t str_start, str_len, pos;
4815 struct lysc_node_leaf *sleaf;
4816 LY_ERR rc = LY_SUCCESS;
4817
4818 if (options & LYXP_SCNODE_ALL) {
4819 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4820 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4821 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 +02004822 } else if (!warn_is_string_type(sleaf->type)) {
4823 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004824 }
4825 }
4826
4827 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4828 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4829 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 +02004830 } else if (!warn_is_numeric_type(sleaf->type)) {
4831 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004832 }
4833 }
4834
Michal Vasko69730152020-10-09 16:30:07 +02004835 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4836 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004837 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4838 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 +02004839 } else if (!warn_is_numeric_type(sleaf->type)) {
4840 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004841 }
4842 }
4843 set_scnode_clear_ctx(set);
4844 return rc;
4845 }
4846
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004847 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004848 LY_CHECK_RET(rc);
4849
4850 /* start */
4851 if (xpath_round(&args[1], 1, args[1], options)) {
4852 return -1;
4853 }
4854 if (isfinite(args[1]->val.num)) {
4855 start = args[1]->val.num - 1;
4856 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004857 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004858 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004859 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004860 }
4861
4862 /* len */
4863 if (arg_count == 3) {
4864 rc = xpath_round(&args[2], 1, args[2], options);
4865 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004866 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004867 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004868 } else if (isfinite(args[2]->val.num)) {
4869 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004870 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004871 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004872 }
4873 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004874 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004875 }
4876
4877 /* find matching character positions */
4878 str_start = 0;
4879 str_len = 0;
4880 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4881 if (pos < start) {
4882 ++str_start;
4883 } else if (pos < start + len) {
4884 ++str_len;
4885 } else {
4886 break;
4887 }
4888 }
4889
4890 set_fill_string(set, args[0]->val.str + str_start, str_len);
4891 return LY_SUCCESS;
4892}
4893
4894/**
4895 * @brief Execute the XPath substring-after(string, string) function.
4896 * Returns LYXP_SET_STRING with the string succeeding the occurance
4897 * of the second argument in the first or an empty string.
4898 *
4899 * @param[in] args Array of arguments.
4900 * @param[in] arg_count Count of elements in @p args.
4901 * @param[in,out] set Context and result set at the same time.
4902 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004903 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004904 */
4905static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004906xpath_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 +02004907{
4908 char *ptr;
4909 struct lysc_node_leaf *sleaf;
4910 LY_ERR rc = LY_SUCCESS;
4911
4912 if (options & LYXP_SCNODE_ALL) {
4913 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4914 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4915 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 +02004916 } else if (!warn_is_string_type(sleaf->type)) {
4917 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004918 }
4919 }
4920
4921 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4922 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4923 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 +02004924 } else if (!warn_is_string_type(sleaf->type)) {
4925 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004926 }
4927 }
4928 set_scnode_clear_ctx(set);
4929 return rc;
4930 }
4931
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004932 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004933 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004934 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004935 LY_CHECK_RET(rc);
4936
4937 ptr = strstr(args[0]->val.str, args[1]->val.str);
4938 if (ptr) {
4939 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4940 } else {
4941 set_fill_string(set, "", 0);
4942 }
4943
4944 return LY_SUCCESS;
4945}
4946
4947/**
4948 * @brief Execute the XPath substring-before(string, string) function.
4949 * Returns LYXP_SET_STRING with the string preceding the occurance
4950 * of the second argument in the first or an empty string.
4951 *
4952 * @param[in] args Array of arguments.
4953 * @param[in] arg_count Count of elements in @p args.
4954 * @param[in,out] set Context and result set at the same time.
4955 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004956 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004957 */
4958static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004959xpath_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 +02004960{
4961 char *ptr;
4962 struct lysc_node_leaf *sleaf;
4963 LY_ERR rc = LY_SUCCESS;
4964
4965 if (options & LYXP_SCNODE_ALL) {
4966 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4967 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4968 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 +02004969 } else if (!warn_is_string_type(sleaf->type)) {
4970 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004971 }
4972 }
4973
4974 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4975 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4976 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 +02004977 } else if (!warn_is_string_type(sleaf->type)) {
4978 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004979 }
4980 }
4981 set_scnode_clear_ctx(set);
4982 return rc;
4983 }
4984
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004985 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004986 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004987 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004988 LY_CHECK_RET(rc);
4989
4990 ptr = strstr(args[0]->val.str, args[1]->val.str);
4991 if (ptr) {
4992 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4993 } else {
4994 set_fill_string(set, "", 0);
4995 }
4996
4997 return LY_SUCCESS;
4998}
4999
5000/**
5001 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5002 * with the sum of all the nodes in the context.
5003 *
5004 * @param[in] args Array of arguments.
5005 * @param[in] arg_count Count of elements in @p args.
5006 * @param[in,out] set Context and result set at the same time.
5007 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005008 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005009 */
5010static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005011xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005012{
5013 long double num;
5014 char *str;
5015 uint16_t i;
5016 struct lyxp_set set_item;
5017 struct lysc_node_leaf *sleaf;
5018 LY_ERR rc = LY_SUCCESS;
5019
5020 if (options & LYXP_SCNODE_ALL) {
5021 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5022 for (i = 0; i < args[0]->used; ++i) {
5023 if (args[0]->val.scnodes[i].in_ctx == 1) {
5024 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5025 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5026 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005027 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005028 } else if (!warn_is_numeric_type(sleaf->type)) {
5029 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005030 }
5031 }
5032 }
5033 }
5034 set_scnode_clear_ctx(set);
5035 return rc;
5036 }
5037
5038 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005039
5040 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005041 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 +02005042 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005043 } else if (!args[0]->used) {
5044 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005045 }
5046
Michal Vasko5c4e5892019-11-14 12:31:38 +01005047 set_init(&set_item, set);
5048
Michal Vasko03ff5a72019-09-11 13:49:33 +02005049 set_item.type = LYXP_SET_NODE_SET;
5050 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5051 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5052
5053 set_item.used = 1;
5054 set_item.size = 1;
5055
5056 for (i = 0; i < args[0]->used; ++i) {
5057 set_item.val.nodes[0] = args[0]->val.nodes[i];
5058
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005059 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005060 LY_CHECK_RET(rc);
5061 num = cast_string_to_number(str);
5062 free(str);
5063 set->val.num += num;
5064 }
5065
5066 free(set_item.val.nodes);
5067
5068 return LY_SUCCESS;
5069}
5070
5071/**
5072 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5073 * with the text content of the nodes in the context.
5074 *
5075 * @param[in] args Array of arguments.
5076 * @param[in] arg_count Count of elements in @p args.
5077 * @param[in,out] set Context and result set at the same time.
5078 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005079 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005080 */
5081static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005082xpath_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 +02005083{
5084 uint32_t i;
5085
5086 if (options & LYXP_SCNODE_ALL) {
5087 set_scnode_clear_ctx(set);
5088 return LY_SUCCESS;
5089 }
5090
Michal Vasko03ff5a72019-09-11 13:49:33 +02005091 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005092 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 +02005093 return LY_EVALID;
5094 }
5095
Michal Vaskod989ba02020-08-24 10:59:24 +02005096 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005097 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005098 case LYXP_NODE_NONE:
5099 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005100 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005101 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5102 set->val.nodes[i].type = LYXP_NODE_TEXT;
5103 ++i;
5104 break;
5105 }
Radek Krejci0f969882020-08-21 16:56:47 +02005106 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005107 case LYXP_NODE_ROOT:
5108 case LYXP_NODE_ROOT_CONFIG:
5109 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005110 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005111 set_remove_node(set, i);
5112 break;
5113 }
5114 }
5115
5116 return LY_SUCCESS;
5117}
5118
5119/**
5120 * @brief Execute the XPath translate(string, string, string) function.
5121 * Returns LYXP_SET_STRING with the first argument with the characters
5122 * from the second argument replaced by those on the corresponding
5123 * positions in the third argument.
5124 *
5125 * @param[in] args Array of arguments.
5126 * @param[in] arg_count Count of elements in @p args.
5127 * @param[in,out] set Context and result set at the same time.
5128 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005129 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005130 */
5131static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005132xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005133{
5134 uint16_t i, j, new_used;
5135 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005136 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005137 struct lysc_node_leaf *sleaf;
5138 LY_ERR rc = LY_SUCCESS;
5139
5140 if (options & LYXP_SCNODE_ALL) {
5141 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5142 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5143 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 +02005144 } else if (!warn_is_string_type(sleaf->type)) {
5145 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005146 }
5147 }
5148
5149 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5150 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5151 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 +02005152 } else if (!warn_is_string_type(sleaf->type)) {
5153 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005154 }
5155 }
5156
5157 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5158 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5159 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 +02005160 } else if (!warn_is_string_type(sleaf->type)) {
5161 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005162 }
5163 }
5164 set_scnode_clear_ctx(set);
5165 return rc;
5166 }
5167
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005168 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005169 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005170 rc = lyxp_set_cast(args[1], 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[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005173 LY_CHECK_RET(rc);
5174
5175 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5176 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5177 new_used = 0;
5178
5179 have_removed = 0;
5180 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005181 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005182
5183 for (j = 0; args[1]->val.str[j]; ++j) {
5184 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5185 /* removing this char */
5186 if (j >= strlen(args[2]->val.str)) {
5187 have_removed = 1;
5188 found = 1;
5189 break;
5190 }
5191 /* replacing this char */
5192 new[new_used] = args[2]->val.str[j];
5193 ++new_used;
5194 found = 1;
5195 break;
5196 }
5197 }
5198
5199 /* copying this char */
5200 if (!found) {
5201 new[new_used] = args[0]->val.str[i];
5202 ++new_used;
5203 }
5204 }
5205
5206 if (have_removed) {
5207 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5208 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5209 }
5210 new[new_used] = '\0';
5211
Michal Vaskod3678892020-05-21 10:06:58 +02005212 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005213 set->type = LYXP_SET_STRING;
5214 set->val.str = new;
5215
5216 return LY_SUCCESS;
5217}
5218
5219/**
5220 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5221 * with true value.
5222 *
5223 * @param[in] args Array of arguments.
5224 * @param[in] arg_count Count of elements in @p args.
5225 * @param[in,out] set Context and result set at the same time.
5226 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005227 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005228 */
5229static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005230xpath_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 +02005231{
5232 if (options & LYXP_SCNODE_ALL) {
5233 set_scnode_clear_ctx(set);
5234 return LY_SUCCESS;
5235 }
5236
5237 set_fill_boolean(set, 1);
5238 return LY_SUCCESS;
5239}
5240
5241/*
5242 * moveto functions
5243 *
5244 * They and only they actually change the context (set).
5245 */
5246
5247/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005248 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005249 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005250 * XPath @p set is expected to be a (sc)node set!
5251 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005252 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5253 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005254 * @param[in] set Set with general XPath context.
5255 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005256 * @param[out] moveto_mod Expected module of a matching node.
5257 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005258 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005259static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005260moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5261 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005262{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005263 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005264 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005265 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005266
Michal Vasko2104e9f2020-03-06 08:23:25 +01005267 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5268
Michal Vasko6346ece2019-09-24 13:12:53 +02005269 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5270 /* specific module */
5271 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005272 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005273
Michal Vasko004d3152020-06-11 19:59:22 +02005274 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005275 if (!mod || !mod->implemented) {
Michal Vasko2104e9f2020-03-06 08:23:25 +01005276 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005277 LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko2104e9f2020-03-06 08:23:25 +01005278 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005279 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko2104e9f2020-03-06 08:23:25 +01005280 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005281 return LY_EVALID;
5282 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005283
Michal Vasko6346ece2019-09-24 13:12:53 +02005284 *qname += pref_len + 1;
5285 *qname_len -= pref_len + 1;
5286 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5287 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005288 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005289 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005290 switch (set->format) {
5291 case LY_PREF_SCHEMA:
5292 case LY_PREF_SCHEMA_RESOLVED:
5293 /* current module */
5294 mod = set->cur_mod;
5295 break;
5296 case LY_PREF_JSON:
5297 /* inherit parent (context node) module */
5298 if (ctx_scnode) {
5299 mod = ctx_scnode->module;
5300 } else {
5301 mod = NULL;
5302 }
5303 break;
5304 case LY_PREF_XML:
5305 /* not defined */
5306 LOGINT_RET(set->ctx);
5307 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005308 }
5309
Michal Vasko6346ece2019-09-24 13:12:53 +02005310 *moveto_mod = mod;
5311 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005312}
5313
5314/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005315 * @brief Move context @p set to the root. Handles absolute path.
5316 * Result is LYXP_SET_NODE_SET.
5317 *
5318 * @param[in,out] set Set to use.
5319 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005320 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005321 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005322static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005323moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005324{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005325 if (!set) {
Michal Vaskob0099a92020-08-31 14:55:23 +02005326 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005327 }
5328
5329 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005330 set_scnode_clear_ctx(set);
Michal Vaskob0099a92020-08-31 14:55:23 +02005331 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005332 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005333 set->type = LYXP_SET_NODE_SET;
5334 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005335 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005336 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005337
5338 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005339}
5340
5341/**
Michal Vaskoa1424542019-11-14 16:08:52 +01005342 * @brief Check whether a node has some unresolved "when".
5343 *
5344 * @param[in] node Node to check.
5345 * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when")
5346 */
5347static LY_ERR
5348moveto_when_check(const struct lyd_node *node)
5349{
5350 const struct lysc_node *schema;
5351
5352 if (!node) {
5353 return LY_SUCCESS;
5354 }
5355
5356 schema = node->schema;
5357 do {
5358 if (schema->when && !(node->flags & LYD_WHEN_TRUE)) {
5359 return LY_EINCOMPLETE;
5360 }
5361 schema = schema->parent;
5362 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
5363
5364 return LY_SUCCESS;
5365}
5366
5367/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005368 * @brief Check @p node as a part of NameTest processing.
5369 *
5370 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005371 * @param[in] ctx_node Context node.
5372 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005373 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005374 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005375 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5376 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005377 */
5378static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005379moveto_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 +02005380 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005381{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005382 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5383 switch (set->format) {
5384 case LY_PREF_SCHEMA:
5385 case LY_PREF_SCHEMA_RESOLVED:
5386 /* use current module */
5387 moveto_mod = set->cur_mod;
5388 break;
5389 case LY_PREF_JSON:
5390 /* inherit module of the context node, if any */
5391 if (ctx_node) {
5392 moveto_mod = ctx_node->schema->module;
5393 }
5394 break;
5395 case LY_PREF_XML:
5396 /* not defined */
5397 LOGINT(set->ctx);
5398 return LY_EINVAL;
5399 }
5400 }
5401
Michal Vasko03ff5a72019-09-11 13:49:33 +02005402 /* module check */
5403 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005404 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005405 }
5406
Michal Vasko5c4e5892019-11-14 12:31:38 +01005407 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005408 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005409 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005410 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5411 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005412 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005413 }
5414
5415 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005416 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005417 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005418 }
5419
Michal Vaskoa1424542019-11-14 16:08:52 +01005420 /* when check */
5421 if (moveto_when_check(node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005422 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005423 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005424
5425 /* match */
5426 return LY_SUCCESS;
5427}
5428
5429/**
5430 * @brief Check @p node as a part of schema NameTest processing.
5431 *
5432 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005433 * @param[in] ctx_scnode Context node.
5434 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005435 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005436 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005437 * @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 +02005438 */
5439static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005440moveto_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 +02005441 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005442{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005443 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5444 switch (set->format) {
5445 case LY_PREF_SCHEMA:
5446 case LY_PREF_SCHEMA_RESOLVED:
5447 /* use current module */
5448 moveto_mod = set->cur_mod;
5449 break;
5450 case LY_PREF_JSON:
5451 /* inherit module of the context node, if any */
5452 if (ctx_scnode) {
5453 moveto_mod = ctx_scnode->module;
5454 }
5455 break;
5456 case LY_PREF_XML:
5457 /* not defined */
5458 LOGINT(set->ctx);
5459 return LY_EINVAL;
5460 }
5461 }
5462
Michal Vasko03ff5a72019-09-11 13:49:33 +02005463 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005464 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005465 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005466 }
5467
5468 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005469 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005470 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005471 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005472 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005473 }
5474
5475 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005476 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005477 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005478 }
5479
5480 /* match */
5481 return LY_SUCCESS;
5482}
5483
5484/**
Michal Vaskod3678892020-05-21 10:06:58 +02005485 * @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 +02005486 *
5487 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005488 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005489 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005490 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5491 */
5492static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005493moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005494{
Michal Vaskof03ed032020-03-04 13:31:44 +01005495 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005496 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005497 LY_ERR rc;
5498
Michal Vaskod3678892020-05-21 10:06:58 +02005499 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005500 return LY_SUCCESS;
5501 }
5502
5503 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005504 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 +02005505 return LY_EVALID;
5506 }
5507
Michal Vasko03ff5a72019-09-11 13:49:33 +02005508 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005509 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005510
5511 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 +01005512 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005513
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005514 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005515 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005516 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005517 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005518 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005519 ctx_node = set->val.nodes[i].node;
5520 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005521 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005522
Michal Vaskod3678892020-05-21 10:06:58 +02005523 for (sub = siblings; sub; sub = sub->next) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005524 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02005525 if (rc == LY_SUCCESS) {
5526 if (!replaced) {
5527 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5528 replaced = 1;
5529 } else {
5530 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005531 }
Michal Vaskod3678892020-05-21 10:06:58 +02005532 ++i;
5533 } else if (rc == LY_EINCOMPLETE) {
5534 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005535 }
5536 }
5537
5538 if (!replaced) {
5539 /* no match */
5540 set_remove_node(set, i);
5541 }
5542 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005543
5544 return LY_SUCCESS;
5545}
5546
5547/**
Michal Vaskod3678892020-05-21 10:06:58 +02005548 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5549 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005550 *
5551 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005552 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005553 * @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 +02005554 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5555 */
5556static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02005557moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates)
Michal Vaskod3678892020-05-21 10:06:58 +02005558{
Michal Vasko004d3152020-06-11 19:59:22 +02005559 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005560 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005561 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005562 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005563
Michal Vasko004d3152020-06-11 19:59:22 +02005564 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005565
5566 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005567 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005568 }
5569
5570 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005571 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 +02005572 ret = LY_EVALID;
5573 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005574 }
5575
5576 /* context check for all the nodes since we have the schema node */
5577 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5578 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005579 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005580 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5581 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005582 lyxp_set_free_content(set);
5583 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005584 }
5585
5586 /* create specific data instance if needed */
5587 if (scnode->nodetype == LYS_LIST) {
5588 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5589 } else if (scnode->nodetype == LYS_LEAFLIST) {
5590 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005591 }
5592
5593 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005594 siblings = NULL;
5595
5596 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5597 assert(!set->val.nodes[i].node);
5598
5599 /* search in all the trees */
5600 siblings = set->tree;
5601 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5602 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005603 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005604 }
5605
5606 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005607 if (inst) {
5608 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005609 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005610 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005611 }
5612
5613 /* when check */
5614 if (sub && moveto_when_check(sub)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005615 ret = LY_EINCOMPLETE;
5616 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005617 }
5618
5619 if (sub) {
5620 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005621 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005622 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005623 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005624 /* no match */
5625 set_remove_node(set, i);
5626 }
5627 }
5628
Michal Vasko004d3152020-06-11 19:59:22 +02005629cleanup:
5630 lyd_free_tree(inst);
5631 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005632}
5633
5634/**
5635 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5636 *
5637 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005638 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005639 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005640 * @param[in] options XPath options.
5641 * @return LY_ERR
5642 */
5643static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005644moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005645{
Radek Krejci857189e2020-09-01 13:26:36 +02005646 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005647 uint32_t getnext_opts;
5648 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005649 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005650 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005651 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005652
Michal Vaskod3678892020-05-21 10:06:58 +02005653 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005654 return LY_SUCCESS;
5655 }
5656
5657 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005658 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 +02005659 return LY_EVALID;
5660 }
5661
Michal Vaskocafad9d2019-11-07 15:20:03 +01005662 /* getnext opts */
5663 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
5664 if (options & LYXP_SCNODE_OUTPUT) {
5665 getnext_opts |= LYS_GETNEXT_OUTPUT;
5666 }
5667
Michal Vasko03ff5a72019-09-11 13:49:33 +02005668 orig_used = set->used;
5669 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005670 uint32_t idx;
5671
Michal Vasko03ff5a72019-09-11 13:49:33 +02005672 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005673 if (set->val.scnodes[i].in_ctx != -2) {
5674 continue;
5675 }
5676
5677 /* remember context node */
5678 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005679 } else {
5680 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005681 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005682
5683 start_parent = set->val.scnodes[i].scnode;
5684
5685 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 +02005686 /* 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 +02005687 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005688 mod_idx = 0;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005689 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005690 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005691 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005692 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005693 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005694 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5695
Michal Vasko03ff5a72019-09-11 13:49:33 +02005696 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005697 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005698 set->val.scnodes[idx].in_ctx = 2;
5699 temp_ctx = 1;
5700 }
5701 }
5702 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005703 }
5704
Michal Vasko519fd602020-05-26 12:17:39 +02005705 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5706 iter = NULL;
5707 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005708 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005709 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5710
5711 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005712 set->val.scnodes[idx].in_ctx = 2;
5713 temp_ctx = 1;
5714 }
5715 }
5716 }
5717 }
5718 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005719
5720 /* correct temporary in_ctx values */
5721 if (temp_ctx) {
5722 for (i = 0; i < orig_used; ++i) {
5723 if (set->val.scnodes[i].in_ctx == 2) {
5724 set->val.scnodes[i].in_ctx = 1;
5725 }
5726 }
5727 }
5728
5729 return LY_SUCCESS;
5730}
5731
5732/**
Michal Vaskod3678892020-05-21 10:06:58 +02005733 * @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 +02005734 * Context position aware.
5735 *
5736 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005737 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005738 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005739 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5740 */
5741static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005742moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005743{
5744 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005745 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005746 struct lyxp_set ret_set;
5747 LY_ERR rc;
5748
Michal Vaskod3678892020-05-21 10:06:58 +02005749 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005750 return LY_SUCCESS;
5751 }
5752
5753 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005754 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 +02005755 return LY_EVALID;
5756 }
5757
Michal Vasko9f96a052020-03-10 09:41:45 +01005758 /* 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 +02005759 rc = moveto_node(set, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005760 LY_CHECK_RET(rc);
5761
Michal Vasko6346ece2019-09-24 13:12:53 +02005762 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005763 set_init(&ret_set, set);
5764 for (i = 0; i < set->used; ++i) {
5765
5766 /* TREE DFS */
5767 start = set->val.nodes[i].node;
5768 for (elem = next = start; elem; elem = next) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005769 rc = moveto_node_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005770 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005771 /* add matching node into result set */
5772 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5773 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5774 /* the node is a duplicate, we'll process it later in the set */
5775 goto skip_children;
5776 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005777 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005778 return rc;
5779 } else if (rc == LY_EINVAL) {
5780 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005781 }
5782
5783 /* TREE DFS NEXT ELEM */
5784 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005785 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005786 if (!next) {
5787skip_children:
5788 /* no children, so try siblings, but only if it's not the start,
5789 * that is considered to be the root and it's siblings are not traversed */
5790 if (elem != start) {
5791 next = elem->next;
5792 } else {
5793 break;
5794 }
5795 }
5796 while (!next) {
5797 /* no siblings, go back through the parents */
5798 if ((struct lyd_node *)elem->parent == start) {
5799 /* we are done, no next element to process */
5800 break;
5801 }
5802 /* parent is already processed, go to its sibling */
5803 elem = (struct lyd_node *)elem->parent;
5804 next = elem->next;
5805 }
5806 }
5807 }
5808
5809 /* make the temporary set the current one */
5810 ret_set.ctx_pos = set->ctx_pos;
5811 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005812 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005813 memcpy(set, &ret_set, sizeof *set);
5814
5815 return LY_SUCCESS;
5816}
5817
5818/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005819 * @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 +02005820 *
5821 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005822 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005823 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005824 * @param[in] options XPath options.
5825 * @return LY_ERR
5826 */
5827static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005828moveto_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 +02005829{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005830 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005831 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005832 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005833
Michal Vaskod3678892020-05-21 10:06:58 +02005834 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005835 return LY_SUCCESS;
5836 }
5837
5838 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005839 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 +02005840 return LY_EVALID;
5841 }
5842
Michal Vasko03ff5a72019-09-11 13:49:33 +02005843 orig_used = set->used;
5844 for (i = 0; i < orig_used; ++i) {
5845 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005846 if (set->val.scnodes[i].in_ctx != -2) {
5847 continue;
5848 }
5849
5850 /* remember context node */
5851 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005852 } else {
5853 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005854 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005855
5856 /* TREE DFS */
5857 start = set->val.scnodes[i].scnode;
5858 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005859 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5860 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005861 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005862 }
5863
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005864 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005865 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005866 uint32_t idx;
5867
5868 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005869 set->val.scnodes[idx].in_ctx = 1;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005870 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005871 /* we will process it later in the set */
5872 goto skip_children;
5873 }
5874 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005875 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005876 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005877 } else if (rc == LY_EINVAL) {
5878 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005879 }
5880
5881next_iter:
5882 /* TREE DFS NEXT ELEM */
5883 /* select element for the next run - children first */
5884 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005885 if (!next) {
5886skip_children:
5887 /* no children, so try siblings, but only if it's not the start,
5888 * that is considered to be the root and it's siblings are not traversed */
5889 if (elem != start) {
5890 next = elem->next;
5891 } else {
5892 break;
5893 }
5894 }
5895 while (!next) {
5896 /* no siblings, go back through the parents */
5897 if (elem->parent == start) {
5898 /* we are done, no next element to process */
5899 break;
5900 }
5901 /* parent is already processed, go to its sibling */
5902 elem = elem->parent;
5903 next = elem->next;
5904 }
5905 }
5906 }
5907
5908 return LY_SUCCESS;
5909}
5910
5911/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005912 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005913 * Indirectly context position aware.
5914 *
5915 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005916 * @param[in] mod Matching metadata module, NULL for any.
5917 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005918 * @return LY_ERR
5919 */
5920static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005921moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005922{
Michal Vasko9f96a052020-03-10 09:41:45 +01005923 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005924
Michal Vaskod3678892020-05-21 10:06:58 +02005925 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005926 return LY_SUCCESS;
5927 }
5928
5929 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005930 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 +02005931 return LY_EVALID;
5932 }
5933
Radek Krejci1deb5be2020-08-26 16:43:36 +02005934 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005935 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005936
5937 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5938 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005939 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005940 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005941
5942 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005943 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005944 continue;
5945 }
5946
Michal Vaskod3678892020-05-21 10:06:58 +02005947 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005948 /* match */
5949 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005950 set->val.meta[i].meta = sub;
5951 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005952 /* pos does not change */
5953 replaced = 1;
5954 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005955 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 +02005956 }
5957 ++i;
5958 }
5959 }
5960 }
5961
5962 if (!replaced) {
5963 /* no match */
5964 set_remove_node(set, i);
5965 }
5966 }
5967
5968 return LY_SUCCESS;
5969}
5970
5971/**
5972 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005973 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005974 *
5975 * @param[in,out] set1 Set to use for the result.
5976 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005977 * @return LY_ERR
5978 */
5979static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005980moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005981{
5982 LY_ERR rc;
5983
Michal Vaskod3678892020-05-21 10:06:58 +02005984 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005985 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 +02005986 return LY_EVALID;
5987 }
5988
5989 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005990 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005991 return LY_SUCCESS;
5992 }
5993
Michal Vaskod3678892020-05-21 10:06:58 +02005994 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005995 memcpy(set1, set2, sizeof *set1);
5996 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005997 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005998 return LY_SUCCESS;
5999 }
6000
6001 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006002 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006003
6004 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006005 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006006 LY_CHECK_RET(rc);
6007
6008 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006009 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006010
6011 return LY_SUCCESS;
6012}
6013
6014/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006015 * @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 +02006016 * Context position aware.
6017 *
6018 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006019 * @param[in] mod Matching metadata module, NULL for any.
6020 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006021 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6022 */
6023static int
Michal Vaskod3678892020-05-21 10:06:58 +02006024moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006025{
Michal Vasko9f96a052020-03-10 09:41:45 +01006026 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006027 struct lyxp_set *set_all_desc = NULL;
6028 LY_ERR rc;
6029
Michal Vaskod3678892020-05-21 10:06:58 +02006030 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006031 return LY_SUCCESS;
6032 }
6033
6034 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006035 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 +02006036 return LY_EVALID;
6037 }
6038
Michal Vasko03ff5a72019-09-11 13:49:33 +02006039 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6040 * but it likely won't be used much, so it's a waste of time */
6041 /* copy the context */
6042 set_all_desc = set_copy(set);
6043 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskod3678892020-05-21 10:06:58 +02006044 rc = moveto_node_alldesc(set_all_desc, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006045 if (rc != LY_SUCCESS) {
6046 lyxp_set_free(set_all_desc);
6047 return rc;
6048 }
6049 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006050 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006051 if (rc != LY_SUCCESS) {
6052 lyxp_set_free(set_all_desc);
6053 return rc;
6054 }
6055 lyxp_set_free(set_all_desc);
6056
Radek Krejci1deb5be2020-08-26 16:43:36 +02006057 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006058 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006059
6060 /* only attributes of an elem can be in the result, skip all the rest,
6061 * we have all attributes qualified in lyd tree */
6062 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006063 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006064 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006065 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006066 continue;
6067 }
6068
Michal Vaskod3678892020-05-21 10:06:58 +02006069 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006070 /* match */
6071 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006072 set->val.meta[i].meta = sub;
6073 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006074 /* pos does not change */
6075 replaced = 1;
6076 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006077 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 +02006078 }
6079 ++i;
6080 }
6081 }
6082 }
6083
6084 if (!replaced) {
6085 /* no match */
6086 set_remove_node(set, i);
6087 }
6088 }
6089
6090 return LY_SUCCESS;
6091}
6092
6093/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006094 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6095 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006096 *
6097 * @param[in] parent Current parent.
6098 * @param[in] parent_pos Position of @p parent.
6099 * @param[in] parent_type Node type of @p parent.
6100 * @param[in,out] to_set Set to use.
6101 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006102 * @param[in] options XPath options.
6103 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6104 */
6105static LY_ERR
6106moveto_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 +02006107 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006108{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006109 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006110 LY_ERR rc;
6111
6112 switch (parent_type) {
6113 case LYXP_NODE_ROOT:
6114 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006115 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006116
Michal Vasko61ac2f62020-05-25 12:39:51 +02006117 /* add all top-level nodes as elements */
6118 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006119 break;
6120 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006121 /* add just the text node of this term element node */
6122 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006123 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6124 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6125 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006126 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006127 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006128
6129 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006130 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006131 break;
6132 default:
6133 LOGINT_RET(parent->schema->module->ctx);
6134 }
6135
Michal Vasko61ac2f62020-05-25 12:39:51 +02006136 /* add all top-level nodes as elements */
6137 LY_LIST_FOR(first, iter) {
6138 /* context check */
6139 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6140 continue;
6141 }
6142
6143 /* when check */
6144 if (moveto_when_check(iter)) {
6145 return LY_EINCOMPLETE;
6146 }
6147
6148 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6149 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6150
6151 /* also add all the children of this node, recursively */
6152 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6153 LY_CHECK_RET(rc);
6154 }
6155 }
6156
Michal Vasko03ff5a72019-09-11 13:49:33 +02006157 return LY_SUCCESS;
6158}
6159
6160/**
6161 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6162 * (or LYXP_SET_EMPTY). Context position aware.
6163 *
6164 * @param[in,out] set Set to use.
6165 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6166 * @param[in] options XPath options.
6167 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6168 */
6169static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006170moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006171{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006172 struct lyxp_set ret_set;
6173 LY_ERR rc;
6174
Michal Vaskod3678892020-05-21 10:06:58 +02006175 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006176 return LY_SUCCESS;
6177 }
6178
6179 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006180 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 +02006181 return LY_EVALID;
6182 }
6183
6184 /* nothing to do */
6185 if (!all_desc) {
6186 return LY_SUCCESS;
6187 }
6188
Michal Vasko03ff5a72019-09-11 13:49:33 +02006189 /* add all the children, they get added recursively */
6190 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006191 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006192 /* copy the current node to tmp */
6193 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6194
6195 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006196 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006197 continue;
6198 }
6199
Michal Vasko03ff5a72019-09-11 13:49:33 +02006200 /* add all the children */
6201 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 +02006202 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006203 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006204 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006205 return rc;
6206 }
6207 }
6208
6209 /* use the temporary set as the current one */
6210 ret_set.ctx_pos = set->ctx_pos;
6211 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006212 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006213 memcpy(set, &ret_set, sizeof *set);
6214
6215 return LY_SUCCESS;
6216}
6217
6218/**
6219 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6220 * (or LYXP_SET_EMPTY).
6221 *
6222 * @param[in,out] set Set to use.
6223 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6224 * @param[in] options XPath options.
6225 * @return LY_ERR
6226 */
6227static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006228moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006229{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006230 uint32_t getnext_opts;
6231 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006232 const struct lysc_node *iter, *start_parent;
6233 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006234
Michal Vaskod3678892020-05-21 10:06:58 +02006235 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006236 return LY_SUCCESS;
6237 }
6238
6239 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006240 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 +02006241 return LY_EVALID;
6242 }
6243
6244 /* nothing to do */
6245 if (!all_desc) {
6246 return LY_SUCCESS;
6247 }
6248
Michal Vasko519fd602020-05-26 12:17:39 +02006249 /* getnext opts */
6250 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
6251 if (options & LYXP_SCNODE_OUTPUT) {
6252 getnext_opts |= LYS_GETNEXT_OUTPUT;
6253 }
6254
6255 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006256 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006257 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006258 if (set->val.scnodes[i].in_ctx != -2) {
6259 continue;
6260 }
6261
Michal Vasko519fd602020-05-26 12:17:39 +02006262 /* remember context node */
6263 set->val.scnodes[i].in_ctx = -1;
6264 } else {
6265 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006266 }
6267
Michal Vasko519fd602020-05-26 12:17:39 +02006268 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006269
Michal Vasko519fd602020-05-26 12:17:39 +02006270 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6271 /* it can actually be in any module, it's all <running> */
6272 mod_idx = 0;
6273 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6274 iter = NULL;
6275 /* module may not be implemented */
6276 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6277 /* context check */
6278 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6279 continue;
6280 }
6281
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006282 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006283 /* throw away the insert index, we want to consider that node again, recursively */
6284 }
6285 }
6286
6287 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6288 iter = NULL;
6289 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006290 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006291 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006292 continue;
6293 }
6294
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006295 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006296 }
6297 }
6298 }
6299
6300 return LY_SUCCESS;
6301}
6302
6303/**
6304 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6305 * (or LYXP_SET_EMPTY). Context position aware.
6306 *
6307 * @param[in] set Set to use.
6308 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6309 * @param[in] options XPath options.
6310 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6311 */
6312static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006313moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006314{
6315 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006316 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006317 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006318
Michal Vaskod3678892020-05-21 10:06:58 +02006319 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006320 return LY_SUCCESS;
6321 }
6322
6323 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006324 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 +02006325 return LY_EVALID;
6326 }
6327
6328 if (all_desc) {
6329 /* <path>//.. == <path>//./.. */
6330 rc = moveto_self(set, 1, options);
6331 LY_CHECK_RET(rc);
6332 }
6333
Radek Krejci1deb5be2020-08-26 16:43:36 +02006334 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006335 node = set->val.nodes[i].node;
6336
6337 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6338 new_node = (struct lyd_node *)node->parent;
6339 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6340 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006341 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6342 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006343 if (!new_node) {
6344 LOGINT_RET(set->ctx);
6345 }
6346 } else {
6347 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006348 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006349 continue;
6350 }
6351
Michal Vaskoa1424542019-11-14 16:08:52 +01006352 /* when check */
6353 if (moveto_when_check(new_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006354 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006355 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006356
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006357 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006358 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006359 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006360
Michal Vasko03ff5a72019-09-11 13:49:33 +02006361 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006362 /* 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 +02006363 new_type = LYXP_NODE_ELEM;
6364 }
6365
Michal Vasko03ff5a72019-09-11 13:49:33 +02006366 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006367 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006368 } else {
6369 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006370 }
6371 }
6372
Michal Vasko2caefc12019-11-14 16:07:56 +01006373 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006374 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006375
6376 return LY_SUCCESS;
6377}
6378
6379/**
6380 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6381 * (or LYXP_SET_EMPTY).
6382 *
6383 * @param[in] set Set to use.
6384 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6385 * @param[in] options XPath options.
6386 * @return LY_ERR
6387 */
6388static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006389moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006390{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006391 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006392 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006393 const struct lysc_node *node, *new_node;
6394 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006395
Michal Vaskod3678892020-05-21 10:06:58 +02006396 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006397 return LY_SUCCESS;
6398 }
6399
6400 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006401 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 +02006402 return LY_EVALID;
6403 }
6404
6405 if (all_desc) {
6406 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006407 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006408 }
6409
Michal Vasko03ff5a72019-09-11 13:49:33 +02006410 orig_used = set->used;
6411 for (i = 0; i < orig_used; ++i) {
6412 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006413 if (set->val.scnodes[i].in_ctx != -2) {
6414 continue;
6415 }
6416
6417 /* remember context node */
6418 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01006419 } else {
6420 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006421 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006422
6423 node = set->val.scnodes[i].scnode;
6424
6425 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006426 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006427 } else {
6428 /* root does not have a parent */
6429 continue;
6430 }
6431
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006432 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006433 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006434 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006435
Michal Vasko03ff5a72019-09-11 13:49:33 +02006436 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006437 /* 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 +02006438 new_type = LYXP_NODE_ELEM;
6439 }
6440
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006441 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6442 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006443 set->val.scnodes[idx].in_ctx = 2;
6444 temp_ctx = 1;
6445 }
6446 }
6447
6448 if (temp_ctx) {
6449 for (i = 0; i < orig_used; ++i) {
6450 if (set->val.scnodes[i].in_ctx == 2) {
6451 set->val.scnodes[i].in_ctx = 1;
6452 }
6453 }
6454 }
6455
6456 return LY_SUCCESS;
6457}
6458
6459/**
6460 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6461 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6462 *
6463 * @param[in,out] set1 Set to use for the result.
6464 * @param[in] set2 Set acting as the second operand for @p op.
6465 * @param[in] op Comparison operator to process.
6466 * @param[in] options XPath options.
6467 * @return LY_ERR
6468 */
6469static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006470moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006471{
6472 /*
6473 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6474 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6475 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6476 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6477 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6478 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6479 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6480 *
6481 * '=' or '!='
6482 * BOOLEAN + BOOLEAN
6483 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6484 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6485 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6486 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6487 * NUMBER + NUMBER
6488 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6489 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6490 * STRING + STRING
6491 *
6492 * '<=', '<', '>=', '>'
6493 * NUMBER + NUMBER
6494 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6495 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6496 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6497 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6498 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6499 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6500 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6501 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6502 */
6503 struct lyxp_set iter1, iter2;
6504 int result;
6505 int64_t i;
6506 LY_ERR rc;
6507
Michal Vasko004d3152020-06-11 19:59:22 +02006508 memset(&iter1, 0, sizeof iter1);
6509 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006510
6511 /* iterative evaluation with node-sets */
6512 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6513 if (set1->type == LYXP_SET_NODE_SET) {
6514 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006515 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006516 switch (set2->type) {
6517 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006518 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006519 break;
6520 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006521 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006522 break;
6523 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006524 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006525 break;
6526 }
6527 LY_CHECK_RET(rc);
6528
Michal Vasko4c7763f2020-07-27 17:40:37 +02006529 /* canonize set2 */
6530 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6531
6532 /* compare recursively */
6533 rc = moveto_op_comp(&iter1, &iter2, op, options);
6534 lyxp_set_free_content(&iter2);
6535 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006536
6537 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006538 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006539 set_fill_boolean(set1, 1);
6540 return LY_SUCCESS;
6541 }
6542 }
6543 } else {
6544 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006545 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006546 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006547 case LYXP_SET_NUMBER:
6548 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6549 break;
6550 case LYXP_SET_BOOLEAN:
6551 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6552 break;
6553 default:
6554 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6555 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006556 }
6557 LY_CHECK_RET(rc);
6558
Michal Vasko4c7763f2020-07-27 17:40:37 +02006559 /* canonize set1 */
6560 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 +02006561
Michal Vasko4c7763f2020-07-27 17:40:37 +02006562 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006563 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006564 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006565 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006566
6567 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006568 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006569 set_fill_boolean(set1, 1);
6570 return LY_SUCCESS;
6571 }
6572 }
6573 }
6574
6575 /* false for all nodes */
6576 set_fill_boolean(set1, 0);
6577 return LY_SUCCESS;
6578 }
6579
6580 /* first convert properly */
6581 if ((op[0] == '=') || (op[0] == '!')) {
6582 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006583 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6584 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006585 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006586 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006587 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006588 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006589 LY_CHECK_RET(rc);
6590 } /* else we have 2 strings */
6591 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006592 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006593 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006594 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006595 LY_CHECK_RET(rc);
6596 }
6597
6598 assert(set1->type == set2->type);
6599
6600 /* compute result */
6601 if (op[0] == '=') {
6602 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006603 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006604 } else if (set1->type == LYXP_SET_NUMBER) {
6605 result = (set1->val.num == set2->val.num);
6606 } else {
6607 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006608 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006609 }
6610 } else if (op[0] == '!') {
6611 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006612 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006613 } else if (set1->type == LYXP_SET_NUMBER) {
6614 result = (set1->val.num != set2->val.num);
6615 } else {
6616 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006617 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006618 }
6619 } else {
6620 assert(set1->type == LYXP_SET_NUMBER);
6621 if (op[0] == '<') {
6622 if (op[1] == '=') {
6623 result = (set1->val.num <= set2->val.num);
6624 } else {
6625 result = (set1->val.num < set2->val.num);
6626 }
6627 } else {
6628 if (op[1] == '=') {
6629 result = (set1->val.num >= set2->val.num);
6630 } else {
6631 result = (set1->val.num > set2->val.num);
6632 }
6633 }
6634 }
6635
6636 /* assign result */
6637 if (result) {
6638 set_fill_boolean(set1, 1);
6639 } else {
6640 set_fill_boolean(set1, 0);
6641 }
6642
6643 return LY_SUCCESS;
6644}
6645
6646/**
6647 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6648 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6649 *
6650 * @param[in,out] set1 Set to use for the result.
6651 * @param[in] set2 Set acting as the second operand for @p op.
6652 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006653 * @return LY_ERR
6654 */
6655static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006656moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006657{
6658 LY_ERR rc;
6659
6660 /* unary '-' */
6661 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006662 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006663 LY_CHECK_RET(rc);
6664 set1->val.num *= -1;
6665 lyxp_set_free(set2);
6666 return LY_SUCCESS;
6667 }
6668
6669 assert(set1 && set2);
6670
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006671 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006672 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006673 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006674 LY_CHECK_RET(rc);
6675
6676 switch (op[0]) {
6677 /* '+' */
6678 case '+':
6679 set1->val.num += set2->val.num;
6680 break;
6681
6682 /* '-' */
6683 case '-':
6684 set1->val.num -= set2->val.num;
6685 break;
6686
6687 /* '*' */
6688 case '*':
6689 set1->val.num *= set2->val.num;
6690 break;
6691
6692 /* 'div' */
6693 case 'd':
6694 set1->val.num /= set2->val.num;
6695 break;
6696
6697 /* 'mod' */
6698 case 'm':
6699 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6700 break;
6701
6702 default:
6703 LOGINT_RET(set1->ctx);
6704 }
6705
6706 return LY_SUCCESS;
6707}
6708
6709/*
6710 * eval functions
6711 *
6712 * They execute a parsed XPath expression on some data subtree.
6713 */
6714
6715/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006716 * @brief Evaluate Predicate. Logs directly on error.
6717 *
Michal Vaskod3678892020-05-21 10:06:58 +02006718 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006719 *
6720 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006721 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006722 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6723 * @param[in] options XPath options.
6724 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6725 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6726 */
6727static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006728eval_predicate(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 +02006729{
6730 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006731 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006732 uint32_t orig_pos, orig_size;
6733 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006734 struct lyxp_set set2;
6735 struct lyd_node *orig_parent;
6736
6737 /* '[' */
6738 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006739 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006740 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006741
6742 if (!set) {
6743only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006744 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006745 LY_CHECK_RET(rc);
6746 } else if (set->type == LYXP_SET_NODE_SET) {
6747 /* 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 +01006748 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006749
6750 /* empty set, nothing to evaluate */
6751 if (!set->used) {
6752 goto only_parse;
6753 }
6754
Michal Vasko004d3152020-06-11 19:59:22 +02006755 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006756 orig_pos = 0;
6757 orig_size = set->used;
6758 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006759 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006760 set_init(&set2, set);
6761 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6762 /* remember the node context position for position() and context size for last(),
6763 * predicates should always be evaluated with respect to the child axis (since we do
6764 * not support explicit axes) so we assign positions based on their parents */
6765 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6766 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6767 orig_pos = 1;
6768 } else {
6769 ++orig_pos;
6770 }
6771
6772 set2.ctx_pos = orig_pos;
6773 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006774 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006775
Michal Vasko004d3152020-06-11 19:59:22 +02006776 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006777 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006778 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006779 return rc;
6780 }
6781
6782 /* number is a position */
6783 if (set2.type == LYXP_SET_NUMBER) {
6784 if ((long long)set2.val.num == orig_pos) {
6785 set2.val.num = 1;
6786 } else {
6787 set2.val.num = 0;
6788 }
6789 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006790 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006791
6792 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006793 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006794 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006795 }
6796 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006797 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006798
6799 } else if (set->type == LYXP_SET_SCNODE_SET) {
6800 for (i = 0; i < set->used; ++i) {
6801 if (set->val.scnodes[i].in_ctx == 1) {
6802 /* there is a currently-valid node */
6803 break;
6804 }
6805 }
6806 /* empty set, nothing to evaluate */
6807 if (i == set->used) {
6808 goto only_parse;
6809 }
6810
Michal Vasko004d3152020-06-11 19:59:22 +02006811 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006812
Michal Vasko03ff5a72019-09-11 13:49:33 +02006813 /* set special in_ctx to all the valid snodes */
6814 pred_in_ctx = set_scnode_new_in_ctx(set);
6815
6816 /* use the valid snodes one-by-one */
6817 for (i = 0; i < set->used; ++i) {
6818 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6819 continue;
6820 }
6821 set->val.scnodes[i].in_ctx = 1;
6822
Michal Vasko004d3152020-06-11 19:59:22 +02006823 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006824
Michal Vasko004d3152020-06-11 19:59:22 +02006825 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006826 LY_CHECK_RET(rc);
6827
6828 set->val.scnodes[i].in_ctx = pred_in_ctx;
6829 }
6830
6831 /* restore the state as it was before the predicate */
6832 for (i = 0; i < set->used; ++i) {
6833 if (set->val.scnodes[i].in_ctx == 1) {
6834 set->val.scnodes[i].in_ctx = 0;
6835 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6836 set->val.scnodes[i].in_ctx = 1;
6837 }
6838 }
6839
6840 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006841 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006842 set_fill_set(&set2, set);
6843
Michal Vasko004d3152020-06-11 19:59:22 +02006844 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006845 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006846 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006847 return rc;
6848 }
6849
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006850 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006851 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006852 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006853 }
Michal Vaskod3678892020-05-21 10:06:58 +02006854 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006855 }
6856
6857 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006858 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006859 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006860 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006861 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006862
6863 return LY_SUCCESS;
6864}
6865
6866/**
Michal Vaskod3678892020-05-21 10:06:58 +02006867 * @brief Evaluate Literal. Logs directly on error.
6868 *
6869 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006870 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006871 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6872 */
6873static void
Michal Vasko004d3152020-06-11 19:59:22 +02006874eval_literal(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006875{
6876 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006877 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006878 set_fill_string(set, "", 0);
6879 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006880 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 +02006881 }
6882 }
6883 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006884 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006885 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006886}
6887
6888/**
Michal Vasko004d3152020-06-11 19:59:22 +02006889 * @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 +02006890 *
Michal Vasko004d3152020-06-11 19:59:22 +02006891 * @param[in] exp Full parsed XPath expression.
6892 * @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 +02006893 * @param[in] ctx_node Found schema node as the context for the predicate.
6894 * @param[in] cur_mod Current module for the expression.
6895 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006896 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006897 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006898 * @param[out] predicates Parsed predicates.
6899 * @param[out] pred_type Type of @p predicates.
6900 * @return LY_SUCCESS on success,
6901 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006902 */
6903static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006904eval_name_test_try_compile_predicates(struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *ctx_node,
6905 const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_PREFIX_FORMAT format, void *prefix_data,
6906 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006907{
Michal Vasko004d3152020-06-11 19:59:22 +02006908 LY_ERR ret = LY_SUCCESS;
6909 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006910 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006911 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006912 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006913 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006914
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006915 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006916
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006917 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006918 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006919 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006920 return LY_EINVAL;
6921 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006922 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 +02006923 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006924
Michal Vasko004d3152020-06-11 19:59:22 +02006925 /* learn where the predicates end */
6926 e_idx = *tok_idx;
6927 while (key_count) {
6928 /* '[' */
6929 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6930 return LY_EINVAL;
6931 }
6932 ++e_idx;
6933
6934 /* ']' */
6935 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6936 ++e_idx;
6937 }
6938 ++e_idx;
6939
6940 /* another presumably key predicate parsed */
6941 --key_count;
6942 }
Michal Vasko004d3152020-06-11 19:59:22 +02006943 } else {
6944 /* learn just where this single predicate ends */
6945 e_idx = *tok_idx;
6946
Michal Vaskod3678892020-05-21 10:06:58 +02006947 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006948 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6949 return LY_EINVAL;
6950 }
6951 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006952
6953 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006954 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6955 ++e_idx;
6956 }
6957 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006958 }
6959
Michal Vasko004d3152020-06-11 19:59:22 +02006960 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6961 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6962
6963 /* turn logging off */
6964 prev_lo = ly_log_options(0);
6965
6966 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006967 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
6968 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02006969
6970 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006971 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
6972 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006973 LY_CHECK_GOTO(ret, cleanup);
6974
6975 /* success, the predicate must include all the needed information for hash-based search */
6976 *tok_idx = e_idx;
6977
6978cleanup:
6979 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006980 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02006981 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006982}
6983
6984/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006985 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
6986 * data node(s) could be found using a single hash-based search.
6987 *
6988 * @param[in] node Next context node to check.
6989 * @param[in] name Expected node name.
6990 * @param[in] name_len Length of @p name.
6991 * @param[in] moveto_mod Expected node module.
6992 * @param[in] root_type XPath root type.
6993 * @param[in] no_prefix Whether the node name was unprefixed.
6994 * @param[in] format Prefix format.
6995 * @param[in,out] found Previously found node, is updated.
6996 * @return LY_SUCCESS on success,
6997 * @return LY_ENOT if the whole check failed and hashes cannot be used.
6998 */
6999static LY_ERR
7000eval_name_test_with_predicate_get_scnode(const struct lyd_node *node, const char *name, uint16_t name_len,
7001 const struct lys_module *moveto_mod, enum lyxp_node_type root_type, ly_bool no_prefix, LY_PREFIX_FORMAT format,
7002 const struct lysc_node **found)
7003{
7004 const struct lysc_node *scnode;
7005 const struct lys_module *mod;
7006 uint32_t idx = 0;
7007
7008continue_search:
7009 if (!node) {
7010 if (no_prefix && (format == LY_PREF_JSON)) {
7011 /* search all modules for a single match */
7012 while ((mod = ly_ctx_get_module_iter(moveto_mod->ctx, &idx))) {
7013 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7014 if (scnode) {
7015 /* we have found a match */
7016 break;
7017 }
7018 }
7019
7020 /* all modules searched */
7021 idx = 0;
7022 } else {
7023 /* search in top-level */
7024 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7025 }
7026 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
7027 if (no_prefix && (format == LY_PREF_JSON)) {
7028 /* we must adjust the module to inherit the one from the context node */
7029 moveto_mod = node->schema->module;
7030 }
7031
7032 /* search in children, do not repeat the same search */
7033 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
7034 } else {
7035 /* skip redundant search */
7036 scnode = NULL;
7037 }
7038
7039 /* additional context check */
7040 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7041 scnode = NULL;
7042 }
7043
7044 if (scnode) {
7045 if (*found) {
7046 /* we found a schema node with the same name but at different level, give up, too complicated
7047 * (more hash-based searches would be required, not supported) */
7048 return LY_ENOT;
7049 } else {
7050 /* remember the found schema node and continue to make sure it can be used */
7051 *found = scnode;
7052 }
7053 }
7054
7055 if (idx) {
7056 /* continue searching all the following models */
7057 goto continue_search;
7058 }
7059
7060 return LY_SUCCESS;
7061}
7062
7063/**
Michal Vaskod3678892020-05-21 10:06:58 +02007064 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7065 *
7066 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7067 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7068 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7069 *
7070 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007071 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007072 * @param[in] attr_axis Whether to search attributes or standard nodes.
7073 * @param[in] all_desc Whether to search all the descendants or children only.
7074 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7075 * @param[in] options XPath options.
7076 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7077 */
7078static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007079eval_name_test_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool attr_axis, ly_bool all_desc,
7080 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007081{
Michal Vaskod3678892020-05-21 10:06:58 +02007082 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007083 const char *ncname, *ncname_dict = NULL;
7084 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007085 const struct lys_module *moveto_mod = NULL;
7086 const struct lysc_node *scnode = NULL, *ctx_scnode;
Michal Vasko004d3152020-06-11 19:59:22 +02007087 struct ly_path_predicate *predicates = NULL;
7088 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007089 LY_ERR rc = LY_SUCCESS;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007090 ly_bool no_prefix;
Michal Vaskod3678892020-05-21 10:06:58 +02007091
7092 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007093 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007094 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007095
7096 if (!set) {
7097 goto moveto;
7098 }
7099
Michal Vasko004d3152020-06-11 19:59:22 +02007100 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7101 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007102
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007103 /* get the first schema context node */
7104 if (set->used) {
7105 if (set->type == LYXP_SET_NODE_SET) {
7106 ctx_scnode = set->val.nodes[0].node ? set->val.nodes[0].node->schema : NULL;
7107 } else {
7108 ctx_scnode = set->val.scnodes[0].scnode;
7109 }
7110 } else {
7111 ctx_scnode = NULL;
7112 }
7113
7114 /* parse (and skip) module name, use any context node (if available) just so we get some moveto_mod if there
7115 * is no prefix for ::LY_PREF_JSON */
7116 rc = moveto_resolve_model(&ncname, &ncname_len, set, ctx_scnode, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007117 LY_CHECK_GOTO(rc, cleanup);
7118
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007119 if (ncname_len == exp->tok_len[*tok_idx - 1]) {
7120 /* remember that there is no prefix */
7121 no_prefix = 1;
7122 } else {
7123 no_prefix = 0;
7124 }
7125
Michal Vaskod3678892020-05-21 10:06:58 +02007126 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
7127 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007128 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007129 if (eval_name_test_with_predicate_get_scnode(set->val.nodes[i].node, ncname, ncname_len, moveto_mod,
7130 set->root_type, no_prefix, set->format, &scnode)) {
7131 /* check failed */
7132 scnode = NULL;
7133 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007134 }
7135 }
7136
Michal Vasko004d3152020-06-11 19:59:22 +02007137 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7138 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007139 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7140 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007141 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007142 scnode = NULL;
7143 }
7144 }
7145 }
7146
Michal Vasko004d3152020-06-11 19:59:22 +02007147 if (!scnode && moveto_mod) {
7148 /* we are not able to match based on a schema node and not all the modules match,
7149 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007150 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007151 }
7152
7153moveto:
7154 /* move to the attribute(s), data node(s), or schema node(s) */
7155 if (attr_axis) {
7156 if (set && (options & LYXP_SCNODE_ALL)) {
7157 set_scnode_clear_ctx(set);
7158 } else {
7159 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007160 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007161 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007162 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007163 }
7164 LY_CHECK_GOTO(rc, cleanup);
7165 }
7166 } else {
7167 if (set && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007168 int64_t i;
7169
Michal Vaskod3678892020-05-21 10:06:58 +02007170 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007171 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007172 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007173 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007174 }
7175 LY_CHECK_GOTO(rc, cleanup);
7176
7177 for (i = set->used - 1; i > -1; --i) {
7178 if (set->val.scnodes[i].in_ctx > 0) {
7179 break;
7180 }
7181 }
7182 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007183 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02007184 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02007185 ncname_len, ncname, ncname - exp->expr, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007186 free(path);
7187 }
7188 } else {
7189 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007190 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007191 } else {
7192 if (scnode) {
7193 /* we can find the nodes using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02007194 rc = moveto_node_hash(set, scnode, predicates);
Michal Vaskod3678892020-05-21 10:06:58 +02007195 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007196 rc = moveto_node(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007197 }
7198 }
7199 LY_CHECK_GOTO(rc, cleanup);
7200 }
7201 }
7202
7203 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007204 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7205 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007206 LY_CHECK_RET(rc);
7207 }
7208
7209cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007210 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007211 lydict_remove(set->ctx, ncname_dict);
7212 ly_path_predicates_free(set->ctx, pred_type, scnode, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007213 }
Michal Vaskod3678892020-05-21 10:06:58 +02007214 return rc;
7215}
7216
7217/**
7218 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7219 *
7220 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7221 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7222 * [8] NodeType ::= 'text' | 'node'
7223 *
7224 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007225 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007226 * @param[in] attr_axis Whether to search attributes or standard nodes.
7227 * @param[in] all_desc Whether to search all the descendants or children only.
7228 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7229 * @param[in] options XPath options.
7230 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7231 */
7232static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02007233eval_node_type_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool attr_axis, ly_bool all_desc,
Radek Krejci1deb5be2020-08-26 16:43:36 +02007234 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007235{
7236 LY_ERR rc;
7237
7238 /* TODO */
7239 (void)attr_axis;
7240 (void)all_desc;
7241
7242 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007243 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007244 if (set->type == LYXP_SET_SCNODE_SET) {
7245 set_scnode_clear_ctx(set);
7246 /* just for the debug message below */
7247 set = NULL;
7248 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007249 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007250 rc = xpath_node(NULL, 0, set, options);
7251 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007252 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007253 rc = xpath_text(NULL, 0, set, options);
7254 }
7255 LY_CHECK_RET(rc);
7256 }
7257 }
7258 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007259 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007260 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007261
7262 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007263 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007264 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007265 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007266 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007267
7268 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007269 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007270 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007271 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007272 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007273
7274 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007275 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7276 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007277 LY_CHECK_RET(rc);
7278 }
7279
7280 return LY_SUCCESS;
7281}
7282
7283/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007284 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7285 *
7286 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7287 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007288 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007289 *
7290 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007291 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007292 * @param[in] all_desc Whether to search all the descendants or children only.
7293 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7294 * @param[in] options XPath options.
7295 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7296 */
7297static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02007298eval_relative_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007299{
Radek Krejci857189e2020-09-01 13:26:36 +02007300 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007301 LY_ERR rc;
7302
7303 goto step;
7304 do {
7305 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007306 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007307 all_desc = 0;
7308 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007309 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007310 all_desc = 1;
7311 }
7312 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007313 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007314 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007315
7316step:
Michal Vaskod3678892020-05-21 10:06:58 +02007317 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007318 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007319 attr_axis = 1;
7320
7321 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007322 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007323 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007324 } else {
7325 attr_axis = 0;
7326 }
7327
Michal Vasko03ff5a72019-09-11 13:49:33 +02007328 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007329 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007330 case LYXP_TOKEN_DOT:
7331 /* evaluate '.' */
7332 if (set && (options & LYXP_SCNODE_ALL)) {
7333 rc = moveto_scnode_self(set, all_desc, options);
7334 } else {
7335 rc = moveto_self(set, all_desc, options);
7336 }
7337 LY_CHECK_RET(rc);
7338 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007339 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007340 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007341 break;
7342
7343 case LYXP_TOKEN_DDOT:
7344 /* evaluate '..' */
7345 if (set && (options & LYXP_SCNODE_ALL)) {
7346 rc = moveto_scnode_parent(set, all_desc, options);
7347 } else {
7348 rc = moveto_parent(set, all_desc, options);
7349 }
7350 LY_CHECK_RET(rc);
7351 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007352 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007353 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007354 break;
7355
Michal Vasko03ff5a72019-09-11 13:49:33 +02007356 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007357 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007358 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007359 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007360 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007361
Michal Vaskod3678892020-05-21 10:06:58 +02007362 case LYXP_TOKEN_NODETYPE:
7363 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007364 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007365 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007366 break;
7367
7368 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007369 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007370 }
Michal Vasko004d3152020-06-11 19:59:22 +02007371 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007372
7373 return LY_SUCCESS;
7374}
7375
7376/**
7377 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7378 *
7379 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7380 *
7381 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007382 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007383 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7384 * @param[in] options XPath options.
7385 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7386 */
7387static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007388eval_absolute_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007389{
Radek Krejci857189e2020-09-01 13:26:36 +02007390 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007391
7392 if (set) {
7393 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007394 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007395 }
7396
7397 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007398 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007399 /* evaluate '/' - deferred */
7400 all_desc = 0;
7401 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007402 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007403 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007404
Michal Vasko004d3152020-06-11 19:59:22 +02007405 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007406 return LY_SUCCESS;
7407 }
Michal Vasko004d3152020-06-11 19:59:22 +02007408 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007409 case LYXP_TOKEN_DOT:
7410 case LYXP_TOKEN_DDOT:
7411 case LYXP_TOKEN_AT:
7412 case LYXP_TOKEN_NAMETEST:
7413 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007414 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007415 break;
7416 default:
7417 break;
7418 }
7419
Michal Vasko03ff5a72019-09-11 13:49:33 +02007420 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007421 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007422 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7423 all_desc = 1;
7424 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007425 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007426 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007427
Michal Vaskob0099a92020-08-31 14:55:23 +02007428 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007429 }
7430
7431 return LY_SUCCESS;
7432}
7433
7434/**
7435 * @brief Evaluate FunctionCall. Logs directly on error.
7436 *
Michal Vaskod3678892020-05-21 10:06:58 +02007437 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007438 *
7439 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007440 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007441 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7442 * @param[in] options XPath options.
7443 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7444 */
7445static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007446eval_function_call(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007447{
7448 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007449
Radek Krejci1deb5be2020-08-26 16:43:36 +02007450 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007451 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007452 struct lyxp_set **args = NULL, **args_aux;
7453
7454 if (set) {
7455 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007456 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007457 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007458 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007459 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007460 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007461 xpath_func = &xpath_sum;
7462 }
7463 break;
7464 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007465 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007466 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007467 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007468 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007469 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007470 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007471 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007472 xpath_func = &xpath_true;
7473 }
7474 break;
7475 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007476 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007477 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007478 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007479 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007480 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007481 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007482 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007483 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007484 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007485 xpath_func = &xpath_deref;
7486 }
7487 break;
7488 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007489 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007490 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007491 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007492 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007493 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007494 xpath_func = &xpath_string;
7495 }
7496 break;
7497 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007498 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007499 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007500 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007501 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007502 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007503 xpath_func = &xpath_current;
7504 }
7505 break;
7506 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007507 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007508 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007509 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007510 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007511 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007512 xpath_func = &xpath_re_match;
7513 }
7514 break;
7515 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007516 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007517 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007518 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007519 xpath_func = &xpath_translate;
7520 }
7521 break;
7522 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007523 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007524 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007525 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007526 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007527 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007528 xpath_func = &xpath_bit_is_set;
7529 }
7530 break;
7531 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007532 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007533 xpath_func = &xpath_starts_with;
7534 }
7535 break;
7536 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007537 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007538 xpath_func = &xpath_derived_from;
7539 }
7540 break;
7541 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007542 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007543 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007544 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545 xpath_func = &xpath_string_length;
7546 }
7547 break;
7548 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007549 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007550 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007551 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007552 xpath_func = &xpath_substring_after;
7553 }
7554 break;
7555 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007556 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007557 xpath_func = &xpath_substring_before;
7558 }
7559 break;
7560 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007561 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007562 xpath_func = &xpath_derived_from_or_self;
7563 }
7564 break;
7565 }
7566
7567 if (!xpath_func) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007568 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 +02007569 return LY_EVALID;
7570 }
7571 }
7572
7573 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007574 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007575 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007576
7577 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007578 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007579 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007580 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007581 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007582
7583 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007584 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007585 if (set) {
7586 args = malloc(sizeof *args);
7587 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7588 arg_count = 1;
7589 args[0] = set_copy(set);
7590 if (!args[0]) {
7591 rc = LY_EMEM;
7592 goto cleanup;
7593 }
7594
Michal Vasko004d3152020-06-11 19:59:22 +02007595 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007596 LY_CHECK_GOTO(rc, cleanup);
7597 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007598 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007599 LY_CHECK_GOTO(rc, cleanup);
7600 }
7601 }
Michal Vasko004d3152020-06-11 19:59:22 +02007602 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007603 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007604 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007605 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007606
7607 if (set) {
7608 ++arg_count;
7609 args_aux = realloc(args, arg_count * sizeof *args);
7610 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7611 args = args_aux;
7612 args[arg_count - 1] = set_copy(set);
7613 if (!args[arg_count - 1]) {
7614 rc = LY_EMEM;
7615 goto cleanup;
7616 }
7617
Michal Vasko004d3152020-06-11 19:59:22 +02007618 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007619 LY_CHECK_GOTO(rc, cleanup);
7620 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007621 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007622 LY_CHECK_GOTO(rc, cleanup);
7623 }
7624 }
7625
7626 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007627 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007628 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007629 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007630 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007631
7632 if (set) {
7633 /* evaluate function */
7634 rc = xpath_func(args, arg_count, set, options);
7635
7636 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007637 /* merge all nodes from arg evaluations */
7638 for (i = 0; i < arg_count; ++i) {
7639 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007640 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007641 }
7642 }
7643 } else {
7644 rc = LY_SUCCESS;
7645 }
7646
7647cleanup:
7648 for (i = 0; i < arg_count; ++i) {
7649 lyxp_set_free(args[i]);
7650 }
7651 free(args);
7652
7653 return rc;
7654}
7655
7656/**
7657 * @brief Evaluate Number. Logs directly on error.
7658 *
7659 * @param[in] ctx Context for errors.
7660 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007661 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007662 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7663 * @return LY_ERR
7664 */
7665static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007666eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007667{
7668 long double num;
7669 char *endptr;
7670
7671 if (set) {
7672 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007673 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007674 if (errno) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007675 LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7676 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 +02007677 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007678 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007679 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007680 LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7681 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 +02007682 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007683 return LY_EVALID;
7684 }
7685
7686 set_fill_number(set, num);
7687 }
7688
7689 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007690 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007691 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007692 return LY_SUCCESS;
7693}
7694
7695/**
7696 * @brief Evaluate PathExpr. Logs directly on error.
7697 *
Michal Vaskod3678892020-05-21 10:06:58 +02007698 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007699 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7700 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7701 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007702 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007703 *
7704 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007705 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007706 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7707 * @param[in] options XPath options.
7708 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7709 */
7710static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007711eval_path_expr(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007712{
Radek Krejci857189e2020-09-01 13:26:36 +02007713 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007714 LY_ERR rc;
7715
Michal Vasko004d3152020-06-11 19:59:22 +02007716 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007717 case LYXP_TOKEN_PAR1:
7718 /* '(' Expr ')' */
7719
7720 /* '(' */
7721 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007722 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007723 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007724
7725 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007726 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007727 LY_CHECK_RET(rc);
7728
7729 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007730 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007731 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007732 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007733 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007734
7735 parent_pos_pred = 0;
7736 goto predicate;
7737
7738 case LYXP_TOKEN_DOT:
7739 case LYXP_TOKEN_DDOT:
7740 case LYXP_TOKEN_AT:
7741 case LYXP_TOKEN_NAMETEST:
7742 case LYXP_TOKEN_NODETYPE:
7743 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007744 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007745 LY_CHECK_RET(rc);
7746 break;
7747
7748 case LYXP_TOKEN_FUNCNAME:
7749 /* FunctionCall */
7750 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007751 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007752 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007753 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007754 }
7755 LY_CHECK_RET(rc);
7756
7757 parent_pos_pred = 1;
7758 goto predicate;
7759
Michal Vasko3e48bf32020-06-01 08:39:07 +02007760 case LYXP_TOKEN_OPER_PATH:
7761 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007762 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007763 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007764 LY_CHECK_RET(rc);
7765 break;
7766
7767 case LYXP_TOKEN_LITERAL:
7768 /* Literal */
7769 if (!set || (options & LYXP_SCNODE_ALL)) {
7770 if (set) {
7771 set_scnode_clear_ctx(set);
7772 }
Michal Vasko004d3152020-06-11 19:59:22 +02007773 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007774 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007775 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007776 }
7777
7778 parent_pos_pred = 1;
7779 goto predicate;
7780
7781 case LYXP_TOKEN_NUMBER:
7782 /* Number */
7783 if (!set || (options & LYXP_SCNODE_ALL)) {
7784 if (set) {
7785 set_scnode_clear_ctx(set);
7786 }
Michal Vasko004d3152020-06-11 19:59:22 +02007787 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007788 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007789 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007790 }
7791 LY_CHECK_RET(rc);
7792
7793 parent_pos_pred = 1;
7794 goto predicate;
7795
7796 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007797 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 +02007798 &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007799 return LY_EVALID;
7800 }
7801
7802 return LY_SUCCESS;
7803
7804predicate:
7805 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007806 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7807 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007808 LY_CHECK_RET(rc);
7809 }
7810
7811 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007812 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007813
7814 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007815 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007816 all_desc = 0;
7817 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007818 all_desc = 1;
7819 }
7820
7821 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007822 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007823 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007824
Michal Vasko004d3152020-06-11 19:59:22 +02007825 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007826 LY_CHECK_RET(rc);
7827 }
7828
7829 return LY_SUCCESS;
7830}
7831
7832/**
7833 * @brief Evaluate UnionExpr. Logs directly on error.
7834 *
Michal Vaskod3678892020-05-21 10:06:58 +02007835 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007836 *
7837 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007838 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007839 * @param[in] repeat How many times this expression is repeated.
7840 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7841 * @param[in] options XPath options.
7842 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7843 */
7844static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007845eval_union_expr(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 +02007846{
7847 LY_ERR rc = LY_SUCCESS;
7848 struct lyxp_set orig_set, set2;
7849 uint16_t i;
7850
7851 assert(repeat);
7852
7853 set_init(&orig_set, set);
7854 set_init(&set2, set);
7855
7856 set_fill_set(&orig_set, set);
7857
Michal Vasko004d3152020-06-11 19:59:22 +02007858 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007859 LY_CHECK_GOTO(rc, cleanup);
7860
7861 /* ('|' PathExpr)* */
7862 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007863 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007864 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007865 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007866 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007867
7868 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007869 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007870 LY_CHECK_GOTO(rc, cleanup);
7871 continue;
7872 }
7873
7874 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007875 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007876 LY_CHECK_GOTO(rc, cleanup);
7877
7878 /* eval */
7879 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007880 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007881 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007882 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007883 LY_CHECK_GOTO(rc, cleanup);
7884 }
7885 }
7886
7887cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007888 lyxp_set_free_content(&orig_set);
7889 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007890 return rc;
7891}
7892
7893/**
7894 * @brief Evaluate UnaryExpr. Logs directly on error.
7895 *
Michal Vaskod3678892020-05-21 10:06:58 +02007896 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007897 *
7898 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007899 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007900 * @param[in] repeat How many times this expression is repeated.
7901 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7902 * @param[in] options XPath options.
7903 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7904 */
7905static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007906eval_unary_expr(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 +02007907{
7908 LY_ERR rc;
7909 uint16_t this_op, i;
7910
7911 assert(repeat);
7912
7913 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007914 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007915 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007916 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 +02007917
7918 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007919 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007920 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007921 }
7922
Michal Vasko004d3152020-06-11 19:59:22 +02007923 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007924 LY_CHECK_RET(rc);
7925
7926 if (set && (repeat % 2)) {
7927 if (options & LYXP_SCNODE_ALL) {
7928 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7929 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007930 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007931 LY_CHECK_RET(rc);
7932 }
7933 }
7934
7935 return LY_SUCCESS;
7936}
7937
7938/**
7939 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7940 *
Michal Vaskod3678892020-05-21 10:06:58 +02007941 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007942 * | MultiplicativeExpr '*' UnaryExpr
7943 * | MultiplicativeExpr 'div' UnaryExpr
7944 * | MultiplicativeExpr 'mod' UnaryExpr
7945 *
7946 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007947 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007948 * @param[in] repeat How many times this expression is repeated.
7949 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7950 * @param[in] options XPath options.
7951 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7952 */
7953static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007954eval_multiplicative_expr(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 +02007955{
7956 LY_ERR rc;
7957 uint16_t this_op;
7958 struct lyxp_set orig_set, set2;
7959 uint16_t i;
7960
7961 assert(repeat);
7962
7963 set_init(&orig_set, set);
7964 set_init(&set2, set);
7965
7966 set_fill_set(&orig_set, set);
7967
Michal Vasko004d3152020-06-11 19:59:22 +02007968 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007969 LY_CHECK_GOTO(rc, cleanup);
7970
7971 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7972 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007973 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007974
Michal Vasko004d3152020-06-11 19:59:22 +02007975 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007976 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007977 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007978 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007979
7980 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007981 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007982 LY_CHECK_GOTO(rc, cleanup);
7983 continue;
7984 }
7985
7986 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007987 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007988 LY_CHECK_GOTO(rc, cleanup);
7989
7990 /* eval */
7991 if (options & LYXP_SCNODE_ALL) {
7992 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007993 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007994 set_scnode_clear_ctx(set);
7995 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007996 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007997 LY_CHECK_GOTO(rc, cleanup);
7998 }
7999 }
8000
8001cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008002 lyxp_set_free_content(&orig_set);
8003 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008004 return rc;
8005}
8006
8007/**
8008 * @brief Evaluate AdditiveExpr. Logs directly on error.
8009 *
Michal Vaskod3678892020-05-21 10:06:58 +02008010 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008011 * | AdditiveExpr '+' MultiplicativeExpr
8012 * | AdditiveExpr '-' MultiplicativeExpr
8013 *
8014 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008015 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008016 * @param[in] repeat How many times this expression is repeated.
8017 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8018 * @param[in] options XPath options.
8019 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8020 */
8021static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02008022eval_additive_expr(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 +02008023{
8024 LY_ERR rc;
8025 uint16_t this_op;
8026 struct lyxp_set orig_set, set2;
8027 uint16_t i;
8028
8029 assert(repeat);
8030
8031 set_init(&orig_set, set);
8032 set_init(&set2, set);
8033
8034 set_fill_set(&orig_set, set);
8035
Michal Vasko004d3152020-06-11 19:59:22 +02008036 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008037 LY_CHECK_GOTO(rc, cleanup);
8038
8039 /* ('+' / '-' MultiplicativeExpr)* */
8040 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008041 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008042
Michal Vasko004d3152020-06-11 19:59:22 +02008043 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008044 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008045 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008046 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008047
8048 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008049 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008050 LY_CHECK_GOTO(rc, cleanup);
8051 continue;
8052 }
8053
8054 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008055 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008056 LY_CHECK_GOTO(rc, cleanup);
8057
8058 /* eval */
8059 if (options & LYXP_SCNODE_ALL) {
8060 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008061 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008062 set_scnode_clear_ctx(set);
8063 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008064 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008065 LY_CHECK_GOTO(rc, cleanup);
8066 }
8067 }
8068
8069cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008070 lyxp_set_free_content(&orig_set);
8071 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008072 return rc;
8073}
8074
8075/**
8076 * @brief Evaluate RelationalExpr. Logs directly on error.
8077 *
Michal Vaskod3678892020-05-21 10:06:58 +02008078 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008079 * | RelationalExpr '<' AdditiveExpr
8080 * | RelationalExpr '>' AdditiveExpr
8081 * | RelationalExpr '<=' AdditiveExpr
8082 * | RelationalExpr '>=' AdditiveExpr
8083 *
8084 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008085 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008086 * @param[in] repeat How many times this expression is repeated.
8087 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8088 * @param[in] options XPath options.
8089 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8090 */
8091static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02008092eval_relational_expr(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 +02008093{
8094 LY_ERR rc;
8095 uint16_t this_op;
8096 struct lyxp_set orig_set, set2;
8097 uint16_t i;
8098
8099 assert(repeat);
8100
8101 set_init(&orig_set, set);
8102 set_init(&set2, set);
8103
8104 set_fill_set(&orig_set, set);
8105
Michal Vasko004d3152020-06-11 19:59:22 +02008106 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008107 LY_CHECK_GOTO(rc, cleanup);
8108
8109 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8110 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008111 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008112
Michal Vasko004d3152020-06-11 19:59:22 +02008113 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008114 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008115 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008116 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008117
8118 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008119 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008120 LY_CHECK_GOTO(rc, cleanup);
8121 continue;
8122 }
8123
8124 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008125 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008126 LY_CHECK_GOTO(rc, cleanup);
8127
8128 /* eval */
8129 if (options & LYXP_SCNODE_ALL) {
8130 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008131 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008132 set_scnode_clear_ctx(set);
8133 } else {
8134 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8135 LY_CHECK_GOTO(rc, cleanup);
8136 }
8137 }
8138
8139cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008140 lyxp_set_free_content(&orig_set);
8141 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008142 return rc;
8143}
8144
8145/**
8146 * @brief Evaluate EqualityExpr. Logs directly on error.
8147 *
Michal Vaskod3678892020-05-21 10:06:58 +02008148 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008149 * | EqualityExpr '!=' RelationalExpr
8150 *
8151 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008152 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008153 * @param[in] repeat How many times this expression is repeated.
8154 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8155 * @param[in] options XPath options.
8156 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8157 */
8158static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02008159eval_equality_expr(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 +02008160{
8161 LY_ERR rc;
8162 uint16_t this_op;
8163 struct lyxp_set orig_set, set2;
8164 uint16_t i;
8165
8166 assert(repeat);
8167
8168 set_init(&orig_set, set);
8169 set_init(&set2, set);
8170
8171 set_fill_set(&orig_set, set);
8172
Michal Vasko004d3152020-06-11 19:59:22 +02008173 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008174 LY_CHECK_GOTO(rc, cleanup);
8175
8176 /* ('=' / '!=' RelationalExpr)* */
8177 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008178 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008179
Michal Vasko004d3152020-06-11 19:59:22 +02008180 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008181 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008182 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008183 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008184
8185 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008186 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008187 LY_CHECK_GOTO(rc, cleanup);
8188 continue;
8189 }
8190
8191 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008192 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008193 LY_CHECK_GOTO(rc, cleanup);
8194
8195 /* eval */
8196 if (options & LYXP_SCNODE_ALL) {
8197 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008198 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8199 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008200 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008201 set_scnode_clear_ctx(set);
8202 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008203 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8204 LY_CHECK_GOTO(rc, cleanup);
8205 }
8206 }
8207
8208cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008209 lyxp_set_free_content(&orig_set);
8210 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008211 return rc;
8212}
8213
8214/**
8215 * @brief Evaluate AndExpr. Logs directly on error.
8216 *
Michal Vaskod3678892020-05-21 10:06:58 +02008217 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008218 *
8219 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008220 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008221 * @param[in] repeat How many times this expression is repeated.
8222 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8223 * @param[in] options XPath options.
8224 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8225 */
8226static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02008227eval_and_expr(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 +02008228{
8229 LY_ERR rc;
8230 struct lyxp_set orig_set, set2;
8231 uint16_t i;
8232
8233 assert(repeat);
8234
8235 set_init(&orig_set, set);
8236 set_init(&set2, set);
8237
8238 set_fill_set(&orig_set, set);
8239
Michal Vasko004d3152020-06-11 19:59:22 +02008240 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008241 LY_CHECK_GOTO(rc, cleanup);
8242
8243 /* cast to boolean, we know that will be the final result */
8244 if (set && (options & LYXP_SCNODE_ALL)) {
8245 set_scnode_clear_ctx(set);
8246 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008247 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008248 }
8249
8250 /* ('and' EqualityExpr)* */
8251 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008252 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8253 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008254 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008255 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008256
8257 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008258 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8259 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008260 LY_CHECK_GOTO(rc, cleanup);
8261 continue;
8262 }
8263
8264 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008265 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008266 LY_CHECK_GOTO(rc, cleanup);
8267
8268 /* eval - just get boolean value actually */
8269 if (set->type == LYXP_SET_SCNODE_SET) {
8270 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008271 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008272 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008273 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008274 set_fill_set(set, &set2);
8275 }
8276 }
8277
8278cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008279 lyxp_set_free_content(&orig_set);
8280 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008281 return rc;
8282}
8283
8284/**
8285 * @brief Evaluate OrExpr. Logs directly on error.
8286 *
Michal Vaskod3678892020-05-21 10:06:58 +02008287 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008288 *
8289 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008290 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008291 * @param[in] repeat How many times this expression is repeated.
8292 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8293 * @param[in] options XPath options.
8294 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8295 */
8296static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02008297eval_or_expr(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 +02008298{
8299 LY_ERR rc;
8300 struct lyxp_set orig_set, set2;
8301 uint16_t i;
8302
8303 assert(repeat);
8304
8305 set_init(&orig_set, set);
8306 set_init(&set2, set);
8307
8308 set_fill_set(&orig_set, set);
8309
Michal Vasko004d3152020-06-11 19:59:22 +02008310 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008311 LY_CHECK_GOTO(rc, cleanup);
8312
8313 /* cast to boolean, we know that will be the final result */
8314 if (set && (options & LYXP_SCNODE_ALL)) {
8315 set_scnode_clear_ctx(set);
8316 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008317 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008318 }
8319
8320 /* ('or' AndExpr)* */
8321 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008322 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8323 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008324 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008325 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008326
8327 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008328 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8329 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008330 LY_CHECK_GOTO(rc, cleanup);
8331 continue;
8332 }
8333
8334 set_fill_set(&set2, &orig_set);
8335 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8336 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008337 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008338 LY_CHECK_GOTO(rc, cleanup);
8339
8340 /* eval - just get boolean value actually */
8341 if (set->type == LYXP_SET_SCNODE_SET) {
8342 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008343 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008344 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008345 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008346 set_fill_set(set, &set2);
8347 }
8348 }
8349
8350cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008351 lyxp_set_free_content(&orig_set);
8352 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008353 return rc;
8354}
8355
8356/**
Michal Vasko004d3152020-06-11 19:59:22 +02008357 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008358 *
8359 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008360 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008361 * @param[in] etype Expression type to evaluate.
8362 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8363 * @param[in] options XPath options.
8364 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8365 */
8366static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02008367eval_expr_select(struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008368{
8369 uint16_t i, count;
8370 enum lyxp_expr_type next_etype;
8371 LY_ERR rc;
8372
8373 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008374 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008375 next_etype = LYXP_EXPR_NONE;
8376 } else {
8377 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008378 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008379
8380 /* select one-priority lower because etype expression called us */
8381 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008382 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008383 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008384 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008385 } else {
8386 next_etype = LYXP_EXPR_NONE;
8387 }
8388 }
8389
8390 /* decide what expression are we parsing based on the repeat */
8391 switch (next_etype) {
8392 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008393 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008394 break;
8395 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008396 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008397 break;
8398 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008399 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008400 break;
8401 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008402 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008403 break;
8404 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008405 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008406 break;
8407 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008408 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008409 break;
8410 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008411 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008412 break;
8413 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008414 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008415 break;
8416 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008417 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008418 break;
8419 default:
8420 LOGINT_RET(set->ctx);
8421 }
8422
8423 return rc;
8424}
8425
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008426/**
8427 * @brief Get root type.
8428 *
8429 * @param[in] ctx_node Context node.
8430 * @param[in] ctx_scnode Schema context node.
8431 * @param[in] options XPath options.
8432 * @return Root type.
8433 */
8434static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008435lyxp_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 +01008436{
Michal Vasko6b26e742020-07-17 15:02:10 +02008437 const struct lysc_node *op;
8438
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008439 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008440 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008441 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008442
8443 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008444 /* general root that can access everything */
8445 return LYXP_NODE_ROOT;
8446 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8447 /* root context node can access only config data (because we said so, it is unspecified) */
8448 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008449 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008450 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008451 }
8452
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008453 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008454 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008455 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008456
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008457 if (op || !(options & LYXP_SCHEMA)) {
8458 /* general root that can access everything */
8459 return LYXP_NODE_ROOT;
8460 } else if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008461 /* root context node can access only config data (because we said so, it is unspecified) */
8462 return LYXP_NODE_ROOT_CONFIG;
8463 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008464 return LYXP_NODE_ROOT;
8465}
8466
Michal Vasko03ff5a72019-09-11 13:49:33 +02008467LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008468lyxp_eval(struct lyxp_expr *exp, const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data,
8469 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 +02008470{
Michal Vasko004d3152020-06-11 19:59:22 +02008471 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008472 LY_ERR rc;
8473
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008474 LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL);
8475 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8476 LOGARG(NULL, "Current module must be set if schema format is used.");
8477 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008478 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008479
8480 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008481 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008482 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008483 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8484 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8485
8486 set->ctx = (struct ly_ctx *)(cur_mod ? cur_mod->ctx : (ctx_node ? LYD_CTX(ctx_node) : (tree ? LYD_CTX(tree) : NULL)));
8487 set->cur_node = ctx_node;
8488 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008489 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8490 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008491 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008492 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008493 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008494 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008495
8496 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008497 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008498 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008499 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008500 }
8501
Michal Vasko03ff5a72019-09-11 13:49:33 +02008502 return rc;
8503}
8504
8505#if 0
8506
8507/* full xml printing of set elements, not used currently */
8508
8509void
8510lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8511{
8512 uint32_t i;
8513 char *str_num;
8514 struct lyout out;
8515
8516 memset(&out, 0, sizeof out);
8517
8518 out.type = LYOUT_STREAM;
8519 out.method.f = f;
8520
8521 switch (set->type) {
8522 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008523 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008524 break;
8525 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008526 ly_print_(&out, "Boolean XPath set:\n");
8527 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008528 break;
8529 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008530 ly_print_(&out, "String XPath set:\n");
8531 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008532 break;
8533 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008534 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008535
8536 if (isnan(set->value.num)) {
8537 str_num = strdup("NaN");
8538 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8539 str_num = strdup("0");
8540 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8541 str_num = strdup("Infinity");
8542 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8543 str_num = strdup("-Infinity");
8544 } else if ((long long)set->value.num == set->value.num) {
8545 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8546 str_num = NULL;
8547 }
8548 } else {
8549 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8550 str_num = NULL;
8551 }
8552 }
8553 if (!str_num) {
8554 LOGMEM;
8555 return;
8556 }
Michal Vasko5233e962020-08-14 14:26:20 +02008557 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008558 free(str_num);
8559 break;
8560 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008561 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008562
8563 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008564 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008565 switch (set->node_type[i]) {
8566 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008567 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008568 break;
8569 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008570 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008571 break;
8572 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008573 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008574 break;
8575 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008576 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008577 break;
8578 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008579 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008580 break;
8581 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008582 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008583 break;
8584 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008585 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008586 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008587 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008588 break;
8589 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008590 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 +02008591 break;
8592 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008593 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 +02008594 break;
8595 }
8596 }
8597 break;
8598 }
8599}
8600
8601#endif
8602
8603LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008604lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008605{
8606 long double num;
8607 char *str;
8608 LY_ERR rc;
8609
8610 if (!set || (set->type == target)) {
8611 return LY_SUCCESS;
8612 }
8613
8614 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008615 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008616
8617 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008618 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008619 return LY_EINVAL;
8620 }
8621
8622 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008623 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008624 switch (set->type) {
8625 case LYXP_SET_NUMBER:
8626 if (isnan(set->val.num)) {
8627 set->val.str = strdup("NaN");
8628 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8629 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8630 set->val.str = strdup("0");
8631 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8632 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8633 set->val.str = strdup("Infinity");
8634 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8635 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8636 set->val.str = strdup("-Infinity");
8637 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8638 } else if ((long long)set->val.num == set->val.num) {
8639 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8640 LOGMEM_RET(set->ctx);
8641 }
8642 set->val.str = str;
8643 } else {
8644 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8645 LOGMEM_RET(set->ctx);
8646 }
8647 set->val.str = str;
8648 }
8649 break;
8650 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008651 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008652 set->val.str = strdup("true");
8653 } else {
8654 set->val.str = strdup("false");
8655 }
8656 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8657 break;
8658 case LYXP_SET_NODE_SET:
8659 assert(set->used);
8660
8661 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008662 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008663
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008664 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008665 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008666 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008667 set->val.str = str;
8668 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008669 default:
8670 LOGINT_RET(set->ctx);
8671 }
8672 set->type = LYXP_SET_STRING;
8673 }
8674
8675 /* to NUMBER */
8676 if (target == LYXP_SET_NUMBER) {
8677 switch (set->type) {
8678 case LYXP_SET_STRING:
8679 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008680 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008681 set->val.num = num;
8682 break;
8683 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008684 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008685 set->val.num = 1;
8686 } else {
8687 set->val.num = 0;
8688 }
8689 break;
8690 default:
8691 LOGINT_RET(set->ctx);
8692 }
8693 set->type = LYXP_SET_NUMBER;
8694 }
8695
8696 /* to BOOLEAN */
8697 if (target == LYXP_SET_BOOLEAN) {
8698 switch (set->type) {
8699 case LYXP_SET_NUMBER:
8700 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008701 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008702 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008703 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008704 }
8705 break;
8706 case LYXP_SET_STRING:
8707 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008708 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008709 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008710 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008711 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008712 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008713 }
8714 break;
8715 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008716 if (set->used) {
8717 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008718 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008719 } else {
8720 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008721 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008722 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008723 break;
8724 default:
8725 LOGINT_RET(set->ctx);
8726 }
8727 set->type = LYXP_SET_BOOLEAN;
8728 }
8729
Michal Vasko03ff5a72019-09-11 13:49:33 +02008730 return LY_SUCCESS;
8731}
8732
8733LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008734lyxp_atomize(struct lyxp_expr *exp, const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data,
8735 const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008736{
Michal Vasko004d3152020-06-11 19:59:22 +02008737 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008738
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008739 LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL);
8740 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8741 LOGARG(NULL, "Current module must be set if schema format is used.");
8742 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008743 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008744
8745 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008746 memset(set, 0, sizeof *set);
8747 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008748 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8749 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode ? LYXP_NODE_ELEM : set->root_type, NULL));
Michal Vasko5c4e5892019-11-14 12:31:38 +01008750 set->val.scnodes[0].in_ctx = -2;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008751
8752 set->ctx = cur_mod ? cur_mod->ctx : (ctx_scnode ? ctx_scnode->module->ctx : NULL);
8753 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008754 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008755 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8756 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008757 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008758 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008759 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008760
8761 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008762 return eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008763}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008764
8765API const char *
8766lyxp_get_expr(const struct lyxp_expr *path)
8767{
8768 if (!path) {
8769 return NULL;
8770 }
8771
8772 return path->expr;
8773}