blob: 8f96c45b6b4b14d39886dfb65766ffc34631e952 [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
137 if (!exp || (ly_log_level < LY_LLDBG)) {
138 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 Vasko03ff5a72019-09-11 13:49:33 +0200144 &exp->expr[exp->tok_pos[i]]);
145 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
171 if (ly_log_level < LY_LLDBG) {
172 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:
192 if ((item->node->schema->nodetype == LYS_LIST)
193 && (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) {
194 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Michal Vaskob7be7a82020-08-20 09:09:04 +0200195 item->node->schema->name, LYD_CANON_VALUE(lyd_node_children(item->node, 0)));
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 Vaskob7be7a82020-08-20 09:09:04 +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,
206 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
207 } 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,
213 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 Krejci1deb5be2020-08-26 16:43:36 +0200321cast_string_recursive(const struct lyd_node *node, uint8_t 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
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200354 for (child = lyd_node_children(node, 0); 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 Krejci1deb5be2020-08-26 16:43:36 +0200467cast_string_elem(struct lyd_node *node, uint8_t 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 *
546 * @param[in] val1_p First value.
547 * @param[in] val2_p Second value.
548 * @param[in] mod Whether hash table is being modified.
549 * @param[in] cb_data Callback data.
550 * @return 0 if not equal, non-zero if equal.
551 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200552static uint8_t
553set_values_equal_cb(void *val1_p, void *val2_p, uint8_t UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200554{
555 struct lyxp_set_hash_node *val1, *val2;
556
557 val1 = (struct lyxp_set_hash_node *)val1_p;
558 val2 = (struct lyxp_set_hash_node *)val2_p;
559
560 if ((val1->node == val2->node) && (val1->type == val2->type)) {
561 return 1;
562 }
563
564 return 0;
565}
566
567/**
568 * @brief Insert node and its hash into set.
569 *
570 * @param[in] set et to insert to.
571 * @param[in] node Node with hash.
572 * @param[in] type Node type.
573 */
574static void
575set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
576{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200577 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200578 uint32_t i, hash;
579 struct lyxp_set_hash_node hnode;
580
581 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
582 /* create hash table and add all the nodes */
583 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
584 for (i = 0; i < set->used; ++i) {
585 hnode.node = set->val.nodes[i].node;
586 hnode.type = set->val.nodes[i].type;
587
588 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
589 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
590 hash = dict_hash_multi(hash, NULL, 0);
591
592 r = lyht_insert(set->ht, &hnode, hash, NULL);
593 assert(!r);
594 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200595
Michal Vasko9d6befd2019-12-11 14:56:56 +0100596 if (hnode.node == node) {
597 /* it was just added, do not add it twice */
598 node = NULL;
599 }
600 }
601 }
602
603 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200604 /* add the new node into hash table */
605 hnode.node = node;
606 hnode.type = type;
607
608 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
609 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
610 hash = dict_hash_multi(hash, NULL, 0);
611
612 r = lyht_insert(set->ht, &hnode, hash, NULL);
613 assert(!r);
614 (void)r;
615 }
616}
617
618/**
619 * @brief Remove node and its hash from set.
620 *
621 * @param[in] set Set to remove from.
622 * @param[in] node Node to remove.
623 * @param[in] type Node type.
624 */
625static void
626set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
627{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200628 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200629 struct lyxp_set_hash_node hnode;
630 uint32_t hash;
631
632 if (set->ht) {
633 hnode.node = node;
634 hnode.type = type;
635
636 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
637 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
638 hash = dict_hash_multi(hash, NULL, 0);
639
640 r = lyht_remove(set->ht, &hnode, hash);
641 assert(!r);
642 (void)r;
643
644 if (!set->ht->used) {
645 lyht_free(set->ht);
646 set->ht = NULL;
647 }
648 }
649}
650
651/**
652 * @brief Check whether node is in set based on its hash.
653 *
654 * @param[in] set Set to search in.
655 * @param[in] node Node to search for.
656 * @param[in] type Node type.
657 * @param[in] skip_idx Index in @p set to skip.
658 * @return LY_ERR
659 */
660static LY_ERR
661set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
662{
663 struct lyxp_set_hash_node hnode, *match_p;
664 uint32_t hash;
665
666 hnode.node = node;
667 hnode.type = type;
668
669 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
670 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
671 hash = dict_hash_multi(hash, NULL, 0);
672
673 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
674 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
675 /* we found it on the index that should be skipped, find another */
676 hnode = *match_p;
677 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
678 /* none other found */
679 return LY_SUCCESS;
680 }
681 }
682
683 return LY_EEXIST;
684 }
685
686 /* not found */
687 return LY_SUCCESS;
688}
689
Michal Vaskod3678892020-05-21 10:06:58 +0200690void
691lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200692{
693 if (!set) {
694 return;
695 }
696
697 if (set->type == LYXP_SET_NODE_SET) {
698 free(set->val.nodes);
699 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200700 } else if (set->type == LYXP_SET_SCNODE_SET) {
701 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200702 lyht_free(set->ht);
703 } else {
704 if (set->type == LYXP_SET_STRING) {
705 free(set->val.str);
706 }
707 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200708 }
Michal Vaskod3678892020-05-21 10:06:58 +0200709
710 set->val.nodes = NULL;
711 set->used = 0;
712 set->size = 0;
713 set->ht = NULL;
714 set->ctx_pos = 0;
715 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200716}
717
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100718/**
719 * @brief Free dynamically-allocated set.
720 *
721 * @param[in] set Set to free.
722 */
723static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200724lyxp_set_free(struct lyxp_set *set)
725{
726 if (!set) {
727 return;
728 }
729
Michal Vaskod3678892020-05-21 10:06:58 +0200730 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200731 free(set);
732}
733
734/**
735 * @brief Initialize set context.
736 *
737 * @param[in] new Set to initialize.
738 * @param[in] set Arbitrary initialized set.
739 */
740static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200741set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200742{
743 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200744 if (set) {
745 new->ctx = set->ctx;
746 new->ctx_node = set->ctx_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100747 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200748 new->context_op = set->context_op;
Michal Vasko02a77382019-09-12 11:47:35 +0200749 new->local_mod = set->local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +0100750 new->tree = set->tree;
Michal Vasko02a77382019-09-12 11:47:35 +0200751 new->format = set->format;
752 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200753}
754
755/**
756 * @brief Create a deep copy of a set.
757 *
758 * @param[in] set Set to copy.
759 * @return Copy of @p set.
760 */
761static struct lyxp_set *
762set_copy(struct lyxp_set *set)
763{
764 struct lyxp_set *ret;
765 uint16_t i;
766
767 if (!set) {
768 return NULL;
769 }
770
771 ret = malloc(sizeof *ret);
772 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
773 set_init(ret, set);
774
775 if (set->type == LYXP_SET_SCNODE_SET) {
776 ret->type = set->type;
777
778 for (i = 0; i < set->used; ++i) {
Michal Vaskoba716542019-12-16 10:01:58 +0100779 if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200780 uint32_t idx;
781 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
782 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100783 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200784 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200785 lyxp_set_free(ret);
786 return NULL;
787 }
Michal Vaskoba716542019-12-16 10:01:58 +0100788 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200789 }
790 }
791 } else if (set->type == LYXP_SET_NODE_SET) {
792 ret->type = set->type;
793 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
794 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
795 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
796
797 ret->used = ret->size = set->used;
798 ret->ctx_pos = set->ctx_pos;
799 ret->ctx_size = set->ctx_size;
800 ret->ht = lyht_dup(set->ht);
801 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200802 memcpy(ret, set, sizeof *ret);
803 if (set->type == LYXP_SET_STRING) {
804 ret->val.str = strdup(set->val.str);
805 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
806 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200807 }
808
809 return ret;
810}
811
812/**
813 * @brief Fill XPath set with a string. Any current data are disposed of.
814 *
815 * @param[in] set Set to fill.
816 * @param[in] string String to fill into \p set.
817 * @param[in] str_len Length of \p string. 0 is a valid value!
818 */
819static void
820set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
821{
Michal Vaskod3678892020-05-21 10:06:58 +0200822 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200823
824 set->type = LYXP_SET_STRING;
825 if ((str_len == 0) && (string[0] != '\0')) {
826 string = "";
827 }
828 set->val.str = strndup(string, str_len);
829}
830
831/**
832 * @brief Fill XPath set with a number. Any current data are disposed of.
833 *
834 * @param[in] set Set to fill.
835 * @param[in] number Number to fill into \p set.
836 */
837static void
838set_fill_number(struct lyxp_set *set, long double number)
839{
Michal Vaskod3678892020-05-21 10:06:58 +0200840 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200841
842 set->type = LYXP_SET_NUMBER;
843 set->val.num = number;
844}
845
846/**
847 * @brief Fill XPath set with a boolean. Any current data are disposed of.
848 *
849 * @param[in] set Set to fill.
850 * @param[in] boolean Boolean to fill into \p set.
851 */
852static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200853set_fill_boolean(struct lyxp_set *set, uint8_t boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200854{
Michal Vaskod3678892020-05-21 10:06:58 +0200855 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200856
857 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200858 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200859}
860
861/**
862 * @brief Fill XPath set with the value from another set (deep assign).
863 * Any current data are disposed of.
864 *
865 * @param[in] trg Set to fill.
866 * @param[in] src Source set to copy into \p trg.
867 */
868static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200869set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200870{
871 if (!trg || !src) {
872 return;
873 }
874
875 if (trg->type == LYXP_SET_NODE_SET) {
876 free(trg->val.nodes);
877 } else if (trg->type == LYXP_SET_STRING) {
878 free(trg->val.str);
879 }
880 set_init(trg, src);
881
882 if (src->type == LYXP_SET_SCNODE_SET) {
883 trg->type = LYXP_SET_SCNODE_SET;
884 trg->used = src->used;
885 trg->size = src->used;
886
887 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
888 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
889 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
890 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200891 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200892 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200893 set_fill_number(trg, src->val.num);
894 } else if (src->type == LYXP_SET_STRING) {
895 set_fill_string(trg, src->val.str, strlen(src->val.str));
896 } else {
897 if (trg->type == LYXP_SET_NODE_SET) {
898 free(trg->val.nodes);
899 } else if (trg->type == LYXP_SET_STRING) {
900 free(trg->val.str);
901 }
902
Michal Vaskod3678892020-05-21 10:06:58 +0200903 assert(src->type == LYXP_SET_NODE_SET);
904
905 trg->type = LYXP_SET_NODE_SET;
906 trg->used = src->used;
907 trg->size = src->used;
908 trg->ctx_pos = src->ctx_pos;
909 trg->ctx_size = src->ctx_size;
910
911 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
912 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
913 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
914 if (src->ht) {
915 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200916 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200917 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200918 }
919 }
920}
921
922/**
923 * @brief Clear context of all schema nodes.
924 *
925 * @param[in] set Set to clear.
926 */
927static void
928set_scnode_clear_ctx(struct lyxp_set *set)
929{
930 uint32_t i;
931
932 for (i = 0; i < set->used; ++i) {
933 if (set->val.scnodes[i].in_ctx == 1) {
934 set->val.scnodes[i].in_ctx = 0;
Michal Vasko5c4e5892019-11-14 12:31:38 +0100935 } else if (set->val.scnodes[i].in_ctx == -2) {
936 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200937 }
938 }
939}
940
941/**
942 * @brief Remove a node from a set. Removing last node changes
943 * set into LYXP_SET_EMPTY. Context position aware.
944 *
945 * @param[in] set Set to use.
946 * @param[in] idx Index from @p set of the node to be removed.
947 */
948static void
949set_remove_node(struct lyxp_set *set, uint32_t idx)
950{
951 assert(set && (set->type == LYXP_SET_NODE_SET));
952 assert(idx < set->used);
953
954 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
955
956 --set->used;
957 if (set->used) {
958 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
959 (set->used - idx) * sizeof *set->val.nodes);
960 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200961 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200962 }
963}
964
965/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100966 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200967 *
968 * @param[in] set Set to use.
969 * @param[in] idx Index from @p set of the node to be removed.
970 */
971static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100972set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200973{
974 assert(set && (set->type == LYXP_SET_NODE_SET));
975 assert(idx < set->used);
976
Michal Vasko2caefc12019-11-14 16:07:56 +0100977 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
978 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
979 }
980 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +0200981}
982
983/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100984 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +0200985 * set into LYXP_SET_EMPTY. Context position aware.
986 *
987 * @param[in] set Set to consolidate.
988 */
989static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100990set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +0200991{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +0200992 uint16_t i, orig_used, end = 0;
Michal Vasko57eab132019-09-24 11:46:26 +0200993 int32_t start;
994
Michal Vaskod3678892020-05-21 10:06:58 +0200995 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +0200996
997 orig_used = set->used;
998 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +0200999 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001000 start = -1;
1001 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001002 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001003 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001004 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001005 end = i;
1006 ++i;
1007 break;
1008 }
1009
1010 ++i;
1011 if (i == orig_used) {
1012 end = i;
1013 }
1014 } while (i < orig_used);
1015
1016 if (start > -1) {
1017 /* move the whole chunk of valid nodes together */
1018 if (set->used != (unsigned)start) {
1019 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1020 }
1021 set->used += end - start;
1022 }
1023 }
Michal Vasko57eab132019-09-24 11:46:26 +02001024}
1025
1026/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001027 * @brief Check for duplicates in a node set.
1028 *
1029 * @param[in] set Set to check.
1030 * @param[in] node Node to look for in @p set.
1031 * @param[in] node_type Type of @p node.
1032 * @param[in] skip_idx Index from @p set to skip.
1033 * @return LY_ERR
1034 */
1035static LY_ERR
1036set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1037{
1038 uint32_t i;
1039
Michal Vasko2caefc12019-11-14 16:07:56 +01001040 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001041 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1042 }
1043
1044 for (i = 0; i < set->used; ++i) {
1045 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1046 continue;
1047 }
1048
1049 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1050 return LY_EEXIST;
1051 }
1052 }
1053
1054 return LY_SUCCESS;
1055}
1056
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001057uint8_t
1058lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1059 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001060{
1061 uint32_t i;
1062
1063 for (i = 0; i < set->used; ++i) {
1064 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1065 continue;
1066 }
1067
1068 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001069 if (index_p) {
1070 *index_p = i;
1071 }
1072 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001073 }
1074 }
1075
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001076 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001077}
1078
Michal Vaskoecd62de2019-11-13 12:35:11 +01001079void
1080lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001081{
1082 uint32_t orig_used, i, j;
1083
Michal Vaskod3678892020-05-21 10:06:58 +02001084 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001085
Michal Vaskod3678892020-05-21 10:06:58 +02001086 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001087 return;
1088 }
1089
Michal Vaskod3678892020-05-21 10:06:58 +02001090 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001091 memcpy(set1, set2, sizeof *set1);
1092 return;
1093 }
1094
1095 if (set1->used + set2->used > set1->size) {
1096 set1->size = set1->used + set2->used;
1097 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1098 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1099 }
1100
1101 orig_used = set1->used;
1102
1103 for (i = 0; i < set2->used; ++i) {
1104 for (j = 0; j < orig_used; ++j) {
1105 /* detect duplicities */
1106 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1107 break;
1108 }
1109 }
1110
1111 if (j == orig_used) {
1112 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1113 ++set1->used;
1114 }
1115 }
1116
Michal Vaskod3678892020-05-21 10:06:58 +02001117 lyxp_set_free_content(set2);
1118 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001119}
1120
1121/**
1122 * @brief Insert a node into a set. Context position aware.
1123 *
1124 * @param[in] set Set to use.
1125 * @param[in] node Node to insert to @p set.
1126 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1127 * @param[in] node_type Node type of @p node.
1128 * @param[in] idx Index in @p set to insert into.
1129 */
1130static void
1131set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1132{
Michal Vaskod3678892020-05-21 10:06:58 +02001133 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001134
Michal Vaskod3678892020-05-21 10:06:58 +02001135 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001136 /* first item */
1137 if (idx) {
1138 /* no real harm done, but it is a bug */
1139 LOGINT(set->ctx);
1140 idx = 0;
1141 }
1142 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1143 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1144 set->type = LYXP_SET_NODE_SET;
1145 set->used = 0;
1146 set->size = LYXP_SET_SIZE_START;
1147 set->ctx_pos = 1;
1148 set->ctx_size = 1;
1149 set->ht = NULL;
1150 } else {
1151 /* not an empty set */
1152 if (set->used == set->size) {
1153
1154 /* set is full */
1155 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1156 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1157 set->size += LYXP_SET_SIZE_STEP;
1158 }
1159
1160 if (idx > set->used) {
1161 LOGINT(set->ctx);
1162 idx = set->used;
1163 }
1164
1165 /* make space for the new node */
1166 if (idx < set->used) {
1167 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1168 }
1169 }
1170
1171 /* finally assign the value */
1172 set->val.nodes[idx].node = (struct lyd_node *)node;
1173 set->val.nodes[idx].type = node_type;
1174 set->val.nodes[idx].pos = pos;
1175 ++set->used;
1176
Michal Vasko2caefc12019-11-14 16:07:56 +01001177 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1178 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1179 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001180}
1181
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001182LY_ERR
1183lyxp_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 +02001184{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001185 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001186
1187 assert(set->type == LYXP_SET_SCNODE_SET);
1188
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001189 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
1190 set->val.scnodes[index].in_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001191 } else {
1192 if (set->used == set->size) {
1193 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 +02001194 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001195 set->size += LYXP_SET_SIZE_STEP;
1196 }
1197
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001198 index = set->used;
1199 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1200 set->val.scnodes[index].type = node_type;
1201 set->val.scnodes[index].in_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001202 ++set->used;
1203 }
1204
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001205 if (index_p) {
1206 *index_p = index;
1207 }
1208
1209 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001210}
1211
1212/**
1213 * @brief Replace a node in a set with another. Context position aware.
1214 *
1215 * @param[in] set Set to use.
1216 * @param[in] node Node to insert to @p set.
1217 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1218 * @param[in] node_type Node type of @p node.
1219 * @param[in] idx Index in @p set of the node to replace.
1220 */
1221static void
1222set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1223{
1224 assert(set && (idx < set->used));
1225
Michal Vasko2caefc12019-11-14 16:07:56 +01001226 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1227 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1228 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001229 set->val.nodes[idx].node = (struct lyd_node *)node;
1230 set->val.nodes[idx].type = node_type;
1231 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001232 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1233 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1234 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001235}
1236
1237/**
1238 * @brief Set all nodes with ctx 1 to a new unique context value.
1239 *
1240 * @param[in] set Set to modify.
1241 * @return New context value.
1242 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001243static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001244set_scnode_new_in_ctx(struct lyxp_set *set)
1245{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001246 uint32_t i;
1247 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001248
1249 assert(set->type == LYXP_SET_SCNODE_SET);
1250
1251 ret_ctx = 3;
1252retry:
1253 for (i = 0; i < set->used; ++i) {
1254 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1255 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1256 goto retry;
1257 }
1258 }
1259 for (i = 0; i < set->used; ++i) {
1260 if (set->val.scnodes[i].in_ctx == 1) {
1261 set->val.scnodes[i].in_ctx = ret_ctx;
1262 }
1263 }
1264
1265 return ret_ctx;
1266}
1267
1268/**
1269 * @brief Get unique @p node position in the data.
1270 *
1271 * @param[in] node Node to find.
1272 * @param[in] node_type Node type of @p node.
1273 * @param[in] root Root node.
1274 * @param[in] root_type Type of the XPath @p root node.
1275 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1276 * be used to increase efficiency and start the DFS from this node.
1277 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1278 * @return Node position.
1279 */
1280static uint32_t
1281get_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 +02001282 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001283{
Michal Vasko56daf732020-08-10 10:57:18 +02001284 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001285 uint32_t pos = 1;
1286
1287 assert(prev && prev_pos && !root->prev->next);
1288
1289 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1290 return 0;
1291 }
1292
1293 if (*prev) {
1294 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001295 pos = *prev_pos;
Radek Krejci1e008d22020-08-17 11:37:37 +02001296 for (top_sibling = *prev; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001297 goto dfs_search;
1298 }
1299
1300 for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) {
Michal Vasko56daf732020-08-10 10:57:18 +02001301 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001302dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001303 if (*prev && !elem) {
1304 /* resume previous DFS */
1305 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1306 LYD_TREE_DFS_continue = 0;
1307 }
1308
Michal Vasko03ff5a72019-09-11 13:49:33 +02001309 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001310 /* skip */
1311 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001312 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001313 if (elem == node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001314 break;
1315 }
Michal Vasko56daf732020-08-10 10:57:18 +02001316 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001317 }
Michal Vasko56daf732020-08-10 10:57:18 +02001318
1319 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001320 }
1321
1322 /* node found */
1323 if (elem) {
1324 break;
1325 }
1326 }
1327
1328 if (!elem) {
1329 if (!(*prev)) {
1330 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001331 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001332 return 0;
1333 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001334 /* start the search again from the beginning */
1335 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001336
Michal Vasko56daf732020-08-10 10:57:18 +02001337 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001338 pos = 1;
1339 goto dfs_search;
1340 }
1341 }
1342
1343 /* remember the last found node for next time */
1344 *prev = node;
1345 *prev_pos = pos;
1346
1347 return pos;
1348}
1349
1350/**
1351 * @brief Assign (fill) missing node positions.
1352 *
1353 * @param[in] set Set to fill positions in.
1354 * @param[in] root Context root node.
1355 * @param[in] root_type Context root type.
1356 * @return LY_ERR
1357 */
1358static LY_ERR
1359set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1360{
1361 const struct lyd_node *prev = NULL, *tmp_node;
1362 uint32_t i, tmp_pos = 0;
1363
1364 for (i = 0; i < set->used; ++i) {
1365 if (!set->val.nodes[i].pos) {
1366 tmp_node = NULL;
1367 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001368 case LYXP_NODE_META:
1369 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001370 if (!tmp_node) {
1371 LOGINT_RET(root->schema->module->ctx);
1372 }
Radek Krejci0f969882020-08-21 16:56:47 +02001373 /* fallthrough */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001374 case LYXP_NODE_ELEM:
1375 case LYXP_NODE_TEXT:
1376 if (!tmp_node) {
1377 tmp_node = set->val.nodes[i].node;
1378 }
1379 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1380 break;
1381 default:
1382 /* all roots have position 0 */
1383 break;
1384 }
1385 }
1386 }
1387
1388 return LY_SUCCESS;
1389}
1390
1391/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001392 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001393 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001394 * @param[in] meta Metadata to use.
1395 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001396 */
1397static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001398get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001399{
1400 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001401 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001402
Michal Vasko9f96a052020-03-10 09:41:45 +01001403 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001404 ++pos;
1405 }
1406
Michal Vasko9f96a052020-03-10 09:41:45 +01001407 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001408 return pos;
1409}
1410
1411/**
1412 * @brief Compare 2 nodes in respect to XPath document order.
1413 *
1414 * @param[in] item1 1st node.
1415 * @param[in] item2 2nd node.
1416 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1417 */
1418static int
1419set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1420{
Michal Vasko9f96a052020-03-10 09:41:45 +01001421 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001422
1423 if (item1->pos < item2->pos) {
1424 return -1;
1425 }
1426
1427 if (item1->pos > item2->pos) {
1428 return 1;
1429 }
1430
1431 /* node positions are equal, the fun case */
1432
1433 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1434 /* special case since text nodes are actually saved as their parents */
1435 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1436 if (item1->type == LYXP_NODE_ELEM) {
1437 assert(item2->type == LYXP_NODE_TEXT);
1438 return -1;
1439 } else {
1440 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1441 return 1;
1442 }
1443 }
1444
Michal Vasko9f96a052020-03-10 09:41:45 +01001445 /* we need meta positions now */
1446 if (item1->type == LYXP_NODE_META) {
1447 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001448 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001449 if (item2->type == LYXP_NODE_META) {
1450 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001451 }
1452
Michal Vasko9f96a052020-03-10 09:41:45 +01001453 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001454 /* check for duplicates */
1455 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001456 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001457 return 0;
1458 }
1459
Michal Vasko9f96a052020-03-10 09:41:45 +01001460 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001461 /* elem is always first, 2nd node is after it */
1462 if (item1->type == LYXP_NODE_ELEM) {
1463 assert(item2->type != LYXP_NODE_ELEM);
1464 return -1;
1465 }
1466
Michal Vasko9f96a052020-03-10 09:41:45 +01001467 /* 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 +02001468 /* 2nd is before 1st */
1469 if (((item1->type == LYXP_NODE_TEXT)
Michal Vasko9f96a052020-03-10 09:41:45 +01001470 && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META)))
1471 || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM))
1472 || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META))
1473 && (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001474 return 1;
1475 }
1476
Michal Vasko9f96a052020-03-10 09:41:45 +01001477 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001478 /* 2nd is after 1st */
1479 return -1;
1480}
1481
1482/**
1483 * @brief Set cast for comparisons.
1484 *
1485 * @param[in] trg Target set to cast source into.
1486 * @param[in] src Source set.
1487 * @param[in] type Target set type.
1488 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001489 * @return LY_ERR
1490 */
1491static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001492set_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 +02001493{
1494 assert(src->type == LYXP_SET_NODE_SET);
1495
1496 set_init(trg, src);
1497
1498 /* insert node into target set */
1499 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1500
1501 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001502 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001503}
1504
Michal Vasko4c7763f2020-07-27 17:40:37 +02001505/**
1506 * @brief Set content canonization for comparisons.
1507 *
1508 * @param[in] trg Target set to put the canononized source into.
1509 * @param[in] src Source set.
1510 * @param[in] xp_node Source XPath node/meta to use for canonization.
1511 * @return LY_ERR
1512 */
1513static LY_ERR
1514set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1515{
1516 struct lysc_type *type = NULL;
1517 struct lyd_value val;
1518 struct ly_err_item *err = NULL;
1519 char *str, *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001520 uint8_t dynamic;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001521 LY_ERR rc;
1522
1523 /* is there anything to canonize even? */
1524 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1525 /* do we have a type to use for canonization? */
1526 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1527 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1528 } else if (xp_node->type == LYXP_NODE_META) {
1529 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1530 }
1531 }
1532 if (!type) {
1533 goto fill;
1534 }
1535
1536 if (src->type == LYXP_SET_NUMBER) {
1537 /* canonize number */
1538 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1539 LOGMEM(src->ctx);
1540 return LY_EMEM;
1541 }
1542 } else {
1543 /* canonize string */
1544 str = strdup(src->val.str);
1545 }
1546
1547 /* ignore errors, the value may not satisfy schema constraints */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001548 rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_DYNAMIC,
1549 LY_PREF_JSON, NULL, NULL, NULL, &val, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001550 ly_err_free(err);
1551 if (rc) {
1552 /* invalid value */
1553 free(str);
1554 goto fill;
1555 }
1556
1557 /* storing successful, now print the canonical value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001558 str = (char *)type->plugin->print(&val, LY_PREF_JSON, NULL, &dynamic);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001559
1560 /* use the canonized value */
1561 set_init(trg, src);
1562 trg->type = src->type;
1563 if (src->type == LYXP_SET_NUMBER) {
1564 trg->val.num = strtold(str, &ptr);
1565 if (dynamic) {
1566 free(str);
1567 }
1568 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1569 } else {
1570 trg->val.str = (dynamic ? str : strdup(str));
1571 }
1572 type->plugin->free(src->ctx, &val);
1573 return LY_SUCCESS;
1574
1575fill:
1576 /* no canonization needed/possible */
1577 set_fill_set(trg, src);
1578 return LY_SUCCESS;
1579}
1580
Michal Vasko03ff5a72019-09-11 13:49:33 +02001581#ifndef NDEBUG
1582
1583/**
1584 * @brief Bubble sort @p set into XPath document order.
1585 * Context position aware. Unused in the 'Release' build target.
1586 *
1587 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001588 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1589 */
1590static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001591set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001592{
1593 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001594 int ret = 0, cmp;
1595 uint8_t inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001596 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001597 struct lyxp_set_node item;
1598 struct lyxp_set_hash_node hnode;
1599 uint64_t hash;
1600
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001601 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001602 return 0;
1603 }
1604
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001605 /* find first top-level node to be used as anchor for positions */
Radek Krejci1e008d22020-08-17 11:37:37 +02001606 for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001607 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001608
1609 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001610 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001611 return -1;
1612 }
1613
1614 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1615 print_set_debug(set);
1616
1617 for (i = 0; i < set->used; ++i) {
1618 inverted = 0;
1619 change = 0;
1620
1621 for (j = 1; j < set->used - i; ++j) {
1622 /* compare node positions */
1623 if (inverted) {
1624 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1625 } else {
1626 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1627 }
1628
1629 /* swap if needed */
1630 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1631 change = 1;
1632
1633 item = set->val.nodes[j - 1];
1634 set->val.nodes[j - 1] = set->val.nodes[j];
1635 set->val.nodes[j] = item;
1636 } else {
1637 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1638 inverted = !inverted;
1639 }
1640 }
1641
1642 ++ret;
1643
1644 if (!change) {
1645 break;
1646 }
1647 }
1648
1649 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1650 print_set_debug(set);
1651
1652 /* check node hashes */
1653 if (set->used >= LYD_HT_MIN_ITEMS) {
1654 assert(set->ht);
1655 for (i = 0; i < set->used; ++i) {
1656 hnode.node = set->val.nodes[i].node;
1657 hnode.type = set->val.nodes[i].type;
1658
1659 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1660 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1661 hash = dict_hash_multi(hash, NULL, 0);
1662
1663 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1664 }
1665 }
1666
1667 return ret - 1;
1668}
1669
1670/**
1671 * @brief Remove duplicate entries in a sorted node set.
1672 *
1673 * @param[in] set Sorted set to check.
1674 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1675 */
1676static LY_ERR
1677set_sorted_dup_node_clean(struct lyxp_set *set)
1678{
1679 uint32_t i = 0;
1680 LY_ERR ret = LY_SUCCESS;
1681
1682 if (set->used > 1) {
1683 while (i < set->used - 1) {
1684 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node)
1685 && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001686 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001687 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001688 }
Michal Vasko57eab132019-09-24 11:46:26 +02001689 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001690 }
1691 }
1692
Michal Vasko2caefc12019-11-14 16:07:56 +01001693 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001694 return ret;
1695}
1696
1697#endif
1698
1699/**
1700 * @brief Merge 2 sorted sets into one.
1701 *
1702 * @param[in,out] trg Set to merge into. Duplicates are removed.
1703 * @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 +02001704 * @return LY_ERR
1705 */
1706static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001707set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001708{
1709 uint32_t i, j, k, count, dup_count;
1710 int cmp;
1711 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001712
Michal Vaskod3678892020-05-21 10:06:58 +02001713 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001714 return LY_EINVAL;
1715 }
1716
Michal Vaskod3678892020-05-21 10:06:58 +02001717 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001718 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001719 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001720 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001721 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001722 return LY_SUCCESS;
1723 }
1724
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001725 /* find first top-level node to be used as anchor for positions */
Radek Krejci1e008d22020-08-17 11:37:37 +02001726 for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001727 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001728
1729 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001730 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001731 return LY_EINT;
1732 }
1733
1734#ifndef NDEBUG
1735 LOGDBG(LY_LDGXPATH, "MERGE target");
1736 print_set_debug(trg);
1737 LOGDBG(LY_LDGXPATH, "MERGE source");
1738 print_set_debug(src);
1739#endif
1740
1741 /* make memory for the merge (duplicates are not detected yet, so space
1742 * will likely be wasted on them, too bad) */
1743 if (trg->size - trg->used < src->used) {
1744 trg->size = trg->used + src->used;
1745
1746 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1747 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1748 }
1749
1750 i = 0;
1751 j = 0;
1752 count = 0;
1753 dup_count = 0;
1754 do {
1755 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1756 if (!cmp) {
1757 if (!count) {
1758 /* duplicate, just skip it */
1759 ++i;
1760 ++j;
1761 } else {
1762 /* we are copying something already, so let's copy the duplicate too,
1763 * we are hoping that afterwards there are some more nodes to
1764 * copy and this way we can copy them all together */
1765 ++count;
1766 ++dup_count;
1767 ++i;
1768 ++j;
1769 }
1770 } else if (cmp < 0) {
1771 /* inserting src node into trg, just remember it for now */
1772 ++count;
1773 ++i;
1774
1775 /* insert the hash now */
1776 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1777 } else if (count) {
1778copy_nodes:
1779 /* time to actually copy the nodes, we have found the largest block of nodes */
1780 memmove(&trg->val.nodes[j + (count - dup_count)],
1781 &trg->val.nodes[j],
1782 (trg->used - j) * sizeof *trg->val.nodes);
1783 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1784
1785 trg->used += count - dup_count;
1786 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1787 j += count - dup_count;
1788
1789 count = 0;
1790 dup_count = 0;
1791 } else {
1792 ++j;
1793 }
1794 } while ((i < src->used) && (j < trg->used));
1795
1796 if ((i < src->used) || count) {
1797 /* insert all the hashes first */
1798 for (k = i; k < src->used; ++k) {
1799 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1800 }
1801
1802 /* loop ended, but we need to copy something at trg end */
1803 count += src->used - i;
1804 i = src->used;
1805 goto copy_nodes;
1806 }
1807
1808 /* we are inserting hashes before the actual node insert, which causes
1809 * situations when there were initially not enough items for a hash table,
1810 * but even after some were inserted, hash table was not created (during
1811 * insertion the number of items is not updated yet) */
1812 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1813 set_insert_node_hash(trg, NULL, 0);
1814 }
1815
1816#ifndef NDEBUG
1817 LOGDBG(LY_LDGXPATH, "MERGE result");
1818 print_set_debug(trg);
1819#endif
1820
Michal Vaskod3678892020-05-21 10:06:58 +02001821 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001822 return LY_SUCCESS;
1823}
1824
1825/*
1826 * (re)parse functions
1827 *
1828 * Parse functions parse the expression into
1829 * tokens (syntactic analysis).
1830 *
1831 * Reparse functions perform semantic analysis
1832 * (do not save the result, just a check) of
1833 * the expression and fill repeat indices.
1834 */
1835
Michal Vasko14676352020-05-29 11:35:55 +02001836LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001837lyxp_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 +02001838{
Michal Vasko004d3152020-06-11 19:59:22 +02001839 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001840 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001841 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1842 }
Michal Vasko14676352020-05-29 11:35:55 +02001843 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001844 }
1845
Michal Vasko004d3152020-06-11 19:59:22 +02001846 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001847 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001848 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001849 lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001850 }
Michal Vasko14676352020-05-29 11:35:55 +02001851 return LY_ENOT;
1852 }
1853
1854 return LY_SUCCESS;
1855}
1856
Michal Vasko004d3152020-06-11 19:59:22 +02001857LY_ERR
1858lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1859{
1860 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1861
1862 /* skip the token */
1863 ++(*tok_idx);
1864
1865 return LY_SUCCESS;
1866}
1867
Michal Vasko14676352020-05-29 11:35:55 +02001868/* just like lyxp_check_token() but tests for 2 tokens */
1869static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001870exp_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 +02001871 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001872{
Michal Vasko004d3152020-06-11 19:59:22 +02001873 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001874 if (ctx) {
1875 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1876 }
1877 return LY_EINCOMPLETE;
1878 }
1879
Michal Vasko004d3152020-06-11 19:59:22 +02001880 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001881 if (ctx) {
1882 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001883 lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001884 }
1885 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001886 }
1887
1888 return LY_SUCCESS;
1889}
1890
1891/**
1892 * @brief Stack operation push on the repeat array.
1893 *
1894 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001895 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001896 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1897 */
1898static void
Michal Vasko004d3152020-06-11 19:59:22 +02001899exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001900{
1901 uint16_t i;
1902
Michal Vasko004d3152020-06-11 19:59:22 +02001903 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001904 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001905 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1906 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1907 exp->repeat[tok_idx][i] = repeat_op_idx;
1908 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001909 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001910 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1911 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1912 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001913 }
1914}
1915
1916/**
1917 * @brief Reparse Predicate. Logs directly on error.
1918 *
1919 * [7] Predicate ::= '[' Expr ']'
1920 *
1921 * @param[in] ctx Context for logging.
1922 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001923 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001924 * @return LY_ERR
1925 */
1926static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001927reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001928{
1929 LY_ERR rc;
1930
Michal Vasko004d3152020-06-11 19:59:22 +02001931 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001932 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001933 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001934
Michal Vasko004d3152020-06-11 19:59:22 +02001935 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001936 LY_CHECK_RET(rc);
1937
Michal Vasko004d3152020-06-11 19:59:22 +02001938 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001939 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001940 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001941
1942 return LY_SUCCESS;
1943}
1944
1945/**
1946 * @brief Reparse RelativeLocationPath. Logs directly on error.
1947 *
1948 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1949 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1950 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1951 *
1952 * @param[in] ctx Context for logging.
1953 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001954 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001955 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1956 */
1957static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001958reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001959{
1960 LY_ERR rc;
1961
Michal Vasko004d3152020-06-11 19:59:22 +02001962 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001963 LY_CHECK_RET(rc);
1964
1965 goto step;
1966 do {
1967 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001968 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001969
Michal Vasko004d3152020-06-11 19:59:22 +02001970 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001971 LY_CHECK_RET(rc);
1972step:
1973 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001974 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001975 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001976 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001977 break;
1978
1979 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001980 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001981 break;
1982
1983 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02001984 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001985
Michal Vasko004d3152020-06-11 19:59:22 +02001986 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001987 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001988 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001989 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001990 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001991 return LY_EVALID;
1992 }
Radek Krejci0f969882020-08-21 16:56:47 +02001993 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001994 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02001995 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001996 goto reparse_predicate;
1997 break;
1998
1999 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002000 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002001
2002 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002003 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002004 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002005 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002006
2007 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002008 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002009 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002010 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002011
2012reparse_predicate:
2013 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002014 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2015 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002016 LY_CHECK_RET(rc);
2017 }
2018 break;
2019 default:
2020 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02002021 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002022 return LY_EVALID;
2023 }
Michal Vasko004d3152020-06-11 19:59:22 +02002024 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002025
2026 return LY_SUCCESS;
2027}
2028
2029/**
2030 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2031 *
2032 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2033 *
2034 * @param[in] ctx Context for logging.
2035 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002036 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002037 * @return LY_ERR
2038 */
2039static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002040reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002041{
2042 LY_ERR rc;
2043
Michal Vasko004d3152020-06-11 19:59:22 +02002044 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 +02002045
2046 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002047 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002048 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002049 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002050
Michal Vasko004d3152020-06-11 19:59:22 +02002051 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002052 return LY_SUCCESS;
2053 }
Michal Vasko004d3152020-06-11 19:59:22 +02002054 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002055 case LYXP_TOKEN_DOT:
2056 case LYXP_TOKEN_DDOT:
2057 case LYXP_TOKEN_AT:
2058 case LYXP_TOKEN_NAMETEST:
2059 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002060 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002061 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002062 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002063 default:
2064 break;
2065 }
2066
Michal Vasko03ff5a72019-09-11 13:49:33 +02002067 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002068 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002069 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002070
Michal Vasko004d3152020-06-11 19:59:22 +02002071 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002072 LY_CHECK_RET(rc);
2073 }
2074
2075 return LY_SUCCESS;
2076}
2077
2078/**
2079 * @brief Reparse FunctionCall. Logs directly on error.
2080 *
2081 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2082 *
2083 * @param[in] ctx Context for logging.
2084 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002085 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002086 * @return LY_ERR
2087 */
2088static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002089reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002090{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002091 int8_t min_arg_count = -1;
2092 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002093 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002094 LY_ERR rc;
2095
Michal Vasko004d3152020-06-11 19:59:22 +02002096 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002097 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002098 func_tok_idx = *tok_idx;
2099 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002100 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002101 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002102 min_arg_count = 1;
2103 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002104 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002105 min_arg_count = 1;
2106 max_arg_count = 1;
2107 }
2108 break;
2109 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002110 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002111 min_arg_count = 1;
2112 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002113 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002114 min_arg_count = 0;
2115 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002116 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002117 min_arg_count = 0;
2118 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002119 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002120 min_arg_count = 0;
2121 max_arg_count = 0;
2122 }
2123 break;
2124 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002125 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002126 min_arg_count = 1;
2127 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002128 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002129 min_arg_count = 0;
2130 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002131 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002132 min_arg_count = 1;
2133 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002134 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 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]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002138 min_arg_count = 1;
2139 max_arg_count = 1;
2140 }
2141 break;
2142 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002143 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002144 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002145 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002146 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002147 min_arg_count = 0;
2148 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002149 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002150 min_arg_count = 0;
2151 max_arg_count = 1;
2152 }
2153 break;
2154 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002155 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002156 min_arg_count = 1;
2157 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002158 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 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]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002162 min_arg_count = 0;
2163 max_arg_count = 0;
2164 }
2165 break;
2166 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002167 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002168 min_arg_count = 2;
2169 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002170 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002171 min_arg_count = 0;
2172 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002173 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002174 min_arg_count = 2;
2175 max_arg_count = 2;
2176 }
2177 break;
2178 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002179 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002180 min_arg_count = 2;
2181 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002182 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002183 min_arg_count = 3;
2184 max_arg_count = 3;
2185 }
2186 break;
2187 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002188 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002189 min_arg_count = 0;
2190 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002191 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002192 min_arg_count = 1;
2193 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002194 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002195 min_arg_count = 2;
2196 max_arg_count = 2;
2197 }
2198 break;
2199 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002200 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002201 min_arg_count = 2;
2202 max_arg_count = 2;
2203 }
2204 break;
2205 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002206 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002207 min_arg_count = 2;
2208 max_arg_count = 2;
2209 }
2210 break;
2211 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002212 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002213 min_arg_count = 0;
2214 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002215 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002216 min_arg_count = 0;
2217 max_arg_count = 1;
2218 }
2219 break;
2220 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002221 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002222 min_arg_count = 0;
2223 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002224 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002225 min_arg_count = 2;
2226 max_arg_count = 2;
2227 }
2228 break;
2229 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002230 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002231 min_arg_count = 2;
2232 max_arg_count = 2;
2233 }
2234 break;
2235 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002236 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002237 min_arg_count = 2;
2238 max_arg_count = 2;
2239 }
2240 break;
2241 }
2242 if (min_arg_count == -1) {
Michal Vasko004d3152020-06-11 19:59:22 +02002243 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 +02002244 return LY_EINVAL;
2245 }
Michal Vasko004d3152020-06-11 19:59:22 +02002246 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002247
2248 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002249 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002250 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002251 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002252
2253 /* ( Expr ( ',' Expr )* )? */
2254 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002255 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002256 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002257 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002258 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002259 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002260 LY_CHECK_RET(rc);
2261 }
Michal Vasko004d3152020-06-11 19:59:22 +02002262 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2263 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002264
2265 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002266 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002267 LY_CHECK_RET(rc);
2268 }
2269
2270 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002271 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002272 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002273 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002274
Radek Krejci1deb5be2020-08-26 16:43:36 +02002275 if ((arg_count < (uint8_t)min_arg_count) || (arg_count > max_arg_count)) {
Michal Vasko004d3152020-06-11 19:59:22 +02002276 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx],
2277 &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002278 return LY_EVALID;
2279 }
2280
2281 return LY_SUCCESS;
2282}
2283
2284/**
2285 * @brief Reparse PathExpr. Logs directly on error.
2286 *
2287 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2288 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2289 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2290 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2291 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2292 *
2293 * @param[in] ctx Context for logging.
2294 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002295 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002296 * @return LY_ERR
2297 */
2298static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002299reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002300{
2301 LY_ERR rc;
2302
Michal Vasko004d3152020-06-11 19:59:22 +02002303 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002304 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002305 }
2306
Michal Vasko004d3152020-06-11 19:59:22 +02002307 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002308 case LYXP_TOKEN_PAR1:
2309 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002310 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002311
Michal Vasko004d3152020-06-11 19:59:22 +02002312 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002313 LY_CHECK_RET(rc);
2314
Michal Vasko004d3152020-06-11 19:59:22 +02002315 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002316 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002317 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002318 goto predicate;
2319 break;
2320 case LYXP_TOKEN_DOT:
2321 case LYXP_TOKEN_DDOT:
2322 case LYXP_TOKEN_AT:
2323 case LYXP_TOKEN_NAMETEST:
2324 case LYXP_TOKEN_NODETYPE:
2325 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002326 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002327 LY_CHECK_RET(rc);
2328 break;
2329 case LYXP_TOKEN_FUNCNAME:
2330 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002331 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002332 LY_CHECK_RET(rc);
2333 goto predicate;
2334 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002335 case LYXP_TOKEN_OPER_PATH:
2336 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002337 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002338 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002339 LY_CHECK_RET(rc);
2340 break;
2341 case LYXP_TOKEN_LITERAL:
2342 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002343 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002344 goto predicate;
2345 break;
2346 case LYXP_TOKEN_NUMBER:
2347 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002348 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002349 goto predicate;
2350 break;
2351 default:
2352 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02002353 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002354 return LY_EVALID;
2355 }
2356
2357 return LY_SUCCESS;
2358
2359predicate:
2360 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002361 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2362 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002363 LY_CHECK_RET(rc);
2364 }
2365
2366 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002367 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002368
2369 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002370 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002371
Michal Vasko004d3152020-06-11 19:59:22 +02002372 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002373 LY_CHECK_RET(rc);
2374 }
2375
2376 return LY_SUCCESS;
2377}
2378
2379/**
2380 * @brief Reparse UnaryExpr. Logs directly on error.
2381 *
2382 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2383 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2384 *
2385 * @param[in] ctx Context for logging.
2386 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002387 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002388 * @return LY_ERR
2389 */
2390static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002391reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002392{
2393 uint16_t prev_exp;
2394 LY_ERR rc;
2395
2396 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002397 prev_exp = *tok_idx;
2398 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2399 && (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002400 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002401 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002402 }
2403
2404 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002405 prev_exp = *tok_idx;
2406 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002407 LY_CHECK_RET(rc);
2408
2409 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002410 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002411 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002412 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002413
Michal Vasko004d3152020-06-11 19:59:22 +02002414 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002415 LY_CHECK_RET(rc);
2416 }
2417
2418 return LY_SUCCESS;
2419}
2420
2421/**
2422 * @brief Reparse AdditiveExpr. Logs directly on error.
2423 *
2424 * [15] AdditiveExpr ::= MultiplicativeExpr
2425 * | AdditiveExpr '+' MultiplicativeExpr
2426 * | AdditiveExpr '-' MultiplicativeExpr
2427 * [16] MultiplicativeExpr ::= UnaryExpr
2428 * | MultiplicativeExpr '*' UnaryExpr
2429 * | MultiplicativeExpr 'div' UnaryExpr
2430 * | MultiplicativeExpr 'mod' UnaryExpr
2431 *
2432 * @param[in] ctx Context for logging.
2433 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002434 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002435 * @return LY_ERR
2436 */
2437static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002438reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002439{
2440 uint16_t prev_add_exp, prev_mul_exp;
2441 LY_ERR rc;
2442
Michal Vasko004d3152020-06-11 19:59:22 +02002443 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002444 goto reparse_multiplicative_expr;
2445
2446 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002447 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2448 && ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002449 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002450 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002451
2452reparse_multiplicative_expr:
2453 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002454 prev_mul_exp = *tok_idx;
2455 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002456 LY_CHECK_RET(rc);
2457
2458 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002459 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2460 && ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002461 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002462 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002463
Michal Vasko004d3152020-06-11 19:59:22 +02002464 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002465 LY_CHECK_RET(rc);
2466 }
2467 }
2468
2469 return LY_SUCCESS;
2470}
2471
2472/**
2473 * @brief Reparse EqualityExpr. Logs directly on error.
2474 *
2475 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2476 * | EqualityExpr '!=' RelationalExpr
2477 * [14] RelationalExpr ::= AdditiveExpr
2478 * | RelationalExpr '<' AdditiveExpr
2479 * | RelationalExpr '>' AdditiveExpr
2480 * | RelationalExpr '<=' AdditiveExpr
2481 * | RelationalExpr '>=' AdditiveExpr
2482 *
2483 * @param[in] ctx Context for logging.
2484 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002485 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002486 * @return LY_ERR
2487 */
2488static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002489reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002490{
2491 uint16_t prev_eq_exp, prev_rel_exp;
2492 LY_ERR rc;
2493
Michal Vasko004d3152020-06-11 19:59:22 +02002494 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002495 goto reparse_additive_expr;
2496
2497 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002498 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002499 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002500 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002501
2502reparse_additive_expr:
2503 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002504 prev_rel_exp = *tok_idx;
2505 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002506 LY_CHECK_RET(rc);
2507
2508 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002509 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002510 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002511 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002512
Michal Vasko004d3152020-06-11 19:59:22 +02002513 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002514 LY_CHECK_RET(rc);
2515 }
2516 }
2517
2518 return LY_SUCCESS;
2519}
2520
2521/**
2522 * @brief Reparse OrExpr. Logs directly on error.
2523 *
2524 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2525 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2526 *
2527 * @param[in] ctx Context for logging.
2528 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002529 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002530 * @return LY_ERR
2531 */
2532static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002533reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002534{
2535 uint16_t prev_or_exp, prev_and_exp;
2536 LY_ERR rc;
2537
Michal Vasko004d3152020-06-11 19:59:22 +02002538 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002539 goto reparse_equality_expr;
2540
2541 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002542 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 +02002543 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002544 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002545
2546reparse_equality_expr:
2547 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002548 prev_and_exp = *tok_idx;
2549 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002550 LY_CHECK_RET(rc);
2551
2552 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002553 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 +02002554 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002555 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002556
Michal Vasko004d3152020-06-11 19:59:22 +02002557 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002558 LY_CHECK_RET(rc);
2559 }
2560 }
2561
2562 return LY_SUCCESS;
2563}
Radek Krejcib1646a92018-11-02 16:08:26 +01002564
2565/**
2566 * @brief Parse NCName.
2567 *
2568 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002569 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002570 */
Radek Krejcid4270262019-01-07 15:07:25 +01002571static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002572parse_ncname(const char *ncname)
2573{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002574 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002575 size_t size;
2576 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002577
2578 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2579 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2580 return len;
2581 }
2582
2583 do {
2584 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002585 if (!*ncname) {
2586 break;
2587 }
Radek Krejcid4270262019-01-07 15:07:25 +01002588 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002589 } while (is_xmlqnamechar(uc) && (uc != ':'));
2590
2591 return len;
2592}
2593
2594/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002595 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002596 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002597 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002598 * @param[in] exp Expression to use.
2599 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002600 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002601 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002602 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002603 */
2604static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002605exp_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 +01002606{
2607 uint32_t prev;
2608
2609 if (exp->used == exp->size) {
2610 prev = exp->size;
2611 exp->size += LYXP_EXPR_SIZE_STEP;
2612 if (prev > exp->size) {
2613 LOGINT(ctx);
2614 return LY_EINT;
2615 }
2616
2617 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2618 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2619 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2620 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2621 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2622 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2623 }
2624
2625 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002626 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002627 exp->tok_len[exp->used] = tok_len;
2628 ++exp->used;
2629 return LY_SUCCESS;
2630}
2631
2632void
Michal Vasko14676352020-05-29 11:35:55 +02002633lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002634{
2635 uint16_t i;
2636
2637 if (!expr) {
2638 return;
2639 }
2640
2641 lydict_remove(ctx, expr->expr);
2642 free(expr->tokens);
2643 free(expr->tok_pos);
2644 free(expr->tok_len);
2645 if (expr->repeat) {
2646 for (i = 0; i < expr->used; ++i) {
2647 free(expr->repeat[i]);
2648 }
2649 }
2650 free(expr->repeat);
2651 free(expr);
2652}
2653
2654struct lyxp_expr *
Radek Krejci1deb5be2020-08-26 16:43:36 +02002655lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr, size_t expr_len, uint8_t reparse)
Radek Krejcib1646a92018-11-02 16:08:26 +01002656{
2657 struct lyxp_expr *ret;
Radek Krejcid4270262019-01-07 15:07:25 +01002658 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002659 enum lyxp_token tok_type;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002660 uint8_t prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002661 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002662
Michal Vasko004d3152020-06-11 19:59:22 +02002663 if (!expr[0]) {
2664 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
2665 return NULL;
2666 }
2667
2668 if (!expr_len) {
2669 expr_len = strlen(expr);
2670 }
2671 if (expr_len > UINT16_MAX) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002672 LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
2673 return NULL;
2674 }
2675
2676 /* init lyxp_expr structure */
2677 ret = calloc(1, sizeof *ret);
2678 LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error);
Michal Vasko004d3152020-06-11 19:59:22 +02002679 ret->expr = lydict_insert(ctx, expr, expr_len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002680 LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error);
2681 ret->used = 0;
2682 ret->size = LYXP_EXPR_SIZE_START;
2683 ret->tokens = malloc(ret->size * sizeof *ret->tokens);
2684 LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error);
2685
2686 ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos);
2687 LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error);
2688
2689 ret->tok_len = malloc(ret->size * sizeof *ret->tok_len);
2690 LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error);
2691
Michal Vasko004d3152020-06-11 19:59:22 +02002692 /* make expr 0-terminated */
2693 expr = ret->expr;
2694
Radek Krejcib1646a92018-11-02 16:08:26 +01002695 while (is_xmlws(expr[parsed])) {
2696 ++parsed;
2697 }
2698
2699 do {
2700 if (expr[parsed] == '(') {
2701
2702 /* '(' */
2703 tok_len = 1;
2704 tok_type = LYXP_TOKEN_PAR1;
2705
2706 if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) {
2707 /* it is a NodeType/FunctionName after all */
2708 if (((ret->tok_len[ret->used - 1] == 4)
2709 && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4)
2710 || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) ||
2711 ((ret->tok_len[ret->used - 1] == 7)
2712 && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) {
2713 ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE;
2714 } else {
2715 ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME;
2716 }
2717 prev_function_check = 0;
2718 }
2719
2720 } else if (expr[parsed] == ')') {
2721
2722 /* ')' */
2723 tok_len = 1;
2724 tok_type = LYXP_TOKEN_PAR2;
2725
2726 } else if (expr[parsed] == '[') {
2727
2728 /* '[' */
2729 tok_len = 1;
2730 tok_type = LYXP_TOKEN_BRACK1;
2731
2732 } else if (expr[parsed] == ']') {
2733
2734 /* ']' */
2735 tok_len = 1;
2736 tok_type = LYXP_TOKEN_BRACK2;
2737
2738 } else if (!strncmp(&expr[parsed], "..", 2)) {
2739
2740 /* '..' */
2741 tok_len = 2;
2742 tok_type = LYXP_TOKEN_DDOT;
2743
2744 } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) {
2745
2746 /* '.' */
2747 tok_len = 1;
2748 tok_type = LYXP_TOKEN_DOT;
2749
2750 } else if (expr[parsed] == '@') {
2751
2752 /* '@' */
2753 tok_len = 1;
2754 tok_type = LYXP_TOKEN_AT;
2755
2756 } else if (expr[parsed] == ',') {
2757
2758 /* ',' */
2759 tok_len = 1;
2760 tok_type = LYXP_TOKEN_COMMA;
2761
2762 } else if (expr[parsed] == '\'') {
2763
2764 /* Literal with ' */
Radek Krejci1e008d22020-08-17 11:37:37 +02002765 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002766 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2767 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2768 ++tok_len;
2769 tok_type = LYXP_TOKEN_LITERAL;
2770
2771 } else if (expr[parsed] == '\"') {
2772
2773 /* Literal with " */
Radek Krejci1e008d22020-08-17 11:37:37 +02002774 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002775 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2776 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2777 ++tok_len;
2778 tok_type = LYXP_TOKEN_LITERAL;
2779
2780 } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) {
2781
2782 /* Number */
Radek Krejci1e008d22020-08-17 11:37:37 +02002783 for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002784 if (expr[parsed + tok_len] == '.') {
2785 ++tok_len;
Michal Vaskod989ba02020-08-24 10:59:24 +02002786 for ( ; isdigit(expr[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002787 }
2788 tok_type = LYXP_TOKEN_NUMBER;
2789
2790 } else if (expr[parsed] == '/') {
2791
2792 /* Operator '/', '//' */
2793 if (!strncmp(&expr[parsed], "//", 2)) {
2794 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002795 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002796 } else {
2797 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002798 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002799 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002800
Michal Vaskod989ba02020-08-24 10:59:24 +02002801 } else if (!strncmp(&expr[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002802
Michal Vasko3e48bf32020-06-01 08:39:07 +02002803 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002804 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002805 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2806
2807 } else if (!strncmp(&expr[parsed], "<=", 2) || !strncmp(&expr[parsed], ">=", 2)) {
2808
2809 /* Operator '<=', '>=' */
2810 tok_len = 2;
2811 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002812
2813 } else if (expr[parsed] == '|') {
2814
2815 /* Operator '|' */
2816 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002817 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002818
2819 } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) {
2820
2821 /* Operator '+', '-' */
2822 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002823 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002824
Michal Vasko3e48bf32020-06-01 08:39:07 +02002825 } else if (expr[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002826
Michal Vasko3e48bf32020-06-01 08:39:07 +02002827 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002828 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002829 tok_type = LYXP_TOKEN_OPER_EQUAL;
2830
2831 } else if ((expr[parsed] == '<') || (expr[parsed] == '>')) {
2832
2833 /* Operator '<', '>' */
2834 tok_len = 1;
2835 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002836
2837 } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT)
2838 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1)
2839 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1)
2840 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA)
Michal Vasko3e48bf32020-06-01 08:39:07 +02002841 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_LOG)
2842 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_EQUAL)
2843 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_NEQUAL)
2844 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_COMP)
2845 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_MATH)
2846 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_UNI)
2847 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_PATH)
2848 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002849
2850 /* Operator '*', 'or', 'and', 'mod', or 'div' */
2851 if (expr[parsed] == '*') {
2852 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002853 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002854
2855 } else if (!strncmp(&expr[parsed], "or", 2)) {
2856 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002857 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002858
2859 } else if (!strncmp(&expr[parsed], "and", 3)) {
2860 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002861 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002862
2863 } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) {
2864 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002865 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002866
2867 } else if (prev_function_check) {
Michal Vasko53078572019-05-24 08:50:15 +02002868 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
2869 expr[parsed], expr[parsed], ret->tok_len[ret->used - 1], &ret->expr[ret->tok_pos[ret->used - 1]]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002870 goto error;
2871 } else {
Radek Krejcid4270262019-01-07 15:07:25 +01002872 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr);
Radek Krejcib1646a92018-11-02 16:08:26 +01002873 goto error;
2874 }
2875 } else if (expr[parsed] == '*') {
2876
2877 /* NameTest '*' */
2878 tok_len = 1;
2879 tok_type = LYXP_TOKEN_NAMETEST;
2880
2881 } else {
2882
2883 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejci1deb5be2020-08-26 16:43:36 +02002884 long int ncname_len = parse_ncname(&expr[parsed]);
Radek Krejcid4270262019-01-07 15:07:25 +01002885 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002886 tok_len = ncname_len;
2887
2888 if (expr[parsed + tok_len] == ':') {
2889 ++tok_len;
2890 if (expr[parsed + tok_len] == '*') {
2891 ++tok_len;
2892 } else {
2893 ncname_len = parse_ncname(&expr[parsed + tok_len]);
Radek Krejcid4270262019-01-07 15:07:25 +01002894 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002895 tok_len += ncname_len;
2896 }
2897 /* remove old flag to prevent ambiguities */
2898 prev_function_check = 0;
2899 tok_type = LYXP_TOKEN_NAMETEST;
2900 } else {
2901 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2902 prev_function_check = 1;
2903 tok_type = LYXP_TOKEN_NAMETEST;
2904 }
2905 }
2906
2907 /* store the token, move on to the next one */
2908 LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error);
2909 parsed += tok_len;
2910 while (is_xmlws(expr[parsed])) {
2911 ++parsed;
2912 }
2913
2914 } while (expr[parsed]);
2915
Michal Vasko004d3152020-06-11 19:59:22 +02002916 if (reparse) {
2917 /* prealloc repeat */
2918 ret->repeat = calloc(ret->size, sizeof *ret->repeat);
2919 LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002920
Michal Vasko004d3152020-06-11 19:59:22 +02002921 /* fill repeat */
2922 LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &tok_idx), error);
2923 if (ret->used > tok_idx) {
2924 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
2925 &ret->expr[ret->tok_pos[tok_idx]]);
2926 goto error;
2927 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002928 }
2929
2930 print_expr_struct_debug(ret);
2931
Radek Krejcib1646a92018-11-02 16:08:26 +01002932 return ret;
2933
2934error:
2935 lyxp_expr_free(ctx, ret);
2936 return NULL;
2937}
2938
Michal Vasko004d3152020-06-11 19:59:22 +02002939struct lyxp_expr *
2940lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp)
2941{
2942 struct lyxp_expr *dup;
2943 uint32_t i, j;
2944
2945 dup = calloc(1, sizeof *dup);
2946 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx), error);
2947
2948 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
2949 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx), error);
2950 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2951
2952 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
2953 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx), error);
2954 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2955
2956 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
2957 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx), error);
2958 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2959
2960 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
2961 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx), error);
2962 for (i = 0; i < exp->used; ++i) {
2963 if (!exp->repeat[i]) {
2964 dup->repeat[i] = NULL;
2965 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02002966 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002967 /* the ending 0 as well */
2968 ++j;
2969
Michal Vasko99c71642020-07-03 13:33:36 +02002970 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko004d3152020-06-11 19:59:22 +02002971 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx), error);
2972 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
2973 dup->repeat[i][j - 1] = 0;
2974 }
2975 }
2976
2977 dup->used = exp->used;
2978 dup->size = exp->used;
2979 dup->expr = lydict_insert(ctx, exp->expr, 0);
2980
2981 return dup;
2982
2983error:
2984 lyxp_expr_free(ctx, dup);
2985 return NULL;
2986}
2987
Michal Vasko03ff5a72019-09-11 13:49:33 +02002988/*
2989 * warn functions
2990 *
2991 * Warn functions check specific reasonable conditions for schema XPath
2992 * and print a warning if they are not satisfied.
2993 */
2994
2995/**
2996 * @brief Get the last-added schema node that is currently in the context.
2997 *
2998 * @param[in] set Set to search in.
2999 * @return Last-added schema context node, NULL if no node is in context.
3000 */
3001static struct lysc_node *
3002warn_get_scnode_in_ctx(struct lyxp_set *set)
3003{
3004 uint32_t i;
3005
3006 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3007 return NULL;
3008 }
3009
3010 i = set->used;
3011 do {
3012 --i;
3013 if (set->val.scnodes[i].in_ctx == 1) {
3014 /* if there are more, simply return the first found (last added) */
3015 return set->val.scnodes[i].scnode;
3016 }
3017 } while (i);
3018
3019 return NULL;
3020}
3021
3022/**
3023 * @brief Test whether a type is numeric - integer type or decimal64.
3024 *
3025 * @param[in] type Type to test.
3026 * @return 1 if numeric, 0 otherwise.
3027 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02003028static uint8_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02003029warn_is_numeric_type(struct lysc_type *type)
3030{
3031 struct lysc_type_union *uni;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003032 uint8_t ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003033 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003034
3035 switch (type->basetype) {
3036 case LY_TYPE_DEC64:
3037 case LY_TYPE_INT8:
3038 case LY_TYPE_UINT8:
3039 case LY_TYPE_INT16:
3040 case LY_TYPE_UINT16:
3041 case LY_TYPE_INT32:
3042 case LY_TYPE_UINT32:
3043 case LY_TYPE_INT64:
3044 case LY_TYPE_UINT64:
3045 return 1;
3046 case LY_TYPE_UNION:
3047 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003048 LY_ARRAY_FOR(uni->types, u) {
3049 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003050 if (ret) {
3051 /* found a suitable type */
3052 return 1;
3053 }
3054 }
3055 /* did not find any suitable type */
3056 return 0;
3057 case LY_TYPE_LEAFREF:
3058 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3059 default:
3060 return 0;
3061 }
3062}
3063
3064/**
3065 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3066 *
3067 * @param[in] type Type to test.
3068 * @return 1 if string, 0 otherwise.
3069 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02003070static uint8_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02003071warn_is_string_type(struct lysc_type *type)
3072{
3073 struct lysc_type_union *uni;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003074 uint8_t ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003075 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003076
3077 switch (type->basetype) {
3078 case LY_TYPE_BITS:
3079 case LY_TYPE_ENUM:
3080 case LY_TYPE_IDENT:
3081 case LY_TYPE_INST:
3082 case LY_TYPE_STRING:
3083 return 1;
3084 case LY_TYPE_UNION:
3085 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003086 LY_ARRAY_FOR(uni->types, u) {
3087 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003088 if (ret) {
3089 /* found a suitable type */
3090 return 1;
3091 }
3092 }
3093 /* did not find any suitable type */
3094 return 0;
3095 case LY_TYPE_LEAFREF:
3096 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3097 default:
3098 return 0;
3099 }
3100}
3101
3102/**
3103 * @brief Test whether a type is one specific type.
3104 *
3105 * @param[in] type Type to test.
3106 * @param[in] base Expected type.
3107 * @return 1 if it is, 0 otherwise.
3108 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02003109static uint8_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02003110warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3111{
3112 struct lysc_type_union *uni;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003113 uint8_t ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003114 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003115
3116 if (type->basetype == base) {
3117 return 1;
3118 } else if (type->basetype == LY_TYPE_UNION) {
3119 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003120 LY_ARRAY_FOR(uni->types, u) {
3121 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003122 if (ret) {
3123 /* found a suitable type */
3124 return 1;
3125 }
3126 }
3127 /* did not find any suitable type */
3128 return 0;
3129 } else if (type->basetype == LY_TYPE_LEAFREF) {
3130 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3131 }
3132
3133 return 0;
3134}
3135
3136/**
3137 * @brief Get next type of a (union) type.
3138 *
3139 * @param[in] type Base type.
3140 * @param[in] prev_type Previously returned type.
3141 * @return Next type or NULL.
3142 */
3143static struct lysc_type *
3144warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3145{
3146 struct lysc_type_union *uni;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003147 uint8_t found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003148 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003149
3150 switch (type->basetype) {
3151 case LY_TYPE_UNION:
3152 uni = (struct lysc_type_union *)type;
3153 if (!prev_type) {
3154 return uni->types[0];
3155 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003156 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003157 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003158 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003159 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003160 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003161 found = 1;
3162 }
3163 }
3164 return NULL;
3165 default:
3166 if (prev_type) {
3167 assert(type == prev_type);
3168 return NULL;
3169 } else {
3170 return type;
3171 }
3172 }
3173}
3174
3175/**
3176 * @brief Test whether 2 types have a common type.
3177 *
3178 * @param[in] type1 First type.
3179 * @param[in] type2 Second type.
3180 * @return 1 if they do, 0 otherwise.
3181 */
3182static int
3183warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3184{
3185 struct lysc_type *t1, *rt1, *t2, *rt2;
3186
3187 t1 = NULL;
3188 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3189 if (t1->basetype == LY_TYPE_LEAFREF) {
3190 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3191 } else {
3192 rt1 = t1;
3193 }
3194
3195 t2 = NULL;
3196 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3197 if (t2->basetype == LY_TYPE_LEAFREF) {
3198 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3199 } else {
3200 rt2 = t2;
3201 }
3202
3203 if (rt2->basetype == rt1->basetype) {
3204 /* match found */
3205 return 1;
3206 }
3207 }
3208 }
3209
3210 return 0;
3211}
3212
3213/**
3214 * @brief Check both operands of comparison operators.
3215 *
3216 * @param[in] ctx Context for errors.
3217 * @param[in] set1 First operand set.
3218 * @param[in] set2 Second operand set.
3219 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3220 * @param[in] expr Start of the expression to print with the warning.
3221 * @param[in] tok_pos Token position.
3222 */
3223static void
Radek Krejci1deb5be2020-08-26 16:43:36 +02003224warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, uint8_t numbers_only, const char *expr, uint16_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003225{
3226 struct lysc_node_leaf *node1, *node2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003227 uint8_t leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003228
3229 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3230 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3231
3232 if (!node1 && !node2) {
3233 /* no node-sets involved, nothing to do */
3234 return;
3235 }
3236
3237 if (node1) {
3238 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3239 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3240 warning = 1;
3241 leaves = 0;
3242 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3243 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3244 warning = 1;
3245 }
3246 }
3247
3248 if (node2) {
3249 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3250 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3251 warning = 1;
3252 leaves = 0;
3253 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3254 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3255 warning = 1;
3256 }
3257 }
3258
3259 if (node1 && node2 && leaves && !numbers_only) {
3260 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type))
3261 || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type))
3262 || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)
3263 && !warn_is_equal_type(node1->type, node2->type))) {
3264 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3265 warning = 1;
3266 }
3267 }
3268
3269 if (warning) {
3270 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3271 }
3272}
3273
3274/**
3275 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3276 *
3277 * @param[in] exp Parsed XPath expression.
3278 * @param[in] set Set with the leaf/leaf-list.
3279 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3280 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3281 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3282 */
3283static void
3284warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp)
3285{
3286 struct lysc_node *scnode;
3287 struct lysc_type *type;
3288 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003289 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003290 LY_ERR rc;
3291 struct ly_err_item *err = NULL;
3292
3293 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3294 && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
3295 /* check that the node can have the specified value */
3296 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3297 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3298 } else {
3299 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3300 }
3301 if (!value) {
3302 LOGMEM(set->ctx);
3303 return;
3304 }
3305
3306 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3307 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
3308 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
3309 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003310 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko03ff5a72019-09-11 13:49:33 +02003311 exp->expr + exp->tok_pos[equal_exp]);
3312 }
3313
3314 type = ((struct lysc_node_leaf *)scnode)->type;
3315 if (type->basetype != LY_TYPE_IDENT) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003316 rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA, LY_PREF_SCHEMA,
Radek Krejci0f969882020-08-21 16:56:47 +02003317 (void *)set->local_mod, NULL, NULL, &storage, &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003318
3319 if (err) {
3320 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3321 ly_err_free(err);
3322 } else if (rc != LY_SUCCESS) {
3323 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3324 }
3325 if (rc != LY_SUCCESS) {
3326 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003327 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko03ff5a72019-09-11 13:49:33 +02003328 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003329 } else {
3330 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003331 }
3332 }
3333 free(value);
3334 }
3335}
3336
3337/*
3338 * XPath functions
3339 */
3340
3341/**
3342 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3343 * depending on whether the first node bit value from the second argument is set.
3344 *
3345 * @param[in] args Array of arguments.
3346 * @param[in] arg_count Count of elements in @p args.
3347 * @param[in,out] set Context and result set at the same time.
3348 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003349 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003350 */
3351static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003352xpath_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 +02003353{
3354 struct lyd_node_term *leaf;
3355 struct lysc_node_leaf *sleaf;
3356 struct lysc_type_bits *bits;
3357 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003358 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003359
3360 if (options & LYXP_SCNODE_ALL) {
3361 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3362 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003363 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3364 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 +02003365 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3366 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003367 }
3368
3369 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3370 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3371 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 +02003372 } else if (!warn_is_string_type(sleaf->type)) {
3373 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003374 }
3375 }
3376 set_scnode_clear_ctx(set);
3377 return rc;
3378 }
3379
Michal Vaskod3678892020-05-21 10:06:58 +02003380 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003381 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "bit-is-set(node-set, string)");
3382 return LY_EVALID;
3383 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003384 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003385 LY_CHECK_RET(rc);
3386
3387 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003388 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003389 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3390 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3391 && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
3392 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003393 LY_ARRAY_FOR(bits->bits, u) {
3394 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003395 set_fill_boolean(set, 1);
3396 break;
3397 }
3398 }
3399 }
3400 }
3401
3402 return LY_SUCCESS;
3403}
3404
3405/**
3406 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3407 * with the argument converted to boolean.
3408 *
3409 * @param[in] args Array of arguments.
3410 * @param[in] arg_count Count of elements in @p args.
3411 * @param[in,out] set Context and result set at the same time.
3412 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003413 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003414 */
3415static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003416xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003417{
3418 LY_ERR rc;
3419
3420 if (options & LYXP_SCNODE_ALL) {
3421 set_scnode_clear_ctx(set);
3422 return LY_SUCCESS;
3423 }
3424
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003425 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003426 LY_CHECK_RET(rc);
3427 set_fill_set(set, args[0]);
3428
3429 return LY_SUCCESS;
3430}
3431
3432/**
3433 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3434 * with the first argument rounded up to the nearest integer.
3435 *
3436 * @param[in] args Array of arguments.
3437 * @param[in] arg_count Count of elements in @p args.
3438 * @param[in,out] set Context and result set at the same time.
3439 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003440 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003441 */
3442static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003443xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003444{
3445 struct lysc_node_leaf *sleaf;
3446 LY_ERR rc = LY_SUCCESS;
3447
3448 if (options & LYXP_SCNODE_ALL) {
3449 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3450 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003451 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3452 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 +02003453 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3454 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003455 }
3456 set_scnode_clear_ctx(set);
3457 return rc;
3458 }
3459
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003460 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003461 LY_CHECK_RET(rc);
3462 if ((long long)args[0]->val.num != args[0]->val.num) {
3463 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3464 } else {
3465 set_fill_number(set, args[0]->val.num);
3466 }
3467
3468 return LY_SUCCESS;
3469}
3470
3471/**
3472 * @brief Execute the XPath concat(string, string, string*) function.
3473 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3474 *
3475 * @param[in] args Array of arguments.
3476 * @param[in] arg_count Count of elements in @p args.
3477 * @param[in,out] set Context and result set at the same time.
3478 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003479 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003480 */
3481static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003482xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003483{
3484 uint16_t i;
3485 char *str = NULL;
3486 size_t used = 1;
3487 LY_ERR rc = LY_SUCCESS;
3488 struct lysc_node_leaf *sleaf;
3489
3490 if (options & LYXP_SCNODE_ALL) {
3491 for (i = 0; i < arg_count; ++i) {
3492 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3493 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3494 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
3495 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003496 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003497 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 +02003498 }
3499 }
3500 }
3501 set_scnode_clear_ctx(set);
3502 return rc;
3503 }
3504
3505 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003506 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003507 if (rc != LY_SUCCESS) {
3508 free(str);
3509 return rc;
3510 }
3511
3512 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3513 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3514 strcpy(str + used - 1, args[i]->val.str);
3515 used += strlen(args[i]->val.str);
3516 }
3517
3518 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003519 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003520 set->type = LYXP_SET_STRING;
3521 set->val.str = str;
3522
3523 return LY_SUCCESS;
3524}
3525
3526/**
3527 * @brief Execute the XPath contains(string, string) function.
3528 * Returns LYXP_SET_BOOLEAN whether the second argument can
3529 * be found in the first or not.
3530 *
3531 * @param[in] args Array of arguments.
3532 * @param[in] arg_count Count of elements in @p args.
3533 * @param[in,out] set Context and result set at the same time.
3534 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003535 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003536 */
3537static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003538xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003539{
3540 struct lysc_node_leaf *sleaf;
3541 LY_ERR rc = LY_SUCCESS;
3542
3543 if (options & LYXP_SCNODE_ALL) {
3544 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3545 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3546 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 +02003547 } else if (!warn_is_string_type(sleaf->type)) {
3548 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003549 }
3550 }
3551
3552 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3553 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3554 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 +02003555 } else if (!warn_is_string_type(sleaf->type)) {
3556 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003557 }
3558 }
3559 set_scnode_clear_ctx(set);
3560 return rc;
3561 }
3562
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003563 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003564 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003565 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003566 LY_CHECK_RET(rc);
3567
3568 if (strstr(args[0]->val.str, args[1]->val.str)) {
3569 set_fill_boolean(set, 1);
3570 } else {
3571 set_fill_boolean(set, 0);
3572 }
3573
3574 return LY_SUCCESS;
3575}
3576
3577/**
3578 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3579 * with the size of the node-set from the argument.
3580 *
3581 * @param[in] args Array of arguments.
3582 * @param[in] arg_count Count of elements in @p args.
3583 * @param[in,out] set Context and result set at the same time.
3584 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003585 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003586 */
3587static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003588xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003589{
3590 struct lysc_node *scnode = NULL;
3591 LY_ERR rc = LY_SUCCESS;
3592
3593 if (options & LYXP_SCNODE_ALL) {
3594 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3595 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003596 }
3597 set_scnode_clear_ctx(set);
3598 return rc;
3599 }
3600
Michal Vasko03ff5a72019-09-11 13:49:33 +02003601 if (args[0]->type != LYXP_SET_NODE_SET) {
3602 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
3603 return LY_EVALID;
3604 }
3605
3606 set_fill_number(set, args[0]->used);
3607 return LY_SUCCESS;
3608}
3609
3610/**
3611 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3612 * with the context with the intial node.
3613 *
3614 * @param[in] args Array of arguments.
3615 * @param[in] arg_count Count of elements in @p args.
3616 * @param[in,out] set Context and result set at the same time.
3617 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003618 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003619 */
3620static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003621xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003622{
3623 if (arg_count || args) {
3624 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
3625 return LY_EVALID;
3626 }
3627
3628 if (options & LYXP_SCNODE_ALL) {
3629 set_scnode_clear_ctx(set);
3630
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003631 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003632 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003633 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003634
3635 /* position is filled later */
3636 set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0);
3637 }
3638
3639 return LY_SUCCESS;
3640}
3641
3642/**
3643 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3644 * leafref or instance-identifier target node(s).
3645 *
3646 * @param[in] args Array of arguments.
3647 * @param[in] arg_count Count of elements in @p args.
3648 * @param[in,out] set Context and result set at the same time.
3649 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003650 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003651 */
3652static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003653xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003654{
3655 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003656 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003657 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003658 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003659 struct ly_path *p;
3660 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003661 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003662 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003663 LY_ERR rc = LY_SUCCESS;
3664
3665 if (options & LYXP_SCNODE_ALL) {
3666 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3667 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003668 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3669 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 +02003670 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3671 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3672 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003673 }
3674 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003675 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003676 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vasko00cbf532020-06-15 13:58:47 +02003677 oper = lysc_is_output((struct lysc_node *)sleaf) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003678
3679 /* it was already evaluated on schema, it must succeed */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003680 rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE,
3681 oper, LY_PATH_TARGET_MANY, set->format, lref->path_context, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003682 assert(!rc);
3683
3684 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003685 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003686 ly_path_free(set->ctx, p);
3687
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003688 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003689 }
3690
Michal Vasko03ff5a72019-09-11 13:49:33 +02003691 return rc;
3692 }
3693
Michal Vaskod3678892020-05-21 10:06:58 +02003694 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003695 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
3696 return LY_EVALID;
3697 }
3698
Michal Vaskod3678892020-05-21 10:06:58 +02003699 lyxp_set_free_content(set);
3700 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003701 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3702 sleaf = (struct lysc_node_leaf *)leaf->schema;
3703 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3704 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3705 /* find leafref target */
Michal Vasko004d3152020-06-11 19:59:22 +02003706 if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf,
3707 &leaf->value, set->tree, &node, &errmsg)) {
3708 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003709 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003710 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003711 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003712 } else {
3713 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003714 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003715 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Michal Vaskob7be7a82020-08-20 09:09:04 +02003716 LYD_CANON_VALUE(leaf));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003717 return LY_EVALID;
3718 }
3719 }
Michal Vasko004d3152020-06-11 19:59:22 +02003720
3721 /* insert it */
3722 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003723 }
3724 }
3725
3726 return LY_SUCCESS;
3727}
3728
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003729static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003730xpath_derived_(struct lyxp_set **args, struct lyxp_set *set, uint32_t options, uint8_t self_match, const char *func)
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003731{
3732 uint16_t i;
3733 LY_ARRAY_COUNT_TYPE u;
3734 struct lyd_node_term *leaf;
3735 struct lysc_node_leaf *sleaf;
3736 struct lyd_meta *meta;
3737 struct lyd_value data = {0}, *val;
3738 struct ly_err_item *err = NULL;
3739 LY_ERR rc = LY_SUCCESS;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003740 uint8_t found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003741
3742 if (options & LYXP_SCNODE_ALL) {
3743 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3744 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
3745 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3746 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3747 sleaf->name);
3748 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3749 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3750 }
3751
3752 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3753 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3754 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3755 sleaf->name);
3756 } else if (!warn_is_string_type(sleaf->type)) {
3757 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3758 }
3759 }
3760 set_scnode_clear_ctx(set);
3761 return rc;
3762 }
3763
3764 if (args[0]->type != LYXP_SET_NODE_SET) {
3765 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3766 "derived-from(-or-self)(node-set, string)");
3767 return LY_EVALID;
3768 }
3769 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3770 LY_CHECK_RET(rc);
3771
3772 set_fill_boolean(set, 0);
3773 found = 0;
3774 for (i = 0; i < args[0]->used; ++i) {
3775 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3776 continue;
3777 }
3778
3779 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3780 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3781 sleaf = (struct lysc_node_leaf *)leaf->schema;
3782 val = &leaf->value;
3783 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3784 /* uninteresting */
3785 continue;
3786 }
3787
3788 /* store args[1] as ident */
3789 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str),
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003790 0, set->format, (void *)set->local_mod,
Radek Krejci0f969882020-08-21 16:56:47 +02003791 (struct lyd_node *)leaf, set->tree, &data, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003792 } else {
3793 meta = args[0]->val.meta[i].meta;
3794 val = &meta->value;
3795 if (val->realtype->basetype != LY_TYPE_IDENT) {
3796 /* uninteresting */
3797 continue;
3798 }
3799
3800 /* store args[1] as ident */
3801 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str),
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003802 0, set->format, (void *)meta->annotation->module,
3803 meta->parent, set->tree, &data, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003804 }
3805
3806 if (err) {
3807 ly_err_print(err);
3808 ly_err_free(err);
3809 }
3810 LY_CHECK_RET(rc);
3811
3812 /* finally check the identity itself */
3813 if (self_match && (data.ident == val->ident)) {
3814 set_fill_boolean(set, 1);
3815 found = 1;
3816 }
3817 if (!found) {
3818 LY_ARRAY_FOR(data.ident->derived, u) {
3819 if (data.ident->derived[u] == val->ident) {
3820 set_fill_boolean(set, 1);
3821 found = 1;
3822 break;
3823 }
3824 }
3825 }
3826
3827 /* free temporary value */
3828 val->realtype->plugin->free(set->ctx, &data);
3829 if (found) {
3830 break;
3831 }
3832 }
3833
3834 return LY_SUCCESS;
3835}
3836
Michal Vasko03ff5a72019-09-11 13:49:33 +02003837/**
3838 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3839 * on whether the first argument nodes contain a node of an identity derived from the second
3840 * argument identity.
3841 *
3842 * @param[in] args Array of arguments.
3843 * @param[in] arg_count Count of elements in @p args.
3844 * @param[in,out] set Context and result set at the same time.
3845 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003846 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003847 */
3848static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003849xpath_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 +02003850{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003851 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003852}
3853
3854/**
3855 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3856 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3857 * the second argument identity.
3858 *
3859 * @param[in] args Array of arguments.
3860 * @param[in] arg_count Count of elements in @p args.
3861 * @param[in,out] set Context and result set at the same time.
3862 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003863 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003864 */
3865static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003866xpath_derived_from_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 +02003867{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003868 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003869}
3870
3871/**
3872 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3873 * with the integer value of the first node's enum value, otherwise NaN.
3874 *
3875 * @param[in] args Array of arguments.
3876 * @param[in] arg_count Count of elements in @p args.
3877 * @param[in,out] set Context and result set at the same time.
3878 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003879 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003880 */
3881static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003882xpath_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 +02003883{
3884 struct lyd_node_term *leaf;
3885 struct lysc_node_leaf *sleaf;
3886 LY_ERR rc = LY_SUCCESS;
3887
3888 if (options & LYXP_SCNODE_ALL) {
3889 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3890 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003891 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3892 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 +02003893 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3894 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003895 }
3896 set_scnode_clear_ctx(set);
3897 return rc;
3898 }
3899
Michal Vaskod3678892020-05-21 10:06:58 +02003900 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003901 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
3902 return LY_EVALID;
3903 }
3904
3905 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003906 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003907 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3908 sleaf = (struct lysc_node_leaf *)leaf->schema;
3909 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3910 set_fill_number(set, leaf->value.enum_item->value);
3911 }
3912 }
3913
3914 return LY_SUCCESS;
3915}
3916
3917/**
3918 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3919 * with false value.
3920 *
3921 * @param[in] args Array of arguments.
3922 * @param[in] arg_count Count of elements in @p args.
3923 * @param[in,out] set Context and result set at the same time.
3924 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003925 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003926 */
3927static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003928xpath_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 +02003929{
3930 if (options & LYXP_SCNODE_ALL) {
3931 set_scnode_clear_ctx(set);
3932 return LY_SUCCESS;
3933 }
3934
3935 set_fill_boolean(set, 0);
3936 return LY_SUCCESS;
3937}
3938
3939/**
3940 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3941 * with the first argument floored (truncated).
3942 *
3943 * @param[in] args Array of arguments.
3944 * @param[in] arg_count Count of elements in @p args.
3945 * @param[in,out] set Context and result set at the same time.
3946 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003947 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003948 */
3949static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003950xpath_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 +02003951{
3952 LY_ERR rc;
3953
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003954 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003955 LY_CHECK_RET(rc);
3956 if (isfinite(args[0]->val.num)) {
3957 set_fill_number(set, (long long)args[0]->val.num);
3958 }
3959
3960 return LY_SUCCESS;
3961}
3962
3963/**
3964 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3965 * whether the language of the text matches the one from the argument.
3966 *
3967 * @param[in] args Array of arguments.
3968 * @param[in] arg_count Count of elements in @p args.
3969 * @param[in,out] set Context and result set at the same time.
3970 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003971 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003972 */
3973static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003974xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003975{
3976 const struct lyd_node *node;
3977 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01003978 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003979 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003980 LY_ERR rc = LY_SUCCESS;
3981
3982 if (options & LYXP_SCNODE_ALL) {
3983 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3984 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3985 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 +02003986 } else if (!warn_is_string_type(sleaf->type)) {
3987 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003988 }
3989 }
3990 set_scnode_clear_ctx(set);
3991 return rc;
3992 }
3993
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003994 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003995 LY_CHECK_RET(rc);
3996
Michal Vasko03ff5a72019-09-11 13:49:33 +02003997 if (set->type != LYXP_SET_NODE_SET) {
3998 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
3999 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004000 } else if (!set->used) {
4001 set_fill_boolean(set, 0);
4002 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004003 }
4004
4005 switch (set->val.nodes[0].type) {
4006 case LYXP_NODE_ELEM:
4007 case LYXP_NODE_TEXT:
4008 node = set->val.nodes[0].node;
4009 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004010 case LYXP_NODE_META:
4011 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004012 break;
4013 default:
4014 /* nothing to do with roots */
4015 set_fill_boolean(set, 0);
4016 return LY_SUCCESS;
4017 }
4018
Michal Vasko9f96a052020-03-10 09:41:45 +01004019 /* find lang metadata */
Michal Vaskod989ba02020-08-24 10:59:24 +02004020 for ( ; node; node = (struct lyd_node *)node->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004021 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004022 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004023 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004024 break;
4025 }
4026 }
4027
Michal Vasko9f96a052020-03-10 09:41:45 +01004028 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004029 break;
4030 }
4031 }
4032
4033 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004034 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004035 set_fill_boolean(set, 0);
4036 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004037 uint64_t i;
4038
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004039 val = meta->value.canonical;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004040 for (i = 0; args[0]->val.str[i]; ++i) {
4041 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4042 set_fill_boolean(set, 0);
4043 break;
4044 }
4045 }
4046 if (!args[0]->val.str[i]) {
4047 if (!val[i] || (val[i] == '-')) {
4048 set_fill_boolean(set, 1);
4049 } else {
4050 set_fill_boolean(set, 0);
4051 }
4052 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004053 }
4054
4055 return LY_SUCCESS;
4056}
4057
4058/**
4059 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4060 * with the context size.
4061 *
4062 * @param[in] args Array of arguments.
4063 * @param[in] arg_count Count of elements in @p args.
4064 * @param[in,out] set Context and result set at the same time.
4065 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004066 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004067 */
4068static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004069xpath_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 +02004070{
4071 if (options & LYXP_SCNODE_ALL) {
4072 set_scnode_clear_ctx(set);
4073 return LY_SUCCESS;
4074 }
4075
Michal Vasko03ff5a72019-09-11 13:49:33 +02004076 if (set->type != LYXP_SET_NODE_SET) {
4077 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
4078 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004079 } else if (!set->used) {
4080 set_fill_number(set, 0);
4081 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004082 }
4083
4084 set_fill_number(set, set->ctx_size);
4085 return LY_SUCCESS;
4086}
4087
4088/**
4089 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4090 * with the node name without namespace from the argument or the context.
4091 *
4092 * @param[in] args Array of arguments.
4093 * @param[in] arg_count Count of elements in @p args.
4094 * @param[in,out] set Context and result set at the same time.
4095 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004096 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004097 */
4098static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004099xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004100{
4101 struct lyxp_set_node *item;
4102 /* suppress unused variable warning */
4103 (void)options;
4104
4105 if (options & LYXP_SCNODE_ALL) {
4106 set_scnode_clear_ctx(set);
4107 return LY_SUCCESS;
4108 }
4109
4110 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004111 if (args[0]->type != LYXP_SET_NODE_SET) {
4112 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)");
4113 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004114 } else if (!args[0]->used) {
4115 set_fill_string(set, "", 0);
4116 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004117 }
4118
4119 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004120 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004121
4122 item = &args[0]->val.nodes[0];
4123 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004124 if (set->type != LYXP_SET_NODE_SET) {
4125 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
4126 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004127 } else if (!set->used) {
4128 set_fill_string(set, "", 0);
4129 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004130 }
4131
4132 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004133 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004134
4135 item = &set->val.nodes[0];
4136 }
4137
4138 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004139 case LYXP_NODE_NONE:
4140 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004141 case LYXP_NODE_ROOT:
4142 case LYXP_NODE_ROOT_CONFIG:
4143 case LYXP_NODE_TEXT:
4144 set_fill_string(set, "", 0);
4145 break;
4146 case LYXP_NODE_ELEM:
4147 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4148 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004149 case LYXP_NODE_META:
4150 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004151 break;
4152 }
4153
4154 return LY_SUCCESS;
4155}
4156
4157/**
4158 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4159 * with the node name fully qualified (with namespace) from the argument or the context.
4160 *
4161 * @param[in] args Array of arguments.
4162 * @param[in] arg_count Count of elements in @p args.
4163 * @param[in,out] set Context and result set at the same time.
4164 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004165 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004166 */
4167static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004168xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004169{
4170 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004171 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004172 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004173 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004174
4175 if (options & LYXP_SCNODE_ALL) {
4176 set_scnode_clear_ctx(set);
4177 return LY_SUCCESS;
4178 }
4179
4180 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004181 if (args[0]->type != LYXP_SET_NODE_SET) {
4182 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
4183 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004184 } else if (!args[0]->used) {
4185 set_fill_string(set, "", 0);
4186 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004187 }
4188
4189 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004190 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004191
4192 item = &args[0]->val.nodes[0];
4193 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004194 if (set->type != LYXP_SET_NODE_SET) {
4195 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
4196 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004197 } else if (!set->used) {
4198 set_fill_string(set, "", 0);
4199 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004200 }
4201
4202 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004203 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004204
4205 item = &set->val.nodes[0];
4206 }
4207
4208 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004209 case LYXP_NODE_NONE:
4210 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004211 case LYXP_NODE_ROOT:
4212 case LYXP_NODE_ROOT_CONFIG:
4213 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004214 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004215 break;
4216 case LYXP_NODE_ELEM:
4217 mod = item->node->schema->module;
4218 name = item->node->schema->name;
4219 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004220 case LYXP_NODE_META:
4221 mod = ((struct lyd_meta *)item->node)->annotation->module;
4222 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004223 break;
4224 }
4225
4226 if (mod && name) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004227 int rc = -1;
4228
Michal Vasko03ff5a72019-09-11 13:49:33 +02004229 switch (set->format) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02004230 case LY_PREF_SCHEMA:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004231 rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name);
4232 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02004233 case LY_PREF_JSON:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004234 rc = asprintf(&str, "%s:%s", mod->name, name);
4235 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02004236 case LY_PREF_XML:
Michal Vasko9409ef62019-09-12 11:47:17 +02004237 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004238 }
4239 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4240 set_fill_string(set, str, strlen(str));
4241 free(str);
4242 } else {
4243 set_fill_string(set, "", 0);
4244 }
4245
4246 return LY_SUCCESS;
4247}
4248
4249/**
4250 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4251 * with the namespace of the node from the argument or the context.
4252 *
4253 * @param[in] args Array of arguments.
4254 * @param[in] arg_count Count of elements in @p args.
4255 * @param[in,out] set Context and result set at the same time.
4256 * @param[in] options XPath options.
4257 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4258 */
4259static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004260xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004261{
4262 struct lyxp_set_node *item;
4263 struct lys_module *mod;
4264 /* suppress unused variable warning */
4265 (void)options;
4266
4267 if (options & LYXP_SCNODE_ALL) {
4268 set_scnode_clear_ctx(set);
4269 return LY_SUCCESS;
4270 }
4271
4272 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004273 if (args[0]->type != LYXP_SET_NODE_SET) {
4274 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
4275 "namespace-uri(node-set?)");
4276 return LY_EVALID;
4277 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004278 set_fill_string(set, "", 0);
4279 return LY_SUCCESS;
4280 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004281
4282 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004283 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004284
4285 item = &args[0]->val.nodes[0];
4286 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004287 if (set->type != LYXP_SET_NODE_SET) {
4288 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
4289 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004290 } else if (!set->used) {
4291 set_fill_string(set, "", 0);
4292 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004293 }
4294
4295 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004296 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004297
4298 item = &set->val.nodes[0];
4299 }
4300
4301 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004302 case LYXP_NODE_NONE:
4303 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004304 case LYXP_NODE_ROOT:
4305 case LYXP_NODE_ROOT_CONFIG:
4306 case LYXP_NODE_TEXT:
4307 set_fill_string(set, "", 0);
4308 break;
4309 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004310 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004311 if (item->type == LYXP_NODE_ELEM) {
4312 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004313 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004314 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004315 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004316 }
4317
4318 set_fill_string(set, mod->ns, strlen(mod->ns));
4319 break;
4320 }
4321
4322 return LY_SUCCESS;
4323}
4324
4325/**
4326 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4327 * with only nodes from the context. In practice it either leaves the context
4328 * as it is or returns an empty node set.
4329 *
4330 * @param[in] args Array of arguments.
4331 * @param[in] arg_count Count of elements in @p args.
4332 * @param[in,out] set Context and result set at the same time.
4333 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004334 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004335 */
4336static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004337xpath_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 +02004338{
4339 if (options & LYXP_SCNODE_ALL) {
4340 set_scnode_clear_ctx(set);
4341 return LY_SUCCESS;
4342 }
4343
4344 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004345 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004346 }
4347 return LY_SUCCESS;
4348}
4349
4350/**
4351 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4352 * with normalized value (no leading, trailing, double white spaces) of the node
4353 * from the argument or the context.
4354 *
4355 * @param[in] args Array of arguments.
4356 * @param[in] arg_count Count of elements in @p args.
4357 * @param[in,out] set Context and result set at the same time.
4358 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004359 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004360 */
4361static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004362xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004363{
4364 uint16_t i, new_used;
4365 char *new;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004366 uint8_t have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004367 struct lysc_node_leaf *sleaf;
4368 LY_ERR rc = LY_SUCCESS;
4369
4370 if (options & LYXP_SCNODE_ALL) {
4371 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4372 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4373 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 +02004374 } else if (!warn_is_string_type(sleaf->type)) {
4375 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004376 }
4377 }
4378 set_scnode_clear_ctx(set);
4379 return rc;
4380 }
4381
4382 if (arg_count) {
4383 set_fill_set(set, args[0]);
4384 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004385 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004386 LY_CHECK_RET(rc);
4387
4388 /* is there any normalization necessary? */
4389 for (i = 0; set->val.str[i]; ++i) {
4390 if (is_xmlws(set->val.str[i])) {
4391 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4392 have_spaces = 1;
4393 break;
4394 }
4395 space_before = 1;
4396 } else {
4397 space_before = 0;
4398 }
4399 }
4400
4401 /* yep, there is */
4402 if (have_spaces) {
4403 /* it's enough, at least one character will go, makes space for ending '\0' */
4404 new = malloc(strlen(set->val.str) * sizeof(char));
4405 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4406 new_used = 0;
4407
4408 space_before = 0;
4409 for (i = 0; set->val.str[i]; ++i) {
4410 if (is_xmlws(set->val.str[i])) {
4411 if ((i == 0) || space_before) {
4412 space_before = 1;
4413 continue;
4414 } else {
4415 space_before = 1;
4416 }
4417 } else {
4418 space_before = 0;
4419 }
4420
4421 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4422 ++new_used;
4423 }
4424
4425 /* at worst there is one trailing space now */
4426 if (new_used && is_xmlws(new[new_used - 1])) {
4427 --new_used;
4428 }
4429
4430 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4431 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4432 new[new_used] = '\0';
4433
4434 free(set->val.str);
4435 set->val.str = new;
4436 }
4437
4438 return LY_SUCCESS;
4439}
4440
4441/**
4442 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4443 * with the argument converted to boolean and logically inverted.
4444 *
4445 * @param[in] args Array of arguments.
4446 * @param[in] arg_count Count of elements in @p args.
4447 * @param[in,out] set Context and result set at the same time.
4448 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004449 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004450 */
4451static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004452xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004453{
4454 if (options & LYXP_SCNODE_ALL) {
4455 set_scnode_clear_ctx(set);
4456 return LY_SUCCESS;
4457 }
4458
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004459 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004460 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004461 set_fill_boolean(set, 0);
4462 } else {
4463 set_fill_boolean(set, 1);
4464 }
4465
4466 return LY_SUCCESS;
4467}
4468
4469/**
4470 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4471 * with the number representation of either the argument or the context.
4472 *
4473 * @param[in] args Array of arguments.
4474 * @param[in] arg_count Count of elements in @p args.
4475 * @param[in,out] set Context and result set at the same time.
4476 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004477 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004478 */
4479static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004480xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004481{
4482 LY_ERR rc;
4483
4484 if (options & LYXP_SCNODE_ALL) {
4485 set_scnode_clear_ctx(set);
4486 return LY_SUCCESS;
4487 }
4488
4489 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004490 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004491 LY_CHECK_RET(rc);
4492 set_fill_set(set, args[0]);
4493 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004494 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004495 LY_CHECK_RET(rc);
4496 }
4497
4498 return LY_SUCCESS;
4499}
4500
4501/**
4502 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4503 * with the context position.
4504 *
4505 * @param[in] args Array of arguments.
4506 * @param[in] arg_count Count of elements in @p args.
4507 * @param[in,out] set Context and result set at the same time.
4508 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004509 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004510 */
4511static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004512xpath_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 +02004513{
4514 if (options & LYXP_SCNODE_ALL) {
4515 set_scnode_clear_ctx(set);
4516 return LY_SUCCESS;
4517 }
4518
Michal Vasko03ff5a72019-09-11 13:49:33 +02004519 if (set->type != LYXP_SET_NODE_SET) {
4520 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
4521 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004522 } else if (!set->used) {
4523 set_fill_number(set, 0);
4524 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004525 }
4526
4527 set_fill_number(set, set->ctx_pos);
4528
4529 /* UNUSED in 'Release' build type */
4530 (void)options;
4531 return LY_SUCCESS;
4532}
4533
4534/**
4535 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4536 * depending on whether the second argument regex matches the first argument string. For details refer to
4537 * YANG 1.1 RFC section 10.2.1.
4538 *
4539 * @param[in] args Array of arguments.
4540 * @param[in] arg_count Count of elements in @p args.
4541 * @param[in,out] set Context and result set at the same time.
4542 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004543 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004544 */
4545static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004546xpath_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 +02004547{
4548 struct lysc_pattern **patterns = NULL, **pattern;
4549 struct lysc_node_leaf *sleaf;
4550 char *path;
4551 LY_ERR rc = LY_SUCCESS;
4552 struct ly_err_item *err;
4553
4554 if (options & LYXP_SCNODE_ALL) {
4555 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4556 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4557 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 +02004558 } else if (!warn_is_string_type(sleaf->type)) {
4559 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004560 }
4561 }
4562
4563 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4564 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4565 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 +02004566 } else if (!warn_is_string_type(sleaf->type)) {
4567 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004568 }
4569 }
4570 set_scnode_clear_ctx(set);
4571 return rc;
4572 }
4573
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004574 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004575 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004576 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004577 LY_CHECK_RET(rc);
4578
4579 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4580 *pattern = malloc(sizeof **pattern);
4581 path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0);
4582 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4583 free(path);
4584 if (rc != LY_SUCCESS) {
4585 LY_ARRAY_FREE(patterns);
4586 return rc;
4587 }
4588
4589 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4590 pcre2_code_free((*pattern)->code);
4591 free(*pattern);
4592 LY_ARRAY_FREE(patterns);
4593 if (rc && (rc != LY_EVALID)) {
4594 ly_err_print(err);
4595 ly_err_free(err);
4596 return rc;
4597 }
4598
4599 if (rc == LY_EVALID) {
4600 ly_err_free(err);
4601 set_fill_boolean(set, 0);
4602 } else {
4603 set_fill_boolean(set, 1);
4604 }
4605
4606 return LY_SUCCESS;
4607}
4608
4609/**
4610 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4611 * with the rounded first argument. For details refer to
4612 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4613 *
4614 * @param[in] args Array of arguments.
4615 * @param[in] arg_count Count of elements in @p args.
4616 * @param[in,out] set Context and result set at the same time.
4617 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004618 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004619 */
4620static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004621xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004622{
4623 struct lysc_node_leaf *sleaf;
4624 LY_ERR rc = LY_SUCCESS;
4625
4626 if (options & LYXP_SCNODE_ALL) {
4627 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4628 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004629 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4630 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 +02004631 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4632 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004633 }
4634 set_scnode_clear_ctx(set);
4635 return rc;
4636 }
4637
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004638 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004639 LY_CHECK_RET(rc);
4640
4641 /* cover only the cases where floor can't be used */
4642 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4643 set_fill_number(set, -0.0f);
4644 } else {
4645 args[0]->val.num += 0.5;
4646 rc = xpath_floor(args, 1, args[0], options);
4647 LY_CHECK_RET(rc);
4648 set_fill_number(set, args[0]->val.num);
4649 }
4650
4651 return LY_SUCCESS;
4652}
4653
4654/**
4655 * @brief Execute the XPath starts-with(string, string) function.
4656 * Returns LYXP_SET_BOOLEAN whether the second argument is
4657 * the prefix of the first or not.
4658 *
4659 * @param[in] args Array of arguments.
4660 * @param[in] arg_count Count of elements in @p args.
4661 * @param[in,out] set Context and result set at the same time.
4662 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004663 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004664 */
4665static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004666xpath_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 +02004667{
4668 struct lysc_node_leaf *sleaf;
4669 LY_ERR rc = LY_SUCCESS;
4670
4671 if (options & LYXP_SCNODE_ALL) {
4672 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4673 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4674 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 +02004675 } else if (!warn_is_string_type(sleaf->type)) {
4676 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004677 }
4678 }
4679
4680 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4681 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4682 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 +02004683 } else if (!warn_is_string_type(sleaf->type)) {
4684 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004685 }
4686 }
4687 set_scnode_clear_ctx(set);
4688 return rc;
4689 }
4690
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004691 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004692 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004693 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004694 LY_CHECK_RET(rc);
4695
4696 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4697 set_fill_boolean(set, 0);
4698 } else {
4699 set_fill_boolean(set, 1);
4700 }
4701
4702 return LY_SUCCESS;
4703}
4704
4705/**
4706 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4707 * with the string representation of either the argument or the context.
4708 *
4709 * @param[in] args Array of arguments.
4710 * @param[in] arg_count Count of elements in @p args.
4711 * @param[in,out] set Context and result set at the same time.
4712 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004713 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004714 */
4715static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004716xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004717{
4718 LY_ERR rc;
4719
4720 if (options & LYXP_SCNODE_ALL) {
4721 set_scnode_clear_ctx(set);
4722 return LY_SUCCESS;
4723 }
4724
4725 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004726 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004727 LY_CHECK_RET(rc);
4728 set_fill_set(set, args[0]);
4729 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004730 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004731 LY_CHECK_RET(rc);
4732 }
4733
4734 return LY_SUCCESS;
4735}
4736
4737/**
4738 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4739 * with the length of the string in either the argument or the context.
4740 *
4741 * @param[in] args Array of arguments.
4742 * @param[in] arg_count Count of elements in @p args.
4743 * @param[in,out] set Context and result set at the same time.
4744 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004745 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004746 */
4747static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004748xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004749{
4750 struct lysc_node_leaf *sleaf;
4751 LY_ERR rc = LY_SUCCESS;
4752
4753 if (options & LYXP_SCNODE_ALL) {
4754 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4755 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4756 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 +02004757 } else if (!warn_is_string_type(sleaf->type)) {
4758 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004759 }
4760 }
4761 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4762 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4763 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 +02004764 } else if (!warn_is_string_type(sleaf->type)) {
4765 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004766 }
4767 }
4768 set_scnode_clear_ctx(set);
4769 return rc;
4770 }
4771
4772 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004773 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004774 LY_CHECK_RET(rc);
4775 set_fill_number(set, strlen(args[0]->val.str));
4776 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004777 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004778 LY_CHECK_RET(rc);
4779 set_fill_number(set, strlen(set->val.str));
4780 }
4781
4782 return LY_SUCCESS;
4783}
4784
4785/**
4786 * @brief Execute the XPath substring(string, number, number?) function.
4787 * Returns LYXP_SET_STRING substring of the first argument starting
4788 * on the second argument index ending on the third argument index,
4789 * indexed from 1. For exact definition refer to
4790 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4791 *
4792 * @param[in] args Array of arguments.
4793 * @param[in] arg_count Count of elements in @p args.
4794 * @param[in,out] set Context and result set at the same time.
4795 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004796 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004797 */
4798static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004799xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004800{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004801 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004802 uint16_t str_start, str_len, pos;
4803 struct lysc_node_leaf *sleaf;
4804 LY_ERR rc = LY_SUCCESS;
4805
4806 if (options & LYXP_SCNODE_ALL) {
4807 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4808 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4809 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 +02004810 } else if (!warn_is_string_type(sleaf->type)) {
4811 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004812 }
4813 }
4814
4815 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4816 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4817 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 +02004818 } else if (!warn_is_numeric_type(sleaf->type)) {
4819 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004820 }
4821 }
4822
4823 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET)
Radek Krejci0f969882020-08-21 16:56:47 +02004824 && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004825 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4826 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 +02004827 } else if (!warn_is_numeric_type(sleaf->type)) {
4828 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004829 }
4830 }
4831 set_scnode_clear_ctx(set);
4832 return rc;
4833 }
4834
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004835 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004836 LY_CHECK_RET(rc);
4837
4838 /* start */
4839 if (xpath_round(&args[1], 1, args[1], options)) {
4840 return -1;
4841 }
4842 if (isfinite(args[1]->val.num)) {
4843 start = args[1]->val.num - 1;
4844 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004845 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004846 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004847 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004848 }
4849
4850 /* len */
4851 if (arg_count == 3) {
4852 rc = xpath_round(&args[2], 1, args[2], options);
4853 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004854 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004855 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004856 } else if (isfinite(args[2]->val.num)) {
4857 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004858 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004859 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004860 }
4861 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004862 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004863 }
4864
4865 /* find matching character positions */
4866 str_start = 0;
4867 str_len = 0;
4868 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4869 if (pos < start) {
4870 ++str_start;
4871 } else if (pos < start + len) {
4872 ++str_len;
4873 } else {
4874 break;
4875 }
4876 }
4877
4878 set_fill_string(set, args[0]->val.str + str_start, str_len);
4879 return LY_SUCCESS;
4880}
4881
4882/**
4883 * @brief Execute the XPath substring-after(string, string) function.
4884 * Returns LYXP_SET_STRING with the string succeeding the occurance
4885 * of the second argument in the first or an empty string.
4886 *
4887 * @param[in] args Array of arguments.
4888 * @param[in] arg_count Count of elements in @p args.
4889 * @param[in,out] set Context and result set at the same time.
4890 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004891 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004892 */
4893static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004894xpath_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 +02004895{
4896 char *ptr;
4897 struct lysc_node_leaf *sleaf;
4898 LY_ERR rc = LY_SUCCESS;
4899
4900 if (options & LYXP_SCNODE_ALL) {
4901 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4902 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4903 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 +02004904 } else if (!warn_is_string_type(sleaf->type)) {
4905 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004906 }
4907 }
4908
4909 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4910 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4911 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 +02004912 } else if (!warn_is_string_type(sleaf->type)) {
4913 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004914 }
4915 }
4916 set_scnode_clear_ctx(set);
4917 return rc;
4918 }
4919
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004920 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004921 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004922 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004923 LY_CHECK_RET(rc);
4924
4925 ptr = strstr(args[0]->val.str, args[1]->val.str);
4926 if (ptr) {
4927 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4928 } else {
4929 set_fill_string(set, "", 0);
4930 }
4931
4932 return LY_SUCCESS;
4933}
4934
4935/**
4936 * @brief Execute the XPath substring-before(string, string) function.
4937 * Returns LYXP_SET_STRING with the string preceding the occurance
4938 * of the second argument in the first or an empty string.
4939 *
4940 * @param[in] args Array of arguments.
4941 * @param[in] arg_count Count of elements in @p args.
4942 * @param[in,out] set Context and result set at the same time.
4943 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004944 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004945 */
4946static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004947xpath_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 +02004948{
4949 char *ptr;
4950 struct lysc_node_leaf *sleaf;
4951 LY_ERR rc = LY_SUCCESS;
4952
4953 if (options & LYXP_SCNODE_ALL) {
4954 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4955 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4956 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 +02004957 } else if (!warn_is_string_type(sleaf->type)) {
4958 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004959 }
4960 }
4961
4962 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4963 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4964 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 +02004965 } else if (!warn_is_string_type(sleaf->type)) {
4966 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004967 }
4968 }
4969 set_scnode_clear_ctx(set);
4970 return rc;
4971 }
4972
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004973 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004974 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004975 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004976 LY_CHECK_RET(rc);
4977
4978 ptr = strstr(args[0]->val.str, args[1]->val.str);
4979 if (ptr) {
4980 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4981 } else {
4982 set_fill_string(set, "", 0);
4983 }
4984
4985 return LY_SUCCESS;
4986}
4987
4988/**
4989 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
4990 * with the sum of all the nodes in the context.
4991 *
4992 * @param[in] args Array of arguments.
4993 * @param[in] arg_count Count of elements in @p args.
4994 * @param[in,out] set Context and result set at the same time.
4995 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004996 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004997 */
4998static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004999xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005000{
5001 long double num;
5002 char *str;
5003 uint16_t i;
5004 struct lyxp_set set_item;
5005 struct lysc_node_leaf *sleaf;
5006 LY_ERR rc = LY_SUCCESS;
5007
5008 if (options & LYXP_SCNODE_ALL) {
5009 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5010 for (i = 0; i < args[0]->used; ++i) {
5011 if (args[0]->val.scnodes[i].in_ctx == 1) {
5012 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5013 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5014 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
5015 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005016 } else if (!warn_is_numeric_type(sleaf->type)) {
5017 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005018 }
5019 }
5020 }
5021 }
5022 set_scnode_clear_ctx(set);
5023 return rc;
5024 }
5025
5026 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005027
5028 if (args[0]->type != LYXP_SET_NODE_SET) {
5029 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
5030 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005031 } else if (!args[0]->used) {
5032 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005033 }
5034
Michal Vasko5c4e5892019-11-14 12:31:38 +01005035 set_init(&set_item, set);
5036
Michal Vasko03ff5a72019-09-11 13:49:33 +02005037 set_item.type = LYXP_SET_NODE_SET;
5038 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5039 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5040
5041 set_item.used = 1;
5042 set_item.size = 1;
5043
5044 for (i = 0; i < args[0]->used; ++i) {
5045 set_item.val.nodes[0] = args[0]->val.nodes[i];
5046
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005047 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005048 LY_CHECK_RET(rc);
5049 num = cast_string_to_number(str);
5050 free(str);
5051 set->val.num += num;
5052 }
5053
5054 free(set_item.val.nodes);
5055
5056 return LY_SUCCESS;
5057}
5058
5059/**
5060 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5061 * with the text content of the nodes in the context.
5062 *
5063 * @param[in] args Array of arguments.
5064 * @param[in] arg_count Count of elements in @p args.
5065 * @param[in,out] set Context and result set at the same time.
5066 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005067 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005068 */
5069static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005070xpath_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 +02005071{
5072 uint32_t i;
5073
5074 if (options & LYXP_SCNODE_ALL) {
5075 set_scnode_clear_ctx(set);
5076 return LY_SUCCESS;
5077 }
5078
Michal Vasko03ff5a72019-09-11 13:49:33 +02005079 if (set->type != LYXP_SET_NODE_SET) {
5080 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5081 return LY_EVALID;
5082 }
5083
Michal Vaskod989ba02020-08-24 10:59:24 +02005084 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005085 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005086 case LYXP_NODE_NONE:
5087 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005088 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005089 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5090 set->val.nodes[i].type = LYXP_NODE_TEXT;
5091 ++i;
5092 break;
5093 }
Radek Krejci0f969882020-08-21 16:56:47 +02005094 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005095 case LYXP_NODE_ROOT:
5096 case LYXP_NODE_ROOT_CONFIG:
5097 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005098 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005099 set_remove_node(set, i);
5100 break;
5101 }
5102 }
5103
5104 return LY_SUCCESS;
5105}
5106
5107/**
5108 * @brief Execute the XPath translate(string, string, string) function.
5109 * Returns LYXP_SET_STRING with the first argument with the characters
5110 * from the second argument replaced by those on the corresponding
5111 * positions in the third argument.
5112 *
5113 * @param[in] args Array of arguments.
5114 * @param[in] arg_count Count of elements in @p args.
5115 * @param[in,out] set Context and result set at the same time.
5116 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005117 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005118 */
5119static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005120xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005121{
5122 uint16_t i, j, new_used;
5123 char *new;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005124 uint8_t have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005125 struct lysc_node_leaf *sleaf;
5126 LY_ERR rc = LY_SUCCESS;
5127
5128 if (options & LYXP_SCNODE_ALL) {
5129 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5130 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5131 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 +02005132 } else if (!warn_is_string_type(sleaf->type)) {
5133 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005134 }
5135 }
5136
5137 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5138 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5139 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 +02005140 } else if (!warn_is_string_type(sleaf->type)) {
5141 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005142 }
5143 }
5144
5145 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5146 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5147 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 +02005148 } else if (!warn_is_string_type(sleaf->type)) {
5149 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005150 }
5151 }
5152 set_scnode_clear_ctx(set);
5153 return rc;
5154 }
5155
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005156 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005157 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005158 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005159 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005160 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005161 LY_CHECK_RET(rc);
5162
5163 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5164 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5165 new_used = 0;
5166
5167 have_removed = 0;
5168 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005169 uint8_t found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005170
5171 for (j = 0; args[1]->val.str[j]; ++j) {
5172 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5173 /* removing this char */
5174 if (j >= strlen(args[2]->val.str)) {
5175 have_removed = 1;
5176 found = 1;
5177 break;
5178 }
5179 /* replacing this char */
5180 new[new_used] = args[2]->val.str[j];
5181 ++new_used;
5182 found = 1;
5183 break;
5184 }
5185 }
5186
5187 /* copying this char */
5188 if (!found) {
5189 new[new_used] = args[0]->val.str[i];
5190 ++new_used;
5191 }
5192 }
5193
5194 if (have_removed) {
5195 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5196 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5197 }
5198 new[new_used] = '\0';
5199
Michal Vaskod3678892020-05-21 10:06:58 +02005200 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005201 set->type = LYXP_SET_STRING;
5202 set->val.str = new;
5203
5204 return LY_SUCCESS;
5205}
5206
5207/**
5208 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5209 * with true value.
5210 *
5211 * @param[in] args Array of arguments.
5212 * @param[in] arg_count Count of elements in @p args.
5213 * @param[in,out] set Context and result set at the same time.
5214 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005215 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005216 */
5217static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005218xpath_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 +02005219{
5220 if (options & LYXP_SCNODE_ALL) {
5221 set_scnode_clear_ctx(set);
5222 return LY_SUCCESS;
5223 }
5224
5225 set_fill_boolean(set, 1);
5226 return LY_SUCCESS;
5227}
5228
5229/*
5230 * moveto functions
5231 *
5232 * They and only they actually change the context (set).
5233 */
5234
5235/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005236 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005237 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005238 * XPath @p set is expected to be a (sc)node set!
5239 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005240 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5241 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
5242 * @param[in] set Set with XPath context.
5243 * @param[out] moveto_mod Expected module of a matching node.
5244 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005245 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005246static LY_ERR
5247moveto_resolve_model(const char **qname, uint16_t *qname_len, struct lyxp_set *set, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005248{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005249 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005250 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005251 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005252
Michal Vasko2104e9f2020-03-06 08:23:25 +01005253 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5254
Michal Vasko6346ece2019-09-24 13:12:53 +02005255 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5256 /* specific module */
5257 pref_len = ptr - *qname;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02005258 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, (void *)set->local_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005259
Michal Vasko004d3152020-06-11 19:59:22 +02005260 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005261 if (!mod || !mod->implemented) {
Michal Vasko2104e9f2020-03-06 08:23:25 +01005262 if (set->type == LYXP_SET_SCNODE_SET) {
5263 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname);
5264 } else {
5265 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname);
5266 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005267 return LY_EVALID;
5268 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005269
Michal Vasko6346ece2019-09-24 13:12:53 +02005270 *qname += pref_len + 1;
5271 *qname_len -= pref_len + 1;
5272 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5273 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005274 mod = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02005275 } else if (set->type == LYXP_SET_SCNODE_SET) {
5276 /* current node module */
5277 mod = set->ctx_scnode->module;
Michal Vasko6346ece2019-09-24 13:12:53 +02005278 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005279 /* current node module */
5280 mod = set->ctx_node->schema->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005281 }
5282
Michal Vasko6346ece2019-09-24 13:12:53 +02005283 *moveto_mod = mod;
5284 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005285}
5286
5287/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005288 * @brief Move context @p set to the root. Handles absolute path.
5289 * Result is LYXP_SET_NODE_SET.
5290 *
5291 * @param[in,out] set Set to use.
5292 * @param[in] options Xpath options.
5293 */
5294static void
Radek Krejci1deb5be2020-08-26 16:43:36 +02005295moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005296{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005297 if (!set) {
5298 return;
5299 }
5300
5301 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005302 set_scnode_clear_ctx(set);
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005303 lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005304 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005305 set->type = LYXP_SET_NODE_SET;
5306 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005307 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005308 }
5309}
5310
5311/**
Michal Vaskoa1424542019-11-14 16:08:52 +01005312 * @brief Check whether a node has some unresolved "when".
5313 *
5314 * @param[in] node Node to check.
5315 * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when")
5316 */
5317static LY_ERR
5318moveto_when_check(const struct lyd_node *node)
5319{
5320 const struct lysc_node *schema;
5321
5322 if (!node) {
5323 return LY_SUCCESS;
5324 }
5325
5326 schema = node->schema;
5327 do {
5328 if (schema->when && !(node->flags & LYD_WHEN_TRUE)) {
5329 return LY_EINCOMPLETE;
5330 }
5331 schema = schema->parent;
5332 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
5333
5334 return LY_SUCCESS;
5335}
5336
5337/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005338 * @brief Check @p node as a part of NameTest processing.
5339 *
5340 * @param[in] node Node to check.
5341 * @param[in] root_type XPath root node type.
Michal Vasko6b26e742020-07-17 15:02:10 +02005342 * @param[in] context_op XPath operation parent.
Michal Vaskod3678892020-05-21 10:06:58 +02005343 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
5344 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005345 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5346 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005347 */
5348static LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +02005349moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const struct lysc_node *context_op,
Radek Krejci0f969882020-08-21 16:56:47 +02005350 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005351{
5352 /* module check */
5353 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005354 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005355 }
5356
Michal Vasko5c4e5892019-11-14 12:31:38 +01005357 /* context check */
5358 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005359 return LY_EINVAL;
Michal Vasko6b26e742020-07-17 15:02:10 +02005360 } else if (context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node->schema != context_op)) {
5361 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005362 }
5363
5364 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005365 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005366 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005367 }
5368
Michal Vaskoa1424542019-11-14 16:08:52 +01005369 /* when check */
5370 if (moveto_when_check(node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005371 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005372 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005373
5374 /* match */
5375 return LY_SUCCESS;
5376}
5377
5378/**
5379 * @brief Check @p node as a part of schema NameTest processing.
5380 *
5381 * @param[in] node Schema node to check.
5382 * @param[in] root_type XPath root node type.
Michal Vasko6b26e742020-07-17 15:02:10 +02005383 * @param[in] context_op XPath operation parent.
Michal Vaskod3678892020-05-21 10:06:58 +02005384 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
5385 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005386 * @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 +02005387 */
5388static LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +02005389moveto_scnode_check(const struct lysc_node *node, enum lyxp_node_type root_type, const struct lysc_node *context_op,
Radek Krejci0f969882020-08-21 16:56:47 +02005390 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005391{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005392 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005393 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005394 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005395 }
5396
5397 /* context check */
5398 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
5399 return LY_EINVAL;
Michal Vasko6b26e742020-07-17 15:02:10 +02005400 } else if (context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != context_op)) {
5401 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005402 }
5403
5404 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005405 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005406 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005407 }
5408
5409 /* match */
5410 return LY_SUCCESS;
5411}
5412
5413/**
Michal Vaskod3678892020-05-21 10:06:58 +02005414 * @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 +02005415 *
5416 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005417 * @param[in] mod Matching node module, NULL for any.
5418 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005419 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5420 */
5421static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005422moveto_node(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005423{
Michal Vaskof03ed032020-03-04 13:31:44 +01005424 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005425 const struct lyd_node *siblings, *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005426 LY_ERR rc;
5427
Michal Vaskod3678892020-05-21 10:06:58 +02005428 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005429 return LY_SUCCESS;
5430 }
5431
5432 if (set->type != LYXP_SET_NODE_SET) {
5433 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5434 return LY_EVALID;
5435 }
5436
Michal Vasko03ff5a72019-09-11 13:49:33 +02005437 for (i = 0; i < set->used; ) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005438 uint8_t replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005439
5440 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 +01005441 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005442
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005443 /* search in all the trees */
Michal Vaskod3678892020-05-21 10:06:58 +02005444 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005445 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005446 /* search in children */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005447 siblings = lyd_node_children(set->val.nodes[i].node, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02005448 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005449
Michal Vaskod3678892020-05-21 10:06:58 +02005450 for (sub = siblings; sub; sub = sub->next) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005451 rc = moveto_node_check(sub, set->root_type, set->context_op, ncname, mod);
Michal Vaskod3678892020-05-21 10:06:58 +02005452 if (rc == LY_SUCCESS) {
5453 if (!replaced) {
5454 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5455 replaced = 1;
5456 } else {
5457 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005458 }
Michal Vaskod3678892020-05-21 10:06:58 +02005459 ++i;
5460 } else if (rc == LY_EINCOMPLETE) {
5461 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005462 }
5463 }
5464
5465 if (!replaced) {
5466 /* no match */
5467 set_remove_node(set, i);
5468 }
5469 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005470
5471 return LY_SUCCESS;
5472}
5473
5474/**
Michal Vaskod3678892020-05-21 10:06:58 +02005475 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5476 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005477 *
5478 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005479 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005480 * @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 +02005481 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5482 */
5483static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02005484moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates)
Michal Vaskod3678892020-05-21 10:06:58 +02005485{
Michal Vasko004d3152020-06-11 19:59:22 +02005486 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005487 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005488 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005489 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005490
Michal Vasko004d3152020-06-11 19:59:22 +02005491 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005492
5493 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005494 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005495 }
5496
5497 if (set->type != LYXP_SET_NODE_SET) {
5498 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005499 ret = LY_EVALID;
5500 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005501 }
5502
5503 /* context check for all the nodes since we have the schema node */
5504 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5505 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005506 goto cleanup;
Radek Krejci0f969882020-08-21 16:56:47 +02005507 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko6b26e742020-07-17 15:02:10 +02005508 && (scnode != set->context_op)) {
5509 lyxp_set_free_content(set);
5510 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005511 }
5512
5513 /* create specific data instance if needed */
5514 if (scnode->nodetype == LYS_LIST) {
5515 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5516 } else if (scnode->nodetype == LYS_LEAFLIST) {
5517 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005518 }
5519
5520 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005521 siblings = NULL;
5522
5523 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5524 assert(!set->val.nodes[i].node);
5525
5526 /* search in all the trees */
5527 siblings = set->tree;
5528 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5529 /* search in children */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005530 siblings = lyd_node_children(set->val.nodes[i].node, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02005531 }
5532
5533 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005534 if (inst) {
5535 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005536 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005537 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005538 }
5539
5540 /* when check */
5541 if (sub && moveto_when_check(sub)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005542 ret = LY_EINCOMPLETE;
5543 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005544 }
5545
5546 if (sub) {
5547 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005548 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005549 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005550 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005551 /* no match */
5552 set_remove_node(set, i);
5553 }
5554 }
5555
Michal Vasko004d3152020-06-11 19:59:22 +02005556cleanup:
5557 lyd_free_tree(inst);
5558 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005559}
5560
5561/**
5562 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5563 *
5564 * @param[in,out] set Set to use.
5565 * @param[in] mod Matching node module, NULL for any.
5566 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005567 * @param[in] options XPath options.
5568 * @return LY_ERR
5569 */
5570static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005571moveto_scnode(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005572{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005573 uint8_t temp_ctx = 0;
5574 uint32_t getnext_opts;
5575 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005576 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005577 const struct lysc_node *iter, *start_parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005578
Michal Vaskod3678892020-05-21 10:06:58 +02005579 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005580 return LY_SUCCESS;
5581 }
5582
5583 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005584 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005585 return LY_EVALID;
5586 }
5587
Michal Vaskocafad9d2019-11-07 15:20:03 +01005588 /* getnext opts */
5589 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
5590 if (options & LYXP_SCNODE_OUTPUT) {
5591 getnext_opts |= LYS_GETNEXT_OUTPUT;
5592 }
5593
Michal Vasko03ff5a72019-09-11 13:49:33 +02005594 orig_used = set->used;
5595 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005596 uint32_t idx;
5597
Michal Vasko03ff5a72019-09-11 13:49:33 +02005598 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005599 if (set->val.scnodes[i].in_ctx != -2) {
5600 continue;
5601 }
5602
5603 /* remember context node */
5604 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005605 } else {
5606 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005607 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005608
5609 start_parent = set->val.scnodes[i].scnode;
5610
5611 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
Michal Vaskod3678892020-05-21 10:06:58 +02005612 /* it can actually be in any module, it's all <running>, but we know it's mod (if set),
Michal Vasko03ff5a72019-09-11 13:49:33 +02005613 * so use it directly (root node itself is useless in this case) */
5614 mod_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02005615 while (mod || (mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005616 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005617 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005618 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005619 if (!moveto_scnode_check(iter, set->root_type, set->context_op, ncname, mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005620 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5621
Michal Vasko03ff5a72019-09-11 13:49:33 +02005622 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005623 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005624 set->val.scnodes[idx].in_ctx = 2;
5625 temp_ctx = 1;
5626 }
5627 }
5628 }
5629
5630 if (!mod_idx) {
Michal Vaskod3678892020-05-21 10:06:58 +02005631 /* mod was specified, we are not going through the whole context */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005632 break;
5633 }
5634 /* next iteration */
Michal Vaskod3678892020-05-21 10:06:58 +02005635 mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005636 }
5637
Michal Vasko519fd602020-05-26 12:17:39 +02005638 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5639 iter = NULL;
5640 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005641 if (!moveto_scnode_check(iter, set->root_type, set->context_op, ncname, (mod ? mod : set->local_mod))) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005642 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5643
5644 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005645 set->val.scnodes[idx].in_ctx = 2;
5646 temp_ctx = 1;
5647 }
5648 }
5649 }
5650 }
5651 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005652
5653 /* correct temporary in_ctx values */
5654 if (temp_ctx) {
5655 for (i = 0; i < orig_used; ++i) {
5656 if (set->val.scnodes[i].in_ctx == 2) {
5657 set->val.scnodes[i].in_ctx = 1;
5658 }
5659 }
5660 }
5661
5662 return LY_SUCCESS;
5663}
5664
5665/**
Michal Vaskod3678892020-05-21 10:06:58 +02005666 * @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 +02005667 * Context position aware.
5668 *
5669 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005670 * @param[in] mod Matching node module, NULL for any.
5671 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005672 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5673 */
5674static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005675moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005676{
5677 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005678 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005679 struct lyxp_set ret_set;
5680 LY_ERR rc;
5681
Michal Vaskod3678892020-05-21 10:06:58 +02005682 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005683 return LY_SUCCESS;
5684 }
5685
5686 if (set->type != LYXP_SET_NODE_SET) {
5687 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5688 return LY_EVALID;
5689 }
5690
Michal Vasko9f96a052020-03-10 09:41:45 +01005691 /* 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 +02005692 rc = moveto_node(set, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005693 LY_CHECK_RET(rc);
5694
Michal Vasko6346ece2019-09-24 13:12:53 +02005695 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005696 set_init(&ret_set, set);
5697 for (i = 0; i < set->used; ++i) {
5698
5699 /* TREE DFS */
5700 start = set->val.nodes[i].node;
5701 for (elem = next = start; elem; elem = next) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005702 rc = moveto_node_check(elem, set->root_type, set->context_op, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005703 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005704 /* add matching node into result set */
5705 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5706 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5707 /* the node is a duplicate, we'll process it later in the set */
5708 goto skip_children;
5709 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005710 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005711 return rc;
5712 } else if (rc == LY_EINVAL) {
5713 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005714 }
5715
5716 /* TREE DFS NEXT ELEM */
5717 /* select element for the next run - children first */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005718 next = lyd_node_children(elem, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005719 if (!next) {
5720skip_children:
5721 /* no children, so try siblings, but only if it's not the start,
5722 * that is considered to be the root and it's siblings are not traversed */
5723 if (elem != start) {
5724 next = elem->next;
5725 } else {
5726 break;
5727 }
5728 }
5729 while (!next) {
5730 /* no siblings, go back through the parents */
5731 if ((struct lyd_node *)elem->parent == start) {
5732 /* we are done, no next element to process */
5733 break;
5734 }
5735 /* parent is already processed, go to its sibling */
5736 elem = (struct lyd_node *)elem->parent;
5737 next = elem->next;
5738 }
5739 }
5740 }
5741
5742 /* make the temporary set the current one */
5743 ret_set.ctx_pos = set->ctx_pos;
5744 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005745 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005746 memcpy(set, &ret_set, sizeof *set);
5747
5748 return LY_SUCCESS;
5749}
5750
5751/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005752 * @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 +02005753 *
5754 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005755 * @param[in] mod Matching node module, NULL for any.
5756 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005757 * @param[in] options XPath options.
5758 * @return LY_ERR
5759 */
5760static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005761moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005762{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005763 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005764 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005765 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005766
Michal Vaskod3678892020-05-21 10:06:58 +02005767 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005768 return LY_SUCCESS;
5769 }
5770
5771 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005772 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005773 return LY_EVALID;
5774 }
5775
Michal Vasko03ff5a72019-09-11 13:49:33 +02005776 orig_used = set->used;
5777 for (i = 0; i < orig_used; ++i) {
5778 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005779 if (set->val.scnodes[i].in_ctx != -2) {
5780 continue;
5781 }
5782
5783 /* remember context node */
5784 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005785 } else {
5786 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005787 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005788
5789 /* TREE DFS */
5790 start = set->val.scnodes[i].scnode;
5791 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005792 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5793 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005794 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005795 }
5796
Michal Vasko6b26e742020-07-17 15:02:10 +02005797 rc = moveto_scnode_check(elem, set->root_type, set->context_op, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005798 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005799 uint32_t idx;
5800
5801 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005802 set->val.scnodes[idx].in_ctx = 1;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005803 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005804 /* we will process it later in the set */
5805 goto skip_children;
5806 }
5807 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005808 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005809 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005810 } else if (rc == LY_EINVAL) {
5811 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005812 }
5813
5814next_iter:
5815 /* TREE DFS NEXT ELEM */
5816 /* select element for the next run - children first */
5817 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005818 if (!next) {
5819skip_children:
5820 /* no children, so try siblings, but only if it's not the start,
5821 * that is considered to be the root and it's siblings are not traversed */
5822 if (elem != start) {
5823 next = elem->next;
5824 } else {
5825 break;
5826 }
5827 }
5828 while (!next) {
5829 /* no siblings, go back through the parents */
5830 if (elem->parent == start) {
5831 /* we are done, no next element to process */
5832 break;
5833 }
5834 /* parent is already processed, go to its sibling */
5835 elem = elem->parent;
5836 next = elem->next;
5837 }
5838 }
5839 }
5840
5841 return LY_SUCCESS;
5842}
5843
5844/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005845 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005846 * Indirectly context position aware.
5847 *
5848 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005849 * @param[in] mod Matching metadata module, NULL for any.
5850 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005851 * @return LY_ERR
5852 */
5853static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005854moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005855{
Michal Vasko9f96a052020-03-10 09:41:45 +01005856 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005857
Michal Vaskod3678892020-05-21 10:06:58 +02005858 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005859 return LY_SUCCESS;
5860 }
5861
5862 if (set->type != LYXP_SET_NODE_SET) {
5863 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5864 return LY_EVALID;
5865 }
5866
Radek Krejci1deb5be2020-08-26 16:43:36 +02005867 for (uint32_t i = 0; i < set->used; ) {
5868 uint8_t replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005869
5870 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5871 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005872 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005873 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005874
5875 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005876 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005877 continue;
5878 }
5879
Michal Vaskod3678892020-05-21 10:06:58 +02005880 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005881 /* match */
5882 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005883 set->val.meta[i].meta = sub;
5884 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005885 /* pos does not change */
5886 replaced = 1;
5887 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005888 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 +02005889 }
5890 ++i;
5891 }
5892 }
5893 }
5894
5895 if (!replaced) {
5896 /* no match */
5897 set_remove_node(set, i);
5898 }
5899 }
5900
5901 return LY_SUCCESS;
5902}
5903
5904/**
5905 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005906 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005907 *
5908 * @param[in,out] set1 Set to use for the result.
5909 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005910 * @return LY_ERR
5911 */
5912static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005913moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005914{
5915 LY_ERR rc;
5916
Michal Vaskod3678892020-05-21 10:06:58 +02005917 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005918 LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
5919 return LY_EVALID;
5920 }
5921
5922 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005923 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005924 return LY_SUCCESS;
5925 }
5926
Michal Vaskod3678892020-05-21 10:06:58 +02005927 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005928 memcpy(set1, set2, sizeof *set1);
5929 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005930 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005931 return LY_SUCCESS;
5932 }
5933
5934 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005935 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005936
5937 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005938 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005939 LY_CHECK_RET(rc);
5940
5941 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005942 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005943
5944 return LY_SUCCESS;
5945}
5946
5947/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005948 * @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 +02005949 * Context position aware.
5950 *
5951 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005952 * @param[in] mod Matching metadata module, NULL for any.
5953 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005954 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5955 */
5956static int
Michal Vaskod3678892020-05-21 10:06:58 +02005957moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005958{
Michal Vasko9f96a052020-03-10 09:41:45 +01005959 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005960 struct lyxp_set *set_all_desc = NULL;
5961 LY_ERR rc;
5962
Michal Vaskod3678892020-05-21 10:06:58 +02005963 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005964 return LY_SUCCESS;
5965 }
5966
5967 if (set->type != LYXP_SET_NODE_SET) {
5968 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5969 return LY_EVALID;
5970 }
5971
Michal Vasko03ff5a72019-09-11 13:49:33 +02005972 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
5973 * but it likely won't be used much, so it's a waste of time */
5974 /* copy the context */
5975 set_all_desc = set_copy(set);
5976 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskod3678892020-05-21 10:06:58 +02005977 rc = moveto_node_alldesc(set_all_desc, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005978 if (rc != LY_SUCCESS) {
5979 lyxp_set_free(set_all_desc);
5980 return rc;
5981 }
5982 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005983 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005984 if (rc != LY_SUCCESS) {
5985 lyxp_set_free(set_all_desc);
5986 return rc;
5987 }
5988 lyxp_set_free(set_all_desc);
5989
Radek Krejci1deb5be2020-08-26 16:43:36 +02005990 for (uint32_t i = 0; i < set->used; ) {
5991 uint8_t replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005992
5993 /* only attributes of an elem can be in the result, skip all the rest,
5994 * we have all attributes qualified in lyd tree */
5995 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005996 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005997 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005998 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005999 continue;
6000 }
6001
Michal Vaskod3678892020-05-21 10:06:58 +02006002 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006003 /* match */
6004 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006005 set->val.meta[i].meta = sub;
6006 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006007 /* pos does not change */
6008 replaced = 1;
6009 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006010 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 +02006011 }
6012 ++i;
6013 }
6014 }
6015 }
6016
6017 if (!replaced) {
6018 /* no match */
6019 set_remove_node(set, i);
6020 }
6021 }
6022
6023 return LY_SUCCESS;
6024}
6025
6026/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006027 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6028 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006029 *
6030 * @param[in] parent Current parent.
6031 * @param[in] parent_pos Position of @p parent.
6032 * @param[in] parent_type Node type of @p parent.
6033 * @param[in,out] to_set Set to use.
6034 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006035 * @param[in] options XPath options.
6036 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6037 */
6038static LY_ERR
6039moveto_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 +02006040 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006041{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006042 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006043 LY_ERR rc;
6044
6045 switch (parent_type) {
6046 case LYXP_NODE_ROOT:
6047 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006048 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006049
Michal Vasko61ac2f62020-05-25 12:39:51 +02006050 /* add all top-level nodes as elements */
6051 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006052 break;
6053 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006054 /* add just the text node of this term element node */
6055 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006056 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6057 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6058 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006059 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006060 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006061
6062 /* add all the children of this node */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02006063 first = lyd_node_children(parent, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006064 break;
6065 default:
6066 LOGINT_RET(parent->schema->module->ctx);
6067 }
6068
Michal Vasko61ac2f62020-05-25 12:39:51 +02006069 /* add all top-level nodes as elements */
6070 LY_LIST_FOR(first, iter) {
6071 /* context check */
6072 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6073 continue;
6074 }
6075
6076 /* when check */
6077 if (moveto_when_check(iter)) {
6078 return LY_EINCOMPLETE;
6079 }
6080
6081 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6082 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6083
6084 /* also add all the children of this node, recursively */
6085 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6086 LY_CHECK_RET(rc);
6087 }
6088 }
6089
Michal Vasko03ff5a72019-09-11 13:49:33 +02006090 return LY_SUCCESS;
6091}
6092
6093/**
6094 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6095 * (or LYXP_SET_EMPTY). Context position aware.
6096 *
6097 * @param[in,out] set Set to use.
6098 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6099 * @param[in] options XPath options.
6100 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6101 */
6102static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006103moveto_self(struct lyxp_set *set, uint8_t all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006104{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006105 struct lyxp_set ret_set;
6106 LY_ERR rc;
6107
Michal Vaskod3678892020-05-21 10:06:58 +02006108 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006109 return LY_SUCCESS;
6110 }
6111
6112 if (set->type != LYXP_SET_NODE_SET) {
6113 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6114 return LY_EVALID;
6115 }
6116
6117 /* nothing to do */
6118 if (!all_desc) {
6119 return LY_SUCCESS;
6120 }
6121
Michal Vasko03ff5a72019-09-11 13:49:33 +02006122 /* add all the children, they get added recursively */
6123 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006124 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006125 /* copy the current node to tmp */
6126 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6127
6128 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006129 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006130 continue;
6131 }
6132
Michal Vasko03ff5a72019-09-11 13:49:33 +02006133 /* add all the children */
6134 rc = moveto_self_add_children_r(set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, &ret_set,
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006135 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006136 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006137 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006138 return rc;
6139 }
6140 }
6141
6142 /* use the temporary set as the current one */
6143 ret_set.ctx_pos = set->ctx_pos;
6144 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006145 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006146 memcpy(set, &ret_set, sizeof *set);
6147
6148 return LY_SUCCESS;
6149}
6150
6151/**
6152 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6153 * (or LYXP_SET_EMPTY).
6154 *
6155 * @param[in,out] set Set to use.
6156 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6157 * @param[in] options XPath options.
6158 * @return LY_ERR
6159 */
6160static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006161moveto_scnode_self(struct lyxp_set *set, uint8_t all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006162{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006163 uint32_t getnext_opts;
6164 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006165 const struct lysc_node *iter, *start_parent;
6166 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006167
Michal Vaskod3678892020-05-21 10:06:58 +02006168 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006169 return LY_SUCCESS;
6170 }
6171
6172 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006173 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006174 return LY_EVALID;
6175 }
6176
6177 /* nothing to do */
6178 if (!all_desc) {
6179 return LY_SUCCESS;
6180 }
6181
Michal Vasko519fd602020-05-26 12:17:39 +02006182 /* getnext opts */
6183 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
6184 if (options & LYXP_SCNODE_OUTPUT) {
6185 getnext_opts |= LYS_GETNEXT_OUTPUT;
6186 }
6187
6188 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006189 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006190 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006191 if (set->val.scnodes[i].in_ctx != -2) {
6192 continue;
6193 }
6194
Michal Vasko519fd602020-05-26 12:17:39 +02006195 /* remember context node */
6196 set->val.scnodes[i].in_ctx = -1;
6197 } else {
6198 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006199 }
6200
Michal Vasko519fd602020-05-26 12:17:39 +02006201 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006202
Michal Vasko519fd602020-05-26 12:17:39 +02006203 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6204 /* it can actually be in any module, it's all <running> */
6205 mod_idx = 0;
6206 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6207 iter = NULL;
6208 /* module may not be implemented */
6209 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6210 /* context check */
6211 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6212 continue;
6213 }
6214
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006215 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006216 /* throw away the insert index, we want to consider that node again, recursively */
6217 }
6218 }
6219
6220 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6221 iter = NULL;
6222 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006223 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006224 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006225 continue;
6226 }
6227
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006228 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006229 }
6230 }
6231 }
6232
6233 return LY_SUCCESS;
6234}
6235
6236/**
6237 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6238 * (or LYXP_SET_EMPTY). Context position aware.
6239 *
6240 * @param[in] set Set to use.
6241 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6242 * @param[in] options XPath options.
6243 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6244 */
6245static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006246moveto_parent(struct lyxp_set *set, uint8_t all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006247{
6248 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006249 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006250 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006251
Michal Vaskod3678892020-05-21 10:06:58 +02006252 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006253 return LY_SUCCESS;
6254 }
6255
6256 if (set->type != LYXP_SET_NODE_SET) {
6257 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6258 return LY_EVALID;
6259 }
6260
6261 if (all_desc) {
6262 /* <path>//.. == <path>//./.. */
6263 rc = moveto_self(set, 1, options);
6264 LY_CHECK_RET(rc);
6265 }
6266
Radek Krejci1deb5be2020-08-26 16:43:36 +02006267 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006268 node = set->val.nodes[i].node;
6269
6270 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6271 new_node = (struct lyd_node *)node->parent;
6272 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6273 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006274 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6275 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006276 if (!new_node) {
6277 LOGINT_RET(set->ctx);
6278 }
6279 } else {
6280 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006281 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006282 continue;
6283 }
6284
Michal Vaskoa1424542019-11-14 16:08:52 +01006285 /* when check */
6286 if (moveto_when_check(new_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006287 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006288 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006289
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006290 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006291 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006292 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006293
Michal Vasko03ff5a72019-09-11 13:49:33 +02006294 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006295 /* 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 +02006296 new_type = LYXP_NODE_ELEM;
6297 }
6298
Michal Vasko03ff5a72019-09-11 13:49:33 +02006299 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006300 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006301 } else {
6302 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006303 }
6304 }
6305
Michal Vasko2caefc12019-11-14 16:07:56 +01006306 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006307 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006308
6309 return LY_SUCCESS;
6310}
6311
6312/**
6313 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6314 * (or LYXP_SET_EMPTY).
6315 *
6316 * @param[in] set Set to use.
6317 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6318 * @param[in] options XPath options.
6319 * @return LY_ERR
6320 */
6321static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006322moveto_scnode_parent(struct lyxp_set *set, uint8_t all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006323{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006324 uint32_t i, orig_used, idx;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006325 uint8_t temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006326 const struct lysc_node *node, *new_node;
6327 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006328
Michal Vaskod3678892020-05-21 10:06:58 +02006329 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006330 return LY_SUCCESS;
6331 }
6332
6333 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006334 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006335 return LY_EVALID;
6336 }
6337
6338 if (all_desc) {
6339 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006340 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006341 }
6342
Michal Vasko03ff5a72019-09-11 13:49:33 +02006343 orig_used = set->used;
6344 for (i = 0; i < orig_used; ++i) {
6345 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006346 if (set->val.scnodes[i].in_ctx != -2) {
6347 continue;
6348 }
6349
6350 /* remember context node */
6351 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01006352 } else {
6353 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006354 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006355
6356 node = set->val.scnodes[i].scnode;
6357
6358 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006359 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006360 } else {
6361 /* root does not have a parent */
6362 continue;
6363 }
6364
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006365 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006366 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006367 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006368
Michal Vasko03ff5a72019-09-11 13:49:33 +02006369 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006370 /* 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 +02006371 new_type = LYXP_NODE_ELEM;
6372 }
6373
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006374 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6375 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006376 set->val.scnodes[idx].in_ctx = 2;
6377 temp_ctx = 1;
6378 }
6379 }
6380
6381 if (temp_ctx) {
6382 for (i = 0; i < orig_used; ++i) {
6383 if (set->val.scnodes[i].in_ctx == 2) {
6384 set->val.scnodes[i].in_ctx = 1;
6385 }
6386 }
6387 }
6388
6389 return LY_SUCCESS;
6390}
6391
6392/**
6393 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6394 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6395 *
6396 * @param[in,out] set1 Set to use for the result.
6397 * @param[in] set2 Set acting as the second operand for @p op.
6398 * @param[in] op Comparison operator to process.
6399 * @param[in] options XPath options.
6400 * @return LY_ERR
6401 */
6402static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006403moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006404{
6405 /*
6406 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6407 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6408 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6409 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6410 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6411 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6412 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6413 *
6414 * '=' or '!='
6415 * BOOLEAN + BOOLEAN
6416 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6417 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6418 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6419 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6420 * NUMBER + NUMBER
6421 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6422 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6423 * STRING + STRING
6424 *
6425 * '<=', '<', '>=', '>'
6426 * NUMBER + NUMBER
6427 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6428 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6429 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6430 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6431 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6432 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6433 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6434 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6435 */
6436 struct lyxp_set iter1, iter2;
6437 int result;
6438 int64_t i;
6439 LY_ERR rc;
6440
Michal Vasko004d3152020-06-11 19:59:22 +02006441 memset(&iter1, 0, sizeof iter1);
6442 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006443
6444 /* iterative evaluation with node-sets */
6445 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6446 if (set1->type == LYXP_SET_NODE_SET) {
6447 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006448 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006449 switch (set2->type) {
6450 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006451 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006452 break;
6453 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006454 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006455 break;
6456 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006457 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006458 break;
6459 }
6460 LY_CHECK_RET(rc);
6461
Michal Vasko4c7763f2020-07-27 17:40:37 +02006462 /* canonize set2 */
6463 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6464
6465 /* compare recursively */
6466 rc = moveto_op_comp(&iter1, &iter2, op, options);
6467 lyxp_set_free_content(&iter2);
6468 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006469
6470 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006471 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006472 set_fill_boolean(set1, 1);
6473 return LY_SUCCESS;
6474 }
6475 }
6476 } else {
6477 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006478 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006479 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006480 case LYXP_SET_NUMBER:
6481 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6482 break;
6483 case LYXP_SET_BOOLEAN:
6484 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6485 break;
6486 default:
6487 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6488 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006489 }
6490 LY_CHECK_RET(rc);
6491
Michal Vasko4c7763f2020-07-27 17:40:37 +02006492 /* canonize set1 */
6493 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 +02006494
Michal Vasko4c7763f2020-07-27 17:40:37 +02006495 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006496 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006497 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006498 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006499
6500 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006501 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006502 set_fill_boolean(set1, 1);
6503 return LY_SUCCESS;
6504 }
6505 }
6506 }
6507
6508 /* false for all nodes */
6509 set_fill_boolean(set1, 0);
6510 return LY_SUCCESS;
6511 }
6512
6513 /* first convert properly */
6514 if ((op[0] == '=') || (op[0] == '!')) {
6515 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006516 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6517 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006518 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006519 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006520 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006521 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006522 LY_CHECK_RET(rc);
6523 } /* else we have 2 strings */
6524 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006525 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006526 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006527 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006528 LY_CHECK_RET(rc);
6529 }
6530
6531 assert(set1->type == set2->type);
6532
6533 /* compute result */
6534 if (op[0] == '=') {
6535 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006536 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006537 } else if (set1->type == LYXP_SET_NUMBER) {
6538 result = (set1->val.num == set2->val.num);
6539 } else {
6540 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006541 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006542 }
6543 } else if (op[0] == '!') {
6544 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006545 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006546 } else if (set1->type == LYXP_SET_NUMBER) {
6547 result = (set1->val.num != set2->val.num);
6548 } else {
6549 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006550 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006551 }
6552 } else {
6553 assert(set1->type == LYXP_SET_NUMBER);
6554 if (op[0] == '<') {
6555 if (op[1] == '=') {
6556 result = (set1->val.num <= set2->val.num);
6557 } else {
6558 result = (set1->val.num < set2->val.num);
6559 }
6560 } else {
6561 if (op[1] == '=') {
6562 result = (set1->val.num >= set2->val.num);
6563 } else {
6564 result = (set1->val.num > set2->val.num);
6565 }
6566 }
6567 }
6568
6569 /* assign result */
6570 if (result) {
6571 set_fill_boolean(set1, 1);
6572 } else {
6573 set_fill_boolean(set1, 0);
6574 }
6575
6576 return LY_SUCCESS;
6577}
6578
6579/**
6580 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6581 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6582 *
6583 * @param[in,out] set1 Set to use for the result.
6584 * @param[in] set2 Set acting as the second operand for @p op.
6585 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006586 * @return LY_ERR
6587 */
6588static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006589moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006590{
6591 LY_ERR rc;
6592
6593 /* unary '-' */
6594 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006595 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006596 LY_CHECK_RET(rc);
6597 set1->val.num *= -1;
6598 lyxp_set_free(set2);
6599 return LY_SUCCESS;
6600 }
6601
6602 assert(set1 && set2);
6603
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006604 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006605 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006606 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006607 LY_CHECK_RET(rc);
6608
6609 switch (op[0]) {
6610 /* '+' */
6611 case '+':
6612 set1->val.num += set2->val.num;
6613 break;
6614
6615 /* '-' */
6616 case '-':
6617 set1->val.num -= set2->val.num;
6618 break;
6619
6620 /* '*' */
6621 case '*':
6622 set1->val.num *= set2->val.num;
6623 break;
6624
6625 /* 'div' */
6626 case 'd':
6627 set1->val.num /= set2->val.num;
6628 break;
6629
6630 /* 'mod' */
6631 case 'm':
6632 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6633 break;
6634
6635 default:
6636 LOGINT_RET(set1->ctx);
6637 }
6638
6639 return LY_SUCCESS;
6640}
6641
6642/*
6643 * eval functions
6644 *
6645 * They execute a parsed XPath expression on some data subtree.
6646 */
6647
6648/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006649 * @brief Evaluate Predicate. Logs directly on error.
6650 *
Michal Vaskod3678892020-05-21 10:06:58 +02006651 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006652 *
6653 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006654 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006655 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6656 * @param[in] options XPath options.
6657 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6658 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6659 */
6660static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006661eval_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options, uint8_t parent_pos_pred)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006662{
6663 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006664 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006665 uint32_t orig_pos, orig_size;
6666 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006667 struct lyxp_set set2;
6668 struct lyd_node *orig_parent;
6669
6670 /* '[' */
6671 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006672 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6673 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006674
6675 if (!set) {
6676only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006677 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006678 LY_CHECK_RET(rc);
6679 } else if (set->type == LYXP_SET_NODE_SET) {
6680 /* 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 +01006681 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006682
6683 /* empty set, nothing to evaluate */
6684 if (!set->used) {
6685 goto only_parse;
6686 }
6687
Michal Vasko004d3152020-06-11 19:59:22 +02006688 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006689 orig_pos = 0;
6690 orig_size = set->used;
6691 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006692 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006693 set_init(&set2, set);
6694 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6695 /* remember the node context position for position() and context size for last(),
6696 * predicates should always be evaluated with respect to the child axis (since we do
6697 * not support explicit axes) so we assign positions based on their parents */
6698 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6699 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6700 orig_pos = 1;
6701 } else {
6702 ++orig_pos;
6703 }
6704
6705 set2.ctx_pos = orig_pos;
6706 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006707 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006708
Michal Vasko004d3152020-06-11 19:59:22 +02006709 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006710 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006711 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006712 return rc;
6713 }
6714
6715 /* number is a position */
6716 if (set2.type == LYXP_SET_NUMBER) {
6717 if ((long long)set2.val.num == orig_pos) {
6718 set2.val.num = 1;
6719 } else {
6720 set2.val.num = 0;
6721 }
6722 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006723 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006724
6725 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006726 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006727 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006728 }
6729 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006730 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006731
6732 } else if (set->type == LYXP_SET_SCNODE_SET) {
6733 for (i = 0; i < set->used; ++i) {
6734 if (set->val.scnodes[i].in_ctx == 1) {
6735 /* there is a currently-valid node */
6736 break;
6737 }
6738 }
6739 /* empty set, nothing to evaluate */
6740 if (i == set->used) {
6741 goto only_parse;
6742 }
6743
Michal Vasko004d3152020-06-11 19:59:22 +02006744 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006745
Michal Vasko03ff5a72019-09-11 13:49:33 +02006746 /* set special in_ctx to all the valid snodes */
6747 pred_in_ctx = set_scnode_new_in_ctx(set);
6748
6749 /* use the valid snodes one-by-one */
6750 for (i = 0; i < set->used; ++i) {
6751 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6752 continue;
6753 }
6754 set->val.scnodes[i].in_ctx = 1;
6755
Michal Vasko004d3152020-06-11 19:59:22 +02006756 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006757
Michal Vasko004d3152020-06-11 19:59:22 +02006758 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006759 LY_CHECK_RET(rc);
6760
6761 set->val.scnodes[i].in_ctx = pred_in_ctx;
6762 }
6763
6764 /* restore the state as it was before the predicate */
6765 for (i = 0; i < set->used; ++i) {
6766 if (set->val.scnodes[i].in_ctx == 1) {
6767 set->val.scnodes[i].in_ctx = 0;
6768 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6769 set->val.scnodes[i].in_ctx = 1;
6770 }
6771 }
6772
6773 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006774 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006775 set_fill_set(&set2, set);
6776
Michal Vasko004d3152020-06-11 19:59:22 +02006777 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006778 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006779 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006780 return rc;
6781 }
6782
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006783 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006784 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006785 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006786 }
Michal Vaskod3678892020-05-21 10:06:58 +02006787 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006788 }
6789
6790 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006791 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006792 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006793 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6794 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006795
6796 return LY_SUCCESS;
6797}
6798
6799/**
Michal Vaskod3678892020-05-21 10:06:58 +02006800 * @brief Evaluate Literal. Logs directly on error.
6801 *
6802 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006803 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006804 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6805 */
6806static void
Michal Vasko004d3152020-06-11 19:59:22 +02006807eval_literal(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006808{
6809 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006810 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006811 set_fill_string(set, "", 0);
6812 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006813 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 +02006814 }
6815 }
6816 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006817 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6818 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006819}
6820
6821/**
Michal Vasko004d3152020-06-11 19:59:22 +02006822 * @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 +02006823 *
Michal Vasko004d3152020-06-11 19:59:22 +02006824 * @param[in] exp Full parsed XPath expression.
6825 * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success.
6826 * @param[in] scnode Found schema node as context for the predicate.
6827 * @param[in] format Format of any prefixes in key names/values.
6828 * @param[out] predicates Parsed predicates.
6829 * @param[out] pred_type Type of @p predicates.
6830 * @return LY_SUCCESS on success,
6831 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006832 */
6833static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006834eval_name_test_try_compile_predicates(struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *scnode,
Radek Krejci0f969882020-08-21 16:56:47 +02006835 LY_PREFIX_FORMAT format, struct ly_path_predicate **predicates,
6836 enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006837{
Michal Vasko004d3152020-06-11 19:59:22 +02006838 LY_ERR ret = LY_SUCCESS;
6839 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006840 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006841 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006842 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006843 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006844
Michal Vasko004d3152020-06-11 19:59:22 +02006845 assert(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006846
Michal Vasko004d3152020-06-11 19:59:22 +02006847 if (scnode->nodetype == LYS_LIST) {
6848 /* get key count */
6849 if (scnode->flags & LYS_KEYLESS) {
6850 return LY_EINVAL;
6851 }
Radek Krejci1e008d22020-08-17 11:37:37 +02006852 for (key_count = 0, key = lysc_node_children(scnode, 0); key && (key->flags & LYS_KEY); key = key->next, ++key_count) {}
Michal Vasko004d3152020-06-11 19:59:22 +02006853 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006854
Michal Vasko004d3152020-06-11 19:59:22 +02006855 /* learn where the predicates end */
6856 e_idx = *tok_idx;
6857 while (key_count) {
6858 /* '[' */
6859 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6860 return LY_EINVAL;
6861 }
6862 ++e_idx;
6863
6864 /* ']' */
6865 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6866 ++e_idx;
6867 }
6868 ++e_idx;
6869
6870 /* another presumably key predicate parsed */
6871 --key_count;
6872 }
Michal Vasko004d3152020-06-11 19:59:22 +02006873 } else {
6874 /* learn just where this single predicate ends */
6875 e_idx = *tok_idx;
6876
Michal Vaskod3678892020-05-21 10:06:58 +02006877 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006878 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6879 return LY_EINVAL;
6880 }
6881 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006882
6883 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006884 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6885 ++e_idx;
6886 }
6887 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006888 }
6889
Michal Vasko004d3152020-06-11 19:59:22 +02006890 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6891 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6892
6893 /* turn logging off */
6894 prev_lo = ly_log_options(0);
6895
6896 /* parse the predicate(s) */
Michal Vasko6b26e742020-07-17 15:02:10 +02006897 LY_CHECK_GOTO(ret = ly_path_parse_predicate(scnode->module->ctx, scnode, exp->expr + exp->tok_pos[*tok_idx], pred_len,
Michal Vasko004d3152020-06-11 19:59:22 +02006898 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
6899
6900 /* compile */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006901 ret = ly_path_compile_predicate(scnode->module->ctx, scnode, scnode->module, scnode, exp2, &pred_idx,
6902 format, scnode->module, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006903 LY_CHECK_GOTO(ret, cleanup);
6904
6905 /* success, the predicate must include all the needed information for hash-based search */
6906 *tok_idx = e_idx;
6907
6908cleanup:
6909 ly_log_options(prev_lo);
6910 lyxp_expr_free(scnode->module->ctx, exp2);
6911 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006912}
6913
6914/**
6915 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
6916 *
6917 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
6918 * [6] NodeTest ::= NameTest | NodeType '(' ')'
6919 * [7] NameTest ::= '*' | NCName ':' '*' | QName
6920 *
6921 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006922 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006923 * @param[in] attr_axis Whether to search attributes or standard nodes.
6924 * @param[in] all_desc Whether to search all the descendants or children only.
6925 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6926 * @param[in] options XPath options.
6927 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6928 */
6929static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006930eval_name_test_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, uint8_t attr_axis, uint8_t all_desc, struct lyxp_set *set,
6931 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02006932{
Michal Vaskod3678892020-05-21 10:06:58 +02006933 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02006934 const char *ncname, *ncname_dict = NULL;
6935 uint16_t ncname_len;
Michal Vaskod3678892020-05-21 10:06:58 +02006936 const struct lys_module *moveto_mod;
6937 const struct lysc_node *scnode = NULL, *tmp;
Michal Vasko004d3152020-06-11 19:59:22 +02006938 struct ly_path_predicate *predicates = NULL;
6939 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006940 LY_ERR rc = LY_SUCCESS;
6941
6942 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006943 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6944 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006945
6946 if (!set) {
6947 goto moveto;
6948 }
6949
Michal Vasko004d3152020-06-11 19:59:22 +02006950 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
6951 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02006952
6953 /* parse (and skip) module name */
6954 rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod);
6955 LY_CHECK_GOTO(rc, cleanup);
6956
6957 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
6958 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006959 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +02006960 if (set->val.nodes[i].type == set->root_type) {
6961 tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0);
6962 } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM)
6963 && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) {
6964 /* do not repeat the same search */
6965 tmp = lys_find_child(set->val.nodes[i].node->schema, moveto_mod, ncname, ncname_len, 0, 0);
Radek Krejcib1247842020-07-02 16:22:38 +02006966 } else {
6967 tmp = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006968 }
6969
6970 /* additional context check */
6971 if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) {
6972 tmp = NULL;
6973 }
6974
6975 if (tmp) {
6976 if (scnode) {
6977 /* we found a schema node with the same name but at different level, give up, too complicated */
6978 scnode = NULL;
6979 break;
6980 } else {
6981 /* remember the found schema node and continue to make sure it can be used */
6982 scnode = tmp;
6983 }
Michal Vaskod3678892020-05-21 10:06:58 +02006984 }
6985 }
6986
Michal Vasko004d3152020-06-11 19:59:22 +02006987 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
6988 /* try to create the predicates */
6989 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) {
6990 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02006991 scnode = NULL;
6992 }
6993 }
6994 }
6995
Michal Vasko004d3152020-06-11 19:59:22 +02006996 if (!scnode && moveto_mod) {
6997 /* we are not able to match based on a schema node and not all the modules match,
6998 * use dictionary for efficient comparison */
6999 ncname_dict = lydict_insert(set->ctx, ncname, ncname_len);
Michal Vaskod3678892020-05-21 10:06:58 +02007000 }
7001
7002moveto:
7003 /* move to the attribute(s), data node(s), or schema node(s) */
7004 if (attr_axis) {
7005 if (set && (options & LYXP_SCNODE_ALL)) {
7006 set_scnode_clear_ctx(set);
7007 } else {
7008 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007009 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007010 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007011 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007012 }
7013 LY_CHECK_GOTO(rc, cleanup);
7014 }
7015 } else {
7016 if (set && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007017 int64_t i;
7018
Michal Vaskod3678892020-05-21 10:06:58 +02007019 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007020 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007021 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007022 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007023 }
7024 LY_CHECK_GOTO(rc, cleanup);
7025
7026 for (i = set->used - 1; i > -1; --i) {
7027 if (set->val.scnodes[i].in_ctx > 0) {
7028 break;
7029 }
7030 }
7031 if (i == -1) {
7032 path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0);
7033 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
Michal Vasko6b26e742020-07-17 15:02:10 +02007034 ncname_len, ncname, ncname - exp->expr, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007035 free(path);
7036 }
7037 } else {
7038 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007039 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007040 } else {
7041 if (scnode) {
7042 /* we can find the nodes using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02007043 rc = moveto_node_hash(set, scnode, predicates);
Michal Vaskod3678892020-05-21 10:06:58 +02007044 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007045 rc = moveto_node(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007046 }
7047 }
7048 LY_CHECK_GOTO(rc, cleanup);
7049 }
7050 }
7051
7052 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007053 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7054 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007055 LY_CHECK_RET(rc);
7056 }
7057
7058cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007059 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007060 lydict_remove(set->ctx, ncname_dict);
7061 ly_path_predicates_free(set->ctx, pred_type, scnode, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007062 }
Michal Vaskod3678892020-05-21 10:06:58 +02007063 return rc;
7064}
7065
7066/**
7067 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7068 *
7069 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7070 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7071 * [8] NodeType ::= 'text' | 'node'
7072 *
7073 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007074 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007075 * @param[in] attr_axis Whether to search attributes or standard nodes.
7076 * @param[in] all_desc Whether to search all the descendants or children only.
7077 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7078 * @param[in] options XPath options.
7079 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7080 */
7081static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007082eval_node_type_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, uint8_t attr_axis, uint8_t all_desc,
7083 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007084{
7085 LY_ERR rc;
7086
7087 /* TODO */
7088 (void)attr_axis;
7089 (void)all_desc;
7090
7091 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007092 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007093 if (set->type == LYXP_SET_SCNODE_SET) {
7094 set_scnode_clear_ctx(set);
7095 /* just for the debug message below */
7096 set = NULL;
7097 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007098 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007099 rc = xpath_node(NULL, 0, set, options);
7100 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007101 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007102 rc = xpath_text(NULL, 0, set, options);
7103 }
7104 LY_CHECK_RET(rc);
7105 }
7106 }
7107 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007108 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7109 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007110
7111 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007112 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007113 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007114 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7115 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007116
7117 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007118 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007119 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007120 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7121 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007122
7123 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007124 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7125 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007126 LY_CHECK_RET(rc);
7127 }
7128
7129 return LY_SUCCESS;
7130}
7131
7132/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007133 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7134 *
7135 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7136 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007137 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007138 *
7139 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007140 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007141 * @param[in] all_desc Whether to search all the descendants or children only.
7142 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7143 * @param[in] options XPath options.
7144 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7145 */
7146static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007147eval_relative_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, uint8_t all_desc, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007148{
Radek Krejci1deb5be2020-08-26 16:43:36 +02007149 uint8_t attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007150 LY_ERR rc;
7151
7152 goto step;
7153 do {
7154 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007155 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007156 all_desc = 0;
7157 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007158 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007159 all_desc = 1;
7160 }
7161 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007162 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7163 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007164
7165step:
Michal Vaskod3678892020-05-21 10:06:58 +02007166 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007167 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007168 attr_axis = 1;
7169
7170 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007171 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7172 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007173 } else {
7174 attr_axis = 0;
7175 }
7176
Michal Vasko03ff5a72019-09-11 13:49:33 +02007177 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007178 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007179 case LYXP_TOKEN_DOT:
7180 /* evaluate '.' */
7181 if (set && (options & LYXP_SCNODE_ALL)) {
7182 rc = moveto_scnode_self(set, all_desc, options);
7183 } else {
7184 rc = moveto_self(set, all_desc, options);
7185 }
7186 LY_CHECK_RET(rc);
7187 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007188 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7189 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007190 break;
7191
7192 case LYXP_TOKEN_DDOT:
7193 /* evaluate '..' */
7194 if (set && (options & LYXP_SCNODE_ALL)) {
7195 rc = moveto_scnode_parent(set, all_desc, options);
7196 } else {
7197 rc = moveto_parent(set, all_desc, options);
7198 }
7199 LY_CHECK_RET(rc);
7200 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007201 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7202 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007203 break;
7204
Michal Vasko03ff5a72019-09-11 13:49:33 +02007205 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007206 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007207 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007208 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007209 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007210
Michal Vaskod3678892020-05-21 10:06:58 +02007211 case LYXP_TOKEN_NODETYPE:
7212 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007213 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007214 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007215 break;
7216
7217 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007218 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007219 }
Michal Vasko004d3152020-06-11 19:59:22 +02007220 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007221
7222 return LY_SUCCESS;
7223}
7224
7225/**
7226 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7227 *
7228 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7229 *
7230 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007231 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007232 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7233 * @param[in] options XPath options.
7234 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7235 */
7236static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007237eval_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 +02007238{
Radek Krejci1deb5be2020-08-26 16:43:36 +02007239 uint8_t all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007240 LY_ERR rc;
7241
7242 if (set) {
7243 /* no matter what tokens follow, we need to be at the root */
7244 moveto_root(set, options);
7245 }
7246
7247 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007248 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007249 /* evaluate '/' - deferred */
7250 all_desc = 0;
7251 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007252 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7253 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007254
Michal Vasko004d3152020-06-11 19:59:22 +02007255 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007256 return LY_SUCCESS;
7257 }
Michal Vasko004d3152020-06-11 19:59:22 +02007258 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007259 case LYXP_TOKEN_DOT:
7260 case LYXP_TOKEN_DDOT:
7261 case LYXP_TOKEN_AT:
7262 case LYXP_TOKEN_NAMETEST:
7263 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02007264 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007265 LY_CHECK_RET(rc);
7266 break;
7267 default:
7268 break;
7269 }
7270
Michal Vasko03ff5a72019-09-11 13:49:33 +02007271 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007272 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007273 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7274 all_desc = 1;
7275 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007276 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7277 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007278
Michal Vasko004d3152020-06-11 19:59:22 +02007279 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007280 LY_CHECK_RET(rc);
7281 }
7282
7283 return LY_SUCCESS;
7284}
7285
7286/**
7287 * @brief Evaluate FunctionCall. Logs directly on error.
7288 *
Michal Vaskod3678892020-05-21 10:06:58 +02007289 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007290 *
7291 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007292 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007293 * @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 (LY_EINCOMPLETE on unresolved when)
7296 */
7297static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007298eval_function_call(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007299{
7300 LY_ERR rc;
Radek Krejci1deb5be2020-08-26 16:43:36 +02007301 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007302 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007303 struct lyxp_set **args = NULL, **args_aux;
7304
7305 if (set) {
7306 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007307 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007308 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007309 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007310 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007311 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007312 xpath_func = &xpath_sum;
7313 }
7314 break;
7315 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007316 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007317 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007318 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007319 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007320 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007321 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007322 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007323 xpath_func = &xpath_true;
7324 }
7325 break;
7326 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007327 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007328 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007329 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007330 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007331 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007332 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007333 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007334 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007335 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007336 xpath_func = &xpath_deref;
7337 }
7338 break;
7339 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007340 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007341 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007342 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007343 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007344 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007345 xpath_func = &xpath_string;
7346 }
7347 break;
7348 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007349 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007350 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007351 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007352 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007353 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007354 xpath_func = &xpath_current;
7355 }
7356 break;
7357 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007358 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007359 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007360 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007361 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007362 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007363 xpath_func = &xpath_re_match;
7364 }
7365 break;
7366 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007367 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007368 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007369 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007370 xpath_func = &xpath_translate;
7371 }
7372 break;
7373 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007374 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007375 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007376 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007377 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007378 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007379 xpath_func = &xpath_bit_is_set;
7380 }
7381 break;
7382 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007383 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007384 xpath_func = &xpath_starts_with;
7385 }
7386 break;
7387 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007388 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007389 xpath_func = &xpath_derived_from;
7390 }
7391 break;
7392 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007393 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007394 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007395 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007396 xpath_func = &xpath_string_length;
7397 }
7398 break;
7399 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007400 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007401 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007402 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007403 xpath_func = &xpath_substring_after;
7404 }
7405 break;
7406 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007407 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007408 xpath_func = &xpath_substring_before;
7409 }
7410 break;
7411 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007412 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007413 xpath_func = &xpath_derived_from_or_self;
7414 }
7415 break;
7416 }
7417
7418 if (!xpath_func) {
Michal Vasko004d3152020-06-11 19:59:22 +02007419 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007420 return LY_EVALID;
7421 }
7422 }
7423
7424 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007425 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7426 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007427
7428 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007429 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007430 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007431 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7432 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007433
7434 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007435 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007436 if (set) {
7437 args = malloc(sizeof *args);
7438 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7439 arg_count = 1;
7440 args[0] = set_copy(set);
7441 if (!args[0]) {
7442 rc = LY_EMEM;
7443 goto cleanup;
7444 }
7445
Michal Vasko004d3152020-06-11 19:59:22 +02007446 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007447 LY_CHECK_GOTO(rc, cleanup);
7448 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007449 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007450 LY_CHECK_GOTO(rc, cleanup);
7451 }
7452 }
Michal Vasko004d3152020-06-11 19:59:22 +02007453 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007454 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007455 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7456 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007457
7458 if (set) {
7459 ++arg_count;
7460 args_aux = realloc(args, arg_count * sizeof *args);
7461 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7462 args = args_aux;
7463 args[arg_count - 1] = set_copy(set);
7464 if (!args[arg_count - 1]) {
7465 rc = LY_EMEM;
7466 goto cleanup;
7467 }
7468
Michal Vasko004d3152020-06-11 19:59:22 +02007469 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007470 LY_CHECK_GOTO(rc, cleanup);
7471 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007472 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007473 LY_CHECK_GOTO(rc, cleanup);
7474 }
7475 }
7476
7477 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007478 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007479 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007480 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7481 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007482
7483 if (set) {
7484 /* evaluate function */
7485 rc = xpath_func(args, arg_count, set, options);
7486
7487 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007488 /* merge all nodes from arg evaluations */
7489 for (i = 0; i < arg_count; ++i) {
7490 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007491 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007492 }
7493 }
7494 } else {
7495 rc = LY_SUCCESS;
7496 }
7497
7498cleanup:
7499 for (i = 0; i < arg_count; ++i) {
7500 lyxp_set_free(args[i]);
7501 }
7502 free(args);
7503
7504 return rc;
7505}
7506
7507/**
7508 * @brief Evaluate Number. Logs directly on error.
7509 *
7510 * @param[in] ctx Context for errors.
7511 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007512 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007513 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7514 * @return LY_ERR
7515 */
7516static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007517eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007518{
7519 long double num;
7520 char *endptr;
7521
7522 if (set) {
7523 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007524 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007525 if (errno) {
Michal Vasko004d3152020-06-11 19:59:22 +02007526 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007527 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko004d3152020-06-11 19:59:22 +02007528 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007529 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007530 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
7531 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007532 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko004d3152020-06-11 19:59:22 +02007533 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007534 return LY_EVALID;
7535 }
7536
7537 set_fill_number(set, num);
7538 }
7539
7540 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007541 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7542 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007543 return LY_SUCCESS;
7544}
7545
7546/**
7547 * @brief Evaluate PathExpr. Logs directly on error.
7548 *
Michal Vaskod3678892020-05-21 10:06:58 +02007549 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007550 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7551 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7552 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007553 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007554 *
7555 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007556 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007557 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7558 * @param[in] options XPath options.
7559 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7560 */
7561static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007562eval_path_expr(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007563{
Radek Krejci1deb5be2020-08-26 16:43:36 +02007564 uint8_t all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007565 LY_ERR rc;
7566
Michal Vasko004d3152020-06-11 19:59:22 +02007567 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007568 case LYXP_TOKEN_PAR1:
7569 /* '(' Expr ')' */
7570
7571 /* '(' */
7572 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007573 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7574 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007575
7576 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007577 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007578 LY_CHECK_RET(rc);
7579
7580 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007581 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007582 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007583 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7584 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007585
7586 parent_pos_pred = 0;
7587 goto predicate;
7588
7589 case LYXP_TOKEN_DOT:
7590 case LYXP_TOKEN_DDOT:
7591 case LYXP_TOKEN_AT:
7592 case LYXP_TOKEN_NAMETEST:
7593 case LYXP_TOKEN_NODETYPE:
7594 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007595 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007596 LY_CHECK_RET(rc);
7597 break;
7598
7599 case LYXP_TOKEN_FUNCNAME:
7600 /* FunctionCall */
7601 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007602 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007603 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007604 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007605 }
7606 LY_CHECK_RET(rc);
7607
7608 parent_pos_pred = 1;
7609 goto predicate;
7610
Michal Vasko3e48bf32020-06-01 08:39:07 +02007611 case LYXP_TOKEN_OPER_PATH:
7612 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007613 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007614 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007615 LY_CHECK_RET(rc);
7616 break;
7617
7618 case LYXP_TOKEN_LITERAL:
7619 /* Literal */
7620 if (!set || (options & LYXP_SCNODE_ALL)) {
7621 if (set) {
7622 set_scnode_clear_ctx(set);
7623 }
Michal Vasko004d3152020-06-11 19:59:22 +02007624 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007625 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007626 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007627 }
7628
7629 parent_pos_pred = 1;
7630 goto predicate;
7631
7632 case LYXP_TOKEN_NUMBER:
7633 /* Number */
7634 if (!set || (options & LYXP_SCNODE_ALL)) {
7635 if (set) {
7636 set_scnode_clear_ctx(set);
7637 }
Michal Vasko004d3152020-06-11 19:59:22 +02007638 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007639 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007640 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007641 }
7642 LY_CHECK_RET(rc);
7643
7644 parent_pos_pred = 1;
7645 goto predicate;
7646
7647 default:
Michal Vasko004d3152020-06-11 19:59:22 +02007648 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]),
7649 &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007650 return LY_EVALID;
7651 }
7652
7653 return LY_SUCCESS;
7654
7655predicate:
7656 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007657 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7658 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007659 LY_CHECK_RET(rc);
7660 }
7661
7662 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007663 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007664
7665 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007666 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007667 all_desc = 0;
7668 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007669 all_desc = 1;
7670 }
7671
7672 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007673 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7674 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007675
Michal Vasko004d3152020-06-11 19:59:22 +02007676 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007677 LY_CHECK_RET(rc);
7678 }
7679
7680 return LY_SUCCESS;
7681}
7682
7683/**
7684 * @brief Evaluate UnionExpr. Logs directly on error.
7685 *
Michal Vaskod3678892020-05-21 10:06:58 +02007686 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007687 *
7688 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007689 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007690 * @param[in] repeat How many times this expression is repeated.
7691 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7692 * @param[in] options XPath options.
7693 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7694 */
7695static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007696eval_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 +02007697{
7698 LY_ERR rc = LY_SUCCESS;
7699 struct lyxp_set orig_set, set2;
7700 uint16_t i;
7701
7702 assert(repeat);
7703
7704 set_init(&orig_set, set);
7705 set_init(&set2, set);
7706
7707 set_fill_set(&orig_set, set);
7708
Michal Vasko004d3152020-06-11 19:59:22 +02007709 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007710 LY_CHECK_GOTO(rc, cleanup);
7711
7712 /* ('|' PathExpr)* */
7713 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007714 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007715 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007716 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7717 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007718
7719 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007720 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007721 LY_CHECK_GOTO(rc, cleanup);
7722 continue;
7723 }
7724
7725 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007726 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007727 LY_CHECK_GOTO(rc, cleanup);
7728
7729 /* eval */
7730 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007731 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007732 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007733 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007734 LY_CHECK_GOTO(rc, cleanup);
7735 }
7736 }
7737
7738cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007739 lyxp_set_free_content(&orig_set);
7740 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007741 return rc;
7742}
7743
7744/**
7745 * @brief Evaluate UnaryExpr. Logs directly on error.
7746 *
Michal Vaskod3678892020-05-21 10:06:58 +02007747 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007748 *
7749 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007750 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007751 * @param[in] repeat How many times this expression is repeated.
7752 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7753 * @param[in] options XPath options.
7754 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7755 */
7756static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007757eval_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 +02007758{
7759 LY_ERR rc;
7760 uint16_t this_op, i;
7761
7762 assert(repeat);
7763
7764 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007765 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007766 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007767 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 +02007768
7769 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007770 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7771 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007772 }
7773
Michal Vasko004d3152020-06-11 19:59:22 +02007774 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007775 LY_CHECK_RET(rc);
7776
7777 if (set && (repeat % 2)) {
7778 if (options & LYXP_SCNODE_ALL) {
7779 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7780 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007781 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007782 LY_CHECK_RET(rc);
7783 }
7784 }
7785
7786 return LY_SUCCESS;
7787}
7788
7789/**
7790 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7791 *
Michal Vaskod3678892020-05-21 10:06:58 +02007792 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007793 * | MultiplicativeExpr '*' UnaryExpr
7794 * | MultiplicativeExpr 'div' UnaryExpr
7795 * | MultiplicativeExpr 'mod' UnaryExpr
7796 *
7797 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007798 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007799 * @param[in] repeat How many times this expression is repeated.
7800 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7801 * @param[in] options XPath options.
7802 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7803 */
7804static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007805eval_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 +02007806{
7807 LY_ERR rc;
7808 uint16_t this_op;
7809 struct lyxp_set orig_set, set2;
7810 uint16_t i;
7811
7812 assert(repeat);
7813
7814 set_init(&orig_set, set);
7815 set_init(&set2, set);
7816
7817 set_fill_set(&orig_set, set);
7818
Michal Vasko004d3152020-06-11 19:59:22 +02007819 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007820 LY_CHECK_GOTO(rc, cleanup);
7821
7822 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7823 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007824 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007825
Michal Vasko004d3152020-06-11 19:59:22 +02007826 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007827 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007828 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7829 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007830
7831 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007832 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007833 LY_CHECK_GOTO(rc, cleanup);
7834 continue;
7835 }
7836
7837 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007838 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007839 LY_CHECK_GOTO(rc, cleanup);
7840
7841 /* eval */
7842 if (options & LYXP_SCNODE_ALL) {
7843 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007844 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007845 set_scnode_clear_ctx(set);
7846 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007847 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007848 LY_CHECK_GOTO(rc, cleanup);
7849 }
7850 }
7851
7852cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007853 lyxp_set_free_content(&orig_set);
7854 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007855 return rc;
7856}
7857
7858/**
7859 * @brief Evaluate AdditiveExpr. Logs directly on error.
7860 *
Michal Vaskod3678892020-05-21 10:06:58 +02007861 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007862 * | AdditiveExpr '+' MultiplicativeExpr
7863 * | AdditiveExpr '-' MultiplicativeExpr
7864 *
7865 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007866 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007867 * @param[in] repeat How many times this expression is repeated.
7868 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7869 * @param[in] options XPath options.
7870 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7871 */
7872static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007873eval_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 +02007874{
7875 LY_ERR rc;
7876 uint16_t this_op;
7877 struct lyxp_set orig_set, set2;
7878 uint16_t i;
7879
7880 assert(repeat);
7881
7882 set_init(&orig_set, set);
7883 set_init(&set2, set);
7884
7885 set_fill_set(&orig_set, set);
7886
Michal Vasko004d3152020-06-11 19:59:22 +02007887 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007888 LY_CHECK_GOTO(rc, cleanup);
7889
7890 /* ('+' / '-' MultiplicativeExpr)* */
7891 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007892 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007893
Michal Vasko004d3152020-06-11 19:59:22 +02007894 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007895 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007896 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7897 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007898
7899 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007900 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007901 LY_CHECK_GOTO(rc, cleanup);
7902 continue;
7903 }
7904
7905 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007906 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007907 LY_CHECK_GOTO(rc, cleanup);
7908
7909 /* eval */
7910 if (options & LYXP_SCNODE_ALL) {
7911 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007912 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007913 set_scnode_clear_ctx(set);
7914 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007915 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007916 LY_CHECK_GOTO(rc, cleanup);
7917 }
7918 }
7919
7920cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007921 lyxp_set_free_content(&orig_set);
7922 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007923 return rc;
7924}
7925
7926/**
7927 * @brief Evaluate RelationalExpr. Logs directly on error.
7928 *
Michal Vaskod3678892020-05-21 10:06:58 +02007929 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007930 * | RelationalExpr '<' AdditiveExpr
7931 * | RelationalExpr '>' AdditiveExpr
7932 * | RelationalExpr '<=' AdditiveExpr
7933 * | RelationalExpr '>=' AdditiveExpr
7934 *
7935 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007936 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007937 * @param[in] repeat How many times this expression is repeated.
7938 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7939 * @param[in] options XPath options.
7940 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7941 */
7942static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007943eval_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 +02007944{
7945 LY_ERR rc;
7946 uint16_t this_op;
7947 struct lyxp_set orig_set, set2;
7948 uint16_t i;
7949
7950 assert(repeat);
7951
7952 set_init(&orig_set, set);
7953 set_init(&set2, set);
7954
7955 set_fill_set(&orig_set, set);
7956
Michal Vasko004d3152020-06-11 19:59:22 +02007957 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007958 LY_CHECK_GOTO(rc, cleanup);
7959
7960 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
7961 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007962 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007963
Michal Vasko004d3152020-06-11 19:59:22 +02007964 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007965 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007966 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7967 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007968
7969 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007970 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007971 LY_CHECK_GOTO(rc, cleanup);
7972 continue;
7973 }
7974
7975 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007976 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007977 LY_CHECK_GOTO(rc, cleanup);
7978
7979 /* eval */
7980 if (options & LYXP_SCNODE_ALL) {
7981 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007982 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007983 set_scnode_clear_ctx(set);
7984 } else {
7985 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
7986 LY_CHECK_GOTO(rc, cleanup);
7987 }
7988 }
7989
7990cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007991 lyxp_set_free_content(&orig_set);
7992 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007993 return rc;
7994}
7995
7996/**
7997 * @brief Evaluate EqualityExpr. Logs directly on error.
7998 *
Michal Vaskod3678892020-05-21 10:06:58 +02007999 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008000 * | EqualityExpr '!=' RelationalExpr
8001 *
8002 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008003 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008004 * @param[in] repeat How many times this expression is repeated.
8005 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8006 * @param[in] options XPath options.
8007 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8008 */
8009static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02008010eval_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 +02008011{
8012 LY_ERR rc;
8013 uint16_t this_op;
8014 struct lyxp_set orig_set, set2;
8015 uint16_t i;
8016
8017 assert(repeat);
8018
8019 set_init(&orig_set, set);
8020 set_init(&set2, set);
8021
8022 set_fill_set(&orig_set, set);
8023
Michal Vasko004d3152020-06-11 19:59:22 +02008024 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008025 LY_CHECK_GOTO(rc, cleanup);
8026
8027 /* ('=' / '!=' RelationalExpr)* */
8028 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008029 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008030
Michal Vasko004d3152020-06-11 19:59:22 +02008031 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008032 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02008033 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8034 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008035
8036 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008037 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008038 LY_CHECK_GOTO(rc, cleanup);
8039 continue;
8040 }
8041
8042 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008043 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008044 LY_CHECK_GOTO(rc, cleanup);
8045
8046 /* eval */
8047 if (options & LYXP_SCNODE_ALL) {
8048 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008049 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8050 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008051 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008052 set_scnode_clear_ctx(set);
8053 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008054 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8055 LY_CHECK_GOTO(rc, cleanup);
8056 }
8057 }
8058
8059cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008060 lyxp_set_free_content(&orig_set);
8061 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008062 return rc;
8063}
8064
8065/**
8066 * @brief Evaluate AndExpr. Logs directly on error.
8067 *
Michal Vaskod3678892020-05-21 10:06:58 +02008068 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008069 *
8070 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008071 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008072 * @param[in] repeat How many times this expression is repeated.
8073 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8074 * @param[in] options XPath options.
8075 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8076 */
8077static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02008078eval_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 +02008079{
8080 LY_ERR rc;
8081 struct lyxp_set orig_set, set2;
8082 uint16_t i;
8083
8084 assert(repeat);
8085
8086 set_init(&orig_set, set);
8087 set_init(&set2, set);
8088
8089 set_fill_set(&orig_set, set);
8090
Michal Vasko004d3152020-06-11 19:59:22 +02008091 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008092 LY_CHECK_GOTO(rc, cleanup);
8093
8094 /* cast to boolean, we know that will be the final result */
8095 if (set && (options & LYXP_SCNODE_ALL)) {
8096 set_scnode_clear_ctx(set);
8097 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008098 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008099 }
8100
8101 /* ('and' EqualityExpr)* */
8102 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008103 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8104 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
8105 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8106 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008107
8108 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008109 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8110 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008111 LY_CHECK_GOTO(rc, cleanup);
8112 continue;
8113 }
8114
8115 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008116 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008117 LY_CHECK_GOTO(rc, cleanup);
8118
8119 /* eval - just get boolean value actually */
8120 if (set->type == LYXP_SET_SCNODE_SET) {
8121 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008122 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008123 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008124 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008125 set_fill_set(set, &set2);
8126 }
8127 }
8128
8129cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008130 lyxp_set_free_content(&orig_set);
8131 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008132 return rc;
8133}
8134
8135/**
8136 * @brief Evaluate OrExpr. Logs directly on error.
8137 *
Michal Vaskod3678892020-05-21 10:06:58 +02008138 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008139 *
8140 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008141 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008142 * @param[in] repeat How many times this expression is repeated.
8143 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8144 * @param[in] options XPath options.
8145 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8146 */
8147static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02008148eval_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 +02008149{
8150 LY_ERR rc;
8151 struct lyxp_set orig_set, set2;
8152 uint16_t i;
8153
8154 assert(repeat);
8155
8156 set_init(&orig_set, set);
8157 set_init(&set2, set);
8158
8159 set_fill_set(&orig_set, set);
8160
Michal Vasko004d3152020-06-11 19:59:22 +02008161 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008162 LY_CHECK_GOTO(rc, cleanup);
8163
8164 /* cast to boolean, we know that will be the final result */
8165 if (set && (options & LYXP_SCNODE_ALL)) {
8166 set_scnode_clear_ctx(set);
8167 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008168 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008169 }
8170
8171 /* ('or' AndExpr)* */
8172 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008173 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8174 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
8175 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8176 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008177
8178 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008179 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8180 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008181 LY_CHECK_GOTO(rc, cleanup);
8182 continue;
8183 }
8184
8185 set_fill_set(&set2, &orig_set);
8186 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8187 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008188 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008189 LY_CHECK_GOTO(rc, cleanup);
8190
8191 /* eval - just get boolean value actually */
8192 if (set->type == LYXP_SET_SCNODE_SET) {
8193 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008194 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008195 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008196 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008197 set_fill_set(set, &set2);
8198 }
8199 }
8200
8201cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008202 lyxp_set_free_content(&orig_set);
8203 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008204 return rc;
8205}
8206
8207/**
Michal Vasko004d3152020-06-11 19:59:22 +02008208 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008209 *
8210 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008211 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008212 * @param[in] etype Expression type to evaluate.
8213 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8214 * @param[in] options XPath options.
8215 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8216 */
8217static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02008218eval_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 +02008219{
8220 uint16_t i, count;
8221 enum lyxp_expr_type next_etype;
8222 LY_ERR rc;
8223
8224 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008225 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008226 next_etype = LYXP_EXPR_NONE;
8227 } else {
8228 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008229 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008230
8231 /* select one-priority lower because etype expression called us */
8232 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008233 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008234 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008235 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008236 } else {
8237 next_etype = LYXP_EXPR_NONE;
8238 }
8239 }
8240
8241 /* decide what expression are we parsing based on the repeat */
8242 switch (next_etype) {
8243 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008244 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008245 break;
8246 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008247 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008248 break;
8249 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008250 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008251 break;
8252 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008253 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008254 break;
8255 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008256 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008257 break;
8258 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008259 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008260 break;
8261 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008262 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008263 break;
8264 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008265 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008266 break;
8267 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008268 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008269 break;
8270 default:
8271 LOGINT_RET(set->ctx);
8272 }
8273
8274 return rc;
8275}
8276
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008277/**
8278 * @brief Get root type.
8279 *
8280 * @param[in] ctx_node Context node.
8281 * @param[in] ctx_scnode Schema context node.
8282 * @param[in] options XPath options.
8283 * @return Root type.
8284 */
8285static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008286lyxp_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 +01008287{
Michal Vasko6b26e742020-07-17 15:02:10 +02008288 const struct lysc_node *op;
8289
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008290 if (options & LYXP_SCNODE_ALL) {
Radek Krejci1e008d22020-08-17 11:37:37 +02008291 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008292
8293 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008294 /* general root that can access everything */
8295 return LYXP_NODE_ROOT;
8296 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8297 /* root context node can access only config data (because we said so, it is unspecified) */
8298 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008299 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008300 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008301 }
8302
Michal Vasko6b26e742020-07-17 15:02:10 +02008303 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008304 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008305
8306 if (!ctx_node || (!op && (ctx_node->schema->flags & LYS_CONFIG_W))) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008307 /* root context node can access only config data (because we said so, it is unspecified) */
8308 return LYXP_NODE_ROOT_CONFIG;
8309 }
8310
8311 return LYXP_NODE_ROOT;
8312}
8313
Michal Vasko03ff5a72019-09-11 13:49:33 +02008314LY_ERR
Michal Vaskoc8a230d2020-08-14 12:17:10 +02008315lyxp_eval(struct lyxp_expr *exp, LY_PREFIX_FORMAT format, const struct lys_module *local_mod, const struct lyd_node *ctx_node,
Radek Krejci1deb5be2020-08-26 16:43:36 +02008316 enum lyxp_node_type ctx_node_type, const struct lyd_node *tree, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008317{
Michal Vasko004d3152020-06-11 19:59:22 +02008318 uint16_t tok_idx = 0;
8319 const struct lyd_node *real_ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008320 LY_ERR rc;
8321
Michal Vasko004d3152020-06-11 19:59:22 +02008322 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL);
8323
8324 if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) {
8325 /* we always need some context node because it is used for resolving unqualified names */
8326 real_ctx_node = NULL;
8327 } else {
8328 real_ctx_node = ctx_node;
8329 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008330
8331 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008332 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008333 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008334 set->type = LYXP_SET_NODE_SET;
Michal Vasko004d3152020-06-11 19:59:22 +02008335 set_insert_node(set, (struct lyd_node *)real_ctx_node, 0, ctx_node_type, 0);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008336 set->ctx = local_mod->ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008337 set->ctx_node = ctx_node;
Michal Vasko004d3152020-06-11 19:59:22 +02008338 set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options);
Michal Vasko6b26e742020-07-17 15:02:10 +02008339 for (set->context_op = ctx_node->schema;
Radek Krejci0f969882020-08-21 16:56:47 +02008340 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8341 set->context_op = set->context_op->parent) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008342 set->local_mod = local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +01008343 set->tree = tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008344 set->format = format;
8345
8346 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008347 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008348 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008349 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008350 }
8351
Michal Vasko03ff5a72019-09-11 13:49:33 +02008352 return rc;
8353}
8354
8355#if 0
8356
8357/* full xml printing of set elements, not used currently */
8358
8359void
8360lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8361{
8362 uint32_t i;
8363 char *str_num;
8364 struct lyout out;
8365
8366 memset(&out, 0, sizeof out);
8367
8368 out.type = LYOUT_STREAM;
8369 out.method.f = f;
8370
8371 switch (set->type) {
8372 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008373 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008374 break;
8375 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008376 ly_print_(&out, "Boolean XPath set:\n");
8377 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008378 break;
8379 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008380 ly_print_(&out, "String XPath set:\n");
8381 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008382 break;
8383 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008384 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008385
8386 if (isnan(set->value.num)) {
8387 str_num = strdup("NaN");
8388 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8389 str_num = strdup("0");
8390 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8391 str_num = strdup("Infinity");
8392 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8393 str_num = strdup("-Infinity");
8394 } else if ((long long)set->value.num == set->value.num) {
8395 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8396 str_num = NULL;
8397 }
8398 } else {
8399 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8400 str_num = NULL;
8401 }
8402 }
8403 if (!str_num) {
8404 LOGMEM;
8405 return;
8406 }
Michal Vasko5233e962020-08-14 14:26:20 +02008407 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008408 free(str_num);
8409 break;
8410 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008411 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008412
8413 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008414 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008415 switch (set->node_type[i]) {
8416 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008417 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008418 break;
8419 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008420 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008421 break;
8422 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008423 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008424 break;
8425 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008426 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008427 break;
8428 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008429 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008430 break;
8431 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008432 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008433 break;
8434 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008435 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008436 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008437 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008438 break;
8439 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008440 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 +02008441 break;
8442 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008443 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 +02008444 break;
8445 }
8446 }
8447 break;
8448 }
8449}
8450
8451#endif
8452
8453LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008454lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008455{
8456 long double num;
8457 char *str;
8458 LY_ERR rc;
8459
8460 if (!set || (set->type == target)) {
8461 return LY_SUCCESS;
8462 }
8463
8464 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008465 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008466
8467 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008468 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008469 return LY_EINVAL;
8470 }
8471
8472 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008473 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008474 switch (set->type) {
8475 case LYXP_SET_NUMBER:
8476 if (isnan(set->val.num)) {
8477 set->val.str = strdup("NaN");
8478 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8479 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8480 set->val.str = strdup("0");
8481 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8482 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8483 set->val.str = strdup("Infinity");
8484 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8485 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8486 set->val.str = strdup("-Infinity");
8487 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8488 } else if ((long long)set->val.num == set->val.num) {
8489 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8490 LOGMEM_RET(set->ctx);
8491 }
8492 set->val.str = str;
8493 } else {
8494 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8495 LOGMEM_RET(set->ctx);
8496 }
8497 set->val.str = str;
8498 }
8499 break;
8500 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008501 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008502 set->val.str = strdup("true");
8503 } else {
8504 set->val.str = strdup("false");
8505 }
8506 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8507 break;
8508 case LYXP_SET_NODE_SET:
8509 assert(set->used);
8510
8511 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008512 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008513
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008514 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008515 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008516 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008517 set->val.str = str;
8518 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008519 default:
8520 LOGINT_RET(set->ctx);
8521 }
8522 set->type = LYXP_SET_STRING;
8523 }
8524
8525 /* to NUMBER */
8526 if (target == LYXP_SET_NUMBER) {
8527 switch (set->type) {
8528 case LYXP_SET_STRING:
8529 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008530 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008531 set->val.num = num;
8532 break;
8533 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008534 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008535 set->val.num = 1;
8536 } else {
8537 set->val.num = 0;
8538 }
8539 break;
8540 default:
8541 LOGINT_RET(set->ctx);
8542 }
8543 set->type = LYXP_SET_NUMBER;
8544 }
8545
8546 /* to BOOLEAN */
8547 if (target == LYXP_SET_BOOLEAN) {
8548 switch (set->type) {
8549 case LYXP_SET_NUMBER:
8550 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008551 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008552 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008553 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008554 }
8555 break;
8556 case LYXP_SET_STRING:
8557 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008558 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008559 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008560 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008561 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008562 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008563 }
8564 break;
8565 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008566 if (set->used) {
8567 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008568 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008569 } else {
8570 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008571 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008572 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008573 break;
8574 default:
8575 LOGINT_RET(set->ctx);
8576 }
8577 set->type = LYXP_SET_BOOLEAN;
8578 }
8579
Michal Vasko03ff5a72019-09-11 13:49:33 +02008580 return LY_SUCCESS;
8581}
8582
8583LY_ERR
Michal Vaskoc8a230d2020-08-14 12:17:10 +02008584lyxp_atomize(struct lyxp_expr *exp, LY_PREFIX_FORMAT format, const struct lys_module *local_mod,
Radek Krejci1deb5be2020-08-26 16:43:36 +02008585 const struct lysc_node *ctx_scnode, enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008586{
8587 struct ly_ctx *ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008588 const struct lysc_node *real_ctx_scnode;
8589 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008590
Michal Vasko004d3152020-06-11 19:59:22 +02008591 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_scnode, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008592
8593 ctx = local_mod->ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008594 if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) {
8595 /* we always need some context node because it is used for resolving unqualified names */
8596 real_ctx_scnode = NULL;
8597 } else {
8598 real_ctx_scnode = ctx_scnode;
8599 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008600
8601 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008602 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008603 memset(set, 0, sizeof *set);
8604 set->type = LYXP_SET_SCNODE_SET;
Radek Krejciaa6b53f2020-08-27 15:19:03 +02008605 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, real_ctx_scnode, ctx_scnode_type, NULL));
Michal Vasko5c4e5892019-11-14 12:31:38 +01008606 set->val.scnodes[0].in_ctx = -2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008607 set->ctx = ctx;
8608 set->ctx_scnode = ctx_scnode;
Michal Vasko004d3152020-06-11 19:59:22 +02008609 set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options);
Michal Vasko6b26e742020-07-17 15:02:10 +02008610 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008611 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8612 set->context_op = set->context_op->parent) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008613 set->local_mod = local_mod;
8614 set->format = format;
8615
8616 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008617 return eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008618}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008619
8620API const char *
8621lyxp_get_expr(const struct lyxp_expr *path)
8622{
8623 if (!path) {
8624 return NULL;
8625 }
8626
8627 return path->expr;
8628}