blob: 0f5c74449dbb45fdab87e4fee8f592c89d55155d [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);
47static LY_ERR eval_expr_select(struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options);
Michal Vasko03ff5a72019-09-11 13:49:33 +020048
49/**
50 * @brief Print the type of an XPath \p set.
51 *
52 * @param[in] set Set to use.
53 * @return Set type string.
54 */
55static const char *
56print_set_type(struct lyxp_set *set)
57{
58 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020059 case LYXP_SET_NODE_SET:
60 return "node set";
61 case LYXP_SET_SCNODE_SET:
62 return "schema node set";
63 case LYXP_SET_BOOLEAN:
64 return "boolean";
65 case LYXP_SET_NUMBER:
66 return "number";
67 case LYXP_SET_STRING:
68 return "string";
69 }
70
71 return NULL;
72}
73
Michal Vasko24cddf82020-06-01 08:17:01 +020074const char *
75lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020076{
77 switch (tok) {
78 case LYXP_TOKEN_PAR1:
79 return "(";
80 case LYXP_TOKEN_PAR2:
81 return ")";
82 case LYXP_TOKEN_BRACK1:
83 return "[";
84 case LYXP_TOKEN_BRACK2:
85 return "]";
86 case LYXP_TOKEN_DOT:
87 return ".";
88 case LYXP_TOKEN_DDOT:
89 return "..";
90 case LYXP_TOKEN_AT:
91 return "@";
92 case LYXP_TOKEN_COMMA:
93 return ",";
94 case LYXP_TOKEN_NAMETEST:
95 return "NameTest";
96 case LYXP_TOKEN_NODETYPE:
97 return "NodeType";
98 case LYXP_TOKEN_FUNCNAME:
99 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200100 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200101 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200102 case LYXP_TOKEN_OPER_EQUAL:
103 return "Operator(Equal)";
104 case LYXP_TOKEN_OPER_NEQUAL:
105 return "Operator(Non-equal)";
106 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200107 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200108 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200109 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200110 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200111 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200112 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200113 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200114 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200115 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200116 case LYXP_TOKEN_LITERAL:
117 return "Literal";
118 case LYXP_TOKEN_NUMBER:
119 return "Number";
120 default:
121 LOGINT(NULL);
122 return "";
123 }
124}
125
126/**
127 * @brief Print the whole expression \p exp to debug output.
128 *
129 * @param[in] exp Expression to use.
130 */
131static void
132print_expr_struct_debug(struct lyxp_expr *exp)
133{
134 uint16_t i, j;
135 char tmp[128];
136
137 if (!exp || (ly_log_level < LY_LLDBG)) {
138 return;
139 }
140
141 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
142 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200143 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko03ff5a72019-09-11 13:49:33 +0200144 &exp->expr[exp->tok_pos[i]]);
145 if (exp->repeat[i]) {
146 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
147 for (j = 1; exp->repeat[i][j]; ++j) {
148 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
149 }
150 strcat(tmp, ")");
151 }
152 LOGDBG(LY_LDGXPATH, tmp);
153 }
154}
155
156#ifndef NDEBUG
157
158/**
159 * @brief Print XPath set content to debug output.
160 *
161 * @param[in] set Set to print.
162 */
163static void
164print_set_debug(struct lyxp_set *set)
165{
166 uint32_t i;
167 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200168 struct lyxp_set_node *item;
169 struct lyxp_set_scnode *sitem;
170
171 if (ly_log_level < LY_LLDBG) {
172 return;
173 }
174
175 switch (set->type) {
176 case LYXP_SET_NODE_SET:
177 LOGDBG(LY_LDGXPATH, "set NODE SET:");
178 for (i = 0; i < set->used; ++i) {
179 item = &set->val.nodes[i];
180
181 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100182 case LYXP_NODE_NONE:
183 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
184 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200185 case LYXP_NODE_ROOT:
186 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
187 break;
188 case LYXP_NODE_ROOT_CONFIG:
189 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
190 break;
191 case LYXP_NODE_ELEM:
192 if ((item->node->schema->nodetype == LYS_LIST)
193 && (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) {
194 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Michal Vaskob7be7a82020-08-20 09:09:04 +0200195 item->node->schema->name, LYD_CANON_VALUE(lyd_node_children(item->node, 0)));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200196 } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) {
197 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Michal Vaskob7be7a82020-08-20 09:09:04 +0200198 item->node->schema->name, LYD_CANON_VALUE(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200199 } else {
200 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
201 }
202 break;
203 case LYXP_NODE_TEXT:
204 if (item->node->schema->nodetype & LYS_ANYDATA) {
205 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
206 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
207 } else {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200208 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, LYD_CANON_VALUE(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200209 }
210 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100211 case LYXP_NODE_META:
212 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
213 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200214 break;
215 }
216 }
217 break;
218
219 case LYXP_SET_SCNODE_SET:
220 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
221 for (i = 0; i < set->used; ++i) {
222 sitem = &set->val.scnodes[i];
223
224 switch (sitem->type) {
225 case LYXP_NODE_ROOT:
226 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
227 break;
228 case LYXP_NODE_ROOT_CONFIG:
229 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
230 break;
231 case LYXP_NODE_ELEM:
232 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
233 break;
234 default:
235 LOGINT(NULL);
236 break;
237 }
238 }
239 break;
240
Michal Vasko03ff5a72019-09-11 13:49:33 +0200241 case LYXP_SET_BOOLEAN:
242 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200243 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200244 break;
245
246 case LYXP_SET_STRING:
247 LOGDBG(LY_LDGXPATH, "set STRING");
248 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
249 break;
250
251 case LYXP_SET_NUMBER:
252 LOGDBG(LY_LDGXPATH, "set NUMBER");
253
254 if (isnan(set->val.num)) {
255 str = strdup("NaN");
256 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
257 str = strdup("0");
258 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
259 str = strdup("Infinity");
260 } else if (isinf(set->val.num) && signbit(set->val.num)) {
261 str = strdup("-Infinity");
262 } else if ((long long)set->val.num == set->val.num) {
263 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
264 str = NULL;
265 }
266 } else {
267 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
268 str = NULL;
269 }
270 }
271 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
272
273 LOGDBG(LY_LDGXPATH, "\t%s", str);
274 free(str);
275 }
276}
277
278#endif
279
280/**
281 * @brief Realloc the string \p str.
282 *
283 * @param[in] ctx libyang context for logging.
284 * @param[in] needed How much free space is required.
285 * @param[in,out] str Pointer to the string to use.
286 * @param[in,out] used Used bytes in \p str.
287 * @param[in,out] size Allocated bytes in \p str.
288 * @return LY_ERR
289 */
290static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100291cast_string_realloc(const struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200292{
293 if (*size - *used < needed) {
294 do {
295 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
296 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
297 return LY_EINVAL;
298 }
299 *size += LYXP_STRING_CAST_SIZE_STEP;
300 } while (*size - *used < needed);
301 *str = ly_realloc(*str, *size * sizeof(char));
302 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
303 }
304
305 return LY_SUCCESS;
306}
307
308/**
309 * @brief Cast nodes recursively to one string @p str.
310 *
311 * @param[in] node Node to cast.
312 * @param[in] fake_cont Whether to put the data into a "fake" container.
313 * @param[in] root_type Type of the XPath root.
314 * @param[in] indent Current indent.
315 * @param[in,out] str Resulting string.
316 * @param[in,out] used Used bytes in @p str.
317 * @param[in,out] size Allocated bytes in @p str.
318 * @return LY_ERR
319 */
320static LY_ERR
321cast_string_recursive(const struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
Radek Krejci0f969882020-08-21 16:56:47 +0200322 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200323{
Radek Krejci7f769d72020-07-11 23:13:56 +0200324 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200325 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200326 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200327 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200328 struct lyd_node_any *any;
329 LY_ERR rc;
330
331 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
332 return LY_SUCCESS;
333 }
334
335 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200336 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200337 LY_CHECK_RET(rc);
338 strcpy(*str + (*used - 1), "\n");
339 ++(*used);
340
341 ++indent;
342 }
343
344 switch (node->schema->nodetype) {
345 case LYS_CONTAINER:
346 case LYS_LIST:
347 case LYS_RPC:
348 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200349 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200350 LY_CHECK_RET(rc);
351 strcpy(*str + (*used - 1), "\n");
352 ++(*used);
353
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200354 for (child = lyd_node_children(node, 0); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200355 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
356 LY_CHECK_RET(rc);
357 }
358
359 break;
360
361 case LYS_LEAF:
362 case LYS_LEAFLIST:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200363 value_str = LYD_CANON_VALUE(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200364
365 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200366 LY_CHECK_RET(cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200367 memset(*str + (*used - 1), ' ', indent * 2);
368 *used += indent * 2;
369
370 /* print value */
371 if (*used == 1) {
372 sprintf(*str + (*used - 1), "%s", value_str);
373 *used += strlen(value_str);
374 } else {
375 sprintf(*str + (*used - 1), "%s\n", value_str);
376 *used += strlen(value_str) + 1;
377 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200378
379 break;
380
381 case LYS_ANYXML:
382 case LYS_ANYDATA:
383 any = (struct lyd_node_any *)node;
384 if (!(void *)any->value.tree) {
385 /* no content */
386 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200387 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200388 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200389 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100390
Michal Vasko60ea6352020-06-29 13:39:39 +0200391 if (any->value_type == LYD_ANYDATA_LYB) {
392 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200393 if (lyd_parse_data_mem((struct ly_ctx *)LYD_CTX(node), any->value.mem, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200394 /* successfully parsed */
395 free(any->value.mem);
396 any->value.tree = tree;
397 any->value_type = LYD_ANYDATA_DATATREE;
398 }
Radek Krejci7931b192020-06-25 17:05:03 +0200399 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200400 }
401
Michal Vasko03ff5a72019-09-11 13:49:33 +0200402 switch (any->value_type) {
403 case LYD_ANYDATA_STRING:
404 case LYD_ANYDATA_XML:
405 case LYD_ANYDATA_JSON:
406 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200407 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200408 break;
409 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200410 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200411 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200412 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100413 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200414 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200415 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200416 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200417 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200418 }
419 }
420
421 line = strtok_r(buf, "\n", &ptr);
422 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200423 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200424 if (rc != LY_SUCCESS) {
425 free(buf);
426 return rc;
427 }
428 memset(*str + (*used - 1), ' ', indent * 2);
429 *used += indent * 2;
430
431 strcpy(*str + (*used - 1), line);
432 *used += strlen(line);
433
434 strcpy(*str + (*used - 1), "\n");
435 *used += 1;
436 } while ((line = strtok_r(NULL, "\n", &ptr)));
437
438 free(buf);
439 break;
440
441 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200442 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200443 }
444
445 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200446 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200447 LY_CHECK_RET(rc);
448 strcpy(*str + (*used - 1), "\n");
449 ++(*used);
450
451 --indent;
452 }
453
454 return LY_SUCCESS;
455}
456
457/**
458 * @brief Cast an element into a string.
459 *
460 * @param[in] node Node to cast.
461 * @param[in] fake_cont Whether to put the data into a "fake" container.
462 * @param[in] root_type Type of the XPath root.
463 * @param[out] str Element cast to dynamically-allocated string.
464 * @return LY_ERR
465 */
466static LY_ERR
467cast_string_elem(struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, char **str)
468{
469 uint16_t used, size;
470 LY_ERR rc;
471
472 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200473 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200474 (*str)[0] = '\0';
475 used = 1;
476 size = LYXP_STRING_CAST_SIZE_START;
477
478 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
479 if (rc != LY_SUCCESS) {
480 free(*str);
481 return rc;
482 }
483
484 if (size > used) {
485 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200486 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200487 }
488 return LY_SUCCESS;
489}
490
491/**
492 * @brief Cast a LYXP_SET_NODE_SET set into a string.
493 * Context position aware.
494 *
495 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200496 * @param[out] str Cast dynamically-allocated string.
497 * @return LY_ERR
498 */
499static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100500cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200501{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200502 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100503 case LYXP_NODE_NONE:
504 /* invalid */
505 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200506 case LYXP_NODE_ROOT:
507 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100508 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200509 case LYXP_NODE_ELEM:
510 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100511 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100512 case LYXP_NODE_META:
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200513 *str = strdup(set->val.meta[0].meta->value.canonical);
514 if (!*str) {
515 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200516 }
517 return LY_SUCCESS;
518 }
519
520 LOGINT_RET(set->ctx);
521}
522
523/**
524 * @brief Cast a string into an XPath number.
525 *
526 * @param[in] str String to use.
527 * @return Cast number.
528 */
529static long double
530cast_string_to_number(const char *str)
531{
532 long double num;
533 char *ptr;
534
535 errno = 0;
536 num = strtold(str, &ptr);
537 if (errno || *ptr) {
538 num = NAN;
539 }
540 return num;
541}
542
543/**
544 * @brief Callback for checking value equality.
545 *
546 * @param[in] val1_p First value.
547 * @param[in] val2_p Second value.
548 * @param[in] mod Whether hash table is being modified.
549 * @param[in] cb_data Callback data.
550 * @return 0 if not equal, non-zero if equal.
551 */
552static int
553set_values_equal_cb(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
554{
555 struct lyxp_set_hash_node *val1, *val2;
556
557 val1 = (struct lyxp_set_hash_node *)val1_p;
558 val2 = (struct lyxp_set_hash_node *)val2_p;
559
560 if ((val1->node == val2->node) && (val1->type == val2->type)) {
561 return 1;
562 }
563
564 return 0;
565}
566
567/**
568 * @brief Insert node and its hash into set.
569 *
570 * @param[in] set et to insert to.
571 * @param[in] node Node with hash.
572 * @param[in] type Node type.
573 */
574static void
575set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
576{
577 int r;
578 uint32_t i, hash;
579 struct lyxp_set_hash_node hnode;
580
581 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
582 /* create hash table and add all the nodes */
583 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
584 for (i = 0; i < set->used; ++i) {
585 hnode.node = set->val.nodes[i].node;
586 hnode.type = set->val.nodes[i].type;
587
588 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
589 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
590 hash = dict_hash_multi(hash, NULL, 0);
591
592 r = lyht_insert(set->ht, &hnode, hash, NULL);
593 assert(!r);
594 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200595
Michal Vasko9d6befd2019-12-11 14:56:56 +0100596 if (hnode.node == node) {
597 /* it was just added, do not add it twice */
598 node = NULL;
599 }
600 }
601 }
602
603 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200604 /* add the new node into hash table */
605 hnode.node = node;
606 hnode.type = type;
607
608 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
609 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
610 hash = dict_hash_multi(hash, NULL, 0);
611
612 r = lyht_insert(set->ht, &hnode, hash, NULL);
613 assert(!r);
614 (void)r;
615 }
616}
617
618/**
619 * @brief Remove node and its hash from set.
620 *
621 * @param[in] set Set to remove from.
622 * @param[in] node Node to remove.
623 * @param[in] type Node type.
624 */
625static void
626set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
627{
628 int r;
629 struct lyxp_set_hash_node hnode;
630 uint32_t hash;
631
632 if (set->ht) {
633 hnode.node = node;
634 hnode.type = type;
635
636 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
637 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
638 hash = dict_hash_multi(hash, NULL, 0);
639
640 r = lyht_remove(set->ht, &hnode, hash);
641 assert(!r);
642 (void)r;
643
644 if (!set->ht->used) {
645 lyht_free(set->ht);
646 set->ht = NULL;
647 }
648 }
649}
650
651/**
652 * @brief Check whether node is in set based on its hash.
653 *
654 * @param[in] set Set to search in.
655 * @param[in] node Node to search for.
656 * @param[in] type Node type.
657 * @param[in] skip_idx Index in @p set to skip.
658 * @return LY_ERR
659 */
660static LY_ERR
661set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
662{
663 struct lyxp_set_hash_node hnode, *match_p;
664 uint32_t hash;
665
666 hnode.node = node;
667 hnode.type = type;
668
669 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
670 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
671 hash = dict_hash_multi(hash, NULL, 0);
672
673 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
674 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
675 /* we found it on the index that should be skipped, find another */
676 hnode = *match_p;
677 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
678 /* none other found */
679 return LY_SUCCESS;
680 }
681 }
682
683 return LY_EEXIST;
684 }
685
686 /* not found */
687 return LY_SUCCESS;
688}
689
Michal Vaskod3678892020-05-21 10:06:58 +0200690void
691lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200692{
693 if (!set) {
694 return;
695 }
696
697 if (set->type == LYXP_SET_NODE_SET) {
698 free(set->val.nodes);
699 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200700 } else if (set->type == LYXP_SET_SCNODE_SET) {
701 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200702 lyht_free(set->ht);
703 } else {
704 if (set->type == LYXP_SET_STRING) {
705 free(set->val.str);
706 }
707 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200708 }
Michal Vaskod3678892020-05-21 10:06:58 +0200709
710 set->val.nodes = NULL;
711 set->used = 0;
712 set->size = 0;
713 set->ht = NULL;
714 set->ctx_pos = 0;
715 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200716}
717
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100718/**
719 * @brief Free dynamically-allocated set.
720 *
721 * @param[in] set Set to free.
722 */
723static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200724lyxp_set_free(struct lyxp_set *set)
725{
726 if (!set) {
727 return;
728 }
729
Michal Vaskod3678892020-05-21 10:06:58 +0200730 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200731 free(set);
732}
733
734/**
735 * @brief Initialize set context.
736 *
737 * @param[in] new Set to initialize.
738 * @param[in] set Arbitrary initialized set.
739 */
740static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200741set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200742{
743 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200744 if (set) {
745 new->ctx = set->ctx;
746 new->ctx_node = set->ctx_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100747 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200748 new->context_op = set->context_op;
Michal Vasko02a77382019-09-12 11:47:35 +0200749 new->local_mod = set->local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +0100750 new->tree = set->tree;
Michal Vasko02a77382019-09-12 11:47:35 +0200751 new->format = set->format;
752 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200753}
754
755/**
756 * @brief Create a deep copy of a set.
757 *
758 * @param[in] set Set to copy.
759 * @return Copy of @p set.
760 */
761static struct lyxp_set *
762set_copy(struct lyxp_set *set)
763{
764 struct lyxp_set *ret;
765 uint16_t i;
Michal Vaskoba716542019-12-16 10:01:58 +0100766 int idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200767
768 if (!set) {
769 return NULL;
770 }
771
772 ret = malloc(sizeof *ret);
773 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
774 set_init(ret, set);
775
776 if (set->type == LYXP_SET_SCNODE_SET) {
777 ret->type = set->type;
778
779 for (i = 0; i < set->used; ++i) {
Michal Vaskoba716542019-12-16 10:01:58 +0100780 if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) {
781 idx = lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type);
Michal Vasko3f27c522020-01-06 08:37:49 +0100782 /* coverity seems to think scnodes can be NULL */
783 if ((idx == -1) || !ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200784 lyxp_set_free(ret);
785 return NULL;
786 }
Michal Vaskoba716542019-12-16 10:01:58 +0100787 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200788 }
789 }
790 } else if (set->type == LYXP_SET_NODE_SET) {
791 ret->type = set->type;
792 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
793 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
794 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
795
796 ret->used = ret->size = set->used;
797 ret->ctx_pos = set->ctx_pos;
798 ret->ctx_size = set->ctx_size;
799 ret->ht = lyht_dup(set->ht);
800 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200801 memcpy(ret, set, sizeof *ret);
802 if (set->type == LYXP_SET_STRING) {
803 ret->val.str = strdup(set->val.str);
804 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
805 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200806 }
807
808 return ret;
809}
810
811/**
812 * @brief Fill XPath set with a string. Any current data are disposed of.
813 *
814 * @param[in] set Set to fill.
815 * @param[in] string String to fill into \p set.
816 * @param[in] str_len Length of \p string. 0 is a valid value!
817 */
818static void
819set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
820{
Michal Vaskod3678892020-05-21 10:06:58 +0200821 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200822
823 set->type = LYXP_SET_STRING;
824 if ((str_len == 0) && (string[0] != '\0')) {
825 string = "";
826 }
827 set->val.str = strndup(string, str_len);
828}
829
830/**
831 * @brief Fill XPath set with a number. Any current data are disposed of.
832 *
833 * @param[in] set Set to fill.
834 * @param[in] number Number to fill into \p set.
835 */
836static void
837set_fill_number(struct lyxp_set *set, long double number)
838{
Michal Vaskod3678892020-05-21 10:06:58 +0200839 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200840
841 set->type = LYXP_SET_NUMBER;
842 set->val.num = number;
843}
844
845/**
846 * @brief Fill XPath set with a boolean. Any current data are disposed of.
847 *
848 * @param[in] set Set to fill.
849 * @param[in] boolean Boolean to fill into \p set.
850 */
851static void
852set_fill_boolean(struct lyxp_set *set, int boolean)
853{
Michal Vaskod3678892020-05-21 10:06:58 +0200854 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200855
856 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200857 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200858}
859
860/**
861 * @brief Fill XPath set with the value from another set (deep assign).
862 * Any current data are disposed of.
863 *
864 * @param[in] trg Set to fill.
865 * @param[in] src Source set to copy into \p trg.
866 */
867static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200868set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200869{
870 if (!trg || !src) {
871 return;
872 }
873
874 if (trg->type == LYXP_SET_NODE_SET) {
875 free(trg->val.nodes);
876 } else if (trg->type == LYXP_SET_STRING) {
877 free(trg->val.str);
878 }
879 set_init(trg, src);
880
881 if (src->type == LYXP_SET_SCNODE_SET) {
882 trg->type = LYXP_SET_SCNODE_SET;
883 trg->used = src->used;
884 trg->size = src->used;
885
886 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
887 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
888 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
889 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200890 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200891 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200892 set_fill_number(trg, src->val.num);
893 } else if (src->type == LYXP_SET_STRING) {
894 set_fill_string(trg, src->val.str, strlen(src->val.str));
895 } else {
896 if (trg->type == LYXP_SET_NODE_SET) {
897 free(trg->val.nodes);
898 } else if (trg->type == LYXP_SET_STRING) {
899 free(trg->val.str);
900 }
901
Michal Vaskod3678892020-05-21 10:06:58 +0200902 assert(src->type == LYXP_SET_NODE_SET);
903
904 trg->type = LYXP_SET_NODE_SET;
905 trg->used = src->used;
906 trg->size = src->used;
907 trg->ctx_pos = src->ctx_pos;
908 trg->ctx_size = src->ctx_size;
909
910 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
911 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
912 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
913 if (src->ht) {
914 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200915 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200916 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200917 }
918 }
919}
920
921/**
922 * @brief Clear context of all schema nodes.
923 *
924 * @param[in] set Set to clear.
925 */
926static void
927set_scnode_clear_ctx(struct lyxp_set *set)
928{
929 uint32_t i;
930
931 for (i = 0; i < set->used; ++i) {
932 if (set->val.scnodes[i].in_ctx == 1) {
933 set->val.scnodes[i].in_ctx = 0;
Michal Vasko5c4e5892019-11-14 12:31:38 +0100934 } else if (set->val.scnodes[i].in_ctx == -2) {
935 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200936 }
937 }
938}
939
940/**
941 * @brief Remove a node from a set. Removing last node changes
942 * set into LYXP_SET_EMPTY. Context position aware.
943 *
944 * @param[in] set Set to use.
945 * @param[in] idx Index from @p set of the node to be removed.
946 */
947static void
948set_remove_node(struct lyxp_set *set, uint32_t idx)
949{
950 assert(set && (set->type == LYXP_SET_NODE_SET));
951 assert(idx < set->used);
952
953 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
954
955 --set->used;
956 if (set->used) {
957 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
958 (set->used - idx) * sizeof *set->val.nodes);
959 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200960 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200961 }
962}
963
964/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100965 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200966 *
967 * @param[in] set Set to use.
968 * @param[in] idx Index from @p set of the node to be removed.
969 */
970static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100971set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200972{
973 assert(set && (set->type == LYXP_SET_NODE_SET));
974 assert(idx < set->used);
975
Michal Vasko2caefc12019-11-14 16:07:56 +0100976 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
977 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
978 }
979 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +0200980}
981
982/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100983 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +0200984 * set into LYXP_SET_EMPTY. Context position aware.
985 *
986 * @param[in] set Set to consolidate.
987 */
988static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100989set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +0200990{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +0200991 uint16_t i, orig_used, end = 0;
Michal Vasko57eab132019-09-24 11:46:26 +0200992 int32_t start;
993
Michal Vaskod3678892020-05-21 10:06:58 +0200994 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +0200995
996 orig_used = set->used;
997 set->used = 0;
998 for (i = 0; i < orig_used;) {
999 start = -1;
1000 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001001 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001002 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001003 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001004 end = i;
1005 ++i;
1006 break;
1007 }
1008
1009 ++i;
1010 if (i == orig_used) {
1011 end = i;
1012 }
1013 } while (i < orig_used);
1014
1015 if (start > -1) {
1016 /* move the whole chunk of valid nodes together */
1017 if (set->used != (unsigned)start) {
1018 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1019 }
1020 set->used += end - start;
1021 }
1022 }
Michal Vasko57eab132019-09-24 11:46:26 +02001023}
1024
1025/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001026 * @brief Check for duplicates in a node set.
1027 *
1028 * @param[in] set Set to check.
1029 * @param[in] node Node to look for in @p set.
1030 * @param[in] node_type Type of @p node.
1031 * @param[in] skip_idx Index from @p set to skip.
1032 * @return LY_ERR
1033 */
1034static LY_ERR
1035set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1036{
1037 uint32_t i;
1038
Michal Vasko2caefc12019-11-14 16:07:56 +01001039 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001040 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1041 }
1042
1043 for (i = 0; i < set->used; ++i) {
1044 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1045 continue;
1046 }
1047
1048 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1049 return LY_EEXIST;
1050 }
1051 }
1052
1053 return LY_SUCCESS;
1054}
1055
Michal Vaskoecd62de2019-11-13 12:35:11 +01001056int
1057lyxp_set_scnode_dup_node_check(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001058{
1059 uint32_t i;
1060
1061 for (i = 0; i < set->used; ++i) {
1062 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1063 continue;
1064 }
1065
1066 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
1067 return i;
1068 }
1069 }
1070
1071 return -1;
1072}
1073
Michal Vaskoecd62de2019-11-13 12:35:11 +01001074void
1075lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001076{
1077 uint32_t orig_used, i, j;
1078
Michal Vaskod3678892020-05-21 10:06:58 +02001079 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001080
Michal Vaskod3678892020-05-21 10:06:58 +02001081 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001082 return;
1083 }
1084
Michal Vaskod3678892020-05-21 10:06:58 +02001085 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001086 memcpy(set1, set2, sizeof *set1);
1087 return;
1088 }
1089
1090 if (set1->used + set2->used > set1->size) {
1091 set1->size = set1->used + set2->used;
1092 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1093 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1094 }
1095
1096 orig_used = set1->used;
1097
1098 for (i = 0; i < set2->used; ++i) {
1099 for (j = 0; j < orig_used; ++j) {
1100 /* detect duplicities */
1101 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1102 break;
1103 }
1104 }
1105
1106 if (j == orig_used) {
1107 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1108 ++set1->used;
1109 }
1110 }
1111
Michal Vaskod3678892020-05-21 10:06:58 +02001112 lyxp_set_free_content(set2);
1113 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001114}
1115
1116/**
1117 * @brief Insert a node into a set. Context position aware.
1118 *
1119 * @param[in] set Set to use.
1120 * @param[in] node Node to insert to @p set.
1121 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1122 * @param[in] node_type Node type of @p node.
1123 * @param[in] idx Index in @p set to insert into.
1124 */
1125static void
1126set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1127{
Michal Vaskod3678892020-05-21 10:06:58 +02001128 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001129
Michal Vaskod3678892020-05-21 10:06:58 +02001130 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001131 /* first item */
1132 if (idx) {
1133 /* no real harm done, but it is a bug */
1134 LOGINT(set->ctx);
1135 idx = 0;
1136 }
1137 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1138 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1139 set->type = LYXP_SET_NODE_SET;
1140 set->used = 0;
1141 set->size = LYXP_SET_SIZE_START;
1142 set->ctx_pos = 1;
1143 set->ctx_size = 1;
1144 set->ht = NULL;
1145 } else {
1146 /* not an empty set */
1147 if (set->used == set->size) {
1148
1149 /* set is full */
1150 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1151 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1152 set->size += LYXP_SET_SIZE_STEP;
1153 }
1154
1155 if (idx > set->used) {
1156 LOGINT(set->ctx);
1157 idx = set->used;
1158 }
1159
1160 /* make space for the new node */
1161 if (idx < set->used) {
1162 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1163 }
1164 }
1165
1166 /* finally assign the value */
1167 set->val.nodes[idx].node = (struct lyd_node *)node;
1168 set->val.nodes[idx].type = node_type;
1169 set->val.nodes[idx].pos = pos;
1170 ++set->used;
1171
Michal Vasko2caefc12019-11-14 16:07:56 +01001172 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1173 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1174 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001175}
1176
Michal Vaskoecd62de2019-11-13 12:35:11 +01001177int
1178lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001179{
1180 int ret;
1181
1182 assert(set->type == LYXP_SET_SCNODE_SET);
1183
Michal Vaskoecd62de2019-11-13 12:35:11 +01001184 ret = lyxp_set_scnode_dup_node_check(set, node, node_type, -1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001185 if (ret > -1) {
1186 set->val.scnodes[ret].in_ctx = 1;
1187 } else {
1188 if (set->used == set->size) {
1189 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes);
1190 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), -1);
1191 set->size += LYXP_SET_SIZE_STEP;
1192 }
1193
1194 ret = set->used;
1195 set->val.scnodes[ret].scnode = (struct lysc_node *)node;
1196 set->val.scnodes[ret].type = node_type;
1197 set->val.scnodes[ret].in_ctx = 1;
1198 ++set->used;
1199 }
1200
1201 return ret;
1202}
1203
1204/**
1205 * @brief Replace a node in a set with another. Context position aware.
1206 *
1207 * @param[in] set Set to use.
1208 * @param[in] node Node to insert to @p set.
1209 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1210 * @param[in] node_type Node type of @p node.
1211 * @param[in] idx Index in @p set of the node to replace.
1212 */
1213static void
1214set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1215{
1216 assert(set && (idx < set->used));
1217
Michal Vasko2caefc12019-11-14 16:07:56 +01001218 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1219 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1220 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001221 set->val.nodes[idx].node = (struct lyd_node *)node;
1222 set->val.nodes[idx].type = node_type;
1223 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001224 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1225 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1226 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001227}
1228
1229/**
1230 * @brief Set all nodes with ctx 1 to a new unique context value.
1231 *
1232 * @param[in] set Set to modify.
1233 * @return New context value.
1234 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001235static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001236set_scnode_new_in_ctx(struct lyxp_set *set)
1237{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001238 uint32_t i;
1239 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001240
1241 assert(set->type == LYXP_SET_SCNODE_SET);
1242
1243 ret_ctx = 3;
1244retry:
1245 for (i = 0; i < set->used; ++i) {
1246 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1247 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1248 goto retry;
1249 }
1250 }
1251 for (i = 0; i < set->used; ++i) {
1252 if (set->val.scnodes[i].in_ctx == 1) {
1253 set->val.scnodes[i].in_ctx = ret_ctx;
1254 }
1255 }
1256
1257 return ret_ctx;
1258}
1259
1260/**
1261 * @brief Get unique @p node position in the data.
1262 *
1263 * @param[in] node Node to find.
1264 * @param[in] node_type Node type of @p node.
1265 * @param[in] root Root node.
1266 * @param[in] root_type Type of the XPath @p root node.
1267 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1268 * be used to increase efficiency and start the DFS from this node.
1269 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1270 * @return Node position.
1271 */
1272static uint32_t
1273get_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 +02001274 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001275{
Michal Vasko56daf732020-08-10 10:57:18 +02001276 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001277 uint32_t pos = 1;
1278
1279 assert(prev && prev_pos && !root->prev->next);
1280
1281 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1282 return 0;
1283 }
1284
1285 if (*prev) {
1286 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001287 pos = *prev_pos;
Radek Krejci1e008d22020-08-17 11:37:37 +02001288 for (top_sibling = *prev; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001289 goto dfs_search;
1290 }
1291
1292 for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) {
Michal Vasko56daf732020-08-10 10:57:18 +02001293 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001294dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001295 if (*prev && !elem) {
1296 /* resume previous DFS */
1297 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1298 LYD_TREE_DFS_continue = 0;
1299 }
1300
Michal Vasko03ff5a72019-09-11 13:49:33 +02001301 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001302 /* skip */
1303 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001304 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001305 if (elem == node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001306 break;
1307 }
Michal Vasko56daf732020-08-10 10:57:18 +02001308 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001309 }
Michal Vasko56daf732020-08-10 10:57:18 +02001310
1311 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001312 }
1313
1314 /* node found */
1315 if (elem) {
1316 break;
1317 }
1318 }
1319
1320 if (!elem) {
1321 if (!(*prev)) {
1322 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001323 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001324 return 0;
1325 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001326 /* start the search again from the beginning */
1327 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001328
Michal Vasko56daf732020-08-10 10:57:18 +02001329 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001330 pos = 1;
1331 goto dfs_search;
1332 }
1333 }
1334
1335 /* remember the last found node for next time */
1336 *prev = node;
1337 *prev_pos = pos;
1338
1339 return pos;
1340}
1341
1342/**
1343 * @brief Assign (fill) missing node positions.
1344 *
1345 * @param[in] set Set to fill positions in.
1346 * @param[in] root Context root node.
1347 * @param[in] root_type Context root type.
1348 * @return LY_ERR
1349 */
1350static LY_ERR
1351set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1352{
1353 const struct lyd_node *prev = NULL, *tmp_node;
1354 uint32_t i, tmp_pos = 0;
1355
1356 for (i = 0; i < set->used; ++i) {
1357 if (!set->val.nodes[i].pos) {
1358 tmp_node = NULL;
1359 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001360 case LYXP_NODE_META:
1361 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001362 if (!tmp_node) {
1363 LOGINT_RET(root->schema->module->ctx);
1364 }
Radek Krejci0f969882020-08-21 16:56:47 +02001365 /* fallthrough */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001366 case LYXP_NODE_ELEM:
1367 case LYXP_NODE_TEXT:
1368 if (!tmp_node) {
1369 tmp_node = set->val.nodes[i].node;
1370 }
1371 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1372 break;
1373 default:
1374 /* all roots have position 0 */
1375 break;
1376 }
1377 }
1378 }
1379
1380 return LY_SUCCESS;
1381}
1382
1383/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001384 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001385 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001386 * @param[in] meta Metadata to use.
1387 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001388 */
1389static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001390get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001391{
1392 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001393 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001394
Michal Vasko9f96a052020-03-10 09:41:45 +01001395 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001396 ++pos;
1397 }
1398
Michal Vasko9f96a052020-03-10 09:41:45 +01001399 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001400 return pos;
1401}
1402
1403/**
1404 * @brief Compare 2 nodes in respect to XPath document order.
1405 *
1406 * @param[in] item1 1st node.
1407 * @param[in] item2 2nd node.
1408 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1409 */
1410static int
1411set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1412{
Michal Vasko9f96a052020-03-10 09:41:45 +01001413 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001414
1415 if (item1->pos < item2->pos) {
1416 return -1;
1417 }
1418
1419 if (item1->pos > item2->pos) {
1420 return 1;
1421 }
1422
1423 /* node positions are equal, the fun case */
1424
1425 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1426 /* special case since text nodes are actually saved as their parents */
1427 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1428 if (item1->type == LYXP_NODE_ELEM) {
1429 assert(item2->type == LYXP_NODE_TEXT);
1430 return -1;
1431 } else {
1432 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1433 return 1;
1434 }
1435 }
1436
Michal Vasko9f96a052020-03-10 09:41:45 +01001437 /* we need meta positions now */
1438 if (item1->type == LYXP_NODE_META) {
1439 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001440 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001441 if (item2->type == LYXP_NODE_META) {
1442 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001443 }
1444
Michal Vasko9f96a052020-03-10 09:41:45 +01001445 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001446 /* check for duplicates */
1447 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001448 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001449 return 0;
1450 }
1451
Michal Vasko9f96a052020-03-10 09:41:45 +01001452 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001453 /* elem is always first, 2nd node is after it */
1454 if (item1->type == LYXP_NODE_ELEM) {
1455 assert(item2->type != LYXP_NODE_ELEM);
1456 return -1;
1457 }
1458
Michal Vasko9f96a052020-03-10 09:41:45 +01001459 /* 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 +02001460 /* 2nd is before 1st */
1461 if (((item1->type == LYXP_NODE_TEXT)
Michal Vasko9f96a052020-03-10 09:41:45 +01001462 && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META)))
1463 || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM))
1464 || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META))
1465 && (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001466 return 1;
1467 }
1468
Michal Vasko9f96a052020-03-10 09:41:45 +01001469 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001470 /* 2nd is after 1st */
1471 return -1;
1472}
1473
1474/**
1475 * @brief Set cast for comparisons.
1476 *
1477 * @param[in] trg Target set to cast source into.
1478 * @param[in] src Source set.
1479 * @param[in] type Target set type.
1480 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001481 * @return LY_ERR
1482 */
1483static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001484set_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 +02001485{
1486 assert(src->type == LYXP_SET_NODE_SET);
1487
1488 set_init(trg, src);
1489
1490 /* insert node into target set */
1491 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1492
1493 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001494 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001495}
1496
Michal Vasko4c7763f2020-07-27 17:40:37 +02001497/**
1498 * @brief Set content canonization for comparisons.
1499 *
1500 * @param[in] trg Target set to put the canononized source into.
1501 * @param[in] src Source set.
1502 * @param[in] xp_node Source XPath node/meta to use for canonization.
1503 * @return LY_ERR
1504 */
1505static LY_ERR
1506set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1507{
1508 struct lysc_type *type = NULL;
1509 struct lyd_value val;
1510 struct ly_err_item *err = NULL;
1511 char *str, *ptr;
1512 int dynamic;
1513 LY_ERR rc;
1514
1515 /* is there anything to canonize even? */
1516 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1517 /* do we have a type to use for canonization? */
1518 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1519 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1520 } else if (xp_node->type == LYXP_NODE_META) {
1521 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1522 }
1523 }
1524 if (!type) {
1525 goto fill;
1526 }
1527
1528 if (src->type == LYXP_SET_NUMBER) {
1529 /* canonize number */
1530 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1531 LOGMEM(src->ctx);
1532 return LY_EMEM;
1533 }
1534 } else {
1535 /* canonize string */
1536 str = strdup(src->val.str);
1537 }
1538
1539 /* ignore errors, the value may not satisfy schema constraints */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001540 rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_DYNAMIC,
1541 LY_PREF_JSON, NULL, NULL, NULL, &val, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001542 ly_err_free(err);
1543 if (rc) {
1544 /* invalid value */
1545 free(str);
1546 goto fill;
1547 }
1548
1549 /* storing successful, now print the canonical value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001550 str = (char *)type->plugin->print(&val, LY_PREF_JSON, NULL, &dynamic);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001551
1552 /* use the canonized value */
1553 set_init(trg, src);
1554 trg->type = src->type;
1555 if (src->type == LYXP_SET_NUMBER) {
1556 trg->val.num = strtold(str, &ptr);
1557 if (dynamic) {
1558 free(str);
1559 }
1560 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1561 } else {
1562 trg->val.str = (dynamic ? str : strdup(str));
1563 }
1564 type->plugin->free(src->ctx, &val);
1565 return LY_SUCCESS;
1566
1567fill:
1568 /* no canonization needed/possible */
1569 set_fill_set(trg, src);
1570 return LY_SUCCESS;
1571}
1572
Michal Vasko03ff5a72019-09-11 13:49:33 +02001573#ifndef NDEBUG
1574
1575/**
1576 * @brief Bubble sort @p set into XPath document order.
1577 * Context position aware. Unused in the 'Release' build target.
1578 *
1579 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001580 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1581 */
1582static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001583set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001584{
1585 uint32_t i, j;
1586 int ret = 0, cmp, inverted, change;
1587 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001588 struct lyxp_set_node item;
1589 struct lyxp_set_hash_node hnode;
1590 uint64_t hash;
1591
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001592 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001593 return 0;
1594 }
1595
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001596 /* find first top-level node to be used as anchor for positions */
Radek Krejci1e008d22020-08-17 11:37:37 +02001597 for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent) {}
1598 for (; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001599
1600 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001601 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001602 return -1;
1603 }
1604
1605 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1606 print_set_debug(set);
1607
1608 for (i = 0; i < set->used; ++i) {
1609 inverted = 0;
1610 change = 0;
1611
1612 for (j = 1; j < set->used - i; ++j) {
1613 /* compare node positions */
1614 if (inverted) {
1615 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1616 } else {
1617 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1618 }
1619
1620 /* swap if needed */
1621 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1622 change = 1;
1623
1624 item = set->val.nodes[j - 1];
1625 set->val.nodes[j - 1] = set->val.nodes[j];
1626 set->val.nodes[j] = item;
1627 } else {
1628 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1629 inverted = !inverted;
1630 }
1631 }
1632
1633 ++ret;
1634
1635 if (!change) {
1636 break;
1637 }
1638 }
1639
1640 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1641 print_set_debug(set);
1642
1643 /* check node hashes */
1644 if (set->used >= LYD_HT_MIN_ITEMS) {
1645 assert(set->ht);
1646 for (i = 0; i < set->used; ++i) {
1647 hnode.node = set->val.nodes[i].node;
1648 hnode.type = set->val.nodes[i].type;
1649
1650 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1651 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1652 hash = dict_hash_multi(hash, NULL, 0);
1653
1654 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1655 }
1656 }
1657
1658 return ret - 1;
1659}
1660
1661/**
1662 * @brief Remove duplicate entries in a sorted node set.
1663 *
1664 * @param[in] set Sorted set to check.
1665 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1666 */
1667static LY_ERR
1668set_sorted_dup_node_clean(struct lyxp_set *set)
1669{
1670 uint32_t i = 0;
1671 LY_ERR ret = LY_SUCCESS;
1672
1673 if (set->used > 1) {
1674 while (i < set->used - 1) {
1675 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node)
1676 && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001677 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001678 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001679 }
Michal Vasko57eab132019-09-24 11:46:26 +02001680 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001681 }
1682 }
1683
Michal Vasko2caefc12019-11-14 16:07:56 +01001684 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001685 return ret;
1686}
1687
1688#endif
1689
1690/**
1691 * @brief Merge 2 sorted sets into one.
1692 *
1693 * @param[in,out] trg Set to merge into. Duplicates are removed.
1694 * @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 +02001695 * @return LY_ERR
1696 */
1697static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001698set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001699{
1700 uint32_t i, j, k, count, dup_count;
1701 int cmp;
1702 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001703
Michal Vaskod3678892020-05-21 10:06:58 +02001704 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001705 return LY_EINVAL;
1706 }
1707
Michal Vaskod3678892020-05-21 10:06:58 +02001708 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001709 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001710 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001711 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001712 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001713 return LY_SUCCESS;
1714 }
1715
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001716 /* find first top-level node to be used as anchor for positions */
Radek Krejci1e008d22020-08-17 11:37:37 +02001717 for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent) {}
1718 for (; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001719
1720 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001721 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001722 return LY_EINT;
1723 }
1724
1725#ifndef NDEBUG
1726 LOGDBG(LY_LDGXPATH, "MERGE target");
1727 print_set_debug(trg);
1728 LOGDBG(LY_LDGXPATH, "MERGE source");
1729 print_set_debug(src);
1730#endif
1731
1732 /* make memory for the merge (duplicates are not detected yet, so space
1733 * will likely be wasted on them, too bad) */
1734 if (trg->size - trg->used < src->used) {
1735 trg->size = trg->used + src->used;
1736
1737 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1738 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1739 }
1740
1741 i = 0;
1742 j = 0;
1743 count = 0;
1744 dup_count = 0;
1745 do {
1746 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1747 if (!cmp) {
1748 if (!count) {
1749 /* duplicate, just skip it */
1750 ++i;
1751 ++j;
1752 } else {
1753 /* we are copying something already, so let's copy the duplicate too,
1754 * we are hoping that afterwards there are some more nodes to
1755 * copy and this way we can copy them all together */
1756 ++count;
1757 ++dup_count;
1758 ++i;
1759 ++j;
1760 }
1761 } else if (cmp < 0) {
1762 /* inserting src node into trg, just remember it for now */
1763 ++count;
1764 ++i;
1765
1766 /* insert the hash now */
1767 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1768 } else if (count) {
1769copy_nodes:
1770 /* time to actually copy the nodes, we have found the largest block of nodes */
1771 memmove(&trg->val.nodes[j + (count - dup_count)],
1772 &trg->val.nodes[j],
1773 (trg->used - j) * sizeof *trg->val.nodes);
1774 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1775
1776 trg->used += count - dup_count;
1777 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1778 j += count - dup_count;
1779
1780 count = 0;
1781 dup_count = 0;
1782 } else {
1783 ++j;
1784 }
1785 } while ((i < src->used) && (j < trg->used));
1786
1787 if ((i < src->used) || count) {
1788 /* insert all the hashes first */
1789 for (k = i; k < src->used; ++k) {
1790 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1791 }
1792
1793 /* loop ended, but we need to copy something at trg end */
1794 count += src->used - i;
1795 i = src->used;
1796 goto copy_nodes;
1797 }
1798
1799 /* we are inserting hashes before the actual node insert, which causes
1800 * situations when there were initially not enough items for a hash table,
1801 * but even after some were inserted, hash table was not created (during
1802 * insertion the number of items is not updated yet) */
1803 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1804 set_insert_node_hash(trg, NULL, 0);
1805 }
1806
1807#ifndef NDEBUG
1808 LOGDBG(LY_LDGXPATH, "MERGE result");
1809 print_set_debug(trg);
1810#endif
1811
Michal Vaskod3678892020-05-21 10:06:58 +02001812 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001813 return LY_SUCCESS;
1814}
1815
1816/*
1817 * (re)parse functions
1818 *
1819 * Parse functions parse the expression into
1820 * tokens (syntactic analysis).
1821 *
1822 * Reparse functions perform semantic analysis
1823 * (do not save the result, just a check) of
1824 * the expression and fill repeat indices.
1825 */
1826
Michal Vasko14676352020-05-29 11:35:55 +02001827LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001828lyxp_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 +02001829{
Michal Vasko004d3152020-06-11 19:59:22 +02001830 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001831 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001832 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1833 }
Michal Vasko14676352020-05-29 11:35:55 +02001834 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001835 }
1836
Michal Vasko004d3152020-06-11 19:59:22 +02001837 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001838 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001839 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001840 lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001841 }
Michal Vasko14676352020-05-29 11:35:55 +02001842 return LY_ENOT;
1843 }
1844
1845 return LY_SUCCESS;
1846}
1847
Michal Vasko004d3152020-06-11 19:59:22 +02001848LY_ERR
1849lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1850{
1851 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1852
1853 /* skip the token */
1854 ++(*tok_idx);
1855
1856 return LY_SUCCESS;
1857}
1858
Michal Vasko14676352020-05-29 11:35:55 +02001859/* just like lyxp_check_token() but tests for 2 tokens */
1860static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001861exp_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 +02001862 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001863{
Michal Vasko004d3152020-06-11 19:59:22 +02001864 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001865 if (ctx) {
1866 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1867 }
1868 return LY_EINCOMPLETE;
1869 }
1870
Michal Vasko004d3152020-06-11 19:59:22 +02001871 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001872 if (ctx) {
1873 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001874 lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001875 }
1876 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001877 }
1878
1879 return LY_SUCCESS;
1880}
1881
1882/**
1883 * @brief Stack operation push on the repeat array.
1884 *
1885 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001886 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001887 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1888 */
1889static void
Michal Vasko004d3152020-06-11 19:59:22 +02001890exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001891{
1892 uint16_t i;
1893
Michal Vasko004d3152020-06-11 19:59:22 +02001894 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001895 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001896 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1897 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1898 exp->repeat[tok_idx][i] = repeat_op_idx;
1899 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001900 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001901 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1902 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1903 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001904 }
1905}
1906
1907/**
1908 * @brief Reparse Predicate. Logs directly on error.
1909 *
1910 * [7] Predicate ::= '[' Expr ']'
1911 *
1912 * @param[in] ctx Context for logging.
1913 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001914 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001915 * @return LY_ERR
1916 */
1917static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001918reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001919{
1920 LY_ERR rc;
1921
Michal Vasko004d3152020-06-11 19:59:22 +02001922 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001923 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001924 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001925
Michal Vasko004d3152020-06-11 19:59:22 +02001926 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001927 LY_CHECK_RET(rc);
1928
Michal Vasko004d3152020-06-11 19:59:22 +02001929 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001930 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001931 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001932
1933 return LY_SUCCESS;
1934}
1935
1936/**
1937 * @brief Reparse RelativeLocationPath. Logs directly on error.
1938 *
1939 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1940 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1941 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1942 *
1943 * @param[in] ctx Context for logging.
1944 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001945 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001946 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1947 */
1948static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001949reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001950{
1951 LY_ERR rc;
1952
Michal Vasko004d3152020-06-11 19:59:22 +02001953 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001954 LY_CHECK_RET(rc);
1955
1956 goto step;
1957 do {
1958 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001959 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001960
Michal Vasko004d3152020-06-11 19:59:22 +02001961 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001962 LY_CHECK_RET(rc);
1963step:
1964 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001965 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001966 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001967 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001968 break;
1969
1970 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001971 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001972 break;
1973
1974 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02001975 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001976
Michal Vasko004d3152020-06-11 19:59:22 +02001977 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001978 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001979 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001980 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001981 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001982 return LY_EVALID;
1983 }
Radek Krejci0f969882020-08-21 16:56:47 +02001984 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001985 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02001986 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001987 goto reparse_predicate;
1988 break;
1989
1990 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02001991 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001992
1993 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02001994 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001995 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001996 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001997
1998 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02001999 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002000 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002001 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002002
2003reparse_predicate:
2004 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002005 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2006 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002007 LY_CHECK_RET(rc);
2008 }
2009 break;
2010 default:
2011 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02002012 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002013 return LY_EVALID;
2014 }
Michal Vasko004d3152020-06-11 19:59:22 +02002015 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002016
2017 return LY_SUCCESS;
2018}
2019
2020/**
2021 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2022 *
2023 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2024 *
2025 * @param[in] ctx Context for logging.
2026 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002027 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002028 * @return LY_ERR
2029 */
2030static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002031reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002032{
2033 LY_ERR rc;
2034
Michal Vasko004d3152020-06-11 19:59:22 +02002035 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 +02002036
2037 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002038 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002039 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002040 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002041
Michal Vasko004d3152020-06-11 19:59:22 +02002042 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002043 return LY_SUCCESS;
2044 }
Michal Vasko004d3152020-06-11 19:59:22 +02002045 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002046 case LYXP_TOKEN_DOT:
2047 case LYXP_TOKEN_DDOT:
2048 case LYXP_TOKEN_AT:
2049 case LYXP_TOKEN_NAMETEST:
2050 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002051 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002052 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002053 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002054 default:
2055 break;
2056 }
2057
Michal Vasko03ff5a72019-09-11 13:49:33 +02002058 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002059 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002060 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002061
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);
2064 }
2065
2066 return LY_SUCCESS;
2067}
2068
2069/**
2070 * @brief Reparse FunctionCall. Logs directly on error.
2071 *
2072 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2073 *
2074 * @param[in] ctx Context for logging.
2075 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002076 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002077 * @return LY_ERR
2078 */
2079static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002080reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002081{
2082 int min_arg_count = -1, max_arg_count, arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002083 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002084 LY_ERR rc;
2085
Michal Vasko004d3152020-06-11 19:59:22 +02002086 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002087 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002088 func_tok_idx = *tok_idx;
2089 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002090 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002091 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002092 min_arg_count = 1;
2093 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002094 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002095 min_arg_count = 1;
2096 max_arg_count = 1;
2097 }
2098 break;
2099 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002100 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002101 min_arg_count = 1;
2102 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002103 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002104 min_arg_count = 0;
2105 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002106 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002107 min_arg_count = 0;
2108 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002109 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002110 min_arg_count = 0;
2111 max_arg_count = 0;
2112 }
2113 break;
2114 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002115 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002116 min_arg_count = 1;
2117 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002118 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002119 min_arg_count = 0;
2120 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002121 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002122 min_arg_count = 1;
2123 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002124 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002125 min_arg_count = 1;
2126 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002127 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002128 min_arg_count = 1;
2129 max_arg_count = 1;
2130 }
2131 break;
2132 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002133 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002134 min_arg_count = 2;
Michal Vaskobe2e3562019-10-15 15:35:35 +02002135 max_arg_count = INT_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002136 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002137 min_arg_count = 0;
2138 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002139 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002140 min_arg_count = 0;
2141 max_arg_count = 1;
2142 }
2143 break;
2144 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002145 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002146 min_arg_count = 1;
2147 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002148 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002149 min_arg_count = 1;
2150 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002151 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002152 min_arg_count = 0;
2153 max_arg_count = 0;
2154 }
2155 break;
2156 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002157 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002158 min_arg_count = 2;
2159 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002160 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002161 min_arg_count = 0;
2162 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002163 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002164 min_arg_count = 2;
2165 max_arg_count = 2;
2166 }
2167 break;
2168 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002169 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002170 min_arg_count = 2;
2171 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002172 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002173 min_arg_count = 3;
2174 max_arg_count = 3;
2175 }
2176 break;
2177 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002178 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002179 min_arg_count = 0;
2180 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002181 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002182 min_arg_count = 1;
2183 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002184 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002185 min_arg_count = 2;
2186 max_arg_count = 2;
2187 }
2188 break;
2189 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002190 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002191 min_arg_count = 2;
2192 max_arg_count = 2;
2193 }
2194 break;
2195 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002196 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002197 min_arg_count = 2;
2198 max_arg_count = 2;
2199 }
2200 break;
2201 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002202 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002203 min_arg_count = 0;
2204 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002205 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002206 min_arg_count = 0;
2207 max_arg_count = 1;
2208 }
2209 break;
2210 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002211 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002212 min_arg_count = 0;
2213 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002214 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002215 min_arg_count = 2;
2216 max_arg_count = 2;
2217 }
2218 break;
2219 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002220 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002221 min_arg_count = 2;
2222 max_arg_count = 2;
2223 }
2224 break;
2225 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002226 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002227 min_arg_count = 2;
2228 max_arg_count = 2;
2229 }
2230 break;
2231 }
2232 if (min_arg_count == -1) {
Michal Vasko004d3152020-06-11 19:59:22 +02002233 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 +02002234 return LY_EINVAL;
2235 }
Michal Vasko004d3152020-06-11 19:59:22 +02002236 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002237
2238 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002239 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002240 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002241 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002242
2243 /* ( Expr ( ',' Expr )* )? */
2244 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002245 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002246 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002247 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002248 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002249 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002250 LY_CHECK_RET(rc);
2251 }
Michal Vasko004d3152020-06-11 19:59:22 +02002252 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2253 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002254
2255 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002256 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002257 LY_CHECK_RET(rc);
2258 }
2259
2260 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002261 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002262 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002263 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002264
2265 if ((arg_count < min_arg_count) || (arg_count > max_arg_count)) {
Michal Vasko004d3152020-06-11 19:59:22 +02002266 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx],
2267 &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002268 return LY_EVALID;
2269 }
2270
2271 return LY_SUCCESS;
2272}
2273
2274/**
2275 * @brief Reparse PathExpr. Logs directly on error.
2276 *
2277 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2278 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2279 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2280 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2281 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2282 *
2283 * @param[in] ctx Context for logging.
2284 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002285 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002286 * @return LY_ERR
2287 */
2288static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002289reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002290{
2291 LY_ERR rc;
2292
Michal Vasko004d3152020-06-11 19:59:22 +02002293 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002294 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002295 }
2296
Michal Vasko004d3152020-06-11 19:59:22 +02002297 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002298 case LYXP_TOKEN_PAR1:
2299 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002300 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002301
Michal Vasko004d3152020-06-11 19:59:22 +02002302 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002303 LY_CHECK_RET(rc);
2304
Michal Vasko004d3152020-06-11 19:59:22 +02002305 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002306 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002307 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002308 goto predicate;
2309 break;
2310 case LYXP_TOKEN_DOT:
2311 case LYXP_TOKEN_DDOT:
2312 case LYXP_TOKEN_AT:
2313 case LYXP_TOKEN_NAMETEST:
2314 case LYXP_TOKEN_NODETYPE:
2315 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002316 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002317 LY_CHECK_RET(rc);
2318 break;
2319 case LYXP_TOKEN_FUNCNAME:
2320 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002321 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002322 LY_CHECK_RET(rc);
2323 goto predicate;
2324 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002325 case LYXP_TOKEN_OPER_PATH:
2326 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002327 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002328 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002329 LY_CHECK_RET(rc);
2330 break;
2331 case LYXP_TOKEN_LITERAL:
2332 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002333 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002334 goto predicate;
2335 break;
2336 case LYXP_TOKEN_NUMBER:
2337 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002338 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002339 goto predicate;
2340 break;
2341 default:
2342 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02002343 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002344 return LY_EVALID;
2345 }
2346
2347 return LY_SUCCESS;
2348
2349predicate:
2350 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002351 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2352 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002353 LY_CHECK_RET(rc);
2354 }
2355
2356 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002357 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002358
2359 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002360 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002361
Michal Vasko004d3152020-06-11 19:59:22 +02002362 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002363 LY_CHECK_RET(rc);
2364 }
2365
2366 return LY_SUCCESS;
2367}
2368
2369/**
2370 * @brief Reparse UnaryExpr. Logs directly on error.
2371 *
2372 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2373 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2374 *
2375 * @param[in] ctx Context for logging.
2376 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002377 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002378 * @return LY_ERR
2379 */
2380static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002381reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002382{
2383 uint16_t prev_exp;
2384 LY_ERR rc;
2385
2386 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002387 prev_exp = *tok_idx;
2388 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2389 && (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002390 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002391 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002392 }
2393
2394 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002395 prev_exp = *tok_idx;
2396 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002397 LY_CHECK_RET(rc);
2398
2399 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002400 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002401 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002402 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002403
Michal Vasko004d3152020-06-11 19:59:22 +02002404 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002405 LY_CHECK_RET(rc);
2406 }
2407
2408 return LY_SUCCESS;
2409}
2410
2411/**
2412 * @brief Reparse AdditiveExpr. Logs directly on error.
2413 *
2414 * [15] AdditiveExpr ::= MultiplicativeExpr
2415 * | AdditiveExpr '+' MultiplicativeExpr
2416 * | AdditiveExpr '-' MultiplicativeExpr
2417 * [16] MultiplicativeExpr ::= UnaryExpr
2418 * | MultiplicativeExpr '*' UnaryExpr
2419 * | MultiplicativeExpr 'div' UnaryExpr
2420 * | MultiplicativeExpr 'mod' UnaryExpr
2421 *
2422 * @param[in] ctx Context for logging.
2423 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002424 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002425 * @return LY_ERR
2426 */
2427static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002428reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002429{
2430 uint16_t prev_add_exp, prev_mul_exp;
2431 LY_ERR rc;
2432
Michal Vasko004d3152020-06-11 19:59:22 +02002433 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002434 goto reparse_multiplicative_expr;
2435
2436 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002437 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2438 && ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002439 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002440 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002441
2442reparse_multiplicative_expr:
2443 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002444 prev_mul_exp = *tok_idx;
2445 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002446 LY_CHECK_RET(rc);
2447
2448 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002449 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2450 && ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002451 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002452 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002453
Michal Vasko004d3152020-06-11 19:59:22 +02002454 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002455 LY_CHECK_RET(rc);
2456 }
2457 }
2458
2459 return LY_SUCCESS;
2460}
2461
2462/**
2463 * @brief Reparse EqualityExpr. Logs directly on error.
2464 *
2465 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2466 * | EqualityExpr '!=' RelationalExpr
2467 * [14] RelationalExpr ::= AdditiveExpr
2468 * | RelationalExpr '<' AdditiveExpr
2469 * | RelationalExpr '>' AdditiveExpr
2470 * | RelationalExpr '<=' AdditiveExpr
2471 * | RelationalExpr '>=' AdditiveExpr
2472 *
2473 * @param[in] ctx Context for logging.
2474 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002475 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002476 * @return LY_ERR
2477 */
2478static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002479reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002480{
2481 uint16_t prev_eq_exp, prev_rel_exp;
2482 LY_ERR rc;
2483
Michal Vasko004d3152020-06-11 19:59:22 +02002484 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002485 goto reparse_additive_expr;
2486
2487 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002488 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002489 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002490 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002491
2492reparse_additive_expr:
2493 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002494 prev_rel_exp = *tok_idx;
2495 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002496 LY_CHECK_RET(rc);
2497
2498 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002499 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002500 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002501 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002502
Michal Vasko004d3152020-06-11 19:59:22 +02002503 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002504 LY_CHECK_RET(rc);
2505 }
2506 }
2507
2508 return LY_SUCCESS;
2509}
2510
2511/**
2512 * @brief Reparse OrExpr. Logs directly on error.
2513 *
2514 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2515 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2516 *
2517 * @param[in] ctx Context for logging.
2518 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002519 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002520 * @return LY_ERR
2521 */
2522static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002523reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002524{
2525 uint16_t prev_or_exp, prev_and_exp;
2526 LY_ERR rc;
2527
Michal Vasko004d3152020-06-11 19:59:22 +02002528 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002529 goto reparse_equality_expr;
2530
2531 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002532 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 +02002533 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002534 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002535
2536reparse_equality_expr:
2537 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002538 prev_and_exp = *tok_idx;
2539 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002540 LY_CHECK_RET(rc);
2541
2542 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002543 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 +02002544 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002545 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002546
Michal Vasko004d3152020-06-11 19:59:22 +02002547 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002548 LY_CHECK_RET(rc);
2549 }
2550 }
2551
2552 return LY_SUCCESS;
2553}
Radek Krejcib1646a92018-11-02 16:08:26 +01002554
2555/**
2556 * @brief Parse NCName.
2557 *
2558 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002559 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002560 */
Radek Krejcid4270262019-01-07 15:07:25 +01002561static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002562parse_ncname(const char *ncname)
2563{
2564 unsigned int uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002565 size_t size;
2566 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002567
2568 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2569 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2570 return len;
2571 }
2572
2573 do {
2574 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002575 if (!*ncname) {
2576 break;
2577 }
Radek Krejcid4270262019-01-07 15:07:25 +01002578 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002579 } while (is_xmlqnamechar(uc) && (uc != ':'));
2580
2581 return len;
2582}
2583
2584/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002585 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002586 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002587 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002588 * @param[in] exp Expression to use.
2589 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002590 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002591 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002592 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002593 */
2594static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002595exp_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 +01002596{
2597 uint32_t prev;
2598
2599 if (exp->used == exp->size) {
2600 prev = exp->size;
2601 exp->size += LYXP_EXPR_SIZE_STEP;
2602 if (prev > exp->size) {
2603 LOGINT(ctx);
2604 return LY_EINT;
2605 }
2606
2607 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2608 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2609 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2610 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2611 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2612 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2613 }
2614
2615 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002616 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002617 exp->tok_len[exp->used] = tok_len;
2618 ++exp->used;
2619 return LY_SUCCESS;
2620}
2621
2622void
Michal Vasko14676352020-05-29 11:35:55 +02002623lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002624{
2625 uint16_t i;
2626
2627 if (!expr) {
2628 return;
2629 }
2630
2631 lydict_remove(ctx, expr->expr);
2632 free(expr->tokens);
2633 free(expr->tok_pos);
2634 free(expr->tok_len);
2635 if (expr->repeat) {
2636 for (i = 0; i < expr->used; ++i) {
2637 free(expr->repeat[i]);
2638 }
2639 }
2640 free(expr->repeat);
2641 free(expr);
2642}
2643
2644struct lyxp_expr *
Michal Vasko004d3152020-06-11 19:59:22 +02002645lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr, size_t expr_len, int reparse)
Radek Krejcib1646a92018-11-02 16:08:26 +01002646{
2647 struct lyxp_expr *ret;
Radek Krejcid4270262019-01-07 15:07:25 +01002648 size_t parsed = 0, tok_len;
2649 long int ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002650 enum lyxp_token tok_type;
2651 int prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002652 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002653
Michal Vasko004d3152020-06-11 19:59:22 +02002654 if (!expr[0]) {
2655 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
2656 return NULL;
2657 }
2658
2659 if (!expr_len) {
2660 expr_len = strlen(expr);
2661 }
2662 if (expr_len > UINT16_MAX) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002663 LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
2664 return NULL;
2665 }
2666
2667 /* init lyxp_expr structure */
2668 ret = calloc(1, sizeof *ret);
2669 LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error);
Michal Vasko004d3152020-06-11 19:59:22 +02002670 ret->expr = lydict_insert(ctx, expr, expr_len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002671 LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error);
2672 ret->used = 0;
2673 ret->size = LYXP_EXPR_SIZE_START;
2674 ret->tokens = malloc(ret->size * sizeof *ret->tokens);
2675 LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error);
2676
2677 ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos);
2678 LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error);
2679
2680 ret->tok_len = malloc(ret->size * sizeof *ret->tok_len);
2681 LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error);
2682
Michal Vasko004d3152020-06-11 19:59:22 +02002683 /* make expr 0-terminated */
2684 expr = ret->expr;
2685
Radek Krejcib1646a92018-11-02 16:08:26 +01002686 while (is_xmlws(expr[parsed])) {
2687 ++parsed;
2688 }
2689
2690 do {
2691 if (expr[parsed] == '(') {
2692
2693 /* '(' */
2694 tok_len = 1;
2695 tok_type = LYXP_TOKEN_PAR1;
2696
2697 if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) {
2698 /* it is a NodeType/FunctionName after all */
2699 if (((ret->tok_len[ret->used - 1] == 4)
2700 && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4)
2701 || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) ||
2702 ((ret->tok_len[ret->used - 1] == 7)
2703 && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) {
2704 ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE;
2705 } else {
2706 ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME;
2707 }
2708 prev_function_check = 0;
2709 }
2710
2711 } else if (expr[parsed] == ')') {
2712
2713 /* ')' */
2714 tok_len = 1;
2715 tok_type = LYXP_TOKEN_PAR2;
2716
2717 } else if (expr[parsed] == '[') {
2718
2719 /* '[' */
2720 tok_len = 1;
2721 tok_type = LYXP_TOKEN_BRACK1;
2722
2723 } else if (expr[parsed] == ']') {
2724
2725 /* ']' */
2726 tok_len = 1;
2727 tok_type = LYXP_TOKEN_BRACK2;
2728
2729 } else if (!strncmp(&expr[parsed], "..", 2)) {
2730
2731 /* '..' */
2732 tok_len = 2;
2733 tok_type = LYXP_TOKEN_DDOT;
2734
2735 } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) {
2736
2737 /* '.' */
2738 tok_len = 1;
2739 tok_type = LYXP_TOKEN_DOT;
2740
2741 } else if (expr[parsed] == '@') {
2742
2743 /* '@' */
2744 tok_len = 1;
2745 tok_type = LYXP_TOKEN_AT;
2746
2747 } else if (expr[parsed] == ',') {
2748
2749 /* ',' */
2750 tok_len = 1;
2751 tok_type = LYXP_TOKEN_COMMA;
2752
2753 } else if (expr[parsed] == '\'') {
2754
2755 /* Literal with ' */
Radek Krejci1e008d22020-08-17 11:37:37 +02002756 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002757 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2758 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2759 ++tok_len;
2760 tok_type = LYXP_TOKEN_LITERAL;
2761
2762 } else if (expr[parsed] == '\"') {
2763
2764 /* Literal with " */
Radek Krejci1e008d22020-08-17 11:37:37 +02002765 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002766 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2767 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2768 ++tok_len;
2769 tok_type = LYXP_TOKEN_LITERAL;
2770
2771 } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) {
2772
2773 /* Number */
Radek Krejci1e008d22020-08-17 11:37:37 +02002774 for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002775 if (expr[parsed + tok_len] == '.') {
2776 ++tok_len;
Radek Krejci1e008d22020-08-17 11:37:37 +02002777 for (; isdigit(expr[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002778 }
2779 tok_type = LYXP_TOKEN_NUMBER;
2780
2781 } else if (expr[parsed] == '/') {
2782
2783 /* Operator '/', '//' */
2784 if (!strncmp(&expr[parsed], "//", 2)) {
2785 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002786 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002787 } else {
2788 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002789 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002790 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002791
Michal Vasko3e48bf32020-06-01 08:39:07 +02002792 } else if (!strncmp(&expr[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002793
Michal Vasko3e48bf32020-06-01 08:39:07 +02002794 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002795 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002796 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2797
2798 } else if (!strncmp(&expr[parsed], "<=", 2) || !strncmp(&expr[parsed], ">=", 2)) {
2799
2800 /* Operator '<=', '>=' */
2801 tok_len = 2;
2802 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002803
2804 } else if (expr[parsed] == '|') {
2805
2806 /* Operator '|' */
2807 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002808 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002809
2810 } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) {
2811
2812 /* Operator '+', '-' */
2813 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002814 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002815
Michal Vasko3e48bf32020-06-01 08:39:07 +02002816 } else if (expr[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002817
Michal Vasko3e48bf32020-06-01 08:39:07 +02002818 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002819 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002820 tok_type = LYXP_TOKEN_OPER_EQUAL;
2821
2822 } else if ((expr[parsed] == '<') || (expr[parsed] == '>')) {
2823
2824 /* Operator '<', '>' */
2825 tok_len = 1;
2826 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002827
2828 } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT)
2829 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1)
2830 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1)
2831 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA)
Michal Vasko3e48bf32020-06-01 08:39:07 +02002832 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_LOG)
2833 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_EQUAL)
2834 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_NEQUAL)
2835 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_COMP)
2836 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_MATH)
2837 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_UNI)
2838 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_PATH)
2839 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002840
2841 /* Operator '*', 'or', 'and', 'mod', or 'div' */
2842 if (expr[parsed] == '*') {
2843 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002844 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002845
2846 } else if (!strncmp(&expr[parsed], "or", 2)) {
2847 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002848 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002849
2850 } else if (!strncmp(&expr[parsed], "and", 3)) {
2851 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002852 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002853
2854 } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) {
2855 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002856 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002857
2858 } else if (prev_function_check) {
Michal Vasko53078572019-05-24 08:50:15 +02002859 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
2860 expr[parsed], expr[parsed], ret->tok_len[ret->used - 1], &ret->expr[ret->tok_pos[ret->used - 1]]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002861 goto error;
2862 } else {
Radek Krejcid4270262019-01-07 15:07:25 +01002863 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr);
Radek Krejcib1646a92018-11-02 16:08:26 +01002864 goto error;
2865 }
2866 } else if (expr[parsed] == '*') {
2867
2868 /* NameTest '*' */
2869 tok_len = 1;
2870 tok_type = LYXP_TOKEN_NAMETEST;
2871
2872 } else {
2873
2874 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
2875 ncname_len = parse_ncname(&expr[parsed]);
Radek Krejcid4270262019-01-07 15:07:25 +01002876 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002877 tok_len = ncname_len;
2878
2879 if (expr[parsed + tok_len] == ':') {
2880 ++tok_len;
2881 if (expr[parsed + tok_len] == '*') {
2882 ++tok_len;
2883 } else {
2884 ncname_len = parse_ncname(&expr[parsed + tok_len]);
Radek Krejcid4270262019-01-07 15:07:25 +01002885 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002886 tok_len += ncname_len;
2887 }
2888 /* remove old flag to prevent ambiguities */
2889 prev_function_check = 0;
2890 tok_type = LYXP_TOKEN_NAMETEST;
2891 } else {
2892 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2893 prev_function_check = 1;
2894 tok_type = LYXP_TOKEN_NAMETEST;
2895 }
2896 }
2897
2898 /* store the token, move on to the next one */
2899 LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error);
2900 parsed += tok_len;
2901 while (is_xmlws(expr[parsed])) {
2902 ++parsed;
2903 }
2904
2905 } while (expr[parsed]);
2906
Michal Vasko004d3152020-06-11 19:59:22 +02002907 if (reparse) {
2908 /* prealloc repeat */
2909 ret->repeat = calloc(ret->size, sizeof *ret->repeat);
2910 LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002911
Michal Vasko004d3152020-06-11 19:59:22 +02002912 /* fill repeat */
2913 LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &tok_idx), error);
2914 if (ret->used > tok_idx) {
2915 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
2916 &ret->expr[ret->tok_pos[tok_idx]]);
2917 goto error;
2918 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002919 }
2920
2921 print_expr_struct_debug(ret);
2922
Radek Krejcib1646a92018-11-02 16:08:26 +01002923 return ret;
2924
2925error:
2926 lyxp_expr_free(ctx, ret);
2927 return NULL;
2928}
2929
Michal Vasko004d3152020-06-11 19:59:22 +02002930struct lyxp_expr *
2931lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp)
2932{
2933 struct lyxp_expr *dup;
2934 uint32_t i, j;
2935
2936 dup = calloc(1, sizeof *dup);
2937 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx), error);
2938
2939 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
2940 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx), error);
2941 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2942
2943 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
2944 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx), error);
2945 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2946
2947 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
2948 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx), error);
2949 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2950
2951 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
2952 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx), error);
2953 for (i = 0; i < exp->used; ++i) {
2954 if (!exp->repeat[i]) {
2955 dup->repeat[i] = NULL;
2956 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02002957 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002958 /* the ending 0 as well */
2959 ++j;
2960
Michal Vasko99c71642020-07-03 13:33:36 +02002961 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko004d3152020-06-11 19:59:22 +02002962 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx), error);
2963 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
2964 dup->repeat[i][j - 1] = 0;
2965 }
2966 }
2967
2968 dup->used = exp->used;
2969 dup->size = exp->used;
2970 dup->expr = lydict_insert(ctx, exp->expr, 0);
2971
2972 return dup;
2973
2974error:
2975 lyxp_expr_free(ctx, dup);
2976 return NULL;
2977}
2978
Michal Vasko03ff5a72019-09-11 13:49:33 +02002979/*
2980 * warn functions
2981 *
2982 * Warn functions check specific reasonable conditions for schema XPath
2983 * and print a warning if they are not satisfied.
2984 */
2985
2986/**
2987 * @brief Get the last-added schema node that is currently in the context.
2988 *
2989 * @param[in] set Set to search in.
2990 * @return Last-added schema context node, NULL if no node is in context.
2991 */
2992static struct lysc_node *
2993warn_get_scnode_in_ctx(struct lyxp_set *set)
2994{
2995 uint32_t i;
2996
2997 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
2998 return NULL;
2999 }
3000
3001 i = set->used;
3002 do {
3003 --i;
3004 if (set->val.scnodes[i].in_ctx == 1) {
3005 /* if there are more, simply return the first found (last added) */
3006 return set->val.scnodes[i].scnode;
3007 }
3008 } while (i);
3009
3010 return NULL;
3011}
3012
3013/**
3014 * @brief Test whether a type is numeric - integer type or decimal64.
3015 *
3016 * @param[in] type Type to test.
3017 * @return 1 if numeric, 0 otherwise.
3018 */
3019static int
3020warn_is_numeric_type(struct lysc_type *type)
3021{
3022 struct lysc_type_union *uni;
3023 int ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003024 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003025
3026 switch (type->basetype) {
3027 case LY_TYPE_DEC64:
3028 case LY_TYPE_INT8:
3029 case LY_TYPE_UINT8:
3030 case LY_TYPE_INT16:
3031 case LY_TYPE_UINT16:
3032 case LY_TYPE_INT32:
3033 case LY_TYPE_UINT32:
3034 case LY_TYPE_INT64:
3035 case LY_TYPE_UINT64:
3036 return 1;
3037 case LY_TYPE_UNION:
3038 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003039 LY_ARRAY_FOR(uni->types, u) {
3040 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003041 if (ret) {
3042 /* found a suitable type */
3043 return 1;
3044 }
3045 }
3046 /* did not find any suitable type */
3047 return 0;
3048 case LY_TYPE_LEAFREF:
3049 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3050 default:
3051 return 0;
3052 }
3053}
3054
3055/**
3056 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3057 *
3058 * @param[in] type Type to test.
3059 * @return 1 if string, 0 otherwise.
3060 */
3061static int
3062warn_is_string_type(struct lysc_type *type)
3063{
3064 struct lysc_type_union *uni;
3065 int ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003066 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003067
3068 switch (type->basetype) {
3069 case LY_TYPE_BITS:
3070 case LY_TYPE_ENUM:
3071 case LY_TYPE_IDENT:
3072 case LY_TYPE_INST:
3073 case LY_TYPE_STRING:
3074 return 1;
3075 case LY_TYPE_UNION:
3076 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003077 LY_ARRAY_FOR(uni->types, u) {
3078 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003079 if (ret) {
3080 /* found a suitable type */
3081 return 1;
3082 }
3083 }
3084 /* did not find any suitable type */
3085 return 0;
3086 case LY_TYPE_LEAFREF:
3087 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3088 default:
3089 return 0;
3090 }
3091}
3092
3093/**
3094 * @brief Test whether a type is one specific type.
3095 *
3096 * @param[in] type Type to test.
3097 * @param[in] base Expected type.
3098 * @return 1 if it is, 0 otherwise.
3099 */
3100static int
3101warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3102{
3103 struct lysc_type_union *uni;
3104 int ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003105 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003106
3107 if (type->basetype == base) {
3108 return 1;
3109 } else if (type->basetype == LY_TYPE_UNION) {
3110 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003111 LY_ARRAY_FOR(uni->types, u) {
3112 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003113 if (ret) {
3114 /* found a suitable type */
3115 return 1;
3116 }
3117 }
3118 /* did not find any suitable type */
3119 return 0;
3120 } else if (type->basetype == LY_TYPE_LEAFREF) {
3121 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3122 }
3123
3124 return 0;
3125}
3126
3127/**
3128 * @brief Get next type of a (union) type.
3129 *
3130 * @param[in] type Base type.
3131 * @param[in] prev_type Previously returned type.
3132 * @return Next type or NULL.
3133 */
3134static struct lysc_type *
3135warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3136{
3137 struct lysc_type_union *uni;
3138 int found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003139 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003140
3141 switch (type->basetype) {
3142 case LY_TYPE_UNION:
3143 uni = (struct lysc_type_union *)type;
3144 if (!prev_type) {
3145 return uni->types[0];
3146 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003147 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003148 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003149 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003150 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003151 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003152 found = 1;
3153 }
3154 }
3155 return NULL;
3156 default:
3157 if (prev_type) {
3158 assert(type == prev_type);
3159 return NULL;
3160 } else {
3161 return type;
3162 }
3163 }
3164}
3165
3166/**
3167 * @brief Test whether 2 types have a common type.
3168 *
3169 * @param[in] type1 First type.
3170 * @param[in] type2 Second type.
3171 * @return 1 if they do, 0 otherwise.
3172 */
3173static int
3174warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3175{
3176 struct lysc_type *t1, *rt1, *t2, *rt2;
3177
3178 t1 = NULL;
3179 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3180 if (t1->basetype == LY_TYPE_LEAFREF) {
3181 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3182 } else {
3183 rt1 = t1;
3184 }
3185
3186 t2 = NULL;
3187 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3188 if (t2->basetype == LY_TYPE_LEAFREF) {
3189 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3190 } else {
3191 rt2 = t2;
3192 }
3193
3194 if (rt2->basetype == rt1->basetype) {
3195 /* match found */
3196 return 1;
3197 }
3198 }
3199 }
3200
3201 return 0;
3202}
3203
3204/**
3205 * @brief Check both operands of comparison operators.
3206 *
3207 * @param[in] ctx Context for errors.
3208 * @param[in] set1 First operand set.
3209 * @param[in] set2 Second operand set.
3210 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3211 * @param[in] expr Start of the expression to print with the warning.
3212 * @param[in] tok_pos Token position.
3213 */
3214static void
3215warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, int numbers_only, const char *expr, uint16_t tok_pos)
3216{
3217 struct lysc_node_leaf *node1, *node2;
3218 int leaves = 1, warning = 0;
3219
3220 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3221 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3222
3223 if (!node1 && !node2) {
3224 /* no node-sets involved, nothing to do */
3225 return;
3226 }
3227
3228 if (node1) {
3229 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3230 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3231 warning = 1;
3232 leaves = 0;
3233 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3234 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3235 warning = 1;
3236 }
3237 }
3238
3239 if (node2) {
3240 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3241 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3242 warning = 1;
3243 leaves = 0;
3244 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3245 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3246 warning = 1;
3247 }
3248 }
3249
3250 if (node1 && node2 && leaves && !numbers_only) {
3251 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type))
3252 || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type))
3253 || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)
3254 && !warn_is_equal_type(node1->type, node2->type))) {
3255 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3256 warning = 1;
3257 }
3258 }
3259
3260 if (warning) {
3261 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3262 }
3263}
3264
3265/**
3266 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3267 *
3268 * @param[in] exp Parsed XPath expression.
3269 * @param[in] set Set with the leaf/leaf-list.
3270 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3271 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3272 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3273 */
3274static void
3275warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp)
3276{
3277 struct lysc_node *scnode;
3278 struct lysc_type *type;
3279 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003280 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003281 LY_ERR rc;
3282 struct ly_err_item *err = NULL;
3283
3284 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3285 && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
3286 /* check that the node can have the specified value */
3287 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3288 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3289 } else {
3290 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3291 }
3292 if (!value) {
3293 LOGMEM(set->ctx);
3294 return;
3295 }
3296
3297 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3298 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
3299 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
3300 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003301 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko03ff5a72019-09-11 13:49:33 +02003302 exp->expr + exp->tok_pos[equal_exp]);
3303 }
3304
3305 type = ((struct lysc_node_leaf *)scnode)->type;
3306 if (type->basetype != LY_TYPE_IDENT) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003307 rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA, LY_PREF_SCHEMA,
Radek Krejci0f969882020-08-21 16:56:47 +02003308 (void *)set->local_mod, NULL, NULL, &storage, &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003309
3310 if (err) {
3311 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3312 ly_err_free(err);
3313 } else if (rc != LY_SUCCESS) {
3314 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3315 }
3316 if (rc != LY_SUCCESS) {
3317 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003318 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko03ff5a72019-09-11 13:49:33 +02003319 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003320 } else {
3321 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003322 }
3323 }
3324 free(value);
3325 }
3326}
3327
3328/*
3329 * XPath functions
3330 */
3331
3332/**
3333 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3334 * depending on whether the first node bit value from the second argument is set.
3335 *
3336 * @param[in] args Array of arguments.
3337 * @param[in] arg_count Count of elements in @p args.
3338 * @param[in,out] set Context and result set at the same time.
3339 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003340 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003341 */
3342static LY_ERR
3343xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3344{
3345 struct lyd_node_term *leaf;
3346 struct lysc_node_leaf *sleaf;
3347 struct lysc_type_bits *bits;
3348 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003349 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003350
3351 if (options & LYXP_SCNODE_ALL) {
3352 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3353 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003354 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3355 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 +02003356 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3357 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003358 }
3359
3360 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3361 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3362 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 +02003363 } else if (!warn_is_string_type(sleaf->type)) {
3364 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003365 }
3366 }
3367 set_scnode_clear_ctx(set);
3368 return rc;
3369 }
3370
Michal Vaskod3678892020-05-21 10:06:58 +02003371 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003372 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)");
3373 return LY_EVALID;
3374 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003375 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003376 LY_CHECK_RET(rc);
3377
3378 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003379 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003380 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3381 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3382 && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
3383 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003384 LY_ARRAY_FOR(bits->bits, u) {
3385 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003386 set_fill_boolean(set, 1);
3387 break;
3388 }
3389 }
3390 }
3391 }
3392
3393 return LY_SUCCESS;
3394}
3395
3396/**
3397 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3398 * with the argument converted to boolean.
3399 *
3400 * @param[in] args Array of arguments.
3401 * @param[in] arg_count Count of elements in @p args.
3402 * @param[in,out] set Context and result set at the same time.
3403 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003404 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003405 */
3406static LY_ERR
3407xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3408{
3409 LY_ERR rc;
3410
3411 if (options & LYXP_SCNODE_ALL) {
3412 set_scnode_clear_ctx(set);
3413 return LY_SUCCESS;
3414 }
3415
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003416 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003417 LY_CHECK_RET(rc);
3418 set_fill_set(set, args[0]);
3419
3420 return LY_SUCCESS;
3421}
3422
3423/**
3424 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3425 * with the first argument rounded up to the nearest integer.
3426 *
3427 * @param[in] args Array of arguments.
3428 * @param[in] arg_count Count of elements in @p args.
3429 * @param[in,out] set Context and result set at the same time.
3430 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003431 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003432 */
3433static LY_ERR
3434xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3435{
3436 struct lysc_node_leaf *sleaf;
3437 LY_ERR rc = LY_SUCCESS;
3438
3439 if (options & LYXP_SCNODE_ALL) {
3440 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3441 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003442 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3443 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 +02003444 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3445 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003446 }
3447 set_scnode_clear_ctx(set);
3448 return rc;
3449 }
3450
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003451 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003452 LY_CHECK_RET(rc);
3453 if ((long long)args[0]->val.num != args[0]->val.num) {
3454 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3455 } else {
3456 set_fill_number(set, args[0]->val.num);
3457 }
3458
3459 return LY_SUCCESS;
3460}
3461
3462/**
3463 * @brief Execute the XPath concat(string, string, string*) function.
3464 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3465 *
3466 * @param[in] args Array of arguments.
3467 * @param[in] arg_count Count of elements in @p args.
3468 * @param[in,out] set Context and result set at the same time.
3469 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003470 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003471 */
3472static LY_ERR
3473xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3474{
3475 uint16_t i;
3476 char *str = NULL;
3477 size_t used = 1;
3478 LY_ERR rc = LY_SUCCESS;
3479 struct lysc_node_leaf *sleaf;
3480
3481 if (options & LYXP_SCNODE_ALL) {
3482 for (i = 0; i < arg_count; ++i) {
3483 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3484 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3485 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
3486 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003487 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003488 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 +02003489 }
3490 }
3491 }
3492 set_scnode_clear_ctx(set);
3493 return rc;
3494 }
3495
3496 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003497 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003498 if (rc != LY_SUCCESS) {
3499 free(str);
3500 return rc;
3501 }
3502
3503 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3504 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3505 strcpy(str + used - 1, args[i]->val.str);
3506 used += strlen(args[i]->val.str);
3507 }
3508
3509 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003510 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003511 set->type = LYXP_SET_STRING;
3512 set->val.str = str;
3513
3514 return LY_SUCCESS;
3515}
3516
3517/**
3518 * @brief Execute the XPath contains(string, string) function.
3519 * Returns LYXP_SET_BOOLEAN whether the second argument can
3520 * be found in the first or not.
3521 *
3522 * @param[in] args Array of arguments.
3523 * @param[in] arg_count Count of elements in @p args.
3524 * @param[in,out] set Context and result set at the same time.
3525 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003526 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003527 */
3528static LY_ERR
3529xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3530{
3531 struct lysc_node_leaf *sleaf;
3532 LY_ERR rc = LY_SUCCESS;
3533
3534 if (options & LYXP_SCNODE_ALL) {
3535 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3536 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3537 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 +02003538 } else if (!warn_is_string_type(sleaf->type)) {
3539 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003540 }
3541 }
3542
3543 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3544 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3545 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 +02003546 } else if (!warn_is_string_type(sleaf->type)) {
3547 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003548 }
3549 }
3550 set_scnode_clear_ctx(set);
3551 return rc;
3552 }
3553
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003554 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003555 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003556 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003557 LY_CHECK_RET(rc);
3558
3559 if (strstr(args[0]->val.str, args[1]->val.str)) {
3560 set_fill_boolean(set, 1);
3561 } else {
3562 set_fill_boolean(set, 0);
3563 }
3564
3565 return LY_SUCCESS;
3566}
3567
3568/**
3569 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3570 * with the size of the node-set from the argument.
3571 *
3572 * @param[in] args Array of arguments.
3573 * @param[in] arg_count Count of elements in @p args.
3574 * @param[in,out] set Context and result set at the same time.
3575 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003576 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003577 */
3578static LY_ERR
3579xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3580{
3581 struct lysc_node *scnode = NULL;
3582 LY_ERR rc = LY_SUCCESS;
3583
3584 if (options & LYXP_SCNODE_ALL) {
3585 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3586 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003587 }
3588 set_scnode_clear_ctx(set);
3589 return rc;
3590 }
3591
Michal Vasko03ff5a72019-09-11 13:49:33 +02003592 if (args[0]->type != LYXP_SET_NODE_SET) {
3593 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
3594 return LY_EVALID;
3595 }
3596
3597 set_fill_number(set, args[0]->used);
3598 return LY_SUCCESS;
3599}
3600
3601/**
3602 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3603 * with the context with the intial node.
3604 *
3605 * @param[in] args Array of arguments.
3606 * @param[in] arg_count Count of elements in @p args.
3607 * @param[in,out] set Context and result set at the same time.
3608 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003609 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003610 */
3611static LY_ERR
3612xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3613{
3614 if (arg_count || args) {
3615 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
3616 return LY_EVALID;
3617 }
3618
3619 if (options & LYXP_SCNODE_ALL) {
3620 set_scnode_clear_ctx(set);
3621
Michal Vaskoecd62de2019-11-13 12:35:11 +01003622 lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003623 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003624 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003625
3626 /* position is filled later */
3627 set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0);
3628 }
3629
3630 return LY_SUCCESS;
3631}
3632
3633/**
3634 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3635 * leafref or instance-identifier target node(s).
3636 *
3637 * @param[in] args Array of arguments.
3638 * @param[in] arg_count Count of elements in @p args.
3639 * @param[in,out] set Context and result set at the same time.
3640 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003641 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003642 */
3643static LY_ERR
3644xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3645{
3646 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003647 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003648 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003649 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003650 struct ly_path *p;
3651 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003652 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003653 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003654 LY_ERR rc = LY_SUCCESS;
3655
3656 if (options & LYXP_SCNODE_ALL) {
3657 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3658 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003659 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3660 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 +02003661 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3662 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3663 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003664 }
3665 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003666 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003667 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vasko00cbf532020-06-15 13:58:47 +02003668 oper = lysc_is_output((struct lysc_node *)sleaf) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003669
3670 /* it was already evaluated on schema, it must succeed */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003671 rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE,
3672 oper, LY_PATH_TARGET_MANY, set->format, lref->path_context, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003673 assert(!rc);
3674
3675 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003676 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003677 ly_path_free(set->ctx, p);
3678
3679 lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM);
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003680 }
3681
Michal Vasko03ff5a72019-09-11 13:49:33 +02003682 return rc;
3683 }
3684
Michal Vaskod3678892020-05-21 10:06:58 +02003685 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003686 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
3687 return LY_EVALID;
3688 }
3689
Michal Vaskod3678892020-05-21 10:06:58 +02003690 lyxp_set_free_content(set);
3691 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003692 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3693 sleaf = (struct lysc_node_leaf *)leaf->schema;
3694 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3695 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3696 /* find leafref target */
Michal Vasko004d3152020-06-11 19:59:22 +02003697 if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf,
3698 &leaf->value, set->tree, &node, &errmsg)) {
3699 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003700 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003701 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003702 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003703 } else {
3704 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003705 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003706 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Michal Vaskob7be7a82020-08-20 09:09:04 +02003707 LYD_CANON_VALUE(leaf));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003708 return LY_EVALID;
3709 }
3710 }
Michal Vasko004d3152020-06-11 19:59:22 +02003711
3712 /* insert it */
3713 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003714 }
3715 }
3716
3717 return LY_SUCCESS;
3718}
3719
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003720static LY_ERR
3721xpath_derived_(struct lyxp_set **args, struct lyxp_set *set, int options, int self_match, const char *func)
3722{
3723 uint16_t i;
3724 LY_ARRAY_COUNT_TYPE u;
3725 struct lyd_node_term *leaf;
3726 struct lysc_node_leaf *sleaf;
3727 struct lyd_meta *meta;
3728 struct lyd_value data = {0}, *val;
3729 struct ly_err_item *err = NULL;
3730 LY_ERR rc = LY_SUCCESS;
3731 int found;
3732
3733 if (options & LYXP_SCNODE_ALL) {
3734 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3735 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
3736 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3737 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3738 sleaf->name);
3739 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3740 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3741 }
3742
3743 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3744 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3745 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3746 sleaf->name);
3747 } else if (!warn_is_string_type(sleaf->type)) {
3748 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3749 }
3750 }
3751 set_scnode_clear_ctx(set);
3752 return rc;
3753 }
3754
3755 if (args[0]->type != LYXP_SET_NODE_SET) {
3756 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3757 "derived-from(-or-self)(node-set, string)");
3758 return LY_EVALID;
3759 }
3760 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3761 LY_CHECK_RET(rc);
3762
3763 set_fill_boolean(set, 0);
3764 found = 0;
3765 for (i = 0; i < args[0]->used; ++i) {
3766 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3767 continue;
3768 }
3769
3770 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3771 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3772 sleaf = (struct lysc_node_leaf *)leaf->schema;
3773 val = &leaf->value;
3774 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3775 /* uninteresting */
3776 continue;
3777 }
3778
3779 /* store args[1] as ident */
3780 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str),
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003781 0, set->format, (void *)set->local_mod,
Radek Krejci0f969882020-08-21 16:56:47 +02003782 (struct lyd_node *)leaf, set->tree, &data, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003783 } else {
3784 meta = args[0]->val.meta[i].meta;
3785 val = &meta->value;
3786 if (val->realtype->basetype != LY_TYPE_IDENT) {
3787 /* uninteresting */
3788 continue;
3789 }
3790
3791 /* store args[1] as ident */
3792 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str),
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003793 0, set->format, (void *)meta->annotation->module,
3794 meta->parent, set->tree, &data, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003795 }
3796
3797 if (err) {
3798 ly_err_print(err);
3799 ly_err_free(err);
3800 }
3801 LY_CHECK_RET(rc);
3802
3803 /* finally check the identity itself */
3804 if (self_match && (data.ident == val->ident)) {
3805 set_fill_boolean(set, 1);
3806 found = 1;
3807 }
3808 if (!found) {
3809 LY_ARRAY_FOR(data.ident->derived, u) {
3810 if (data.ident->derived[u] == val->ident) {
3811 set_fill_boolean(set, 1);
3812 found = 1;
3813 break;
3814 }
3815 }
3816 }
3817
3818 /* free temporary value */
3819 val->realtype->plugin->free(set->ctx, &data);
3820 if (found) {
3821 break;
3822 }
3823 }
3824
3825 return LY_SUCCESS;
3826}
3827
Michal Vasko03ff5a72019-09-11 13:49:33 +02003828/**
3829 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3830 * on whether the first argument nodes contain a node of an identity derived from the second
3831 * argument identity.
3832 *
3833 * @param[in] args Array of arguments.
3834 * @param[in] arg_count Count of elements in @p args.
3835 * @param[in,out] set Context and result set at the same time.
3836 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003837 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003838 */
3839static LY_ERR
3840xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3841{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003842 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003843}
3844
3845/**
3846 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3847 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3848 * the second argument identity.
3849 *
3850 * @param[in] args Array of arguments.
3851 * @param[in] arg_count Count of elements in @p args.
3852 * @param[in,out] set Context and result set at the same time.
3853 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003854 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003855 */
3856static LY_ERR
3857xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3858{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003859 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003860}
3861
3862/**
3863 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3864 * with the integer value of the first node's enum value, otherwise NaN.
3865 *
3866 * @param[in] args Array of arguments.
3867 * @param[in] arg_count Count of elements in @p args.
3868 * @param[in,out] set Context and result set at the same time.
3869 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003870 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003871 */
3872static LY_ERR
3873xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3874{
3875 struct lyd_node_term *leaf;
3876 struct lysc_node_leaf *sleaf;
3877 LY_ERR rc = LY_SUCCESS;
3878
3879 if (options & LYXP_SCNODE_ALL) {
3880 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3881 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003882 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3883 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 +02003884 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3885 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003886 }
3887 set_scnode_clear_ctx(set);
3888 return rc;
3889 }
3890
Michal Vaskod3678892020-05-21 10:06:58 +02003891 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003892 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
3893 return LY_EVALID;
3894 }
3895
3896 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003897 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003898 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3899 sleaf = (struct lysc_node_leaf *)leaf->schema;
3900 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3901 set_fill_number(set, leaf->value.enum_item->value);
3902 }
3903 }
3904
3905 return LY_SUCCESS;
3906}
3907
3908/**
3909 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3910 * with false value.
3911 *
3912 * @param[in] args Array of arguments.
3913 * @param[in] arg_count Count of elements in @p args.
3914 * @param[in,out] set Context and result set at the same time.
3915 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003916 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003917 */
3918static LY_ERR
3919xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3920{
3921 if (options & LYXP_SCNODE_ALL) {
3922 set_scnode_clear_ctx(set);
3923 return LY_SUCCESS;
3924 }
3925
3926 set_fill_boolean(set, 0);
3927 return LY_SUCCESS;
3928}
3929
3930/**
3931 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3932 * with the first argument floored (truncated).
3933 *
3934 * @param[in] args Array of arguments.
3935 * @param[in] arg_count Count of elements in @p args.
3936 * @param[in,out] set Context and result set at the same time.
3937 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003938 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003939 */
3940static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003941xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int UNUSED(options))
Michal Vasko03ff5a72019-09-11 13:49:33 +02003942{
3943 LY_ERR rc;
3944
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003945 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003946 LY_CHECK_RET(rc);
3947 if (isfinite(args[0]->val.num)) {
3948 set_fill_number(set, (long long)args[0]->val.num);
3949 }
3950
3951 return LY_SUCCESS;
3952}
3953
3954/**
3955 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3956 * whether the language of the text matches the one from the argument.
3957 *
3958 * @param[in] args Array of arguments.
3959 * @param[in] arg_count Count of elements in @p args.
3960 * @param[in,out] set Context and result set at the same time.
3961 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003962 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003963 */
3964static LY_ERR
3965xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3966{
3967 const struct lyd_node *node;
3968 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01003969 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003970 const char *val;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003971 int i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003972 LY_ERR rc = LY_SUCCESS;
3973
3974 if (options & LYXP_SCNODE_ALL) {
3975 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3976 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3977 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 +02003978 } else if (!warn_is_string_type(sleaf->type)) {
3979 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003980 }
3981 }
3982 set_scnode_clear_ctx(set);
3983 return rc;
3984 }
3985
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003986 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003987 LY_CHECK_RET(rc);
3988
Michal Vasko03ff5a72019-09-11 13:49:33 +02003989 if (set->type != LYXP_SET_NODE_SET) {
3990 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
3991 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02003992 } else if (!set->used) {
3993 set_fill_boolean(set, 0);
3994 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003995 }
3996
3997 switch (set->val.nodes[0].type) {
3998 case LYXP_NODE_ELEM:
3999 case LYXP_NODE_TEXT:
4000 node = set->val.nodes[0].node;
4001 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004002 case LYXP_NODE_META:
4003 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004004 break;
4005 default:
4006 /* nothing to do with roots */
4007 set_fill_boolean(set, 0);
4008 return LY_SUCCESS;
4009 }
4010
Michal Vasko9f96a052020-03-10 09:41:45 +01004011 /* find lang metadata */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004012 for (; node; node = (struct lyd_node *)node->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004013 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004014 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004015 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004016 break;
4017 }
4018 }
4019
Michal Vasko9f96a052020-03-10 09:41:45 +01004020 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004021 break;
4022 }
4023 }
4024
4025 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004026 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004027 set_fill_boolean(set, 0);
4028 } else {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004029 val = meta->value.canonical;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004030 for (i = 0; args[0]->val.str[i]; ++i) {
4031 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4032 set_fill_boolean(set, 0);
4033 break;
4034 }
4035 }
4036 if (!args[0]->val.str[i]) {
4037 if (!val[i] || (val[i] == '-')) {
4038 set_fill_boolean(set, 1);
4039 } else {
4040 set_fill_boolean(set, 0);
4041 }
4042 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004043 }
4044
4045 return LY_SUCCESS;
4046}
4047
4048/**
4049 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4050 * with the context size.
4051 *
4052 * @param[in] args Array of arguments.
4053 * @param[in] arg_count Count of elements in @p args.
4054 * @param[in,out] set Context and result set at the same time.
4055 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004056 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004057 */
4058static LY_ERR
4059xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4060{
4061 if (options & LYXP_SCNODE_ALL) {
4062 set_scnode_clear_ctx(set);
4063 return LY_SUCCESS;
4064 }
4065
Michal Vasko03ff5a72019-09-11 13:49:33 +02004066 if (set->type != LYXP_SET_NODE_SET) {
4067 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
4068 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004069 } else if (!set->used) {
4070 set_fill_number(set, 0);
4071 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004072 }
4073
4074 set_fill_number(set, set->ctx_size);
4075 return LY_SUCCESS;
4076}
4077
4078/**
4079 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4080 * with the node name without namespace from the argument or the context.
4081 *
4082 * @param[in] args Array of arguments.
4083 * @param[in] arg_count Count of elements in @p args.
4084 * @param[in,out] set Context and result set at the same time.
4085 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004086 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004087 */
4088static LY_ERR
4089xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4090{
4091 struct lyxp_set_node *item;
4092 /* suppress unused variable warning */
4093 (void)options;
4094
4095 if (options & LYXP_SCNODE_ALL) {
4096 set_scnode_clear_ctx(set);
4097 return LY_SUCCESS;
4098 }
4099
4100 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004101 if (args[0]->type != LYXP_SET_NODE_SET) {
4102 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)");
4103 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004104 } else if (!args[0]->used) {
4105 set_fill_string(set, "", 0);
4106 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004107 }
4108
4109 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004110 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004111
4112 item = &args[0]->val.nodes[0];
4113 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004114 if (set->type != LYXP_SET_NODE_SET) {
4115 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
4116 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004117 } else if (!set->used) {
4118 set_fill_string(set, "", 0);
4119 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004120 }
4121
4122 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004123 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004124
4125 item = &set->val.nodes[0];
4126 }
4127
4128 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004129 case LYXP_NODE_NONE:
4130 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004131 case LYXP_NODE_ROOT:
4132 case LYXP_NODE_ROOT_CONFIG:
4133 case LYXP_NODE_TEXT:
4134 set_fill_string(set, "", 0);
4135 break;
4136 case LYXP_NODE_ELEM:
4137 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4138 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004139 case LYXP_NODE_META:
4140 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004141 break;
4142 }
4143
4144 return LY_SUCCESS;
4145}
4146
4147/**
4148 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4149 * with the node name fully qualified (with namespace) from the argument or the context.
4150 *
4151 * @param[in] args Array of arguments.
4152 * @param[in] arg_count Count of elements in @p args.
4153 * @param[in,out] set Context and result set at the same time.
4154 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004155 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004156 */
4157static LY_ERR
4158xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4159{
4160 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004161 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004162 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004163 const char *name = NULL;
4164 int rc = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004165
4166 if (options & LYXP_SCNODE_ALL) {
4167 set_scnode_clear_ctx(set);
4168 return LY_SUCCESS;
4169 }
4170
4171 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004172 if (args[0]->type != LYXP_SET_NODE_SET) {
4173 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
4174 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004175 } else if (!args[0]->used) {
4176 set_fill_string(set, "", 0);
4177 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004178 }
4179
4180 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004181 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004182
4183 item = &args[0]->val.nodes[0];
4184 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004185 if (set->type != LYXP_SET_NODE_SET) {
4186 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
4187 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004188 } else if (!set->used) {
4189 set_fill_string(set, "", 0);
4190 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004191 }
4192
4193 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004194 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004195
4196 item = &set->val.nodes[0];
4197 }
4198
4199 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004200 case LYXP_NODE_NONE:
4201 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004202 case LYXP_NODE_ROOT:
4203 case LYXP_NODE_ROOT_CONFIG:
4204 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004205 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004206 break;
4207 case LYXP_NODE_ELEM:
4208 mod = item->node->schema->module;
4209 name = item->node->schema->name;
4210 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004211 case LYXP_NODE_META:
4212 mod = ((struct lyd_meta *)item->node)->annotation->module;
4213 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004214 break;
4215 }
4216
4217 if (mod && name) {
4218 switch (set->format) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02004219 case LY_PREF_SCHEMA:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004220 rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name);
4221 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02004222 case LY_PREF_JSON:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004223 rc = asprintf(&str, "%s:%s", mod->name, name);
4224 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02004225 case LY_PREF_XML:
Michal Vasko9409ef62019-09-12 11:47:17 +02004226 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004227 }
4228 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4229 set_fill_string(set, str, strlen(str));
4230 free(str);
4231 } else {
4232 set_fill_string(set, "", 0);
4233 }
4234
4235 return LY_SUCCESS;
4236}
4237
4238/**
4239 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4240 * with the namespace of the node from the argument or the context.
4241 *
4242 * @param[in] args Array of arguments.
4243 * @param[in] arg_count Count of elements in @p args.
4244 * @param[in,out] set Context and result set at the same time.
4245 * @param[in] options XPath options.
4246 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4247 */
4248static LY_ERR
4249xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4250{
4251 struct lyxp_set_node *item;
4252 struct lys_module *mod;
4253 /* suppress unused variable warning */
4254 (void)options;
4255
4256 if (options & LYXP_SCNODE_ALL) {
4257 set_scnode_clear_ctx(set);
4258 return LY_SUCCESS;
4259 }
4260
4261 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004262 if (args[0]->type != LYXP_SET_NODE_SET) {
4263 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
4264 "namespace-uri(node-set?)");
4265 return LY_EVALID;
4266 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004267 set_fill_string(set, "", 0);
4268 return LY_SUCCESS;
4269 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004270
4271 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004272 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004273
4274 item = &args[0]->val.nodes[0];
4275 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004276 if (set->type != LYXP_SET_NODE_SET) {
4277 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
4278 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004279 } else if (!set->used) {
4280 set_fill_string(set, "", 0);
4281 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004282 }
4283
4284 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004285 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004286
4287 item = &set->val.nodes[0];
4288 }
4289
4290 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004291 case LYXP_NODE_NONE:
4292 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004293 case LYXP_NODE_ROOT:
4294 case LYXP_NODE_ROOT_CONFIG:
4295 case LYXP_NODE_TEXT:
4296 set_fill_string(set, "", 0);
4297 break;
4298 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004299 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004300 if (item->type == LYXP_NODE_ELEM) {
4301 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004302 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004303 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004304 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004305 }
4306
4307 set_fill_string(set, mod->ns, strlen(mod->ns));
4308 break;
4309 }
4310
4311 return LY_SUCCESS;
4312}
4313
4314/**
4315 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4316 * with only nodes from the context. In practice it either leaves the context
4317 * as it is or returns an empty node set.
4318 *
4319 * @param[in] args Array of arguments.
4320 * @param[in] arg_count Count of elements in @p args.
4321 * @param[in,out] set Context and result set at the same time.
4322 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004323 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004324 */
4325static LY_ERR
4326xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4327{
4328 if (options & LYXP_SCNODE_ALL) {
4329 set_scnode_clear_ctx(set);
4330 return LY_SUCCESS;
4331 }
4332
4333 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004334 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004335 }
4336 return LY_SUCCESS;
4337}
4338
4339/**
4340 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4341 * with normalized value (no leading, trailing, double white spaces) of the node
4342 * from the argument or the context.
4343 *
4344 * @param[in] args Array of arguments.
4345 * @param[in] arg_count Count of elements in @p args.
4346 * @param[in,out] set Context and result set at the same time.
4347 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004348 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004349 */
4350static LY_ERR
4351xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4352{
4353 uint16_t i, new_used;
4354 char *new;
4355 int have_spaces = 0, space_before = 0;
4356 struct lysc_node_leaf *sleaf;
4357 LY_ERR rc = LY_SUCCESS;
4358
4359 if (options & LYXP_SCNODE_ALL) {
4360 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4361 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4362 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 +02004363 } else if (!warn_is_string_type(sleaf->type)) {
4364 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004365 }
4366 }
4367 set_scnode_clear_ctx(set);
4368 return rc;
4369 }
4370
4371 if (arg_count) {
4372 set_fill_set(set, args[0]);
4373 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004374 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004375 LY_CHECK_RET(rc);
4376
4377 /* is there any normalization necessary? */
4378 for (i = 0; set->val.str[i]; ++i) {
4379 if (is_xmlws(set->val.str[i])) {
4380 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4381 have_spaces = 1;
4382 break;
4383 }
4384 space_before = 1;
4385 } else {
4386 space_before = 0;
4387 }
4388 }
4389
4390 /* yep, there is */
4391 if (have_spaces) {
4392 /* it's enough, at least one character will go, makes space for ending '\0' */
4393 new = malloc(strlen(set->val.str) * sizeof(char));
4394 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4395 new_used = 0;
4396
4397 space_before = 0;
4398 for (i = 0; set->val.str[i]; ++i) {
4399 if (is_xmlws(set->val.str[i])) {
4400 if ((i == 0) || space_before) {
4401 space_before = 1;
4402 continue;
4403 } else {
4404 space_before = 1;
4405 }
4406 } else {
4407 space_before = 0;
4408 }
4409
4410 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4411 ++new_used;
4412 }
4413
4414 /* at worst there is one trailing space now */
4415 if (new_used && is_xmlws(new[new_used - 1])) {
4416 --new_used;
4417 }
4418
4419 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4420 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4421 new[new_used] = '\0';
4422
4423 free(set->val.str);
4424 set->val.str = new;
4425 }
4426
4427 return LY_SUCCESS;
4428}
4429
4430/**
4431 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4432 * with the argument converted to boolean and logically inverted.
4433 *
4434 * @param[in] args Array of arguments.
4435 * @param[in] arg_count Count of elements in @p args.
4436 * @param[in,out] set Context and result set at the same time.
4437 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004438 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004439 */
4440static LY_ERR
4441xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4442{
4443 if (options & LYXP_SCNODE_ALL) {
4444 set_scnode_clear_ctx(set);
4445 return LY_SUCCESS;
4446 }
4447
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004448 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004449 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004450 set_fill_boolean(set, 0);
4451 } else {
4452 set_fill_boolean(set, 1);
4453 }
4454
4455 return LY_SUCCESS;
4456}
4457
4458/**
4459 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4460 * with the number representation of either the argument or the context.
4461 *
4462 * @param[in] args Array of arguments.
4463 * @param[in] arg_count Count of elements in @p args.
4464 * @param[in,out] set Context and result set at the same time.
4465 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004466 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004467 */
4468static LY_ERR
4469xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4470{
4471 LY_ERR rc;
4472
4473 if (options & LYXP_SCNODE_ALL) {
4474 set_scnode_clear_ctx(set);
4475 return LY_SUCCESS;
4476 }
4477
4478 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004479 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004480 LY_CHECK_RET(rc);
4481 set_fill_set(set, args[0]);
4482 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004483 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004484 LY_CHECK_RET(rc);
4485 }
4486
4487 return LY_SUCCESS;
4488}
4489
4490/**
4491 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4492 * with the context position.
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
4501xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4502{
4503 if (options & LYXP_SCNODE_ALL) {
4504 set_scnode_clear_ctx(set);
4505 return LY_SUCCESS;
4506 }
4507
Michal Vasko03ff5a72019-09-11 13:49:33 +02004508 if (set->type != LYXP_SET_NODE_SET) {
4509 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
4510 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004511 } else if (!set->used) {
4512 set_fill_number(set, 0);
4513 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004514 }
4515
4516 set_fill_number(set, set->ctx_pos);
4517
4518 /* UNUSED in 'Release' build type */
4519 (void)options;
4520 return LY_SUCCESS;
4521}
4522
4523/**
4524 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4525 * depending on whether the second argument regex matches the first argument string. For details refer to
4526 * YANG 1.1 RFC section 10.2.1.
4527 *
4528 * @param[in] args Array of arguments.
4529 * @param[in] arg_count Count of elements in @p args.
4530 * @param[in,out] set Context and result set at the same time.
4531 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004532 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004533 */
4534static LY_ERR
4535xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4536{
4537 struct lysc_pattern **patterns = NULL, **pattern;
4538 struct lysc_node_leaf *sleaf;
4539 char *path;
4540 LY_ERR rc = LY_SUCCESS;
4541 struct ly_err_item *err;
4542
4543 if (options & LYXP_SCNODE_ALL) {
4544 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4545 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4546 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 +02004547 } else if (!warn_is_string_type(sleaf->type)) {
4548 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004549 }
4550 }
4551
4552 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4553 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4554 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 +02004555 } else if (!warn_is_string_type(sleaf->type)) {
4556 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004557 }
4558 }
4559 set_scnode_clear_ctx(set);
4560 return rc;
4561 }
4562
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004563 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004564 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004565 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004566 LY_CHECK_RET(rc);
4567
4568 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4569 *pattern = malloc(sizeof **pattern);
4570 path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0);
4571 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4572 free(path);
4573 if (rc != LY_SUCCESS) {
4574 LY_ARRAY_FREE(patterns);
4575 return rc;
4576 }
4577
4578 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4579 pcre2_code_free((*pattern)->code);
4580 free(*pattern);
4581 LY_ARRAY_FREE(patterns);
4582 if (rc && (rc != LY_EVALID)) {
4583 ly_err_print(err);
4584 ly_err_free(err);
4585 return rc;
4586 }
4587
4588 if (rc == LY_EVALID) {
4589 ly_err_free(err);
4590 set_fill_boolean(set, 0);
4591 } else {
4592 set_fill_boolean(set, 1);
4593 }
4594
4595 return LY_SUCCESS;
4596}
4597
4598/**
4599 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4600 * with the rounded first argument. For details refer to
4601 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4602 *
4603 * @param[in] args Array of arguments.
4604 * @param[in] arg_count Count of elements in @p args.
4605 * @param[in,out] set Context and result set at the same time.
4606 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004607 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004608 */
4609static LY_ERR
4610xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4611{
4612 struct lysc_node_leaf *sleaf;
4613 LY_ERR rc = LY_SUCCESS;
4614
4615 if (options & LYXP_SCNODE_ALL) {
4616 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4617 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004618 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4619 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 +02004620 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4621 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004622 }
4623 set_scnode_clear_ctx(set);
4624 return rc;
4625 }
4626
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004627 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004628 LY_CHECK_RET(rc);
4629
4630 /* cover only the cases where floor can't be used */
4631 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4632 set_fill_number(set, -0.0f);
4633 } else {
4634 args[0]->val.num += 0.5;
4635 rc = xpath_floor(args, 1, args[0], options);
4636 LY_CHECK_RET(rc);
4637 set_fill_number(set, args[0]->val.num);
4638 }
4639
4640 return LY_SUCCESS;
4641}
4642
4643/**
4644 * @brief Execute the XPath starts-with(string, string) function.
4645 * Returns LYXP_SET_BOOLEAN whether the second argument is
4646 * the prefix of the first or not.
4647 *
4648 * @param[in] args Array of arguments.
4649 * @param[in] arg_count Count of elements in @p args.
4650 * @param[in,out] set Context and result set at the same time.
4651 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004652 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004653 */
4654static LY_ERR
4655xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4656{
4657 struct lysc_node_leaf *sleaf;
4658 LY_ERR rc = LY_SUCCESS;
4659
4660 if (options & LYXP_SCNODE_ALL) {
4661 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4662 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4663 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 +02004664 } else if (!warn_is_string_type(sleaf->type)) {
4665 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004666 }
4667 }
4668
4669 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4670 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4671 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 +02004672 } else if (!warn_is_string_type(sleaf->type)) {
4673 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004674 }
4675 }
4676 set_scnode_clear_ctx(set);
4677 return rc;
4678 }
4679
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004680 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004681 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004682 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004683 LY_CHECK_RET(rc);
4684
4685 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4686 set_fill_boolean(set, 0);
4687 } else {
4688 set_fill_boolean(set, 1);
4689 }
4690
4691 return LY_SUCCESS;
4692}
4693
4694/**
4695 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4696 * with the string representation of either the argument or the context.
4697 *
4698 * @param[in] args Array of arguments.
4699 * @param[in] arg_count Count of elements in @p args.
4700 * @param[in,out] set Context and result set at the same time.
4701 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004702 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004703 */
4704static LY_ERR
4705xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4706{
4707 LY_ERR rc;
4708
4709 if (options & LYXP_SCNODE_ALL) {
4710 set_scnode_clear_ctx(set);
4711 return LY_SUCCESS;
4712 }
4713
4714 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004715 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004716 LY_CHECK_RET(rc);
4717 set_fill_set(set, args[0]);
4718 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004719 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004720 LY_CHECK_RET(rc);
4721 }
4722
4723 return LY_SUCCESS;
4724}
4725
4726/**
4727 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4728 * with the length of the string in 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
4737xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4738{
4739 struct lysc_node_leaf *sleaf;
4740 LY_ERR rc = LY_SUCCESS;
4741
4742 if (options & LYXP_SCNODE_ALL) {
4743 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4744 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4745 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 +02004746 } else if (!warn_is_string_type(sleaf->type)) {
4747 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004748 }
4749 }
4750 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4751 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4752 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 +02004753 } else if (!warn_is_string_type(sleaf->type)) {
4754 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004755 }
4756 }
4757 set_scnode_clear_ctx(set);
4758 return rc;
4759 }
4760
4761 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004762 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004763 LY_CHECK_RET(rc);
4764 set_fill_number(set, strlen(args[0]->val.str));
4765 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004766 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004767 LY_CHECK_RET(rc);
4768 set_fill_number(set, strlen(set->val.str));
4769 }
4770
4771 return LY_SUCCESS;
4772}
4773
4774/**
4775 * @brief Execute the XPath substring(string, number, number?) function.
4776 * Returns LYXP_SET_STRING substring of the first argument starting
4777 * on the second argument index ending on the third argument index,
4778 * indexed from 1. For exact definition refer to
4779 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4780 *
4781 * @param[in] args Array of arguments.
4782 * @param[in] arg_count Count of elements in @p args.
4783 * @param[in,out] set Context and result set at the same time.
4784 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004785 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004786 */
4787static LY_ERR
4788xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4789{
4790 int start, len;
4791 uint16_t str_start, str_len, pos;
4792 struct lysc_node_leaf *sleaf;
4793 LY_ERR rc = LY_SUCCESS;
4794
4795 if (options & LYXP_SCNODE_ALL) {
4796 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4797 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4798 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 +02004799 } else if (!warn_is_string_type(sleaf->type)) {
4800 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004801 }
4802 }
4803
4804 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4805 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4806 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 +02004807 } else if (!warn_is_numeric_type(sleaf->type)) {
4808 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004809 }
4810 }
4811
4812 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET)
Radek Krejci0f969882020-08-21 16:56:47 +02004813 && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004814 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4815 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 +02004816 } else if (!warn_is_numeric_type(sleaf->type)) {
4817 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004818 }
4819 }
4820 set_scnode_clear_ctx(set);
4821 return rc;
4822 }
4823
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004824 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004825 LY_CHECK_RET(rc);
4826
4827 /* start */
4828 if (xpath_round(&args[1], 1, args[1], options)) {
4829 return -1;
4830 }
4831 if (isfinite(args[1]->val.num)) {
4832 start = args[1]->val.num - 1;
4833 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
4834 start = INT_MIN;
4835 } else {
4836 start = INT_MAX;
4837 }
4838
4839 /* len */
4840 if (arg_count == 3) {
4841 rc = xpath_round(&args[2], 1, args[2], options);
4842 LY_CHECK_RET(rc);
4843 if (isfinite(args[2]->val.num)) {
4844 len = args[2]->val.num;
4845 } else if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
4846 len = 0;
4847 } else {
4848 len = INT_MAX;
4849 }
4850 } else {
4851 len = INT_MAX;
4852 }
4853
4854 /* find matching character positions */
4855 str_start = 0;
4856 str_len = 0;
4857 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4858 if (pos < start) {
4859 ++str_start;
4860 } else if (pos < start + len) {
4861 ++str_len;
4862 } else {
4863 break;
4864 }
4865 }
4866
4867 set_fill_string(set, args[0]->val.str + str_start, str_len);
4868 return LY_SUCCESS;
4869}
4870
4871/**
4872 * @brief Execute the XPath substring-after(string, string) function.
4873 * Returns LYXP_SET_STRING with the string succeeding the occurance
4874 * of the second argument in the first or an empty string.
4875 *
4876 * @param[in] args Array of arguments.
4877 * @param[in] arg_count Count of elements in @p args.
4878 * @param[in,out] set Context and result set at the same time.
4879 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004880 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004881 */
4882static LY_ERR
4883xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4884{
4885 char *ptr;
4886 struct lysc_node_leaf *sleaf;
4887 LY_ERR rc = LY_SUCCESS;
4888
4889 if (options & LYXP_SCNODE_ALL) {
4890 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4891 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4892 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 +02004893 } else if (!warn_is_string_type(sleaf->type)) {
4894 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004895 }
4896 }
4897
4898 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4899 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4900 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 +02004901 } else if (!warn_is_string_type(sleaf->type)) {
4902 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004903 }
4904 }
4905 set_scnode_clear_ctx(set);
4906 return rc;
4907 }
4908
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004909 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004910 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004911 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004912 LY_CHECK_RET(rc);
4913
4914 ptr = strstr(args[0]->val.str, args[1]->val.str);
4915 if (ptr) {
4916 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4917 } else {
4918 set_fill_string(set, "", 0);
4919 }
4920
4921 return LY_SUCCESS;
4922}
4923
4924/**
4925 * @brief Execute the XPath substring-before(string, string) function.
4926 * Returns LYXP_SET_STRING with the string preceding the occurance
4927 * of the second argument in the first or an empty string.
4928 *
4929 * @param[in] args Array of arguments.
4930 * @param[in] arg_count Count of elements in @p args.
4931 * @param[in,out] set Context and result set at the same time.
4932 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004933 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004934 */
4935static LY_ERR
4936xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4937{
4938 char *ptr;
4939 struct lysc_node_leaf *sleaf;
4940 LY_ERR rc = LY_SUCCESS;
4941
4942 if (options & LYXP_SCNODE_ALL) {
4943 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4944 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4945 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 +02004946 } else if (!warn_is_string_type(sleaf->type)) {
4947 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004948 }
4949 }
4950
4951 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4952 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4953 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 +02004954 } else if (!warn_is_string_type(sleaf->type)) {
4955 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004956 }
4957 }
4958 set_scnode_clear_ctx(set);
4959 return rc;
4960 }
4961
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004962 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004963 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004964 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004965 LY_CHECK_RET(rc);
4966
4967 ptr = strstr(args[0]->val.str, args[1]->val.str);
4968 if (ptr) {
4969 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4970 } else {
4971 set_fill_string(set, "", 0);
4972 }
4973
4974 return LY_SUCCESS;
4975}
4976
4977/**
4978 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
4979 * with the sum of all the nodes in the context.
4980 *
4981 * @param[in] args Array of arguments.
4982 * @param[in] arg_count Count of elements in @p args.
4983 * @param[in,out] set Context and result set at the same time.
4984 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004985 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004986 */
4987static LY_ERR
4988xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4989{
4990 long double num;
4991 char *str;
4992 uint16_t i;
4993 struct lyxp_set set_item;
4994 struct lysc_node_leaf *sleaf;
4995 LY_ERR rc = LY_SUCCESS;
4996
4997 if (options & LYXP_SCNODE_ALL) {
4998 if (args[0]->type == LYXP_SET_SCNODE_SET) {
4999 for (i = 0; i < args[0]->used; ++i) {
5000 if (args[0]->val.scnodes[i].in_ctx == 1) {
5001 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5002 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5003 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
5004 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005005 } else if (!warn_is_numeric_type(sleaf->type)) {
5006 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005007 }
5008 }
5009 }
5010 }
5011 set_scnode_clear_ctx(set);
5012 return rc;
5013 }
5014
5015 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005016
5017 if (args[0]->type != LYXP_SET_NODE_SET) {
5018 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
5019 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005020 } else if (!args[0]->used) {
5021 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005022 }
5023
Michal Vasko5c4e5892019-11-14 12:31:38 +01005024 set_init(&set_item, set);
5025
Michal Vasko03ff5a72019-09-11 13:49:33 +02005026 set_item.type = LYXP_SET_NODE_SET;
5027 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5028 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5029
5030 set_item.used = 1;
5031 set_item.size = 1;
5032
5033 for (i = 0; i < args[0]->used; ++i) {
5034 set_item.val.nodes[0] = args[0]->val.nodes[i];
5035
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005036 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005037 LY_CHECK_RET(rc);
5038 num = cast_string_to_number(str);
5039 free(str);
5040 set->val.num += num;
5041 }
5042
5043 free(set_item.val.nodes);
5044
5045 return LY_SUCCESS;
5046}
5047
5048/**
5049 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5050 * with the text content of the nodes in the context.
5051 *
5052 * @param[in] args Array of arguments.
5053 * @param[in] arg_count Count of elements in @p args.
5054 * @param[in,out] set Context and result set at the same time.
5055 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005056 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005057 */
5058static LY_ERR
5059xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5060{
5061 uint32_t i;
5062
5063 if (options & LYXP_SCNODE_ALL) {
5064 set_scnode_clear_ctx(set);
5065 return LY_SUCCESS;
5066 }
5067
Michal Vasko03ff5a72019-09-11 13:49:33 +02005068 if (set->type != LYXP_SET_NODE_SET) {
5069 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5070 return LY_EVALID;
5071 }
5072
5073 for (i = 0; i < set->used;) {
5074 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005075 case LYXP_NODE_NONE:
5076 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005077 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005078 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5079 set->val.nodes[i].type = LYXP_NODE_TEXT;
5080 ++i;
5081 break;
5082 }
Radek Krejci0f969882020-08-21 16:56:47 +02005083 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005084 case LYXP_NODE_ROOT:
5085 case LYXP_NODE_ROOT_CONFIG:
5086 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005087 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005088 set_remove_node(set, i);
5089 break;
5090 }
5091 }
5092
5093 return LY_SUCCESS;
5094}
5095
5096/**
5097 * @brief Execute the XPath translate(string, string, string) function.
5098 * Returns LYXP_SET_STRING with the first argument with the characters
5099 * from the second argument replaced by those on the corresponding
5100 * positions in the third argument.
5101 *
5102 * @param[in] args Array of arguments.
5103 * @param[in] arg_count Count of elements in @p args.
5104 * @param[in,out] set Context and result set at the same time.
5105 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005106 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005107 */
5108static LY_ERR
5109xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5110{
5111 uint16_t i, j, new_used;
5112 char *new;
5113 int found, have_removed;
5114 struct lysc_node_leaf *sleaf;
5115 LY_ERR rc = LY_SUCCESS;
5116
5117 if (options & LYXP_SCNODE_ALL) {
5118 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5119 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5120 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 +02005121 } else if (!warn_is_string_type(sleaf->type)) {
5122 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005123 }
5124 }
5125
5126 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5127 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5128 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 +02005129 } else if (!warn_is_string_type(sleaf->type)) {
5130 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005131 }
5132 }
5133
5134 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5135 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5136 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 +02005137 } else if (!warn_is_string_type(sleaf->type)) {
5138 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005139 }
5140 }
5141 set_scnode_clear_ctx(set);
5142 return rc;
5143 }
5144
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005145 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005146 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005147 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005148 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005149 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005150 LY_CHECK_RET(rc);
5151
5152 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5153 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5154 new_used = 0;
5155
5156 have_removed = 0;
5157 for (i = 0; args[0]->val.str[i]; ++i) {
5158 found = 0;
5159
5160 for (j = 0; args[1]->val.str[j]; ++j) {
5161 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5162 /* removing this char */
5163 if (j >= strlen(args[2]->val.str)) {
5164 have_removed = 1;
5165 found = 1;
5166 break;
5167 }
5168 /* replacing this char */
5169 new[new_used] = args[2]->val.str[j];
5170 ++new_used;
5171 found = 1;
5172 break;
5173 }
5174 }
5175
5176 /* copying this char */
5177 if (!found) {
5178 new[new_used] = args[0]->val.str[i];
5179 ++new_used;
5180 }
5181 }
5182
5183 if (have_removed) {
5184 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5185 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5186 }
5187 new[new_used] = '\0';
5188
Michal Vaskod3678892020-05-21 10:06:58 +02005189 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005190 set->type = LYXP_SET_STRING;
5191 set->val.str = new;
5192
5193 return LY_SUCCESS;
5194}
5195
5196/**
5197 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5198 * with true value.
5199 *
5200 * @param[in] args Array of arguments.
5201 * @param[in] arg_count Count of elements in @p args.
5202 * @param[in,out] set Context and result set at the same time.
5203 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005204 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005205 */
5206static LY_ERR
5207xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5208{
5209 if (options & LYXP_SCNODE_ALL) {
5210 set_scnode_clear_ctx(set);
5211 return LY_SUCCESS;
5212 }
5213
5214 set_fill_boolean(set, 1);
5215 return LY_SUCCESS;
5216}
5217
5218/*
5219 * moveto functions
5220 *
5221 * They and only they actually change the context (set).
5222 */
5223
5224/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005225 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005226 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005227 * XPath @p set is expected to be a (sc)node set!
5228 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005229 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5230 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
5231 * @param[in] set Set with XPath context.
5232 * @param[out] moveto_mod Expected module of a matching node.
5233 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005234 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005235static LY_ERR
5236moveto_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 +02005237{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005238 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005239 const char *ptr;
5240 int pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005241
Michal Vasko2104e9f2020-03-06 08:23:25 +01005242 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5243
Michal Vasko6346ece2019-09-24 13:12:53 +02005244 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5245 /* specific module */
5246 pref_len = ptr - *qname;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02005247 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, (void *)set->local_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005248
Michal Vasko004d3152020-06-11 19:59:22 +02005249 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005250 if (!mod || !mod->implemented) {
Michal Vasko2104e9f2020-03-06 08:23:25 +01005251 if (set->type == LYXP_SET_SCNODE_SET) {
5252 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname);
5253 } else {
5254 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname);
5255 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005256 return LY_EVALID;
5257 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005258
Michal Vasko6346ece2019-09-24 13:12:53 +02005259 *qname += pref_len + 1;
5260 *qname_len -= pref_len + 1;
5261 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5262 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005263 mod = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02005264 } else if (set->type == LYXP_SET_SCNODE_SET) {
5265 /* current node module */
5266 mod = set->ctx_scnode->module;
Michal Vasko6346ece2019-09-24 13:12:53 +02005267 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005268 /* current node module */
5269 mod = set->ctx_node->schema->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005270 }
5271
Michal Vasko6346ece2019-09-24 13:12:53 +02005272 *moveto_mod = mod;
5273 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005274}
5275
5276/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005277 * @brief Move context @p set to the root. Handles absolute path.
5278 * Result is LYXP_SET_NODE_SET.
5279 *
5280 * @param[in,out] set Set to use.
5281 * @param[in] options Xpath options.
5282 */
5283static void
5284moveto_root(struct lyxp_set *set, int options)
5285{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005286 if (!set) {
5287 return;
5288 }
5289
5290 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005291 set_scnode_clear_ctx(set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01005292 lyxp_set_scnode_insert_node(set, NULL, set->root_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005293 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005294 set->type = LYXP_SET_NODE_SET;
5295 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005296 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005297 }
5298}
5299
5300/**
Michal Vaskoa1424542019-11-14 16:08:52 +01005301 * @brief Check whether a node has some unresolved "when".
5302 *
5303 * @param[in] node Node to check.
5304 * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when")
5305 */
5306static LY_ERR
5307moveto_when_check(const struct lyd_node *node)
5308{
5309 const struct lysc_node *schema;
5310
5311 if (!node) {
5312 return LY_SUCCESS;
5313 }
5314
5315 schema = node->schema;
5316 do {
5317 if (schema->when && !(node->flags & LYD_WHEN_TRUE)) {
5318 return LY_EINCOMPLETE;
5319 }
5320 schema = schema->parent;
5321 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
5322
5323 return LY_SUCCESS;
5324}
5325
5326/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005327 * @brief Check @p node as a part of NameTest processing.
5328 *
5329 * @param[in] node Node to check.
5330 * @param[in] root_type XPath root node type.
Michal Vasko6b26e742020-07-17 15:02:10 +02005331 * @param[in] context_op XPath operation parent.
Michal Vaskod3678892020-05-21 10:06:58 +02005332 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
5333 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005334 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5335 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005336 */
5337static LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +02005338moveto_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 +02005339 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005340{
5341 /* module check */
5342 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005343 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005344 }
5345
Michal Vasko5c4e5892019-11-14 12:31:38 +01005346 /* context check */
5347 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005348 return LY_EINVAL;
Michal Vasko6b26e742020-07-17 15:02:10 +02005349 } else if (context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node->schema != context_op)) {
5350 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005351 }
5352
5353 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005354 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005355 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005356 }
5357
Michal Vaskoa1424542019-11-14 16:08:52 +01005358 /* when check */
5359 if (moveto_when_check(node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005360 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005361 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005362
5363 /* match */
5364 return LY_SUCCESS;
5365}
5366
5367/**
5368 * @brief Check @p node as a part of schema NameTest processing.
5369 *
5370 * @param[in] node Schema node to check.
5371 * @param[in] root_type XPath root node type.
Michal Vasko6b26e742020-07-17 15:02:10 +02005372 * @param[in] context_op XPath operation parent.
Michal Vaskod3678892020-05-21 10:06:58 +02005373 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
5374 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005375 * @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 +02005376 */
5377static LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +02005378moveto_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 +02005379 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005380{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005381 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005382 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005383 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005384 }
5385
5386 /* context check */
5387 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
5388 return LY_EINVAL;
Michal Vasko6b26e742020-07-17 15:02:10 +02005389 } else if (context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != context_op)) {
5390 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005391 }
5392
5393 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005394 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005395 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005396 }
5397
5398 /* match */
5399 return LY_SUCCESS;
5400}
5401
5402/**
Michal Vaskod3678892020-05-21 10:06:58 +02005403 * @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 +02005404 *
5405 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005406 * @param[in] mod Matching node module, NULL for any.
5407 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005408 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5409 */
5410static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005411moveto_node(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005412{
Michal Vaskof03ed032020-03-04 13:31:44 +01005413 uint32_t i;
Michal Vasko6346ece2019-09-24 13:12:53 +02005414 int replaced;
Michal Vaskod3678892020-05-21 10:06:58 +02005415 const struct lyd_node *siblings, *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005416 LY_ERR rc;
5417
Michal Vaskod3678892020-05-21 10:06:58 +02005418 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005419 return LY_SUCCESS;
5420 }
5421
5422 if (set->type != LYXP_SET_NODE_SET) {
5423 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5424 return LY_EVALID;
5425 }
5426
Michal Vasko03ff5a72019-09-11 13:49:33 +02005427 for (i = 0; i < set->used; ) {
5428 replaced = 0;
5429
5430 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 +01005431 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005432
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005433 /* search in all the trees */
Michal Vaskod3678892020-05-21 10:06:58 +02005434 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005435 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005436 /* search in children */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005437 siblings = lyd_node_children(set->val.nodes[i].node, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02005438 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005439
Michal Vaskod3678892020-05-21 10:06:58 +02005440 for (sub = siblings; sub; sub = sub->next) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005441 rc = moveto_node_check(sub, set->root_type, set->context_op, ncname, mod);
Michal Vaskod3678892020-05-21 10:06:58 +02005442 if (rc == LY_SUCCESS) {
5443 if (!replaced) {
5444 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5445 replaced = 1;
5446 } else {
5447 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005448 }
Michal Vaskod3678892020-05-21 10:06:58 +02005449 ++i;
5450 } else if (rc == LY_EINCOMPLETE) {
5451 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005452 }
5453 }
5454
5455 if (!replaced) {
5456 /* no match */
5457 set_remove_node(set, i);
5458 }
5459 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005460
5461 return LY_SUCCESS;
5462}
5463
5464/**
Michal Vaskod3678892020-05-21 10:06:58 +02005465 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5466 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005467 *
5468 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005469 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005470 * @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 +02005471 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5472 */
5473static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02005474moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates)
Michal Vaskod3678892020-05-21 10:06:58 +02005475{
Michal Vasko004d3152020-06-11 19:59:22 +02005476 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005477 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005478 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005479 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005480
Michal Vasko004d3152020-06-11 19:59:22 +02005481 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005482
5483 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005484 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005485 }
5486
5487 if (set->type != LYXP_SET_NODE_SET) {
5488 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 +02005489 ret = LY_EVALID;
5490 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005491 }
5492
5493 /* context check for all the nodes since we have the schema node */
5494 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5495 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005496 goto cleanup;
Radek Krejci0f969882020-08-21 16:56:47 +02005497 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko6b26e742020-07-17 15:02:10 +02005498 && (scnode != set->context_op)) {
5499 lyxp_set_free_content(set);
5500 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005501 }
5502
5503 /* create specific data instance if needed */
5504 if (scnode->nodetype == LYS_LIST) {
5505 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5506 } else if (scnode->nodetype == LYS_LEAFLIST) {
5507 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005508 }
5509
5510 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005511 siblings = NULL;
5512
5513 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5514 assert(!set->val.nodes[i].node);
5515
5516 /* search in all the trees */
5517 siblings = set->tree;
5518 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5519 /* search in children */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005520 siblings = lyd_node_children(set->val.nodes[i].node, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02005521 }
5522
5523 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005524 if (inst) {
5525 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005526 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005527 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005528 }
5529
5530 /* when check */
5531 if (sub && moveto_when_check(sub)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005532 ret = LY_EINCOMPLETE;
5533 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005534 }
5535
5536 if (sub) {
5537 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005538 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005539 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005540 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005541 /* no match */
5542 set_remove_node(set, i);
5543 }
5544 }
5545
Michal Vasko004d3152020-06-11 19:59:22 +02005546cleanup:
5547 lyd_free_tree(inst);
5548 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005549}
5550
5551/**
5552 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5553 *
5554 * @param[in,out] set Set to use.
5555 * @param[in] mod Matching node module, NULL for any.
5556 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005557 * @param[in] options XPath options.
5558 * @return LY_ERR
5559 */
5560static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005561moveto_scnode(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005562{
Michal Vaskocafad9d2019-11-07 15:20:03 +01005563 int i, orig_used, idx, temp_ctx = 0, getnext_opts;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005564 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005565 const struct lysc_node *iter, *start_parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005566
Michal Vaskod3678892020-05-21 10:06:58 +02005567 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005568 return LY_SUCCESS;
5569 }
5570
5571 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005572 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 +02005573 return LY_EVALID;
5574 }
5575
Michal Vaskocafad9d2019-11-07 15:20:03 +01005576 /* getnext opts */
5577 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
5578 if (options & LYXP_SCNODE_OUTPUT) {
5579 getnext_opts |= LYS_GETNEXT_OUTPUT;
5580 }
5581
Michal Vasko03ff5a72019-09-11 13:49:33 +02005582 orig_used = set->used;
5583 for (i = 0; i < orig_used; ++i) {
5584 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005585 if (set->val.scnodes[i].in_ctx != -2) {
5586 continue;
5587 }
5588
5589 /* remember context node */
5590 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005591 } else {
5592 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005593 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005594
5595 start_parent = set->val.scnodes[i].scnode;
5596
5597 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
Michal Vaskod3678892020-05-21 10:06:58 +02005598 /* it can actually be in any module, it's all <running>, but we know it's mod (if set),
Michal Vasko03ff5a72019-09-11 13:49:33 +02005599 * so use it directly (root node itself is useless in this case) */
5600 mod_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02005601 while (mod || (mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005602 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005603 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005604 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005605 if (!moveto_scnode_check(iter, set->root_type, set->context_op, ncname, mod)) {
Michal Vasko519fd602020-05-26 12:17:39 +02005606 idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005607 /* we need to prevent these nodes from being considered in this moveto */
5608 if ((idx < orig_used) && (idx > i)) {
5609 set->val.scnodes[idx].in_ctx = 2;
5610 temp_ctx = 1;
5611 }
5612 }
5613 }
5614
5615 if (!mod_idx) {
Michal Vaskod3678892020-05-21 10:06:58 +02005616 /* mod was specified, we are not going through the whole context */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005617 break;
5618 }
5619 /* next iteration */
Michal Vaskod3678892020-05-21 10:06:58 +02005620 mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005621 }
5622
Michal Vasko519fd602020-05-26 12:17:39 +02005623 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5624 iter = NULL;
5625 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005626 if (!moveto_scnode_check(iter, set->root_type, set->context_op, ncname, (mod ? mod : set->local_mod))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005627 idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005628 if ((idx < orig_used) && (idx > i)) {
5629 set->val.scnodes[idx].in_ctx = 2;
5630 temp_ctx = 1;
5631 }
5632 }
5633 }
5634 }
5635 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005636
5637 /* correct temporary in_ctx values */
5638 if (temp_ctx) {
5639 for (i = 0; i < orig_used; ++i) {
5640 if (set->val.scnodes[i].in_ctx == 2) {
5641 set->val.scnodes[i].in_ctx = 1;
5642 }
5643 }
5644 }
5645
5646 return LY_SUCCESS;
5647}
5648
5649/**
Michal Vaskod3678892020-05-21 10:06:58 +02005650 * @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 +02005651 * Context position aware.
5652 *
5653 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005654 * @param[in] mod Matching node module, NULL for any.
5655 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005656 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5657 */
5658static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005659moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005660{
5661 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005662 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005663 struct lyxp_set ret_set;
5664 LY_ERR rc;
5665
Michal Vaskod3678892020-05-21 10:06:58 +02005666 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005667 return LY_SUCCESS;
5668 }
5669
5670 if (set->type != LYXP_SET_NODE_SET) {
5671 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5672 return LY_EVALID;
5673 }
5674
Michal Vasko9f96a052020-03-10 09:41:45 +01005675 /* 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 +02005676 rc = moveto_node(set, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005677 LY_CHECK_RET(rc);
5678
Michal Vasko6346ece2019-09-24 13:12:53 +02005679 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005680 set_init(&ret_set, set);
5681 for (i = 0; i < set->used; ++i) {
5682
5683 /* TREE DFS */
5684 start = set->val.nodes[i].node;
5685 for (elem = next = start; elem; elem = next) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005686 rc = moveto_node_check(elem, set->root_type, set->context_op, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005687 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005688 /* add matching node into result set */
5689 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5690 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5691 /* the node is a duplicate, we'll process it later in the set */
5692 goto skip_children;
5693 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005694 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005695 return rc;
5696 } else if (rc == LY_EINVAL) {
5697 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005698 }
5699
5700 /* TREE DFS NEXT ELEM */
5701 /* select element for the next run - children first */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005702 next = lyd_node_children(elem, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005703 if (!next) {
5704skip_children:
5705 /* no children, so try siblings, but only if it's not the start,
5706 * that is considered to be the root and it's siblings are not traversed */
5707 if (elem != start) {
5708 next = elem->next;
5709 } else {
5710 break;
5711 }
5712 }
5713 while (!next) {
5714 /* no siblings, go back through the parents */
5715 if ((struct lyd_node *)elem->parent == start) {
5716 /* we are done, no next element to process */
5717 break;
5718 }
5719 /* parent is already processed, go to its sibling */
5720 elem = (struct lyd_node *)elem->parent;
5721 next = elem->next;
5722 }
5723 }
5724 }
5725
5726 /* make the temporary set the current one */
5727 ret_set.ctx_pos = set->ctx_pos;
5728 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005729 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005730 memcpy(set, &ret_set, sizeof *set);
5731
5732 return LY_SUCCESS;
5733}
5734
5735/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005736 * @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 +02005737 *
5738 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005739 * @param[in] mod Matching node module, NULL for any.
5740 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005741 * @param[in] options XPath options.
5742 * @return LY_ERR
5743 */
5744static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005745moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005746{
Michal Vasko6346ece2019-09-24 13:12:53 +02005747 int i, orig_used, idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005748 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005749 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005750
Michal Vaskod3678892020-05-21 10:06:58 +02005751 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005752 return LY_SUCCESS;
5753 }
5754
5755 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005756 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 +02005757 return LY_EVALID;
5758 }
5759
Michal Vasko03ff5a72019-09-11 13:49:33 +02005760 orig_used = set->used;
5761 for (i = 0; i < orig_used; ++i) {
5762 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005763 if (set->val.scnodes[i].in_ctx != -2) {
5764 continue;
5765 }
5766
5767 /* remember context node */
5768 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005769 } else {
5770 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005771 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005772
5773 /* TREE DFS */
5774 start = set->val.scnodes[i].scnode;
5775 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005776 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5777 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005778 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005779 }
5780
Michal Vasko6b26e742020-07-17 15:02:10 +02005781 rc = moveto_scnode_check(elem, set->root_type, set->context_op, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005782 if (!rc) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005783 if ((idx = lyxp_set_scnode_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) > -1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005784 set->val.scnodes[idx].in_ctx = 1;
5785 if (idx > i) {
5786 /* we will process it later in the set */
5787 goto skip_children;
5788 }
5789 } else {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005790 lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005791 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005792 } else if (rc == LY_EINVAL) {
5793 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005794 }
5795
5796next_iter:
5797 /* TREE DFS NEXT ELEM */
5798 /* select element for the next run - children first */
5799 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005800 if (!next) {
5801skip_children:
5802 /* no children, so try siblings, but only if it's not the start,
5803 * that is considered to be the root and it's siblings are not traversed */
5804 if (elem != start) {
5805 next = elem->next;
5806 } else {
5807 break;
5808 }
5809 }
5810 while (!next) {
5811 /* no siblings, go back through the parents */
5812 if (elem->parent == start) {
5813 /* we are done, no next element to process */
5814 break;
5815 }
5816 /* parent is already processed, go to its sibling */
5817 elem = elem->parent;
5818 next = elem->next;
5819 }
5820 }
5821 }
5822
5823 return LY_SUCCESS;
5824}
5825
5826/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005827 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005828 * Indirectly context position aware.
5829 *
5830 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005831 * @param[in] mod Matching metadata module, NULL for any.
5832 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005833 * @return LY_ERR
5834 */
5835static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005836moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005837{
5838 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005839 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005840 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005841
Michal Vaskod3678892020-05-21 10:06:58 +02005842 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005843 return LY_SUCCESS;
5844 }
5845
5846 if (set->type != LYXP_SET_NODE_SET) {
5847 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5848 return LY_EVALID;
5849 }
5850
Michal Vasko03ff5a72019-09-11 13:49:33 +02005851 for (i = 0; i < set->used; ) {
5852 replaced = 0;
5853
5854 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5855 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005856 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005857 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005858
5859 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005860 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005861 continue;
5862 }
5863
Michal Vaskod3678892020-05-21 10:06:58 +02005864 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005865 /* match */
5866 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005867 set->val.meta[i].meta = sub;
5868 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005869 /* pos does not change */
5870 replaced = 1;
5871 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005872 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 +02005873 }
5874 ++i;
5875 }
5876 }
5877 }
5878
5879 if (!replaced) {
5880 /* no match */
5881 set_remove_node(set, i);
5882 }
5883 }
5884
5885 return LY_SUCCESS;
5886}
5887
5888/**
5889 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005890 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005891 *
5892 * @param[in,out] set1 Set to use for the result.
5893 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005894 * @return LY_ERR
5895 */
5896static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005897moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005898{
5899 LY_ERR rc;
5900
Michal Vaskod3678892020-05-21 10:06:58 +02005901 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005902 LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
5903 return LY_EVALID;
5904 }
5905
5906 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005907 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005908 return LY_SUCCESS;
5909 }
5910
Michal Vaskod3678892020-05-21 10:06:58 +02005911 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005912 memcpy(set1, set2, sizeof *set1);
5913 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005914 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005915 return LY_SUCCESS;
5916 }
5917
5918 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005919 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005920
5921 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005922 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005923 LY_CHECK_RET(rc);
5924
5925 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005926 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005927
5928 return LY_SUCCESS;
5929}
5930
5931/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005932 * @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 +02005933 * Context position aware.
5934 *
5935 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005936 * @param[in] mod Matching metadata module, NULL for any.
5937 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005938 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5939 */
5940static int
Michal Vaskod3678892020-05-21 10:06:58 +02005941moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005942{
5943 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005944 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005945 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005946 struct lyxp_set *set_all_desc = NULL;
5947 LY_ERR rc;
5948
Michal Vaskod3678892020-05-21 10:06:58 +02005949 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005950 return LY_SUCCESS;
5951 }
5952
5953 if (set->type != LYXP_SET_NODE_SET) {
5954 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5955 return LY_EVALID;
5956 }
5957
Michal Vasko03ff5a72019-09-11 13:49:33 +02005958 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
5959 * but it likely won't be used much, so it's a waste of time */
5960 /* copy the context */
5961 set_all_desc = set_copy(set);
5962 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskod3678892020-05-21 10:06:58 +02005963 rc = moveto_node_alldesc(set_all_desc, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005964 if (rc != LY_SUCCESS) {
5965 lyxp_set_free(set_all_desc);
5966 return rc;
5967 }
5968 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005969 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005970 if (rc != LY_SUCCESS) {
5971 lyxp_set_free(set_all_desc);
5972 return rc;
5973 }
5974 lyxp_set_free(set_all_desc);
5975
Michal Vasko03ff5a72019-09-11 13:49:33 +02005976 for (i = 0; i < set->used; ) {
5977 replaced = 0;
5978
5979 /* only attributes of an elem can be in the result, skip all the rest,
5980 * we have all attributes qualified in lyd tree */
5981 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005982 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005983 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005984 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005985 continue;
5986 }
5987
Michal Vaskod3678892020-05-21 10:06:58 +02005988 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005989 /* match */
5990 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005991 set->val.meta[i].meta = sub;
5992 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005993 /* pos does not change */
5994 replaced = 1;
5995 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005996 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 +02005997 }
5998 ++i;
5999 }
6000 }
6001 }
6002
6003 if (!replaced) {
6004 /* no match */
6005 set_remove_node(set, i);
6006 }
6007 }
6008
6009 return LY_SUCCESS;
6010}
6011
6012/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006013 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6014 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006015 *
6016 * @param[in] parent Current parent.
6017 * @param[in] parent_pos Position of @p parent.
6018 * @param[in] parent_type Node type of @p parent.
6019 * @param[in,out] to_set Set to use.
6020 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006021 * @param[in] options XPath options.
6022 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6023 */
6024static LY_ERR
6025moveto_self_add_children_r(const struct lyd_node *parent, uint32_t parent_pos, enum lyxp_node_type parent_type,
Radek Krejci0f969882020-08-21 16:56:47 +02006026 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006027{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006028 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006029 LY_ERR rc;
6030
6031 switch (parent_type) {
6032 case LYXP_NODE_ROOT:
6033 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006034 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006035
Michal Vasko61ac2f62020-05-25 12:39:51 +02006036 /* add all top-level nodes as elements */
6037 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006038 break;
6039 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006040 /* add just the text node of this term element node */
6041 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006042 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6043 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6044 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006045 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006046 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006047
6048 /* add all the children of this node */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02006049 first = lyd_node_children(parent, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006050 break;
6051 default:
6052 LOGINT_RET(parent->schema->module->ctx);
6053 }
6054
Michal Vasko61ac2f62020-05-25 12:39:51 +02006055 /* add all top-level nodes as elements */
6056 LY_LIST_FOR(first, iter) {
6057 /* context check */
6058 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6059 continue;
6060 }
6061
6062 /* when check */
6063 if (moveto_when_check(iter)) {
6064 return LY_EINCOMPLETE;
6065 }
6066
6067 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6068 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6069
6070 /* also add all the children of this node, recursively */
6071 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6072 LY_CHECK_RET(rc);
6073 }
6074 }
6075
Michal Vasko03ff5a72019-09-11 13:49:33 +02006076 return LY_SUCCESS;
6077}
6078
6079/**
6080 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6081 * (or LYXP_SET_EMPTY). Context position aware.
6082 *
6083 * @param[in,out] set Set to use.
6084 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6085 * @param[in] options XPath options.
6086 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6087 */
6088static LY_ERR
6089moveto_self(struct lyxp_set *set, int all_desc, int options)
6090{
6091 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006092 struct lyxp_set ret_set;
6093 LY_ERR rc;
6094
Michal Vaskod3678892020-05-21 10:06:58 +02006095 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006096 return LY_SUCCESS;
6097 }
6098
6099 if (set->type != LYXP_SET_NODE_SET) {
6100 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6101 return LY_EVALID;
6102 }
6103
6104 /* nothing to do */
6105 if (!all_desc) {
6106 return LY_SUCCESS;
6107 }
6108
Michal Vasko03ff5a72019-09-11 13:49:33 +02006109 /* add all the children, they get added recursively */
6110 set_init(&ret_set, set);
6111 for (i = 0; i < set->used; ++i) {
6112 /* copy the current node to tmp */
6113 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6114
6115 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006116 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006117 continue;
6118 }
6119
Michal Vasko03ff5a72019-09-11 13:49:33 +02006120 /* add all the children */
6121 rc = moveto_self_add_children_r(set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, &ret_set,
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006122 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006123 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006124 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006125 return rc;
6126 }
6127 }
6128
6129 /* use the temporary set as the current one */
6130 ret_set.ctx_pos = set->ctx_pos;
6131 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006132 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006133 memcpy(set, &ret_set, sizeof *set);
6134
6135 return LY_SUCCESS;
6136}
6137
6138/**
6139 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6140 * (or LYXP_SET_EMPTY).
6141 *
6142 * @param[in,out] set Set to use.
6143 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6144 * @param[in] options XPath options.
6145 * @return LY_ERR
6146 */
6147static LY_ERR
6148moveto_scnode_self(struct lyxp_set *set, int all_desc, int options)
6149{
Michal Vasko519fd602020-05-26 12:17:39 +02006150 int getnext_opts;
6151 uint32_t i, mod_idx;
6152 const struct lysc_node *iter, *start_parent;
6153 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006154
Michal Vaskod3678892020-05-21 10:06:58 +02006155 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006156 return LY_SUCCESS;
6157 }
6158
6159 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006160 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 +02006161 return LY_EVALID;
6162 }
6163
6164 /* nothing to do */
6165 if (!all_desc) {
6166 return LY_SUCCESS;
6167 }
6168
Michal Vasko519fd602020-05-26 12:17:39 +02006169 /* getnext opts */
6170 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
6171 if (options & LYXP_SCNODE_OUTPUT) {
6172 getnext_opts |= LYS_GETNEXT_OUTPUT;
6173 }
6174
6175 /* add all the children, recursively as they are being added into the same set */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006176 for (i = 0; i < set->used; ++i) {
6177 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006178 if (set->val.scnodes[i].in_ctx != -2) {
6179 continue;
6180 }
6181
Michal Vasko519fd602020-05-26 12:17:39 +02006182 /* remember context node */
6183 set->val.scnodes[i].in_ctx = -1;
6184 } else {
6185 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006186 }
6187
Michal Vasko519fd602020-05-26 12:17:39 +02006188 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006189
Michal Vasko519fd602020-05-26 12:17:39 +02006190 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6191 /* it can actually be in any module, it's all <running> */
6192 mod_idx = 0;
6193 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6194 iter = NULL;
6195 /* module may not be implemented */
6196 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6197 /* context check */
6198 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6199 continue;
6200 }
6201
6202 lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
6203 /* throw away the insert index, we want to consider that node again, recursively */
6204 }
6205 }
6206
6207 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6208 iter = NULL;
6209 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006210 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006211 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006212 continue;
6213 }
6214
Michal Vasko519fd602020-05-26 12:17:39 +02006215 lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006216 }
6217 }
6218 }
6219
6220 return LY_SUCCESS;
6221}
6222
6223/**
6224 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6225 * (or LYXP_SET_EMPTY). Context position aware.
6226 *
6227 * @param[in] set Set to use.
6228 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6229 * @param[in] options XPath options.
6230 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6231 */
6232static LY_ERR
6233moveto_parent(struct lyxp_set *set, int all_desc, int options)
6234{
6235 LY_ERR rc;
6236 uint32_t i;
6237 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006238 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006239
Michal Vaskod3678892020-05-21 10:06:58 +02006240 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006241 return LY_SUCCESS;
6242 }
6243
6244 if (set->type != LYXP_SET_NODE_SET) {
6245 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6246 return LY_EVALID;
6247 }
6248
6249 if (all_desc) {
6250 /* <path>//.. == <path>//./.. */
6251 rc = moveto_self(set, 1, options);
6252 LY_CHECK_RET(rc);
6253 }
6254
Michal Vasko57eab132019-09-24 11:46:26 +02006255 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006256 node = set->val.nodes[i].node;
6257
6258 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6259 new_node = (struct lyd_node *)node->parent;
6260 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6261 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006262 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6263 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006264 if (!new_node) {
6265 LOGINT_RET(set->ctx);
6266 }
6267 } else {
6268 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006269 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006270 continue;
6271 }
6272
Michal Vaskoa1424542019-11-14 16:08:52 +01006273 /* when check */
6274 if (moveto_when_check(new_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006275 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006276 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006277
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006278 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006279 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006280 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006281
Michal Vasko03ff5a72019-09-11 13:49:33 +02006282 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006283 /* 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 +02006284 new_type = LYXP_NODE_ELEM;
6285 }
6286
Michal Vasko03ff5a72019-09-11 13:49:33 +02006287 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006288 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006289 } else {
6290 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006291 }
6292 }
6293
Michal Vasko2caefc12019-11-14 16:07:56 +01006294 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006295 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006296
6297 return LY_SUCCESS;
6298}
6299
6300/**
6301 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6302 * (or LYXP_SET_EMPTY).
6303 *
6304 * @param[in] set Set to use.
6305 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6306 * @param[in] options XPath options.
6307 * @return LY_ERR
6308 */
6309static LY_ERR
6310moveto_scnode_parent(struct lyxp_set *set, int all_desc, int options)
6311{
6312 int idx, i, orig_used, temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006313 const struct lysc_node *node, *new_node;
6314 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006315 LY_ERR rc;
6316
Michal Vaskod3678892020-05-21 10:06:58 +02006317 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006318 return LY_SUCCESS;
6319 }
6320
6321 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006322 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 +02006323 return LY_EVALID;
6324 }
6325
6326 if (all_desc) {
6327 /* <path>//.. == <path>//./.. */
6328 rc = moveto_scnode_self(set, 1, options);
6329 LY_CHECK_RET(rc);
6330 }
6331
Michal Vasko03ff5a72019-09-11 13:49:33 +02006332 orig_used = set->used;
6333 for (i = 0; i < orig_used; ++i) {
6334 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006335 if (set->val.scnodes[i].in_ctx != -2) {
6336 continue;
6337 }
6338
6339 /* remember context node */
6340 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01006341 } else {
6342 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006343 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006344
6345 node = set->val.scnodes[i].scnode;
6346
6347 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006348 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006349 } else {
6350 /* root does not have a parent */
6351 continue;
6352 }
6353
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006354 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006355 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006356 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006357
Michal Vasko03ff5a72019-09-11 13:49:33 +02006358 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006359 /* 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 +02006360 new_type = LYXP_NODE_ELEM;
6361 }
6362
Michal Vaskoecd62de2019-11-13 12:35:11 +01006363 idx = lyxp_set_scnode_insert_node(set, new_node, new_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006364 if ((idx < orig_used) && (idx > i)) {
6365 set->val.scnodes[idx].in_ctx = 2;
6366 temp_ctx = 1;
6367 }
6368 }
6369
6370 if (temp_ctx) {
6371 for (i = 0; i < orig_used; ++i) {
6372 if (set->val.scnodes[i].in_ctx == 2) {
6373 set->val.scnodes[i].in_ctx = 1;
6374 }
6375 }
6376 }
6377
6378 return LY_SUCCESS;
6379}
6380
6381/**
6382 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6383 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6384 *
6385 * @param[in,out] set1 Set to use for the result.
6386 * @param[in] set2 Set acting as the second operand for @p op.
6387 * @param[in] op Comparison operator to process.
6388 * @param[in] options XPath options.
6389 * @return LY_ERR
6390 */
6391static LY_ERR
6392moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, int options)
6393{
6394 /*
6395 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6396 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6397 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6398 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6399 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6400 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6401 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6402 *
6403 * '=' or '!='
6404 * BOOLEAN + BOOLEAN
6405 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6406 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6407 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6408 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6409 * NUMBER + NUMBER
6410 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6411 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6412 * STRING + STRING
6413 *
6414 * '<=', '<', '>=', '>'
6415 * NUMBER + NUMBER
6416 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6417 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6418 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6419 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6420 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6421 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6422 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6423 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6424 */
6425 struct lyxp_set iter1, iter2;
6426 int result;
6427 int64_t i;
6428 LY_ERR rc;
6429
Michal Vasko004d3152020-06-11 19:59:22 +02006430 memset(&iter1, 0, sizeof iter1);
6431 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006432
6433 /* iterative evaluation with node-sets */
6434 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6435 if (set1->type == LYXP_SET_NODE_SET) {
6436 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006437 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006438 switch (set2->type) {
6439 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006440 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006441 break;
6442 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006443 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006444 break;
6445 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006446 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006447 break;
6448 }
6449 LY_CHECK_RET(rc);
6450
Michal Vasko4c7763f2020-07-27 17:40:37 +02006451 /* canonize set2 */
6452 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6453
6454 /* compare recursively */
6455 rc = moveto_op_comp(&iter1, &iter2, op, options);
6456 lyxp_set_free_content(&iter2);
6457 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006458
6459 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006460 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006461 set_fill_boolean(set1, 1);
6462 return LY_SUCCESS;
6463 }
6464 }
6465 } else {
6466 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006467 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006468 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006469 case LYXP_SET_NUMBER:
6470 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6471 break;
6472 case LYXP_SET_BOOLEAN:
6473 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6474 break;
6475 default:
6476 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6477 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006478 }
6479 LY_CHECK_RET(rc);
6480
Michal Vasko4c7763f2020-07-27 17:40:37 +02006481 /* canonize set1 */
6482 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 +02006483
Michal Vasko4c7763f2020-07-27 17:40:37 +02006484 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006485 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006486 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006487 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006488
6489 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006490 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006491 set_fill_boolean(set1, 1);
6492 return LY_SUCCESS;
6493 }
6494 }
6495 }
6496
6497 /* false for all nodes */
6498 set_fill_boolean(set1, 0);
6499 return LY_SUCCESS;
6500 }
6501
6502 /* first convert properly */
6503 if ((op[0] == '=') || (op[0] == '!')) {
6504 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006505 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6506 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006507 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006508 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006509 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006510 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006511 LY_CHECK_RET(rc);
6512 } /* else we have 2 strings */
6513 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006514 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006515 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006516 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006517 LY_CHECK_RET(rc);
6518 }
6519
6520 assert(set1->type == set2->type);
6521
6522 /* compute result */
6523 if (op[0] == '=') {
6524 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006525 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006526 } else if (set1->type == LYXP_SET_NUMBER) {
6527 result = (set1->val.num == set2->val.num);
6528 } else {
6529 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006530 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006531 }
6532 } else if (op[0] == '!') {
6533 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006534 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006535 } else if (set1->type == LYXP_SET_NUMBER) {
6536 result = (set1->val.num != set2->val.num);
6537 } else {
6538 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006539 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006540 }
6541 } else {
6542 assert(set1->type == LYXP_SET_NUMBER);
6543 if (op[0] == '<') {
6544 if (op[1] == '=') {
6545 result = (set1->val.num <= set2->val.num);
6546 } else {
6547 result = (set1->val.num < set2->val.num);
6548 }
6549 } else {
6550 if (op[1] == '=') {
6551 result = (set1->val.num >= set2->val.num);
6552 } else {
6553 result = (set1->val.num > set2->val.num);
6554 }
6555 }
6556 }
6557
6558 /* assign result */
6559 if (result) {
6560 set_fill_boolean(set1, 1);
6561 } else {
6562 set_fill_boolean(set1, 0);
6563 }
6564
6565 return LY_SUCCESS;
6566}
6567
6568/**
6569 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6570 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6571 *
6572 * @param[in,out] set1 Set to use for the result.
6573 * @param[in] set2 Set acting as the second operand for @p op.
6574 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006575 * @return LY_ERR
6576 */
6577static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006578moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006579{
6580 LY_ERR rc;
6581
6582 /* unary '-' */
6583 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006584 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006585 LY_CHECK_RET(rc);
6586 set1->val.num *= -1;
6587 lyxp_set_free(set2);
6588 return LY_SUCCESS;
6589 }
6590
6591 assert(set1 && set2);
6592
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006593 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006594 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006595 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006596 LY_CHECK_RET(rc);
6597
6598 switch (op[0]) {
6599 /* '+' */
6600 case '+':
6601 set1->val.num += set2->val.num;
6602 break;
6603
6604 /* '-' */
6605 case '-':
6606 set1->val.num -= set2->val.num;
6607 break;
6608
6609 /* '*' */
6610 case '*':
6611 set1->val.num *= set2->val.num;
6612 break;
6613
6614 /* 'div' */
6615 case 'd':
6616 set1->val.num /= set2->val.num;
6617 break;
6618
6619 /* 'mod' */
6620 case 'm':
6621 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6622 break;
6623
6624 default:
6625 LOGINT_RET(set1->ctx);
6626 }
6627
6628 return LY_SUCCESS;
6629}
6630
6631/*
6632 * eval functions
6633 *
6634 * They execute a parsed XPath expression on some data subtree.
6635 */
6636
6637/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006638 * @brief Evaluate Predicate. Logs directly on error.
6639 *
Michal Vaskod3678892020-05-21 10:06:58 +02006640 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006641 *
6642 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006643 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006644 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6645 * @param[in] options XPath options.
6646 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6647 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6648 */
6649static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006650eval_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options, int parent_pos_pred)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006651{
6652 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006653 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006654 uint32_t orig_pos, orig_size;
6655 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006656 struct lyxp_set set2;
6657 struct lyd_node *orig_parent;
6658
6659 /* '[' */
6660 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006661 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6662 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006663
6664 if (!set) {
6665only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006666 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006667 LY_CHECK_RET(rc);
6668 } else if (set->type == LYXP_SET_NODE_SET) {
6669 /* 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 +01006670 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006671
6672 /* empty set, nothing to evaluate */
6673 if (!set->used) {
6674 goto only_parse;
6675 }
6676
Michal Vasko004d3152020-06-11 19:59:22 +02006677 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006678 orig_pos = 0;
6679 orig_size = set->used;
6680 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006681 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006682 set_init(&set2, set);
6683 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6684 /* remember the node context position for position() and context size for last(),
6685 * predicates should always be evaluated with respect to the child axis (since we do
6686 * not support explicit axes) so we assign positions based on their parents */
6687 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6688 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6689 orig_pos = 1;
6690 } else {
6691 ++orig_pos;
6692 }
6693
6694 set2.ctx_pos = orig_pos;
6695 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006696 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006697
Michal Vasko004d3152020-06-11 19:59:22 +02006698 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006699 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006700 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006701 return rc;
6702 }
6703
6704 /* number is a position */
6705 if (set2.type == LYXP_SET_NUMBER) {
6706 if ((long long)set2.val.num == orig_pos) {
6707 set2.val.num = 1;
6708 } else {
6709 set2.val.num = 0;
6710 }
6711 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006712 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006713
6714 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006715 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006716 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006717 }
6718 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006719 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006720
6721 } else if (set->type == LYXP_SET_SCNODE_SET) {
6722 for (i = 0; i < set->used; ++i) {
6723 if (set->val.scnodes[i].in_ctx == 1) {
6724 /* there is a currently-valid node */
6725 break;
6726 }
6727 }
6728 /* empty set, nothing to evaluate */
6729 if (i == set->used) {
6730 goto only_parse;
6731 }
6732
Michal Vasko004d3152020-06-11 19:59:22 +02006733 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006734
Michal Vasko03ff5a72019-09-11 13:49:33 +02006735 /* set special in_ctx to all the valid snodes */
6736 pred_in_ctx = set_scnode_new_in_ctx(set);
6737
6738 /* use the valid snodes one-by-one */
6739 for (i = 0; i < set->used; ++i) {
6740 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6741 continue;
6742 }
6743 set->val.scnodes[i].in_ctx = 1;
6744
Michal Vasko004d3152020-06-11 19:59:22 +02006745 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006746
Michal Vasko004d3152020-06-11 19:59:22 +02006747 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006748 LY_CHECK_RET(rc);
6749
6750 set->val.scnodes[i].in_ctx = pred_in_ctx;
6751 }
6752
6753 /* restore the state as it was before the predicate */
6754 for (i = 0; i < set->used; ++i) {
6755 if (set->val.scnodes[i].in_ctx == 1) {
6756 set->val.scnodes[i].in_ctx = 0;
6757 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6758 set->val.scnodes[i].in_ctx = 1;
6759 }
6760 }
6761
6762 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006763 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006764 set_fill_set(&set2, set);
6765
Michal Vasko004d3152020-06-11 19:59:22 +02006766 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006767 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006768 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006769 return rc;
6770 }
6771
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006772 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006773 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006774 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006775 }
Michal Vaskod3678892020-05-21 10:06:58 +02006776 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006777 }
6778
6779 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006780 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006781 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006782 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6783 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006784
6785 return LY_SUCCESS;
6786}
6787
6788/**
Michal Vaskod3678892020-05-21 10:06:58 +02006789 * @brief Evaluate Literal. Logs directly on error.
6790 *
6791 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006792 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006793 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6794 */
6795static void
Michal Vasko004d3152020-06-11 19:59:22 +02006796eval_literal(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006797{
6798 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006799 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006800 set_fill_string(set, "", 0);
6801 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006802 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 +02006803 }
6804 }
6805 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006806 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6807 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006808}
6809
6810/**
Michal Vasko004d3152020-06-11 19:59:22 +02006811 * @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 +02006812 *
Michal Vasko004d3152020-06-11 19:59:22 +02006813 * @param[in] exp Full parsed XPath expression.
6814 * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success.
6815 * @param[in] scnode Found schema node as context for the predicate.
6816 * @param[in] format Format of any prefixes in key names/values.
6817 * @param[out] predicates Parsed predicates.
6818 * @param[out] pred_type Type of @p predicates.
6819 * @return LY_SUCCESS on success,
6820 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006821 */
6822static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006823eval_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 +02006824 LY_PREFIX_FORMAT format, struct ly_path_predicate **predicates,
6825 enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006826{
Michal Vasko004d3152020-06-11 19:59:22 +02006827 LY_ERR ret = LY_SUCCESS;
6828 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006829 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006830 size_t pred_len;
6831 int prev_lo;
6832 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006833
Michal Vasko004d3152020-06-11 19:59:22 +02006834 assert(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006835
Michal Vasko004d3152020-06-11 19:59:22 +02006836 if (scnode->nodetype == LYS_LIST) {
6837 /* get key count */
6838 if (scnode->flags & LYS_KEYLESS) {
6839 return LY_EINVAL;
6840 }
Radek Krejci1e008d22020-08-17 11:37:37 +02006841 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 +02006842 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006843
Michal Vasko004d3152020-06-11 19:59:22 +02006844 /* learn where the predicates end */
6845 e_idx = *tok_idx;
6846 while (key_count) {
6847 /* '[' */
6848 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6849 return LY_EINVAL;
6850 }
6851 ++e_idx;
6852
6853 /* ']' */
6854 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6855 ++e_idx;
6856 }
6857 ++e_idx;
6858
6859 /* another presumably key predicate parsed */
6860 --key_count;
6861 }
Michal Vasko004d3152020-06-11 19:59:22 +02006862 } else {
6863 /* learn just where this single predicate ends */
6864 e_idx = *tok_idx;
6865
Michal Vaskod3678892020-05-21 10:06:58 +02006866 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006867 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6868 return LY_EINVAL;
6869 }
6870 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006871
6872 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006873 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6874 ++e_idx;
6875 }
6876 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006877 }
6878
Michal Vasko004d3152020-06-11 19:59:22 +02006879 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6880 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6881
6882 /* turn logging off */
6883 prev_lo = ly_log_options(0);
6884
6885 /* parse the predicate(s) */
Michal Vasko6b26e742020-07-17 15:02:10 +02006886 LY_CHECK_GOTO(ret = ly_path_parse_predicate(scnode->module->ctx, scnode, exp->expr + exp->tok_pos[*tok_idx], pred_len,
Michal Vasko004d3152020-06-11 19:59:22 +02006887 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
6888
6889 /* compile */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006890 ret = ly_path_compile_predicate(scnode->module->ctx, scnode, scnode->module, scnode, exp2, &pred_idx,
6891 format, scnode->module, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006892 LY_CHECK_GOTO(ret, cleanup);
6893
6894 /* success, the predicate must include all the needed information for hash-based search */
6895 *tok_idx = e_idx;
6896
6897cleanup:
6898 ly_log_options(prev_lo);
6899 lyxp_expr_free(scnode->module->ctx, exp2);
6900 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006901}
6902
6903/**
6904 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
6905 *
6906 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
6907 * [6] NodeTest ::= NameTest | NodeType '(' ')'
6908 * [7] NameTest ::= '*' | NCName ':' '*' | QName
6909 *
6910 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006911 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006912 * @param[in] attr_axis Whether to search attributes or standard nodes.
6913 * @param[in] all_desc Whether to search all the descendants or children only.
6914 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6915 * @param[in] options XPath options.
6916 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6917 */
6918static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006919eval_name_test_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, int attr_axis, int all_desc, struct lyxp_set *set,
Radek Krejci0f969882020-08-21 16:56:47 +02006920 int options)
Michal Vaskod3678892020-05-21 10:06:58 +02006921{
6922 int i;
6923 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02006924 const char *ncname, *ncname_dict = NULL;
6925 uint16_t ncname_len;
Michal Vaskod3678892020-05-21 10:06:58 +02006926 const struct lys_module *moveto_mod;
6927 const struct lysc_node *scnode = NULL, *tmp;
Michal Vasko004d3152020-06-11 19:59:22 +02006928 struct ly_path_predicate *predicates = NULL;
6929 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006930 LY_ERR rc = LY_SUCCESS;
6931
6932 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006933 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6934 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006935
6936 if (!set) {
6937 goto moveto;
6938 }
6939
Michal Vasko004d3152020-06-11 19:59:22 +02006940 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
6941 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02006942
6943 /* parse (and skip) module name */
6944 rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod);
6945 LY_CHECK_GOTO(rc, cleanup);
6946
6947 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
6948 /* find the matching schema node in some parent in the context */
6949 for (i = 0; i < (signed)set->used; ++i) {
6950 if (set->val.nodes[i].type == set->root_type) {
6951 tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0);
6952 } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM)
6953 && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) {
6954 /* do not repeat the same search */
6955 tmp = lys_find_child(set->val.nodes[i].node->schema, moveto_mod, ncname, ncname_len, 0, 0);
Radek Krejcib1247842020-07-02 16:22:38 +02006956 } else {
6957 tmp = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006958 }
6959
6960 /* additional context check */
6961 if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) {
6962 tmp = NULL;
6963 }
6964
6965 if (tmp) {
6966 if (scnode) {
6967 /* we found a schema node with the same name but at different level, give up, too complicated */
6968 scnode = NULL;
6969 break;
6970 } else {
6971 /* remember the found schema node and continue to make sure it can be used */
6972 scnode = tmp;
6973 }
Michal Vaskod3678892020-05-21 10:06:58 +02006974 }
6975 }
6976
Michal Vasko004d3152020-06-11 19:59:22 +02006977 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
6978 /* try to create the predicates */
6979 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) {
6980 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02006981 scnode = NULL;
6982 }
6983 }
6984 }
6985
Michal Vasko004d3152020-06-11 19:59:22 +02006986 if (!scnode && moveto_mod) {
6987 /* we are not able to match based on a schema node and not all the modules match,
6988 * use dictionary for efficient comparison */
6989 ncname_dict = lydict_insert(set->ctx, ncname, ncname_len);
Michal Vaskod3678892020-05-21 10:06:58 +02006990 }
6991
6992moveto:
6993 /* move to the attribute(s), data node(s), or schema node(s) */
6994 if (attr_axis) {
6995 if (set && (options & LYXP_SCNODE_ALL)) {
6996 set_scnode_clear_ctx(set);
6997 } else {
6998 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02006999 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007000 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007001 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007002 }
7003 LY_CHECK_GOTO(rc, cleanup);
7004 }
7005 } else {
7006 if (set && (options & LYXP_SCNODE_ALL)) {
7007 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007008 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007009 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007010 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007011 }
7012 LY_CHECK_GOTO(rc, cleanup);
7013
7014 for (i = set->used - 1; i > -1; --i) {
7015 if (set->val.scnodes[i].in_ctx > 0) {
7016 break;
7017 }
7018 }
7019 if (i == -1) {
7020 path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0);
7021 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
Michal Vasko6b26e742020-07-17 15:02:10 +02007022 ncname_len, ncname, ncname - exp->expr, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007023 free(path);
7024 }
7025 } else {
7026 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007027 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007028 } else {
7029 if (scnode) {
7030 /* we can find the nodes using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02007031 rc = moveto_node_hash(set, scnode, predicates);
Michal Vaskod3678892020-05-21 10:06:58 +02007032 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007033 rc = moveto_node(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007034 }
7035 }
7036 LY_CHECK_GOTO(rc, cleanup);
7037 }
7038 }
7039
7040 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007041 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7042 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007043 LY_CHECK_RET(rc);
7044 }
7045
7046cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007047 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007048 lydict_remove(set->ctx, ncname_dict);
7049 ly_path_predicates_free(set->ctx, pred_type, scnode, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007050 }
Michal Vaskod3678892020-05-21 10:06:58 +02007051 return rc;
7052}
7053
7054/**
7055 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7056 *
7057 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7058 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7059 * [8] NodeType ::= 'text' | 'node'
7060 *
7061 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007062 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007063 * @param[in] attr_axis Whether to search attributes or standard nodes.
7064 * @param[in] all_desc Whether to search all the descendants or children only.
7065 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7066 * @param[in] options XPath options.
7067 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7068 */
7069static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007070eval_node_type_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, int attr_axis, int all_desc,
Radek Krejci0f969882020-08-21 16:56:47 +02007071 struct lyxp_set *set, int options)
Michal Vaskod3678892020-05-21 10:06:58 +02007072{
7073 LY_ERR rc;
7074
7075 /* TODO */
7076 (void)attr_axis;
7077 (void)all_desc;
7078
7079 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007080 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007081 if (set->type == LYXP_SET_SCNODE_SET) {
7082 set_scnode_clear_ctx(set);
7083 /* just for the debug message below */
7084 set = NULL;
7085 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007086 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007087 rc = xpath_node(NULL, 0, set, options);
7088 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007089 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007090 rc = xpath_text(NULL, 0, set, options);
7091 }
7092 LY_CHECK_RET(rc);
7093 }
7094 }
7095 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007096 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7097 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007098
7099 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007100 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007101 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007102 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7103 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007104
7105 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007106 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007107 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007108 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7109 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007110
7111 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007112 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7113 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007114 LY_CHECK_RET(rc);
7115 }
7116
7117 return LY_SUCCESS;
7118}
7119
7120/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007121 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7122 *
7123 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7124 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007125 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007126 *
7127 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007128 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007129 * @param[in] all_desc Whether to search all the descendants or children only.
7130 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7131 * @param[in] options XPath options.
7132 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7133 */
7134static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007135eval_relative_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, int all_desc, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007136{
7137 int attr_axis;
7138 LY_ERR rc;
7139
7140 goto step;
7141 do {
7142 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007143 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007144 all_desc = 0;
7145 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007146 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007147 all_desc = 1;
7148 }
7149 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007150 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7151 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007152
7153step:
Michal Vaskod3678892020-05-21 10:06:58 +02007154 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007155 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007156 attr_axis = 1;
7157
7158 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007159 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7160 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007161 } else {
7162 attr_axis = 0;
7163 }
7164
Michal Vasko03ff5a72019-09-11 13:49:33 +02007165 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007166 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007167 case LYXP_TOKEN_DOT:
7168 /* evaluate '.' */
7169 if (set && (options & LYXP_SCNODE_ALL)) {
7170 rc = moveto_scnode_self(set, all_desc, options);
7171 } else {
7172 rc = moveto_self(set, all_desc, options);
7173 }
7174 LY_CHECK_RET(rc);
7175 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007176 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7177 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007178 break;
7179
7180 case LYXP_TOKEN_DDOT:
7181 /* evaluate '..' */
7182 if (set && (options & LYXP_SCNODE_ALL)) {
7183 rc = moveto_scnode_parent(set, all_desc, options);
7184 } else {
7185 rc = moveto_parent(set, all_desc, options);
7186 }
7187 LY_CHECK_RET(rc);
7188 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007189 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7190 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007191 break;
7192
Michal Vasko03ff5a72019-09-11 13:49:33 +02007193 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007194 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007195 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007196 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007197 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007198
Michal Vaskod3678892020-05-21 10:06:58 +02007199 case LYXP_TOKEN_NODETYPE:
7200 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007201 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007202 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007203 break;
7204
7205 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007206 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007207 }
Michal Vasko004d3152020-06-11 19:59:22 +02007208 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007209
7210 return LY_SUCCESS;
7211}
7212
7213/**
7214 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7215 *
7216 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7217 *
7218 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007219 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007220 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7221 * @param[in] options XPath options.
7222 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7223 */
7224static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007225eval_absolute_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007226{
7227 int all_desc;
7228 LY_ERR rc;
7229
7230 if (set) {
7231 /* no matter what tokens follow, we need to be at the root */
7232 moveto_root(set, options);
7233 }
7234
7235 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007236 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007237 /* evaluate '/' - deferred */
7238 all_desc = 0;
7239 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007240 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7241 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007242
Michal Vasko004d3152020-06-11 19:59:22 +02007243 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007244 return LY_SUCCESS;
7245 }
Michal Vasko004d3152020-06-11 19:59:22 +02007246 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007247 case LYXP_TOKEN_DOT:
7248 case LYXP_TOKEN_DDOT:
7249 case LYXP_TOKEN_AT:
7250 case LYXP_TOKEN_NAMETEST:
7251 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02007252 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007253 LY_CHECK_RET(rc);
7254 break;
7255 default:
7256 break;
7257 }
7258
Michal Vasko03ff5a72019-09-11 13:49:33 +02007259 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007260 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007261 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7262 all_desc = 1;
7263 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007264 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7265 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007266
Michal Vasko004d3152020-06-11 19:59:22 +02007267 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007268 LY_CHECK_RET(rc);
7269 }
7270
7271 return LY_SUCCESS;
7272}
7273
7274/**
7275 * @brief Evaluate FunctionCall. Logs directly on error.
7276 *
Michal Vaskod3678892020-05-21 10:06:58 +02007277 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007278 *
7279 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007280 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007281 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7282 * @param[in] options XPath options.
7283 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7284 */
7285static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007286eval_function_call(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007287{
7288 LY_ERR rc;
7289 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, int) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007290 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007291 struct lyxp_set **args = NULL, **args_aux;
7292
7293 if (set) {
7294 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007295 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007296 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007297 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007298 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007299 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007300 xpath_func = &xpath_sum;
7301 }
7302 break;
7303 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007304 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007305 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007306 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007307 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007308 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007309 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007310 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007311 xpath_func = &xpath_true;
7312 }
7313 break;
7314 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007315 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007316 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007317 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007318 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007319 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007320 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007321 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007322 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007323 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007324 xpath_func = &xpath_deref;
7325 }
7326 break;
7327 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007328 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007329 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007330 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007331 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007332 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007333 xpath_func = &xpath_string;
7334 }
7335 break;
7336 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007337 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007338 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007339 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007340 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007341 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007342 xpath_func = &xpath_current;
7343 }
7344 break;
7345 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007346 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007347 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007348 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007349 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007350 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007351 xpath_func = &xpath_re_match;
7352 }
7353 break;
7354 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007355 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007356 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007357 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007358 xpath_func = &xpath_translate;
7359 }
7360 break;
7361 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007362 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007363 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007364 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007365 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007366 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007367 xpath_func = &xpath_bit_is_set;
7368 }
7369 break;
7370 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007371 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007372 xpath_func = &xpath_starts_with;
7373 }
7374 break;
7375 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007376 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007377 xpath_func = &xpath_derived_from;
7378 }
7379 break;
7380 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007381 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007382 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007383 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007384 xpath_func = &xpath_string_length;
7385 }
7386 break;
7387 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007388 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007389 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007390 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007391 xpath_func = &xpath_substring_after;
7392 }
7393 break;
7394 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007395 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007396 xpath_func = &xpath_substring_before;
7397 }
7398 break;
7399 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007400 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007401 xpath_func = &xpath_derived_from_or_self;
7402 }
7403 break;
7404 }
7405
7406 if (!xpath_func) {
Michal Vasko004d3152020-06-11 19:59:22 +02007407 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 +02007408 return LY_EVALID;
7409 }
7410 }
7411
7412 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007413 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7414 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007415
7416 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007417 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007418 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007419 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7420 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007421
7422 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007423 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007424 if (set) {
7425 args = malloc(sizeof *args);
7426 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7427 arg_count = 1;
7428 args[0] = set_copy(set);
7429 if (!args[0]) {
7430 rc = LY_EMEM;
7431 goto cleanup;
7432 }
7433
Michal Vasko004d3152020-06-11 19:59:22 +02007434 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007435 LY_CHECK_GOTO(rc, cleanup);
7436 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007437 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007438 LY_CHECK_GOTO(rc, cleanup);
7439 }
7440 }
Michal Vasko004d3152020-06-11 19:59:22 +02007441 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007442 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007443 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7444 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007445
7446 if (set) {
7447 ++arg_count;
7448 args_aux = realloc(args, arg_count * sizeof *args);
7449 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7450 args = args_aux;
7451 args[arg_count - 1] = set_copy(set);
7452 if (!args[arg_count - 1]) {
7453 rc = LY_EMEM;
7454 goto cleanup;
7455 }
7456
Michal Vasko004d3152020-06-11 19:59:22 +02007457 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007458 LY_CHECK_GOTO(rc, cleanup);
7459 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007460 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007461 LY_CHECK_GOTO(rc, cleanup);
7462 }
7463 }
7464
7465 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007466 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007467 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007468 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7469 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007470
7471 if (set) {
7472 /* evaluate function */
7473 rc = xpath_func(args, arg_count, set, options);
7474
7475 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007476 /* merge all nodes from arg evaluations */
7477 for (i = 0; i < arg_count; ++i) {
7478 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007479 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007480 }
7481 }
7482 } else {
7483 rc = LY_SUCCESS;
7484 }
7485
7486cleanup:
7487 for (i = 0; i < arg_count; ++i) {
7488 lyxp_set_free(args[i]);
7489 }
7490 free(args);
7491
7492 return rc;
7493}
7494
7495/**
7496 * @brief Evaluate Number. Logs directly on error.
7497 *
7498 * @param[in] ctx Context for errors.
7499 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007500 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007501 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7502 * @return LY_ERR
7503 */
7504static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007505eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007506{
7507 long double num;
7508 char *endptr;
7509
7510 if (set) {
7511 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007512 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007513 if (errno) {
Michal Vasko004d3152020-06-11 19:59:22 +02007514 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 +02007515 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko004d3152020-06-11 19:59:22 +02007516 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007517 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007518 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
7519 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 +02007520 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko004d3152020-06-11 19:59:22 +02007521 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007522 return LY_EVALID;
7523 }
7524
7525 set_fill_number(set, num);
7526 }
7527
7528 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007529 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7530 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007531 return LY_SUCCESS;
7532}
7533
7534/**
7535 * @brief Evaluate PathExpr. Logs directly on error.
7536 *
Michal Vaskod3678892020-05-21 10:06:58 +02007537 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007538 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7539 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7540 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007541 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007542 *
7543 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007544 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7546 * @param[in] options XPath options.
7547 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7548 */
7549static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007550eval_path_expr(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007551{
7552 int all_desc, parent_pos_pred;
7553 LY_ERR rc;
7554
Michal Vasko004d3152020-06-11 19:59:22 +02007555 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007556 case LYXP_TOKEN_PAR1:
7557 /* '(' Expr ')' */
7558
7559 /* '(' */
7560 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007561 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7562 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007563
7564 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007565 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007566 LY_CHECK_RET(rc);
7567
7568 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007569 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007571 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7572 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007573
7574 parent_pos_pred = 0;
7575 goto predicate;
7576
7577 case LYXP_TOKEN_DOT:
7578 case LYXP_TOKEN_DDOT:
7579 case LYXP_TOKEN_AT:
7580 case LYXP_TOKEN_NAMETEST:
7581 case LYXP_TOKEN_NODETYPE:
7582 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007583 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007584 LY_CHECK_RET(rc);
7585 break;
7586
7587 case LYXP_TOKEN_FUNCNAME:
7588 /* FunctionCall */
7589 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007590 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007591 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007592 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007593 }
7594 LY_CHECK_RET(rc);
7595
7596 parent_pos_pred = 1;
7597 goto predicate;
7598
Michal Vasko3e48bf32020-06-01 08:39:07 +02007599 case LYXP_TOKEN_OPER_PATH:
7600 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007601 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007602 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007603 LY_CHECK_RET(rc);
7604 break;
7605
7606 case LYXP_TOKEN_LITERAL:
7607 /* Literal */
7608 if (!set || (options & LYXP_SCNODE_ALL)) {
7609 if (set) {
7610 set_scnode_clear_ctx(set);
7611 }
Michal Vasko004d3152020-06-11 19:59:22 +02007612 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007613 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007614 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007615 }
7616
7617 parent_pos_pred = 1;
7618 goto predicate;
7619
7620 case LYXP_TOKEN_NUMBER:
7621 /* Number */
7622 if (!set || (options & LYXP_SCNODE_ALL)) {
7623 if (set) {
7624 set_scnode_clear_ctx(set);
7625 }
Michal Vasko004d3152020-06-11 19:59:22 +02007626 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007627 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007628 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007629 }
7630 LY_CHECK_RET(rc);
7631
7632 parent_pos_pred = 1;
7633 goto predicate;
7634
7635 default:
Michal Vasko004d3152020-06-11 19:59:22 +02007636 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]),
7637 &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007638 return LY_EVALID;
7639 }
7640
7641 return LY_SUCCESS;
7642
7643predicate:
7644 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007645 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7646 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007647 LY_CHECK_RET(rc);
7648 }
7649
7650 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007651 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007652
7653 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007654 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007655 all_desc = 0;
7656 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007657 all_desc = 1;
7658 }
7659
7660 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007661 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7662 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007663
Michal Vasko004d3152020-06-11 19:59:22 +02007664 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007665 LY_CHECK_RET(rc);
7666 }
7667
7668 return LY_SUCCESS;
7669}
7670
7671/**
7672 * @brief Evaluate UnionExpr. Logs directly on error.
7673 *
Michal Vaskod3678892020-05-21 10:06:58 +02007674 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007675 *
7676 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007677 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007678 * @param[in] repeat How many times this expression is repeated.
7679 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7680 * @param[in] options XPath options.
7681 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7682 */
7683static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007684eval_union_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007685{
7686 LY_ERR rc = LY_SUCCESS;
7687 struct lyxp_set orig_set, set2;
7688 uint16_t i;
7689
7690 assert(repeat);
7691
7692 set_init(&orig_set, set);
7693 set_init(&set2, set);
7694
7695 set_fill_set(&orig_set, set);
7696
Michal Vasko004d3152020-06-11 19:59:22 +02007697 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007698 LY_CHECK_GOTO(rc, cleanup);
7699
7700 /* ('|' PathExpr)* */
7701 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007702 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007703 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007704 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7705 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007706
7707 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007708 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007709 LY_CHECK_GOTO(rc, cleanup);
7710 continue;
7711 }
7712
7713 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007714 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007715 LY_CHECK_GOTO(rc, cleanup);
7716
7717 /* eval */
7718 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007719 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007720 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007721 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007722 LY_CHECK_GOTO(rc, cleanup);
7723 }
7724 }
7725
7726cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007727 lyxp_set_free_content(&orig_set);
7728 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007729 return rc;
7730}
7731
7732/**
7733 * @brief Evaluate UnaryExpr. Logs directly on error.
7734 *
Michal Vaskod3678892020-05-21 10:06:58 +02007735 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007736 *
7737 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007738 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007739 * @param[in] repeat How many times this expression is repeated.
7740 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7741 * @param[in] options XPath options.
7742 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7743 */
7744static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007745eval_unary_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007746{
7747 LY_ERR rc;
7748 uint16_t this_op, i;
7749
7750 assert(repeat);
7751
7752 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007753 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007754 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007755 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 +02007756
7757 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007758 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7759 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007760 }
7761
Michal Vasko004d3152020-06-11 19:59:22 +02007762 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007763 LY_CHECK_RET(rc);
7764
7765 if (set && (repeat % 2)) {
7766 if (options & LYXP_SCNODE_ALL) {
7767 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7768 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007769 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007770 LY_CHECK_RET(rc);
7771 }
7772 }
7773
7774 return LY_SUCCESS;
7775}
7776
7777/**
7778 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7779 *
Michal Vaskod3678892020-05-21 10:06:58 +02007780 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007781 * | MultiplicativeExpr '*' UnaryExpr
7782 * | MultiplicativeExpr 'div' UnaryExpr
7783 * | MultiplicativeExpr 'mod' UnaryExpr
7784 *
7785 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007786 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007787 * @param[in] repeat How many times this expression is repeated.
7788 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7789 * @param[in] options XPath options.
7790 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7791 */
7792static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007793eval_multiplicative_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007794{
7795 LY_ERR rc;
7796 uint16_t this_op;
7797 struct lyxp_set orig_set, set2;
7798 uint16_t i;
7799
7800 assert(repeat);
7801
7802 set_init(&orig_set, set);
7803 set_init(&set2, set);
7804
7805 set_fill_set(&orig_set, set);
7806
Michal Vasko004d3152020-06-11 19:59:22 +02007807 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007808 LY_CHECK_GOTO(rc, cleanup);
7809
7810 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7811 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007812 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007813
Michal Vasko004d3152020-06-11 19:59:22 +02007814 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007815 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007816 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7817 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007818
7819 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007820 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007821 LY_CHECK_GOTO(rc, cleanup);
7822 continue;
7823 }
7824
7825 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007826 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007827 LY_CHECK_GOTO(rc, cleanup);
7828
7829 /* eval */
7830 if (options & LYXP_SCNODE_ALL) {
7831 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007832 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007833 set_scnode_clear_ctx(set);
7834 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007835 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007836 LY_CHECK_GOTO(rc, cleanup);
7837 }
7838 }
7839
7840cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007841 lyxp_set_free_content(&orig_set);
7842 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007843 return rc;
7844}
7845
7846/**
7847 * @brief Evaluate AdditiveExpr. Logs directly on error.
7848 *
Michal Vaskod3678892020-05-21 10:06:58 +02007849 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007850 * | AdditiveExpr '+' MultiplicativeExpr
7851 * | AdditiveExpr '-' MultiplicativeExpr
7852 *
7853 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007854 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007855 * @param[in] repeat How many times this expression is repeated.
7856 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7857 * @param[in] options XPath options.
7858 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7859 */
7860static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007861eval_additive_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007862{
7863 LY_ERR rc;
7864 uint16_t this_op;
7865 struct lyxp_set orig_set, set2;
7866 uint16_t i;
7867
7868 assert(repeat);
7869
7870 set_init(&orig_set, set);
7871 set_init(&set2, set);
7872
7873 set_fill_set(&orig_set, set);
7874
Michal Vasko004d3152020-06-11 19:59:22 +02007875 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007876 LY_CHECK_GOTO(rc, cleanup);
7877
7878 /* ('+' / '-' MultiplicativeExpr)* */
7879 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007880 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007881
Michal Vasko004d3152020-06-11 19:59:22 +02007882 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007883 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007884 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7885 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007886
7887 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007888 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007889 LY_CHECK_GOTO(rc, cleanup);
7890 continue;
7891 }
7892
7893 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007894 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007895 LY_CHECK_GOTO(rc, cleanup);
7896
7897 /* eval */
7898 if (options & LYXP_SCNODE_ALL) {
7899 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007900 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007901 set_scnode_clear_ctx(set);
7902 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007903 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007904 LY_CHECK_GOTO(rc, cleanup);
7905 }
7906 }
7907
7908cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007909 lyxp_set_free_content(&orig_set);
7910 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007911 return rc;
7912}
7913
7914/**
7915 * @brief Evaluate RelationalExpr. Logs directly on error.
7916 *
Michal Vaskod3678892020-05-21 10:06:58 +02007917 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007918 * | RelationalExpr '<' AdditiveExpr
7919 * | RelationalExpr '>' AdditiveExpr
7920 * | RelationalExpr '<=' AdditiveExpr
7921 * | RelationalExpr '>=' AdditiveExpr
7922 *
7923 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007924 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007925 * @param[in] repeat How many times this expression is repeated.
7926 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7927 * @param[in] options XPath options.
7928 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7929 */
7930static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007931eval_relational_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007932{
7933 LY_ERR rc;
7934 uint16_t this_op;
7935 struct lyxp_set orig_set, set2;
7936 uint16_t i;
7937
7938 assert(repeat);
7939
7940 set_init(&orig_set, set);
7941 set_init(&set2, set);
7942
7943 set_fill_set(&orig_set, set);
7944
Michal Vasko004d3152020-06-11 19:59:22 +02007945 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007946 LY_CHECK_GOTO(rc, cleanup);
7947
7948 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
7949 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007950 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007951
Michal Vasko004d3152020-06-11 19:59:22 +02007952 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007953 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007954 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7955 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007956
7957 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007958 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007959 LY_CHECK_GOTO(rc, cleanup);
7960 continue;
7961 }
7962
7963 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007964 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007965 LY_CHECK_GOTO(rc, cleanup);
7966
7967 /* eval */
7968 if (options & LYXP_SCNODE_ALL) {
7969 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007970 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007971 set_scnode_clear_ctx(set);
7972 } else {
7973 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
7974 LY_CHECK_GOTO(rc, cleanup);
7975 }
7976 }
7977
7978cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007979 lyxp_set_free_content(&orig_set);
7980 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007981 return rc;
7982}
7983
7984/**
7985 * @brief Evaluate EqualityExpr. Logs directly on error.
7986 *
Michal Vaskod3678892020-05-21 10:06:58 +02007987 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007988 * | EqualityExpr '!=' RelationalExpr
7989 *
7990 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007991 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007992 * @param[in] repeat How many times this expression is repeated.
7993 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7994 * @param[in] options XPath options.
7995 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7996 */
7997static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007998eval_equality_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007999{
8000 LY_ERR rc;
8001 uint16_t this_op;
8002 struct lyxp_set orig_set, set2;
8003 uint16_t i;
8004
8005 assert(repeat);
8006
8007 set_init(&orig_set, set);
8008 set_init(&set2, set);
8009
8010 set_fill_set(&orig_set, set);
8011
Michal Vasko004d3152020-06-11 19:59:22 +02008012 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008013 LY_CHECK_GOTO(rc, cleanup);
8014
8015 /* ('=' / '!=' RelationalExpr)* */
8016 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008017 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008018
Michal Vasko004d3152020-06-11 19:59:22 +02008019 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008020 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02008021 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8022 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008023
8024 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008025 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008026 LY_CHECK_GOTO(rc, cleanup);
8027 continue;
8028 }
8029
8030 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008031 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008032 LY_CHECK_GOTO(rc, cleanup);
8033
8034 /* eval */
8035 if (options & LYXP_SCNODE_ALL) {
8036 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008037 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8038 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008039 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008040 set_scnode_clear_ctx(set);
8041 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008042 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8043 LY_CHECK_GOTO(rc, cleanup);
8044 }
8045 }
8046
8047cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008048 lyxp_set_free_content(&orig_set);
8049 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008050 return rc;
8051}
8052
8053/**
8054 * @brief Evaluate AndExpr. Logs directly on error.
8055 *
Michal Vaskod3678892020-05-21 10:06:58 +02008056 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008057 *
8058 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008059 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008060 * @param[in] repeat How many times this expression is repeated.
8061 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8062 * @param[in] options XPath options.
8063 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8064 */
8065static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008066eval_and_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008067{
8068 LY_ERR rc;
8069 struct lyxp_set orig_set, set2;
8070 uint16_t i;
8071
8072 assert(repeat);
8073
8074 set_init(&orig_set, set);
8075 set_init(&set2, set);
8076
8077 set_fill_set(&orig_set, set);
8078
Michal Vasko004d3152020-06-11 19:59:22 +02008079 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008080 LY_CHECK_GOTO(rc, cleanup);
8081
8082 /* cast to boolean, we know that will be the final result */
8083 if (set && (options & LYXP_SCNODE_ALL)) {
8084 set_scnode_clear_ctx(set);
8085 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008086 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008087 }
8088
8089 /* ('and' EqualityExpr)* */
8090 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008091 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8092 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
8093 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8094 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008095
8096 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008097 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8098 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008099 LY_CHECK_GOTO(rc, cleanup);
8100 continue;
8101 }
8102
8103 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008104 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008105 LY_CHECK_GOTO(rc, cleanup);
8106
8107 /* eval - just get boolean value actually */
8108 if (set->type == LYXP_SET_SCNODE_SET) {
8109 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008110 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008111 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008112 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008113 set_fill_set(set, &set2);
8114 }
8115 }
8116
8117cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008118 lyxp_set_free_content(&orig_set);
8119 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008120 return rc;
8121}
8122
8123/**
8124 * @brief Evaluate OrExpr. Logs directly on error.
8125 *
Michal Vaskod3678892020-05-21 10:06:58 +02008126 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008127 *
8128 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008129 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008130 * @param[in] repeat How many times this expression is repeated.
8131 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8132 * @param[in] options XPath options.
8133 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8134 */
8135static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008136eval_or_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008137{
8138 LY_ERR rc;
8139 struct lyxp_set orig_set, set2;
8140 uint16_t i;
8141
8142 assert(repeat);
8143
8144 set_init(&orig_set, set);
8145 set_init(&set2, set);
8146
8147 set_fill_set(&orig_set, set);
8148
Michal Vasko004d3152020-06-11 19:59:22 +02008149 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008150 LY_CHECK_GOTO(rc, cleanup);
8151
8152 /* cast to boolean, we know that will be the final result */
8153 if (set && (options & LYXP_SCNODE_ALL)) {
8154 set_scnode_clear_ctx(set);
8155 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008156 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008157 }
8158
8159 /* ('or' AndExpr)* */
8160 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008161 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8162 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
8163 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8164 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008165
8166 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008167 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8168 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008169 LY_CHECK_GOTO(rc, cleanup);
8170 continue;
8171 }
8172
8173 set_fill_set(&set2, &orig_set);
8174 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8175 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008176 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008177 LY_CHECK_GOTO(rc, cleanup);
8178
8179 /* eval - just get boolean value actually */
8180 if (set->type == LYXP_SET_SCNODE_SET) {
8181 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008182 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008183 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008184 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008185 set_fill_set(set, &set2);
8186 }
8187 }
8188
8189cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008190 lyxp_set_free_content(&orig_set);
8191 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008192 return rc;
8193}
8194
8195/**
Michal Vasko004d3152020-06-11 19:59:22 +02008196 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008197 *
8198 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008199 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008200 * @param[in] etype Expression type to evaluate.
8201 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8202 * @param[in] options XPath options.
8203 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8204 */
8205static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008206eval_expr_select(struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008207{
8208 uint16_t i, count;
8209 enum lyxp_expr_type next_etype;
8210 LY_ERR rc;
8211
8212 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008213 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008214 next_etype = LYXP_EXPR_NONE;
8215 } else {
8216 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008217 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008218
8219 /* select one-priority lower because etype expression called us */
8220 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008221 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008222 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008223 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008224 } else {
8225 next_etype = LYXP_EXPR_NONE;
8226 }
8227 }
8228
8229 /* decide what expression are we parsing based on the repeat */
8230 switch (next_etype) {
8231 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008232 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008233 break;
8234 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008235 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008236 break;
8237 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008238 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008239 break;
8240 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008241 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008242 break;
8243 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008244 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008245 break;
8246 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008247 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008248 break;
8249 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008250 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008251 break;
8252 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008253 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008254 break;
8255 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008256 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008257 break;
8258 default:
8259 LOGINT_RET(set->ctx);
8260 }
8261
8262 return rc;
8263}
8264
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008265/**
8266 * @brief Get root type.
8267 *
8268 * @param[in] ctx_node Context node.
8269 * @param[in] ctx_scnode Schema context node.
8270 * @param[in] options XPath options.
8271 * @return Root type.
8272 */
8273static enum lyxp_node_type
8274lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options)
8275{
Michal Vasko6b26e742020-07-17 15:02:10 +02008276 const struct lysc_node *op;
8277
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008278 if (options & LYXP_SCNODE_ALL) {
Radek Krejci1e008d22020-08-17 11:37:37 +02008279 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008280
8281 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008282 /* general root that can access everything */
8283 return LYXP_NODE_ROOT;
8284 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8285 /* root context node can access only config data (because we said so, it is unspecified) */
8286 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008287 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008288 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008289 }
8290
Michal Vasko6b26e742020-07-17 15:02:10 +02008291 op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci1e008d22020-08-17 11:37:37 +02008292 for (; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008293
8294 if (!ctx_node || (!op && (ctx_node->schema->flags & LYS_CONFIG_W))) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008295 /* root context node can access only config data (because we said so, it is unspecified) */
8296 return LYXP_NODE_ROOT_CONFIG;
8297 }
8298
8299 return LYXP_NODE_ROOT;
8300}
8301
Michal Vasko03ff5a72019-09-11 13:49:33 +02008302LY_ERR
Michal Vaskoc8a230d2020-08-14 12:17:10 +02008303lyxp_eval(struct lyxp_expr *exp, LY_PREFIX_FORMAT format, const struct lys_module *local_mod, const struct lyd_node *ctx_node,
Radek Krejci0f969882020-08-21 16:56:47 +02008304 enum lyxp_node_type ctx_node_type, const struct lyd_node *tree, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008305{
Michal Vasko004d3152020-06-11 19:59:22 +02008306 uint16_t tok_idx = 0;
8307 const struct lyd_node *real_ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008308 LY_ERR rc;
8309
Michal Vasko004d3152020-06-11 19:59:22 +02008310 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL);
8311
8312 if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) {
8313 /* we always need some context node because it is used for resolving unqualified names */
8314 real_ctx_node = NULL;
8315 } else {
8316 real_ctx_node = ctx_node;
8317 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008318
8319 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008320 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008321 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008322 set->type = LYXP_SET_NODE_SET;
Michal Vasko004d3152020-06-11 19:59:22 +02008323 set_insert_node(set, (struct lyd_node *)real_ctx_node, 0, ctx_node_type, 0);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008324 set->ctx = local_mod->ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008325 set->ctx_node = ctx_node;
Michal Vasko004d3152020-06-11 19:59:22 +02008326 set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options);
Michal Vasko6b26e742020-07-17 15:02:10 +02008327 for (set->context_op = ctx_node->schema;
Radek Krejci0f969882020-08-21 16:56:47 +02008328 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8329 set->context_op = set->context_op->parent) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008330 set->local_mod = local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +01008331 set->tree = tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008332 set->format = format;
8333
8334 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008335 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008336 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008337 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008338 }
8339
Michal Vasko03ff5a72019-09-11 13:49:33 +02008340 return rc;
8341}
8342
8343#if 0
8344
8345/* full xml printing of set elements, not used currently */
8346
8347void
8348lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8349{
8350 uint32_t i;
8351 char *str_num;
8352 struct lyout out;
8353
8354 memset(&out, 0, sizeof out);
8355
8356 out.type = LYOUT_STREAM;
8357 out.method.f = f;
8358
8359 switch (set->type) {
8360 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008361 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008362 break;
8363 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008364 ly_print_(&out, "Boolean XPath set:\n");
8365 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008366 break;
8367 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008368 ly_print_(&out, "String XPath set:\n");
8369 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008370 break;
8371 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008372 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008373
8374 if (isnan(set->value.num)) {
8375 str_num = strdup("NaN");
8376 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8377 str_num = strdup("0");
8378 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8379 str_num = strdup("Infinity");
8380 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8381 str_num = strdup("-Infinity");
8382 } else if ((long long)set->value.num == set->value.num) {
8383 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8384 str_num = NULL;
8385 }
8386 } else {
8387 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8388 str_num = NULL;
8389 }
8390 }
8391 if (!str_num) {
8392 LOGMEM;
8393 return;
8394 }
Michal Vasko5233e962020-08-14 14:26:20 +02008395 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008396 free(str_num);
8397 break;
8398 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008399 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008400
8401 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008402 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008403 switch (set->node_type[i]) {
8404 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008405 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008406 break;
8407 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008408 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008409 break;
8410 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008411 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008412 break;
8413 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008414 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008415 break;
8416 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008417 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008418 break;
8419 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008420 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008421 break;
8422 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008423 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008424 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008425 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008426 break;
8427 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008428 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 +02008429 break;
8430 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008431 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 +02008432 break;
8433 }
8434 }
8435 break;
8436 }
8437}
8438
8439#endif
8440
8441LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008442lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008443{
8444 long double num;
8445 char *str;
8446 LY_ERR rc;
8447
8448 if (!set || (set->type == target)) {
8449 return LY_SUCCESS;
8450 }
8451
8452 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008453 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008454
8455 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008456 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008457 return LY_EINVAL;
8458 }
8459
8460 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008461 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008462 switch (set->type) {
8463 case LYXP_SET_NUMBER:
8464 if (isnan(set->val.num)) {
8465 set->val.str = strdup("NaN");
8466 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8467 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8468 set->val.str = strdup("0");
8469 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8470 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8471 set->val.str = strdup("Infinity");
8472 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8473 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8474 set->val.str = strdup("-Infinity");
8475 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8476 } else if ((long long)set->val.num == set->val.num) {
8477 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8478 LOGMEM_RET(set->ctx);
8479 }
8480 set->val.str = str;
8481 } else {
8482 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8483 LOGMEM_RET(set->ctx);
8484 }
8485 set->val.str = str;
8486 }
8487 break;
8488 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008489 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008490 set->val.str = strdup("true");
8491 } else {
8492 set->val.str = strdup("false");
8493 }
8494 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8495 break;
8496 case LYXP_SET_NODE_SET:
8497 assert(set->used);
8498
8499 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008500 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008501
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008502 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008503 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008504 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008505 set->val.str = str;
8506 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008507 default:
8508 LOGINT_RET(set->ctx);
8509 }
8510 set->type = LYXP_SET_STRING;
8511 }
8512
8513 /* to NUMBER */
8514 if (target == LYXP_SET_NUMBER) {
8515 switch (set->type) {
8516 case LYXP_SET_STRING:
8517 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008518 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008519 set->val.num = num;
8520 break;
8521 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008522 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008523 set->val.num = 1;
8524 } else {
8525 set->val.num = 0;
8526 }
8527 break;
8528 default:
8529 LOGINT_RET(set->ctx);
8530 }
8531 set->type = LYXP_SET_NUMBER;
8532 }
8533
8534 /* to BOOLEAN */
8535 if (target == LYXP_SET_BOOLEAN) {
8536 switch (set->type) {
8537 case LYXP_SET_NUMBER:
8538 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008539 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008540 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008541 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008542 }
8543 break;
8544 case LYXP_SET_STRING:
8545 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008546 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008547 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008548 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008549 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008550 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008551 }
8552 break;
8553 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008554 if (set->used) {
8555 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008556 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008557 } else {
8558 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008559 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008560 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008561 break;
8562 default:
8563 LOGINT_RET(set->ctx);
8564 }
8565 set->type = LYXP_SET_BOOLEAN;
8566 }
8567
Michal Vasko03ff5a72019-09-11 13:49:33 +02008568 return LY_SUCCESS;
8569}
8570
8571LY_ERR
Michal Vaskoc8a230d2020-08-14 12:17:10 +02008572lyxp_atomize(struct lyxp_expr *exp, LY_PREFIX_FORMAT format, const struct lys_module *local_mod,
Radek Krejci0f969882020-08-21 16:56:47 +02008573 const struct lysc_node *ctx_scnode, enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008574{
8575 struct ly_ctx *ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008576 const struct lysc_node *real_ctx_scnode;
8577 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008578
Michal Vasko004d3152020-06-11 19:59:22 +02008579 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_scnode, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008580
8581 ctx = local_mod->ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008582 if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) {
8583 /* we always need some context node because it is used for resolving unqualified names */
8584 real_ctx_scnode = NULL;
8585 } else {
8586 real_ctx_scnode = ctx_scnode;
8587 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008588
8589 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008590 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008591 memset(set, 0, sizeof *set);
8592 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko004d3152020-06-11 19:59:22 +02008593 lyxp_set_scnode_insert_node(set, real_ctx_scnode, ctx_scnode_type);
Michal Vasko5c4e5892019-11-14 12:31:38 +01008594 set->val.scnodes[0].in_ctx = -2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008595 set->ctx = ctx;
8596 set->ctx_scnode = ctx_scnode;
Michal Vasko004d3152020-06-11 19:59:22 +02008597 set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options);
Michal Vasko6b26e742020-07-17 15:02:10 +02008598 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008599 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8600 set->context_op = set->context_op->parent) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008601 set->local_mod = local_mod;
8602 set->format = format;
8603
8604 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008605 return eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008606}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008607
8608API const char *
8609lyxp_get_expr(const struct lyxp_expr *path)
8610{
8611 if (!path) {
8612 return NULL;
8613 }
8614
8615 return path->expr;
8616}