blob: 23539a6c111859309e07cfd552dc174afa71a066 [file] [log] [blame]
Radek Krejcib1646a92018-11-02 16:08:26 +01001/**
2 * @file xpath.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG XPath evaluation functions
5 *
Michal Vasko61ac2f62020-05-25 12:39:51 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcib1646a92018-11-02 16:08:26 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Michal Vasko03ff5a72019-09-11 13:49:33 +020014#define _GNU_SOURCE
15
16/* needed by libmath functions isfinite(), isinf(), isnan(), signbit(), ... */
17#define _ISOC99_SOURCE
Radek Krejcib1646a92018-11-02 16:08:26 +010018
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010020
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010022#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include <errno.h>
24#include <limits.h>
25#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010027#include <stdio.h>
28#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010029#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010030
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020032#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020033#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020035#include "hash_table.h"
Radek Krejci7931b192020-06-25 17:05:03 +020036#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020037#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020038#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020039#include "printer.h"
40#include "printer_data.h"
41#include "tree.h"
42#include "tree_data_internal.h"
43#include "tree_schema_internal.h"
44#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020045
Michal Vasko004d3152020-06-11 19:59:22 +020046static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx);
Radek Krejci1deb5be2020-08-26 16:43:36 +020047static LY_ERR eval_expr_select(struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, uint32_t options);
Michal Vasko03ff5a72019-09-11 13:49:33 +020048
49/**
50 * @brief Print the type of an XPath \p set.
51 *
52 * @param[in] set Set to use.
53 * @return Set type string.
54 */
55static const char *
56print_set_type(struct lyxp_set *set)
57{
58 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020059 case LYXP_SET_NODE_SET:
60 return "node set";
61 case LYXP_SET_SCNODE_SET:
62 return "schema node set";
63 case LYXP_SET_BOOLEAN:
64 return "boolean";
65 case LYXP_SET_NUMBER:
66 return "number";
67 case LYXP_SET_STRING:
68 return "string";
69 }
70
71 return NULL;
72}
73
Michal Vasko24cddf82020-06-01 08:17:01 +020074const char *
75lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020076{
77 switch (tok) {
78 case LYXP_TOKEN_PAR1:
79 return "(";
80 case LYXP_TOKEN_PAR2:
81 return ")";
82 case LYXP_TOKEN_BRACK1:
83 return "[";
84 case LYXP_TOKEN_BRACK2:
85 return "]";
86 case LYXP_TOKEN_DOT:
87 return ".";
88 case LYXP_TOKEN_DDOT:
89 return "..";
90 case LYXP_TOKEN_AT:
91 return "@";
92 case LYXP_TOKEN_COMMA:
93 return ",";
94 case LYXP_TOKEN_NAMETEST:
95 return "NameTest";
96 case LYXP_TOKEN_NODETYPE:
97 return "NodeType";
98 case LYXP_TOKEN_FUNCNAME:
99 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200100 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200101 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200102 case LYXP_TOKEN_OPER_EQUAL:
103 return "Operator(Equal)";
104 case LYXP_TOKEN_OPER_NEQUAL:
105 return "Operator(Non-equal)";
106 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200107 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200108 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200109 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200110 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200111 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200112 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200113 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200114 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200115 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200116 case LYXP_TOKEN_LITERAL:
117 return "Literal";
118 case LYXP_TOKEN_NUMBER:
119 return "Number";
120 default:
121 LOGINT(NULL);
122 return "";
123 }
124}
125
126/**
127 * @brief Print the whole expression \p exp to debug output.
128 *
129 * @param[in] exp Expression to use.
130 */
131static void
132print_expr_struct_debug(struct lyxp_expr *exp)
133{
134 uint16_t i, j;
135 char tmp[128];
136
Radek Krejci52b6d512020-10-12 12:33:17 +0200137 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200138 return;
139 }
140
141 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
142 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200143 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200144 &exp->expr[exp->tok_pos[i]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200145 if (exp->repeat[i]) {
146 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
147 for (j = 1; exp->repeat[i][j]; ++j) {
148 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
149 }
150 strcat(tmp, ")");
151 }
152 LOGDBG(LY_LDGXPATH, tmp);
153 }
154}
155
156#ifndef NDEBUG
157
158/**
159 * @brief Print XPath set content to debug output.
160 *
161 * @param[in] set Set to print.
162 */
163static void
164print_set_debug(struct lyxp_set *set)
165{
166 uint32_t i;
167 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200168 struct lyxp_set_node *item;
169 struct lyxp_set_scnode *sitem;
170
Radek Krejci52b6d512020-10-12 12:33:17 +0200171 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200172 return;
173 }
174
175 switch (set->type) {
176 case LYXP_SET_NODE_SET:
177 LOGDBG(LY_LDGXPATH, "set NODE SET:");
178 for (i = 0; i < set->used; ++i) {
179 item = &set->val.nodes[i];
180
181 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100182 case LYXP_NODE_NONE:
183 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
184 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200185 case LYXP_NODE_ROOT:
186 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
187 break;
188 case LYXP_NODE_ROOT_CONFIG:
189 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
190 break;
191 case LYXP_NODE_ELEM:
Michal Vasko69730152020-10-09 16:30:07 +0200192 if ((item->node->schema->nodetype == LYS_LIST) &&
193 (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200194 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200195 item->node->schema->name, LYD_CANON_VALUE(lyd_child(item->node)));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200196 } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) {
197 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200198 item->node->schema->name, LYD_CANON_VALUE(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200199 } else {
200 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
201 }
202 break;
203 case LYXP_NODE_TEXT:
204 if (item->node->schema->nodetype & LYS_ANYDATA) {
205 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200206 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200207 } else {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200208 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, LYD_CANON_VALUE(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200209 }
210 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100211 case LYXP_NODE_META:
212 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
Michal Vasko69730152020-10-09 16:30:07 +0200213 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200214 break;
215 }
216 }
217 break;
218
219 case LYXP_SET_SCNODE_SET:
220 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
221 for (i = 0; i < set->used; ++i) {
222 sitem = &set->val.scnodes[i];
223
224 switch (sitem->type) {
225 case LYXP_NODE_ROOT:
226 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
227 break;
228 case LYXP_NODE_ROOT_CONFIG:
229 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
230 break;
231 case LYXP_NODE_ELEM:
232 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
233 break;
234 default:
235 LOGINT(NULL);
236 break;
237 }
238 }
239 break;
240
Michal Vasko03ff5a72019-09-11 13:49:33 +0200241 case LYXP_SET_BOOLEAN:
242 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200243 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200244 break;
245
246 case LYXP_SET_STRING:
247 LOGDBG(LY_LDGXPATH, "set STRING");
248 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
249 break;
250
251 case LYXP_SET_NUMBER:
252 LOGDBG(LY_LDGXPATH, "set NUMBER");
253
254 if (isnan(set->val.num)) {
255 str = strdup("NaN");
256 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
257 str = strdup("0");
258 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
259 str = strdup("Infinity");
260 } else if (isinf(set->val.num) && signbit(set->val.num)) {
261 str = strdup("-Infinity");
262 } else if ((long long)set->val.num == set->val.num) {
263 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
264 str = NULL;
265 }
266 } else {
267 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
268 str = NULL;
269 }
270 }
271 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
272
273 LOGDBG(LY_LDGXPATH, "\t%s", str);
274 free(str);
275 }
276}
277
278#endif
279
280/**
281 * @brief Realloc the string \p str.
282 *
283 * @param[in] ctx libyang context for logging.
284 * @param[in] needed How much free space is required.
285 * @param[in,out] str Pointer to the string to use.
286 * @param[in,out] used Used bytes in \p str.
287 * @param[in,out] size Allocated bytes in \p str.
288 * @return LY_ERR
289 */
290static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100291cast_string_realloc(const struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200292{
293 if (*size - *used < needed) {
294 do {
295 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
296 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
297 return LY_EINVAL;
298 }
299 *size += LYXP_STRING_CAST_SIZE_STEP;
300 } while (*size - *used < needed);
301 *str = ly_realloc(*str, *size * sizeof(char));
302 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
303 }
304
305 return LY_SUCCESS;
306}
307
308/**
309 * @brief Cast nodes recursively to one string @p str.
310 *
311 * @param[in] node Node to cast.
312 * @param[in] fake_cont Whether to put the data into a "fake" container.
313 * @param[in] root_type Type of the XPath root.
314 * @param[in] indent Current indent.
315 * @param[in,out] str Resulting string.
316 * @param[in,out] used Used bytes in @p str.
317 * @param[in,out] size Allocated bytes in @p str.
318 * @return LY_ERR
319 */
320static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200321cast_string_recursive(const struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
Radek Krejci0f969882020-08-21 16:56:47 +0200322 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200323{
Radek Krejci7f769d72020-07-11 23:13:56 +0200324 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200325 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200326 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200327 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200328 struct lyd_node_any *any;
329 LY_ERR rc;
330
331 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
332 return LY_SUCCESS;
333 }
334
335 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200336 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200337 LY_CHECK_RET(rc);
338 strcpy(*str + (*used - 1), "\n");
339 ++(*used);
340
341 ++indent;
342 }
343
344 switch (node->schema->nodetype) {
345 case LYS_CONTAINER:
346 case LYS_LIST:
347 case LYS_RPC:
348 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200349 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200350 LY_CHECK_RET(rc);
351 strcpy(*str + (*used - 1), "\n");
352 ++(*used);
353
Radek Krejcia1c1e542020-09-29 16:06:52 +0200354 for (child = lyd_child(node); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200355 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
356 LY_CHECK_RET(rc);
357 }
358
359 break;
360
361 case LYS_LEAF:
362 case LYS_LEAFLIST:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200363 value_str = LYD_CANON_VALUE(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200364
365 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200366 LY_CHECK_RET(cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200367 memset(*str + (*used - 1), ' ', indent * 2);
368 *used += indent * 2;
369
370 /* print value */
371 if (*used == 1) {
372 sprintf(*str + (*used - 1), "%s", value_str);
373 *used += strlen(value_str);
374 } else {
375 sprintf(*str + (*used - 1), "%s\n", value_str);
376 *used += strlen(value_str) + 1;
377 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200378
379 break;
380
381 case LYS_ANYXML:
382 case LYS_ANYDATA:
383 any = (struct lyd_node_any *)node;
384 if (!(void *)any->value.tree) {
385 /* no content */
386 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200387 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200388 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200389 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100390
Michal Vasko60ea6352020-06-29 13:39:39 +0200391 if (any->value_type == LYD_ANYDATA_LYB) {
392 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200393 if (lyd_parse_data_mem((struct ly_ctx *)LYD_CTX(node), any->value.mem, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200394 /* successfully parsed */
395 free(any->value.mem);
396 any->value.tree = tree;
397 any->value_type = LYD_ANYDATA_DATATREE;
398 }
Radek Krejci7931b192020-06-25 17:05:03 +0200399 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200400 }
401
Michal Vasko03ff5a72019-09-11 13:49:33 +0200402 switch (any->value_type) {
403 case LYD_ANYDATA_STRING:
404 case LYD_ANYDATA_XML:
405 case LYD_ANYDATA_JSON:
406 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200407 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200408 break;
409 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200410 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200411 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200412 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100413 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200414 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200415 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200416 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200417 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200418 }
419 }
420
421 line = strtok_r(buf, "\n", &ptr);
422 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200423 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200424 if (rc != LY_SUCCESS) {
425 free(buf);
426 return rc;
427 }
428 memset(*str + (*used - 1), ' ', indent * 2);
429 *used += indent * 2;
430
431 strcpy(*str + (*used - 1), line);
432 *used += strlen(line);
433
434 strcpy(*str + (*used - 1), "\n");
435 *used += 1;
436 } while ((line = strtok_r(NULL, "\n", &ptr)));
437
438 free(buf);
439 break;
440
441 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200442 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200443 }
444
445 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200446 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200447 LY_CHECK_RET(rc);
448 strcpy(*str + (*used - 1), "\n");
449 ++(*used);
450
451 --indent;
452 }
453
454 return LY_SUCCESS;
455}
456
457/**
458 * @brief Cast an element into a string.
459 *
460 * @param[in] node Node to cast.
461 * @param[in] fake_cont Whether to put the data into a "fake" container.
462 * @param[in] root_type Type of the XPath root.
463 * @param[out] str Element cast to dynamically-allocated string.
464 * @return LY_ERR
465 */
466static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200467cast_string_elem(struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200468{
469 uint16_t used, size;
470 LY_ERR rc;
471
472 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200473 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200474 (*str)[0] = '\0';
475 used = 1;
476 size = LYXP_STRING_CAST_SIZE_START;
477
478 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
479 if (rc != LY_SUCCESS) {
480 free(*str);
481 return rc;
482 }
483
484 if (size > used) {
485 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200486 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200487 }
488 return LY_SUCCESS;
489}
490
491/**
492 * @brief Cast a LYXP_SET_NODE_SET set into a string.
493 * Context position aware.
494 *
495 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200496 * @param[out] str Cast dynamically-allocated string.
497 * @return LY_ERR
498 */
499static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100500cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200501{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200502 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100503 case LYXP_NODE_NONE:
504 /* invalid */
505 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200506 case LYXP_NODE_ROOT:
507 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100508 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200509 case LYXP_NODE_ELEM:
510 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100511 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100512 case LYXP_NODE_META:
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200513 *str = strdup(set->val.meta[0].meta->value.canonical);
514 if (!*str) {
515 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200516 }
517 return LY_SUCCESS;
518 }
519
520 LOGINT_RET(set->ctx);
521}
522
523/**
524 * @brief Cast a string into an XPath number.
525 *
526 * @param[in] str String to use.
527 * @return Cast number.
528 */
529static long double
530cast_string_to_number(const char *str)
531{
532 long double num;
533 char *ptr;
534
535 errno = 0;
536 num = strtold(str, &ptr);
537 if (errno || *ptr) {
538 num = NAN;
539 }
540 return num;
541}
542
543/**
544 * @brief Callback for checking value equality.
545 *
Radek Krejci857189e2020-09-01 13:26:36 +0200546 * Implementation of ::values_equal_cb.
547 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200548 * @param[in] val1_p First value.
549 * @param[in] val2_p Second value.
550 * @param[in] mod Whether hash table is being modified.
551 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200552 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200553 */
Radek Krejci857189e2020-09-01 13:26:36 +0200554static ly_bool
555set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200556{
557 struct lyxp_set_hash_node *val1, *val2;
558
559 val1 = (struct lyxp_set_hash_node *)val1_p;
560 val2 = (struct lyxp_set_hash_node *)val2_p;
561
562 if ((val1->node == val2->node) && (val1->type == val2->type)) {
563 return 1;
564 }
565
566 return 0;
567}
568
569/**
570 * @brief Insert node and its hash into set.
571 *
572 * @param[in] set et to insert to.
573 * @param[in] node Node with hash.
574 * @param[in] type Node type.
575 */
576static void
577set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
578{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200579 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200580 uint32_t i, hash;
581 struct lyxp_set_hash_node hnode;
582
583 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
584 /* create hash table and add all the nodes */
585 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
586 for (i = 0; i < set->used; ++i) {
587 hnode.node = set->val.nodes[i].node;
588 hnode.type = set->val.nodes[i].type;
589
590 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
591 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
592 hash = dict_hash_multi(hash, NULL, 0);
593
594 r = lyht_insert(set->ht, &hnode, hash, NULL);
595 assert(!r);
596 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200597
Michal Vasko9d6befd2019-12-11 14:56:56 +0100598 if (hnode.node == node) {
599 /* it was just added, do not add it twice */
600 node = NULL;
601 }
602 }
603 }
604
605 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200606 /* add the new node into hash table */
607 hnode.node = node;
608 hnode.type = type;
609
610 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
611 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
612 hash = dict_hash_multi(hash, NULL, 0);
613
614 r = lyht_insert(set->ht, &hnode, hash, NULL);
615 assert(!r);
616 (void)r;
617 }
618}
619
620/**
621 * @brief Remove node and its hash from set.
622 *
623 * @param[in] set Set to remove from.
624 * @param[in] node Node to remove.
625 * @param[in] type Node type.
626 */
627static void
628set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
629{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200630 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200631 struct lyxp_set_hash_node hnode;
632 uint32_t hash;
633
634 if (set->ht) {
635 hnode.node = node;
636 hnode.type = type;
637
638 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
639 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
640 hash = dict_hash_multi(hash, NULL, 0);
641
642 r = lyht_remove(set->ht, &hnode, hash);
643 assert(!r);
644 (void)r;
645
646 if (!set->ht->used) {
647 lyht_free(set->ht);
648 set->ht = NULL;
649 }
650 }
651}
652
653/**
654 * @brief Check whether node is in set based on its hash.
655 *
656 * @param[in] set Set to search in.
657 * @param[in] node Node to search for.
658 * @param[in] type Node type.
659 * @param[in] skip_idx Index in @p set to skip.
660 * @return LY_ERR
661 */
662static LY_ERR
663set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
664{
665 struct lyxp_set_hash_node hnode, *match_p;
666 uint32_t hash;
667
668 hnode.node = node;
669 hnode.type = type;
670
671 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
672 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
673 hash = dict_hash_multi(hash, NULL, 0);
674
675 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
676 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
677 /* we found it on the index that should be skipped, find another */
678 hnode = *match_p;
679 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
680 /* none other found */
681 return LY_SUCCESS;
682 }
683 }
684
685 return LY_EEXIST;
686 }
687
688 /* not found */
689 return LY_SUCCESS;
690}
691
Michal Vaskod3678892020-05-21 10:06:58 +0200692void
693lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200694{
695 if (!set) {
696 return;
697 }
698
699 if (set->type == LYXP_SET_NODE_SET) {
700 free(set->val.nodes);
701 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200702 } else if (set->type == LYXP_SET_SCNODE_SET) {
703 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200704 lyht_free(set->ht);
705 } else {
706 if (set->type == LYXP_SET_STRING) {
707 free(set->val.str);
708 }
709 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200710 }
Michal Vaskod3678892020-05-21 10:06:58 +0200711
712 set->val.nodes = NULL;
713 set->used = 0;
714 set->size = 0;
715 set->ht = NULL;
716 set->ctx_pos = 0;
717 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200718}
719
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100720/**
721 * @brief Free dynamically-allocated set.
722 *
723 * @param[in] set Set to free.
724 */
725static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200726lyxp_set_free(struct lyxp_set *set)
727{
728 if (!set) {
729 return;
730 }
731
Michal Vaskod3678892020-05-21 10:06:58 +0200732 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200733 free(set);
734}
735
736/**
737 * @brief Initialize set context.
738 *
739 * @param[in] new Set to initialize.
740 * @param[in] set Arbitrary initialized set.
741 */
742static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200743set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200744{
745 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200746 if (set) {
747 new->ctx = set->ctx;
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 */
Michal Vasko69730152020-10-09 16:30:07 +02001471 if (((item1->type == LYXP_NODE_TEXT) &&
1472 ((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{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001518 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001519 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 Vaskofeca4fb2020-10-05 08:58:40 +02001550 rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_OPTS_DYNAMIC, LY_PREF_JSON, NULL, LYD_HINT_DATA,
1551 xp_node->node->schema, &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) {
Michal Vasko69730152020-10-09 16:30:07 +02001686 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 Vasko69730152020-10-09 16:30:07 +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 Vasko69730152020-10-09 16:30:07 +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 Vasko69730152020-10-09 16:30:07 +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 Vasko69730152020-10-09 16:30:07 +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],
Michal Vasko69730152020-10-09 16:30:07 +02002279 &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 Vasko69730152020-10-09 16:30:07 +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;
Michal Vasko69730152020-10-09 16:30:07 +02002400 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 Vasko69730152020-10-09 16:30:07 +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 Vasko69730152020-10-09 16:30:07 +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
Radek Krejcif03a9e22020-09-18 20:09:31 +02002656LY_ERR
2657lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr_str, size_t expr_len, ly_bool reparse, struct lyxp_expr **expr_p)
Radek Krejcib1646a92018-11-02 16:08:26 +01002658{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002659 LY_ERR ret = LY_SUCCESS;
2660 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002661 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002662 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002663 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002664 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002665
Radek Krejcif03a9e22020-09-18 20:09:31 +02002666 assert(expr_p);
2667
2668 if (!expr_str[0]) {
Michal Vasko004d3152020-06-11 19:59:22 +02002669 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002670 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002671 }
2672
2673 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002674 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002675 }
2676 if (expr_len > UINT16_MAX) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002677 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
2678 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002679 }
2680
2681 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002682 expr = calloc(1, sizeof *expr);
2683 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2684 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2685 expr->used = 0;
2686 expr->size = LYXP_EXPR_SIZE_START;
2687 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2688 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002689
Radek Krejcif03a9e22020-09-18 20:09:31 +02002690 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2691 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002692
Radek Krejcif03a9e22020-09-18 20:09:31 +02002693 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2694 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002695
Michal Vasko004d3152020-06-11 19:59:22 +02002696 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002697 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002698
Radek Krejcif03a9e22020-09-18 20:09:31 +02002699 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002700 ++parsed;
2701 }
2702
2703 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002704 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002705
2706 /* '(' */
2707 tok_len = 1;
2708 tok_type = LYXP_TOKEN_PAR1;
2709
Radek Krejcif03a9e22020-09-18 20:09:31 +02002710 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002711 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002712 if (((expr->tok_len[expr->used - 1] == 4) &&
2713 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2714 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2715 ((expr->tok_len[expr->used - 1] == 7) &&
2716 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002717 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002718 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002719 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002720 }
2721 prev_function_check = 0;
2722 }
2723
Radek Krejcif03a9e22020-09-18 20:09:31 +02002724 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002725
2726 /* ')' */
2727 tok_len = 1;
2728 tok_type = LYXP_TOKEN_PAR2;
2729
Radek Krejcif03a9e22020-09-18 20:09:31 +02002730 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002731
2732 /* '[' */
2733 tok_len = 1;
2734 tok_type = LYXP_TOKEN_BRACK1;
2735
Radek Krejcif03a9e22020-09-18 20:09:31 +02002736 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002737
2738 /* ']' */
2739 tok_len = 1;
2740 tok_type = LYXP_TOKEN_BRACK2;
2741
Radek Krejcif03a9e22020-09-18 20:09:31 +02002742 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002743
2744 /* '..' */
2745 tok_len = 2;
2746 tok_type = LYXP_TOKEN_DDOT;
2747
Radek Krejcif03a9e22020-09-18 20:09:31 +02002748 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002749
2750 /* '.' */
2751 tok_len = 1;
2752 tok_type = LYXP_TOKEN_DOT;
2753
Radek Krejcif03a9e22020-09-18 20:09:31 +02002754 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002755
2756 /* '@' */
2757 tok_len = 1;
2758 tok_type = LYXP_TOKEN_AT;
2759
Radek Krejcif03a9e22020-09-18 20:09:31 +02002760 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002761
2762 /* ',' */
2763 tok_len = 1;
2764 tok_type = LYXP_TOKEN_COMMA;
2765
Radek Krejcif03a9e22020-09-18 20:09:31 +02002766 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002767
2768 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002769 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2770 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Michal Vasko69730152020-10-09 16:30:07 +02002771 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
2772 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002773 ++tok_len;
2774 tok_type = LYXP_TOKEN_LITERAL;
2775
Radek Krejcif03a9e22020-09-18 20:09:31 +02002776 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002777
2778 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002779 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2780 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Michal Vasko69730152020-10-09 16:30:07 +02002781 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
2782 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002783 ++tok_len;
2784 tok_type = LYXP_TOKEN_LITERAL;
2785
Radek Krejcif03a9e22020-09-18 20:09:31 +02002786 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002787
2788 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002789 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2790 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002791 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002792 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002793 }
2794 tok_type = LYXP_TOKEN_NUMBER;
2795
Radek Krejcif03a9e22020-09-18 20:09:31 +02002796 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002797
2798 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002799 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002800 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002801 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002802 } else {
2803 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002804 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002805 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002806
Radek Krejcif03a9e22020-09-18 20:09:31 +02002807 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002808
Michal Vasko3e48bf32020-06-01 08:39:07 +02002809 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002810 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002811 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2812
Radek Krejcif03a9e22020-09-18 20:09:31 +02002813 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002814
2815 /* Operator '<=', '>=' */
2816 tok_len = 2;
2817 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002818
Radek Krejcif03a9e22020-09-18 20:09:31 +02002819 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002820
2821 /* Operator '|' */
2822 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002823 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002824
Radek Krejcif03a9e22020-09-18 20:09:31 +02002825 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002826
2827 /* Operator '+', '-' */
2828 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002829 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002830
Radek Krejcif03a9e22020-09-18 20:09:31 +02002831 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002832
Michal Vasko3e48bf32020-06-01 08:39:07 +02002833 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002834 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002835 tok_type = LYXP_TOKEN_OPER_EQUAL;
2836
Radek Krejcif03a9e22020-09-18 20:09:31 +02002837 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002838
2839 /* Operator '<', '>' */
2840 tok_len = 1;
2841 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002842
Michal Vasko69730152020-10-09 16:30:07 +02002843 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2844 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2845 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2846 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2847 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2848 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2849 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2850 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2851 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2852 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2853 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2854 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002855
2856 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002857 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002858 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002859 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002860
Radek Krejcif03a9e22020-09-18 20:09:31 +02002861 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002862 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002863 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002864
Radek Krejcif03a9e22020-09-18 20:09:31 +02002865 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002866 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002867 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002868
Radek Krejcif03a9e22020-09-18 20:09:31 +02002869 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002870 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002871 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002872
2873 } else if (prev_function_check) {
Michal Vasko53078572019-05-24 08:50:15 +02002874 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
Michal Vasko69730152020-10-09 16:30:07 +02002875 expr_str[parsed], expr_str[parsed], expr->tok_len[expr->used - 1], &expr->expr[expr->tok_pos[expr->used - 1]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002876 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002877 goto error;
2878 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002879 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr_str);
2880 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002881 goto error;
2882 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002883 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002884
2885 /* NameTest '*' */
2886 tok_len = 1;
2887 tok_type = LYXP_TOKEN_NAMETEST;
2888
2889 } else {
2890
2891 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002892 long int ncname_len = parse_ncname(&expr_str[parsed]);
2893 LY_CHECK_ERR_GOTO(ncname_len < 0,
Michal Vasko69730152020-10-09 16:30:07 +02002894 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
2895 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002896 tok_len = ncname_len;
2897
Radek Krejcif03a9e22020-09-18 20:09:31 +02002898 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002899 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002900 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002901 ++tok_len;
2902 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002903 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
2904 LY_CHECK_ERR_GOTO(ncname_len < 0,
Michal Vasko69730152020-10-09 16:30:07 +02002905 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
2906 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002907 tok_len += ncname_len;
2908 }
2909 /* remove old flag to prevent ambiguities */
2910 prev_function_check = 0;
2911 tok_type = LYXP_TOKEN_NAMETEST;
2912 } else {
2913 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2914 prev_function_check = 1;
2915 tok_type = LYXP_TOKEN_NAMETEST;
2916 }
2917 }
2918
2919 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002920 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002921 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002922 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002923 ++parsed;
2924 }
2925
Radek Krejcif03a9e22020-09-18 20:09:31 +02002926 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002927
Michal Vasko004d3152020-06-11 19:59:22 +02002928 if (reparse) {
2929 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002930 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2931 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002932
Michal Vasko004d3152020-06-11 19:59:22 +02002933 /* fill repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002934 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error);
2935 if (expr->used > tok_idx) {
Michal Vasko004d3152020-06-11 19:59:22 +02002936 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002937 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002938 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002939 goto error;
2940 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002941 }
2942
Radek Krejcif03a9e22020-09-18 20:09:31 +02002943 print_expr_struct_debug(expr);
2944 *expr_p = expr;
2945 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002946
2947error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002948 lyxp_expr_free(ctx, expr);
2949 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002950}
2951
Michal Vasko1734be92020-09-22 08:55:10 +02002952LY_ERR
2953lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02002954{
Michal Vasko1734be92020-09-22 08:55:10 +02002955 LY_ERR ret = LY_SUCCESS;
2956 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02002957 uint32_t i, j;
2958
Michal Vasko7f45cf22020-10-01 12:49:44 +02002959 if (!exp) {
2960 goto cleanup;
2961 }
2962
Michal Vasko004d3152020-06-11 19:59:22 +02002963 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02002964 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002965
2966 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
Michal Vasko1734be92020-09-22 08:55:10 +02002967 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002968 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2969
2970 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
Michal Vasko1734be92020-09-22 08:55:10 +02002971 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002972 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2973
2974 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
Michal Vasko1734be92020-09-22 08:55:10 +02002975 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002976 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2977
2978 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002979 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002980 for (i = 0; i < exp->used; ++i) {
2981 if (!exp->repeat[i]) {
2982 dup->repeat[i] = NULL;
2983 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02002984 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002985 /* the ending 0 as well */
2986 ++j;
2987
Michal Vasko99c71642020-07-03 13:33:36 +02002988 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002989 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002990 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
2991 dup->repeat[i][j - 1] = 0;
2992 }
2993 }
2994
2995 dup->used = exp->used;
2996 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02002997 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002998
Michal Vasko1734be92020-09-22 08:55:10 +02002999cleanup:
3000 if (ret) {
3001 lyxp_expr_free(ctx, dup);
3002 } else {
3003 *dup_p = dup;
3004 }
3005 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003006}
3007
Michal Vasko03ff5a72019-09-11 13:49:33 +02003008/*
3009 * warn functions
3010 *
3011 * Warn functions check specific reasonable conditions for schema XPath
3012 * and print a warning if they are not satisfied.
3013 */
3014
3015/**
3016 * @brief Get the last-added schema node that is currently in the context.
3017 *
3018 * @param[in] set Set to search in.
3019 * @return Last-added schema context node, NULL if no node is in context.
3020 */
3021static struct lysc_node *
3022warn_get_scnode_in_ctx(struct lyxp_set *set)
3023{
3024 uint32_t i;
3025
3026 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3027 return NULL;
3028 }
3029
3030 i = set->used;
3031 do {
3032 --i;
3033 if (set->val.scnodes[i].in_ctx == 1) {
3034 /* if there are more, simply return the first found (last added) */
3035 return set->val.scnodes[i].scnode;
3036 }
3037 } while (i);
3038
3039 return NULL;
3040}
3041
3042/**
3043 * @brief Test whether a type is numeric - integer type or decimal64.
3044 *
3045 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003046 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003047 */
Radek Krejci857189e2020-09-01 13:26:36 +02003048static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003049warn_is_numeric_type(struct lysc_type *type)
3050{
3051 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003052 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003053 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003054
3055 switch (type->basetype) {
3056 case LY_TYPE_DEC64:
3057 case LY_TYPE_INT8:
3058 case LY_TYPE_UINT8:
3059 case LY_TYPE_INT16:
3060 case LY_TYPE_UINT16:
3061 case LY_TYPE_INT32:
3062 case LY_TYPE_UINT32:
3063 case LY_TYPE_INT64:
3064 case LY_TYPE_UINT64:
3065 return 1;
3066 case LY_TYPE_UNION:
3067 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003068 LY_ARRAY_FOR(uni->types, u) {
3069 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003070 if (ret) {
3071 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003072 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003073 }
3074 }
3075 /* did not find any suitable type */
3076 return 0;
3077 case LY_TYPE_LEAFREF:
3078 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3079 default:
3080 return 0;
3081 }
3082}
3083
3084/**
3085 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3086 *
3087 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003088 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003089 */
Radek Krejci857189e2020-09-01 13:26:36 +02003090static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003091warn_is_string_type(struct lysc_type *type)
3092{
3093 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003094 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003095 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003096
3097 switch (type->basetype) {
3098 case LY_TYPE_BITS:
3099 case LY_TYPE_ENUM:
3100 case LY_TYPE_IDENT:
3101 case LY_TYPE_INST:
3102 case LY_TYPE_STRING:
3103 return 1;
3104 case LY_TYPE_UNION:
3105 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003106 LY_ARRAY_FOR(uni->types, u) {
3107 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003108 if (ret) {
3109 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003110 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003111 }
3112 }
3113 /* did not find any suitable type */
3114 return 0;
3115 case LY_TYPE_LEAFREF:
3116 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3117 default:
3118 return 0;
3119 }
3120}
3121
3122/**
3123 * @brief Test whether a type is one specific type.
3124 *
3125 * @param[in] type Type to test.
3126 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003127 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003128 */
Radek Krejci857189e2020-09-01 13:26:36 +02003129static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003130warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3131{
3132 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003133 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003134 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003135
3136 if (type->basetype == base) {
3137 return 1;
3138 } else if (type->basetype == LY_TYPE_UNION) {
3139 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003140 LY_ARRAY_FOR(uni->types, u) {
3141 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003142 if (ret) {
3143 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003144 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003145 }
3146 }
3147 /* did not find any suitable type */
3148 return 0;
3149 } else if (type->basetype == LY_TYPE_LEAFREF) {
3150 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3151 }
3152
3153 return 0;
3154}
3155
3156/**
3157 * @brief Get next type of a (union) type.
3158 *
3159 * @param[in] type Base type.
3160 * @param[in] prev_type Previously returned type.
3161 * @return Next type or NULL.
3162 */
3163static struct lysc_type *
3164warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3165{
3166 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003167 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003168 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003169
3170 switch (type->basetype) {
3171 case LY_TYPE_UNION:
3172 uni = (struct lysc_type_union *)type;
3173 if (!prev_type) {
3174 return uni->types[0];
3175 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003176 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003177 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003178 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003179 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003180 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003181 found = 1;
3182 }
3183 }
3184 return NULL;
3185 default:
3186 if (prev_type) {
3187 assert(type == prev_type);
3188 return NULL;
3189 } else {
3190 return type;
3191 }
3192 }
3193}
3194
3195/**
3196 * @brief Test whether 2 types have a common type.
3197 *
3198 * @param[in] type1 First type.
3199 * @param[in] type2 Second type.
3200 * @return 1 if they do, 0 otherwise.
3201 */
3202static int
3203warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3204{
3205 struct lysc_type *t1, *rt1, *t2, *rt2;
3206
3207 t1 = NULL;
3208 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3209 if (t1->basetype == LY_TYPE_LEAFREF) {
3210 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3211 } else {
3212 rt1 = t1;
3213 }
3214
3215 t2 = NULL;
3216 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3217 if (t2->basetype == LY_TYPE_LEAFREF) {
3218 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3219 } else {
3220 rt2 = t2;
3221 }
3222
3223 if (rt2->basetype == rt1->basetype) {
3224 /* match found */
3225 return 1;
3226 }
3227 }
3228 }
3229
3230 return 0;
3231}
3232
3233/**
3234 * @brief Check both operands of comparison operators.
3235 *
3236 * @param[in] ctx Context for errors.
3237 * @param[in] set1 First operand set.
3238 * @param[in] set2 Second operand set.
3239 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3240 * @param[in] expr Start of the expression to print with the warning.
3241 * @param[in] tok_pos Token position.
3242 */
3243static void
Radek Krejci857189e2020-09-01 13:26:36 +02003244warn_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 +02003245{
3246 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003247 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003248
3249 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3250 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3251
3252 if (!node1 && !node2) {
3253 /* no node-sets involved, nothing to do */
3254 return;
3255 }
3256
3257 if (node1) {
3258 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3259 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3260 warning = 1;
3261 leaves = 0;
3262 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3263 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3264 warning = 1;
3265 }
3266 }
3267
3268 if (node2) {
3269 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3270 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3271 warning = 1;
3272 leaves = 0;
3273 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3274 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3275 warning = 1;
3276 }
3277 }
3278
3279 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003280 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3281 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3282 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3283 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003284 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3285 warning = 1;
3286 }
3287 }
3288
3289 if (warning) {
3290 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3291 }
3292}
3293
3294/**
3295 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3296 *
3297 * @param[in] exp Parsed XPath expression.
3298 * @param[in] set Set with the leaf/leaf-list.
3299 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3300 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3301 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3302 */
3303static void
3304warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp)
3305{
3306 struct lysc_node *scnode;
3307 struct lysc_type *type;
3308 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003309 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003310 LY_ERR rc;
3311 struct ly_err_item *err = NULL;
3312
Michal Vasko69730152020-10-09 16:30:07 +02003313 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3314 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003315 /* check that the node can have the specified value */
3316 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3317 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3318 } else {
3319 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3320 }
3321 if (!value) {
3322 LOGMEM(set->ctx);
3323 return;
3324 }
3325
3326 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3327 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003328 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003329 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003330 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003331 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003332 }
3333
3334 type = ((struct lysc_node_leaf *)scnode)->type;
3335 if (type->basetype != LY_TYPE_IDENT) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003336 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, LY_PREF_SCHEMA, (void *)set->local_mod,
3337 LYD_HINT_DATA, scnode, &storage, &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003338
3339 if (err) {
3340 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3341 ly_err_free(err);
3342 } else if (rc != LY_SUCCESS) {
3343 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3344 }
3345 if (rc != LY_SUCCESS) {
3346 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003347 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003348 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003349 } else {
3350 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003351 }
3352 }
3353 free(value);
3354 }
3355}
3356
3357/*
3358 * XPath functions
3359 */
3360
3361/**
3362 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3363 * depending on whether the first node bit value from the second argument is set.
3364 *
3365 * @param[in] args Array of arguments.
3366 * @param[in] arg_count Count of elements in @p args.
3367 * @param[in,out] set Context and result set at the same time.
3368 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003369 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003370 */
3371static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003372xpath_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 +02003373{
3374 struct lyd_node_term *leaf;
3375 struct lysc_node_leaf *sleaf;
3376 struct lysc_type_bits *bits;
3377 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003378 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003379
3380 if (options & LYXP_SCNODE_ALL) {
3381 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3382 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003383 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3384 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 +02003385 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3386 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003387 }
3388
3389 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3390 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3391 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 +02003392 } else if (!warn_is_string_type(sleaf->type)) {
3393 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003394 }
3395 }
3396 set_scnode_clear_ctx(set);
3397 return rc;
3398 }
3399
Michal Vaskod3678892020-05-21 10:06:58 +02003400 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003401 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)");
3402 return LY_EVALID;
3403 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003404 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003405 LY_CHECK_RET(rc);
3406
3407 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003408 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003409 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003410 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3411 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003412 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003413 LY_ARRAY_FOR(bits->bits, u) {
3414 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003415 set_fill_boolean(set, 1);
3416 break;
3417 }
3418 }
3419 }
3420 }
3421
3422 return LY_SUCCESS;
3423}
3424
3425/**
3426 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3427 * with the argument converted to boolean.
3428 *
3429 * @param[in] args Array of arguments.
3430 * @param[in] arg_count Count of elements in @p args.
3431 * @param[in,out] set Context and result set at the same time.
3432 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003433 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003434 */
3435static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003436xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003437{
3438 LY_ERR rc;
3439
3440 if (options & LYXP_SCNODE_ALL) {
3441 set_scnode_clear_ctx(set);
3442 return LY_SUCCESS;
3443 }
3444
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003445 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003446 LY_CHECK_RET(rc);
3447 set_fill_set(set, args[0]);
3448
3449 return LY_SUCCESS;
3450}
3451
3452/**
3453 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3454 * with the first argument rounded up to the nearest integer.
3455 *
3456 * @param[in] args Array of arguments.
3457 * @param[in] arg_count Count of elements in @p args.
3458 * @param[in,out] set Context and result set at the same time.
3459 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003460 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003461 */
3462static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003463xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003464{
3465 struct lysc_node_leaf *sleaf;
3466 LY_ERR rc = LY_SUCCESS;
3467
3468 if (options & LYXP_SCNODE_ALL) {
3469 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3470 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003471 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3472 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 +02003473 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3474 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003475 }
3476 set_scnode_clear_ctx(set);
3477 return rc;
3478 }
3479
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003480 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003481 LY_CHECK_RET(rc);
3482 if ((long long)args[0]->val.num != args[0]->val.num) {
3483 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3484 } else {
3485 set_fill_number(set, args[0]->val.num);
3486 }
3487
3488 return LY_SUCCESS;
3489}
3490
3491/**
3492 * @brief Execute the XPath concat(string, string, string*) function.
3493 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3494 *
3495 * @param[in] args Array of arguments.
3496 * @param[in] arg_count Count of elements in @p args.
3497 * @param[in,out] set Context and result set at the same time.
3498 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003499 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003500 */
3501static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003502xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003503{
3504 uint16_t i;
3505 char *str = NULL;
3506 size_t used = 1;
3507 LY_ERR rc = LY_SUCCESS;
3508 struct lysc_node_leaf *sleaf;
3509
3510 if (options & LYXP_SCNODE_ALL) {
3511 for (i = 0; i < arg_count; ++i) {
3512 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3513 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3514 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003515 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003516 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003517 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 +02003518 }
3519 }
3520 }
3521 set_scnode_clear_ctx(set);
3522 return rc;
3523 }
3524
3525 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003526 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003527 if (rc != LY_SUCCESS) {
3528 free(str);
3529 return rc;
3530 }
3531
3532 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3533 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3534 strcpy(str + used - 1, args[i]->val.str);
3535 used += strlen(args[i]->val.str);
3536 }
3537
3538 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003539 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003540 set->type = LYXP_SET_STRING;
3541 set->val.str = str;
3542
3543 return LY_SUCCESS;
3544}
3545
3546/**
3547 * @brief Execute the XPath contains(string, string) function.
3548 * Returns LYXP_SET_BOOLEAN whether the second argument can
3549 * be found in the first or not.
3550 *
3551 * @param[in] args Array of arguments.
3552 * @param[in] arg_count Count of elements in @p args.
3553 * @param[in,out] set Context and result set at the same time.
3554 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003555 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003556 */
3557static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003558xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003559{
3560 struct lysc_node_leaf *sleaf;
3561 LY_ERR rc = LY_SUCCESS;
3562
3563 if (options & LYXP_SCNODE_ALL) {
3564 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3565 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3566 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 +02003567 } else if (!warn_is_string_type(sleaf->type)) {
3568 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003569 }
3570 }
3571
3572 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3573 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3574 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 +02003575 } else if (!warn_is_string_type(sleaf->type)) {
3576 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003577 }
3578 }
3579 set_scnode_clear_ctx(set);
3580 return rc;
3581 }
3582
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003583 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003584 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003585 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003586 LY_CHECK_RET(rc);
3587
3588 if (strstr(args[0]->val.str, args[1]->val.str)) {
3589 set_fill_boolean(set, 1);
3590 } else {
3591 set_fill_boolean(set, 0);
3592 }
3593
3594 return LY_SUCCESS;
3595}
3596
3597/**
3598 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3599 * with the size of the node-set from the argument.
3600 *
3601 * @param[in] args Array of arguments.
3602 * @param[in] arg_count Count of elements in @p args.
3603 * @param[in,out] set Context and result set at the same time.
3604 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003605 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003606 */
3607static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003608xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003609{
3610 struct lysc_node *scnode = NULL;
3611 LY_ERR rc = LY_SUCCESS;
3612
3613 if (options & LYXP_SCNODE_ALL) {
3614 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3615 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003616 }
3617 set_scnode_clear_ctx(set);
3618 return rc;
3619 }
3620
Michal Vasko03ff5a72019-09-11 13:49:33 +02003621 if (args[0]->type != LYXP_SET_NODE_SET) {
3622 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
3623 return LY_EVALID;
3624 }
3625
3626 set_fill_number(set, args[0]->used);
3627 return LY_SUCCESS;
3628}
3629
3630/**
3631 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3632 * with the context with the intial node.
3633 *
3634 * @param[in] args Array of arguments.
3635 * @param[in] arg_count Count of elements in @p args.
3636 * @param[in,out] set Context and result set at the same time.
3637 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003638 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003639 */
3640static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003641xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003642{
3643 if (arg_count || args) {
3644 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
3645 return LY_EVALID;
3646 }
3647
3648 if (options & LYXP_SCNODE_ALL) {
3649 set_scnode_clear_ctx(set);
3650
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003651 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003652 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003653 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003654
3655 /* position is filled later */
3656 set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0);
3657 }
3658
3659 return LY_SUCCESS;
3660}
3661
3662/**
3663 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3664 * leafref or instance-identifier target node(s).
3665 *
3666 * @param[in] args Array of arguments.
3667 * @param[in] arg_count Count of elements in @p args.
3668 * @param[in,out] set Context and result set at the same time.
3669 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003670 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003671 */
3672static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003673xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003674{
3675 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003676 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003677 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003678 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003679 struct ly_path *p;
3680 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003681 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003682 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003683 LY_ERR rc = LY_SUCCESS;
3684
3685 if (options & LYXP_SCNODE_ALL) {
3686 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3687 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003688 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3689 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 +02003690 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3691 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
Michal Vasko69730152020-10-09 16:30:07 +02003692 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003693 }
3694 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003695 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003696 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vasko00cbf532020-06-15 13:58:47 +02003697 oper = lysc_is_output((struct lysc_node *)sleaf) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003698
3699 /* it was already evaluated on schema, it must succeed */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003700 rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE,
Michal Vasko72619ce2020-10-06 14:05:32 +02003701 oper, LY_PATH_TARGET_MANY, set->format, (void *)lref->path_mod, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003702 assert(!rc);
3703
3704 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003705 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003706 ly_path_free(set->ctx, p);
3707
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003708 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003709 }
3710
Michal Vasko03ff5a72019-09-11 13:49:33 +02003711 return rc;
3712 }
3713
Michal Vaskod3678892020-05-21 10:06:58 +02003714 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003715 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
3716 return LY_EVALID;
3717 }
3718
Michal Vaskod3678892020-05-21 10:06:58 +02003719 lyxp_set_free_content(set);
3720 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003721 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3722 sleaf = (struct lysc_node_leaf *)leaf->schema;
3723 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3724 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3725 /* find leafref target */
Michal Vasko004d3152020-06-11 19:59:22 +02003726 if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf,
Michal Vasko69730152020-10-09 16:30:07 +02003727 &leaf->value, set->tree, &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003728 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003729 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003730 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003731 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003732 } else {
3733 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003734 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003735 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Michal Vasko69730152020-10-09 16:30:07 +02003736 LYD_CANON_VALUE(leaf));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003737 return LY_EVALID;
3738 }
3739 }
Michal Vasko004d3152020-06-11 19:59:22 +02003740
3741 /* insert it */
3742 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003743 }
3744 }
3745
3746 return LY_SUCCESS;
3747}
3748
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003749static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003750xpath_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 +02003751{
3752 uint16_t i;
3753 LY_ARRAY_COUNT_TYPE u;
3754 struct lyd_node_term *leaf;
3755 struct lysc_node_leaf *sleaf;
3756 struct lyd_meta *meta;
3757 struct lyd_value data = {0}, *val;
3758 struct ly_err_item *err = NULL;
3759 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003760 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003761
3762 if (options & LYXP_SCNODE_ALL) {
3763 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3764 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
3765 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3766 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003767 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003768 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3769 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3770 }
3771
3772 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3773 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3774 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003775 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003776 } else if (!warn_is_string_type(sleaf->type)) {
3777 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3778 }
3779 }
3780 set_scnode_clear_ctx(set);
3781 return rc;
3782 }
3783
3784 if (args[0]->type != LYXP_SET_NODE_SET) {
3785 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02003786 "derived-from(-or-self)(node-set, string)");
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003787 return LY_EVALID;
3788 }
3789 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3790 LY_CHECK_RET(rc);
3791
3792 set_fill_boolean(set, 0);
3793 found = 0;
3794 for (i = 0; i < args[0]->used; ++i) {
3795 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3796 continue;
3797 }
3798
3799 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3800 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3801 sleaf = (struct lysc_node_leaf *)leaf->schema;
3802 val = &leaf->value;
3803 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3804 /* uninteresting */
3805 continue;
3806 }
3807
3808 /* store args[1] as ident */
3809 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str),
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003810 0, set->format, (void *)set->local_mod, LYD_HINT_DATA, leaf->schema, &data, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003811 } else {
3812 meta = args[0]->val.meta[i].meta;
3813 val = &meta->value;
3814 if (val->realtype->basetype != LY_TYPE_IDENT) {
3815 /* uninteresting */
3816 continue;
3817 }
3818
3819 /* store args[1] as ident */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003820 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), 0,
3821 set->format, (void *)meta->annotation->module, LYD_HINT_DATA, meta->parent->schema, &data, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003822 }
3823
3824 if (err) {
3825 ly_err_print(err);
3826 ly_err_free(err);
3827 }
3828 LY_CHECK_RET(rc);
3829
3830 /* finally check the identity itself */
3831 if (self_match && (data.ident == val->ident)) {
3832 set_fill_boolean(set, 1);
3833 found = 1;
3834 }
3835 if (!found) {
3836 LY_ARRAY_FOR(data.ident->derived, u) {
3837 if (data.ident->derived[u] == val->ident) {
3838 set_fill_boolean(set, 1);
3839 found = 1;
3840 break;
3841 }
3842 }
3843 }
3844
3845 /* free temporary value */
3846 val->realtype->plugin->free(set->ctx, &data);
3847 if (found) {
3848 break;
3849 }
3850 }
3851
3852 return LY_SUCCESS;
3853}
3854
Michal Vasko03ff5a72019-09-11 13:49:33 +02003855/**
3856 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3857 * on whether the first argument nodes contain a node of an identity derived from the second
3858 * argument identity.
3859 *
3860 * @param[in] args Array of arguments.
3861 * @param[in] arg_count Count of elements in @p args.
3862 * @param[in,out] set Context and result set at the same time.
3863 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003864 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003865 */
3866static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003867xpath_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 +02003868{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003869 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003870}
3871
3872/**
3873 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3874 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3875 * the second argument identity.
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_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 +02003885{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003886 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003887}
3888
3889/**
3890 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3891 * with the integer value of the first node's enum value, otherwise NaN.
3892 *
3893 * @param[in] args Array of arguments.
3894 * @param[in] arg_count Count of elements in @p args.
3895 * @param[in,out] set Context and result set at the same time.
3896 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003897 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003898 */
3899static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003900xpath_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 +02003901{
3902 struct lyd_node_term *leaf;
3903 struct lysc_node_leaf *sleaf;
3904 LY_ERR rc = LY_SUCCESS;
3905
3906 if (options & LYXP_SCNODE_ALL) {
3907 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3908 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003909 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3910 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 +02003911 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3912 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003913 }
3914 set_scnode_clear_ctx(set);
3915 return rc;
3916 }
3917
Michal Vaskod3678892020-05-21 10:06:58 +02003918 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003919 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
3920 return LY_EVALID;
3921 }
3922
3923 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003924 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003925 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3926 sleaf = (struct lysc_node_leaf *)leaf->schema;
3927 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3928 set_fill_number(set, leaf->value.enum_item->value);
3929 }
3930 }
3931
3932 return LY_SUCCESS;
3933}
3934
3935/**
3936 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3937 * with false value.
3938 *
3939 * @param[in] args Array of arguments.
3940 * @param[in] arg_count Count of elements in @p args.
3941 * @param[in,out] set Context and result set at the same time.
3942 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003943 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003944 */
3945static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003946xpath_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 +02003947{
3948 if (options & LYXP_SCNODE_ALL) {
3949 set_scnode_clear_ctx(set);
3950 return LY_SUCCESS;
3951 }
3952
3953 set_fill_boolean(set, 0);
3954 return LY_SUCCESS;
3955}
3956
3957/**
3958 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3959 * with the first argument floored (truncated).
3960 *
3961 * @param[in] args Array of arguments.
3962 * @param[in] arg_count Count of elements in @p args.
3963 * @param[in,out] set Context and result set at the same time.
3964 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003965 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003966 */
3967static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003968xpath_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 +02003969{
3970 LY_ERR rc;
3971
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003972 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003973 LY_CHECK_RET(rc);
3974 if (isfinite(args[0]->val.num)) {
3975 set_fill_number(set, (long long)args[0]->val.num);
3976 }
3977
3978 return LY_SUCCESS;
3979}
3980
3981/**
3982 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3983 * whether the language of the text matches the one from the argument.
3984 *
3985 * @param[in] args Array of arguments.
3986 * @param[in] arg_count Count of elements in @p args.
3987 * @param[in,out] set Context and result set at the same time.
3988 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003989 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003990 */
3991static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003992xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003993{
3994 const struct lyd_node *node;
3995 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01003996 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003997 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003998 LY_ERR rc = LY_SUCCESS;
3999
4000 if (options & LYXP_SCNODE_ALL) {
4001 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4002 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4003 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 +02004004 } else if (!warn_is_string_type(sleaf->type)) {
4005 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004006 }
4007 }
4008 set_scnode_clear_ctx(set);
4009 return rc;
4010 }
4011
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004012 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004013 LY_CHECK_RET(rc);
4014
Michal Vasko03ff5a72019-09-11 13:49:33 +02004015 if (set->type != LYXP_SET_NODE_SET) {
4016 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
4017 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004018 } else if (!set->used) {
4019 set_fill_boolean(set, 0);
4020 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004021 }
4022
4023 switch (set->val.nodes[0].type) {
4024 case LYXP_NODE_ELEM:
4025 case LYXP_NODE_TEXT:
4026 node = set->val.nodes[0].node;
4027 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004028 case LYXP_NODE_META:
4029 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004030 break;
4031 default:
4032 /* nothing to do with roots */
4033 set_fill_boolean(set, 0);
4034 return LY_SUCCESS;
4035 }
4036
Michal Vasko9f96a052020-03-10 09:41:45 +01004037 /* find lang metadata */
Michal Vaskod989ba02020-08-24 10:59:24 +02004038 for ( ; node; node = (struct lyd_node *)node->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004039 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004040 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004041 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004042 break;
4043 }
4044 }
4045
Michal Vasko9f96a052020-03-10 09:41:45 +01004046 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004047 break;
4048 }
4049 }
4050
4051 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004052 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004053 set_fill_boolean(set, 0);
4054 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004055 uint64_t i;
4056
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004057 val = meta->value.canonical;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004058 for (i = 0; args[0]->val.str[i]; ++i) {
4059 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4060 set_fill_boolean(set, 0);
4061 break;
4062 }
4063 }
4064 if (!args[0]->val.str[i]) {
4065 if (!val[i] || (val[i] == '-')) {
4066 set_fill_boolean(set, 1);
4067 } else {
4068 set_fill_boolean(set, 0);
4069 }
4070 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004071 }
4072
4073 return LY_SUCCESS;
4074}
4075
4076/**
4077 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4078 * with the context size.
4079 *
4080 * @param[in] args Array of arguments.
4081 * @param[in] arg_count Count of elements in @p args.
4082 * @param[in,out] set Context and result set at the same time.
4083 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004084 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004085 */
4086static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004087xpath_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 +02004088{
4089 if (options & LYXP_SCNODE_ALL) {
4090 set_scnode_clear_ctx(set);
4091 return LY_SUCCESS;
4092 }
4093
Michal Vasko03ff5a72019-09-11 13:49:33 +02004094 if (set->type != LYXP_SET_NODE_SET) {
4095 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
4096 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004097 } else if (!set->used) {
4098 set_fill_number(set, 0);
4099 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004100 }
4101
4102 set_fill_number(set, set->ctx_size);
4103 return LY_SUCCESS;
4104}
4105
4106/**
4107 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4108 * with the node name without namespace from the argument or the context.
4109 *
4110 * @param[in] args Array of arguments.
4111 * @param[in] arg_count Count of elements in @p args.
4112 * @param[in,out] set Context and result set at the same time.
4113 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004114 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004115 */
4116static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004117xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004118{
4119 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004120
Michal Vasko03ff5a72019-09-11 13:49:33 +02004121 /* suppress unused variable warning */
4122 (void)options;
4123
4124 if (options & LYXP_SCNODE_ALL) {
4125 set_scnode_clear_ctx(set);
4126 return LY_SUCCESS;
4127 }
4128
4129 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004130 if (args[0]->type != LYXP_SET_NODE_SET) {
4131 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)");
4132 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004133 } else if (!args[0]->used) {
4134 set_fill_string(set, "", 0);
4135 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004136 }
4137
4138 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004139 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004140
4141 item = &args[0]->val.nodes[0];
4142 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004143 if (set->type != LYXP_SET_NODE_SET) {
4144 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
4145 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004146 } else if (!set->used) {
4147 set_fill_string(set, "", 0);
4148 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004149 }
4150
4151 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004152 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004153
4154 item = &set->val.nodes[0];
4155 }
4156
4157 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004158 case LYXP_NODE_NONE:
4159 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004160 case LYXP_NODE_ROOT:
4161 case LYXP_NODE_ROOT_CONFIG:
4162 case LYXP_NODE_TEXT:
4163 set_fill_string(set, "", 0);
4164 break;
4165 case LYXP_NODE_ELEM:
4166 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4167 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004168 case LYXP_NODE_META:
4169 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004170 break;
4171 }
4172
4173 return LY_SUCCESS;
4174}
4175
4176/**
4177 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4178 * with the node name fully qualified (with namespace) from the argument or the context.
4179 *
4180 * @param[in] args Array of arguments.
4181 * @param[in] arg_count Count of elements in @p args.
4182 * @param[in,out] set Context and result set at the same time.
4183 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004184 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004185 */
4186static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004187xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004188{
4189 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004190 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004191 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004192 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004193
4194 if (options & LYXP_SCNODE_ALL) {
4195 set_scnode_clear_ctx(set);
4196 return LY_SUCCESS;
4197 }
4198
4199 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004200 if (args[0]->type != LYXP_SET_NODE_SET) {
4201 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
4202 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004203 } else if (!args[0]->used) {
4204 set_fill_string(set, "", 0);
4205 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004206 }
4207
4208 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004209 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004210
4211 item = &args[0]->val.nodes[0];
4212 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004213 if (set->type != LYXP_SET_NODE_SET) {
4214 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
4215 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004216 } else if (!set->used) {
4217 set_fill_string(set, "", 0);
4218 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004219 }
4220
4221 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004222 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004223
4224 item = &set->val.nodes[0];
4225 }
4226
4227 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004228 case LYXP_NODE_NONE:
4229 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004230 case LYXP_NODE_ROOT:
4231 case LYXP_NODE_ROOT_CONFIG:
4232 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004233 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004234 break;
4235 case LYXP_NODE_ELEM:
4236 mod = item->node->schema->module;
4237 name = item->node->schema->name;
4238 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004239 case LYXP_NODE_META:
4240 mod = ((struct lyd_meta *)item->node)->annotation->module;
4241 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004242 break;
4243 }
4244
4245 if (mod && name) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004246 int rc = -1;
4247
Michal Vasko03ff5a72019-09-11 13:49:33 +02004248 switch (set->format) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02004249 case LY_PREF_SCHEMA:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004250 rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name);
4251 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02004252 case LY_PREF_JSON:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004253 rc = asprintf(&str, "%s:%s", mod->name, name);
4254 break;
Michal Vaskoc9795582020-10-08 16:22:17 +02004255 case LY_PREF_SCHEMA_RESOLVED:
Michal Vaskoc8a230d2020-08-14 12:17:10 +02004256 case LY_PREF_XML:
Michal Vasko9409ef62019-09-12 11:47:17 +02004257 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004258 }
4259 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4260 set_fill_string(set, str, strlen(str));
4261 free(str);
4262 } else {
4263 set_fill_string(set, "", 0);
4264 }
4265
4266 return LY_SUCCESS;
4267}
4268
4269/**
4270 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4271 * with the namespace of the node from the argument or the context.
4272 *
4273 * @param[in] args Array of arguments.
4274 * @param[in] arg_count Count of elements in @p args.
4275 * @param[in,out] set Context and result set at the same time.
4276 * @param[in] options XPath options.
4277 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4278 */
4279static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004280xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004281{
4282 struct lyxp_set_node *item;
4283 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004284
Michal Vasko03ff5a72019-09-11 13:49:33 +02004285 /* suppress unused variable warning */
4286 (void)options;
4287
4288 if (options & LYXP_SCNODE_ALL) {
4289 set_scnode_clear_ctx(set);
4290 return LY_SUCCESS;
4291 }
4292
4293 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004294 if (args[0]->type != LYXP_SET_NODE_SET) {
4295 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004296 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004297 return LY_EVALID;
4298 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004299 set_fill_string(set, "", 0);
4300 return LY_SUCCESS;
4301 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004302
4303 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004304 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004305
4306 item = &args[0]->val.nodes[0];
4307 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004308 if (set->type != LYXP_SET_NODE_SET) {
4309 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
4310 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004311 } else if (!set->used) {
4312 set_fill_string(set, "", 0);
4313 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004314 }
4315
4316 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004317 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004318
4319 item = &set->val.nodes[0];
4320 }
4321
4322 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004323 case LYXP_NODE_NONE:
4324 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004325 case LYXP_NODE_ROOT:
4326 case LYXP_NODE_ROOT_CONFIG:
4327 case LYXP_NODE_TEXT:
4328 set_fill_string(set, "", 0);
4329 break;
4330 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004331 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004332 if (item->type == LYXP_NODE_ELEM) {
4333 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004334 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004335 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004336 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004337 }
4338
4339 set_fill_string(set, mod->ns, strlen(mod->ns));
4340 break;
4341 }
4342
4343 return LY_SUCCESS;
4344}
4345
4346/**
4347 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4348 * with only nodes from the context. In practice it either leaves the context
4349 * as it is or returns an empty node set.
4350 *
4351 * @param[in] args Array of arguments.
4352 * @param[in] arg_count Count of elements in @p args.
4353 * @param[in,out] set Context and result set at the same time.
4354 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004355 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004356 */
4357static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004358xpath_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 +02004359{
4360 if (options & LYXP_SCNODE_ALL) {
4361 set_scnode_clear_ctx(set);
4362 return LY_SUCCESS;
4363 }
4364
4365 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004366 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004367 }
4368 return LY_SUCCESS;
4369}
4370
4371/**
4372 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4373 * with normalized value (no leading, trailing, double white spaces) of the node
4374 * from the argument or the context.
4375 *
4376 * @param[in] args Array of arguments.
4377 * @param[in] arg_count Count of elements in @p args.
4378 * @param[in,out] set Context and result set at the same time.
4379 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004380 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004381 */
4382static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004383xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004384{
4385 uint16_t i, new_used;
4386 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004387 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004388 struct lysc_node_leaf *sleaf;
4389 LY_ERR rc = LY_SUCCESS;
4390
4391 if (options & LYXP_SCNODE_ALL) {
4392 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4393 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4394 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 +02004395 } else if (!warn_is_string_type(sleaf->type)) {
4396 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004397 }
4398 }
4399 set_scnode_clear_ctx(set);
4400 return rc;
4401 }
4402
4403 if (arg_count) {
4404 set_fill_set(set, args[0]);
4405 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004406 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004407 LY_CHECK_RET(rc);
4408
4409 /* is there any normalization necessary? */
4410 for (i = 0; set->val.str[i]; ++i) {
4411 if (is_xmlws(set->val.str[i])) {
4412 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4413 have_spaces = 1;
4414 break;
4415 }
4416 space_before = 1;
4417 } else {
4418 space_before = 0;
4419 }
4420 }
4421
4422 /* yep, there is */
4423 if (have_spaces) {
4424 /* it's enough, at least one character will go, makes space for ending '\0' */
4425 new = malloc(strlen(set->val.str) * sizeof(char));
4426 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4427 new_used = 0;
4428
4429 space_before = 0;
4430 for (i = 0; set->val.str[i]; ++i) {
4431 if (is_xmlws(set->val.str[i])) {
4432 if ((i == 0) || space_before) {
4433 space_before = 1;
4434 continue;
4435 } else {
4436 space_before = 1;
4437 }
4438 } else {
4439 space_before = 0;
4440 }
4441
4442 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4443 ++new_used;
4444 }
4445
4446 /* at worst there is one trailing space now */
4447 if (new_used && is_xmlws(new[new_used - 1])) {
4448 --new_used;
4449 }
4450
4451 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4452 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4453 new[new_used] = '\0';
4454
4455 free(set->val.str);
4456 set->val.str = new;
4457 }
4458
4459 return LY_SUCCESS;
4460}
4461
4462/**
4463 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4464 * with the argument converted to boolean and logically inverted.
4465 *
4466 * @param[in] args Array of arguments.
4467 * @param[in] arg_count Count of elements in @p args.
4468 * @param[in,out] set Context and result set at the same time.
4469 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004470 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004471 */
4472static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004473xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004474{
4475 if (options & LYXP_SCNODE_ALL) {
4476 set_scnode_clear_ctx(set);
4477 return LY_SUCCESS;
4478 }
4479
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004480 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004481 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004482 set_fill_boolean(set, 0);
4483 } else {
4484 set_fill_boolean(set, 1);
4485 }
4486
4487 return LY_SUCCESS;
4488}
4489
4490/**
4491 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4492 * with the number representation of either the argument or the context.
4493 *
4494 * @param[in] args Array of arguments.
4495 * @param[in] arg_count Count of elements in @p args.
4496 * @param[in,out] set Context and result set at the same time.
4497 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004498 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004499 */
4500static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004501xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004502{
4503 LY_ERR rc;
4504
4505 if (options & LYXP_SCNODE_ALL) {
4506 set_scnode_clear_ctx(set);
4507 return LY_SUCCESS;
4508 }
4509
4510 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004511 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004512 LY_CHECK_RET(rc);
4513 set_fill_set(set, args[0]);
4514 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004515 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004516 LY_CHECK_RET(rc);
4517 }
4518
4519 return LY_SUCCESS;
4520}
4521
4522/**
4523 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4524 * with the context position.
4525 *
4526 * @param[in] args Array of arguments.
4527 * @param[in] arg_count Count of elements in @p args.
4528 * @param[in,out] set Context and result set at the same time.
4529 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004530 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004531 */
4532static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004533xpath_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 +02004534{
4535 if (options & LYXP_SCNODE_ALL) {
4536 set_scnode_clear_ctx(set);
4537 return LY_SUCCESS;
4538 }
4539
Michal Vasko03ff5a72019-09-11 13:49:33 +02004540 if (set->type != LYXP_SET_NODE_SET) {
4541 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
4542 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004543 } else if (!set->used) {
4544 set_fill_number(set, 0);
4545 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004546 }
4547
4548 set_fill_number(set, set->ctx_pos);
4549
4550 /* UNUSED in 'Release' build type */
4551 (void)options;
4552 return LY_SUCCESS;
4553}
4554
4555/**
4556 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4557 * depending on whether the second argument regex matches the first argument string. For details refer to
4558 * YANG 1.1 RFC section 10.2.1.
4559 *
4560 * @param[in] args Array of arguments.
4561 * @param[in] arg_count Count of elements in @p args.
4562 * @param[in,out] set Context and result set at the same time.
4563 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004564 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004565 */
4566static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004567xpath_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 +02004568{
4569 struct lysc_pattern **patterns = NULL, **pattern;
4570 struct lysc_node_leaf *sleaf;
4571 char *path;
4572 LY_ERR rc = LY_SUCCESS;
4573 struct ly_err_item *err;
4574
4575 if (options & LYXP_SCNODE_ALL) {
4576 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4577 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4578 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 +02004579 } else if (!warn_is_string_type(sleaf->type)) {
4580 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004581 }
4582 }
4583
4584 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4585 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4586 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 +02004587 } else if (!warn_is_string_type(sleaf->type)) {
4588 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004589 }
4590 }
4591 set_scnode_clear_ctx(set);
4592 return rc;
4593 }
4594
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004595 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004596 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004597 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004598 LY_CHECK_RET(rc);
4599
4600 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4601 *pattern = malloc(sizeof **pattern);
4602 path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0);
4603 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4604 free(path);
4605 if (rc != LY_SUCCESS) {
4606 LY_ARRAY_FREE(patterns);
4607 return rc;
4608 }
4609
4610 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4611 pcre2_code_free((*pattern)->code);
4612 free(*pattern);
4613 LY_ARRAY_FREE(patterns);
4614 if (rc && (rc != LY_EVALID)) {
4615 ly_err_print(err);
4616 ly_err_free(err);
4617 return rc;
4618 }
4619
4620 if (rc == LY_EVALID) {
4621 ly_err_free(err);
4622 set_fill_boolean(set, 0);
4623 } else {
4624 set_fill_boolean(set, 1);
4625 }
4626
4627 return LY_SUCCESS;
4628}
4629
4630/**
4631 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4632 * with the rounded first argument. For details refer to
4633 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4634 *
4635 * @param[in] args Array of arguments.
4636 * @param[in] arg_count Count of elements in @p args.
4637 * @param[in,out] set Context and result set at the same time.
4638 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004639 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004640 */
4641static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004642xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004643{
4644 struct lysc_node_leaf *sleaf;
4645 LY_ERR rc = LY_SUCCESS;
4646
4647 if (options & LYXP_SCNODE_ALL) {
4648 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4649 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004650 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4651 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 +02004652 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4653 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004654 }
4655 set_scnode_clear_ctx(set);
4656 return rc;
4657 }
4658
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004659 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004660 LY_CHECK_RET(rc);
4661
4662 /* cover only the cases where floor can't be used */
4663 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4664 set_fill_number(set, -0.0f);
4665 } else {
4666 args[0]->val.num += 0.5;
4667 rc = xpath_floor(args, 1, args[0], options);
4668 LY_CHECK_RET(rc);
4669 set_fill_number(set, args[0]->val.num);
4670 }
4671
4672 return LY_SUCCESS;
4673}
4674
4675/**
4676 * @brief Execute the XPath starts-with(string, string) function.
4677 * Returns LYXP_SET_BOOLEAN whether the second argument is
4678 * the prefix of the first or not.
4679 *
4680 * @param[in] args Array of arguments.
4681 * @param[in] arg_count Count of elements in @p args.
4682 * @param[in,out] set Context and result set at the same time.
4683 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004684 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004685 */
4686static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004687xpath_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 +02004688{
4689 struct lysc_node_leaf *sleaf;
4690 LY_ERR rc = LY_SUCCESS;
4691
4692 if (options & LYXP_SCNODE_ALL) {
4693 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4694 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4695 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 +02004696 } else if (!warn_is_string_type(sleaf->type)) {
4697 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004698 }
4699 }
4700
4701 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4702 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4703 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 +02004704 } else if (!warn_is_string_type(sleaf->type)) {
4705 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004706 }
4707 }
4708 set_scnode_clear_ctx(set);
4709 return rc;
4710 }
4711
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004712 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004713 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004714 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004715 LY_CHECK_RET(rc);
4716
4717 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4718 set_fill_boolean(set, 0);
4719 } else {
4720 set_fill_boolean(set, 1);
4721 }
4722
4723 return LY_SUCCESS;
4724}
4725
4726/**
4727 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4728 * with the string representation of either the argument or the context.
4729 *
4730 * @param[in] args Array of arguments.
4731 * @param[in] arg_count Count of elements in @p args.
4732 * @param[in,out] set Context and result set at the same time.
4733 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004734 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004735 */
4736static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004737xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004738{
4739 LY_ERR rc;
4740
4741 if (options & LYXP_SCNODE_ALL) {
4742 set_scnode_clear_ctx(set);
4743 return LY_SUCCESS;
4744 }
4745
4746 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004747 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004748 LY_CHECK_RET(rc);
4749 set_fill_set(set, args[0]);
4750 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004751 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004752 LY_CHECK_RET(rc);
4753 }
4754
4755 return LY_SUCCESS;
4756}
4757
4758/**
4759 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4760 * with the length of the string in either the argument or the context.
4761 *
4762 * @param[in] args Array of arguments.
4763 * @param[in] arg_count Count of elements in @p args.
4764 * @param[in,out] set Context and result set at the same time.
4765 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004766 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004767 */
4768static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004769xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004770{
4771 struct lysc_node_leaf *sleaf;
4772 LY_ERR rc = LY_SUCCESS;
4773
4774 if (options & LYXP_SCNODE_ALL) {
4775 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4776 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4777 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 +02004778 } else if (!warn_is_string_type(sleaf->type)) {
4779 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004780 }
4781 }
4782 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4783 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4784 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 +02004785 } else if (!warn_is_string_type(sleaf->type)) {
4786 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004787 }
4788 }
4789 set_scnode_clear_ctx(set);
4790 return rc;
4791 }
4792
4793 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004794 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004795 LY_CHECK_RET(rc);
4796 set_fill_number(set, strlen(args[0]->val.str));
4797 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004798 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004799 LY_CHECK_RET(rc);
4800 set_fill_number(set, strlen(set->val.str));
4801 }
4802
4803 return LY_SUCCESS;
4804}
4805
4806/**
4807 * @brief Execute the XPath substring(string, number, number?) function.
4808 * Returns LYXP_SET_STRING substring of the first argument starting
4809 * on the second argument index ending on the third argument index,
4810 * indexed from 1. For exact definition refer to
4811 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4812 *
4813 * @param[in] args Array of arguments.
4814 * @param[in] arg_count Count of elements in @p args.
4815 * @param[in,out] set Context and result set at the same time.
4816 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004817 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004818 */
4819static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004820xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004821{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004822 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004823 uint16_t str_start, str_len, pos;
4824 struct lysc_node_leaf *sleaf;
4825 LY_ERR rc = LY_SUCCESS;
4826
4827 if (options & LYXP_SCNODE_ALL) {
4828 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4829 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4830 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 +02004831 } else if (!warn_is_string_type(sleaf->type)) {
4832 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004833 }
4834 }
4835
4836 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4837 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4838 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 +02004839 } else if (!warn_is_numeric_type(sleaf->type)) {
4840 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004841 }
4842 }
4843
Michal Vasko69730152020-10-09 16:30:07 +02004844 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4845 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004846 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4847 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 +02004848 } else if (!warn_is_numeric_type(sleaf->type)) {
4849 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004850 }
4851 }
4852 set_scnode_clear_ctx(set);
4853 return rc;
4854 }
4855
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004856 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004857 LY_CHECK_RET(rc);
4858
4859 /* start */
4860 if (xpath_round(&args[1], 1, args[1], options)) {
4861 return -1;
4862 }
4863 if (isfinite(args[1]->val.num)) {
4864 start = args[1]->val.num - 1;
4865 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004866 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004867 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004868 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004869 }
4870
4871 /* len */
4872 if (arg_count == 3) {
4873 rc = xpath_round(&args[2], 1, args[2], options);
4874 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004875 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004876 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004877 } else if (isfinite(args[2]->val.num)) {
4878 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004879 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004880 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004881 }
4882 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004883 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004884 }
4885
4886 /* find matching character positions */
4887 str_start = 0;
4888 str_len = 0;
4889 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4890 if (pos < start) {
4891 ++str_start;
4892 } else if (pos < start + len) {
4893 ++str_len;
4894 } else {
4895 break;
4896 }
4897 }
4898
4899 set_fill_string(set, args[0]->val.str + str_start, str_len);
4900 return LY_SUCCESS;
4901}
4902
4903/**
4904 * @brief Execute the XPath substring-after(string, string) function.
4905 * Returns LYXP_SET_STRING with the string succeeding the occurance
4906 * of the second argument in the first or an empty string.
4907 *
4908 * @param[in] args Array of arguments.
4909 * @param[in] arg_count Count of elements in @p args.
4910 * @param[in,out] set Context and result set at the same time.
4911 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004912 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004913 */
4914static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004915xpath_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 +02004916{
4917 char *ptr;
4918 struct lysc_node_leaf *sleaf;
4919 LY_ERR rc = LY_SUCCESS;
4920
4921 if (options & LYXP_SCNODE_ALL) {
4922 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4923 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4924 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 +02004925 } else if (!warn_is_string_type(sleaf->type)) {
4926 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004927 }
4928 }
4929
4930 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4931 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4932 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 +02004933 } else if (!warn_is_string_type(sleaf->type)) {
4934 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004935 }
4936 }
4937 set_scnode_clear_ctx(set);
4938 return rc;
4939 }
4940
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004941 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004942 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004943 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004944 LY_CHECK_RET(rc);
4945
4946 ptr = strstr(args[0]->val.str, args[1]->val.str);
4947 if (ptr) {
4948 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4949 } else {
4950 set_fill_string(set, "", 0);
4951 }
4952
4953 return LY_SUCCESS;
4954}
4955
4956/**
4957 * @brief Execute the XPath substring-before(string, string) function.
4958 * Returns LYXP_SET_STRING with the string preceding the occurance
4959 * of the second argument in the first or an empty string.
4960 *
4961 * @param[in] args Array of arguments.
4962 * @param[in] arg_count Count of elements in @p args.
4963 * @param[in,out] set Context and result set at the same time.
4964 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004965 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004966 */
4967static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004968xpath_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 +02004969{
4970 char *ptr;
4971 struct lysc_node_leaf *sleaf;
4972 LY_ERR rc = LY_SUCCESS;
4973
4974 if (options & LYXP_SCNODE_ALL) {
4975 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4976 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4977 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 +02004978 } else if (!warn_is_string_type(sleaf->type)) {
4979 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004980 }
4981 }
4982
4983 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4984 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4985 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 +02004986 } else if (!warn_is_string_type(sleaf->type)) {
4987 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004988 }
4989 }
4990 set_scnode_clear_ctx(set);
4991 return rc;
4992 }
4993
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004994 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004995 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004996 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004997 LY_CHECK_RET(rc);
4998
4999 ptr = strstr(args[0]->val.str, args[1]->val.str);
5000 if (ptr) {
5001 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5002 } else {
5003 set_fill_string(set, "", 0);
5004 }
5005
5006 return LY_SUCCESS;
5007}
5008
5009/**
5010 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5011 * with the sum of all the nodes in the context.
5012 *
5013 * @param[in] args Array of arguments.
5014 * @param[in] arg_count Count of elements in @p args.
5015 * @param[in,out] set Context and result set at the same time.
5016 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005017 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005018 */
5019static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005020xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005021{
5022 long double num;
5023 char *str;
5024 uint16_t i;
5025 struct lyxp_set set_item;
5026 struct lysc_node_leaf *sleaf;
5027 LY_ERR rc = LY_SUCCESS;
5028
5029 if (options & LYXP_SCNODE_ALL) {
5030 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5031 for (i = 0; i < args[0]->used; ++i) {
5032 if (args[0]->val.scnodes[i].in_ctx == 1) {
5033 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5034 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5035 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005036 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005037 } else if (!warn_is_numeric_type(sleaf->type)) {
5038 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005039 }
5040 }
5041 }
5042 }
5043 set_scnode_clear_ctx(set);
5044 return rc;
5045 }
5046
5047 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005048
5049 if (args[0]->type != LYXP_SET_NODE_SET) {
5050 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
5051 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005052 } else if (!args[0]->used) {
5053 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005054 }
5055
Michal Vasko5c4e5892019-11-14 12:31:38 +01005056 set_init(&set_item, set);
5057
Michal Vasko03ff5a72019-09-11 13:49:33 +02005058 set_item.type = LYXP_SET_NODE_SET;
5059 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5060 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5061
5062 set_item.used = 1;
5063 set_item.size = 1;
5064
5065 for (i = 0; i < args[0]->used; ++i) {
5066 set_item.val.nodes[0] = args[0]->val.nodes[i];
5067
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005068 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005069 LY_CHECK_RET(rc);
5070 num = cast_string_to_number(str);
5071 free(str);
5072 set->val.num += num;
5073 }
5074
5075 free(set_item.val.nodes);
5076
5077 return LY_SUCCESS;
5078}
5079
5080/**
5081 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5082 * with the text content of the nodes in the context.
5083 *
5084 * @param[in] args Array of arguments.
5085 * @param[in] arg_count Count of elements in @p args.
5086 * @param[in,out] set Context and result set at the same time.
5087 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005088 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005089 */
5090static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005091xpath_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 +02005092{
5093 uint32_t i;
5094
5095 if (options & LYXP_SCNODE_ALL) {
5096 set_scnode_clear_ctx(set);
5097 return LY_SUCCESS;
5098 }
5099
Michal Vasko03ff5a72019-09-11 13:49:33 +02005100 if (set->type != LYXP_SET_NODE_SET) {
5101 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5102 return LY_EVALID;
5103 }
5104
Michal Vaskod989ba02020-08-24 10:59:24 +02005105 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005106 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005107 case LYXP_NODE_NONE:
5108 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005109 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005110 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5111 set->val.nodes[i].type = LYXP_NODE_TEXT;
5112 ++i;
5113 break;
5114 }
Radek Krejci0f969882020-08-21 16:56:47 +02005115 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005116 case LYXP_NODE_ROOT:
5117 case LYXP_NODE_ROOT_CONFIG:
5118 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005119 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005120 set_remove_node(set, i);
5121 break;
5122 }
5123 }
5124
5125 return LY_SUCCESS;
5126}
5127
5128/**
5129 * @brief Execute the XPath translate(string, string, string) function.
5130 * Returns LYXP_SET_STRING with the first argument with the characters
5131 * from the second argument replaced by those on the corresponding
5132 * positions in the third argument.
5133 *
5134 * @param[in] args Array of arguments.
5135 * @param[in] arg_count Count of elements in @p args.
5136 * @param[in,out] set Context and result set at the same time.
5137 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005138 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005139 */
5140static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005141xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005142{
5143 uint16_t i, j, new_used;
5144 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005145 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005146 struct lysc_node_leaf *sleaf;
5147 LY_ERR rc = LY_SUCCESS;
5148
5149 if (options & LYXP_SCNODE_ALL) {
5150 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5151 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5152 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 +02005153 } else if (!warn_is_string_type(sleaf->type)) {
5154 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005155 }
5156 }
5157
5158 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5159 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5160 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 +02005161 } else if (!warn_is_string_type(sleaf->type)) {
5162 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005163 }
5164 }
5165
5166 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5167 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5168 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 +02005169 } else if (!warn_is_string_type(sleaf->type)) {
5170 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005171 }
5172 }
5173 set_scnode_clear_ctx(set);
5174 return rc;
5175 }
5176
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005177 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005178 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005179 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005180 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005181 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005182 LY_CHECK_RET(rc);
5183
5184 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5185 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5186 new_used = 0;
5187
5188 have_removed = 0;
5189 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005190 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005191
5192 for (j = 0; args[1]->val.str[j]; ++j) {
5193 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5194 /* removing this char */
5195 if (j >= strlen(args[2]->val.str)) {
5196 have_removed = 1;
5197 found = 1;
5198 break;
5199 }
5200 /* replacing this char */
5201 new[new_used] = args[2]->val.str[j];
5202 ++new_used;
5203 found = 1;
5204 break;
5205 }
5206 }
5207
5208 /* copying this char */
5209 if (!found) {
5210 new[new_used] = args[0]->val.str[i];
5211 ++new_used;
5212 }
5213 }
5214
5215 if (have_removed) {
5216 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5217 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5218 }
5219 new[new_used] = '\0';
5220
Michal Vaskod3678892020-05-21 10:06:58 +02005221 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005222 set->type = LYXP_SET_STRING;
5223 set->val.str = new;
5224
5225 return LY_SUCCESS;
5226}
5227
5228/**
5229 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5230 * with true value.
5231 *
5232 * @param[in] args Array of arguments.
5233 * @param[in] arg_count Count of elements in @p args.
5234 * @param[in,out] set Context and result set at the same time.
5235 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005236 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005237 */
5238static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005239xpath_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 +02005240{
5241 if (options & LYXP_SCNODE_ALL) {
5242 set_scnode_clear_ctx(set);
5243 return LY_SUCCESS;
5244 }
5245
5246 set_fill_boolean(set, 1);
5247 return LY_SUCCESS;
5248}
5249
5250/*
5251 * moveto functions
5252 *
5253 * They and only they actually change the context (set).
5254 */
5255
5256/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005257 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005258 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005259 * XPath @p set is expected to be a (sc)node set!
5260 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005261 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5262 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
5263 * @param[in] set Set with XPath context.
5264 * @param[out] moveto_mod Expected module of a matching node.
5265 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005266 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005267static LY_ERR
5268moveto_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 +02005269{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005270 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005271 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005272 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005273
Michal Vasko2104e9f2020-03-06 08:23:25 +01005274 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5275
Michal Vasko6346ece2019-09-24 13:12:53 +02005276 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5277 /* specific module */
5278 pref_len = ptr - *qname;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02005279 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, (void *)set->local_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005280
Michal Vasko004d3152020-06-11 19:59:22 +02005281 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005282 if (!mod || !mod->implemented) {
Michal Vasko2104e9f2020-03-06 08:23:25 +01005283 if (set->type == LYXP_SET_SCNODE_SET) {
5284 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname);
5285 } else {
5286 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname);
5287 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005288 return LY_EVALID;
5289 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005290
Michal Vasko6346ece2019-09-24 13:12:53 +02005291 *qname += pref_len + 1;
5292 *qname_len -= pref_len + 1;
5293 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5294 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005295 mod = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02005296 } else if (set->type == LYXP_SET_SCNODE_SET) {
5297 /* current node module */
5298 mod = set->ctx_scnode->module;
Michal Vasko6346ece2019-09-24 13:12:53 +02005299 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005300 /* current node module */
5301 mod = set->ctx_node->schema->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005302 }
5303
Michal Vasko6346ece2019-09-24 13:12:53 +02005304 *moveto_mod = mod;
5305 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005306}
5307
5308/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005309 * @brief Move context @p set to the root. Handles absolute path.
5310 * Result is LYXP_SET_NODE_SET.
5311 *
5312 * @param[in,out] set Set to use.
5313 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005314 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005315 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005316static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005317moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005318{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005319 if (!set) {
Michal Vaskob0099a92020-08-31 14:55:23 +02005320 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005321 }
5322
5323 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005324 set_scnode_clear_ctx(set);
Michal Vaskob0099a92020-08-31 14:55:23 +02005325 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005326 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005327 set->type = LYXP_SET_NODE_SET;
5328 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005329 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005330 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005331
5332 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005333}
5334
5335/**
Michal Vaskoa1424542019-11-14 16:08:52 +01005336 * @brief Check whether a node has some unresolved "when".
5337 *
5338 * @param[in] node Node to check.
5339 * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when")
5340 */
5341static LY_ERR
5342moveto_when_check(const struct lyd_node *node)
5343{
5344 const struct lysc_node *schema;
5345
5346 if (!node) {
5347 return LY_SUCCESS;
5348 }
5349
5350 schema = node->schema;
5351 do {
5352 if (schema->when && !(node->flags & LYD_WHEN_TRUE)) {
5353 return LY_EINCOMPLETE;
5354 }
5355 schema = schema->parent;
5356 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
5357
5358 return LY_SUCCESS;
5359}
5360
5361/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005362 * @brief Check @p node as a part of NameTest processing.
5363 *
5364 * @param[in] node Node to check.
5365 * @param[in] root_type XPath root node type.
Michal Vasko6b26e742020-07-17 15:02:10 +02005366 * @param[in] context_op XPath operation parent.
Michal Vaskod3678892020-05-21 10:06:58 +02005367 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
5368 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005369 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5370 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005371 */
5372static LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +02005373moveto_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 +02005374 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005375{
5376 /* module check */
5377 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005378 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005379 }
5380
Michal Vasko5c4e5892019-11-14 12:31:38 +01005381 /* context check */
5382 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005383 return LY_EINVAL;
Michal Vasko6b26e742020-07-17 15:02:10 +02005384 } else if (context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node->schema != context_op)) {
5385 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005386 }
5387
5388 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005389 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005390 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005391 }
5392
Michal Vaskoa1424542019-11-14 16:08:52 +01005393 /* when check */
5394 if (moveto_when_check(node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005395 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005396 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005397
5398 /* match */
5399 return LY_SUCCESS;
5400}
5401
5402/**
5403 * @brief Check @p node as a part of schema NameTest processing.
5404 *
5405 * @param[in] node Schema node to check.
5406 * @param[in] root_type XPath root node type.
Michal Vasko6b26e742020-07-17 15:02:10 +02005407 * @param[in] context_op XPath operation parent.
Michal Vaskod3678892020-05-21 10:06:58 +02005408 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
5409 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005410 * @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 +02005411 */
5412static LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +02005413moveto_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 +02005414 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005415{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005416 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005417 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005418 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005419 }
5420
5421 /* context check */
5422 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
5423 return LY_EINVAL;
Michal Vasko6b26e742020-07-17 15:02:10 +02005424 } else if (context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != context_op)) {
5425 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005426 }
5427
5428 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005429 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005430 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005431 }
5432
5433 /* match */
5434 return LY_SUCCESS;
5435}
5436
5437/**
Michal Vaskod3678892020-05-21 10:06:58 +02005438 * @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 +02005439 *
5440 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005441 * @param[in] mod Matching node module, NULL for any.
5442 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005443 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5444 */
5445static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005446moveto_node(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005447{
Michal Vaskof03ed032020-03-04 13:31:44 +01005448 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005449 const struct lyd_node *siblings, *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005450 LY_ERR rc;
5451
Michal Vaskod3678892020-05-21 10:06:58 +02005452 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005453 return LY_SUCCESS;
5454 }
5455
5456 if (set->type != LYXP_SET_NODE_SET) {
5457 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5458 return LY_EVALID;
5459 }
5460
Michal Vasko03ff5a72019-09-11 13:49:33 +02005461 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005462 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005463
5464 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 +01005465 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005466
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005467 /* search in all the trees */
Michal Vaskod3678892020-05-21 10:06:58 +02005468 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005469 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005470 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005471 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005472 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005473
Michal Vaskod3678892020-05-21 10:06:58 +02005474 for (sub = siblings; sub; sub = sub->next) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005475 rc = moveto_node_check(sub, set->root_type, set->context_op, ncname, mod);
Michal Vaskod3678892020-05-21 10:06:58 +02005476 if (rc == LY_SUCCESS) {
5477 if (!replaced) {
5478 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5479 replaced = 1;
5480 } else {
5481 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005482 }
Michal Vaskod3678892020-05-21 10:06:58 +02005483 ++i;
5484 } else if (rc == LY_EINCOMPLETE) {
5485 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005486 }
5487 }
5488
5489 if (!replaced) {
5490 /* no match */
5491 set_remove_node(set, i);
5492 }
5493 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005494
5495 return LY_SUCCESS;
5496}
5497
5498/**
Michal Vaskod3678892020-05-21 10:06:58 +02005499 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5500 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005501 *
5502 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005503 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005504 * @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 +02005505 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5506 */
5507static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02005508moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates)
Michal Vaskod3678892020-05-21 10:06:58 +02005509{
Michal Vasko004d3152020-06-11 19:59:22 +02005510 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005511 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005512 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005513 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005514
Michal Vasko004d3152020-06-11 19:59:22 +02005515 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005516
5517 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005518 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005519 }
5520
5521 if (set->type != LYXP_SET_NODE_SET) {
5522 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 +02005523 ret = LY_EVALID;
5524 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005525 }
5526
5527 /* context check for all the nodes since we have the schema node */
5528 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5529 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005530 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005531 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5532 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005533 lyxp_set_free_content(set);
5534 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005535 }
5536
5537 /* create specific data instance if needed */
5538 if (scnode->nodetype == LYS_LIST) {
5539 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5540 } else if (scnode->nodetype == LYS_LEAFLIST) {
5541 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005542 }
5543
5544 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005545 siblings = NULL;
5546
5547 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5548 assert(!set->val.nodes[i].node);
5549
5550 /* search in all the trees */
5551 siblings = set->tree;
5552 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5553 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005554 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005555 }
5556
5557 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005558 if (inst) {
5559 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005560 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005561 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005562 }
5563
5564 /* when check */
5565 if (sub && moveto_when_check(sub)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005566 ret = LY_EINCOMPLETE;
5567 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005568 }
5569
5570 if (sub) {
5571 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005572 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005573 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005574 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005575 /* no match */
5576 set_remove_node(set, i);
5577 }
5578 }
5579
Michal Vasko004d3152020-06-11 19:59:22 +02005580cleanup:
5581 lyd_free_tree(inst);
5582 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005583}
5584
5585/**
5586 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5587 *
5588 * @param[in,out] set Set to use.
5589 * @param[in] mod Matching node module, NULL for any.
5590 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005591 * @param[in] options XPath options.
5592 * @return LY_ERR
5593 */
5594static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005595moveto_scnode(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005596{
Radek Krejci857189e2020-09-01 13:26:36 +02005597 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005598 uint32_t getnext_opts;
5599 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005600 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005601 const struct lysc_node *iter, *start_parent;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005602 const struct lys_module *moveto_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005603
Michal Vaskod3678892020-05-21 10:06:58 +02005604 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005605 return LY_SUCCESS;
5606 }
5607
5608 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005609 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 +02005610 return LY_EVALID;
5611 }
5612
Michal Vaskocafad9d2019-11-07 15:20:03 +01005613 /* getnext opts */
5614 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
5615 if (options & LYXP_SCNODE_OUTPUT) {
5616 getnext_opts |= LYS_GETNEXT_OUTPUT;
5617 }
5618
Michal Vasko03ff5a72019-09-11 13:49:33 +02005619 orig_used = set->used;
5620 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005621 uint32_t idx;
5622
Michal Vasko03ff5a72019-09-11 13:49:33 +02005623 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005624 if (set->val.scnodes[i].in_ctx != -2) {
5625 continue;
5626 }
5627
5628 /* remember context node */
5629 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005630 } else {
5631 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005632 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005633
5634 start_parent = set->val.scnodes[i].scnode;
5635
5636 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005637 /* it can actually be in any module, it's all <running>, and even if it's mod (if set),
5638 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005639 mod_idx = 0;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005640 moveto_mod = mod;
5641 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005642 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005643 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005644 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005645 if (!moveto_scnode_check(iter, set->root_type, set->context_op, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005646 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5647
Michal Vasko03ff5a72019-09-11 13:49:33 +02005648 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005649 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 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005655 }
5656
Michal Vasko519fd602020-05-26 12:17:39 +02005657 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5658 iter = NULL;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005659 moveto_mod = mod ? mod : set->local_mod;
Michal Vasko519fd602020-05-26 12:17:39 +02005660 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005661 if (!moveto_scnode_check(iter, set->root_type, set->context_op, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005662 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5663
5664 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005665 set->val.scnodes[idx].in_ctx = 2;
5666 temp_ctx = 1;
5667 }
5668 }
5669 }
5670 }
5671 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005672
5673 /* correct temporary in_ctx values */
5674 if (temp_ctx) {
5675 for (i = 0; i < orig_used; ++i) {
5676 if (set->val.scnodes[i].in_ctx == 2) {
5677 set->val.scnodes[i].in_ctx = 1;
5678 }
5679 }
5680 }
5681
5682 return LY_SUCCESS;
5683}
5684
5685/**
Michal Vaskod3678892020-05-21 10:06:58 +02005686 * @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 +02005687 * Context position aware.
5688 *
5689 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005690 * @param[in] mod Matching node module, NULL for any.
5691 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005692 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5693 */
5694static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005695moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005696{
5697 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005698 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005699 struct lyxp_set ret_set;
5700 LY_ERR rc;
5701
Michal Vaskod3678892020-05-21 10:06:58 +02005702 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005703 return LY_SUCCESS;
5704 }
5705
5706 if (set->type != LYXP_SET_NODE_SET) {
5707 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5708 return LY_EVALID;
5709 }
5710
Michal Vasko9f96a052020-03-10 09:41:45 +01005711 /* 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 +02005712 rc = moveto_node(set, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005713 LY_CHECK_RET(rc);
5714
Michal Vasko6346ece2019-09-24 13:12:53 +02005715 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005716 set_init(&ret_set, set);
5717 for (i = 0; i < set->used; ++i) {
5718
5719 /* TREE DFS */
5720 start = set->val.nodes[i].node;
5721 for (elem = next = start; elem; elem = next) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005722 rc = moveto_node_check(elem, set->root_type, set->context_op, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005723 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005724 /* add matching node into result set */
5725 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5726 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5727 /* the node is a duplicate, we'll process it later in the set */
5728 goto skip_children;
5729 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005730 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005731 return rc;
5732 } else if (rc == LY_EINVAL) {
5733 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005734 }
5735
5736 /* TREE DFS NEXT ELEM */
5737 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005738 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005739 if (!next) {
5740skip_children:
5741 /* no children, so try siblings, but only if it's not the start,
5742 * that is considered to be the root and it's siblings are not traversed */
5743 if (elem != start) {
5744 next = elem->next;
5745 } else {
5746 break;
5747 }
5748 }
5749 while (!next) {
5750 /* no siblings, go back through the parents */
5751 if ((struct lyd_node *)elem->parent == start) {
5752 /* we are done, no next element to process */
5753 break;
5754 }
5755 /* parent is already processed, go to its sibling */
5756 elem = (struct lyd_node *)elem->parent;
5757 next = elem->next;
5758 }
5759 }
5760 }
5761
5762 /* make the temporary set the current one */
5763 ret_set.ctx_pos = set->ctx_pos;
5764 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005765 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005766 memcpy(set, &ret_set, sizeof *set);
5767
5768 return LY_SUCCESS;
5769}
5770
5771/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005772 * @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 +02005773 *
5774 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005775 * @param[in] mod Matching node module, NULL for any.
5776 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005777 * @param[in] options XPath options.
5778 * @return LY_ERR
5779 */
5780static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005781moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005782{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005783 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005784 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005785 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005786
Michal Vaskod3678892020-05-21 10:06:58 +02005787 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005788 return LY_SUCCESS;
5789 }
5790
5791 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005792 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 +02005793 return LY_EVALID;
5794 }
5795
Michal Vasko03ff5a72019-09-11 13:49:33 +02005796 orig_used = set->used;
5797 for (i = 0; i < orig_used; ++i) {
5798 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005799 if (set->val.scnodes[i].in_ctx != -2) {
5800 continue;
5801 }
5802
5803 /* remember context node */
5804 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005805 } else {
5806 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005807 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005808
5809 /* TREE DFS */
5810 start = set->val.scnodes[i].scnode;
5811 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005812 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5813 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005814 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005815 }
5816
Michal Vasko6b26e742020-07-17 15:02:10 +02005817 rc = moveto_scnode_check(elem, set->root_type, set->context_op, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005818 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005819 uint32_t idx;
5820
5821 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005822 set->val.scnodes[idx].in_ctx = 1;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005823 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005824 /* we will process it later in the set */
5825 goto skip_children;
5826 }
5827 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005828 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005829 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005830 } else if (rc == LY_EINVAL) {
5831 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005832 }
5833
5834next_iter:
5835 /* TREE DFS NEXT ELEM */
5836 /* select element for the next run - children first */
5837 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005838 if (!next) {
5839skip_children:
5840 /* no children, so try siblings, but only if it's not the start,
5841 * that is considered to be the root and it's siblings are not traversed */
5842 if (elem != start) {
5843 next = elem->next;
5844 } else {
5845 break;
5846 }
5847 }
5848 while (!next) {
5849 /* no siblings, go back through the parents */
5850 if (elem->parent == start) {
5851 /* we are done, no next element to process */
5852 break;
5853 }
5854 /* parent is already processed, go to its sibling */
5855 elem = elem->parent;
5856 next = elem->next;
5857 }
5858 }
5859 }
5860
5861 return LY_SUCCESS;
5862}
5863
5864/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005865 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005866 * Indirectly context position aware.
5867 *
5868 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005869 * @param[in] mod Matching metadata module, NULL for any.
5870 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005871 * @return LY_ERR
5872 */
5873static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005874moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005875{
Michal Vasko9f96a052020-03-10 09:41:45 +01005876 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005877
Michal Vaskod3678892020-05-21 10:06:58 +02005878 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005879 return LY_SUCCESS;
5880 }
5881
5882 if (set->type != LYXP_SET_NODE_SET) {
5883 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5884 return LY_EVALID;
5885 }
5886
Radek Krejci1deb5be2020-08-26 16:43:36 +02005887 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005888 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005889
5890 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5891 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005892 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005893 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005894
5895 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005896 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005897 continue;
5898 }
5899
Michal Vaskod3678892020-05-21 10:06:58 +02005900 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005901 /* match */
5902 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005903 set->val.meta[i].meta = sub;
5904 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005905 /* pos does not change */
5906 replaced = 1;
5907 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005908 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 +02005909 }
5910 ++i;
5911 }
5912 }
5913 }
5914
5915 if (!replaced) {
5916 /* no match */
5917 set_remove_node(set, i);
5918 }
5919 }
5920
5921 return LY_SUCCESS;
5922}
5923
5924/**
5925 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005926 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005927 *
5928 * @param[in,out] set1 Set to use for the result.
5929 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005930 * @return LY_ERR
5931 */
5932static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005933moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005934{
5935 LY_ERR rc;
5936
Michal Vaskod3678892020-05-21 10:06:58 +02005937 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005938 LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
5939 return LY_EVALID;
5940 }
5941
5942 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005943 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005944 return LY_SUCCESS;
5945 }
5946
Michal Vaskod3678892020-05-21 10:06:58 +02005947 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005948 memcpy(set1, set2, sizeof *set1);
5949 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005950 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005951 return LY_SUCCESS;
5952 }
5953
5954 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005955 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005956
5957 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005958 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005959 LY_CHECK_RET(rc);
5960
5961 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005962 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005963
5964 return LY_SUCCESS;
5965}
5966
5967/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005968 * @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 +02005969 * Context position aware.
5970 *
5971 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005972 * @param[in] mod Matching metadata module, NULL for any.
5973 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005974 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5975 */
5976static int
Michal Vaskod3678892020-05-21 10:06:58 +02005977moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005978{
Michal Vasko9f96a052020-03-10 09:41:45 +01005979 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005980 struct lyxp_set *set_all_desc = NULL;
5981 LY_ERR rc;
5982
Michal Vaskod3678892020-05-21 10:06:58 +02005983 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005984 return LY_SUCCESS;
5985 }
5986
5987 if (set->type != LYXP_SET_NODE_SET) {
5988 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5989 return LY_EVALID;
5990 }
5991
Michal Vasko03ff5a72019-09-11 13:49:33 +02005992 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
5993 * but it likely won't be used much, so it's a waste of time */
5994 /* copy the context */
5995 set_all_desc = set_copy(set);
5996 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskod3678892020-05-21 10:06:58 +02005997 rc = moveto_node_alldesc(set_all_desc, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005998 if (rc != LY_SUCCESS) {
5999 lyxp_set_free(set_all_desc);
6000 return rc;
6001 }
6002 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006003 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006004 if (rc != LY_SUCCESS) {
6005 lyxp_set_free(set_all_desc);
6006 return rc;
6007 }
6008 lyxp_set_free(set_all_desc);
6009
Radek Krejci1deb5be2020-08-26 16:43:36 +02006010 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006011 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006012
6013 /* only attributes of an elem can be in the result, skip all the rest,
6014 * we have all attributes qualified in lyd tree */
6015 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006016 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006017 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006018 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006019 continue;
6020 }
6021
Michal Vaskod3678892020-05-21 10:06:58 +02006022 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006023 /* match */
6024 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006025 set->val.meta[i].meta = sub;
6026 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006027 /* pos does not change */
6028 replaced = 1;
6029 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006030 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 +02006031 }
6032 ++i;
6033 }
6034 }
6035 }
6036
6037 if (!replaced) {
6038 /* no match */
6039 set_remove_node(set, i);
6040 }
6041 }
6042
6043 return LY_SUCCESS;
6044}
6045
6046/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006047 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6048 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006049 *
6050 * @param[in] parent Current parent.
6051 * @param[in] parent_pos Position of @p parent.
6052 * @param[in] parent_type Node type of @p parent.
6053 * @param[in,out] to_set Set to use.
6054 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006055 * @param[in] options XPath options.
6056 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6057 */
6058static LY_ERR
6059moveto_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 +02006060 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006061{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006062 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006063 LY_ERR rc;
6064
6065 switch (parent_type) {
6066 case LYXP_NODE_ROOT:
6067 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006068 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006069
Michal Vasko61ac2f62020-05-25 12:39:51 +02006070 /* add all top-level nodes as elements */
6071 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006072 break;
6073 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006074 /* add just the text node of this term element node */
6075 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006076 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6077 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6078 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006079 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006080 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006081
6082 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006083 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006084 break;
6085 default:
6086 LOGINT_RET(parent->schema->module->ctx);
6087 }
6088
Michal Vasko61ac2f62020-05-25 12:39:51 +02006089 /* add all top-level nodes as elements */
6090 LY_LIST_FOR(first, iter) {
6091 /* context check */
6092 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6093 continue;
6094 }
6095
6096 /* when check */
6097 if (moveto_when_check(iter)) {
6098 return LY_EINCOMPLETE;
6099 }
6100
6101 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6102 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6103
6104 /* also add all the children of this node, recursively */
6105 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6106 LY_CHECK_RET(rc);
6107 }
6108 }
6109
Michal Vasko03ff5a72019-09-11 13:49:33 +02006110 return LY_SUCCESS;
6111}
6112
6113/**
6114 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6115 * (or LYXP_SET_EMPTY). Context position aware.
6116 *
6117 * @param[in,out] set Set to use.
6118 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6119 * @param[in] options XPath options.
6120 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6121 */
6122static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006123moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006124{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006125 struct lyxp_set ret_set;
6126 LY_ERR rc;
6127
Michal Vaskod3678892020-05-21 10:06:58 +02006128 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006129 return LY_SUCCESS;
6130 }
6131
6132 if (set->type != LYXP_SET_NODE_SET) {
6133 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6134 return LY_EVALID;
6135 }
6136
6137 /* nothing to do */
6138 if (!all_desc) {
6139 return LY_SUCCESS;
6140 }
6141
Michal Vasko03ff5a72019-09-11 13:49:33 +02006142 /* add all the children, they get added recursively */
6143 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006144 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006145 /* copy the current node to tmp */
6146 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6147
6148 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006149 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006150 continue;
6151 }
6152
Michal Vasko03ff5a72019-09-11 13:49:33 +02006153 /* add all the children */
6154 rc = moveto_self_add_children_r(set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, &ret_set,
Michal Vasko69730152020-10-09 16:30:07 +02006155 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006156 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006157 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006158 return rc;
6159 }
6160 }
6161
6162 /* use the temporary set as the current one */
6163 ret_set.ctx_pos = set->ctx_pos;
6164 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006165 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006166 memcpy(set, &ret_set, sizeof *set);
6167
6168 return LY_SUCCESS;
6169}
6170
6171/**
6172 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6173 * (or LYXP_SET_EMPTY).
6174 *
6175 * @param[in,out] set Set to use.
6176 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6177 * @param[in] options XPath options.
6178 * @return LY_ERR
6179 */
6180static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006181moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006182{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006183 uint32_t getnext_opts;
6184 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006185 const struct lysc_node *iter, *start_parent;
6186 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006187
Michal Vaskod3678892020-05-21 10:06:58 +02006188 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006189 return LY_SUCCESS;
6190 }
6191
6192 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006193 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 +02006194 return LY_EVALID;
6195 }
6196
6197 /* nothing to do */
6198 if (!all_desc) {
6199 return LY_SUCCESS;
6200 }
6201
Michal Vasko519fd602020-05-26 12:17:39 +02006202 /* getnext opts */
6203 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
6204 if (options & LYXP_SCNODE_OUTPUT) {
6205 getnext_opts |= LYS_GETNEXT_OUTPUT;
6206 }
6207
6208 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006209 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006210 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006211 if (set->val.scnodes[i].in_ctx != -2) {
6212 continue;
6213 }
6214
Michal Vasko519fd602020-05-26 12:17:39 +02006215 /* remember context node */
6216 set->val.scnodes[i].in_ctx = -1;
6217 } else {
6218 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006219 }
6220
Michal Vasko519fd602020-05-26 12:17:39 +02006221 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006222
Michal Vasko519fd602020-05-26 12:17:39 +02006223 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6224 /* it can actually be in any module, it's all <running> */
6225 mod_idx = 0;
6226 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6227 iter = NULL;
6228 /* module may not be implemented */
6229 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6230 /* context check */
6231 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6232 continue;
6233 }
6234
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006235 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006236 /* throw away the insert index, we want to consider that node again, recursively */
6237 }
6238 }
6239
6240 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6241 iter = NULL;
6242 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006243 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006244 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006245 continue;
6246 }
6247
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006248 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006249 }
6250 }
6251 }
6252
6253 return LY_SUCCESS;
6254}
6255
6256/**
6257 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6258 * (or LYXP_SET_EMPTY). Context position aware.
6259 *
6260 * @param[in] set Set to use.
6261 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6262 * @param[in] options XPath options.
6263 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6264 */
6265static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006266moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006267{
6268 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006269 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006270 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006271
Michal Vaskod3678892020-05-21 10:06:58 +02006272 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006273 return LY_SUCCESS;
6274 }
6275
6276 if (set->type != LYXP_SET_NODE_SET) {
6277 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6278 return LY_EVALID;
6279 }
6280
6281 if (all_desc) {
6282 /* <path>//.. == <path>//./.. */
6283 rc = moveto_self(set, 1, options);
6284 LY_CHECK_RET(rc);
6285 }
6286
Radek Krejci1deb5be2020-08-26 16:43:36 +02006287 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006288 node = set->val.nodes[i].node;
6289
6290 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6291 new_node = (struct lyd_node *)node->parent;
6292 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6293 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006294 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6295 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006296 if (!new_node) {
6297 LOGINT_RET(set->ctx);
6298 }
6299 } else {
6300 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006301 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006302 continue;
6303 }
6304
Michal Vaskoa1424542019-11-14 16:08:52 +01006305 /* when check */
6306 if (moveto_when_check(new_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006307 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006308 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006309
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006310 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006311 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006312 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006313
Michal Vasko03ff5a72019-09-11 13:49:33 +02006314 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006315 /* 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 +02006316 new_type = LYXP_NODE_ELEM;
6317 }
6318
Michal Vasko03ff5a72019-09-11 13:49:33 +02006319 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006320 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006321 } else {
6322 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006323 }
6324 }
6325
Michal Vasko2caefc12019-11-14 16:07:56 +01006326 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006327 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006328
6329 return LY_SUCCESS;
6330}
6331
6332/**
6333 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6334 * (or LYXP_SET_EMPTY).
6335 *
6336 * @param[in] set Set to use.
6337 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6338 * @param[in] options XPath options.
6339 * @return LY_ERR
6340 */
6341static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006342moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006343{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006344 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006345 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006346 const struct lysc_node *node, *new_node;
6347 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006348
Michal Vaskod3678892020-05-21 10:06:58 +02006349 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006350 return LY_SUCCESS;
6351 }
6352
6353 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006354 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 +02006355 return LY_EVALID;
6356 }
6357
6358 if (all_desc) {
6359 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006360 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006361 }
6362
Michal Vasko03ff5a72019-09-11 13:49:33 +02006363 orig_used = set->used;
6364 for (i = 0; i < orig_used; ++i) {
6365 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006366 if (set->val.scnodes[i].in_ctx != -2) {
6367 continue;
6368 }
6369
6370 /* remember context node */
6371 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01006372 } else {
6373 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006374 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006375
6376 node = set->val.scnodes[i].scnode;
6377
6378 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006379 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006380 } else {
6381 /* root does not have a parent */
6382 continue;
6383 }
6384
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006385 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006386 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006387 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006388
Michal Vasko03ff5a72019-09-11 13:49:33 +02006389 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006390 /* 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 +02006391 new_type = LYXP_NODE_ELEM;
6392 }
6393
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006394 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6395 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006396 set->val.scnodes[idx].in_ctx = 2;
6397 temp_ctx = 1;
6398 }
6399 }
6400
6401 if (temp_ctx) {
6402 for (i = 0; i < orig_used; ++i) {
6403 if (set->val.scnodes[i].in_ctx == 2) {
6404 set->val.scnodes[i].in_ctx = 1;
6405 }
6406 }
6407 }
6408
6409 return LY_SUCCESS;
6410}
6411
6412/**
6413 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6414 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6415 *
6416 * @param[in,out] set1 Set to use for the result.
6417 * @param[in] set2 Set acting as the second operand for @p op.
6418 * @param[in] op Comparison operator to process.
6419 * @param[in] options XPath options.
6420 * @return LY_ERR
6421 */
6422static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006423moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006424{
6425 /*
6426 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6427 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6428 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6429 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6430 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6431 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6432 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6433 *
6434 * '=' or '!='
6435 * BOOLEAN + BOOLEAN
6436 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6437 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6438 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6439 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6440 * NUMBER + NUMBER
6441 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6442 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6443 * STRING + STRING
6444 *
6445 * '<=', '<', '>=', '>'
6446 * NUMBER + NUMBER
6447 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6448 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6449 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6450 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6451 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6452 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6453 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6454 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6455 */
6456 struct lyxp_set iter1, iter2;
6457 int result;
6458 int64_t i;
6459 LY_ERR rc;
6460
Michal Vasko004d3152020-06-11 19:59:22 +02006461 memset(&iter1, 0, sizeof iter1);
6462 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006463
6464 /* iterative evaluation with node-sets */
6465 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6466 if (set1->type == LYXP_SET_NODE_SET) {
6467 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006468 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006469 switch (set2->type) {
6470 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006471 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006472 break;
6473 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006474 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006475 break;
6476 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006477 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006478 break;
6479 }
6480 LY_CHECK_RET(rc);
6481
Michal Vasko4c7763f2020-07-27 17:40:37 +02006482 /* canonize set2 */
6483 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6484
6485 /* compare recursively */
6486 rc = moveto_op_comp(&iter1, &iter2, op, options);
6487 lyxp_set_free_content(&iter2);
6488 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006489
6490 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006491 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006492 set_fill_boolean(set1, 1);
6493 return LY_SUCCESS;
6494 }
6495 }
6496 } else {
6497 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006498 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006499 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006500 case LYXP_SET_NUMBER:
6501 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6502 break;
6503 case LYXP_SET_BOOLEAN:
6504 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6505 break;
6506 default:
6507 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6508 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006509 }
6510 LY_CHECK_RET(rc);
6511
Michal Vasko4c7763f2020-07-27 17:40:37 +02006512 /* canonize set1 */
6513 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 +02006514
Michal Vasko4c7763f2020-07-27 17:40:37 +02006515 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006516 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006517 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006518 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006519
6520 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006521 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006522 set_fill_boolean(set1, 1);
6523 return LY_SUCCESS;
6524 }
6525 }
6526 }
6527
6528 /* false for all nodes */
6529 set_fill_boolean(set1, 0);
6530 return LY_SUCCESS;
6531 }
6532
6533 /* first convert properly */
6534 if ((op[0] == '=') || (op[0] == '!')) {
6535 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006536 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6537 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006538 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006539 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006540 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006541 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006542 LY_CHECK_RET(rc);
6543 } /* else we have 2 strings */
6544 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006545 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006546 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006547 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006548 LY_CHECK_RET(rc);
6549 }
6550
6551 assert(set1->type == set2->type);
6552
6553 /* compute result */
6554 if (op[0] == '=') {
6555 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006556 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006557 } else if (set1->type == LYXP_SET_NUMBER) {
6558 result = (set1->val.num == set2->val.num);
6559 } else {
6560 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006561 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006562 }
6563 } else if (op[0] == '!') {
6564 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006565 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006566 } else if (set1->type == LYXP_SET_NUMBER) {
6567 result = (set1->val.num != set2->val.num);
6568 } else {
6569 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006570 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006571 }
6572 } else {
6573 assert(set1->type == LYXP_SET_NUMBER);
6574 if (op[0] == '<') {
6575 if (op[1] == '=') {
6576 result = (set1->val.num <= set2->val.num);
6577 } else {
6578 result = (set1->val.num < set2->val.num);
6579 }
6580 } else {
6581 if (op[1] == '=') {
6582 result = (set1->val.num >= set2->val.num);
6583 } else {
6584 result = (set1->val.num > set2->val.num);
6585 }
6586 }
6587 }
6588
6589 /* assign result */
6590 if (result) {
6591 set_fill_boolean(set1, 1);
6592 } else {
6593 set_fill_boolean(set1, 0);
6594 }
6595
6596 return LY_SUCCESS;
6597}
6598
6599/**
6600 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6601 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6602 *
6603 * @param[in,out] set1 Set to use for the result.
6604 * @param[in] set2 Set acting as the second operand for @p op.
6605 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006606 * @return LY_ERR
6607 */
6608static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006609moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006610{
6611 LY_ERR rc;
6612
6613 /* unary '-' */
6614 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006615 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006616 LY_CHECK_RET(rc);
6617 set1->val.num *= -1;
6618 lyxp_set_free(set2);
6619 return LY_SUCCESS;
6620 }
6621
6622 assert(set1 && set2);
6623
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006624 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006625 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006626 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006627 LY_CHECK_RET(rc);
6628
6629 switch (op[0]) {
6630 /* '+' */
6631 case '+':
6632 set1->val.num += set2->val.num;
6633 break;
6634
6635 /* '-' */
6636 case '-':
6637 set1->val.num -= set2->val.num;
6638 break;
6639
6640 /* '*' */
6641 case '*':
6642 set1->val.num *= set2->val.num;
6643 break;
6644
6645 /* 'div' */
6646 case 'd':
6647 set1->val.num /= set2->val.num;
6648 break;
6649
6650 /* 'mod' */
6651 case 'm':
6652 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6653 break;
6654
6655 default:
6656 LOGINT_RET(set1->ctx);
6657 }
6658
6659 return LY_SUCCESS;
6660}
6661
6662/*
6663 * eval functions
6664 *
6665 * They execute a parsed XPath expression on some data subtree.
6666 */
6667
6668/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006669 * @brief Evaluate Predicate. Logs directly on error.
6670 *
Michal Vaskod3678892020-05-21 10:06:58 +02006671 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006672 *
6673 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006674 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006675 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6676 * @param[in] options XPath options.
6677 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6678 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6679 */
6680static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006681eval_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 +02006682{
6683 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006684 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006685 uint32_t orig_pos, orig_size;
6686 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006687 struct lyxp_set set2;
6688 struct lyd_node *orig_parent;
6689
6690 /* '[' */
6691 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006692 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006693 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006694
6695 if (!set) {
6696only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006697 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006698 LY_CHECK_RET(rc);
6699 } else if (set->type == LYXP_SET_NODE_SET) {
6700 /* 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 +01006701 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006702
6703 /* empty set, nothing to evaluate */
6704 if (!set->used) {
6705 goto only_parse;
6706 }
6707
Michal Vasko004d3152020-06-11 19:59:22 +02006708 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006709 orig_pos = 0;
6710 orig_size = set->used;
6711 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006712 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006713 set_init(&set2, set);
6714 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6715 /* remember the node context position for position() and context size for last(),
6716 * predicates should always be evaluated with respect to the child axis (since we do
6717 * not support explicit axes) so we assign positions based on their parents */
6718 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6719 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6720 orig_pos = 1;
6721 } else {
6722 ++orig_pos;
6723 }
6724
6725 set2.ctx_pos = orig_pos;
6726 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006727 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006728
Michal Vasko004d3152020-06-11 19:59:22 +02006729 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006730 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006731 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006732 return rc;
6733 }
6734
6735 /* number is a position */
6736 if (set2.type == LYXP_SET_NUMBER) {
6737 if ((long long)set2.val.num == orig_pos) {
6738 set2.val.num = 1;
6739 } else {
6740 set2.val.num = 0;
6741 }
6742 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006743 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006744
6745 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006746 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006747 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006748 }
6749 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006750 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006751
6752 } else if (set->type == LYXP_SET_SCNODE_SET) {
6753 for (i = 0; i < set->used; ++i) {
6754 if (set->val.scnodes[i].in_ctx == 1) {
6755 /* there is a currently-valid node */
6756 break;
6757 }
6758 }
6759 /* empty set, nothing to evaluate */
6760 if (i == set->used) {
6761 goto only_parse;
6762 }
6763
Michal Vasko004d3152020-06-11 19:59:22 +02006764 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006765
Michal Vasko03ff5a72019-09-11 13:49:33 +02006766 /* set special in_ctx to all the valid snodes */
6767 pred_in_ctx = set_scnode_new_in_ctx(set);
6768
6769 /* use the valid snodes one-by-one */
6770 for (i = 0; i < set->used; ++i) {
6771 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6772 continue;
6773 }
6774 set->val.scnodes[i].in_ctx = 1;
6775
Michal Vasko004d3152020-06-11 19:59:22 +02006776 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006777
Michal Vasko004d3152020-06-11 19:59:22 +02006778 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006779 LY_CHECK_RET(rc);
6780
6781 set->val.scnodes[i].in_ctx = pred_in_ctx;
6782 }
6783
6784 /* restore the state as it was before the predicate */
6785 for (i = 0; i < set->used; ++i) {
6786 if (set->val.scnodes[i].in_ctx == 1) {
6787 set->val.scnodes[i].in_ctx = 0;
6788 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6789 set->val.scnodes[i].in_ctx = 1;
6790 }
6791 }
6792
6793 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006794 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006795 set_fill_set(&set2, set);
6796
Michal Vasko004d3152020-06-11 19:59:22 +02006797 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006798 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006799 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006800 return rc;
6801 }
6802
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006803 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006804 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006805 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006806 }
Michal Vaskod3678892020-05-21 10:06:58 +02006807 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006808 }
6809
6810 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006811 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006812 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006813 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006814 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006815
6816 return LY_SUCCESS;
6817}
6818
6819/**
Michal Vaskod3678892020-05-21 10:06:58 +02006820 * @brief Evaluate Literal. Logs directly on error.
6821 *
6822 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006823 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006824 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6825 */
6826static void
Michal Vasko004d3152020-06-11 19:59:22 +02006827eval_literal(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006828{
6829 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006830 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006831 set_fill_string(set, "", 0);
6832 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006833 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 +02006834 }
6835 }
6836 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006837 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006838 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006839}
6840
6841/**
Michal Vasko004d3152020-06-11 19:59:22 +02006842 * @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 +02006843 *
Michal Vasko004d3152020-06-11 19:59:22 +02006844 * @param[in] exp Full parsed XPath expression.
6845 * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success.
6846 * @param[in] scnode Found schema node as context for the predicate.
6847 * @param[in] format Format of any prefixes in key names/values.
6848 * @param[out] predicates Parsed predicates.
6849 * @param[out] pred_type Type of @p predicates.
6850 * @return LY_SUCCESS on success,
6851 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006852 */
6853static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006854eval_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 +02006855 LY_PREFIX_FORMAT format, struct ly_path_predicate **predicates,
6856 enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006857{
Michal Vasko004d3152020-06-11 19:59:22 +02006858 LY_ERR ret = LY_SUCCESS;
6859 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006860 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006861 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006862 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006863 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006864
Michal Vasko004d3152020-06-11 19:59:22 +02006865 assert(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006866
Michal Vasko004d3152020-06-11 19:59:22 +02006867 if (scnode->nodetype == LYS_LIST) {
6868 /* get key count */
6869 if (scnode->flags & LYS_KEYLESS) {
6870 return LY_EINVAL;
6871 }
Radek Krejci1e008d22020-08-17 11:37:37 +02006872 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 +02006873 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006874
Michal Vasko004d3152020-06-11 19:59:22 +02006875 /* learn where the predicates end */
6876 e_idx = *tok_idx;
6877 while (key_count) {
6878 /* '[' */
6879 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6880 return LY_EINVAL;
6881 }
6882 ++e_idx;
6883
6884 /* ']' */
6885 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6886 ++e_idx;
6887 }
6888 ++e_idx;
6889
6890 /* another presumably key predicate parsed */
6891 --key_count;
6892 }
Michal Vasko004d3152020-06-11 19:59:22 +02006893 } else {
6894 /* learn just where this single predicate ends */
6895 e_idx = *tok_idx;
6896
Michal Vaskod3678892020-05-21 10:06:58 +02006897 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006898 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6899 return LY_EINVAL;
6900 }
6901 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006902
6903 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006904 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6905 ++e_idx;
6906 }
6907 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006908 }
6909
Michal Vasko004d3152020-06-11 19:59:22 +02006910 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6911 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6912
6913 /* turn logging off */
6914 prev_lo = ly_log_options(0);
6915
6916 /* parse the predicate(s) */
Michal Vasko6b26e742020-07-17 15:02:10 +02006917 LY_CHECK_GOTO(ret = ly_path_parse_predicate(scnode->module->ctx, scnode, exp->expr + exp->tok_pos[*tok_idx], pred_len,
Michal Vasko69730152020-10-09 16:30:07 +02006918 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02006919
6920 /* compile */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006921 ret = ly_path_compile_predicate(scnode->module->ctx, scnode, scnode->module, scnode, exp2, &pred_idx,
Michal Vasko69730152020-10-09 16:30:07 +02006922 format, scnode->module, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006923 LY_CHECK_GOTO(ret, cleanup);
6924
6925 /* success, the predicate must include all the needed information for hash-based search */
6926 *tok_idx = e_idx;
6927
6928cleanup:
6929 ly_log_options(prev_lo);
6930 lyxp_expr_free(scnode->module->ctx, exp2);
6931 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006932}
6933
6934/**
6935 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
6936 *
6937 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
6938 * [6] NodeTest ::= NameTest | NodeType '(' ')'
6939 * [7] NameTest ::= '*' | NCName ':' '*' | QName
6940 *
6941 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006942 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006943 * @param[in] attr_axis Whether to search attributes or standard nodes.
6944 * @param[in] all_desc Whether to search all the descendants or children only.
6945 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6946 * @param[in] options XPath options.
6947 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6948 */
6949static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006950eval_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 +02006951 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02006952{
Michal Vaskod3678892020-05-21 10:06:58 +02006953 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02006954 const char *ncname, *ncname_dict = NULL;
6955 uint16_t ncname_len;
Michal Vaskod3678892020-05-21 10:06:58 +02006956 const struct lys_module *moveto_mod;
6957 const struct lysc_node *scnode = NULL, *tmp;
Michal Vasko004d3152020-06-11 19:59:22 +02006958 struct ly_path_predicate *predicates = NULL;
6959 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006960 LY_ERR rc = LY_SUCCESS;
6961
6962 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006963 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006964 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006965
6966 if (!set) {
6967 goto moveto;
6968 }
6969
Michal Vasko004d3152020-06-11 19:59:22 +02006970 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
6971 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02006972
6973 /* parse (and skip) module name */
6974 rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod);
6975 LY_CHECK_GOTO(rc, cleanup);
6976
6977 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
6978 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006979 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +02006980 if (set->val.nodes[i].type == set->root_type) {
6981 tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0);
Michal Vasko69730152020-10-09 16:30:07 +02006982 } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM) &&
6983 (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) {
Michal Vaskod3678892020-05-21 10:06:58 +02006984 /* do not repeat the same search */
6985 tmp = lys_find_child(set->val.nodes[i].node->schema, moveto_mod, ncname, ncname_len, 0, 0);
Radek Krejcib1247842020-07-02 16:22:38 +02006986 } else {
6987 tmp = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006988 }
6989
6990 /* additional context check */
6991 if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) {
6992 tmp = NULL;
6993 }
6994
6995 if (tmp) {
6996 if (scnode) {
6997 /* we found a schema node with the same name but at different level, give up, too complicated */
6998 scnode = NULL;
6999 break;
7000 } else {
7001 /* remember the found schema node and continue to make sure it can be used */
7002 scnode = tmp;
7003 }
Michal Vaskod3678892020-05-21 10:06:58 +02007004 }
7005 }
7006
Michal Vasko004d3152020-06-11 19:59:22 +02007007 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7008 /* try to create the predicates */
7009 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) {
7010 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007011 scnode = NULL;
7012 }
7013 }
7014 }
7015
Michal Vasko004d3152020-06-11 19:59:22 +02007016 if (!scnode && moveto_mod) {
7017 /* we are not able to match based on a schema node and not all the modules match,
7018 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007019 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007020 }
7021
7022moveto:
7023 /* move to the attribute(s), data node(s), or schema node(s) */
7024 if (attr_axis) {
7025 if (set && (options & LYXP_SCNODE_ALL)) {
7026 set_scnode_clear_ctx(set);
7027 } else {
7028 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007029 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007030 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007031 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007032 }
7033 LY_CHECK_GOTO(rc, cleanup);
7034 }
7035 } else {
7036 if (set && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007037 int64_t i;
7038
Michal Vaskod3678892020-05-21 10:06:58 +02007039 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007040 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007041 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007042 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007043 }
7044 LY_CHECK_GOTO(rc, cleanup);
7045
7046 for (i = set->used - 1; i > -1; --i) {
7047 if (set->val.scnodes[i].in_ctx > 0) {
7048 break;
7049 }
7050 }
7051 if (i == -1) {
Michal Vasko9e9f26d2020-10-12 16:31:33 +02007052 const struct lysc_node *real_ctx_scnode = set->ctx_scnode->nodetype & (LYS_CHOICE | LYS_CASE) ?
7053 lysc_data_parent(set->ctx_scnode) : set->ctx_scnode;
7054 path = lysc_path(real_ctx_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02007055 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02007056 ncname_len, ncname, ncname - exp->expr, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007057 free(path);
7058 }
7059 } else {
7060 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007061 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007062 } else {
7063 if (scnode) {
7064 /* we can find the nodes using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02007065 rc = moveto_node_hash(set, scnode, predicates);
Michal Vaskod3678892020-05-21 10:06:58 +02007066 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007067 rc = moveto_node(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007068 }
7069 }
7070 LY_CHECK_GOTO(rc, cleanup);
7071 }
7072 }
7073
7074 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007075 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7076 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007077 LY_CHECK_RET(rc);
7078 }
7079
7080cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007081 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007082 lydict_remove(set->ctx, ncname_dict);
7083 ly_path_predicates_free(set->ctx, pred_type, scnode, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007084 }
Michal Vaskod3678892020-05-21 10:06:58 +02007085 return rc;
7086}
7087
7088/**
7089 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7090 *
7091 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7092 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7093 * [8] NodeType ::= 'text' | 'node'
7094 *
7095 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007096 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007097 * @param[in] attr_axis Whether to search attributes or standard nodes.
7098 * @param[in] all_desc Whether to search all the descendants or children only.
7099 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7100 * @param[in] options XPath options.
7101 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7102 */
7103static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02007104eval_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 +02007105 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007106{
7107 LY_ERR rc;
7108
7109 /* TODO */
7110 (void)attr_axis;
7111 (void)all_desc;
7112
7113 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007114 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007115 if (set->type == LYXP_SET_SCNODE_SET) {
7116 set_scnode_clear_ctx(set);
7117 /* just for the debug message below */
7118 set = NULL;
7119 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007120 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007121 rc = xpath_node(NULL, 0, set, options);
7122 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007123 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007124 rc = xpath_text(NULL, 0, set, options);
7125 }
7126 LY_CHECK_RET(rc);
7127 }
7128 }
7129 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007130 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007131 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007132
7133 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007134 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007135 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007136 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007137 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007138
7139 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007140 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007141 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007142 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007143 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007144
7145 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007146 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7147 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007148 LY_CHECK_RET(rc);
7149 }
7150
7151 return LY_SUCCESS;
7152}
7153
7154/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007155 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7156 *
7157 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7158 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007159 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007160 *
7161 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007162 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007163 * @param[in] all_desc Whether to search all the descendants or children only.
7164 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7165 * @param[in] options XPath options.
7166 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7167 */
7168static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02007169eval_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 +02007170{
Radek Krejci857189e2020-09-01 13:26:36 +02007171 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007172 LY_ERR rc;
7173
7174 goto step;
7175 do {
7176 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007177 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007178 all_desc = 0;
7179 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007180 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007181 all_desc = 1;
7182 }
7183 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007184 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007185 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007186
7187step:
Michal Vaskod3678892020-05-21 10:06:58 +02007188 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007189 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007190 attr_axis = 1;
7191
7192 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007193 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007194 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007195 } else {
7196 attr_axis = 0;
7197 }
7198
Michal Vasko03ff5a72019-09-11 13:49:33 +02007199 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007200 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007201 case LYXP_TOKEN_DOT:
7202 /* evaluate '.' */
7203 if (set && (options & LYXP_SCNODE_ALL)) {
7204 rc = moveto_scnode_self(set, all_desc, options);
7205 } else {
7206 rc = moveto_self(set, all_desc, options);
7207 }
7208 LY_CHECK_RET(rc);
7209 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007210 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007211 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007212 break;
7213
7214 case LYXP_TOKEN_DDOT:
7215 /* evaluate '..' */
7216 if (set && (options & LYXP_SCNODE_ALL)) {
7217 rc = moveto_scnode_parent(set, all_desc, options);
7218 } else {
7219 rc = moveto_parent(set, all_desc, options);
7220 }
7221 LY_CHECK_RET(rc);
7222 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007223 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007224 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007225 break;
7226
Michal Vasko03ff5a72019-09-11 13:49:33 +02007227 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007228 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007229 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007230 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007231 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007232
Michal Vaskod3678892020-05-21 10:06:58 +02007233 case LYXP_TOKEN_NODETYPE:
7234 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007235 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007236 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007237 break;
7238
7239 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007240 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007241 }
Michal Vasko004d3152020-06-11 19:59:22 +02007242 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007243
7244 return LY_SUCCESS;
7245}
7246
7247/**
7248 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7249 *
7250 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7251 *
7252 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007253 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007254 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7255 * @param[in] options XPath options.
7256 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7257 */
7258static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007259eval_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 +02007260{
Radek Krejci857189e2020-09-01 13:26:36 +02007261 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007262
7263 if (set) {
7264 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007265 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007266 }
7267
7268 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007269 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007270 /* evaluate '/' - deferred */
7271 all_desc = 0;
7272 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007273 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007274 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007275
Michal Vasko004d3152020-06-11 19:59:22 +02007276 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007277 return LY_SUCCESS;
7278 }
Michal Vasko004d3152020-06-11 19:59:22 +02007279 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007280 case LYXP_TOKEN_DOT:
7281 case LYXP_TOKEN_DDOT:
7282 case LYXP_TOKEN_AT:
7283 case LYXP_TOKEN_NAMETEST:
7284 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007285 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007286 break;
7287 default:
7288 break;
7289 }
7290
Michal Vasko03ff5a72019-09-11 13:49:33 +02007291 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007292 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007293 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7294 all_desc = 1;
7295 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007296 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007297 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007298
Michal Vaskob0099a92020-08-31 14:55:23 +02007299 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007300 }
7301
7302 return LY_SUCCESS;
7303}
7304
7305/**
7306 * @brief Evaluate FunctionCall. Logs directly on error.
7307 *
Michal Vaskod3678892020-05-21 10:06:58 +02007308 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007309 *
7310 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007311 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007312 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7313 * @param[in] options XPath options.
7314 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7315 */
7316static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007317eval_function_call(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007318{
7319 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007320
Radek Krejci1deb5be2020-08-26 16:43:36 +02007321 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007322 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007323 struct lyxp_set **args = NULL, **args_aux;
7324
7325 if (set) {
7326 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007327 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007328 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007329 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007330 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007331 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007332 xpath_func = &xpath_sum;
7333 }
7334 break;
7335 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007336 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007337 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007338 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007339 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007340 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007341 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007342 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007343 xpath_func = &xpath_true;
7344 }
7345 break;
7346 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007347 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007348 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007349 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007350 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007351 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007352 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007353 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007354 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007355 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007356 xpath_func = &xpath_deref;
7357 }
7358 break;
7359 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007360 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007361 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007362 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007363 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007364 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007365 xpath_func = &xpath_string;
7366 }
7367 break;
7368 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007369 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007370 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007371 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007372 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007373 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007374 xpath_func = &xpath_current;
7375 }
7376 break;
7377 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007378 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007379 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007380 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007381 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007382 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007383 xpath_func = &xpath_re_match;
7384 }
7385 break;
7386 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007387 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007388 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007389 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007390 xpath_func = &xpath_translate;
7391 }
7392 break;
7393 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007394 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007395 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007396 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007397 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007398 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007399 xpath_func = &xpath_bit_is_set;
7400 }
7401 break;
7402 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007403 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007404 xpath_func = &xpath_starts_with;
7405 }
7406 break;
7407 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007408 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007409 xpath_func = &xpath_derived_from;
7410 }
7411 break;
7412 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007413 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007414 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007415 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007416 xpath_func = &xpath_string_length;
7417 }
7418 break;
7419 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007420 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007421 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007422 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007423 xpath_func = &xpath_substring_after;
7424 }
7425 break;
7426 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007427 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007428 xpath_func = &xpath_substring_before;
7429 }
7430 break;
7431 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007432 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007433 xpath_func = &xpath_derived_from_or_self;
7434 }
7435 break;
7436 }
7437
7438 if (!xpath_func) {
Michal Vasko004d3152020-06-11 19:59:22 +02007439 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 +02007440 return LY_EVALID;
7441 }
7442 }
7443
7444 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007445 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007446 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007447
7448 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007449 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007450 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007451 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007452 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007453
7454 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007455 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007456 if (set) {
7457 args = malloc(sizeof *args);
7458 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7459 arg_count = 1;
7460 args[0] = set_copy(set);
7461 if (!args[0]) {
7462 rc = LY_EMEM;
7463 goto cleanup;
7464 }
7465
Michal Vasko004d3152020-06-11 19:59:22 +02007466 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007467 LY_CHECK_GOTO(rc, cleanup);
7468 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007469 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007470 LY_CHECK_GOTO(rc, cleanup);
7471 }
7472 }
Michal Vasko004d3152020-06-11 19:59:22 +02007473 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007474 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007475 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007476 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007477
7478 if (set) {
7479 ++arg_count;
7480 args_aux = realloc(args, arg_count * sizeof *args);
7481 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7482 args = args_aux;
7483 args[arg_count - 1] = set_copy(set);
7484 if (!args[arg_count - 1]) {
7485 rc = LY_EMEM;
7486 goto cleanup;
7487 }
7488
Michal Vasko004d3152020-06-11 19:59:22 +02007489 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007490 LY_CHECK_GOTO(rc, cleanup);
7491 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007492 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007493 LY_CHECK_GOTO(rc, cleanup);
7494 }
7495 }
7496
7497 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007498 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007499 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007500 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007501 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007502
7503 if (set) {
7504 /* evaluate function */
7505 rc = xpath_func(args, arg_count, set, options);
7506
7507 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007508 /* merge all nodes from arg evaluations */
7509 for (i = 0; i < arg_count; ++i) {
7510 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007511 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007512 }
7513 }
7514 } else {
7515 rc = LY_SUCCESS;
7516 }
7517
7518cleanup:
7519 for (i = 0; i < arg_count; ++i) {
7520 lyxp_set_free(args[i]);
7521 }
7522 free(args);
7523
7524 return rc;
7525}
7526
7527/**
7528 * @brief Evaluate Number. Logs directly on error.
7529 *
7530 * @param[in] ctx Context for errors.
7531 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007532 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007533 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7534 * @return LY_ERR
7535 */
7536static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007537eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007538{
7539 long double num;
7540 char *endptr;
7541
7542 if (set) {
7543 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007544 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545 if (errno) {
Michal Vasko004d3152020-06-11 19:59:22 +02007546 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 +02007547 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007548 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007549 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007550 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
7551 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 +02007552 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007553 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007554 return LY_EVALID;
7555 }
7556
7557 set_fill_number(set, num);
7558 }
7559
7560 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007561 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007562 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007563 return LY_SUCCESS;
7564}
7565
7566/**
7567 * @brief Evaluate PathExpr. Logs directly on error.
7568 *
Michal Vaskod3678892020-05-21 10:06:58 +02007569 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7571 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7572 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007573 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007574 *
7575 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007576 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007577 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7578 * @param[in] options XPath options.
7579 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7580 */
7581static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007582eval_path_expr(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007583{
Radek Krejci857189e2020-09-01 13:26:36 +02007584 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007585 LY_ERR rc;
7586
Michal Vasko004d3152020-06-11 19:59:22 +02007587 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007588 case LYXP_TOKEN_PAR1:
7589 /* '(' Expr ')' */
7590
7591 /* '(' */
7592 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007593 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007594 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007595
7596 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007597 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007598 LY_CHECK_RET(rc);
7599
7600 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007601 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007602 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007603 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007604 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007605
7606 parent_pos_pred = 0;
7607 goto predicate;
7608
7609 case LYXP_TOKEN_DOT:
7610 case LYXP_TOKEN_DDOT:
7611 case LYXP_TOKEN_AT:
7612 case LYXP_TOKEN_NAMETEST:
7613 case LYXP_TOKEN_NODETYPE:
7614 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007615 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007616 LY_CHECK_RET(rc);
7617 break;
7618
7619 case LYXP_TOKEN_FUNCNAME:
7620 /* FunctionCall */
7621 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007622 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007623 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007624 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007625 }
7626 LY_CHECK_RET(rc);
7627
7628 parent_pos_pred = 1;
7629 goto predicate;
7630
Michal Vasko3e48bf32020-06-01 08:39:07 +02007631 case LYXP_TOKEN_OPER_PATH:
7632 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007633 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007634 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007635 LY_CHECK_RET(rc);
7636 break;
7637
7638 case LYXP_TOKEN_LITERAL:
7639 /* Literal */
7640 if (!set || (options & LYXP_SCNODE_ALL)) {
7641 if (set) {
7642 set_scnode_clear_ctx(set);
7643 }
Michal Vasko004d3152020-06-11 19:59:22 +02007644 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007645 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007646 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007647 }
7648
7649 parent_pos_pred = 1;
7650 goto predicate;
7651
7652 case LYXP_TOKEN_NUMBER:
7653 /* Number */
7654 if (!set || (options & LYXP_SCNODE_ALL)) {
7655 if (set) {
7656 set_scnode_clear_ctx(set);
7657 }
Michal Vasko004d3152020-06-11 19:59:22 +02007658 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007659 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007660 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007661 }
7662 LY_CHECK_RET(rc);
7663
7664 parent_pos_pred = 1;
7665 goto predicate;
7666
7667 default:
Michal Vasko004d3152020-06-11 19:59:22 +02007668 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]),
Michal Vasko69730152020-10-09 16:30:07 +02007669 &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007670 return LY_EVALID;
7671 }
7672
7673 return LY_SUCCESS;
7674
7675predicate:
7676 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007677 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7678 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007679 LY_CHECK_RET(rc);
7680 }
7681
7682 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007683 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007684
7685 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007686 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007687 all_desc = 0;
7688 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007689 all_desc = 1;
7690 }
7691
7692 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007693 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007694 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007695
Michal Vasko004d3152020-06-11 19:59:22 +02007696 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007697 LY_CHECK_RET(rc);
7698 }
7699
7700 return LY_SUCCESS;
7701}
7702
7703/**
7704 * @brief Evaluate UnionExpr. Logs directly on error.
7705 *
Michal Vaskod3678892020-05-21 10:06:58 +02007706 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007707 *
7708 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007709 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007710 * @param[in] repeat How many times this expression is repeated.
7711 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7712 * @param[in] options XPath options.
7713 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7714 */
7715static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007716eval_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 +02007717{
7718 LY_ERR rc = LY_SUCCESS;
7719 struct lyxp_set orig_set, set2;
7720 uint16_t i;
7721
7722 assert(repeat);
7723
7724 set_init(&orig_set, set);
7725 set_init(&set2, set);
7726
7727 set_fill_set(&orig_set, set);
7728
Michal Vasko004d3152020-06-11 19:59:22 +02007729 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007730 LY_CHECK_GOTO(rc, cleanup);
7731
7732 /* ('|' PathExpr)* */
7733 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007734 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007735 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007736 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007737 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007738
7739 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007740 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007741 LY_CHECK_GOTO(rc, cleanup);
7742 continue;
7743 }
7744
7745 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007746 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007747 LY_CHECK_GOTO(rc, cleanup);
7748
7749 /* eval */
7750 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007751 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007752 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007753 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007754 LY_CHECK_GOTO(rc, cleanup);
7755 }
7756 }
7757
7758cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007759 lyxp_set_free_content(&orig_set);
7760 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007761 return rc;
7762}
7763
7764/**
7765 * @brief Evaluate UnaryExpr. Logs directly on error.
7766 *
Michal Vaskod3678892020-05-21 10:06:58 +02007767 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007768 *
7769 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007770 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007771 * @param[in] repeat How many times this expression is repeated.
7772 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7773 * @param[in] options XPath options.
7774 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7775 */
7776static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007777eval_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 +02007778{
7779 LY_ERR rc;
7780 uint16_t this_op, i;
7781
7782 assert(repeat);
7783
7784 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007785 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007786 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007787 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 +02007788
7789 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007790 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007791 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007792 }
7793
Michal Vasko004d3152020-06-11 19:59:22 +02007794 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007795 LY_CHECK_RET(rc);
7796
7797 if (set && (repeat % 2)) {
7798 if (options & LYXP_SCNODE_ALL) {
7799 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7800 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007801 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007802 LY_CHECK_RET(rc);
7803 }
7804 }
7805
7806 return LY_SUCCESS;
7807}
7808
7809/**
7810 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7811 *
Michal Vaskod3678892020-05-21 10:06:58 +02007812 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007813 * | MultiplicativeExpr '*' UnaryExpr
7814 * | MultiplicativeExpr 'div' UnaryExpr
7815 * | MultiplicativeExpr 'mod' UnaryExpr
7816 *
7817 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007818 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007819 * @param[in] repeat How many times this expression is repeated.
7820 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7821 * @param[in] options XPath options.
7822 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7823 */
7824static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007825eval_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 +02007826{
7827 LY_ERR rc;
7828 uint16_t this_op;
7829 struct lyxp_set orig_set, set2;
7830 uint16_t i;
7831
7832 assert(repeat);
7833
7834 set_init(&orig_set, set);
7835 set_init(&set2, set);
7836
7837 set_fill_set(&orig_set, set);
7838
Michal Vasko004d3152020-06-11 19:59:22 +02007839 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007840 LY_CHECK_GOTO(rc, cleanup);
7841
7842 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7843 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007844 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007845
Michal Vasko004d3152020-06-11 19:59:22 +02007846 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007847 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007848 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007849 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007850
7851 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007852 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007853 LY_CHECK_GOTO(rc, cleanup);
7854 continue;
7855 }
7856
7857 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007858 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007859 LY_CHECK_GOTO(rc, cleanup);
7860
7861 /* eval */
7862 if (options & LYXP_SCNODE_ALL) {
7863 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007864 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007865 set_scnode_clear_ctx(set);
7866 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007867 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007868 LY_CHECK_GOTO(rc, cleanup);
7869 }
7870 }
7871
7872cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007873 lyxp_set_free_content(&orig_set);
7874 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007875 return rc;
7876}
7877
7878/**
7879 * @brief Evaluate AdditiveExpr. Logs directly on error.
7880 *
Michal Vaskod3678892020-05-21 10:06:58 +02007881 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007882 * | AdditiveExpr '+' MultiplicativeExpr
7883 * | AdditiveExpr '-' MultiplicativeExpr
7884 *
7885 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007886 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007887 * @param[in] repeat How many times this expression is repeated.
7888 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7889 * @param[in] options XPath options.
7890 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7891 */
7892static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007893eval_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 +02007894{
7895 LY_ERR rc;
7896 uint16_t this_op;
7897 struct lyxp_set orig_set, set2;
7898 uint16_t i;
7899
7900 assert(repeat);
7901
7902 set_init(&orig_set, set);
7903 set_init(&set2, set);
7904
7905 set_fill_set(&orig_set, set);
7906
Michal Vasko004d3152020-06-11 19:59:22 +02007907 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007908 LY_CHECK_GOTO(rc, cleanup);
7909
7910 /* ('+' / '-' MultiplicativeExpr)* */
7911 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007912 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007913
Michal Vasko004d3152020-06-11 19:59:22 +02007914 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007915 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007916 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007917 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007918
7919 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007920 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007921 LY_CHECK_GOTO(rc, cleanup);
7922 continue;
7923 }
7924
7925 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007926 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007927 LY_CHECK_GOTO(rc, cleanup);
7928
7929 /* eval */
7930 if (options & LYXP_SCNODE_ALL) {
7931 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007932 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007933 set_scnode_clear_ctx(set);
7934 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007935 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007936 LY_CHECK_GOTO(rc, cleanup);
7937 }
7938 }
7939
7940cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007941 lyxp_set_free_content(&orig_set);
7942 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007943 return rc;
7944}
7945
7946/**
7947 * @brief Evaluate RelationalExpr. Logs directly on error.
7948 *
Michal Vaskod3678892020-05-21 10:06:58 +02007949 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007950 * | RelationalExpr '<' AdditiveExpr
7951 * | RelationalExpr '>' AdditiveExpr
7952 * | RelationalExpr '<=' AdditiveExpr
7953 * | RelationalExpr '>=' AdditiveExpr
7954 *
7955 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007956 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007957 * @param[in] repeat How many times this expression is repeated.
7958 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7959 * @param[in] options XPath options.
7960 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7961 */
7962static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007963eval_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 +02007964{
7965 LY_ERR rc;
7966 uint16_t this_op;
7967 struct lyxp_set orig_set, set2;
7968 uint16_t i;
7969
7970 assert(repeat);
7971
7972 set_init(&orig_set, set);
7973 set_init(&set2, set);
7974
7975 set_fill_set(&orig_set, set);
7976
Michal Vasko004d3152020-06-11 19:59:22 +02007977 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007978 LY_CHECK_GOTO(rc, cleanup);
7979
7980 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
7981 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007982 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007983
Michal Vasko004d3152020-06-11 19:59:22 +02007984 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007985 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007986 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007987 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007988
7989 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007990 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007991 LY_CHECK_GOTO(rc, cleanup);
7992 continue;
7993 }
7994
7995 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007996 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007997 LY_CHECK_GOTO(rc, cleanup);
7998
7999 /* eval */
8000 if (options & LYXP_SCNODE_ALL) {
8001 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008002 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008003 set_scnode_clear_ctx(set);
8004 } else {
8005 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8006 LY_CHECK_GOTO(rc, cleanup);
8007 }
8008 }
8009
8010cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008011 lyxp_set_free_content(&orig_set);
8012 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008013 return rc;
8014}
8015
8016/**
8017 * @brief Evaluate EqualityExpr. Logs directly on error.
8018 *
Michal Vaskod3678892020-05-21 10:06:58 +02008019 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008020 * | EqualityExpr '!=' RelationalExpr
8021 *
8022 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008023 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008024 * @param[in] repeat How many times this expression is repeated.
8025 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8026 * @param[in] options XPath options.
8027 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8028 */
8029static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02008030eval_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 +02008031{
8032 LY_ERR rc;
8033 uint16_t this_op;
8034 struct lyxp_set orig_set, set2;
8035 uint16_t i;
8036
8037 assert(repeat);
8038
8039 set_init(&orig_set, set);
8040 set_init(&set2, set);
8041
8042 set_fill_set(&orig_set, set);
8043
Michal Vasko004d3152020-06-11 19:59:22 +02008044 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008045 LY_CHECK_GOTO(rc, cleanup);
8046
8047 /* ('=' / '!=' RelationalExpr)* */
8048 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008049 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008050
Michal Vasko004d3152020-06-11 19:59:22 +02008051 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008052 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008053 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008054 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008055
8056 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008057 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008058 LY_CHECK_GOTO(rc, cleanup);
8059 continue;
8060 }
8061
8062 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008063 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008064 LY_CHECK_GOTO(rc, cleanup);
8065
8066 /* eval */
8067 if (options & LYXP_SCNODE_ALL) {
8068 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008069 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8070 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008071 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008072 set_scnode_clear_ctx(set);
8073 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008074 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8075 LY_CHECK_GOTO(rc, cleanup);
8076 }
8077 }
8078
8079cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008080 lyxp_set_free_content(&orig_set);
8081 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008082 return rc;
8083}
8084
8085/**
8086 * @brief Evaluate AndExpr. Logs directly on error.
8087 *
Michal Vaskod3678892020-05-21 10:06:58 +02008088 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008089 *
8090 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008091 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008092 * @param[in] repeat How many times this expression is repeated.
8093 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8094 * @param[in] options XPath options.
8095 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8096 */
8097static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02008098eval_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 +02008099{
8100 LY_ERR rc;
8101 struct lyxp_set orig_set, set2;
8102 uint16_t i;
8103
8104 assert(repeat);
8105
8106 set_init(&orig_set, set);
8107 set_init(&set2, set);
8108
8109 set_fill_set(&orig_set, set);
8110
Michal Vasko004d3152020-06-11 19:59:22 +02008111 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008112 LY_CHECK_GOTO(rc, cleanup);
8113
8114 /* cast to boolean, we know that will be the final result */
8115 if (set && (options & LYXP_SCNODE_ALL)) {
8116 set_scnode_clear_ctx(set);
8117 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008118 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008119 }
8120
8121 /* ('and' EqualityExpr)* */
8122 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008123 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8124 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008125 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008126 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008127
8128 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008129 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8130 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008131 LY_CHECK_GOTO(rc, cleanup);
8132 continue;
8133 }
8134
8135 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008136 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008137 LY_CHECK_GOTO(rc, cleanup);
8138
8139 /* eval - just get boolean value actually */
8140 if (set->type == LYXP_SET_SCNODE_SET) {
8141 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008142 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008143 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008144 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008145 set_fill_set(set, &set2);
8146 }
8147 }
8148
8149cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008150 lyxp_set_free_content(&orig_set);
8151 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008152 return rc;
8153}
8154
8155/**
8156 * @brief Evaluate OrExpr. Logs directly on error.
8157 *
Michal Vaskod3678892020-05-21 10:06:58 +02008158 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008159 *
8160 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008161 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008162 * @param[in] repeat How many times this expression is repeated.
8163 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8164 * @param[in] options XPath options.
8165 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8166 */
8167static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02008168eval_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 +02008169{
8170 LY_ERR rc;
8171 struct lyxp_set orig_set, set2;
8172 uint16_t i;
8173
8174 assert(repeat);
8175
8176 set_init(&orig_set, set);
8177 set_init(&set2, set);
8178
8179 set_fill_set(&orig_set, set);
8180
Michal Vasko004d3152020-06-11 19:59:22 +02008181 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008182 LY_CHECK_GOTO(rc, cleanup);
8183
8184 /* cast to boolean, we know that will be the final result */
8185 if (set && (options & LYXP_SCNODE_ALL)) {
8186 set_scnode_clear_ctx(set);
8187 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008188 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008189 }
8190
8191 /* ('or' AndExpr)* */
8192 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008193 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8194 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008195 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008196 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008197
8198 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008199 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8200 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008201 LY_CHECK_GOTO(rc, cleanup);
8202 continue;
8203 }
8204
8205 set_fill_set(&set2, &orig_set);
8206 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8207 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008208 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008209 LY_CHECK_GOTO(rc, cleanup);
8210
8211 /* eval - just get boolean value actually */
8212 if (set->type == LYXP_SET_SCNODE_SET) {
8213 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008214 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008215 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008216 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008217 set_fill_set(set, &set2);
8218 }
8219 }
8220
8221cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008222 lyxp_set_free_content(&orig_set);
8223 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008224 return rc;
8225}
8226
8227/**
Michal Vasko004d3152020-06-11 19:59:22 +02008228 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008229 *
8230 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008231 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008232 * @param[in] etype Expression type to evaluate.
8233 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8234 * @param[in] options XPath options.
8235 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8236 */
8237static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02008238eval_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 +02008239{
8240 uint16_t i, count;
8241 enum lyxp_expr_type next_etype;
8242 LY_ERR rc;
8243
8244 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008245 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008246 next_etype = LYXP_EXPR_NONE;
8247 } else {
8248 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008249 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008250
8251 /* select one-priority lower because etype expression called us */
8252 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008253 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008254 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008255 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008256 } else {
8257 next_etype = LYXP_EXPR_NONE;
8258 }
8259 }
8260
8261 /* decide what expression are we parsing based on the repeat */
8262 switch (next_etype) {
8263 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008264 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008265 break;
8266 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008267 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008268 break;
8269 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008270 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008271 break;
8272 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008273 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008274 break;
8275 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008276 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008277 break;
8278 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008279 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008280 break;
8281 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008282 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008283 break;
8284 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008285 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008286 break;
8287 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008288 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008289 break;
8290 default:
8291 LOGINT_RET(set->ctx);
8292 }
8293
8294 return rc;
8295}
8296
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008297/**
8298 * @brief Get root type.
8299 *
8300 * @param[in] ctx_node Context node.
8301 * @param[in] ctx_scnode Schema context node.
8302 * @param[in] options XPath options.
8303 * @return Root type.
8304 */
8305static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008306lyxp_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 +01008307{
Michal Vasko6b26e742020-07-17 15:02:10 +02008308 const struct lysc_node *op;
8309
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008310 if (options & LYXP_SCNODE_ALL) {
Radek Krejci1e008d22020-08-17 11:37:37 +02008311 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008312
8313 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008314 /* general root that can access everything */
8315 return LYXP_NODE_ROOT;
8316 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8317 /* root context node can access only config data (because we said so, it is unspecified) */
8318 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008319 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008320 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008321 }
8322
Michal Vasko6b26e742020-07-17 15:02:10 +02008323 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008324 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008325
8326 if (!ctx_node || (!op && (ctx_node->schema->flags & LYS_CONFIG_W))) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008327 /* root context node can access only config data (because we said so, it is unspecified) */
8328 return LYXP_NODE_ROOT_CONFIG;
8329 }
8330
8331 return LYXP_NODE_ROOT;
8332}
8333
Michal Vasko03ff5a72019-09-11 13:49:33 +02008334LY_ERR
Michal Vaskoc8a230d2020-08-14 12:17:10 +02008335lyxp_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 +02008336 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 +02008337{
Michal Vasko004d3152020-06-11 19:59:22 +02008338 uint16_t tok_idx = 0;
8339 const struct lyd_node *real_ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008340 LY_ERR rc;
8341
Michal Vasko004d3152020-06-11 19:59:22 +02008342 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL);
8343
8344 if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) {
8345 /* we always need some context node because it is used for resolving unqualified names */
8346 real_ctx_node = NULL;
8347 } else {
8348 real_ctx_node = ctx_node;
8349 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008350
8351 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008352 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008353 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008354 set->type = LYXP_SET_NODE_SET;
Michal Vasko004d3152020-06-11 19:59:22 +02008355 set_insert_node(set, (struct lyd_node *)real_ctx_node, 0, ctx_node_type, 0);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008356 set->ctx = local_mod->ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008357 set->ctx_node = ctx_node;
Michal Vasko004d3152020-06-11 19:59:22 +02008358 set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options);
Michal Vasko6b26e742020-07-17 15:02:10 +02008359 for (set->context_op = ctx_node->schema;
Radek Krejci0f969882020-08-21 16:56:47 +02008360 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8361 set->context_op = set->context_op->parent) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008362 set->local_mod = local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +01008363 set->tree = tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008364 set->format = format;
8365
8366 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008367 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008368 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008369 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008370 }
8371
Michal Vasko03ff5a72019-09-11 13:49:33 +02008372 return rc;
8373}
8374
8375#if 0
8376
8377/* full xml printing of set elements, not used currently */
8378
8379void
8380lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8381{
8382 uint32_t i;
8383 char *str_num;
8384 struct lyout out;
8385
8386 memset(&out, 0, sizeof out);
8387
8388 out.type = LYOUT_STREAM;
8389 out.method.f = f;
8390
8391 switch (set->type) {
8392 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008393 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008394 break;
8395 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008396 ly_print_(&out, "Boolean XPath set:\n");
8397 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008398 break;
8399 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008400 ly_print_(&out, "String XPath set:\n");
8401 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008402 break;
8403 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008404 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008405
8406 if (isnan(set->value.num)) {
8407 str_num = strdup("NaN");
8408 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8409 str_num = strdup("0");
8410 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8411 str_num = strdup("Infinity");
8412 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8413 str_num = strdup("-Infinity");
8414 } else if ((long long)set->value.num == set->value.num) {
8415 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8416 str_num = NULL;
8417 }
8418 } else {
8419 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8420 str_num = NULL;
8421 }
8422 }
8423 if (!str_num) {
8424 LOGMEM;
8425 return;
8426 }
Michal Vasko5233e962020-08-14 14:26:20 +02008427 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008428 free(str_num);
8429 break;
8430 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008431 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008432
8433 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008434 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008435 switch (set->node_type[i]) {
8436 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008437 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008438 break;
8439 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008440 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008441 break;
8442 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008443 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008444 break;
8445 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008446 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008447 break;
8448 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008449 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008450 break;
8451 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008452 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008453 break;
8454 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008455 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008456 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008457 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008458 break;
8459 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008460 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 +02008461 break;
8462 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008463 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 +02008464 break;
8465 }
8466 }
8467 break;
8468 }
8469}
8470
8471#endif
8472
8473LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008474lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008475{
8476 long double num;
8477 char *str;
8478 LY_ERR rc;
8479
8480 if (!set || (set->type == target)) {
8481 return LY_SUCCESS;
8482 }
8483
8484 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008485 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008486
8487 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008488 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008489 return LY_EINVAL;
8490 }
8491
8492 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008493 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008494 switch (set->type) {
8495 case LYXP_SET_NUMBER:
8496 if (isnan(set->val.num)) {
8497 set->val.str = strdup("NaN");
8498 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8499 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8500 set->val.str = strdup("0");
8501 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8502 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8503 set->val.str = strdup("Infinity");
8504 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8505 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8506 set->val.str = strdup("-Infinity");
8507 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8508 } else if ((long long)set->val.num == set->val.num) {
8509 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8510 LOGMEM_RET(set->ctx);
8511 }
8512 set->val.str = str;
8513 } else {
8514 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8515 LOGMEM_RET(set->ctx);
8516 }
8517 set->val.str = str;
8518 }
8519 break;
8520 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008521 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008522 set->val.str = strdup("true");
8523 } else {
8524 set->val.str = strdup("false");
8525 }
8526 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8527 break;
8528 case LYXP_SET_NODE_SET:
8529 assert(set->used);
8530
8531 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008532 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008533
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008534 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008535 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008536 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008537 set->val.str = str;
8538 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008539 default:
8540 LOGINT_RET(set->ctx);
8541 }
8542 set->type = LYXP_SET_STRING;
8543 }
8544
8545 /* to NUMBER */
8546 if (target == LYXP_SET_NUMBER) {
8547 switch (set->type) {
8548 case LYXP_SET_STRING:
8549 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008550 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008551 set->val.num = num;
8552 break;
8553 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008554 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008555 set->val.num = 1;
8556 } else {
8557 set->val.num = 0;
8558 }
8559 break;
8560 default:
8561 LOGINT_RET(set->ctx);
8562 }
8563 set->type = LYXP_SET_NUMBER;
8564 }
8565
8566 /* to BOOLEAN */
8567 if (target == LYXP_SET_BOOLEAN) {
8568 switch (set->type) {
8569 case LYXP_SET_NUMBER:
8570 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008571 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008572 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008573 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008574 }
8575 break;
8576 case LYXP_SET_STRING:
8577 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008578 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008579 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008580 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008581 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008582 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008583 }
8584 break;
8585 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008586 if (set->used) {
8587 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008588 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008589 } else {
8590 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008591 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008592 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008593 break;
8594 default:
8595 LOGINT_RET(set->ctx);
8596 }
8597 set->type = LYXP_SET_BOOLEAN;
8598 }
8599
Michal Vasko03ff5a72019-09-11 13:49:33 +02008600 return LY_SUCCESS;
8601}
8602
8603LY_ERR
Michal Vaskoc8a230d2020-08-14 12:17:10 +02008604lyxp_atomize(struct lyxp_expr *exp, LY_PREFIX_FORMAT format, const struct lys_module *local_mod,
Radek Krejci1deb5be2020-08-26 16:43:36 +02008605 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 +02008606{
8607 struct ly_ctx *ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008608 const struct lysc_node *real_ctx_scnode;
8609 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008610
Michal Vasko004d3152020-06-11 19:59:22 +02008611 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_scnode, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008612
8613 ctx = local_mod->ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008614 if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) {
8615 /* we always need some context node because it is used for resolving unqualified names */
8616 real_ctx_scnode = NULL;
8617 } else {
8618 real_ctx_scnode = ctx_scnode;
8619 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008620
8621 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008622 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008623 memset(set, 0, sizeof *set);
8624 set->type = LYXP_SET_SCNODE_SET;
Radek Krejciaa6b53f2020-08-27 15:19:03 +02008625 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, real_ctx_scnode, ctx_scnode_type, NULL));
Michal Vasko5c4e5892019-11-14 12:31:38 +01008626 set->val.scnodes[0].in_ctx = -2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008627 set->ctx = ctx;
8628 set->ctx_scnode = ctx_scnode;
Michal Vasko004d3152020-06-11 19:59:22 +02008629 set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options);
Michal Vasko6b26e742020-07-17 15:02:10 +02008630 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008631 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8632 set->context_op = set->context_op->parent) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008633 set->local_mod = local_mod;
8634 set->format = format;
8635
8636 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008637 return eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008638}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008639
8640API const char *
8641lyxp_get_expr(const struct lyxp_expr *path)
8642{
8643 if (!path) {
8644 return NULL;
8645 }
8646
8647 return path->expr;
8648}