blob: c20a384c6920487d2671ed5adf1bab78e359a000 [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 Krejci857189e2020-09-01 13:26:36 +0200321cast_string_recursive(const struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
Radek Krejci0f969882020-08-21 16:56:47 +0200322 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200323{
Radek Krejci7f769d72020-07-11 23:13:56 +0200324 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200325 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200326 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200327 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200328 struct lyd_node_any *any;
329 LY_ERR rc;
330
331 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
332 return LY_SUCCESS;
333 }
334
335 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200336 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200337 LY_CHECK_RET(rc);
338 strcpy(*str + (*used - 1), "\n");
339 ++(*used);
340
341 ++indent;
342 }
343
344 switch (node->schema->nodetype) {
345 case LYS_CONTAINER:
346 case LYS_LIST:
347 case LYS_RPC:
348 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200349 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200350 LY_CHECK_RET(rc);
351 strcpy(*str + (*used - 1), "\n");
352 ++(*used);
353
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 Krejci857189e2020-09-01 13:26:36 +0200467cast_string_elem(struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200468{
469 uint16_t used, size;
470 LY_ERR rc;
471
472 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200473 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200474 (*str)[0] = '\0';
475 used = 1;
476 size = LYXP_STRING_CAST_SIZE_START;
477
478 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
479 if (rc != LY_SUCCESS) {
480 free(*str);
481 return rc;
482 }
483
484 if (size > used) {
485 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200486 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200487 }
488 return LY_SUCCESS;
489}
490
491/**
492 * @brief Cast a LYXP_SET_NODE_SET set into a string.
493 * Context position aware.
494 *
495 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200496 * @param[out] str Cast dynamically-allocated string.
497 * @return LY_ERR
498 */
499static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100500cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200501{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200502 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100503 case LYXP_NODE_NONE:
504 /* invalid */
505 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200506 case LYXP_NODE_ROOT:
507 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100508 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200509 case LYXP_NODE_ELEM:
510 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100511 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100512 case LYXP_NODE_META:
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200513 *str = strdup(set->val.meta[0].meta->value.canonical);
514 if (!*str) {
515 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200516 }
517 return LY_SUCCESS;
518 }
519
520 LOGINT_RET(set->ctx);
521}
522
523/**
524 * @brief Cast a string into an XPath number.
525 *
526 * @param[in] str String to use.
527 * @return Cast number.
528 */
529static long double
530cast_string_to_number(const char *str)
531{
532 long double num;
533 char *ptr;
534
535 errno = 0;
536 num = strtold(str, &ptr);
537 if (errno || *ptr) {
538 num = NAN;
539 }
540 return num;
541}
542
543/**
544 * @brief Callback for checking value equality.
545 *
Radek Krejci857189e2020-09-01 13:26:36 +0200546 * Implementation of ::values_equal_cb.
547 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200548 * @param[in] val1_p First value.
549 * @param[in] val2_p Second value.
550 * @param[in] mod Whether hash table is being modified.
551 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200552 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200553 */
Radek Krejci857189e2020-09-01 13:26:36 +0200554static ly_bool
555set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200556{
557 struct lyxp_set_hash_node *val1, *val2;
558
559 val1 = (struct lyxp_set_hash_node *)val1_p;
560 val2 = (struct lyxp_set_hash_node *)val2_p;
561
562 if ((val1->node == val2->node) && (val1->type == val2->type)) {
563 return 1;
564 }
565
566 return 0;
567}
568
569/**
570 * @brief Insert node and its hash into set.
571 *
572 * @param[in] set et to insert to.
573 * @param[in] node Node with hash.
574 * @param[in] type Node type.
575 */
576static void
577set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
578{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200579 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200580 uint32_t i, hash;
581 struct lyxp_set_hash_node hnode;
582
583 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
584 /* create hash table and add all the nodes */
585 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
586 for (i = 0; i < set->used; ++i) {
587 hnode.node = set->val.nodes[i].node;
588 hnode.type = set->val.nodes[i].type;
589
590 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
591 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
592 hash = dict_hash_multi(hash, NULL, 0);
593
594 r = lyht_insert(set->ht, &hnode, hash, NULL);
595 assert(!r);
596 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200597
Michal Vasko9d6befd2019-12-11 14:56:56 +0100598 if (hnode.node == node) {
599 /* it was just added, do not add it twice */
600 node = NULL;
601 }
602 }
603 }
604
605 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200606 /* add the new node into hash table */
607 hnode.node = node;
608 hnode.type = type;
609
610 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
611 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
612 hash = dict_hash_multi(hash, NULL, 0);
613
614 r = lyht_insert(set->ht, &hnode, hash, NULL);
615 assert(!r);
616 (void)r;
617 }
618}
619
620/**
621 * @brief Remove node and its hash from set.
622 *
623 * @param[in] set Set to remove from.
624 * @param[in] node Node to remove.
625 * @param[in] type Node type.
626 */
627static void
628set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
629{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200630 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200631 struct lyxp_set_hash_node hnode;
632 uint32_t hash;
633
634 if (set->ht) {
635 hnode.node = node;
636 hnode.type = type;
637
638 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
639 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
640 hash = dict_hash_multi(hash, NULL, 0);
641
642 r = lyht_remove(set->ht, &hnode, hash);
643 assert(!r);
644 (void)r;
645
646 if (!set->ht->used) {
647 lyht_free(set->ht);
648 set->ht = NULL;
649 }
650 }
651}
652
653/**
654 * @brief Check whether node is in set based on its hash.
655 *
656 * @param[in] set Set to search in.
657 * @param[in] node Node to search for.
658 * @param[in] type Node type.
659 * @param[in] skip_idx Index in @p set to skip.
660 * @return LY_ERR
661 */
662static LY_ERR
663set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
664{
665 struct lyxp_set_hash_node hnode, *match_p;
666 uint32_t hash;
667
668 hnode.node = node;
669 hnode.type = type;
670
671 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
672 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
673 hash = dict_hash_multi(hash, NULL, 0);
674
675 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
676 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
677 /* we found it on the index that should be skipped, find another */
678 hnode = *match_p;
679 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
680 /* none other found */
681 return LY_SUCCESS;
682 }
683 }
684
685 return LY_EEXIST;
686 }
687
688 /* not found */
689 return LY_SUCCESS;
690}
691
Michal Vaskod3678892020-05-21 10:06:58 +0200692void
693lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200694{
695 if (!set) {
696 return;
697 }
698
699 if (set->type == LYXP_SET_NODE_SET) {
700 free(set->val.nodes);
701 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200702 } else if (set->type == LYXP_SET_SCNODE_SET) {
703 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200704 lyht_free(set->ht);
705 } else {
706 if (set->type == LYXP_SET_STRING) {
707 free(set->val.str);
708 }
709 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200710 }
Michal Vaskod3678892020-05-21 10:06:58 +0200711
712 set->val.nodes = NULL;
713 set->used = 0;
714 set->size = 0;
715 set->ht = NULL;
716 set->ctx_pos = 0;
717 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200718}
719
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100720/**
721 * @brief Free dynamically-allocated set.
722 *
723 * @param[in] set Set to free.
724 */
725static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200726lyxp_set_free(struct lyxp_set *set)
727{
728 if (!set) {
729 return;
730 }
731
Michal Vaskod3678892020-05-21 10:06:58 +0200732 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200733 free(set);
734}
735
736/**
737 * @brief Initialize set context.
738 *
739 * @param[in] new Set to initialize.
740 * @param[in] set Arbitrary initialized set.
741 */
742static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200743set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200744{
745 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200746 if (set) {
747 new->ctx = set->ctx;
748 new->ctx_node = set->ctx_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100749 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200750 new->context_op = set->context_op;
Michal Vasko02a77382019-09-12 11:47:35 +0200751 new->local_mod = set->local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +0100752 new->tree = set->tree;
Michal Vasko02a77382019-09-12 11:47:35 +0200753 new->format = set->format;
754 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200755}
756
757/**
758 * @brief Create a deep copy of a set.
759 *
760 * @param[in] set Set to copy.
761 * @return Copy of @p set.
762 */
763static struct lyxp_set *
764set_copy(struct lyxp_set *set)
765{
766 struct lyxp_set *ret;
767 uint16_t i;
768
769 if (!set) {
770 return NULL;
771 }
772
773 ret = malloc(sizeof *ret);
774 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
775 set_init(ret, set);
776
777 if (set->type == LYXP_SET_SCNODE_SET) {
778 ret->type = set->type;
779
780 for (i = 0; i < set->used; ++i) {
Michal Vaskoba716542019-12-16 10:01:58 +0100781 if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200782 uint32_t idx;
783 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
784 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100785 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200786 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200787 lyxp_set_free(ret);
788 return NULL;
789 }
Michal Vaskoba716542019-12-16 10:01:58 +0100790 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200791 }
792 }
793 } else if (set->type == LYXP_SET_NODE_SET) {
794 ret->type = set->type;
795 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
796 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
797 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
798
799 ret->used = ret->size = set->used;
800 ret->ctx_pos = set->ctx_pos;
801 ret->ctx_size = set->ctx_size;
802 ret->ht = lyht_dup(set->ht);
803 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200804 memcpy(ret, set, sizeof *ret);
805 if (set->type == LYXP_SET_STRING) {
806 ret->val.str = strdup(set->val.str);
807 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
808 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200809 }
810
811 return ret;
812}
813
814/**
815 * @brief Fill XPath set with a string. Any current data are disposed of.
816 *
817 * @param[in] set Set to fill.
818 * @param[in] string String to fill into \p set.
819 * @param[in] str_len Length of \p string. 0 is a valid value!
820 */
821static void
822set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
823{
Michal Vaskod3678892020-05-21 10:06:58 +0200824 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200825
826 set->type = LYXP_SET_STRING;
827 if ((str_len == 0) && (string[0] != '\0')) {
828 string = "";
829 }
830 set->val.str = strndup(string, str_len);
831}
832
833/**
834 * @brief Fill XPath set with a number. Any current data are disposed of.
835 *
836 * @param[in] set Set to fill.
837 * @param[in] number Number to fill into \p set.
838 */
839static void
840set_fill_number(struct lyxp_set *set, long double number)
841{
Michal Vaskod3678892020-05-21 10:06:58 +0200842 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200843
844 set->type = LYXP_SET_NUMBER;
845 set->val.num = number;
846}
847
848/**
849 * @brief Fill XPath set with a boolean. Any current data are disposed of.
850 *
851 * @param[in] set Set to fill.
852 * @param[in] boolean Boolean to fill into \p set.
853 */
854static void
Radek Krejci857189e2020-09-01 13:26:36 +0200855set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200856{
Michal Vaskod3678892020-05-21 10:06:58 +0200857 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200858
859 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200860 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200861}
862
863/**
864 * @brief Fill XPath set with the value from another set (deep assign).
865 * Any current data are disposed of.
866 *
867 * @param[in] trg Set to fill.
868 * @param[in] src Source set to copy into \p trg.
869 */
870static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200871set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200872{
873 if (!trg || !src) {
874 return;
875 }
876
877 if (trg->type == LYXP_SET_NODE_SET) {
878 free(trg->val.nodes);
879 } else if (trg->type == LYXP_SET_STRING) {
880 free(trg->val.str);
881 }
882 set_init(trg, src);
883
884 if (src->type == LYXP_SET_SCNODE_SET) {
885 trg->type = LYXP_SET_SCNODE_SET;
886 trg->used = src->used;
887 trg->size = src->used;
888
889 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
890 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
891 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
892 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200893 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200894 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200895 set_fill_number(trg, src->val.num);
896 } else if (src->type == LYXP_SET_STRING) {
897 set_fill_string(trg, src->val.str, strlen(src->val.str));
898 } else {
899 if (trg->type == LYXP_SET_NODE_SET) {
900 free(trg->val.nodes);
901 } else if (trg->type == LYXP_SET_STRING) {
902 free(trg->val.str);
903 }
904
Michal Vaskod3678892020-05-21 10:06:58 +0200905 assert(src->type == LYXP_SET_NODE_SET);
906
907 trg->type = LYXP_SET_NODE_SET;
908 trg->used = src->used;
909 trg->size = src->used;
910 trg->ctx_pos = src->ctx_pos;
911 trg->ctx_size = src->ctx_size;
912
913 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
914 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
915 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
916 if (src->ht) {
917 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200918 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200919 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200920 }
921 }
922}
923
924/**
925 * @brief Clear context of all schema nodes.
926 *
927 * @param[in] set Set to clear.
928 */
929static void
930set_scnode_clear_ctx(struct lyxp_set *set)
931{
932 uint32_t i;
933
934 for (i = 0; i < set->used; ++i) {
935 if (set->val.scnodes[i].in_ctx == 1) {
936 set->val.scnodes[i].in_ctx = 0;
Michal Vasko5c4e5892019-11-14 12:31:38 +0100937 } else if (set->val.scnodes[i].in_ctx == -2) {
938 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200939 }
940 }
941}
942
943/**
944 * @brief Remove a node from a set. Removing last node changes
945 * set into LYXP_SET_EMPTY. Context position aware.
946 *
947 * @param[in] set Set to use.
948 * @param[in] idx Index from @p set of the node to be removed.
949 */
950static void
951set_remove_node(struct lyxp_set *set, uint32_t idx)
952{
953 assert(set && (set->type == LYXP_SET_NODE_SET));
954 assert(idx < set->used);
955
956 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
957
958 --set->used;
959 if (set->used) {
960 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
961 (set->used - idx) * sizeof *set->val.nodes);
962 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200963 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200964 }
965}
966
967/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100968 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200969 *
970 * @param[in] set Set to use.
971 * @param[in] idx Index from @p set of the node to be removed.
972 */
973static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100974set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200975{
976 assert(set && (set->type == LYXP_SET_NODE_SET));
977 assert(idx < set->used);
978
Michal Vasko2caefc12019-11-14 16:07:56 +0100979 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
980 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
981 }
982 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +0200983}
984
985/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100986 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +0200987 * set into LYXP_SET_EMPTY. Context position aware.
988 *
989 * @param[in] set Set to consolidate.
990 */
991static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100992set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +0200993{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +0200994 uint16_t i, orig_used, end = 0;
Michal Vasko57eab132019-09-24 11:46:26 +0200995 int32_t start;
996
Michal Vaskod3678892020-05-21 10:06:58 +0200997 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +0200998
999 orig_used = set->used;
1000 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001001 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001002 start = -1;
1003 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001004 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001005 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001006 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001007 end = i;
1008 ++i;
1009 break;
1010 }
1011
1012 ++i;
1013 if (i == orig_used) {
1014 end = i;
1015 }
1016 } while (i < orig_used);
1017
1018 if (start > -1) {
1019 /* move the whole chunk of valid nodes together */
1020 if (set->used != (unsigned)start) {
1021 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1022 }
1023 set->used += end - start;
1024 }
1025 }
Michal Vasko57eab132019-09-24 11:46:26 +02001026}
1027
1028/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001029 * @brief Check for duplicates in a node set.
1030 *
1031 * @param[in] set Set to check.
1032 * @param[in] node Node to look for in @p set.
1033 * @param[in] node_type Type of @p node.
1034 * @param[in] skip_idx Index from @p set to skip.
1035 * @return LY_ERR
1036 */
1037static LY_ERR
1038set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1039{
1040 uint32_t i;
1041
Michal Vasko2caefc12019-11-14 16:07:56 +01001042 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001043 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1044 }
1045
1046 for (i = 0; i < set->used; ++i) {
1047 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1048 continue;
1049 }
1050
1051 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1052 return LY_EEXIST;
1053 }
1054 }
1055
1056 return LY_SUCCESS;
1057}
1058
Radek Krejci857189e2020-09-01 13:26:36 +02001059ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001060lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1061 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001062{
1063 uint32_t i;
1064
1065 for (i = 0; i < set->used; ++i) {
1066 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1067 continue;
1068 }
1069
1070 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001071 if (index_p) {
1072 *index_p = i;
1073 }
1074 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001075 }
1076 }
1077
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001078 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001079}
1080
Michal Vaskoecd62de2019-11-13 12:35:11 +01001081void
1082lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001083{
1084 uint32_t orig_used, i, j;
1085
Michal Vaskod3678892020-05-21 10:06:58 +02001086 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001087
Michal Vaskod3678892020-05-21 10:06:58 +02001088 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001089 return;
1090 }
1091
Michal Vaskod3678892020-05-21 10:06:58 +02001092 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001093 memcpy(set1, set2, sizeof *set1);
1094 return;
1095 }
1096
1097 if (set1->used + set2->used > set1->size) {
1098 set1->size = set1->used + set2->used;
1099 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1100 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1101 }
1102
1103 orig_used = set1->used;
1104
1105 for (i = 0; i < set2->used; ++i) {
1106 for (j = 0; j < orig_used; ++j) {
1107 /* detect duplicities */
1108 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1109 break;
1110 }
1111 }
1112
1113 if (j == orig_used) {
1114 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1115 ++set1->used;
1116 }
1117 }
1118
Michal Vaskod3678892020-05-21 10:06:58 +02001119 lyxp_set_free_content(set2);
1120 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001121}
1122
1123/**
1124 * @brief Insert a node into a set. Context position aware.
1125 *
1126 * @param[in] set Set to use.
1127 * @param[in] node Node to insert to @p set.
1128 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1129 * @param[in] node_type Node type of @p node.
1130 * @param[in] idx Index in @p set to insert into.
1131 */
1132static void
1133set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1134{
Michal Vaskod3678892020-05-21 10:06:58 +02001135 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001136
Michal Vaskod3678892020-05-21 10:06:58 +02001137 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001138 /* first item */
1139 if (idx) {
1140 /* no real harm done, but it is a bug */
1141 LOGINT(set->ctx);
1142 idx = 0;
1143 }
1144 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1145 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1146 set->type = LYXP_SET_NODE_SET;
1147 set->used = 0;
1148 set->size = LYXP_SET_SIZE_START;
1149 set->ctx_pos = 1;
1150 set->ctx_size = 1;
1151 set->ht = NULL;
1152 } else {
1153 /* not an empty set */
1154 if (set->used == set->size) {
1155
1156 /* set is full */
1157 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1158 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1159 set->size += LYXP_SET_SIZE_STEP;
1160 }
1161
1162 if (idx > set->used) {
1163 LOGINT(set->ctx);
1164 idx = set->used;
1165 }
1166
1167 /* make space for the new node */
1168 if (idx < set->used) {
1169 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1170 }
1171 }
1172
1173 /* finally assign the value */
1174 set->val.nodes[idx].node = (struct lyd_node *)node;
1175 set->val.nodes[idx].type = node_type;
1176 set->val.nodes[idx].pos = pos;
1177 ++set->used;
1178
Michal Vasko2caefc12019-11-14 16:07:56 +01001179 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1180 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1181 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001182}
1183
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001184LY_ERR
1185lyxp_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 +02001186{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001187 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001188
1189 assert(set->type == LYXP_SET_SCNODE_SET);
1190
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001191 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
1192 set->val.scnodes[index].in_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001193 } else {
1194 if (set->used == set->size) {
1195 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 +02001196 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001197 set->size += LYXP_SET_SIZE_STEP;
1198 }
1199
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001200 index = set->used;
1201 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1202 set->val.scnodes[index].type = node_type;
1203 set->val.scnodes[index].in_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001204 ++set->used;
1205 }
1206
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001207 if (index_p) {
1208 *index_p = index;
1209 }
1210
1211 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001212}
1213
1214/**
1215 * @brief Replace a node in a set with another. Context position aware.
1216 *
1217 * @param[in] set Set to use.
1218 * @param[in] node Node to insert to @p set.
1219 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1220 * @param[in] node_type Node type of @p node.
1221 * @param[in] idx Index in @p set of the node to replace.
1222 */
1223static void
1224set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1225{
1226 assert(set && (idx < set->used));
1227
Michal Vasko2caefc12019-11-14 16:07:56 +01001228 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1229 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1230 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001231 set->val.nodes[idx].node = (struct lyd_node *)node;
1232 set->val.nodes[idx].type = node_type;
1233 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001234 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1235 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1236 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001237}
1238
1239/**
1240 * @brief Set all nodes with ctx 1 to a new unique context value.
1241 *
1242 * @param[in] set Set to modify.
1243 * @return New context value.
1244 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001245static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001246set_scnode_new_in_ctx(struct lyxp_set *set)
1247{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001248 uint32_t i;
1249 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001250
1251 assert(set->type == LYXP_SET_SCNODE_SET);
1252
1253 ret_ctx = 3;
1254retry:
1255 for (i = 0; i < set->used; ++i) {
1256 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1257 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1258 goto retry;
1259 }
1260 }
1261 for (i = 0; i < set->used; ++i) {
1262 if (set->val.scnodes[i].in_ctx == 1) {
1263 set->val.scnodes[i].in_ctx = ret_ctx;
1264 }
1265 }
1266
1267 return ret_ctx;
1268}
1269
1270/**
1271 * @brief Get unique @p node position in the data.
1272 *
1273 * @param[in] node Node to find.
1274 * @param[in] node_type Node type of @p node.
1275 * @param[in] root Root node.
1276 * @param[in] root_type Type of the XPath @p root node.
1277 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1278 * be used to increase efficiency and start the DFS from this node.
1279 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1280 * @return Node position.
1281 */
1282static uint32_t
1283get_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 +02001284 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001285{
Michal Vasko56daf732020-08-10 10:57:18 +02001286 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001287 uint32_t pos = 1;
1288
1289 assert(prev && prev_pos && !root->prev->next);
1290
1291 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1292 return 0;
1293 }
1294
1295 if (*prev) {
1296 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001297 pos = *prev_pos;
Radek Krejci1e008d22020-08-17 11:37:37 +02001298 for (top_sibling = *prev; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001299 goto dfs_search;
1300 }
1301
1302 for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) {
Michal Vasko56daf732020-08-10 10:57:18 +02001303 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001304dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001305 if (*prev && !elem) {
1306 /* resume previous DFS */
1307 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1308 LYD_TREE_DFS_continue = 0;
1309 }
1310
Michal Vasko03ff5a72019-09-11 13:49:33 +02001311 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001312 /* skip */
1313 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001314 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001315 if (elem == node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001316 break;
1317 }
Michal Vasko56daf732020-08-10 10:57:18 +02001318 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001319 }
Michal Vasko56daf732020-08-10 10:57:18 +02001320
1321 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001322 }
1323
1324 /* node found */
1325 if (elem) {
1326 break;
1327 }
1328 }
1329
1330 if (!elem) {
1331 if (!(*prev)) {
1332 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001333 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001334 return 0;
1335 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001336 /* start the search again from the beginning */
1337 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001338
Michal Vasko56daf732020-08-10 10:57:18 +02001339 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001340 pos = 1;
1341 goto dfs_search;
1342 }
1343 }
1344
1345 /* remember the last found node for next time */
1346 *prev = node;
1347 *prev_pos = pos;
1348
1349 return pos;
1350}
1351
1352/**
1353 * @brief Assign (fill) missing node positions.
1354 *
1355 * @param[in] set Set to fill positions in.
1356 * @param[in] root Context root node.
1357 * @param[in] root_type Context root type.
1358 * @return LY_ERR
1359 */
1360static LY_ERR
1361set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1362{
1363 const struct lyd_node *prev = NULL, *tmp_node;
1364 uint32_t i, tmp_pos = 0;
1365
1366 for (i = 0; i < set->used; ++i) {
1367 if (!set->val.nodes[i].pos) {
1368 tmp_node = NULL;
1369 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001370 case LYXP_NODE_META:
1371 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001372 if (!tmp_node) {
1373 LOGINT_RET(root->schema->module->ctx);
1374 }
Radek Krejci0f969882020-08-21 16:56:47 +02001375 /* fallthrough */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001376 case LYXP_NODE_ELEM:
1377 case LYXP_NODE_TEXT:
1378 if (!tmp_node) {
1379 tmp_node = set->val.nodes[i].node;
1380 }
1381 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1382 break;
1383 default:
1384 /* all roots have position 0 */
1385 break;
1386 }
1387 }
1388 }
1389
1390 return LY_SUCCESS;
1391}
1392
1393/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001394 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001395 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001396 * @param[in] meta Metadata to use.
1397 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001398 */
1399static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001400get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001401{
1402 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001403 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001404
Michal Vasko9f96a052020-03-10 09:41:45 +01001405 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001406 ++pos;
1407 }
1408
Michal Vasko9f96a052020-03-10 09:41:45 +01001409 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001410 return pos;
1411}
1412
1413/**
1414 * @brief Compare 2 nodes in respect to XPath document order.
1415 *
1416 * @param[in] item1 1st node.
1417 * @param[in] item2 2nd node.
1418 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1419 */
1420static int
1421set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1422{
Michal Vasko9f96a052020-03-10 09:41:45 +01001423 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001424
1425 if (item1->pos < item2->pos) {
1426 return -1;
1427 }
1428
1429 if (item1->pos > item2->pos) {
1430 return 1;
1431 }
1432
1433 /* node positions are equal, the fun case */
1434
1435 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1436 /* special case since text nodes are actually saved as their parents */
1437 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1438 if (item1->type == LYXP_NODE_ELEM) {
1439 assert(item2->type == LYXP_NODE_TEXT);
1440 return -1;
1441 } else {
1442 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1443 return 1;
1444 }
1445 }
1446
Michal Vasko9f96a052020-03-10 09:41:45 +01001447 /* we need meta positions now */
1448 if (item1->type == LYXP_NODE_META) {
1449 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001450 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001451 if (item2->type == LYXP_NODE_META) {
1452 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001453 }
1454
Michal Vasko9f96a052020-03-10 09:41:45 +01001455 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001456 /* check for duplicates */
1457 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001458 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001459 return 0;
1460 }
1461
Michal Vasko9f96a052020-03-10 09:41:45 +01001462 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001463 /* elem is always first, 2nd node is after it */
1464 if (item1->type == LYXP_NODE_ELEM) {
1465 assert(item2->type != LYXP_NODE_ELEM);
1466 return -1;
1467 }
1468
Michal Vasko9f96a052020-03-10 09:41:45 +01001469 /* 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 +02001470 /* 2nd is before 1st */
1471 if (((item1->type == LYXP_NODE_TEXT)
Michal Vasko9f96a052020-03-10 09:41:45 +01001472 && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META)))
1473 || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM))
1474 || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META))
1475 && (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001476 return 1;
1477 }
1478
Michal Vasko9f96a052020-03-10 09:41:45 +01001479 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001480 /* 2nd is after 1st */
1481 return -1;
1482}
1483
1484/**
1485 * @brief Set cast for comparisons.
1486 *
1487 * @param[in] trg Target set to cast source into.
1488 * @param[in] src Source set.
1489 * @param[in] type Target set type.
1490 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001491 * @return LY_ERR
1492 */
1493static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001494set_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 +02001495{
1496 assert(src->type == LYXP_SET_NODE_SET);
1497
1498 set_init(trg, src);
1499
1500 /* insert node into target set */
1501 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1502
1503 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001504 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001505}
1506
Michal Vasko4c7763f2020-07-27 17:40:37 +02001507/**
1508 * @brief Set content canonization for comparisons.
1509 *
1510 * @param[in] trg Target set to put the canononized source into.
1511 * @param[in] src Source set.
1512 * @param[in] xp_node Source XPath node/meta to use for canonization.
1513 * @return LY_ERR
1514 */
1515static LY_ERR
1516set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1517{
1518 struct lysc_type *type = NULL;
1519 struct lyd_value val;
1520 struct ly_err_item *err = NULL;
1521 char *str, *ptr;
Radek Krejci857189e2020-09-01 13:26:36 +02001522 ly_bool dynamic;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001523 LY_ERR rc;
1524
1525 /* is there anything to canonize even? */
1526 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1527 /* do we have a type to use for canonization? */
1528 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1529 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1530 } else if (xp_node->type == LYXP_NODE_META) {
1531 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1532 }
1533 }
1534 if (!type) {
1535 goto fill;
1536 }
1537
1538 if (src->type == LYXP_SET_NUMBER) {
1539 /* canonize number */
1540 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1541 LOGMEM(src->ctx);
1542 return LY_EMEM;
1543 }
1544 } else {
1545 /* canonize string */
1546 str = strdup(src->val.str);
1547 }
1548
1549 /* ignore errors, the value may not satisfy schema constraints */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001550 rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_DYNAMIC,
1551 LY_PREF_JSON, NULL, NULL, NULL, &val, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001552 ly_err_free(err);
1553 if (rc) {
1554 /* invalid value */
1555 free(str);
1556 goto fill;
1557 }
1558
1559 /* storing successful, now print the canonical value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001560 str = (char *)type->plugin->print(&val, LY_PREF_JSON, NULL, &dynamic);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001561
1562 /* use the canonized value */
1563 set_init(trg, src);
1564 trg->type = src->type;
1565 if (src->type == LYXP_SET_NUMBER) {
1566 trg->val.num = strtold(str, &ptr);
1567 if (dynamic) {
1568 free(str);
1569 }
1570 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1571 } else {
1572 trg->val.str = (dynamic ? str : strdup(str));
1573 }
1574 type->plugin->free(src->ctx, &val);
1575 return LY_SUCCESS;
1576
1577fill:
1578 /* no canonization needed/possible */
1579 set_fill_set(trg, src);
1580 return LY_SUCCESS;
1581}
1582
Michal Vasko03ff5a72019-09-11 13:49:33 +02001583#ifndef NDEBUG
1584
1585/**
1586 * @brief Bubble sort @p set into XPath document order.
1587 * Context position aware. Unused in the 'Release' build target.
1588 *
1589 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001590 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1591 */
1592static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001593set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001594{
1595 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001596 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001597 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001598 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001599 struct lyxp_set_node item;
1600 struct lyxp_set_hash_node hnode;
1601 uint64_t hash;
1602
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001603 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001604 return 0;
1605 }
1606
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001607 /* find first top-level node to be used as anchor for positions */
Radek Krejci1e008d22020-08-17 11:37:37 +02001608 for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001609 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001610
1611 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001612 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001613 return -1;
1614 }
1615
1616 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1617 print_set_debug(set);
1618
1619 for (i = 0; i < set->used; ++i) {
1620 inverted = 0;
1621 change = 0;
1622
1623 for (j = 1; j < set->used - i; ++j) {
1624 /* compare node positions */
1625 if (inverted) {
1626 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1627 } else {
1628 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1629 }
1630
1631 /* swap if needed */
1632 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1633 change = 1;
1634
1635 item = set->val.nodes[j - 1];
1636 set->val.nodes[j - 1] = set->val.nodes[j];
1637 set->val.nodes[j] = item;
1638 } else {
1639 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1640 inverted = !inverted;
1641 }
1642 }
1643
1644 ++ret;
1645
1646 if (!change) {
1647 break;
1648 }
1649 }
1650
1651 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1652 print_set_debug(set);
1653
1654 /* check node hashes */
1655 if (set->used >= LYD_HT_MIN_ITEMS) {
1656 assert(set->ht);
1657 for (i = 0; i < set->used; ++i) {
1658 hnode.node = set->val.nodes[i].node;
1659 hnode.type = set->val.nodes[i].type;
1660
1661 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1662 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1663 hash = dict_hash_multi(hash, NULL, 0);
1664
1665 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1666 }
1667 }
1668
1669 return ret - 1;
1670}
1671
1672/**
1673 * @brief Remove duplicate entries in a sorted node set.
1674 *
1675 * @param[in] set Sorted set to check.
1676 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1677 */
1678static LY_ERR
1679set_sorted_dup_node_clean(struct lyxp_set *set)
1680{
1681 uint32_t i = 0;
1682 LY_ERR ret = LY_SUCCESS;
1683
1684 if (set->used > 1) {
1685 while (i < set->used - 1) {
1686 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node)
1687 && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001688 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001689 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001690 }
Michal Vasko57eab132019-09-24 11:46:26 +02001691 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001692 }
1693 }
1694
Michal Vasko2caefc12019-11-14 16:07:56 +01001695 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001696 return ret;
1697}
1698
1699#endif
1700
1701/**
1702 * @brief Merge 2 sorted sets into one.
1703 *
1704 * @param[in,out] trg Set to merge into. Duplicates are removed.
1705 * @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 +02001706 * @return LY_ERR
1707 */
1708static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001709set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001710{
1711 uint32_t i, j, k, count, dup_count;
1712 int cmp;
1713 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001714
Michal Vaskod3678892020-05-21 10:06:58 +02001715 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001716 return LY_EINVAL;
1717 }
1718
Michal Vaskod3678892020-05-21 10:06:58 +02001719 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001720 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001721 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001722 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001723 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001724 return LY_SUCCESS;
1725 }
1726
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001727 /* find first top-level node to be used as anchor for positions */
Radek Krejci1e008d22020-08-17 11:37:37 +02001728 for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001729 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001730
1731 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001732 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001733 return LY_EINT;
1734 }
1735
1736#ifndef NDEBUG
1737 LOGDBG(LY_LDGXPATH, "MERGE target");
1738 print_set_debug(trg);
1739 LOGDBG(LY_LDGXPATH, "MERGE source");
1740 print_set_debug(src);
1741#endif
1742
1743 /* make memory for the merge (duplicates are not detected yet, so space
1744 * will likely be wasted on them, too bad) */
1745 if (trg->size - trg->used < src->used) {
1746 trg->size = trg->used + src->used;
1747
1748 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1749 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1750 }
1751
1752 i = 0;
1753 j = 0;
1754 count = 0;
1755 dup_count = 0;
1756 do {
1757 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1758 if (!cmp) {
1759 if (!count) {
1760 /* duplicate, just skip it */
1761 ++i;
1762 ++j;
1763 } else {
1764 /* we are copying something already, so let's copy the duplicate too,
1765 * we are hoping that afterwards there are some more nodes to
1766 * copy and this way we can copy them all together */
1767 ++count;
1768 ++dup_count;
1769 ++i;
1770 ++j;
1771 }
1772 } else if (cmp < 0) {
1773 /* inserting src node into trg, just remember it for now */
1774 ++count;
1775 ++i;
1776
1777 /* insert the hash now */
1778 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1779 } else if (count) {
1780copy_nodes:
1781 /* time to actually copy the nodes, we have found the largest block of nodes */
1782 memmove(&trg->val.nodes[j + (count - dup_count)],
1783 &trg->val.nodes[j],
1784 (trg->used - j) * sizeof *trg->val.nodes);
1785 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1786
1787 trg->used += count - dup_count;
1788 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1789 j += count - dup_count;
1790
1791 count = 0;
1792 dup_count = 0;
1793 } else {
1794 ++j;
1795 }
1796 } while ((i < src->used) && (j < trg->used));
1797
1798 if ((i < src->used) || count) {
1799 /* insert all the hashes first */
1800 for (k = i; k < src->used; ++k) {
1801 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1802 }
1803
1804 /* loop ended, but we need to copy something at trg end */
1805 count += src->used - i;
1806 i = src->used;
1807 goto copy_nodes;
1808 }
1809
1810 /* we are inserting hashes before the actual node insert, which causes
1811 * situations when there were initially not enough items for a hash table,
1812 * but even after some were inserted, hash table was not created (during
1813 * insertion the number of items is not updated yet) */
1814 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1815 set_insert_node_hash(trg, NULL, 0);
1816 }
1817
1818#ifndef NDEBUG
1819 LOGDBG(LY_LDGXPATH, "MERGE result");
1820 print_set_debug(trg);
1821#endif
1822
Michal Vaskod3678892020-05-21 10:06:58 +02001823 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001824 return LY_SUCCESS;
1825}
1826
1827/*
1828 * (re)parse functions
1829 *
1830 * Parse functions parse the expression into
1831 * tokens (syntactic analysis).
1832 *
1833 * Reparse functions perform semantic analysis
1834 * (do not save the result, just a check) of
1835 * the expression and fill repeat indices.
1836 */
1837
Michal Vasko14676352020-05-29 11:35:55 +02001838LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001839lyxp_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 +02001840{
Michal Vasko004d3152020-06-11 19:59:22 +02001841 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001842 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001843 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1844 }
Michal Vasko14676352020-05-29 11:35:55 +02001845 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001846 }
1847
Michal Vasko004d3152020-06-11 19:59:22 +02001848 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001849 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001850 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001851 lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001852 }
Michal Vasko14676352020-05-29 11:35:55 +02001853 return LY_ENOT;
1854 }
1855
1856 return LY_SUCCESS;
1857}
1858
Michal Vasko004d3152020-06-11 19:59:22 +02001859LY_ERR
1860lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1861{
1862 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1863
1864 /* skip the token */
1865 ++(*tok_idx);
1866
1867 return LY_SUCCESS;
1868}
1869
Michal Vasko14676352020-05-29 11:35:55 +02001870/* just like lyxp_check_token() but tests for 2 tokens */
1871static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001872exp_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 +02001873 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001874{
Michal Vasko004d3152020-06-11 19:59:22 +02001875 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001876 if (ctx) {
1877 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1878 }
1879 return LY_EINCOMPLETE;
1880 }
1881
Michal Vasko004d3152020-06-11 19:59:22 +02001882 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001883 if (ctx) {
1884 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001885 lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001886 }
1887 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001888 }
1889
1890 return LY_SUCCESS;
1891}
1892
1893/**
1894 * @brief Stack operation push on the repeat array.
1895 *
1896 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001897 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001898 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1899 */
1900static void
Michal Vasko004d3152020-06-11 19:59:22 +02001901exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001902{
1903 uint16_t i;
1904
Michal Vasko004d3152020-06-11 19:59:22 +02001905 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001906 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001907 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1908 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1909 exp->repeat[tok_idx][i] = repeat_op_idx;
1910 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001911 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001912 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1913 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1914 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001915 }
1916}
1917
1918/**
1919 * @brief Reparse Predicate. Logs directly on error.
1920 *
1921 * [7] Predicate ::= '[' Expr ']'
1922 *
1923 * @param[in] ctx Context for logging.
1924 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001925 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001926 * @return LY_ERR
1927 */
1928static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001929reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001930{
1931 LY_ERR rc;
1932
Michal Vasko004d3152020-06-11 19:59:22 +02001933 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001934 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001935 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001936
Michal Vasko004d3152020-06-11 19:59:22 +02001937 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001938 LY_CHECK_RET(rc);
1939
Michal Vasko004d3152020-06-11 19:59:22 +02001940 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001941 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001942 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001943
1944 return LY_SUCCESS;
1945}
1946
1947/**
1948 * @brief Reparse RelativeLocationPath. Logs directly on error.
1949 *
1950 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1951 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1952 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1953 *
1954 * @param[in] ctx Context for logging.
1955 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001956 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001957 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1958 */
1959static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001960reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001961{
1962 LY_ERR rc;
1963
Michal Vasko004d3152020-06-11 19:59:22 +02001964 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001965 LY_CHECK_RET(rc);
1966
1967 goto step;
1968 do {
1969 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001970 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001971
Michal Vasko004d3152020-06-11 19:59:22 +02001972 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001973 LY_CHECK_RET(rc);
1974step:
1975 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001976 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001977 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001978 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001979 break;
1980
1981 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001982 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001983 break;
1984
1985 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02001986 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001987
Michal Vasko004d3152020-06-11 19:59:22 +02001988 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001989 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001990 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001991 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001992 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001993 return LY_EVALID;
1994 }
Radek Krejci0f969882020-08-21 16:56:47 +02001995 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001996 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02001997 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001998 goto reparse_predicate;
1999 break;
2000
2001 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002002 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002003
2004 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002005 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002006 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002007 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002008
2009 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002010 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002011 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002012 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002013
2014reparse_predicate:
2015 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002016 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2017 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002018 LY_CHECK_RET(rc);
2019 }
2020 break;
2021 default:
2022 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02002023 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002024 return LY_EVALID;
2025 }
Michal Vasko004d3152020-06-11 19:59:22 +02002026 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002027
2028 return LY_SUCCESS;
2029}
2030
2031/**
2032 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2033 *
2034 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2035 *
2036 * @param[in] ctx Context for logging.
2037 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002038 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002039 * @return LY_ERR
2040 */
2041static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002042reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002043{
2044 LY_ERR rc;
2045
Michal Vasko004d3152020-06-11 19:59:22 +02002046 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 +02002047
2048 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002049 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002050 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002051 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002052
Michal Vasko004d3152020-06-11 19:59:22 +02002053 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002054 return LY_SUCCESS;
2055 }
Michal Vasko004d3152020-06-11 19:59:22 +02002056 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002057 case LYXP_TOKEN_DOT:
2058 case LYXP_TOKEN_DDOT:
2059 case LYXP_TOKEN_AT:
2060 case LYXP_TOKEN_NAMETEST:
2061 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002062 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002063 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002064 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002065 default:
2066 break;
2067 }
2068
Michal Vasko03ff5a72019-09-11 13:49:33 +02002069 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002070 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002071 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002072
Michal Vasko004d3152020-06-11 19:59:22 +02002073 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002074 LY_CHECK_RET(rc);
2075 }
2076
2077 return LY_SUCCESS;
2078}
2079
2080/**
2081 * @brief Reparse FunctionCall. Logs directly on error.
2082 *
2083 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2084 *
2085 * @param[in] ctx Context for logging.
2086 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002087 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002088 * @return LY_ERR
2089 */
2090static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002091reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002092{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002093 int8_t min_arg_count = -1;
2094 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002095 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002096 LY_ERR rc;
2097
Michal Vasko004d3152020-06-11 19:59:22 +02002098 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002099 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002100 func_tok_idx = *tok_idx;
2101 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002102 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002103 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002104 min_arg_count = 1;
2105 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002106 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002107 min_arg_count = 1;
2108 max_arg_count = 1;
2109 }
2110 break;
2111 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002112 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002113 min_arg_count = 1;
2114 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002115 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002116 min_arg_count = 0;
2117 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002118 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002119 min_arg_count = 0;
2120 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002121 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002122 min_arg_count = 0;
2123 max_arg_count = 0;
2124 }
2125 break;
2126 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002127 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002128 min_arg_count = 1;
2129 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002130 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002131 min_arg_count = 0;
2132 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002133 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002134 min_arg_count = 1;
2135 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002136 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002137 min_arg_count = 1;
2138 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002139 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002140 min_arg_count = 1;
2141 max_arg_count = 1;
2142 }
2143 break;
2144 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002145 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002146 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002147 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002148 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002149 min_arg_count = 0;
2150 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002151 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002152 min_arg_count = 0;
2153 max_arg_count = 1;
2154 }
2155 break;
2156 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002157 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002158 min_arg_count = 1;
2159 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002160 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002161 min_arg_count = 1;
2162 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002163 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002164 min_arg_count = 0;
2165 max_arg_count = 0;
2166 }
2167 break;
2168 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002169 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002170 min_arg_count = 2;
2171 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002172 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002173 min_arg_count = 0;
2174 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002175 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002176 min_arg_count = 2;
2177 max_arg_count = 2;
2178 }
2179 break;
2180 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002181 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002182 min_arg_count = 2;
2183 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002184 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002185 min_arg_count = 3;
2186 max_arg_count = 3;
2187 }
2188 break;
2189 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002190 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002191 min_arg_count = 0;
2192 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002193 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002194 min_arg_count = 1;
2195 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002196 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002197 min_arg_count = 2;
2198 max_arg_count = 2;
2199 }
2200 break;
2201 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002202 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002203 min_arg_count = 2;
2204 max_arg_count = 2;
2205 }
2206 break;
2207 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002208 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002209 min_arg_count = 2;
2210 max_arg_count = 2;
2211 }
2212 break;
2213 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002214 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002215 min_arg_count = 0;
2216 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002217 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002218 min_arg_count = 0;
2219 max_arg_count = 1;
2220 }
2221 break;
2222 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002223 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002224 min_arg_count = 0;
2225 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002226 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002227 min_arg_count = 2;
2228 max_arg_count = 2;
2229 }
2230 break;
2231 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002232 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002233 min_arg_count = 2;
2234 max_arg_count = 2;
2235 }
2236 break;
2237 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002238 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002239 min_arg_count = 2;
2240 max_arg_count = 2;
2241 }
2242 break;
2243 }
2244 if (min_arg_count == -1) {
Michal Vasko004d3152020-06-11 19:59:22 +02002245 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 +02002246 return LY_EINVAL;
2247 }
Michal Vasko004d3152020-06-11 19:59:22 +02002248 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002249
2250 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002251 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002252 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002253 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002254
2255 /* ( Expr ( ',' Expr )* )? */
2256 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002257 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002258 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002259 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002260 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002261 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002262 LY_CHECK_RET(rc);
2263 }
Michal Vasko004d3152020-06-11 19:59:22 +02002264 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2265 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002266
2267 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002268 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002269 LY_CHECK_RET(rc);
2270 }
2271
2272 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002273 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002274 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002275 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002276
Radek Krejci857189e2020-09-01 13:26:36 +02002277 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Michal Vasko004d3152020-06-11 19:59:22 +02002278 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx],
2279 &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002280 return LY_EVALID;
2281 }
2282
2283 return LY_SUCCESS;
2284}
2285
2286/**
2287 * @brief Reparse PathExpr. Logs directly on error.
2288 *
2289 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2290 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2291 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2292 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2293 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2294 *
2295 * @param[in] ctx Context for logging.
2296 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002297 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002298 * @return LY_ERR
2299 */
2300static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002301reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002302{
2303 LY_ERR rc;
2304
Michal Vasko004d3152020-06-11 19:59:22 +02002305 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002306 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002307 }
2308
Michal Vasko004d3152020-06-11 19:59:22 +02002309 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002310 case LYXP_TOKEN_PAR1:
2311 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002312 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002313
Michal Vasko004d3152020-06-11 19:59:22 +02002314 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002315 LY_CHECK_RET(rc);
2316
Michal Vasko004d3152020-06-11 19:59:22 +02002317 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002318 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002319 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002320 goto predicate;
2321 break;
2322 case LYXP_TOKEN_DOT:
2323 case LYXP_TOKEN_DDOT:
2324 case LYXP_TOKEN_AT:
2325 case LYXP_TOKEN_NAMETEST:
2326 case LYXP_TOKEN_NODETYPE:
2327 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002328 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002329 LY_CHECK_RET(rc);
2330 break;
2331 case LYXP_TOKEN_FUNCNAME:
2332 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002333 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002334 LY_CHECK_RET(rc);
2335 goto predicate;
2336 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002337 case LYXP_TOKEN_OPER_PATH:
2338 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002339 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002340 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002341 LY_CHECK_RET(rc);
2342 break;
2343 case LYXP_TOKEN_LITERAL:
2344 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002345 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002346 goto predicate;
2347 break;
2348 case LYXP_TOKEN_NUMBER:
2349 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002350 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002351 goto predicate;
2352 break;
2353 default:
2354 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02002355 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002356 return LY_EVALID;
2357 }
2358
2359 return LY_SUCCESS;
2360
2361predicate:
2362 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002363 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2364 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002365 LY_CHECK_RET(rc);
2366 }
2367
2368 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002369 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002370
2371 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002372 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002373
Michal Vasko004d3152020-06-11 19:59:22 +02002374 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002375 LY_CHECK_RET(rc);
2376 }
2377
2378 return LY_SUCCESS;
2379}
2380
2381/**
2382 * @brief Reparse UnaryExpr. Logs directly on error.
2383 *
2384 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2385 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2386 *
2387 * @param[in] ctx Context for logging.
2388 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002389 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002390 * @return LY_ERR
2391 */
2392static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002393reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002394{
2395 uint16_t prev_exp;
2396 LY_ERR rc;
2397
2398 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002399 prev_exp = *tok_idx;
2400 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2401 && (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002402 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002403 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002404 }
2405
2406 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002407 prev_exp = *tok_idx;
2408 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002409 LY_CHECK_RET(rc);
2410
2411 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002412 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002413 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002414 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002415
Michal Vasko004d3152020-06-11 19:59:22 +02002416 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002417 LY_CHECK_RET(rc);
2418 }
2419
2420 return LY_SUCCESS;
2421}
2422
2423/**
2424 * @brief Reparse AdditiveExpr. Logs directly on error.
2425 *
2426 * [15] AdditiveExpr ::= MultiplicativeExpr
2427 * | AdditiveExpr '+' MultiplicativeExpr
2428 * | AdditiveExpr '-' MultiplicativeExpr
2429 * [16] MultiplicativeExpr ::= UnaryExpr
2430 * | MultiplicativeExpr '*' UnaryExpr
2431 * | MultiplicativeExpr 'div' UnaryExpr
2432 * | MultiplicativeExpr 'mod' UnaryExpr
2433 *
2434 * @param[in] ctx Context for logging.
2435 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002436 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002437 * @return LY_ERR
2438 */
2439static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002440reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002441{
2442 uint16_t prev_add_exp, prev_mul_exp;
2443 LY_ERR rc;
2444
Michal Vasko004d3152020-06-11 19:59:22 +02002445 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002446 goto reparse_multiplicative_expr;
2447
2448 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002449 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2450 && ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002451 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002452 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002453
2454reparse_multiplicative_expr:
2455 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002456 prev_mul_exp = *tok_idx;
2457 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002458 LY_CHECK_RET(rc);
2459
2460 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002461 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2462 && ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002463 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002464 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002465
Michal Vasko004d3152020-06-11 19:59:22 +02002466 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002467 LY_CHECK_RET(rc);
2468 }
2469 }
2470
2471 return LY_SUCCESS;
2472}
2473
2474/**
2475 * @brief Reparse EqualityExpr. Logs directly on error.
2476 *
2477 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2478 * | EqualityExpr '!=' RelationalExpr
2479 * [14] RelationalExpr ::= AdditiveExpr
2480 * | RelationalExpr '<' AdditiveExpr
2481 * | RelationalExpr '>' AdditiveExpr
2482 * | RelationalExpr '<=' AdditiveExpr
2483 * | RelationalExpr '>=' AdditiveExpr
2484 *
2485 * @param[in] ctx Context for logging.
2486 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002487 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002488 * @return LY_ERR
2489 */
2490static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002491reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002492{
2493 uint16_t prev_eq_exp, prev_rel_exp;
2494 LY_ERR rc;
2495
Michal Vasko004d3152020-06-11 19:59:22 +02002496 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002497 goto reparse_additive_expr;
2498
2499 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002500 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002501 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002502 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002503
2504reparse_additive_expr:
2505 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002506 prev_rel_exp = *tok_idx;
2507 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002508 LY_CHECK_RET(rc);
2509
2510 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002511 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002512 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002513 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002514
Michal Vasko004d3152020-06-11 19:59:22 +02002515 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002516 LY_CHECK_RET(rc);
2517 }
2518 }
2519
2520 return LY_SUCCESS;
2521}
2522
2523/**
2524 * @brief Reparse OrExpr. Logs directly on error.
2525 *
2526 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2527 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2528 *
2529 * @param[in] ctx Context for logging.
2530 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002531 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002532 * @return LY_ERR
2533 */
2534static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002535reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002536{
2537 uint16_t prev_or_exp, prev_and_exp;
2538 LY_ERR rc;
2539
Michal Vasko004d3152020-06-11 19:59:22 +02002540 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002541 goto reparse_equality_expr;
2542
2543 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002544 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 +02002545 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002546 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002547
2548reparse_equality_expr:
2549 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002550 prev_and_exp = *tok_idx;
2551 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002552 LY_CHECK_RET(rc);
2553
2554 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002555 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 +02002556 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002557 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002558
Michal Vasko004d3152020-06-11 19:59:22 +02002559 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002560 LY_CHECK_RET(rc);
2561 }
2562 }
2563
2564 return LY_SUCCESS;
2565}
Radek Krejcib1646a92018-11-02 16:08:26 +01002566
2567/**
2568 * @brief Parse NCName.
2569 *
2570 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002571 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002572 */
Radek Krejcid4270262019-01-07 15:07:25 +01002573static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002574parse_ncname(const char *ncname)
2575{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002576 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002577 size_t size;
2578 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002579
2580 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2581 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2582 return len;
2583 }
2584
2585 do {
2586 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002587 if (!*ncname) {
2588 break;
2589 }
Radek Krejcid4270262019-01-07 15:07:25 +01002590 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002591 } while (is_xmlqnamechar(uc) && (uc != ':'));
2592
2593 return len;
2594}
2595
2596/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002597 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002598 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002599 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002600 * @param[in] exp Expression to use.
2601 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002602 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002603 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002604 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002605 */
2606static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002607exp_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 +01002608{
2609 uint32_t prev;
2610
2611 if (exp->used == exp->size) {
2612 prev = exp->size;
2613 exp->size += LYXP_EXPR_SIZE_STEP;
2614 if (prev > exp->size) {
2615 LOGINT(ctx);
2616 return LY_EINT;
2617 }
2618
2619 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2620 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2621 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2622 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2623 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2624 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2625 }
2626
2627 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002628 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002629 exp->tok_len[exp->used] = tok_len;
2630 ++exp->used;
2631 return LY_SUCCESS;
2632}
2633
2634void
Michal Vasko14676352020-05-29 11:35:55 +02002635lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002636{
2637 uint16_t i;
2638
2639 if (!expr) {
2640 return;
2641 }
2642
2643 lydict_remove(ctx, expr->expr);
2644 free(expr->tokens);
2645 free(expr->tok_pos);
2646 free(expr->tok_len);
2647 if (expr->repeat) {
2648 for (i = 0; i < expr->used; ++i) {
2649 free(expr->repeat[i]);
2650 }
2651 }
2652 free(expr->repeat);
2653 free(expr);
2654}
2655
2656struct lyxp_expr *
Radek Krejci857189e2020-09-01 13:26:36 +02002657lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr, size_t expr_len, ly_bool reparse)
Radek Krejcib1646a92018-11-02 16:08:26 +01002658{
2659 struct lyxp_expr *ret;
Radek Krejcid4270262019-01-07 15:07:25 +01002660 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002661 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002662 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002663 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002664
Michal Vasko004d3152020-06-11 19:59:22 +02002665 if (!expr[0]) {
2666 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
2667 return NULL;
2668 }
2669
2670 if (!expr_len) {
2671 expr_len = strlen(expr);
2672 }
2673 if (expr_len > UINT16_MAX) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002674 LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
2675 return NULL;
2676 }
2677
2678 /* init lyxp_expr structure */
2679 ret = calloc(1, sizeof *ret);
2680 LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error);
Michal Vasko004d3152020-06-11 19:59:22 +02002681 ret->expr = lydict_insert(ctx, expr, expr_len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002682 LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error);
2683 ret->used = 0;
2684 ret->size = LYXP_EXPR_SIZE_START;
2685 ret->tokens = malloc(ret->size * sizeof *ret->tokens);
2686 LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error);
2687
2688 ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos);
2689 LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error);
2690
2691 ret->tok_len = malloc(ret->size * sizeof *ret->tok_len);
2692 LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error);
2693
Michal Vasko004d3152020-06-11 19:59:22 +02002694 /* make expr 0-terminated */
2695 expr = ret->expr;
2696
Radek Krejcib1646a92018-11-02 16:08:26 +01002697 while (is_xmlws(expr[parsed])) {
2698 ++parsed;
2699 }
2700
2701 do {
2702 if (expr[parsed] == '(') {
2703
2704 /* '(' */
2705 tok_len = 1;
2706 tok_type = LYXP_TOKEN_PAR1;
2707
2708 if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) {
2709 /* it is a NodeType/FunctionName after all */
2710 if (((ret->tok_len[ret->used - 1] == 4)
2711 && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4)
2712 || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) ||
2713 ((ret->tok_len[ret->used - 1] == 7)
2714 && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) {
2715 ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE;
2716 } else {
2717 ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME;
2718 }
2719 prev_function_check = 0;
2720 }
2721
2722 } else if (expr[parsed] == ')') {
2723
2724 /* ')' */
2725 tok_len = 1;
2726 tok_type = LYXP_TOKEN_PAR2;
2727
2728 } else if (expr[parsed] == '[') {
2729
2730 /* '[' */
2731 tok_len = 1;
2732 tok_type = LYXP_TOKEN_BRACK1;
2733
2734 } else if (expr[parsed] == ']') {
2735
2736 /* ']' */
2737 tok_len = 1;
2738 tok_type = LYXP_TOKEN_BRACK2;
2739
2740 } else if (!strncmp(&expr[parsed], "..", 2)) {
2741
2742 /* '..' */
2743 tok_len = 2;
2744 tok_type = LYXP_TOKEN_DDOT;
2745
2746 } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) {
2747
2748 /* '.' */
2749 tok_len = 1;
2750 tok_type = LYXP_TOKEN_DOT;
2751
2752 } else if (expr[parsed] == '@') {
2753
2754 /* '@' */
2755 tok_len = 1;
2756 tok_type = LYXP_TOKEN_AT;
2757
2758 } else if (expr[parsed] == ',') {
2759
2760 /* ',' */
2761 tok_len = 1;
2762 tok_type = LYXP_TOKEN_COMMA;
2763
2764 } else if (expr[parsed] == '\'') {
2765
2766 /* Literal with ' */
Radek Krejci1e008d22020-08-17 11:37:37 +02002767 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002768 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2769 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2770 ++tok_len;
2771 tok_type = LYXP_TOKEN_LITERAL;
2772
2773 } else if (expr[parsed] == '\"') {
2774
2775 /* Literal with " */
Radek Krejci1e008d22020-08-17 11:37:37 +02002776 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002777 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2778 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2779 ++tok_len;
2780 tok_type = LYXP_TOKEN_LITERAL;
2781
2782 } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) {
2783
2784 /* Number */
Radek Krejci1e008d22020-08-17 11:37:37 +02002785 for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002786 if (expr[parsed + tok_len] == '.') {
2787 ++tok_len;
Michal Vaskod989ba02020-08-24 10:59:24 +02002788 for ( ; isdigit(expr[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002789 }
2790 tok_type = LYXP_TOKEN_NUMBER;
2791
2792 } else if (expr[parsed] == '/') {
2793
2794 /* Operator '/', '//' */
2795 if (!strncmp(&expr[parsed], "//", 2)) {
2796 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002797 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002798 } else {
2799 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002800 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002801 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002802
Michal Vaskod989ba02020-08-24 10:59:24 +02002803 } else if (!strncmp(&expr[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002804
Michal Vasko3e48bf32020-06-01 08:39:07 +02002805 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002806 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002807 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2808
2809 } else if (!strncmp(&expr[parsed], "<=", 2) || !strncmp(&expr[parsed], ">=", 2)) {
2810
2811 /* Operator '<=', '>=' */
2812 tok_len = 2;
2813 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002814
2815 } else if (expr[parsed] == '|') {
2816
2817 /* Operator '|' */
2818 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002819 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002820
2821 } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) {
2822
2823 /* Operator '+', '-' */
2824 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002825 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002826
Michal Vasko3e48bf32020-06-01 08:39:07 +02002827 } else if (expr[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002828
Michal Vasko3e48bf32020-06-01 08:39:07 +02002829 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002830 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002831 tok_type = LYXP_TOKEN_OPER_EQUAL;
2832
2833 } else if ((expr[parsed] == '<') || (expr[parsed] == '>')) {
2834
2835 /* Operator '<', '>' */
2836 tok_len = 1;
2837 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002838
2839 } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT)
2840 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1)
2841 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1)
2842 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA)
Michal Vasko3e48bf32020-06-01 08:39:07 +02002843 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_LOG)
2844 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_EQUAL)
2845 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_NEQUAL)
2846 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_COMP)
2847 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_MATH)
2848 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_UNI)
2849 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_PATH)
2850 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002851
2852 /* Operator '*', 'or', 'and', 'mod', or 'div' */
2853 if (expr[parsed] == '*') {
2854 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002855 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002856
2857 } else if (!strncmp(&expr[parsed], "or", 2)) {
2858 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002859 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002860
2861 } else if (!strncmp(&expr[parsed], "and", 3)) {
2862 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002863 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002864
2865 } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) {
2866 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002867 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002868
2869 } else if (prev_function_check) {
Michal Vasko53078572019-05-24 08:50:15 +02002870 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
2871 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 +01002872 goto error;
2873 } else {
Radek Krejcid4270262019-01-07 15:07:25 +01002874 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr);
Radek Krejcib1646a92018-11-02 16:08:26 +01002875 goto error;
2876 }
2877 } else if (expr[parsed] == '*') {
2878
2879 /* NameTest '*' */
2880 tok_len = 1;
2881 tok_type = LYXP_TOKEN_NAMETEST;
2882
2883 } else {
2884
2885 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejci1deb5be2020-08-26 16:43:36 +02002886 long int ncname_len = parse_ncname(&expr[parsed]);
Radek Krejcid4270262019-01-07 15:07:25 +01002887 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 +01002888 tok_len = ncname_len;
2889
2890 if (expr[parsed + tok_len] == ':') {
2891 ++tok_len;
2892 if (expr[parsed + tok_len] == '*') {
2893 ++tok_len;
2894 } else {
2895 ncname_len = parse_ncname(&expr[parsed + tok_len]);
Radek Krejcid4270262019-01-07 15:07:25 +01002896 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 +01002897 tok_len += ncname_len;
2898 }
2899 /* remove old flag to prevent ambiguities */
2900 prev_function_check = 0;
2901 tok_type = LYXP_TOKEN_NAMETEST;
2902 } else {
2903 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2904 prev_function_check = 1;
2905 tok_type = LYXP_TOKEN_NAMETEST;
2906 }
2907 }
2908
2909 /* store the token, move on to the next one */
2910 LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error);
2911 parsed += tok_len;
2912 while (is_xmlws(expr[parsed])) {
2913 ++parsed;
2914 }
2915
2916 } while (expr[parsed]);
2917
Michal Vasko004d3152020-06-11 19:59:22 +02002918 if (reparse) {
2919 /* prealloc repeat */
2920 ret->repeat = calloc(ret->size, sizeof *ret->repeat);
2921 LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002922
Michal Vasko004d3152020-06-11 19:59:22 +02002923 /* fill repeat */
2924 LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &tok_idx), error);
2925 if (ret->used > tok_idx) {
2926 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
2927 &ret->expr[ret->tok_pos[tok_idx]]);
2928 goto error;
2929 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002930 }
2931
2932 print_expr_struct_debug(ret);
2933
Radek Krejcib1646a92018-11-02 16:08:26 +01002934 return ret;
2935
2936error:
2937 lyxp_expr_free(ctx, ret);
2938 return NULL;
2939}
2940
Michal Vasko004d3152020-06-11 19:59:22 +02002941struct lyxp_expr *
2942lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp)
2943{
2944 struct lyxp_expr *dup;
2945 uint32_t i, j;
2946
2947 dup = calloc(1, sizeof *dup);
2948 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx), error);
2949
2950 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
2951 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx), error);
2952 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2953
2954 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
2955 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx), error);
2956 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2957
2958 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
2959 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx), error);
2960 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2961
2962 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
2963 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx), error);
2964 for (i = 0; i < exp->used; ++i) {
2965 if (!exp->repeat[i]) {
2966 dup->repeat[i] = NULL;
2967 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02002968 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002969 /* the ending 0 as well */
2970 ++j;
2971
Michal Vasko99c71642020-07-03 13:33:36 +02002972 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko004d3152020-06-11 19:59:22 +02002973 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx), error);
2974 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
2975 dup->repeat[i][j - 1] = 0;
2976 }
2977 }
2978
2979 dup->used = exp->used;
2980 dup->size = exp->used;
2981 dup->expr = lydict_insert(ctx, exp->expr, 0);
2982
2983 return dup;
2984
2985error:
2986 lyxp_expr_free(ctx, dup);
2987 return NULL;
2988}
2989
Michal Vasko03ff5a72019-09-11 13:49:33 +02002990/*
2991 * warn functions
2992 *
2993 * Warn functions check specific reasonable conditions for schema XPath
2994 * and print a warning if they are not satisfied.
2995 */
2996
2997/**
2998 * @brief Get the last-added schema node that is currently in the context.
2999 *
3000 * @param[in] set Set to search in.
3001 * @return Last-added schema context node, NULL if no node is in context.
3002 */
3003static struct lysc_node *
3004warn_get_scnode_in_ctx(struct lyxp_set *set)
3005{
3006 uint32_t i;
3007
3008 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3009 return NULL;
3010 }
3011
3012 i = set->used;
3013 do {
3014 --i;
3015 if (set->val.scnodes[i].in_ctx == 1) {
3016 /* if there are more, simply return the first found (last added) */
3017 return set->val.scnodes[i].scnode;
3018 }
3019 } while (i);
3020
3021 return NULL;
3022}
3023
3024/**
3025 * @brief Test whether a type is numeric - integer type or decimal64.
3026 *
3027 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003028 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003029 */
Radek Krejci857189e2020-09-01 13:26:36 +02003030static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003031warn_is_numeric_type(struct lysc_type *type)
3032{
3033 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003034 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003035 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003036
3037 switch (type->basetype) {
3038 case LY_TYPE_DEC64:
3039 case LY_TYPE_INT8:
3040 case LY_TYPE_UINT8:
3041 case LY_TYPE_INT16:
3042 case LY_TYPE_UINT16:
3043 case LY_TYPE_INT32:
3044 case LY_TYPE_UINT32:
3045 case LY_TYPE_INT64:
3046 case LY_TYPE_UINT64:
3047 return 1;
3048 case LY_TYPE_UNION:
3049 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003050 LY_ARRAY_FOR(uni->types, u) {
3051 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003052 if (ret) {
3053 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003054 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003055 }
3056 }
3057 /* did not find any suitable type */
3058 return 0;
3059 case LY_TYPE_LEAFREF:
3060 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3061 default:
3062 return 0;
3063 }
3064}
3065
3066/**
3067 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3068 *
3069 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003070 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003071 */
Radek Krejci857189e2020-09-01 13:26:36 +02003072static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003073warn_is_string_type(struct lysc_type *type)
3074{
3075 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003076 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003077 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003078
3079 switch (type->basetype) {
3080 case LY_TYPE_BITS:
3081 case LY_TYPE_ENUM:
3082 case LY_TYPE_IDENT:
3083 case LY_TYPE_INST:
3084 case LY_TYPE_STRING:
3085 return 1;
3086 case LY_TYPE_UNION:
3087 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003088 LY_ARRAY_FOR(uni->types, u) {
3089 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003090 if (ret) {
3091 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003092 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003093 }
3094 }
3095 /* did not find any suitable type */
3096 return 0;
3097 case LY_TYPE_LEAFREF:
3098 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3099 default:
3100 return 0;
3101 }
3102}
3103
3104/**
3105 * @brief Test whether a type is one specific type.
3106 *
3107 * @param[in] type Type to test.
3108 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003109 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003110 */
Radek Krejci857189e2020-09-01 13:26:36 +02003111static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003112warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3113{
3114 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003115 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003116 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003117
3118 if (type->basetype == base) {
3119 return 1;
3120 } else if (type->basetype == LY_TYPE_UNION) {
3121 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003122 LY_ARRAY_FOR(uni->types, u) {
3123 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003124 if (ret) {
3125 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003126 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003127 }
3128 }
3129 /* did not find any suitable type */
3130 return 0;
3131 } else if (type->basetype == LY_TYPE_LEAFREF) {
3132 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3133 }
3134
3135 return 0;
3136}
3137
3138/**
3139 * @brief Get next type of a (union) type.
3140 *
3141 * @param[in] type Base type.
3142 * @param[in] prev_type Previously returned type.
3143 * @return Next type or NULL.
3144 */
3145static struct lysc_type *
3146warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3147{
3148 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003149 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003150 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003151
3152 switch (type->basetype) {
3153 case LY_TYPE_UNION:
3154 uni = (struct lysc_type_union *)type;
3155 if (!prev_type) {
3156 return uni->types[0];
3157 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003158 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003159 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003160 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003161 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003162 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003163 found = 1;
3164 }
3165 }
3166 return NULL;
3167 default:
3168 if (prev_type) {
3169 assert(type == prev_type);
3170 return NULL;
3171 } else {
3172 return type;
3173 }
3174 }
3175}
3176
3177/**
3178 * @brief Test whether 2 types have a common type.
3179 *
3180 * @param[in] type1 First type.
3181 * @param[in] type2 Second type.
3182 * @return 1 if they do, 0 otherwise.
3183 */
3184static int
3185warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3186{
3187 struct lysc_type *t1, *rt1, *t2, *rt2;
3188
3189 t1 = NULL;
3190 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3191 if (t1->basetype == LY_TYPE_LEAFREF) {
3192 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3193 } else {
3194 rt1 = t1;
3195 }
3196
3197 t2 = NULL;
3198 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3199 if (t2->basetype == LY_TYPE_LEAFREF) {
3200 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3201 } else {
3202 rt2 = t2;
3203 }
3204
3205 if (rt2->basetype == rt1->basetype) {
3206 /* match found */
3207 return 1;
3208 }
3209 }
3210 }
3211
3212 return 0;
3213}
3214
3215/**
3216 * @brief Check both operands of comparison operators.
3217 *
3218 * @param[in] ctx Context for errors.
3219 * @param[in] set1 First operand set.
3220 * @param[in] set2 Second operand set.
3221 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3222 * @param[in] expr Start of the expression to print with the warning.
3223 * @param[in] tok_pos Token position.
3224 */
3225static void
Radek Krejci857189e2020-09-01 13:26:36 +02003226warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr, uint16_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003227{
3228 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003229 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003230
3231 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3232 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3233
3234 if (!node1 && !node2) {
3235 /* no node-sets involved, nothing to do */
3236 return;
3237 }
3238
3239 if (node1) {
3240 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3241 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3242 warning = 1;
3243 leaves = 0;
3244 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3245 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3246 warning = 1;
3247 }
3248 }
3249
3250 if (node2) {
3251 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3252 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3253 warning = 1;
3254 leaves = 0;
3255 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3256 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3257 warning = 1;
3258 }
3259 }
3260
3261 if (node1 && node2 && leaves && !numbers_only) {
3262 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type))
3263 || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type))
3264 || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)
3265 && !warn_is_equal_type(node1->type, node2->type))) {
3266 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3267 warning = 1;
3268 }
3269 }
3270
3271 if (warning) {
3272 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3273 }
3274}
3275
3276/**
3277 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3278 *
3279 * @param[in] exp Parsed XPath expression.
3280 * @param[in] set Set with the leaf/leaf-list.
3281 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3282 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3283 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3284 */
3285static void
3286warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp)
3287{
3288 struct lysc_node *scnode;
3289 struct lysc_type *type;
3290 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003291 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003292 LY_ERR rc;
3293 struct ly_err_item *err = NULL;
3294
3295 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3296 && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
3297 /* check that the node can have the specified value */
3298 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3299 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3300 } else {
3301 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3302 }
3303 if (!value) {
3304 LOGMEM(set->ctx);
3305 return;
3306 }
3307
3308 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3309 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
3310 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
3311 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003312 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko03ff5a72019-09-11 13:49:33 +02003313 exp->expr + exp->tok_pos[equal_exp]);
3314 }
3315
3316 type = ((struct lysc_node_leaf *)scnode)->type;
3317 if (type->basetype != LY_TYPE_IDENT) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003318 rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA, LY_PREF_SCHEMA,
Radek Krejci0f969882020-08-21 16:56:47 +02003319 (void *)set->local_mod, NULL, NULL, &storage, &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003320
3321 if (err) {
3322 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3323 ly_err_free(err);
3324 } else if (rc != LY_SUCCESS) {
3325 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3326 }
3327 if (rc != LY_SUCCESS) {
3328 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003329 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko03ff5a72019-09-11 13:49:33 +02003330 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003331 } else {
3332 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003333 }
3334 }
3335 free(value);
3336 }
3337}
3338
3339/*
3340 * XPath functions
3341 */
3342
3343/**
3344 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3345 * depending on whether the first node bit value from the second argument is set.
3346 *
3347 * @param[in] args Array of arguments.
3348 * @param[in] arg_count Count of elements in @p args.
3349 * @param[in,out] set Context and result set at the same time.
3350 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003351 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003352 */
3353static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003354xpath_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 +02003355{
3356 struct lyd_node_term *leaf;
3357 struct lysc_node_leaf *sleaf;
3358 struct lysc_type_bits *bits;
3359 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003360 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003361
3362 if (options & LYXP_SCNODE_ALL) {
3363 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3364 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003365 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3366 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 +02003367 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3368 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003369 }
3370
3371 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3372 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3373 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 +02003374 } else if (!warn_is_string_type(sleaf->type)) {
3375 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003376 }
3377 }
3378 set_scnode_clear_ctx(set);
3379 return rc;
3380 }
3381
Michal Vaskod3678892020-05-21 10:06:58 +02003382 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003383 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)");
3384 return LY_EVALID;
3385 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003386 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003387 LY_CHECK_RET(rc);
3388
3389 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003390 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003391 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3392 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3393 && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
3394 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003395 LY_ARRAY_FOR(bits->bits, u) {
3396 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003397 set_fill_boolean(set, 1);
3398 break;
3399 }
3400 }
3401 }
3402 }
3403
3404 return LY_SUCCESS;
3405}
3406
3407/**
3408 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3409 * with the argument converted to boolean.
3410 *
3411 * @param[in] args Array of arguments.
3412 * @param[in] arg_count Count of elements in @p args.
3413 * @param[in,out] set Context and result set at the same time.
3414 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003415 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003416 */
3417static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003418xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003419{
3420 LY_ERR rc;
3421
3422 if (options & LYXP_SCNODE_ALL) {
3423 set_scnode_clear_ctx(set);
3424 return LY_SUCCESS;
3425 }
3426
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003427 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003428 LY_CHECK_RET(rc);
3429 set_fill_set(set, args[0]);
3430
3431 return LY_SUCCESS;
3432}
3433
3434/**
3435 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3436 * with the first argument rounded up to the nearest integer.
3437 *
3438 * @param[in] args Array of arguments.
3439 * @param[in] arg_count Count of elements in @p args.
3440 * @param[in,out] set Context and result set at the same time.
3441 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003442 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003443 */
3444static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003445xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003446{
3447 struct lysc_node_leaf *sleaf;
3448 LY_ERR rc = LY_SUCCESS;
3449
3450 if (options & LYXP_SCNODE_ALL) {
3451 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3452 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003453 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3454 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 +02003455 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3456 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003457 }
3458 set_scnode_clear_ctx(set);
3459 return rc;
3460 }
3461
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003462 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003463 LY_CHECK_RET(rc);
3464 if ((long long)args[0]->val.num != args[0]->val.num) {
3465 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3466 } else {
3467 set_fill_number(set, args[0]->val.num);
3468 }
3469
3470 return LY_SUCCESS;
3471}
3472
3473/**
3474 * @brief Execute the XPath concat(string, string, string*) function.
3475 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3476 *
3477 * @param[in] args Array of arguments.
3478 * @param[in] arg_count Count of elements in @p args.
3479 * @param[in,out] set Context and result set at the same time.
3480 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003481 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003482 */
3483static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003484xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003485{
3486 uint16_t i;
3487 char *str = NULL;
3488 size_t used = 1;
3489 LY_ERR rc = LY_SUCCESS;
3490 struct lysc_node_leaf *sleaf;
3491
3492 if (options & LYXP_SCNODE_ALL) {
3493 for (i = 0; i < arg_count; ++i) {
3494 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3495 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3496 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
3497 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003498 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003499 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 +02003500 }
3501 }
3502 }
3503 set_scnode_clear_ctx(set);
3504 return rc;
3505 }
3506
3507 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003508 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003509 if (rc != LY_SUCCESS) {
3510 free(str);
3511 return rc;
3512 }
3513
3514 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3515 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3516 strcpy(str + used - 1, args[i]->val.str);
3517 used += strlen(args[i]->val.str);
3518 }
3519
3520 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003521 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003522 set->type = LYXP_SET_STRING;
3523 set->val.str = str;
3524
3525 return LY_SUCCESS;
3526}
3527
3528/**
3529 * @brief Execute the XPath contains(string, string) function.
3530 * Returns LYXP_SET_BOOLEAN whether the second argument can
3531 * be found in the first or not.
3532 *
3533 * @param[in] args Array of arguments.
3534 * @param[in] arg_count Count of elements in @p args.
3535 * @param[in,out] set Context and result set at the same time.
3536 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003537 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003538 */
3539static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003540xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003541{
3542 struct lysc_node_leaf *sleaf;
3543 LY_ERR rc = LY_SUCCESS;
3544
3545 if (options & LYXP_SCNODE_ALL) {
3546 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3547 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3548 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 +02003549 } else if (!warn_is_string_type(sleaf->type)) {
3550 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003551 }
3552 }
3553
3554 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3555 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3556 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 +02003557 } else if (!warn_is_string_type(sleaf->type)) {
3558 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003559 }
3560 }
3561 set_scnode_clear_ctx(set);
3562 return rc;
3563 }
3564
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003565 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003566 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003567 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003568 LY_CHECK_RET(rc);
3569
3570 if (strstr(args[0]->val.str, args[1]->val.str)) {
3571 set_fill_boolean(set, 1);
3572 } else {
3573 set_fill_boolean(set, 0);
3574 }
3575
3576 return LY_SUCCESS;
3577}
3578
3579/**
3580 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3581 * with the size of the node-set from the argument.
3582 *
3583 * @param[in] args Array of arguments.
3584 * @param[in] arg_count Count of elements in @p args.
3585 * @param[in,out] set Context and result set at the same time.
3586 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003587 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003588 */
3589static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003590xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003591{
3592 struct lysc_node *scnode = NULL;
3593 LY_ERR rc = LY_SUCCESS;
3594
3595 if (options & LYXP_SCNODE_ALL) {
3596 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3597 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003598 }
3599 set_scnode_clear_ctx(set);
3600 return rc;
3601 }
3602
Michal Vasko03ff5a72019-09-11 13:49:33 +02003603 if (args[0]->type != LYXP_SET_NODE_SET) {
3604 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
3605 return LY_EVALID;
3606 }
3607
3608 set_fill_number(set, args[0]->used);
3609 return LY_SUCCESS;
3610}
3611
3612/**
3613 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3614 * with the context with the intial node.
3615 *
3616 * @param[in] args Array of arguments.
3617 * @param[in] arg_count Count of elements in @p args.
3618 * @param[in,out] set Context and result set at the same time.
3619 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003620 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003621 */
3622static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003623xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003624{
3625 if (arg_count || args) {
3626 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
3627 return LY_EVALID;
3628 }
3629
3630 if (options & LYXP_SCNODE_ALL) {
3631 set_scnode_clear_ctx(set);
3632
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003633 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003634 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003635 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003636
3637 /* position is filled later */
3638 set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0);
3639 }
3640
3641 return LY_SUCCESS;
3642}
3643
3644/**
3645 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3646 * leafref or instance-identifier target node(s).
3647 *
3648 * @param[in] args Array of arguments.
3649 * @param[in] arg_count Count of elements in @p args.
3650 * @param[in,out] set Context and result set at the same time.
3651 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003652 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003653 */
3654static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003655xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003656{
3657 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003658 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003659 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003660 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003661 struct ly_path *p;
3662 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003663 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003664 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003665 LY_ERR rc = LY_SUCCESS;
3666
3667 if (options & LYXP_SCNODE_ALL) {
3668 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3669 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003670 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3671 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 +02003672 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3673 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3674 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003675 }
3676 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003677 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003678 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vasko00cbf532020-06-15 13:58:47 +02003679 oper = lysc_is_output((struct lysc_node *)sleaf) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003680
3681 /* it was already evaluated on schema, it must succeed */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003682 rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE,
3683 oper, LY_PATH_TARGET_MANY, set->format, lref->path_context, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003684 assert(!rc);
3685
3686 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003687 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003688 ly_path_free(set->ctx, p);
3689
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003690 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003691 }
3692
Michal Vasko03ff5a72019-09-11 13:49:33 +02003693 return rc;
3694 }
3695
Michal Vaskod3678892020-05-21 10:06:58 +02003696 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003697 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
3698 return LY_EVALID;
3699 }
3700
Michal Vaskod3678892020-05-21 10:06:58 +02003701 lyxp_set_free_content(set);
3702 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003703 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3704 sleaf = (struct lysc_node_leaf *)leaf->schema;
3705 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3706 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3707 /* find leafref target */
Michal Vasko004d3152020-06-11 19:59:22 +02003708 if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf,
3709 &leaf->value, set->tree, &node, &errmsg)) {
3710 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003711 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003712 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003713 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003714 } else {
3715 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003716 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003717 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Michal Vaskob7be7a82020-08-20 09:09:04 +02003718 LYD_CANON_VALUE(leaf));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003719 return LY_EVALID;
3720 }
3721 }
Michal Vasko004d3152020-06-11 19:59:22 +02003722
3723 /* insert it */
3724 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003725 }
3726 }
3727
3728 return LY_SUCCESS;
3729}
3730
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003731static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003732xpath_derived_(struct lyxp_set **args, struct lyxp_set *set, uint32_t options, ly_bool self_match, const char *func)
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003733{
3734 uint16_t i;
3735 LY_ARRAY_COUNT_TYPE u;
3736 struct lyd_node_term *leaf;
3737 struct lysc_node_leaf *sleaf;
3738 struct lyd_meta *meta;
3739 struct lyd_value data = {0}, *val;
3740 struct ly_err_item *err = NULL;
3741 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003742 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003743
3744 if (options & LYXP_SCNODE_ALL) {
3745 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3746 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
3747 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3748 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3749 sleaf->name);
3750 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3751 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3752 }
3753
3754 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3755 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3756 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3757 sleaf->name);
3758 } else if (!warn_is_string_type(sleaf->type)) {
3759 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3760 }
3761 }
3762 set_scnode_clear_ctx(set);
3763 return rc;
3764 }
3765
3766 if (args[0]->type != LYXP_SET_NODE_SET) {
3767 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3768 "derived-from(-or-self)(node-set, string)");
3769 return LY_EVALID;
3770 }
3771 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3772 LY_CHECK_RET(rc);
3773
3774 set_fill_boolean(set, 0);
3775 found = 0;
3776 for (i = 0; i < args[0]->used; ++i) {
3777 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3778 continue;
3779 }
3780
3781 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3782 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3783 sleaf = (struct lysc_node_leaf *)leaf->schema;
3784 val = &leaf->value;
3785 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3786 /* uninteresting */
3787 continue;
3788 }
3789
3790 /* store args[1] as ident */
3791 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 +02003792 0, set->format, (void *)set->local_mod,
Radek Krejci0f969882020-08-21 16:56:47 +02003793 (struct lyd_node *)leaf, set->tree, &data, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003794 } else {
3795 meta = args[0]->val.meta[i].meta;
3796 val = &meta->value;
3797 if (val->realtype->basetype != LY_TYPE_IDENT) {
3798 /* uninteresting */
3799 continue;
3800 }
3801
3802 /* store args[1] as ident */
3803 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 +02003804 0, set->format, (void *)meta->annotation->module,
3805 meta->parent, set->tree, &data, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003806 }
3807
3808 if (err) {
3809 ly_err_print(err);
3810 ly_err_free(err);
3811 }
3812 LY_CHECK_RET(rc);
3813
3814 /* finally check the identity itself */
3815 if (self_match && (data.ident == val->ident)) {
3816 set_fill_boolean(set, 1);
3817 found = 1;
3818 }
3819 if (!found) {
3820 LY_ARRAY_FOR(data.ident->derived, u) {
3821 if (data.ident->derived[u] == val->ident) {
3822 set_fill_boolean(set, 1);
3823 found = 1;
3824 break;
3825 }
3826 }
3827 }
3828
3829 /* free temporary value */
3830 val->realtype->plugin->free(set->ctx, &data);
3831 if (found) {
3832 break;
3833 }
3834 }
3835
3836 return LY_SUCCESS;
3837}
3838
Michal Vasko03ff5a72019-09-11 13:49:33 +02003839/**
3840 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3841 * on whether the first argument nodes contain a node of an identity derived from the second
3842 * argument identity.
3843 *
3844 * @param[in] args Array of arguments.
3845 * @param[in] arg_count Count of elements in @p args.
3846 * @param[in,out] set Context and result set at the same time.
3847 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003848 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003849 */
3850static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003851xpath_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 +02003852{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003853 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003854}
3855
3856/**
3857 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3858 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3859 * the second argument identity.
3860 *
3861 * @param[in] args Array of arguments.
3862 * @param[in] arg_count Count of elements in @p args.
3863 * @param[in,out] set Context and result set at the same time.
3864 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003865 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003866 */
3867static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003868xpath_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 +02003869{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003870 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003871}
3872
3873/**
3874 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3875 * with the integer value of the first node's enum value, otherwise NaN.
3876 *
3877 * @param[in] args Array of arguments.
3878 * @param[in] arg_count Count of elements in @p args.
3879 * @param[in,out] set Context and result set at the same time.
3880 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003881 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003882 */
3883static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003884xpath_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 +02003885{
3886 struct lyd_node_term *leaf;
3887 struct lysc_node_leaf *sleaf;
3888 LY_ERR rc = LY_SUCCESS;
3889
3890 if (options & LYXP_SCNODE_ALL) {
3891 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3892 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003893 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3894 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 +02003895 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3896 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003897 }
3898 set_scnode_clear_ctx(set);
3899 return rc;
3900 }
3901
Michal Vaskod3678892020-05-21 10:06:58 +02003902 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003903 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
3904 return LY_EVALID;
3905 }
3906
3907 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003908 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003909 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3910 sleaf = (struct lysc_node_leaf *)leaf->schema;
3911 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3912 set_fill_number(set, leaf->value.enum_item->value);
3913 }
3914 }
3915
3916 return LY_SUCCESS;
3917}
3918
3919/**
3920 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3921 * with false value.
3922 *
3923 * @param[in] args Array of arguments.
3924 * @param[in] arg_count Count of elements in @p args.
3925 * @param[in,out] set Context and result set at the same time.
3926 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003927 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003928 */
3929static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003930xpath_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 +02003931{
3932 if (options & LYXP_SCNODE_ALL) {
3933 set_scnode_clear_ctx(set);
3934 return LY_SUCCESS;
3935 }
3936
3937 set_fill_boolean(set, 0);
3938 return LY_SUCCESS;
3939}
3940
3941/**
3942 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3943 * with the first argument floored (truncated).
3944 *
3945 * @param[in] args Array of arguments.
3946 * @param[in] arg_count Count of elements in @p args.
3947 * @param[in,out] set Context and result set at the same time.
3948 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003949 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003950 */
3951static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003952xpath_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 +02003953{
3954 LY_ERR rc;
3955
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003956 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003957 LY_CHECK_RET(rc);
3958 if (isfinite(args[0]->val.num)) {
3959 set_fill_number(set, (long long)args[0]->val.num);
3960 }
3961
3962 return LY_SUCCESS;
3963}
3964
3965/**
3966 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3967 * whether the language of the text matches the one from the argument.
3968 *
3969 * @param[in] args Array of arguments.
3970 * @param[in] arg_count Count of elements in @p args.
3971 * @param[in,out] set Context and result set at the same time.
3972 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003973 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003974 */
3975static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003976xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003977{
3978 const struct lyd_node *node;
3979 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01003980 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003981 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003982 LY_ERR rc = LY_SUCCESS;
3983
3984 if (options & LYXP_SCNODE_ALL) {
3985 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3986 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3987 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 +02003988 } else if (!warn_is_string_type(sleaf->type)) {
3989 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003990 }
3991 }
3992 set_scnode_clear_ctx(set);
3993 return rc;
3994 }
3995
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003996 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003997 LY_CHECK_RET(rc);
3998
Michal Vasko03ff5a72019-09-11 13:49:33 +02003999 if (set->type != LYXP_SET_NODE_SET) {
4000 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
4001 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004002 } else if (!set->used) {
4003 set_fill_boolean(set, 0);
4004 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004005 }
4006
4007 switch (set->val.nodes[0].type) {
4008 case LYXP_NODE_ELEM:
4009 case LYXP_NODE_TEXT:
4010 node = set->val.nodes[0].node;
4011 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004012 case LYXP_NODE_META:
4013 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004014 break;
4015 default:
4016 /* nothing to do with roots */
4017 set_fill_boolean(set, 0);
4018 return LY_SUCCESS;
4019 }
4020
Michal Vasko9f96a052020-03-10 09:41:45 +01004021 /* find lang metadata */
Michal Vaskod989ba02020-08-24 10:59:24 +02004022 for ( ; node; node = (struct lyd_node *)node->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004023 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004024 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004025 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004026 break;
4027 }
4028 }
4029
Michal Vasko9f96a052020-03-10 09:41:45 +01004030 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004031 break;
4032 }
4033 }
4034
4035 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004036 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004037 set_fill_boolean(set, 0);
4038 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004039 uint64_t i;
4040
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004041 val = meta->value.canonical;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004042 for (i = 0; args[0]->val.str[i]; ++i) {
4043 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4044 set_fill_boolean(set, 0);
4045 break;
4046 }
4047 }
4048 if (!args[0]->val.str[i]) {
4049 if (!val[i] || (val[i] == '-')) {
4050 set_fill_boolean(set, 1);
4051 } else {
4052 set_fill_boolean(set, 0);
4053 }
4054 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004055 }
4056
4057 return LY_SUCCESS;
4058}
4059
4060/**
4061 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4062 * with the context size.
4063 *
4064 * @param[in] args Array of arguments.
4065 * @param[in] arg_count Count of elements in @p args.
4066 * @param[in,out] set Context and result set at the same time.
4067 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004068 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004069 */
4070static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004071xpath_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 +02004072{
4073 if (options & LYXP_SCNODE_ALL) {
4074 set_scnode_clear_ctx(set);
4075 return LY_SUCCESS;
4076 }
4077
Michal Vasko03ff5a72019-09-11 13:49:33 +02004078 if (set->type != LYXP_SET_NODE_SET) {
4079 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
4080 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004081 } else if (!set->used) {
4082 set_fill_number(set, 0);
4083 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004084 }
4085
4086 set_fill_number(set, set->ctx_size);
4087 return LY_SUCCESS;
4088}
4089
4090/**
4091 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4092 * with the node name without namespace from the argument or the context.
4093 *
4094 * @param[in] args Array of arguments.
4095 * @param[in] arg_count Count of elements in @p args.
4096 * @param[in,out] set Context and result set at the same time.
4097 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004098 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004099 */
4100static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004101xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004102{
4103 struct lyxp_set_node *item;
4104 /* suppress unused variable warning */
4105 (void)options;
4106
4107 if (options & LYXP_SCNODE_ALL) {
4108 set_scnode_clear_ctx(set);
4109 return LY_SUCCESS;
4110 }
4111
4112 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004113 if (args[0]->type != LYXP_SET_NODE_SET) {
4114 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)");
4115 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004116 } else if (!args[0]->used) {
4117 set_fill_string(set, "", 0);
4118 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004119 }
4120
4121 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004122 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004123
4124 item = &args[0]->val.nodes[0];
4125 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004126 if (set->type != LYXP_SET_NODE_SET) {
4127 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
4128 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004129 } else if (!set->used) {
4130 set_fill_string(set, "", 0);
4131 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004132 }
4133
4134 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004135 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004136
4137 item = &set->val.nodes[0];
4138 }
4139
4140 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004141 case LYXP_NODE_NONE:
4142 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004143 case LYXP_NODE_ROOT:
4144 case LYXP_NODE_ROOT_CONFIG:
4145 case LYXP_NODE_TEXT:
4146 set_fill_string(set, "", 0);
4147 break;
4148 case LYXP_NODE_ELEM:
4149 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4150 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004151 case LYXP_NODE_META:
4152 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004153 break;
4154 }
4155
4156 return LY_SUCCESS;
4157}
4158
4159/**
4160 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4161 * with the node name fully qualified (with namespace) from the argument or the context.
4162 *
4163 * @param[in] args Array of arguments.
4164 * @param[in] arg_count Count of elements in @p args.
4165 * @param[in,out] set Context and result set at the same time.
4166 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004167 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004168 */
4169static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004170xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004171{
4172 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004173 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004174 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004175 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004176
4177 if (options & LYXP_SCNODE_ALL) {
4178 set_scnode_clear_ctx(set);
4179 return LY_SUCCESS;
4180 }
4181
4182 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004183 if (args[0]->type != LYXP_SET_NODE_SET) {
4184 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
4185 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004186 } else if (!args[0]->used) {
4187 set_fill_string(set, "", 0);
4188 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004189 }
4190
4191 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004192 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004193
4194 item = &args[0]->val.nodes[0];
4195 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004196 if (set->type != LYXP_SET_NODE_SET) {
4197 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
4198 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004199 } else if (!set->used) {
4200 set_fill_string(set, "", 0);
4201 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004202 }
4203
4204 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004205 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004206
4207 item = &set->val.nodes[0];
4208 }
4209
4210 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004211 case LYXP_NODE_NONE:
4212 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004213 case LYXP_NODE_ROOT:
4214 case LYXP_NODE_ROOT_CONFIG:
4215 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004216 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004217 break;
4218 case LYXP_NODE_ELEM:
4219 mod = item->node->schema->module;
4220 name = item->node->schema->name;
4221 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004222 case LYXP_NODE_META:
4223 mod = ((struct lyd_meta *)item->node)->annotation->module;
4224 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004225 break;
4226 }
4227
4228 if (mod && name) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004229 int rc = -1;
4230
Michal Vasko03ff5a72019-09-11 13:49:33 +02004231 switch (set->format) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02004232 case LY_PREF_SCHEMA:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004233 rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name);
4234 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02004235 case LY_PREF_JSON:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004236 rc = asprintf(&str, "%s:%s", mod->name, name);
4237 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02004238 case LY_PREF_XML:
Michal Vasko9409ef62019-09-12 11:47:17 +02004239 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004240 }
4241 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4242 set_fill_string(set, str, strlen(str));
4243 free(str);
4244 } else {
4245 set_fill_string(set, "", 0);
4246 }
4247
4248 return LY_SUCCESS;
4249}
4250
4251/**
4252 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4253 * with the namespace of the node from the argument or the context.
4254 *
4255 * @param[in] args Array of arguments.
4256 * @param[in] arg_count Count of elements in @p args.
4257 * @param[in,out] set Context and result set at the same time.
4258 * @param[in] options XPath options.
4259 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4260 */
4261static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004262xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004263{
4264 struct lyxp_set_node *item;
4265 struct lys_module *mod;
4266 /* suppress unused variable warning */
4267 (void)options;
4268
4269 if (options & LYXP_SCNODE_ALL) {
4270 set_scnode_clear_ctx(set);
4271 return LY_SUCCESS;
4272 }
4273
4274 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004275 if (args[0]->type != LYXP_SET_NODE_SET) {
4276 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
4277 "namespace-uri(node-set?)");
4278 return LY_EVALID;
4279 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004280 set_fill_string(set, "", 0);
4281 return LY_SUCCESS;
4282 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004283
4284 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004285 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004286
4287 item = &args[0]->val.nodes[0];
4288 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004289 if (set->type != LYXP_SET_NODE_SET) {
4290 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
4291 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004292 } else if (!set->used) {
4293 set_fill_string(set, "", 0);
4294 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004295 }
4296
4297 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004298 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004299
4300 item = &set->val.nodes[0];
4301 }
4302
4303 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004304 case LYXP_NODE_NONE:
4305 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004306 case LYXP_NODE_ROOT:
4307 case LYXP_NODE_ROOT_CONFIG:
4308 case LYXP_NODE_TEXT:
4309 set_fill_string(set, "", 0);
4310 break;
4311 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004312 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004313 if (item->type == LYXP_NODE_ELEM) {
4314 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004315 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004316 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004317 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004318 }
4319
4320 set_fill_string(set, mod->ns, strlen(mod->ns));
4321 break;
4322 }
4323
4324 return LY_SUCCESS;
4325}
4326
4327/**
4328 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4329 * with only nodes from the context. In practice it either leaves the context
4330 * as it is or returns an empty node set.
4331 *
4332 * @param[in] args Array of arguments.
4333 * @param[in] arg_count Count of elements in @p args.
4334 * @param[in,out] set Context and result set at the same time.
4335 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004336 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004337 */
4338static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004339xpath_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 +02004340{
4341 if (options & LYXP_SCNODE_ALL) {
4342 set_scnode_clear_ctx(set);
4343 return LY_SUCCESS;
4344 }
4345
4346 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004347 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004348 }
4349 return LY_SUCCESS;
4350}
4351
4352/**
4353 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4354 * with normalized value (no leading, trailing, double white spaces) of the node
4355 * from the argument or the context.
4356 *
4357 * @param[in] args Array of arguments.
4358 * @param[in] arg_count Count of elements in @p args.
4359 * @param[in,out] set Context and result set at the same time.
4360 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004361 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004362 */
4363static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004364xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004365{
4366 uint16_t i, new_used;
4367 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004368 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004369 struct lysc_node_leaf *sleaf;
4370 LY_ERR rc = LY_SUCCESS;
4371
4372 if (options & LYXP_SCNODE_ALL) {
4373 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4374 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4375 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 +02004376 } else if (!warn_is_string_type(sleaf->type)) {
4377 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004378 }
4379 }
4380 set_scnode_clear_ctx(set);
4381 return rc;
4382 }
4383
4384 if (arg_count) {
4385 set_fill_set(set, args[0]);
4386 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004387 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004388 LY_CHECK_RET(rc);
4389
4390 /* is there any normalization necessary? */
4391 for (i = 0; set->val.str[i]; ++i) {
4392 if (is_xmlws(set->val.str[i])) {
4393 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4394 have_spaces = 1;
4395 break;
4396 }
4397 space_before = 1;
4398 } else {
4399 space_before = 0;
4400 }
4401 }
4402
4403 /* yep, there is */
4404 if (have_spaces) {
4405 /* it's enough, at least one character will go, makes space for ending '\0' */
4406 new = malloc(strlen(set->val.str) * sizeof(char));
4407 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4408 new_used = 0;
4409
4410 space_before = 0;
4411 for (i = 0; set->val.str[i]; ++i) {
4412 if (is_xmlws(set->val.str[i])) {
4413 if ((i == 0) || space_before) {
4414 space_before = 1;
4415 continue;
4416 } else {
4417 space_before = 1;
4418 }
4419 } else {
4420 space_before = 0;
4421 }
4422
4423 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4424 ++new_used;
4425 }
4426
4427 /* at worst there is one trailing space now */
4428 if (new_used && is_xmlws(new[new_used - 1])) {
4429 --new_used;
4430 }
4431
4432 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4433 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4434 new[new_used] = '\0';
4435
4436 free(set->val.str);
4437 set->val.str = new;
4438 }
4439
4440 return LY_SUCCESS;
4441}
4442
4443/**
4444 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4445 * with the argument converted to boolean and logically inverted.
4446 *
4447 * @param[in] args Array of arguments.
4448 * @param[in] arg_count Count of elements in @p args.
4449 * @param[in,out] set Context and result set at the same time.
4450 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004451 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004452 */
4453static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004454xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004455{
4456 if (options & LYXP_SCNODE_ALL) {
4457 set_scnode_clear_ctx(set);
4458 return LY_SUCCESS;
4459 }
4460
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004461 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004462 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004463 set_fill_boolean(set, 0);
4464 } else {
4465 set_fill_boolean(set, 1);
4466 }
4467
4468 return LY_SUCCESS;
4469}
4470
4471/**
4472 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4473 * with the number representation of either the argument or the context.
4474 *
4475 * @param[in] args Array of arguments.
4476 * @param[in] arg_count Count of elements in @p args.
4477 * @param[in,out] set Context and result set at the same time.
4478 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004479 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004480 */
4481static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004482xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004483{
4484 LY_ERR rc;
4485
4486 if (options & LYXP_SCNODE_ALL) {
4487 set_scnode_clear_ctx(set);
4488 return LY_SUCCESS;
4489 }
4490
4491 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004492 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004493 LY_CHECK_RET(rc);
4494 set_fill_set(set, args[0]);
4495 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004496 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004497 LY_CHECK_RET(rc);
4498 }
4499
4500 return LY_SUCCESS;
4501}
4502
4503/**
4504 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4505 * with the context position.
4506 *
4507 * @param[in] args Array of arguments.
4508 * @param[in] arg_count Count of elements in @p args.
4509 * @param[in,out] set Context and result set at the same time.
4510 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004511 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004512 */
4513static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004514xpath_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 +02004515{
4516 if (options & LYXP_SCNODE_ALL) {
4517 set_scnode_clear_ctx(set);
4518 return LY_SUCCESS;
4519 }
4520
Michal Vasko03ff5a72019-09-11 13:49:33 +02004521 if (set->type != LYXP_SET_NODE_SET) {
4522 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
4523 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004524 } else if (!set->used) {
4525 set_fill_number(set, 0);
4526 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004527 }
4528
4529 set_fill_number(set, set->ctx_pos);
4530
4531 /* UNUSED in 'Release' build type */
4532 (void)options;
4533 return LY_SUCCESS;
4534}
4535
4536/**
4537 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4538 * depending on whether the second argument regex matches the first argument string. For details refer to
4539 * YANG 1.1 RFC section 10.2.1.
4540 *
4541 * @param[in] args Array of arguments.
4542 * @param[in] arg_count Count of elements in @p args.
4543 * @param[in,out] set Context and result set at the same time.
4544 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004545 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004546 */
4547static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004548xpath_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 +02004549{
4550 struct lysc_pattern **patterns = NULL, **pattern;
4551 struct lysc_node_leaf *sleaf;
4552 char *path;
4553 LY_ERR rc = LY_SUCCESS;
4554 struct ly_err_item *err;
4555
4556 if (options & LYXP_SCNODE_ALL) {
4557 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4558 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4559 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 +02004560 } else if (!warn_is_string_type(sleaf->type)) {
4561 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004562 }
4563 }
4564
4565 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4566 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4567 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 +02004568 } else if (!warn_is_string_type(sleaf->type)) {
4569 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004570 }
4571 }
4572 set_scnode_clear_ctx(set);
4573 return rc;
4574 }
4575
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004576 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004577 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004578 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004579 LY_CHECK_RET(rc);
4580
4581 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4582 *pattern = malloc(sizeof **pattern);
4583 path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0);
4584 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4585 free(path);
4586 if (rc != LY_SUCCESS) {
4587 LY_ARRAY_FREE(patterns);
4588 return rc;
4589 }
4590
4591 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4592 pcre2_code_free((*pattern)->code);
4593 free(*pattern);
4594 LY_ARRAY_FREE(patterns);
4595 if (rc && (rc != LY_EVALID)) {
4596 ly_err_print(err);
4597 ly_err_free(err);
4598 return rc;
4599 }
4600
4601 if (rc == LY_EVALID) {
4602 ly_err_free(err);
4603 set_fill_boolean(set, 0);
4604 } else {
4605 set_fill_boolean(set, 1);
4606 }
4607
4608 return LY_SUCCESS;
4609}
4610
4611/**
4612 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4613 * with the rounded first argument. For details refer to
4614 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4615 *
4616 * @param[in] args Array of arguments.
4617 * @param[in] arg_count Count of elements in @p args.
4618 * @param[in,out] set Context and result set at the same time.
4619 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004620 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004621 */
4622static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004623xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004624{
4625 struct lysc_node_leaf *sleaf;
4626 LY_ERR rc = LY_SUCCESS;
4627
4628 if (options & LYXP_SCNODE_ALL) {
4629 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4630 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004631 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4632 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 +02004633 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4634 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004635 }
4636 set_scnode_clear_ctx(set);
4637 return rc;
4638 }
4639
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004640 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004641 LY_CHECK_RET(rc);
4642
4643 /* cover only the cases where floor can't be used */
4644 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4645 set_fill_number(set, -0.0f);
4646 } else {
4647 args[0]->val.num += 0.5;
4648 rc = xpath_floor(args, 1, args[0], options);
4649 LY_CHECK_RET(rc);
4650 set_fill_number(set, args[0]->val.num);
4651 }
4652
4653 return LY_SUCCESS;
4654}
4655
4656/**
4657 * @brief Execute the XPath starts-with(string, string) function.
4658 * Returns LYXP_SET_BOOLEAN whether the second argument is
4659 * the prefix of the first or not.
4660 *
4661 * @param[in] args Array of arguments.
4662 * @param[in] arg_count Count of elements in @p args.
4663 * @param[in,out] set Context and result set at the same time.
4664 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004665 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004666 */
4667static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004668xpath_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 +02004669{
4670 struct lysc_node_leaf *sleaf;
4671 LY_ERR rc = LY_SUCCESS;
4672
4673 if (options & LYXP_SCNODE_ALL) {
4674 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4675 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4676 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 +02004677 } else if (!warn_is_string_type(sleaf->type)) {
4678 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004679 }
4680 }
4681
4682 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4683 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4684 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 +02004685 } else if (!warn_is_string_type(sleaf->type)) {
4686 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004687 }
4688 }
4689 set_scnode_clear_ctx(set);
4690 return rc;
4691 }
4692
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004693 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004694 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004695 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004696 LY_CHECK_RET(rc);
4697
4698 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4699 set_fill_boolean(set, 0);
4700 } else {
4701 set_fill_boolean(set, 1);
4702 }
4703
4704 return LY_SUCCESS;
4705}
4706
4707/**
4708 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4709 * with the string representation of either the argument or the context.
4710 *
4711 * @param[in] args Array of arguments.
4712 * @param[in] arg_count Count of elements in @p args.
4713 * @param[in,out] set Context and result set at the same time.
4714 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004715 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004716 */
4717static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004718xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004719{
4720 LY_ERR rc;
4721
4722 if (options & LYXP_SCNODE_ALL) {
4723 set_scnode_clear_ctx(set);
4724 return LY_SUCCESS;
4725 }
4726
4727 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004728 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004729 LY_CHECK_RET(rc);
4730 set_fill_set(set, args[0]);
4731 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004732 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004733 LY_CHECK_RET(rc);
4734 }
4735
4736 return LY_SUCCESS;
4737}
4738
4739/**
4740 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4741 * with the length of the string in either the argument or the context.
4742 *
4743 * @param[in] args Array of arguments.
4744 * @param[in] arg_count Count of elements in @p args.
4745 * @param[in,out] set Context and result set at the same time.
4746 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004747 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004748 */
4749static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004750xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004751{
4752 struct lysc_node_leaf *sleaf;
4753 LY_ERR rc = LY_SUCCESS;
4754
4755 if (options & LYXP_SCNODE_ALL) {
4756 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4757 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4758 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 +02004759 } else if (!warn_is_string_type(sleaf->type)) {
4760 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004761 }
4762 }
4763 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4764 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4765 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 +02004766 } else if (!warn_is_string_type(sleaf->type)) {
4767 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004768 }
4769 }
4770 set_scnode_clear_ctx(set);
4771 return rc;
4772 }
4773
4774 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004775 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004776 LY_CHECK_RET(rc);
4777 set_fill_number(set, strlen(args[0]->val.str));
4778 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004779 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004780 LY_CHECK_RET(rc);
4781 set_fill_number(set, strlen(set->val.str));
4782 }
4783
4784 return LY_SUCCESS;
4785}
4786
4787/**
4788 * @brief Execute the XPath substring(string, number, number?) function.
4789 * Returns LYXP_SET_STRING substring of the first argument starting
4790 * on the second argument index ending on the third argument index,
4791 * indexed from 1. For exact definition refer to
4792 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4793 *
4794 * @param[in] args Array of arguments.
4795 * @param[in] arg_count Count of elements in @p args.
4796 * @param[in,out] set Context and result set at the same time.
4797 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004798 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004799 */
4800static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004801xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004802{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004803 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004804 uint16_t str_start, str_len, pos;
4805 struct lysc_node_leaf *sleaf;
4806 LY_ERR rc = LY_SUCCESS;
4807
4808 if (options & LYXP_SCNODE_ALL) {
4809 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4810 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4811 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 +02004812 } else if (!warn_is_string_type(sleaf->type)) {
4813 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004814 }
4815 }
4816
4817 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4818 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4819 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 +02004820 } else if (!warn_is_numeric_type(sleaf->type)) {
4821 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004822 }
4823 }
4824
4825 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET)
Radek Krejci0f969882020-08-21 16:56:47 +02004826 && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004827 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4828 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 +02004829 } else if (!warn_is_numeric_type(sleaf->type)) {
4830 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004831 }
4832 }
4833 set_scnode_clear_ctx(set);
4834 return rc;
4835 }
4836
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004837 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004838 LY_CHECK_RET(rc);
4839
4840 /* start */
4841 if (xpath_round(&args[1], 1, args[1], options)) {
4842 return -1;
4843 }
4844 if (isfinite(args[1]->val.num)) {
4845 start = args[1]->val.num - 1;
4846 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004847 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004848 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004849 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004850 }
4851
4852 /* len */
4853 if (arg_count == 3) {
4854 rc = xpath_round(&args[2], 1, args[2], options);
4855 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004856 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004857 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004858 } else if (isfinite(args[2]->val.num)) {
4859 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004860 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004861 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004862 }
4863 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004864 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004865 }
4866
4867 /* find matching character positions */
4868 str_start = 0;
4869 str_len = 0;
4870 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4871 if (pos < start) {
4872 ++str_start;
4873 } else if (pos < start + len) {
4874 ++str_len;
4875 } else {
4876 break;
4877 }
4878 }
4879
4880 set_fill_string(set, args[0]->val.str + str_start, str_len);
4881 return LY_SUCCESS;
4882}
4883
4884/**
4885 * @brief Execute the XPath substring-after(string, string) function.
4886 * Returns LYXP_SET_STRING with the string succeeding the occurance
4887 * of the second argument in the first or an empty string.
4888 *
4889 * @param[in] args Array of arguments.
4890 * @param[in] arg_count Count of elements in @p args.
4891 * @param[in,out] set Context and result set at the same time.
4892 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004893 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004894 */
4895static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004896xpath_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 +02004897{
4898 char *ptr;
4899 struct lysc_node_leaf *sleaf;
4900 LY_ERR rc = LY_SUCCESS;
4901
4902 if (options & LYXP_SCNODE_ALL) {
4903 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4904 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4905 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 +02004906 } else if (!warn_is_string_type(sleaf->type)) {
4907 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004908 }
4909 }
4910
4911 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4912 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4913 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 +02004914 } else if (!warn_is_string_type(sleaf->type)) {
4915 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004916 }
4917 }
4918 set_scnode_clear_ctx(set);
4919 return rc;
4920 }
4921
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004922 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004923 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004924 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004925 LY_CHECK_RET(rc);
4926
4927 ptr = strstr(args[0]->val.str, args[1]->val.str);
4928 if (ptr) {
4929 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4930 } else {
4931 set_fill_string(set, "", 0);
4932 }
4933
4934 return LY_SUCCESS;
4935}
4936
4937/**
4938 * @brief Execute the XPath substring-before(string, string) function.
4939 * Returns LYXP_SET_STRING with the string preceding the occurance
4940 * of the second argument in the first or an empty string.
4941 *
4942 * @param[in] args Array of arguments.
4943 * @param[in] arg_count Count of elements in @p args.
4944 * @param[in,out] set Context and result set at the same time.
4945 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004946 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004947 */
4948static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004949xpath_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 +02004950{
4951 char *ptr;
4952 struct lysc_node_leaf *sleaf;
4953 LY_ERR rc = LY_SUCCESS;
4954
4955 if (options & LYXP_SCNODE_ALL) {
4956 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4957 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4958 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 +02004959 } else if (!warn_is_string_type(sleaf->type)) {
4960 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004961 }
4962 }
4963
4964 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4965 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4966 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 +02004967 } else if (!warn_is_string_type(sleaf->type)) {
4968 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004969 }
4970 }
4971 set_scnode_clear_ctx(set);
4972 return rc;
4973 }
4974
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004975 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004976 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004977 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004978 LY_CHECK_RET(rc);
4979
4980 ptr = strstr(args[0]->val.str, args[1]->val.str);
4981 if (ptr) {
4982 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4983 } else {
4984 set_fill_string(set, "", 0);
4985 }
4986
4987 return LY_SUCCESS;
4988}
4989
4990/**
4991 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
4992 * with the sum of all the nodes in the context.
4993 *
4994 * @param[in] args Array of arguments.
4995 * @param[in] arg_count Count of elements in @p args.
4996 * @param[in,out] set Context and result set at the same time.
4997 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004998 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004999 */
5000static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005001xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005002{
5003 long double num;
5004 char *str;
5005 uint16_t i;
5006 struct lyxp_set set_item;
5007 struct lysc_node_leaf *sleaf;
5008 LY_ERR rc = LY_SUCCESS;
5009
5010 if (options & LYXP_SCNODE_ALL) {
5011 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5012 for (i = 0; i < args[0]->used; ++i) {
5013 if (args[0]->val.scnodes[i].in_ctx == 1) {
5014 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5015 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5016 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
5017 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005018 } else if (!warn_is_numeric_type(sleaf->type)) {
5019 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005020 }
5021 }
5022 }
5023 }
5024 set_scnode_clear_ctx(set);
5025 return rc;
5026 }
5027
5028 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005029
5030 if (args[0]->type != LYXP_SET_NODE_SET) {
5031 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
5032 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005033 } else if (!args[0]->used) {
5034 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005035 }
5036
Michal Vasko5c4e5892019-11-14 12:31:38 +01005037 set_init(&set_item, set);
5038
Michal Vasko03ff5a72019-09-11 13:49:33 +02005039 set_item.type = LYXP_SET_NODE_SET;
5040 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5041 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5042
5043 set_item.used = 1;
5044 set_item.size = 1;
5045
5046 for (i = 0; i < args[0]->used; ++i) {
5047 set_item.val.nodes[0] = args[0]->val.nodes[i];
5048
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005049 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005050 LY_CHECK_RET(rc);
5051 num = cast_string_to_number(str);
5052 free(str);
5053 set->val.num += num;
5054 }
5055
5056 free(set_item.val.nodes);
5057
5058 return LY_SUCCESS;
5059}
5060
5061/**
5062 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5063 * with the text content of the nodes in the context.
5064 *
5065 * @param[in] args Array of arguments.
5066 * @param[in] arg_count Count of elements in @p args.
5067 * @param[in,out] set Context and result set at the same time.
5068 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005069 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005070 */
5071static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005072xpath_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 +02005073{
5074 uint32_t i;
5075
5076 if (options & LYXP_SCNODE_ALL) {
5077 set_scnode_clear_ctx(set);
5078 return LY_SUCCESS;
5079 }
5080
Michal Vasko03ff5a72019-09-11 13:49:33 +02005081 if (set->type != LYXP_SET_NODE_SET) {
5082 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5083 return LY_EVALID;
5084 }
5085
Michal Vaskod989ba02020-08-24 10:59:24 +02005086 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005087 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005088 case LYXP_NODE_NONE:
5089 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005090 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005091 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5092 set->val.nodes[i].type = LYXP_NODE_TEXT;
5093 ++i;
5094 break;
5095 }
Radek Krejci0f969882020-08-21 16:56:47 +02005096 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005097 case LYXP_NODE_ROOT:
5098 case LYXP_NODE_ROOT_CONFIG:
5099 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005100 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005101 set_remove_node(set, i);
5102 break;
5103 }
5104 }
5105
5106 return LY_SUCCESS;
5107}
5108
5109/**
5110 * @brief Execute the XPath translate(string, string, string) function.
5111 * Returns LYXP_SET_STRING with the first argument with the characters
5112 * from the second argument replaced by those on the corresponding
5113 * positions in the third argument.
5114 *
5115 * @param[in] args Array of arguments.
5116 * @param[in] arg_count Count of elements in @p args.
5117 * @param[in,out] set Context and result set at the same time.
5118 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005119 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005120 */
5121static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005122xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005123{
5124 uint16_t i, j, new_used;
5125 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005126 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005127 struct lysc_node_leaf *sleaf;
5128 LY_ERR rc = LY_SUCCESS;
5129
5130 if (options & LYXP_SCNODE_ALL) {
5131 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5132 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5133 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 +02005134 } else if (!warn_is_string_type(sleaf->type)) {
5135 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005136 }
5137 }
5138
5139 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5140 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5141 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 +02005142 } else if (!warn_is_string_type(sleaf->type)) {
5143 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005144 }
5145 }
5146
5147 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5148 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5149 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 +02005150 } else if (!warn_is_string_type(sleaf->type)) {
5151 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005152 }
5153 }
5154 set_scnode_clear_ctx(set);
5155 return rc;
5156 }
5157
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005158 rc = lyxp_set_cast(args[0], 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[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005161 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005162 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005163 LY_CHECK_RET(rc);
5164
5165 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5166 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5167 new_used = 0;
5168
5169 have_removed = 0;
5170 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005171 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005172
5173 for (j = 0; args[1]->val.str[j]; ++j) {
5174 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5175 /* removing this char */
5176 if (j >= strlen(args[2]->val.str)) {
5177 have_removed = 1;
5178 found = 1;
5179 break;
5180 }
5181 /* replacing this char */
5182 new[new_used] = args[2]->val.str[j];
5183 ++new_used;
5184 found = 1;
5185 break;
5186 }
5187 }
5188
5189 /* copying this char */
5190 if (!found) {
5191 new[new_used] = args[0]->val.str[i];
5192 ++new_used;
5193 }
5194 }
5195
5196 if (have_removed) {
5197 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5198 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5199 }
5200 new[new_used] = '\0';
5201
Michal Vaskod3678892020-05-21 10:06:58 +02005202 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005203 set->type = LYXP_SET_STRING;
5204 set->val.str = new;
5205
5206 return LY_SUCCESS;
5207}
5208
5209/**
5210 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5211 * with true value.
5212 *
5213 * @param[in] args Array of arguments.
5214 * @param[in] arg_count Count of elements in @p args.
5215 * @param[in,out] set Context and result set at the same time.
5216 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005217 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005218 */
5219static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005220xpath_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 +02005221{
5222 if (options & LYXP_SCNODE_ALL) {
5223 set_scnode_clear_ctx(set);
5224 return LY_SUCCESS;
5225 }
5226
5227 set_fill_boolean(set, 1);
5228 return LY_SUCCESS;
5229}
5230
5231/*
5232 * moveto functions
5233 *
5234 * They and only they actually change the context (set).
5235 */
5236
5237/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005238 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005239 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005240 * XPath @p set is expected to be a (sc)node set!
5241 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005242 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5243 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
5244 * @param[in] set Set with XPath context.
5245 * @param[out] moveto_mod Expected module of a matching node.
5246 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005247 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005248static LY_ERR
5249moveto_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 +02005250{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005251 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005252 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005253 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005254
Michal Vasko2104e9f2020-03-06 08:23:25 +01005255 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5256
Michal Vasko6346ece2019-09-24 13:12:53 +02005257 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5258 /* specific module */
5259 pref_len = ptr - *qname;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02005260 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, (void *)set->local_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005261
Michal Vasko004d3152020-06-11 19:59:22 +02005262 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005263 if (!mod || !mod->implemented) {
Michal Vasko2104e9f2020-03-06 08:23:25 +01005264 if (set->type == LYXP_SET_SCNODE_SET) {
5265 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname);
5266 } else {
5267 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname);
5268 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005269 return LY_EVALID;
5270 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005271
Michal Vasko6346ece2019-09-24 13:12:53 +02005272 *qname += pref_len + 1;
5273 *qname_len -= pref_len + 1;
5274 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5275 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005276 mod = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02005277 } else if (set->type == LYXP_SET_SCNODE_SET) {
5278 /* current node module */
5279 mod = set->ctx_scnode->module;
Michal Vasko6346ece2019-09-24 13:12:53 +02005280 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005281 /* current node module */
5282 mod = set->ctx_node->schema->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005283 }
5284
Michal Vasko6346ece2019-09-24 13:12:53 +02005285 *moveto_mod = mod;
5286 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005287}
5288
5289/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005290 * @brief Move context @p set to the root. Handles absolute path.
5291 * Result is LYXP_SET_NODE_SET.
5292 *
5293 * @param[in,out] set Set to use.
5294 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005295 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005296 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005297static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005298moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005299{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005300 if (!set) {
Michal Vaskob0099a92020-08-31 14:55:23 +02005301 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005302 }
5303
5304 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005305 set_scnode_clear_ctx(set);
Michal Vaskob0099a92020-08-31 14:55:23 +02005306 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005307 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005308 set->type = LYXP_SET_NODE_SET;
5309 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005310 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005311 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005312
5313 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005314}
5315
5316/**
Michal Vaskoa1424542019-11-14 16:08:52 +01005317 * @brief Check whether a node has some unresolved "when".
5318 *
5319 * @param[in] node Node to check.
5320 * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when")
5321 */
5322static LY_ERR
5323moveto_when_check(const struct lyd_node *node)
5324{
5325 const struct lysc_node *schema;
5326
5327 if (!node) {
5328 return LY_SUCCESS;
5329 }
5330
5331 schema = node->schema;
5332 do {
5333 if (schema->when && !(node->flags & LYD_WHEN_TRUE)) {
5334 return LY_EINCOMPLETE;
5335 }
5336 schema = schema->parent;
5337 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
5338
5339 return LY_SUCCESS;
5340}
5341
5342/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005343 * @brief Check @p node as a part of NameTest processing.
5344 *
5345 * @param[in] node Node to check.
5346 * @param[in] root_type XPath root node type.
Michal Vasko6b26e742020-07-17 15:02:10 +02005347 * @param[in] context_op XPath operation parent.
Michal Vaskod3678892020-05-21 10:06:58 +02005348 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
5349 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005350 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5351 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005352 */
5353static LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +02005354moveto_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 +02005355 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005356{
5357 /* module check */
5358 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005359 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005360 }
5361
Michal Vasko5c4e5892019-11-14 12:31:38 +01005362 /* context check */
5363 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005364 return LY_EINVAL;
Michal Vasko6b26e742020-07-17 15:02:10 +02005365 } else if (context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node->schema != context_op)) {
5366 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005367 }
5368
5369 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005370 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005371 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005372 }
5373
Michal Vaskoa1424542019-11-14 16:08:52 +01005374 /* when check */
5375 if (moveto_when_check(node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005376 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005377 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005378
5379 /* match */
5380 return LY_SUCCESS;
5381}
5382
5383/**
5384 * @brief Check @p node as a part of schema NameTest processing.
5385 *
5386 * @param[in] node Schema node to check.
5387 * @param[in] root_type XPath root node type.
Michal Vasko6b26e742020-07-17 15:02:10 +02005388 * @param[in] context_op XPath operation parent.
Michal Vaskod3678892020-05-21 10:06:58 +02005389 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
5390 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005391 * @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 +02005392 */
5393static LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +02005394moveto_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 +02005395 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005396{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005397 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005398 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005399 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005400 }
5401
5402 /* context check */
5403 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
5404 return LY_EINVAL;
Michal Vasko6b26e742020-07-17 15:02:10 +02005405 } else if (context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != context_op)) {
5406 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005407 }
5408
5409 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005410 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005411 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005412 }
5413
5414 /* match */
5415 return LY_SUCCESS;
5416}
5417
5418/**
Michal Vaskod3678892020-05-21 10:06:58 +02005419 * @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 +02005420 *
5421 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005422 * @param[in] mod Matching node module, NULL for any.
5423 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005424 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5425 */
5426static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005427moveto_node(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005428{
Michal Vaskof03ed032020-03-04 13:31:44 +01005429 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005430 const struct lyd_node *siblings, *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005431 LY_ERR rc;
5432
Michal Vaskod3678892020-05-21 10:06:58 +02005433 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005434 return LY_SUCCESS;
5435 }
5436
5437 if (set->type != LYXP_SET_NODE_SET) {
5438 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5439 return LY_EVALID;
5440 }
5441
Michal Vasko03ff5a72019-09-11 13:49:33 +02005442 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005443 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005444
5445 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 +01005446 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005447
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005448 /* search in all the trees */
Michal Vaskod3678892020-05-21 10:06:58 +02005449 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005450 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005451 /* search in children */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005452 siblings = lyd_node_children(set->val.nodes[i].node, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02005453 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005454
Michal Vaskod3678892020-05-21 10:06:58 +02005455 for (sub = siblings; sub; sub = sub->next) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005456 rc = moveto_node_check(sub, set->root_type, set->context_op, ncname, mod);
Michal Vaskod3678892020-05-21 10:06:58 +02005457 if (rc == LY_SUCCESS) {
5458 if (!replaced) {
5459 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5460 replaced = 1;
5461 } else {
5462 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005463 }
Michal Vaskod3678892020-05-21 10:06:58 +02005464 ++i;
5465 } else if (rc == LY_EINCOMPLETE) {
5466 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005467 }
5468 }
5469
5470 if (!replaced) {
5471 /* no match */
5472 set_remove_node(set, i);
5473 }
5474 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005475
5476 return LY_SUCCESS;
5477}
5478
5479/**
Michal Vaskod3678892020-05-21 10:06:58 +02005480 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5481 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005482 *
5483 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005484 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005485 * @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 +02005486 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5487 */
5488static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02005489moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates)
Michal Vaskod3678892020-05-21 10:06:58 +02005490{
Michal Vasko004d3152020-06-11 19:59:22 +02005491 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005492 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005493 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005494 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005495
Michal Vasko004d3152020-06-11 19:59:22 +02005496 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005497
5498 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005499 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005500 }
5501
5502 if (set->type != LYXP_SET_NODE_SET) {
5503 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 +02005504 ret = LY_EVALID;
5505 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005506 }
5507
5508 /* context check for all the nodes since we have the schema node */
5509 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5510 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005511 goto cleanup;
Radek Krejci0f969882020-08-21 16:56:47 +02005512 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko6b26e742020-07-17 15:02:10 +02005513 && (scnode != set->context_op)) {
5514 lyxp_set_free_content(set);
5515 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005516 }
5517
5518 /* create specific data instance if needed */
5519 if (scnode->nodetype == LYS_LIST) {
5520 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5521 } else if (scnode->nodetype == LYS_LEAFLIST) {
5522 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005523 }
5524
5525 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005526 siblings = NULL;
5527
5528 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5529 assert(!set->val.nodes[i].node);
5530
5531 /* search in all the trees */
5532 siblings = set->tree;
5533 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5534 /* search in children */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005535 siblings = lyd_node_children(set->val.nodes[i].node, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02005536 }
5537
5538 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005539 if (inst) {
5540 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005541 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005542 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005543 }
5544
5545 /* when check */
5546 if (sub && moveto_when_check(sub)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005547 ret = LY_EINCOMPLETE;
5548 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005549 }
5550
5551 if (sub) {
5552 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005553 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005554 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005555 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005556 /* no match */
5557 set_remove_node(set, i);
5558 }
5559 }
5560
Michal Vasko004d3152020-06-11 19:59:22 +02005561cleanup:
5562 lyd_free_tree(inst);
5563 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005564}
5565
5566/**
5567 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5568 *
5569 * @param[in,out] set Set to use.
5570 * @param[in] mod Matching node module, NULL for any.
5571 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005572 * @param[in] options XPath options.
5573 * @return LY_ERR
5574 */
5575static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005576moveto_scnode(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005577{
Radek Krejci857189e2020-09-01 13:26:36 +02005578 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005579 uint32_t getnext_opts;
5580 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005581 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005582 const struct lysc_node *iter, *start_parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005583
Michal Vaskod3678892020-05-21 10:06:58 +02005584 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005585 return LY_SUCCESS;
5586 }
5587
5588 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005589 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 +02005590 return LY_EVALID;
5591 }
5592
Michal Vaskocafad9d2019-11-07 15:20:03 +01005593 /* getnext opts */
5594 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
5595 if (options & LYXP_SCNODE_OUTPUT) {
5596 getnext_opts |= LYS_GETNEXT_OUTPUT;
5597 }
5598
Michal Vasko03ff5a72019-09-11 13:49:33 +02005599 orig_used = set->used;
5600 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005601 uint32_t idx;
5602
Michal Vasko03ff5a72019-09-11 13:49:33 +02005603 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005604 if (set->val.scnodes[i].in_ctx != -2) {
5605 continue;
5606 }
5607
5608 /* remember context node */
5609 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005610 } else {
5611 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005612 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005613
5614 start_parent = set->val.scnodes[i].scnode;
5615
5616 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 +02005617 /* 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 +02005618 * so use it directly (root node itself is useless in this case) */
5619 mod_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02005620 while (mod || (mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005621 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005622 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005623 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005624 if (!moveto_scnode_check(iter, set->root_type, set->context_op, ncname, mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005625 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5626
Michal Vasko03ff5a72019-09-11 13:49:33 +02005627 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005628 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005629 set->val.scnodes[idx].in_ctx = 2;
5630 temp_ctx = 1;
5631 }
5632 }
5633 }
5634
5635 if (!mod_idx) {
Michal Vaskod3678892020-05-21 10:06:58 +02005636 /* mod was specified, we are not going through the whole context */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005637 break;
5638 }
5639 /* next iteration */
Michal Vaskod3678892020-05-21 10:06:58 +02005640 mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005641 }
5642
Michal Vasko519fd602020-05-26 12:17:39 +02005643 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5644 iter = NULL;
5645 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005646 if (!moveto_scnode_check(iter, set->root_type, set->context_op, ncname, (mod ? mod : set->local_mod))) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005647 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5648
5649 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005650 set->val.scnodes[idx].in_ctx = 2;
5651 temp_ctx = 1;
5652 }
5653 }
5654 }
5655 }
5656 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005657
5658 /* correct temporary in_ctx values */
5659 if (temp_ctx) {
5660 for (i = 0; i < orig_used; ++i) {
5661 if (set->val.scnodes[i].in_ctx == 2) {
5662 set->val.scnodes[i].in_ctx = 1;
5663 }
5664 }
5665 }
5666
5667 return LY_SUCCESS;
5668}
5669
5670/**
Michal Vaskod3678892020-05-21 10:06:58 +02005671 * @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 +02005672 * Context position aware.
5673 *
5674 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005675 * @param[in] mod Matching node module, NULL for any.
5676 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005677 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5678 */
5679static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005680moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005681{
5682 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005683 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005684 struct lyxp_set ret_set;
5685 LY_ERR rc;
5686
Michal Vaskod3678892020-05-21 10:06:58 +02005687 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005688 return LY_SUCCESS;
5689 }
5690
5691 if (set->type != LYXP_SET_NODE_SET) {
5692 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5693 return LY_EVALID;
5694 }
5695
Michal Vasko9f96a052020-03-10 09:41:45 +01005696 /* 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 +02005697 rc = moveto_node(set, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005698 LY_CHECK_RET(rc);
5699
Michal Vasko6346ece2019-09-24 13:12:53 +02005700 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005701 set_init(&ret_set, set);
5702 for (i = 0; i < set->used; ++i) {
5703
5704 /* TREE DFS */
5705 start = set->val.nodes[i].node;
5706 for (elem = next = start; elem; elem = next) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005707 rc = moveto_node_check(elem, set->root_type, set->context_op, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005708 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005709 /* add matching node into result set */
5710 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5711 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5712 /* the node is a duplicate, we'll process it later in the set */
5713 goto skip_children;
5714 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005715 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005716 return rc;
5717 } else if (rc == LY_EINVAL) {
5718 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005719 }
5720
5721 /* TREE DFS NEXT ELEM */
5722 /* select element for the next run - children first */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005723 next = lyd_node_children(elem, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005724 if (!next) {
5725skip_children:
5726 /* no children, so try siblings, but only if it's not the start,
5727 * that is considered to be the root and it's siblings are not traversed */
5728 if (elem != start) {
5729 next = elem->next;
5730 } else {
5731 break;
5732 }
5733 }
5734 while (!next) {
5735 /* no siblings, go back through the parents */
5736 if ((struct lyd_node *)elem->parent == start) {
5737 /* we are done, no next element to process */
5738 break;
5739 }
5740 /* parent is already processed, go to its sibling */
5741 elem = (struct lyd_node *)elem->parent;
5742 next = elem->next;
5743 }
5744 }
5745 }
5746
5747 /* make the temporary set the current one */
5748 ret_set.ctx_pos = set->ctx_pos;
5749 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005750 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005751 memcpy(set, &ret_set, sizeof *set);
5752
5753 return LY_SUCCESS;
5754}
5755
5756/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005757 * @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 +02005758 *
5759 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005760 * @param[in] mod Matching node module, NULL for any.
5761 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005762 * @param[in] options XPath options.
5763 * @return LY_ERR
5764 */
5765static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005766moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005767{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005768 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005769 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005770 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005771
Michal Vaskod3678892020-05-21 10:06:58 +02005772 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005773 return LY_SUCCESS;
5774 }
5775
5776 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005777 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 +02005778 return LY_EVALID;
5779 }
5780
Michal Vasko03ff5a72019-09-11 13:49:33 +02005781 orig_used = set->used;
5782 for (i = 0; i < orig_used; ++i) {
5783 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005784 if (set->val.scnodes[i].in_ctx != -2) {
5785 continue;
5786 }
5787
5788 /* remember context node */
5789 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005790 } else {
5791 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005792 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005793
5794 /* TREE DFS */
5795 start = set->val.scnodes[i].scnode;
5796 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005797 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5798 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005799 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005800 }
5801
Michal Vasko6b26e742020-07-17 15:02:10 +02005802 rc = moveto_scnode_check(elem, set->root_type, set->context_op, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005803 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005804 uint32_t idx;
5805
5806 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005807 set->val.scnodes[idx].in_ctx = 1;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005808 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005809 /* we will process it later in the set */
5810 goto skip_children;
5811 }
5812 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005813 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005814 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005815 } else if (rc == LY_EINVAL) {
5816 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005817 }
5818
5819next_iter:
5820 /* TREE DFS NEXT ELEM */
5821 /* select element for the next run - children first */
5822 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005823 if (!next) {
5824skip_children:
5825 /* no children, so try siblings, but only if it's not the start,
5826 * that is considered to be the root and it's siblings are not traversed */
5827 if (elem != start) {
5828 next = elem->next;
5829 } else {
5830 break;
5831 }
5832 }
5833 while (!next) {
5834 /* no siblings, go back through the parents */
5835 if (elem->parent == start) {
5836 /* we are done, no next element to process */
5837 break;
5838 }
5839 /* parent is already processed, go to its sibling */
5840 elem = elem->parent;
5841 next = elem->next;
5842 }
5843 }
5844 }
5845
5846 return LY_SUCCESS;
5847}
5848
5849/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005850 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005851 * Indirectly context position aware.
5852 *
5853 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005854 * @param[in] mod Matching metadata module, NULL for any.
5855 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005856 * @return LY_ERR
5857 */
5858static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005859moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005860{
Michal Vasko9f96a052020-03-10 09:41:45 +01005861 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005862
Michal Vaskod3678892020-05-21 10:06:58 +02005863 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005864 return LY_SUCCESS;
5865 }
5866
5867 if (set->type != LYXP_SET_NODE_SET) {
5868 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5869 return LY_EVALID;
5870 }
5871
Radek Krejci1deb5be2020-08-26 16:43:36 +02005872 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005873 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005874
5875 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5876 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005877 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005878 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005879
5880 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005881 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005882 continue;
5883 }
5884
Michal Vaskod3678892020-05-21 10:06:58 +02005885 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005886 /* match */
5887 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005888 set->val.meta[i].meta = sub;
5889 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005890 /* pos does not change */
5891 replaced = 1;
5892 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005893 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 +02005894 }
5895 ++i;
5896 }
5897 }
5898 }
5899
5900 if (!replaced) {
5901 /* no match */
5902 set_remove_node(set, i);
5903 }
5904 }
5905
5906 return LY_SUCCESS;
5907}
5908
5909/**
5910 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005911 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005912 *
5913 * @param[in,out] set1 Set to use for the result.
5914 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005915 * @return LY_ERR
5916 */
5917static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005918moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005919{
5920 LY_ERR rc;
5921
Michal Vaskod3678892020-05-21 10:06:58 +02005922 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005923 LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
5924 return LY_EVALID;
5925 }
5926
5927 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005928 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005929 return LY_SUCCESS;
5930 }
5931
Michal Vaskod3678892020-05-21 10:06:58 +02005932 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005933 memcpy(set1, set2, sizeof *set1);
5934 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005935 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005936 return LY_SUCCESS;
5937 }
5938
5939 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005940 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005941
5942 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005943 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005944 LY_CHECK_RET(rc);
5945
5946 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005947 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005948
5949 return LY_SUCCESS;
5950}
5951
5952/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005953 * @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 +02005954 * Context position aware.
5955 *
5956 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005957 * @param[in] mod Matching metadata module, NULL for any.
5958 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005959 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5960 */
5961static int
Michal Vaskod3678892020-05-21 10:06:58 +02005962moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005963{
Michal Vasko9f96a052020-03-10 09:41:45 +01005964 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005965 struct lyxp_set *set_all_desc = NULL;
5966 LY_ERR rc;
5967
Michal Vaskod3678892020-05-21 10:06:58 +02005968 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005969 return LY_SUCCESS;
5970 }
5971
5972 if (set->type != LYXP_SET_NODE_SET) {
5973 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5974 return LY_EVALID;
5975 }
5976
Michal Vasko03ff5a72019-09-11 13:49:33 +02005977 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
5978 * but it likely won't be used much, so it's a waste of time */
5979 /* copy the context */
5980 set_all_desc = set_copy(set);
5981 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskod3678892020-05-21 10:06:58 +02005982 rc = moveto_node_alldesc(set_all_desc, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005983 if (rc != LY_SUCCESS) {
5984 lyxp_set_free(set_all_desc);
5985 return rc;
5986 }
5987 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005988 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005989 if (rc != LY_SUCCESS) {
5990 lyxp_set_free(set_all_desc);
5991 return rc;
5992 }
5993 lyxp_set_free(set_all_desc);
5994
Radek Krejci1deb5be2020-08-26 16:43:36 +02005995 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005996 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005997
5998 /* only attributes of an elem can be in the result, skip all the rest,
5999 * we have all attributes qualified in lyd tree */
6000 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006001 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006002 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006003 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006004 continue;
6005 }
6006
Michal Vaskod3678892020-05-21 10:06:58 +02006007 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006008 /* match */
6009 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006010 set->val.meta[i].meta = sub;
6011 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006012 /* pos does not change */
6013 replaced = 1;
6014 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006015 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 +02006016 }
6017 ++i;
6018 }
6019 }
6020 }
6021
6022 if (!replaced) {
6023 /* no match */
6024 set_remove_node(set, i);
6025 }
6026 }
6027
6028 return LY_SUCCESS;
6029}
6030
6031/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006032 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6033 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006034 *
6035 * @param[in] parent Current parent.
6036 * @param[in] parent_pos Position of @p parent.
6037 * @param[in] parent_type Node type of @p parent.
6038 * @param[in,out] to_set Set to use.
6039 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006040 * @param[in] options XPath options.
6041 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6042 */
6043static LY_ERR
6044moveto_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 +02006045 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006046{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006047 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006048 LY_ERR rc;
6049
6050 switch (parent_type) {
6051 case LYXP_NODE_ROOT:
6052 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006053 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006054
Michal Vasko61ac2f62020-05-25 12:39:51 +02006055 /* add all top-level nodes as elements */
6056 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006057 break;
6058 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006059 /* add just the text node of this term element node */
6060 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006061 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6062 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6063 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006064 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006065 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006066
6067 /* add all the children of this node */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02006068 first = lyd_node_children(parent, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006069 break;
6070 default:
6071 LOGINT_RET(parent->schema->module->ctx);
6072 }
6073
Michal Vasko61ac2f62020-05-25 12:39:51 +02006074 /* add all top-level nodes as elements */
6075 LY_LIST_FOR(first, iter) {
6076 /* context check */
6077 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6078 continue;
6079 }
6080
6081 /* when check */
6082 if (moveto_when_check(iter)) {
6083 return LY_EINCOMPLETE;
6084 }
6085
6086 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6087 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6088
6089 /* also add all the children of this node, recursively */
6090 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6091 LY_CHECK_RET(rc);
6092 }
6093 }
6094
Michal Vasko03ff5a72019-09-11 13:49:33 +02006095 return LY_SUCCESS;
6096}
6097
6098/**
6099 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6100 * (or LYXP_SET_EMPTY). Context position aware.
6101 *
6102 * @param[in,out] set Set to use.
6103 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6104 * @param[in] options XPath options.
6105 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6106 */
6107static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006108moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006109{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006110 struct lyxp_set ret_set;
6111 LY_ERR rc;
6112
Michal Vaskod3678892020-05-21 10:06:58 +02006113 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006114 return LY_SUCCESS;
6115 }
6116
6117 if (set->type != LYXP_SET_NODE_SET) {
6118 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6119 return LY_EVALID;
6120 }
6121
6122 /* nothing to do */
6123 if (!all_desc) {
6124 return LY_SUCCESS;
6125 }
6126
Michal Vasko03ff5a72019-09-11 13:49:33 +02006127 /* add all the children, they get added recursively */
6128 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006129 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006130 /* copy the current node to tmp */
6131 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6132
6133 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006134 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006135 continue;
6136 }
6137
Michal Vasko03ff5a72019-09-11 13:49:33 +02006138 /* add all the children */
6139 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 +01006140 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006141 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006142 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006143 return rc;
6144 }
6145 }
6146
6147 /* use the temporary set as the current one */
6148 ret_set.ctx_pos = set->ctx_pos;
6149 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006150 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006151 memcpy(set, &ret_set, sizeof *set);
6152
6153 return LY_SUCCESS;
6154}
6155
6156/**
6157 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6158 * (or LYXP_SET_EMPTY).
6159 *
6160 * @param[in,out] set Set to use.
6161 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6162 * @param[in] options XPath options.
6163 * @return LY_ERR
6164 */
6165static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006166moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006167{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006168 uint32_t getnext_opts;
6169 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006170 const struct lysc_node *iter, *start_parent;
6171 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006172
Michal Vaskod3678892020-05-21 10:06:58 +02006173 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006174 return LY_SUCCESS;
6175 }
6176
6177 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006178 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 +02006179 return LY_EVALID;
6180 }
6181
6182 /* nothing to do */
6183 if (!all_desc) {
6184 return LY_SUCCESS;
6185 }
6186
Michal Vasko519fd602020-05-26 12:17:39 +02006187 /* getnext opts */
6188 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
6189 if (options & LYXP_SCNODE_OUTPUT) {
6190 getnext_opts |= LYS_GETNEXT_OUTPUT;
6191 }
6192
6193 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006194 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006195 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006196 if (set->val.scnodes[i].in_ctx != -2) {
6197 continue;
6198 }
6199
Michal Vasko519fd602020-05-26 12:17:39 +02006200 /* remember context node */
6201 set->val.scnodes[i].in_ctx = -1;
6202 } else {
6203 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006204 }
6205
Michal Vasko519fd602020-05-26 12:17:39 +02006206 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006207
Michal Vasko519fd602020-05-26 12:17:39 +02006208 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6209 /* it can actually be in any module, it's all <running> */
6210 mod_idx = 0;
6211 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6212 iter = NULL;
6213 /* module may not be implemented */
6214 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6215 /* context check */
6216 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6217 continue;
6218 }
6219
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006220 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006221 /* throw away the insert index, we want to consider that node again, recursively */
6222 }
6223 }
6224
6225 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6226 iter = NULL;
6227 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006228 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006229 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006230 continue;
6231 }
6232
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006233 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006234 }
6235 }
6236 }
6237
6238 return LY_SUCCESS;
6239}
6240
6241/**
6242 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6243 * (or LYXP_SET_EMPTY). Context position aware.
6244 *
6245 * @param[in] set Set to use.
6246 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6247 * @param[in] options XPath options.
6248 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6249 */
6250static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006251moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006252{
6253 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006254 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006255 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006256
Michal Vaskod3678892020-05-21 10:06:58 +02006257 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006258 return LY_SUCCESS;
6259 }
6260
6261 if (set->type != LYXP_SET_NODE_SET) {
6262 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6263 return LY_EVALID;
6264 }
6265
6266 if (all_desc) {
6267 /* <path>//.. == <path>//./.. */
6268 rc = moveto_self(set, 1, options);
6269 LY_CHECK_RET(rc);
6270 }
6271
Radek Krejci1deb5be2020-08-26 16:43:36 +02006272 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006273 node = set->val.nodes[i].node;
6274
6275 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6276 new_node = (struct lyd_node *)node->parent;
6277 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6278 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006279 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6280 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006281 if (!new_node) {
6282 LOGINT_RET(set->ctx);
6283 }
6284 } else {
6285 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006286 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006287 continue;
6288 }
6289
Michal Vaskoa1424542019-11-14 16:08:52 +01006290 /* when check */
6291 if (moveto_when_check(new_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006292 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006293 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006294
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006295 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006296 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006297 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006298
Michal Vasko03ff5a72019-09-11 13:49:33 +02006299 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006300 /* 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 +02006301 new_type = LYXP_NODE_ELEM;
6302 }
6303
Michal Vasko03ff5a72019-09-11 13:49:33 +02006304 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006305 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006306 } else {
6307 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006308 }
6309 }
6310
Michal Vasko2caefc12019-11-14 16:07:56 +01006311 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006312 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006313
6314 return LY_SUCCESS;
6315}
6316
6317/**
6318 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6319 * (or LYXP_SET_EMPTY).
6320 *
6321 * @param[in] set Set to use.
6322 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6323 * @param[in] options XPath options.
6324 * @return LY_ERR
6325 */
6326static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006327moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006328{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006329 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006330 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006331 const struct lysc_node *node, *new_node;
6332 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006333
Michal Vaskod3678892020-05-21 10:06:58 +02006334 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006335 return LY_SUCCESS;
6336 }
6337
6338 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006339 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 +02006340 return LY_EVALID;
6341 }
6342
6343 if (all_desc) {
6344 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006345 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006346 }
6347
Michal Vasko03ff5a72019-09-11 13:49:33 +02006348 orig_used = set->used;
6349 for (i = 0; i < orig_used; ++i) {
6350 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006351 if (set->val.scnodes[i].in_ctx != -2) {
6352 continue;
6353 }
6354
6355 /* remember context node */
6356 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01006357 } else {
6358 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006359 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006360
6361 node = set->val.scnodes[i].scnode;
6362
6363 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006364 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006365 } else {
6366 /* root does not have a parent */
6367 continue;
6368 }
6369
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006370 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006371 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006372 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006373
Michal Vasko03ff5a72019-09-11 13:49:33 +02006374 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006375 /* 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 +02006376 new_type = LYXP_NODE_ELEM;
6377 }
6378
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006379 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6380 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006381 set->val.scnodes[idx].in_ctx = 2;
6382 temp_ctx = 1;
6383 }
6384 }
6385
6386 if (temp_ctx) {
6387 for (i = 0; i < orig_used; ++i) {
6388 if (set->val.scnodes[i].in_ctx == 2) {
6389 set->val.scnodes[i].in_ctx = 1;
6390 }
6391 }
6392 }
6393
6394 return LY_SUCCESS;
6395}
6396
6397/**
6398 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6399 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6400 *
6401 * @param[in,out] set1 Set to use for the result.
6402 * @param[in] set2 Set acting as the second operand for @p op.
6403 * @param[in] op Comparison operator to process.
6404 * @param[in] options XPath options.
6405 * @return LY_ERR
6406 */
6407static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006408moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006409{
6410 /*
6411 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6412 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6413 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6414 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6415 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6416 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6417 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6418 *
6419 * '=' or '!='
6420 * BOOLEAN + BOOLEAN
6421 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6422 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6423 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6424 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6425 * NUMBER + NUMBER
6426 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6427 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6428 * STRING + STRING
6429 *
6430 * '<=', '<', '>=', '>'
6431 * NUMBER + NUMBER
6432 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6433 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6434 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6435 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6436 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6437 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6438 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6439 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6440 */
6441 struct lyxp_set iter1, iter2;
6442 int result;
6443 int64_t i;
6444 LY_ERR rc;
6445
Michal Vasko004d3152020-06-11 19:59:22 +02006446 memset(&iter1, 0, sizeof iter1);
6447 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006448
6449 /* iterative evaluation with node-sets */
6450 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6451 if (set1->type == LYXP_SET_NODE_SET) {
6452 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006453 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006454 switch (set2->type) {
6455 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006456 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006457 break;
6458 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006459 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006460 break;
6461 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006462 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006463 break;
6464 }
6465 LY_CHECK_RET(rc);
6466
Michal Vasko4c7763f2020-07-27 17:40:37 +02006467 /* canonize set2 */
6468 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6469
6470 /* compare recursively */
6471 rc = moveto_op_comp(&iter1, &iter2, op, options);
6472 lyxp_set_free_content(&iter2);
6473 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006474
6475 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006476 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006477 set_fill_boolean(set1, 1);
6478 return LY_SUCCESS;
6479 }
6480 }
6481 } else {
6482 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006483 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006484 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006485 case LYXP_SET_NUMBER:
6486 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6487 break;
6488 case LYXP_SET_BOOLEAN:
6489 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6490 break;
6491 default:
6492 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6493 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006494 }
6495 LY_CHECK_RET(rc);
6496
Michal Vasko4c7763f2020-07-27 17:40:37 +02006497 /* canonize set1 */
6498 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 +02006499
Michal Vasko4c7763f2020-07-27 17:40:37 +02006500 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006501 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006502 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006503 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006504
6505 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006506 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006507 set_fill_boolean(set1, 1);
6508 return LY_SUCCESS;
6509 }
6510 }
6511 }
6512
6513 /* false for all nodes */
6514 set_fill_boolean(set1, 0);
6515 return LY_SUCCESS;
6516 }
6517
6518 /* first convert properly */
6519 if ((op[0] == '=') || (op[0] == '!')) {
6520 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006521 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6522 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006523 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006524 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006525 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006526 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006527 LY_CHECK_RET(rc);
6528 } /* else we have 2 strings */
6529 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006530 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006531 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006532 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006533 LY_CHECK_RET(rc);
6534 }
6535
6536 assert(set1->type == set2->type);
6537
6538 /* compute result */
6539 if (op[0] == '=') {
6540 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006541 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006542 } else if (set1->type == LYXP_SET_NUMBER) {
6543 result = (set1->val.num == set2->val.num);
6544 } else {
6545 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006546 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006547 }
6548 } else if (op[0] == '!') {
6549 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006550 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006551 } else if (set1->type == LYXP_SET_NUMBER) {
6552 result = (set1->val.num != set2->val.num);
6553 } else {
6554 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006555 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006556 }
6557 } else {
6558 assert(set1->type == LYXP_SET_NUMBER);
6559 if (op[0] == '<') {
6560 if (op[1] == '=') {
6561 result = (set1->val.num <= set2->val.num);
6562 } else {
6563 result = (set1->val.num < set2->val.num);
6564 }
6565 } else {
6566 if (op[1] == '=') {
6567 result = (set1->val.num >= set2->val.num);
6568 } else {
6569 result = (set1->val.num > set2->val.num);
6570 }
6571 }
6572 }
6573
6574 /* assign result */
6575 if (result) {
6576 set_fill_boolean(set1, 1);
6577 } else {
6578 set_fill_boolean(set1, 0);
6579 }
6580
6581 return LY_SUCCESS;
6582}
6583
6584/**
6585 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6586 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6587 *
6588 * @param[in,out] set1 Set to use for the result.
6589 * @param[in] set2 Set acting as the second operand for @p op.
6590 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006591 * @return LY_ERR
6592 */
6593static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006594moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006595{
6596 LY_ERR rc;
6597
6598 /* unary '-' */
6599 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006600 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006601 LY_CHECK_RET(rc);
6602 set1->val.num *= -1;
6603 lyxp_set_free(set2);
6604 return LY_SUCCESS;
6605 }
6606
6607 assert(set1 && set2);
6608
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006609 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006610 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006611 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006612 LY_CHECK_RET(rc);
6613
6614 switch (op[0]) {
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 /* '*' */
6626 case '*':
6627 set1->val.num *= set2->val.num;
6628 break;
6629
6630 /* 'div' */
6631 case 'd':
6632 set1->val.num /= set2->val.num;
6633 break;
6634
6635 /* 'mod' */
6636 case 'm':
6637 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6638 break;
6639
6640 default:
6641 LOGINT_RET(set1->ctx);
6642 }
6643
6644 return LY_SUCCESS;
6645}
6646
6647/*
6648 * eval functions
6649 *
6650 * They execute a parsed XPath expression on some data subtree.
6651 */
6652
6653/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006654 * @brief Evaluate Predicate. Logs directly on error.
6655 *
Michal Vaskod3678892020-05-21 10:06:58 +02006656 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006657 *
6658 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006659 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006660 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6661 * @param[in] options XPath options.
6662 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6663 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6664 */
6665static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006666eval_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options, ly_bool parent_pos_pred)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006667{
6668 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006669 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006670 uint32_t orig_pos, orig_size;
6671 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006672 struct lyxp_set set2;
6673 struct lyd_node *orig_parent;
6674
6675 /* '[' */
6676 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006677 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6678 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006679
6680 if (!set) {
6681only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006682 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006683 LY_CHECK_RET(rc);
6684 } else if (set->type == LYXP_SET_NODE_SET) {
6685 /* 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 +01006686 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006687
6688 /* empty set, nothing to evaluate */
6689 if (!set->used) {
6690 goto only_parse;
6691 }
6692
Michal Vasko004d3152020-06-11 19:59:22 +02006693 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006694 orig_pos = 0;
6695 orig_size = set->used;
6696 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006697 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006698 set_init(&set2, set);
6699 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6700 /* remember the node context position for position() and context size for last(),
6701 * predicates should always be evaluated with respect to the child axis (since we do
6702 * not support explicit axes) so we assign positions based on their parents */
6703 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6704 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6705 orig_pos = 1;
6706 } else {
6707 ++orig_pos;
6708 }
6709
6710 set2.ctx_pos = orig_pos;
6711 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006712 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006713
Michal Vasko004d3152020-06-11 19:59:22 +02006714 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006715 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006716 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006717 return rc;
6718 }
6719
6720 /* number is a position */
6721 if (set2.type == LYXP_SET_NUMBER) {
6722 if ((long long)set2.val.num == orig_pos) {
6723 set2.val.num = 1;
6724 } else {
6725 set2.val.num = 0;
6726 }
6727 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006728 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006729
6730 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006731 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006732 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006733 }
6734 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006735 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006736
6737 } else if (set->type == LYXP_SET_SCNODE_SET) {
6738 for (i = 0; i < set->used; ++i) {
6739 if (set->val.scnodes[i].in_ctx == 1) {
6740 /* there is a currently-valid node */
6741 break;
6742 }
6743 }
6744 /* empty set, nothing to evaluate */
6745 if (i == set->used) {
6746 goto only_parse;
6747 }
6748
Michal Vasko004d3152020-06-11 19:59:22 +02006749 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006750
Michal Vasko03ff5a72019-09-11 13:49:33 +02006751 /* set special in_ctx to all the valid snodes */
6752 pred_in_ctx = set_scnode_new_in_ctx(set);
6753
6754 /* use the valid snodes one-by-one */
6755 for (i = 0; i < set->used; ++i) {
6756 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6757 continue;
6758 }
6759 set->val.scnodes[i].in_ctx = 1;
6760
Michal Vasko004d3152020-06-11 19:59:22 +02006761 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006762
Michal Vasko004d3152020-06-11 19:59:22 +02006763 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006764 LY_CHECK_RET(rc);
6765
6766 set->val.scnodes[i].in_ctx = pred_in_ctx;
6767 }
6768
6769 /* restore the state as it was before the predicate */
6770 for (i = 0; i < set->used; ++i) {
6771 if (set->val.scnodes[i].in_ctx == 1) {
6772 set->val.scnodes[i].in_ctx = 0;
6773 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6774 set->val.scnodes[i].in_ctx = 1;
6775 }
6776 }
6777
6778 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006779 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006780 set_fill_set(&set2, set);
6781
Michal Vasko004d3152020-06-11 19:59:22 +02006782 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006783 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006784 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006785 return rc;
6786 }
6787
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006788 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006789 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006790 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006791 }
Michal Vaskod3678892020-05-21 10:06:58 +02006792 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006793 }
6794
6795 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006796 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006797 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006798 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6799 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006800
6801 return LY_SUCCESS;
6802}
6803
6804/**
Michal Vaskod3678892020-05-21 10:06:58 +02006805 * @brief Evaluate Literal. Logs directly on error.
6806 *
6807 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006808 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006809 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6810 */
6811static void
Michal Vasko004d3152020-06-11 19:59:22 +02006812eval_literal(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006813{
6814 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006815 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006816 set_fill_string(set, "", 0);
6817 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006818 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 +02006819 }
6820 }
6821 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006822 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6823 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006824}
6825
6826/**
Michal Vasko004d3152020-06-11 19:59:22 +02006827 * @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 +02006828 *
Michal Vasko004d3152020-06-11 19:59:22 +02006829 * @param[in] exp Full parsed XPath expression.
6830 * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success.
6831 * @param[in] scnode Found schema node as context for the predicate.
6832 * @param[in] format Format of any prefixes in key names/values.
6833 * @param[out] predicates Parsed predicates.
6834 * @param[out] pred_type Type of @p predicates.
6835 * @return LY_SUCCESS on success,
6836 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006837 */
6838static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006839eval_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 +02006840 LY_PREFIX_FORMAT format, struct ly_path_predicate **predicates,
6841 enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006842{
Michal Vasko004d3152020-06-11 19:59:22 +02006843 LY_ERR ret = LY_SUCCESS;
6844 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006845 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006846 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006847 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006848 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006849
Michal Vasko004d3152020-06-11 19:59:22 +02006850 assert(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006851
Michal Vasko004d3152020-06-11 19:59:22 +02006852 if (scnode->nodetype == LYS_LIST) {
6853 /* get key count */
6854 if (scnode->flags & LYS_KEYLESS) {
6855 return LY_EINVAL;
6856 }
Radek Krejci1e008d22020-08-17 11:37:37 +02006857 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 +02006858 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006859
Michal Vasko004d3152020-06-11 19:59:22 +02006860 /* learn where the predicates end */
6861 e_idx = *tok_idx;
6862 while (key_count) {
6863 /* '[' */
6864 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6865 return LY_EINVAL;
6866 }
6867 ++e_idx;
6868
6869 /* ']' */
6870 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6871 ++e_idx;
6872 }
6873 ++e_idx;
6874
6875 /* another presumably key predicate parsed */
6876 --key_count;
6877 }
Michal Vasko004d3152020-06-11 19:59:22 +02006878 } else {
6879 /* learn just where this single predicate ends */
6880 e_idx = *tok_idx;
6881
Michal Vaskod3678892020-05-21 10:06:58 +02006882 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006883 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6884 return LY_EINVAL;
6885 }
6886 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006887
6888 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006889 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6890 ++e_idx;
6891 }
6892 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006893 }
6894
Michal Vasko004d3152020-06-11 19:59:22 +02006895 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6896 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6897
6898 /* turn logging off */
6899 prev_lo = ly_log_options(0);
6900
6901 /* parse the predicate(s) */
Michal Vasko6b26e742020-07-17 15:02:10 +02006902 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 +02006903 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
6904
6905 /* compile */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006906 ret = ly_path_compile_predicate(scnode->module->ctx, scnode, scnode->module, scnode, exp2, &pred_idx,
6907 format, scnode->module, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006908 LY_CHECK_GOTO(ret, cleanup);
6909
6910 /* success, the predicate must include all the needed information for hash-based search */
6911 *tok_idx = e_idx;
6912
6913cleanup:
6914 ly_log_options(prev_lo);
6915 lyxp_expr_free(scnode->module->ctx, exp2);
6916 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006917}
6918
6919/**
6920 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
6921 *
6922 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
6923 * [6] NodeTest ::= NameTest | NodeType '(' ')'
6924 * [7] NameTest ::= '*' | NCName ':' '*' | QName
6925 *
6926 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006927 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006928 * @param[in] attr_axis Whether to search attributes or standard nodes.
6929 * @param[in] all_desc Whether to search all the descendants or children only.
6930 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6931 * @param[in] options XPath options.
6932 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6933 */
6934static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006935eval_name_test_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool attr_axis, ly_bool all_desc, struct lyxp_set *set,
Radek Krejci1deb5be2020-08-26 16:43:36 +02006936 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02006937{
Michal Vaskod3678892020-05-21 10:06:58 +02006938 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02006939 const char *ncname, *ncname_dict = NULL;
6940 uint16_t ncname_len;
Michal Vaskod3678892020-05-21 10:06:58 +02006941 const struct lys_module *moveto_mod;
6942 const struct lysc_node *scnode = NULL, *tmp;
Michal Vasko004d3152020-06-11 19:59:22 +02006943 struct ly_path_predicate *predicates = NULL;
6944 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006945 LY_ERR rc = LY_SUCCESS;
6946
6947 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006948 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6949 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006950
6951 if (!set) {
6952 goto moveto;
6953 }
6954
Michal Vasko004d3152020-06-11 19:59:22 +02006955 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
6956 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02006957
6958 /* parse (and skip) module name */
6959 rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod);
6960 LY_CHECK_GOTO(rc, cleanup);
6961
6962 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
6963 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006964 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +02006965 if (set->val.nodes[i].type == set->root_type) {
6966 tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0);
6967 } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM)
6968 && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) {
6969 /* do not repeat the same search */
6970 tmp = lys_find_child(set->val.nodes[i].node->schema, moveto_mod, ncname, ncname_len, 0, 0);
Radek Krejcib1247842020-07-02 16:22:38 +02006971 } else {
6972 tmp = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006973 }
6974
6975 /* additional context check */
6976 if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) {
6977 tmp = NULL;
6978 }
6979
6980 if (tmp) {
6981 if (scnode) {
6982 /* we found a schema node with the same name but at different level, give up, too complicated */
6983 scnode = NULL;
6984 break;
6985 } else {
6986 /* remember the found schema node and continue to make sure it can be used */
6987 scnode = tmp;
6988 }
Michal Vaskod3678892020-05-21 10:06:58 +02006989 }
6990 }
6991
Michal Vasko004d3152020-06-11 19:59:22 +02006992 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
6993 /* try to create the predicates */
6994 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) {
6995 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02006996 scnode = NULL;
6997 }
6998 }
6999 }
7000
Michal Vasko004d3152020-06-11 19:59:22 +02007001 if (!scnode && moveto_mod) {
7002 /* we are not able to match based on a schema node and not all the modules match,
7003 * use dictionary for efficient comparison */
7004 ncname_dict = lydict_insert(set->ctx, ncname, ncname_len);
Michal Vaskod3678892020-05-21 10:06:58 +02007005 }
7006
7007moveto:
7008 /* move to the attribute(s), data node(s), or schema node(s) */
7009 if (attr_axis) {
7010 if (set && (options & LYXP_SCNODE_ALL)) {
7011 set_scnode_clear_ctx(set);
7012 } else {
7013 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007014 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007015 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007016 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007017 }
7018 LY_CHECK_GOTO(rc, cleanup);
7019 }
7020 } else {
7021 if (set && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007022 int64_t i;
7023
Michal Vaskod3678892020-05-21 10:06:58 +02007024 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007025 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007026 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007027 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007028 }
7029 LY_CHECK_GOTO(rc, cleanup);
7030
7031 for (i = set->used - 1; i > -1; --i) {
7032 if (set->val.scnodes[i].in_ctx > 0) {
7033 break;
7034 }
7035 }
7036 if (i == -1) {
7037 path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0);
7038 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
Michal Vasko6b26e742020-07-17 15:02:10 +02007039 ncname_len, ncname, ncname - exp->expr, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007040 free(path);
7041 }
7042 } else {
7043 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007044 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007045 } else {
7046 if (scnode) {
7047 /* we can find the nodes using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02007048 rc = moveto_node_hash(set, scnode, predicates);
Michal Vaskod3678892020-05-21 10:06:58 +02007049 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007050 rc = moveto_node(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007051 }
7052 }
7053 LY_CHECK_GOTO(rc, cleanup);
7054 }
7055 }
7056
7057 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007058 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7059 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007060 LY_CHECK_RET(rc);
7061 }
7062
7063cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007064 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007065 lydict_remove(set->ctx, ncname_dict);
7066 ly_path_predicates_free(set->ctx, pred_type, scnode, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007067 }
Michal Vaskod3678892020-05-21 10:06:58 +02007068 return rc;
7069}
7070
7071/**
7072 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7073 *
7074 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7075 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7076 * [8] NodeType ::= 'text' | 'node'
7077 *
7078 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007079 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007080 * @param[in] attr_axis Whether to search attributes or standard nodes.
7081 * @param[in] all_desc Whether to search all the descendants or children only.
7082 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7083 * @param[in] options XPath options.
7084 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7085 */
7086static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02007087eval_node_type_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool attr_axis, ly_bool all_desc,
Radek Krejci1deb5be2020-08-26 16:43:36 +02007088 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007089{
7090 LY_ERR rc;
7091
7092 /* TODO */
7093 (void)attr_axis;
7094 (void)all_desc;
7095
7096 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007097 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007098 if (set->type == LYXP_SET_SCNODE_SET) {
7099 set_scnode_clear_ctx(set);
7100 /* just for the debug message below */
7101 set = NULL;
7102 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007103 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007104 rc = xpath_node(NULL, 0, set, options);
7105 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007106 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007107 rc = xpath_text(NULL, 0, set, options);
7108 }
7109 LY_CHECK_RET(rc);
7110 }
7111 }
7112 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007113 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7114 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007115
7116 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007117 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007118 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007119 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7120 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007121
7122 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007123 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007124 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007125 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7126 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007127
7128 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007129 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7130 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007131 LY_CHECK_RET(rc);
7132 }
7133
7134 return LY_SUCCESS;
7135}
7136
7137/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007138 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7139 *
7140 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7141 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007142 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007143 *
7144 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007145 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007146 * @param[in] all_desc Whether to search all the descendants or children only.
7147 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7148 * @param[in] options XPath options.
7149 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7150 */
7151static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02007152eval_relative_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007153{
Radek Krejci857189e2020-09-01 13:26:36 +02007154 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007155 LY_ERR rc;
7156
7157 goto step;
7158 do {
7159 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007160 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007161 all_desc = 0;
7162 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007163 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007164 all_desc = 1;
7165 }
7166 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007167 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7168 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007169
7170step:
Michal Vaskod3678892020-05-21 10:06:58 +02007171 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007172 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007173 attr_axis = 1;
7174
7175 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007176 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7177 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007178 } else {
7179 attr_axis = 0;
7180 }
7181
Michal Vasko03ff5a72019-09-11 13:49:33 +02007182 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007183 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007184 case LYXP_TOKEN_DOT:
7185 /* evaluate '.' */
7186 if (set && (options & LYXP_SCNODE_ALL)) {
7187 rc = moveto_scnode_self(set, all_desc, options);
7188 } else {
7189 rc = moveto_self(set, all_desc, options);
7190 }
7191 LY_CHECK_RET(rc);
7192 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007193 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7194 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007195 break;
7196
7197 case LYXP_TOKEN_DDOT:
7198 /* evaluate '..' */
7199 if (set && (options & LYXP_SCNODE_ALL)) {
7200 rc = moveto_scnode_parent(set, all_desc, options);
7201 } else {
7202 rc = moveto_parent(set, all_desc, options);
7203 }
7204 LY_CHECK_RET(rc);
7205 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007206 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7207 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007208 break;
7209
Michal Vasko03ff5a72019-09-11 13:49:33 +02007210 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007211 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007212 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007213 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007214 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007215
Michal Vaskod3678892020-05-21 10:06:58 +02007216 case LYXP_TOKEN_NODETYPE:
7217 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007218 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007219 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007220 break;
7221
7222 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007223 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007224 }
Michal Vasko004d3152020-06-11 19:59:22 +02007225 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007226
7227 return LY_SUCCESS;
7228}
7229
7230/**
7231 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7232 *
7233 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7234 *
7235 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007236 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007237 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7238 * @param[in] options XPath options.
7239 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7240 */
7241static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007242eval_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 +02007243{
Radek Krejci857189e2020-09-01 13:26:36 +02007244 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007245
7246 if (set) {
7247 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007248 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007249 }
7250
7251 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007252 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007253 /* evaluate '/' - deferred */
7254 all_desc = 0;
7255 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007256 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7257 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007258
Michal Vasko004d3152020-06-11 19:59:22 +02007259 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007260 return LY_SUCCESS;
7261 }
Michal Vasko004d3152020-06-11 19:59:22 +02007262 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007263 case LYXP_TOKEN_DOT:
7264 case LYXP_TOKEN_DDOT:
7265 case LYXP_TOKEN_AT:
7266 case LYXP_TOKEN_NAMETEST:
7267 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007268 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007269 break;
7270 default:
7271 break;
7272 }
7273
Michal Vasko03ff5a72019-09-11 13:49:33 +02007274 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007275 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007276 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7277 all_desc = 1;
7278 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007279 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7280 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007281
Michal Vaskob0099a92020-08-31 14:55:23 +02007282 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007283 }
7284
7285 return LY_SUCCESS;
7286}
7287
7288/**
7289 * @brief Evaluate FunctionCall. Logs directly on error.
7290 *
Michal Vaskod3678892020-05-21 10:06:58 +02007291 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007292 *
7293 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007294 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007295 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7296 * @param[in] options XPath options.
7297 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7298 */
7299static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007300eval_function_call(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007301{
7302 LY_ERR rc;
Radek Krejci1deb5be2020-08-26 16:43:36 +02007303 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007304 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007305 struct lyxp_set **args = NULL, **args_aux;
7306
7307 if (set) {
7308 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007309 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007310 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007311 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007312 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007313 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007314 xpath_func = &xpath_sum;
7315 }
7316 break;
7317 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007318 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007319 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007320 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007321 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007322 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007323 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007324 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007325 xpath_func = &xpath_true;
7326 }
7327 break;
7328 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007329 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007330 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007331 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007332 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007333 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007334 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007335 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007336 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007337 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007338 xpath_func = &xpath_deref;
7339 }
7340 break;
7341 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007342 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007343 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007344 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007345 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007346 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007347 xpath_func = &xpath_string;
7348 }
7349 break;
7350 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007351 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007352 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007353 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007354 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007355 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007356 xpath_func = &xpath_current;
7357 }
7358 break;
7359 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007360 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007361 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007362 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007363 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007364 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007365 xpath_func = &xpath_re_match;
7366 }
7367 break;
7368 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007369 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007370 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007371 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007372 xpath_func = &xpath_translate;
7373 }
7374 break;
7375 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007376 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007377 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007378 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007379 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007380 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007381 xpath_func = &xpath_bit_is_set;
7382 }
7383 break;
7384 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007385 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007386 xpath_func = &xpath_starts_with;
7387 }
7388 break;
7389 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007390 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007391 xpath_func = &xpath_derived_from;
7392 }
7393 break;
7394 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007395 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007396 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007397 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007398 xpath_func = &xpath_string_length;
7399 }
7400 break;
7401 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007402 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007403 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007404 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007405 xpath_func = &xpath_substring_after;
7406 }
7407 break;
7408 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007409 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007410 xpath_func = &xpath_substring_before;
7411 }
7412 break;
7413 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007414 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007415 xpath_func = &xpath_derived_from_or_self;
7416 }
7417 break;
7418 }
7419
7420 if (!xpath_func) {
Michal Vasko004d3152020-06-11 19:59:22 +02007421 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 +02007422 return LY_EVALID;
7423 }
7424 }
7425
7426 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007427 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7428 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007429
7430 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007431 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007432 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007433 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7434 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007435
7436 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007437 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007438 if (set) {
7439 args = malloc(sizeof *args);
7440 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7441 arg_count = 1;
7442 args[0] = set_copy(set);
7443 if (!args[0]) {
7444 rc = LY_EMEM;
7445 goto cleanup;
7446 }
7447
Michal Vasko004d3152020-06-11 19:59:22 +02007448 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007449 LY_CHECK_GOTO(rc, cleanup);
7450 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007451 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007452 LY_CHECK_GOTO(rc, cleanup);
7453 }
7454 }
Michal Vasko004d3152020-06-11 19:59:22 +02007455 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007456 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007457 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7458 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007459
7460 if (set) {
7461 ++arg_count;
7462 args_aux = realloc(args, arg_count * sizeof *args);
7463 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7464 args = args_aux;
7465 args[arg_count - 1] = set_copy(set);
7466 if (!args[arg_count - 1]) {
7467 rc = LY_EMEM;
7468 goto cleanup;
7469 }
7470
Michal Vasko004d3152020-06-11 19:59:22 +02007471 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007472 LY_CHECK_GOTO(rc, cleanup);
7473 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007474 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007475 LY_CHECK_GOTO(rc, cleanup);
7476 }
7477 }
7478
7479 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007480 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007481 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007482 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7483 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007484
7485 if (set) {
7486 /* evaluate function */
7487 rc = xpath_func(args, arg_count, set, options);
7488
7489 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007490 /* merge all nodes from arg evaluations */
7491 for (i = 0; i < arg_count; ++i) {
7492 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007493 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007494 }
7495 }
7496 } else {
7497 rc = LY_SUCCESS;
7498 }
7499
7500cleanup:
7501 for (i = 0; i < arg_count; ++i) {
7502 lyxp_set_free(args[i]);
7503 }
7504 free(args);
7505
7506 return rc;
7507}
7508
7509/**
7510 * @brief Evaluate Number. Logs directly on error.
7511 *
7512 * @param[in] ctx Context for errors.
7513 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007514 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007515 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7516 * @return LY_ERR
7517 */
7518static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007519eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007520{
7521 long double num;
7522 char *endptr;
7523
7524 if (set) {
7525 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007526 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007527 if (errno) {
Michal Vasko004d3152020-06-11 19:59:22 +02007528 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 +02007529 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 +02007530 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007531 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007532 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
7533 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 +02007534 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 +02007535 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007536 return LY_EVALID;
7537 }
7538
7539 set_fill_number(set, num);
7540 }
7541
7542 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007543 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7544 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545 return LY_SUCCESS;
7546}
7547
7548/**
7549 * @brief Evaluate PathExpr. Logs directly on error.
7550 *
Michal Vaskod3678892020-05-21 10:06:58 +02007551 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007552 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7553 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7554 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007555 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007556 *
7557 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007558 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007559 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7560 * @param[in] options XPath options.
7561 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7562 */
7563static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007564eval_path_expr(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007565{
Radek Krejci857189e2020-09-01 13:26:36 +02007566 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007567 LY_ERR rc;
7568
Michal Vasko004d3152020-06-11 19:59:22 +02007569 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570 case LYXP_TOKEN_PAR1:
7571 /* '(' Expr ')' */
7572
7573 /* '(' */
7574 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007575 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7576 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007577
7578 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007579 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007580 LY_CHECK_RET(rc);
7581
7582 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007583 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007584 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007585 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7586 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007587
7588 parent_pos_pred = 0;
7589 goto predicate;
7590
7591 case LYXP_TOKEN_DOT:
7592 case LYXP_TOKEN_DDOT:
7593 case LYXP_TOKEN_AT:
7594 case LYXP_TOKEN_NAMETEST:
7595 case LYXP_TOKEN_NODETYPE:
7596 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007597 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007598 LY_CHECK_RET(rc);
7599 break;
7600
7601 case LYXP_TOKEN_FUNCNAME:
7602 /* FunctionCall */
7603 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007604 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007605 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007606 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007607 }
7608 LY_CHECK_RET(rc);
7609
7610 parent_pos_pred = 1;
7611 goto predicate;
7612
Michal Vasko3e48bf32020-06-01 08:39:07 +02007613 case LYXP_TOKEN_OPER_PATH:
7614 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007615 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007616 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007617 LY_CHECK_RET(rc);
7618 break;
7619
7620 case LYXP_TOKEN_LITERAL:
7621 /* Literal */
7622 if (!set || (options & LYXP_SCNODE_ALL)) {
7623 if (set) {
7624 set_scnode_clear_ctx(set);
7625 }
Michal Vasko004d3152020-06-11 19:59:22 +02007626 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007627 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007628 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007629 }
7630
7631 parent_pos_pred = 1;
7632 goto predicate;
7633
7634 case LYXP_TOKEN_NUMBER:
7635 /* Number */
7636 if (!set || (options & LYXP_SCNODE_ALL)) {
7637 if (set) {
7638 set_scnode_clear_ctx(set);
7639 }
Michal Vasko004d3152020-06-11 19:59:22 +02007640 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007641 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007642 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007643 }
7644 LY_CHECK_RET(rc);
7645
7646 parent_pos_pred = 1;
7647 goto predicate;
7648
7649 default:
Michal Vasko004d3152020-06-11 19:59:22 +02007650 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]),
7651 &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007652 return LY_EVALID;
7653 }
7654
7655 return LY_SUCCESS;
7656
7657predicate:
7658 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007659 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7660 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007661 LY_CHECK_RET(rc);
7662 }
7663
7664 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007665 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007666
7667 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007668 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007669 all_desc = 0;
7670 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007671 all_desc = 1;
7672 }
7673
7674 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007675 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7676 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007677
Michal Vasko004d3152020-06-11 19:59:22 +02007678 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007679 LY_CHECK_RET(rc);
7680 }
7681
7682 return LY_SUCCESS;
7683}
7684
7685/**
7686 * @brief Evaluate UnionExpr. Logs directly on error.
7687 *
Michal Vaskod3678892020-05-21 10:06:58 +02007688 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007689 *
7690 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007691 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007692 * @param[in] repeat How many times this expression is repeated.
7693 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7694 * @param[in] options XPath options.
7695 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7696 */
7697static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007698eval_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 +02007699{
7700 LY_ERR rc = LY_SUCCESS;
7701 struct lyxp_set orig_set, set2;
7702 uint16_t i;
7703
7704 assert(repeat);
7705
7706 set_init(&orig_set, set);
7707 set_init(&set2, set);
7708
7709 set_fill_set(&orig_set, set);
7710
Michal Vasko004d3152020-06-11 19:59:22 +02007711 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007712 LY_CHECK_GOTO(rc, cleanup);
7713
7714 /* ('|' PathExpr)* */
7715 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007716 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007717 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007718 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7719 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007720
7721 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007722 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007723 LY_CHECK_GOTO(rc, cleanup);
7724 continue;
7725 }
7726
7727 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007728 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007729 LY_CHECK_GOTO(rc, cleanup);
7730
7731 /* eval */
7732 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007733 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007734 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007735 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007736 LY_CHECK_GOTO(rc, cleanup);
7737 }
7738 }
7739
7740cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007741 lyxp_set_free_content(&orig_set);
7742 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007743 return rc;
7744}
7745
7746/**
7747 * @brief Evaluate UnaryExpr. Logs directly on error.
7748 *
Michal Vaskod3678892020-05-21 10:06:58 +02007749 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007750 *
7751 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007752 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007753 * @param[in] repeat How many times this expression is repeated.
7754 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7755 * @param[in] options XPath options.
7756 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7757 */
7758static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007759eval_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 +02007760{
7761 LY_ERR rc;
7762 uint16_t this_op, i;
7763
7764 assert(repeat);
7765
7766 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007767 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007768 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007769 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 +02007770
7771 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007772 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7773 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007774 }
7775
Michal Vasko004d3152020-06-11 19:59:22 +02007776 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007777 LY_CHECK_RET(rc);
7778
7779 if (set && (repeat % 2)) {
7780 if (options & LYXP_SCNODE_ALL) {
7781 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7782 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007783 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007784 LY_CHECK_RET(rc);
7785 }
7786 }
7787
7788 return LY_SUCCESS;
7789}
7790
7791/**
7792 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7793 *
Michal Vaskod3678892020-05-21 10:06:58 +02007794 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007795 * | MultiplicativeExpr '*' UnaryExpr
7796 * | MultiplicativeExpr 'div' UnaryExpr
7797 * | MultiplicativeExpr 'mod' UnaryExpr
7798 *
7799 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007800 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007801 * @param[in] repeat How many times this expression is repeated.
7802 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7803 * @param[in] options XPath options.
7804 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7805 */
7806static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007807eval_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 +02007808{
7809 LY_ERR rc;
7810 uint16_t this_op;
7811 struct lyxp_set orig_set, set2;
7812 uint16_t i;
7813
7814 assert(repeat);
7815
7816 set_init(&orig_set, set);
7817 set_init(&set2, set);
7818
7819 set_fill_set(&orig_set, set);
7820
Michal Vasko004d3152020-06-11 19:59:22 +02007821 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007822 LY_CHECK_GOTO(rc, cleanup);
7823
7824 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7825 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007826 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007827
Michal Vasko004d3152020-06-11 19:59:22 +02007828 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007829 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007830 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7831 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007832
7833 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007834 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007835 LY_CHECK_GOTO(rc, cleanup);
7836 continue;
7837 }
7838
7839 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007840 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007841 LY_CHECK_GOTO(rc, cleanup);
7842
7843 /* eval */
7844 if (options & LYXP_SCNODE_ALL) {
7845 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007846 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007847 set_scnode_clear_ctx(set);
7848 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007849 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007850 LY_CHECK_GOTO(rc, cleanup);
7851 }
7852 }
7853
7854cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007855 lyxp_set_free_content(&orig_set);
7856 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007857 return rc;
7858}
7859
7860/**
7861 * @brief Evaluate AdditiveExpr. Logs directly on error.
7862 *
Michal Vaskod3678892020-05-21 10:06:58 +02007863 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007864 * | AdditiveExpr '+' MultiplicativeExpr
7865 * | AdditiveExpr '-' MultiplicativeExpr
7866 *
7867 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007868 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007869 * @param[in] repeat How many times this expression is repeated.
7870 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7871 * @param[in] options XPath options.
7872 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7873 */
7874static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007875eval_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 +02007876{
7877 LY_ERR rc;
7878 uint16_t this_op;
7879 struct lyxp_set orig_set, set2;
7880 uint16_t i;
7881
7882 assert(repeat);
7883
7884 set_init(&orig_set, set);
7885 set_init(&set2, set);
7886
7887 set_fill_set(&orig_set, set);
7888
Michal Vasko004d3152020-06-11 19:59:22 +02007889 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007890 LY_CHECK_GOTO(rc, cleanup);
7891
7892 /* ('+' / '-' MultiplicativeExpr)* */
7893 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007894 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007895
Michal Vasko004d3152020-06-11 19:59:22 +02007896 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007897 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007898 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7899 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007900
7901 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007902 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007903 LY_CHECK_GOTO(rc, cleanup);
7904 continue;
7905 }
7906
7907 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007908 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007909 LY_CHECK_GOTO(rc, cleanup);
7910
7911 /* eval */
7912 if (options & LYXP_SCNODE_ALL) {
7913 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007914 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007915 set_scnode_clear_ctx(set);
7916 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007917 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007918 LY_CHECK_GOTO(rc, cleanup);
7919 }
7920 }
7921
7922cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007923 lyxp_set_free_content(&orig_set);
7924 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007925 return rc;
7926}
7927
7928/**
7929 * @brief Evaluate RelationalExpr. Logs directly on error.
7930 *
Michal Vaskod3678892020-05-21 10:06:58 +02007931 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007932 * | RelationalExpr '<' AdditiveExpr
7933 * | RelationalExpr '>' AdditiveExpr
7934 * | RelationalExpr '<=' AdditiveExpr
7935 * | RelationalExpr '>=' AdditiveExpr
7936 *
7937 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007938 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007939 * @param[in] repeat How many times this expression is repeated.
7940 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7941 * @param[in] options XPath options.
7942 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7943 */
7944static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007945eval_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 +02007946{
7947 LY_ERR rc;
7948 uint16_t this_op;
7949 struct lyxp_set orig_set, set2;
7950 uint16_t i;
7951
7952 assert(repeat);
7953
7954 set_init(&orig_set, set);
7955 set_init(&set2, set);
7956
7957 set_fill_set(&orig_set, set);
7958
Michal Vasko004d3152020-06-11 19:59:22 +02007959 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007960 LY_CHECK_GOTO(rc, cleanup);
7961
7962 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
7963 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007964 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007965
Michal Vasko004d3152020-06-11 19:59:22 +02007966 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007967 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007968 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7969 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007970
7971 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007972 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007973 LY_CHECK_GOTO(rc, cleanup);
7974 continue;
7975 }
7976
7977 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007978 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007979 LY_CHECK_GOTO(rc, cleanup);
7980
7981 /* eval */
7982 if (options & LYXP_SCNODE_ALL) {
7983 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007984 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007985 set_scnode_clear_ctx(set);
7986 } else {
7987 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
7988 LY_CHECK_GOTO(rc, cleanup);
7989 }
7990 }
7991
7992cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007993 lyxp_set_free_content(&orig_set);
7994 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007995 return rc;
7996}
7997
7998/**
7999 * @brief Evaluate EqualityExpr. Logs directly on error.
8000 *
Michal Vaskod3678892020-05-21 10:06:58 +02008001 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008002 * | EqualityExpr '!=' RelationalExpr
8003 *
8004 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008005 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008006 * @param[in] repeat How many times this expression is repeated.
8007 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8008 * @param[in] options XPath options.
8009 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8010 */
8011static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02008012eval_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 +02008013{
8014 LY_ERR rc;
8015 uint16_t this_op;
8016 struct lyxp_set orig_set, set2;
8017 uint16_t i;
8018
8019 assert(repeat);
8020
8021 set_init(&orig_set, set);
8022 set_init(&set2, set);
8023
8024 set_fill_set(&orig_set, set);
8025
Michal Vasko004d3152020-06-11 19:59:22 +02008026 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008027 LY_CHECK_GOTO(rc, cleanup);
8028
8029 /* ('=' / '!=' RelationalExpr)* */
8030 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008031 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008032
Michal Vasko004d3152020-06-11 19:59:22 +02008033 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008034 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02008035 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8036 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008037
8038 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008039 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008040 LY_CHECK_GOTO(rc, cleanup);
8041 continue;
8042 }
8043
8044 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008045 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008046 LY_CHECK_GOTO(rc, cleanup);
8047
8048 /* eval */
8049 if (options & LYXP_SCNODE_ALL) {
8050 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008051 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8052 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008053 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008054 set_scnode_clear_ctx(set);
8055 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008056 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8057 LY_CHECK_GOTO(rc, cleanup);
8058 }
8059 }
8060
8061cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008062 lyxp_set_free_content(&orig_set);
8063 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008064 return rc;
8065}
8066
8067/**
8068 * @brief Evaluate AndExpr. Logs directly on error.
8069 *
Michal Vaskod3678892020-05-21 10:06:58 +02008070 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008071 *
8072 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008073 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008074 * @param[in] repeat How many times this expression is repeated.
8075 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8076 * @param[in] options XPath options.
8077 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8078 */
8079static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02008080eval_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 +02008081{
8082 LY_ERR rc;
8083 struct lyxp_set orig_set, set2;
8084 uint16_t i;
8085
8086 assert(repeat);
8087
8088 set_init(&orig_set, set);
8089 set_init(&set2, set);
8090
8091 set_fill_set(&orig_set, set);
8092
Michal Vasko004d3152020-06-11 19:59:22 +02008093 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008094 LY_CHECK_GOTO(rc, cleanup);
8095
8096 /* cast to boolean, we know that will be the final result */
8097 if (set && (options & LYXP_SCNODE_ALL)) {
8098 set_scnode_clear_ctx(set);
8099 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008100 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008101 }
8102
8103 /* ('and' EqualityExpr)* */
8104 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008105 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8106 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
8107 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8108 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008109
8110 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008111 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8112 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008113 LY_CHECK_GOTO(rc, cleanup);
8114 continue;
8115 }
8116
8117 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008118 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008119 LY_CHECK_GOTO(rc, cleanup);
8120
8121 /* eval - just get boolean value actually */
8122 if (set->type == LYXP_SET_SCNODE_SET) {
8123 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008124 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008125 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008126 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008127 set_fill_set(set, &set2);
8128 }
8129 }
8130
8131cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008132 lyxp_set_free_content(&orig_set);
8133 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008134 return rc;
8135}
8136
8137/**
8138 * @brief Evaluate OrExpr. Logs directly on error.
8139 *
Michal Vaskod3678892020-05-21 10:06:58 +02008140 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008141 *
8142 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008143 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008144 * @param[in] repeat How many times this expression is repeated.
8145 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8146 * @param[in] options XPath options.
8147 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8148 */
8149static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02008150eval_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 +02008151{
8152 LY_ERR rc;
8153 struct lyxp_set orig_set, set2;
8154 uint16_t i;
8155
8156 assert(repeat);
8157
8158 set_init(&orig_set, set);
8159 set_init(&set2, set);
8160
8161 set_fill_set(&orig_set, set);
8162
Michal Vasko004d3152020-06-11 19:59:22 +02008163 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008164 LY_CHECK_GOTO(rc, cleanup);
8165
8166 /* cast to boolean, we know that will be the final result */
8167 if (set && (options & LYXP_SCNODE_ALL)) {
8168 set_scnode_clear_ctx(set);
8169 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008170 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008171 }
8172
8173 /* ('or' AndExpr)* */
8174 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008175 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8176 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
8177 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8178 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008179
8180 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008181 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8182 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008183 LY_CHECK_GOTO(rc, cleanup);
8184 continue;
8185 }
8186
8187 set_fill_set(&set2, &orig_set);
8188 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8189 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008190 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008191 LY_CHECK_GOTO(rc, cleanup);
8192
8193 /* eval - just get boolean value actually */
8194 if (set->type == LYXP_SET_SCNODE_SET) {
8195 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008196 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008197 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008198 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008199 set_fill_set(set, &set2);
8200 }
8201 }
8202
8203cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008204 lyxp_set_free_content(&orig_set);
8205 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008206 return rc;
8207}
8208
8209/**
Michal Vasko004d3152020-06-11 19:59:22 +02008210 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008211 *
8212 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008213 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008214 * @param[in] etype Expression type to evaluate.
8215 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8216 * @param[in] options XPath options.
8217 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8218 */
8219static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02008220eval_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 +02008221{
8222 uint16_t i, count;
8223 enum lyxp_expr_type next_etype;
8224 LY_ERR rc;
8225
8226 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008227 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008228 next_etype = LYXP_EXPR_NONE;
8229 } else {
8230 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008231 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008232
8233 /* select one-priority lower because etype expression called us */
8234 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008235 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008236 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008237 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008238 } else {
8239 next_etype = LYXP_EXPR_NONE;
8240 }
8241 }
8242
8243 /* decide what expression are we parsing based on the repeat */
8244 switch (next_etype) {
8245 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008246 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008247 break;
8248 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008249 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008250 break;
8251 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008252 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008253 break;
8254 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008255 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008256 break;
8257 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008258 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008259 break;
8260 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008261 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008262 break;
8263 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008264 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008265 break;
8266 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008267 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008268 break;
8269 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008270 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008271 break;
8272 default:
8273 LOGINT_RET(set->ctx);
8274 }
8275
8276 return rc;
8277}
8278
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008279/**
8280 * @brief Get root type.
8281 *
8282 * @param[in] ctx_node Context node.
8283 * @param[in] ctx_scnode Schema context node.
8284 * @param[in] options XPath options.
8285 * @return Root type.
8286 */
8287static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008288lyxp_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 +01008289{
Michal Vasko6b26e742020-07-17 15:02:10 +02008290 const struct lysc_node *op;
8291
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008292 if (options & LYXP_SCNODE_ALL) {
Radek Krejci1e008d22020-08-17 11:37:37 +02008293 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008294
8295 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008296 /* general root that can access everything */
8297 return LYXP_NODE_ROOT;
8298 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8299 /* root context node can access only config data (because we said so, it is unspecified) */
8300 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008301 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008302 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008303 }
8304
Michal Vasko6b26e742020-07-17 15:02:10 +02008305 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008306 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008307
8308 if (!ctx_node || (!op && (ctx_node->schema->flags & LYS_CONFIG_W))) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008309 /* root context node can access only config data (because we said so, it is unspecified) */
8310 return LYXP_NODE_ROOT_CONFIG;
8311 }
8312
8313 return LYXP_NODE_ROOT;
8314}
8315
Michal Vasko03ff5a72019-09-11 13:49:33 +02008316LY_ERR
Michal Vaskoc8a230d2020-08-14 12:17:10 +02008317lyxp_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 +02008318 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 +02008319{
Michal Vasko004d3152020-06-11 19:59:22 +02008320 uint16_t tok_idx = 0;
8321 const struct lyd_node *real_ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008322 LY_ERR rc;
8323
Michal Vasko004d3152020-06-11 19:59:22 +02008324 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL);
8325
8326 if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) {
8327 /* we always need some context node because it is used for resolving unqualified names */
8328 real_ctx_node = NULL;
8329 } else {
8330 real_ctx_node = ctx_node;
8331 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008332
8333 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008334 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008335 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008336 set->type = LYXP_SET_NODE_SET;
Michal Vasko004d3152020-06-11 19:59:22 +02008337 set_insert_node(set, (struct lyd_node *)real_ctx_node, 0, ctx_node_type, 0);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008338 set->ctx = local_mod->ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008339 set->ctx_node = ctx_node;
Michal Vasko004d3152020-06-11 19:59:22 +02008340 set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options);
Michal Vasko6b26e742020-07-17 15:02:10 +02008341 for (set->context_op = ctx_node->schema;
Radek Krejci0f969882020-08-21 16:56:47 +02008342 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8343 set->context_op = set->context_op->parent) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008344 set->local_mod = local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +01008345 set->tree = tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008346 set->format = format;
8347
8348 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008349 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008350 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008351 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008352 }
8353
Michal Vasko03ff5a72019-09-11 13:49:33 +02008354 return rc;
8355}
8356
8357#if 0
8358
8359/* full xml printing of set elements, not used currently */
8360
8361void
8362lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8363{
8364 uint32_t i;
8365 char *str_num;
8366 struct lyout out;
8367
8368 memset(&out, 0, sizeof out);
8369
8370 out.type = LYOUT_STREAM;
8371 out.method.f = f;
8372
8373 switch (set->type) {
8374 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008375 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008376 break;
8377 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008378 ly_print_(&out, "Boolean XPath set:\n");
8379 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008380 break;
8381 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008382 ly_print_(&out, "String XPath set:\n");
8383 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008384 break;
8385 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008386 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008387
8388 if (isnan(set->value.num)) {
8389 str_num = strdup("NaN");
8390 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8391 str_num = strdup("0");
8392 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8393 str_num = strdup("Infinity");
8394 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8395 str_num = strdup("-Infinity");
8396 } else if ((long long)set->value.num == set->value.num) {
8397 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8398 str_num = NULL;
8399 }
8400 } else {
8401 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8402 str_num = NULL;
8403 }
8404 }
8405 if (!str_num) {
8406 LOGMEM;
8407 return;
8408 }
Michal Vasko5233e962020-08-14 14:26:20 +02008409 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008410 free(str_num);
8411 break;
8412 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008413 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008414
8415 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008416 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008417 switch (set->node_type[i]) {
8418 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008419 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008420 break;
8421 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008422 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008423 break;
8424 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008425 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008426 break;
8427 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008428 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008429 break;
8430 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008431 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008432 break;
8433 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008434 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008435 break;
8436 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008437 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008438 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008439 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008440 break;
8441 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008442 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 +02008443 break;
8444 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008445 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 +02008446 break;
8447 }
8448 }
8449 break;
8450 }
8451}
8452
8453#endif
8454
8455LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008456lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008457{
8458 long double num;
8459 char *str;
8460 LY_ERR rc;
8461
8462 if (!set || (set->type == target)) {
8463 return LY_SUCCESS;
8464 }
8465
8466 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008467 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008468
8469 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008470 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008471 return LY_EINVAL;
8472 }
8473
8474 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008475 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008476 switch (set->type) {
8477 case LYXP_SET_NUMBER:
8478 if (isnan(set->val.num)) {
8479 set->val.str = strdup("NaN");
8480 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8481 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8482 set->val.str = strdup("0");
8483 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8484 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8485 set->val.str = strdup("Infinity");
8486 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8487 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8488 set->val.str = strdup("-Infinity");
8489 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8490 } else if ((long long)set->val.num == set->val.num) {
8491 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8492 LOGMEM_RET(set->ctx);
8493 }
8494 set->val.str = str;
8495 } else {
8496 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8497 LOGMEM_RET(set->ctx);
8498 }
8499 set->val.str = str;
8500 }
8501 break;
8502 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008503 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008504 set->val.str = strdup("true");
8505 } else {
8506 set->val.str = strdup("false");
8507 }
8508 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8509 break;
8510 case LYXP_SET_NODE_SET:
8511 assert(set->used);
8512
8513 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008514 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008515
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008516 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008517 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008518 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008519 set->val.str = str;
8520 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008521 default:
8522 LOGINT_RET(set->ctx);
8523 }
8524 set->type = LYXP_SET_STRING;
8525 }
8526
8527 /* to NUMBER */
8528 if (target == LYXP_SET_NUMBER) {
8529 switch (set->type) {
8530 case LYXP_SET_STRING:
8531 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008532 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008533 set->val.num = num;
8534 break;
8535 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008536 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008537 set->val.num = 1;
8538 } else {
8539 set->val.num = 0;
8540 }
8541 break;
8542 default:
8543 LOGINT_RET(set->ctx);
8544 }
8545 set->type = LYXP_SET_NUMBER;
8546 }
8547
8548 /* to BOOLEAN */
8549 if (target == LYXP_SET_BOOLEAN) {
8550 switch (set->type) {
8551 case LYXP_SET_NUMBER:
8552 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008553 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008554 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008555 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008556 }
8557 break;
8558 case LYXP_SET_STRING:
8559 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008560 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008561 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008562 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008563 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008564 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008565 }
8566 break;
8567 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008568 if (set->used) {
8569 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008570 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008571 } else {
8572 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008573 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008574 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008575 break;
8576 default:
8577 LOGINT_RET(set->ctx);
8578 }
8579 set->type = LYXP_SET_BOOLEAN;
8580 }
8581
Michal Vasko03ff5a72019-09-11 13:49:33 +02008582 return LY_SUCCESS;
8583}
8584
8585LY_ERR
Michal Vaskoc8a230d2020-08-14 12:17:10 +02008586lyxp_atomize(struct lyxp_expr *exp, LY_PREFIX_FORMAT format, const struct lys_module *local_mod,
Radek Krejci1deb5be2020-08-26 16:43:36 +02008587 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 +02008588{
8589 struct ly_ctx *ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008590 const struct lysc_node *real_ctx_scnode;
8591 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008592
Michal Vasko004d3152020-06-11 19:59:22 +02008593 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_scnode, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008594
8595 ctx = local_mod->ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008596 if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) {
8597 /* we always need some context node because it is used for resolving unqualified names */
8598 real_ctx_scnode = NULL;
8599 } else {
8600 real_ctx_scnode = ctx_scnode;
8601 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008602
8603 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008604 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008605 memset(set, 0, sizeof *set);
8606 set->type = LYXP_SET_SCNODE_SET;
Radek Krejciaa6b53f2020-08-27 15:19:03 +02008607 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, real_ctx_scnode, ctx_scnode_type, NULL));
Michal Vasko5c4e5892019-11-14 12:31:38 +01008608 set->val.scnodes[0].in_ctx = -2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008609 set->ctx = ctx;
8610 set->ctx_scnode = ctx_scnode;
Michal Vasko004d3152020-06-11 19:59:22 +02008611 set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options);
Michal Vasko6b26e742020-07-17 15:02:10 +02008612 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008613 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8614 set->context_op = set->context_op->parent) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008615 set->local_mod = local_mod;
8616 set->format = format;
8617
8618 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008619 return eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008620}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008621
8622API const char *
8623lyxp_get_expr(const struct lyxp_expr *path)
8624{
8625 if (!path) {
8626 return NULL;
8627 }
8628
8629 return path->expr;
8630}