blob: 0b65909f88dcca6ad21b6f88754039cdc86a573e [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 */
Christian Hopps32874e12021-05-01 09:43:54 -040014#define _GNU_SOURCE /* asprintf, strdup */
15#include <sys/cdefs.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010016
Radek Krejci535ea9f2020-05-29 16:01:05 +020017#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010018
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010020#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010024#include <stdio.h>
25#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010026#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010027
Radek Krejci535ea9f2020-05-29 16:01:05 +020028#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020029#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020032#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010033#include "out.h"
Radek Krejci7931b192020-06-25 17:05:03 +020034#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020035#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020036#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020038#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020039#include "tree.h"
Radek Krejci77114102021-03-10 15:21:57 +010040#include "tree_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020041#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010042#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020043#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);
Michal Vasko40308e72020-10-20 16:38:40 +020047static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype,
48 struct lyxp_set *set, uint32_t options);
Michal Vasko03ff5a72019-09-11 13:49:33 +020049
50/**
51 * @brief Print the type of an XPath \p set.
52 *
53 * @param[in] set Set to use.
54 * @return Set type string.
55 */
56static const char *
57print_set_type(struct lyxp_set *set)
58{
59 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020060 case LYXP_SET_NODE_SET:
61 return "node set";
62 case LYXP_SET_SCNODE_SET:
63 return "schema node set";
64 case LYXP_SET_BOOLEAN:
65 return "boolean";
66 case LYXP_SET_NUMBER:
67 return "number";
68 case LYXP_SET_STRING:
69 return "string";
70 }
71
72 return NULL;
73}
74
Michal Vasko24cddf82020-06-01 08:17:01 +020075const char *
76lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020077{
78 switch (tok) {
79 case LYXP_TOKEN_PAR1:
80 return "(";
81 case LYXP_TOKEN_PAR2:
82 return ")";
83 case LYXP_TOKEN_BRACK1:
84 return "[";
85 case LYXP_TOKEN_BRACK2:
86 return "]";
87 case LYXP_TOKEN_DOT:
88 return ".";
89 case LYXP_TOKEN_DDOT:
90 return "..";
91 case LYXP_TOKEN_AT:
92 return "@";
93 case LYXP_TOKEN_COMMA:
94 return ",";
95 case LYXP_TOKEN_NAMETEST:
96 return "NameTest";
97 case LYXP_TOKEN_NODETYPE:
98 return "NodeType";
99 case LYXP_TOKEN_FUNCNAME:
100 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200101 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200102 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200103 case LYXP_TOKEN_OPER_EQUAL:
104 return "Operator(Equal)";
105 case LYXP_TOKEN_OPER_NEQUAL:
106 return "Operator(Non-equal)";
107 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200108 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200109 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200110 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200111 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200112 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200113 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200114 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200115 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200116 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200117 case LYXP_TOKEN_LITERAL:
118 return "Literal";
119 case LYXP_TOKEN_NUMBER:
120 return "Number";
121 default:
122 LOGINT(NULL);
123 return "";
124 }
125}
126
127/**
128 * @brief Print the whole expression \p exp to debug output.
129 *
130 * @param[in] exp Expression to use.
131 */
132static void
Michal Vasko40308e72020-10-20 16:38:40 +0200133print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200134{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100135#define MSG_BUFFER_SIZE 128
136 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100137 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200138
Radek Krejci52b6d512020-10-12 12:33:17 +0200139 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200140 return;
141 }
142
143 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
144 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200145 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200146 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100147 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200148 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
149 for (j = 1; exp->repeat[i][j]; ++j) {
150 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
151 }
152 strcat(tmp, ")");
153 }
154 LOGDBG(LY_LDGXPATH, tmp);
155 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100156#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200157}
158
159#ifndef NDEBUG
160
161/**
162 * @brief Print XPath set content to debug output.
163 *
164 * @param[in] set Set to print.
165 */
166static void
167print_set_debug(struct lyxp_set *set)
168{
169 uint32_t i;
170 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200171 struct lyxp_set_node *item;
172 struct lyxp_set_scnode *sitem;
173
Radek Krejci52b6d512020-10-12 12:33:17 +0200174 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200175 return;
176 }
177
178 switch (set->type) {
179 case LYXP_SET_NODE_SET:
180 LOGDBG(LY_LDGXPATH, "set NODE SET:");
181 for (i = 0; i < set->used; ++i) {
182 item = &set->val.nodes[i];
183
184 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100185 case LYXP_NODE_NONE:
186 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
187 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200188 case LYXP_NODE_ROOT:
189 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
190 break;
191 case LYXP_NODE_ROOT_CONFIG:
192 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
193 break;
194 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100195 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200196 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200197 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100198 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200199 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200200 item->node->schema->name, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200201 } else {
202 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
203 }
204 break;
205 case LYXP_NODE_TEXT:
206 if (item->node->schema->nodetype & LYS_ANYDATA) {
207 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200208 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200209 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200210 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200211 }
212 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100213 case LYXP_NODE_META:
214 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
Michal Vasko69730152020-10-09 16:30:07 +0200215 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200216 break;
217 }
218 }
219 break;
220
221 case LYXP_SET_SCNODE_SET:
222 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
223 for (i = 0; i < set->used; ++i) {
224 sitem = &set->val.scnodes[i];
225
226 switch (sitem->type) {
227 case LYXP_NODE_ROOT:
228 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
229 break;
230 case LYXP_NODE_ROOT_CONFIG:
231 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
232 break;
233 case LYXP_NODE_ELEM:
234 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
235 break;
236 default:
237 LOGINT(NULL);
238 break;
239 }
240 }
241 break;
242
Michal Vasko03ff5a72019-09-11 13:49:33 +0200243 case LYXP_SET_BOOLEAN:
244 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200245 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200246 break;
247
248 case LYXP_SET_STRING:
249 LOGDBG(LY_LDGXPATH, "set STRING");
250 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
251 break;
252
253 case LYXP_SET_NUMBER:
254 LOGDBG(LY_LDGXPATH, "set NUMBER");
255
256 if (isnan(set->val.num)) {
257 str = strdup("NaN");
258 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
259 str = strdup("0");
260 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
261 str = strdup("Infinity");
262 } else if (isinf(set->val.num) && signbit(set->val.num)) {
263 str = strdup("-Infinity");
264 } else if ((long long)set->val.num == set->val.num) {
265 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
266 str = NULL;
267 }
268 } else {
269 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
270 str = NULL;
271 }
272 }
273 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
274
275 LOGDBG(LY_LDGXPATH, "\t%s", str);
276 free(str);
277 }
278}
279
280#endif
281
282/**
283 * @brief Realloc the string \p str.
284 *
285 * @param[in] ctx libyang context for logging.
286 * @param[in] needed How much free space is required.
287 * @param[in,out] str Pointer to the string to use.
288 * @param[in,out] used Used bytes in \p str.
289 * @param[in,out] size Allocated bytes in \p str.
290 * @return LY_ERR
291 */
292static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100293cast_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 +0200294{
295 if (*size - *used < needed) {
296 do {
297 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
298 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
299 return LY_EINVAL;
300 }
301 *size += LYXP_STRING_CAST_SIZE_STEP;
302 } while (*size - *used < needed);
303 *str = ly_realloc(*str, *size * sizeof(char));
304 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
305 }
306
307 return LY_SUCCESS;
308}
309
310/**
311 * @brief Cast nodes recursively to one string @p str.
312 *
313 * @param[in] node Node to cast.
314 * @param[in] fake_cont Whether to put the data into a "fake" container.
315 * @param[in] root_type Type of the XPath root.
316 * @param[in] indent Current indent.
317 * @param[in,out] str Resulting string.
318 * @param[in,out] used Used bytes in @p str.
319 * @param[in,out] size Allocated bytes in @p str.
320 * @return LY_ERR
321 */
322static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200323cast_string_recursive(const struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
Radek Krejci0f969882020-08-21 16:56:47 +0200324 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200325{
Radek Krejci7f769d72020-07-11 23:13:56 +0200326 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200327 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200328 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200329 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200330 struct lyd_node_any *any;
331 LY_ERR rc;
332
333 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
334 return LY_SUCCESS;
335 }
336
337 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200338 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200339 LY_CHECK_RET(rc);
340 strcpy(*str + (*used - 1), "\n");
341 ++(*used);
342
343 ++indent;
344 }
345
346 switch (node->schema->nodetype) {
347 case LYS_CONTAINER:
348 case LYS_LIST:
349 case LYS_RPC:
350 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200351 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200352 LY_CHECK_RET(rc);
353 strcpy(*str + (*used - 1), "\n");
354 ++(*used);
355
Radek Krejcia1c1e542020-09-29 16:06:52 +0200356 for (child = lyd_child(node); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200357 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
358 LY_CHECK_RET(rc);
359 }
360
361 break;
362
363 case LYS_LEAF:
364 case LYS_LEAFLIST:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200365 value_str = lyd_get_value(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200366
367 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200368 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 +0200369 memset(*str + (*used - 1), ' ', indent * 2);
370 *used += indent * 2;
371
372 /* print value */
373 if (*used == 1) {
374 sprintf(*str + (*used - 1), "%s", value_str);
375 *used += strlen(value_str);
376 } else {
377 sprintf(*str + (*used - 1), "%s\n", value_str);
378 *used += strlen(value_str) + 1;
379 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200380
381 break;
382
383 case LYS_ANYXML:
384 case LYS_ANYDATA:
385 any = (struct lyd_node_any *)node;
386 if (!(void *)any->value.tree) {
387 /* no content */
388 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200389 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200390 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200391 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100392
Michal Vasko60ea6352020-06-29 13:39:39 +0200393 if (any->value_type == LYD_ANYDATA_LYB) {
394 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200395 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 +0200396 /* successfully parsed */
397 free(any->value.mem);
398 any->value.tree = tree;
399 any->value_type = LYD_ANYDATA_DATATREE;
400 }
Radek Krejci7931b192020-06-25 17:05:03 +0200401 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200402 }
403
Michal Vasko03ff5a72019-09-11 13:49:33 +0200404 switch (any->value_type) {
405 case LYD_ANYDATA_STRING:
406 case LYD_ANYDATA_XML:
407 case LYD_ANYDATA_JSON:
408 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200409 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200410 break;
411 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200412 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200413 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200414 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100415 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200416 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200417 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200418 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200419 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200420 }
421 }
422
423 line = strtok_r(buf, "\n", &ptr);
424 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200425 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200426 if (rc != LY_SUCCESS) {
427 free(buf);
428 return rc;
429 }
430 memset(*str + (*used - 1), ' ', indent * 2);
431 *used += indent * 2;
432
433 strcpy(*str + (*used - 1), line);
434 *used += strlen(line);
435
436 strcpy(*str + (*used - 1), "\n");
437 *used += 1;
438 } while ((line = strtok_r(NULL, "\n", &ptr)));
439
440 free(buf);
441 break;
442
443 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200444 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200445 }
446
447 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200448 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200449 LY_CHECK_RET(rc);
450 strcpy(*str + (*used - 1), "\n");
451 ++(*used);
452
453 --indent;
454 }
455
456 return LY_SUCCESS;
457}
458
459/**
460 * @brief Cast an element into a string.
461 *
462 * @param[in] node Node to cast.
463 * @param[in] fake_cont Whether to put the data into a "fake" container.
464 * @param[in] root_type Type of the XPath root.
465 * @param[out] str Element cast to dynamically-allocated string.
466 * @return LY_ERR
467 */
468static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200469cast_string_elem(struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200470{
471 uint16_t used, size;
472 LY_ERR rc;
473
474 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200475 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200476 (*str)[0] = '\0';
477 used = 1;
478 size = LYXP_STRING_CAST_SIZE_START;
479
480 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
481 if (rc != LY_SUCCESS) {
482 free(*str);
483 return rc;
484 }
485
486 if (size > used) {
487 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200488 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200489 }
490 return LY_SUCCESS;
491}
492
493/**
494 * @brief Cast a LYXP_SET_NODE_SET set into a string.
495 * Context position aware.
496 *
497 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200498 * @param[out] str Cast dynamically-allocated string.
499 * @return LY_ERR
500 */
501static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100502cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200503{
Michal Vaskoaa677062021-03-09 13:52:53 +0100504 if (!set->used) {
505 *str = strdup("");
506 if (!*str) {
507 LOGMEM_RET(set->ctx);
508 }
509 return LY_SUCCESS;
510 }
511
Michal Vasko03ff5a72019-09-11 13:49:33 +0200512 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100513 case LYXP_NODE_NONE:
514 /* invalid */
515 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200516 case LYXP_NODE_ROOT:
517 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100518 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200519 case LYXP_NODE_ELEM:
520 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100521 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100522 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200523 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200524 if (!*str) {
525 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200526 }
527 return LY_SUCCESS;
528 }
529
530 LOGINT_RET(set->ctx);
531}
532
533/**
534 * @brief Cast a string into an XPath number.
535 *
536 * @param[in] str String to use.
537 * @return Cast number.
538 */
539static long double
540cast_string_to_number(const char *str)
541{
542 long double num;
543 char *ptr;
544
545 errno = 0;
546 num = strtold(str, &ptr);
547 if (errno || *ptr) {
548 num = NAN;
549 }
550 return num;
551}
552
553/**
554 * @brief Callback for checking value equality.
555 *
Michal Vasko62524a92021-02-26 10:08:50 +0100556 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200557 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200558 * @param[in] val1_p First value.
559 * @param[in] val2_p Second value.
560 * @param[in] mod Whether hash table is being modified.
561 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200562 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200563 */
Radek Krejci857189e2020-09-01 13:26:36 +0200564static ly_bool
565set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200566{
567 struct lyxp_set_hash_node *val1, *val2;
568
569 val1 = (struct lyxp_set_hash_node *)val1_p;
570 val2 = (struct lyxp_set_hash_node *)val2_p;
571
572 if ((val1->node == val2->node) && (val1->type == val2->type)) {
573 return 1;
574 }
575
576 return 0;
577}
578
579/**
580 * @brief Insert node and its hash into set.
581 *
582 * @param[in] set et to insert to.
583 * @param[in] node Node with hash.
584 * @param[in] type Node type.
585 */
586static void
587set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
588{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200589 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200590 uint32_t i, hash;
591 struct lyxp_set_hash_node hnode;
592
593 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
594 /* create hash table and add all the nodes */
595 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
596 for (i = 0; i < set->used; ++i) {
597 hnode.node = set->val.nodes[i].node;
598 hnode.type = set->val.nodes[i].type;
599
600 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
601 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
602 hash = dict_hash_multi(hash, NULL, 0);
603
604 r = lyht_insert(set->ht, &hnode, hash, NULL);
605 assert(!r);
606 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200607
Michal Vasko9d6befd2019-12-11 14:56:56 +0100608 if (hnode.node == node) {
609 /* it was just added, do not add it twice */
610 node = NULL;
611 }
612 }
613 }
614
615 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200616 /* add the new node into hash table */
617 hnode.node = node;
618 hnode.type = type;
619
620 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
621 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
622 hash = dict_hash_multi(hash, NULL, 0);
623
624 r = lyht_insert(set->ht, &hnode, hash, NULL);
625 assert(!r);
626 (void)r;
627 }
628}
629
630/**
631 * @brief Remove node and its hash from set.
632 *
633 * @param[in] set Set to remove from.
634 * @param[in] node Node to remove.
635 * @param[in] type Node type.
636 */
637static void
638set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
639{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200640 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200641 struct lyxp_set_hash_node hnode;
642 uint32_t hash;
643
644 if (set->ht) {
645 hnode.node = node;
646 hnode.type = type;
647
648 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
649 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
650 hash = dict_hash_multi(hash, NULL, 0);
651
652 r = lyht_remove(set->ht, &hnode, hash);
653 assert(!r);
654 (void)r;
655
656 if (!set->ht->used) {
657 lyht_free(set->ht);
658 set->ht = NULL;
659 }
660 }
661}
662
663/**
664 * @brief Check whether node is in set based on its hash.
665 *
666 * @param[in] set Set to search in.
667 * @param[in] node Node to search for.
668 * @param[in] type Node type.
669 * @param[in] skip_idx Index in @p set to skip.
670 * @return LY_ERR
671 */
672static LY_ERR
673set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
674{
675 struct lyxp_set_hash_node hnode, *match_p;
676 uint32_t hash;
677
678 hnode.node = node;
679 hnode.type = type;
680
681 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
682 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
683 hash = dict_hash_multi(hash, NULL, 0);
684
685 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
686 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
687 /* we found it on the index that should be skipped, find another */
688 hnode = *match_p;
689 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
690 /* none other found */
691 return LY_SUCCESS;
692 }
693 }
694
695 return LY_EEXIST;
696 }
697
698 /* not found */
699 return LY_SUCCESS;
700}
701
Michal Vaskod3678892020-05-21 10:06:58 +0200702void
703lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200704{
705 if (!set) {
706 return;
707 }
708
709 if (set->type == LYXP_SET_NODE_SET) {
710 free(set->val.nodes);
711 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200712 } else if (set->type == LYXP_SET_SCNODE_SET) {
713 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200714 lyht_free(set->ht);
715 } else {
716 if (set->type == LYXP_SET_STRING) {
717 free(set->val.str);
718 }
719 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200720 }
Michal Vaskod3678892020-05-21 10:06:58 +0200721
722 set->val.nodes = NULL;
723 set->used = 0;
724 set->size = 0;
725 set->ht = NULL;
726 set->ctx_pos = 0;
727 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200728}
729
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100730/**
731 * @brief Free dynamically-allocated set.
732 *
733 * @param[in] set Set to free.
734 */
735static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200736lyxp_set_free(struct lyxp_set *set)
737{
738 if (!set) {
739 return;
740 }
741
Michal Vaskod3678892020-05-21 10:06:58 +0200742 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200743 free(set);
744}
745
746/**
747 * @brief Initialize set context.
748 *
749 * @param[in] new Set to initialize.
750 * @param[in] set Arbitrary initialized set.
751 */
752static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200753set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200754{
755 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200756 if (set) {
757 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200758 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100759 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200760 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100761 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200762 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200763 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200764 new->prefix_data = set->prefix_data;
Michal Vasko02a77382019-09-12 11:47:35 +0200765 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200766}
767
768/**
769 * @brief Create a deep copy of a set.
770 *
771 * @param[in] set Set to copy.
772 * @return Copy of @p set.
773 */
774static struct lyxp_set *
775set_copy(struct lyxp_set *set)
776{
777 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100778 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200779
780 if (!set) {
781 return NULL;
782 }
783
784 ret = malloc(sizeof *ret);
785 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
786 set_init(ret, set);
787
788 if (set->type == LYXP_SET_SCNODE_SET) {
789 ret->type = set->type;
790
791 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100792 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
793 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200794 uint32_t idx;
795 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
796 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100797 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200798 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200799 lyxp_set_free(ret);
800 return NULL;
801 }
Michal Vaskoba716542019-12-16 10:01:58 +0100802 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200803 }
804 }
805 } else if (set->type == LYXP_SET_NODE_SET) {
806 ret->type = set->type;
807 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
808 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
809 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
810
811 ret->used = ret->size = set->used;
812 ret->ctx_pos = set->ctx_pos;
813 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100814 if (set->ht) {
815 ret->ht = lyht_dup(set->ht);
816 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200817 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200818 memcpy(ret, set, sizeof *ret);
819 if (set->type == LYXP_SET_STRING) {
820 ret->val.str = strdup(set->val.str);
821 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
822 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200823 }
824
825 return ret;
826}
827
828/**
829 * @brief Fill XPath set with a string. Any current data are disposed of.
830 *
831 * @param[in] set Set to fill.
832 * @param[in] string String to fill into \p set.
833 * @param[in] str_len Length of \p string. 0 is a valid value!
834 */
835static void
836set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
837{
Michal Vaskod3678892020-05-21 10:06:58 +0200838 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200839
840 set->type = LYXP_SET_STRING;
841 if ((str_len == 0) && (string[0] != '\0')) {
842 string = "";
843 }
844 set->val.str = strndup(string, str_len);
845}
846
847/**
848 * @brief Fill XPath set with a number. Any current data are disposed of.
849 *
850 * @param[in] set Set to fill.
851 * @param[in] number Number to fill into \p set.
852 */
853static void
854set_fill_number(struct lyxp_set *set, long double number)
855{
Michal Vaskod3678892020-05-21 10:06:58 +0200856 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200857
858 set->type = LYXP_SET_NUMBER;
859 set->val.num = number;
860}
861
862/**
863 * @brief Fill XPath set with a boolean. Any current data are disposed of.
864 *
865 * @param[in] set Set to fill.
866 * @param[in] boolean Boolean to fill into \p set.
867 */
868static void
Radek Krejci857189e2020-09-01 13:26:36 +0200869set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200870{
Michal Vaskod3678892020-05-21 10:06:58 +0200871 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200872
873 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200874 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200875}
876
877/**
878 * @brief Fill XPath set with the value from another set (deep assign).
879 * Any current data are disposed of.
880 *
881 * @param[in] trg Set to fill.
882 * @param[in] src Source set to copy into \p trg.
883 */
884static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200885set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200886{
887 if (!trg || !src) {
888 return;
889 }
890
891 if (trg->type == LYXP_SET_NODE_SET) {
892 free(trg->val.nodes);
893 } else if (trg->type == LYXP_SET_STRING) {
894 free(trg->val.str);
895 }
896 set_init(trg, src);
897
898 if (src->type == LYXP_SET_SCNODE_SET) {
899 trg->type = LYXP_SET_SCNODE_SET;
900 trg->used = src->used;
901 trg->size = src->used;
902
903 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
904 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
905 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
906 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200907 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200908 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200909 set_fill_number(trg, src->val.num);
910 } else if (src->type == LYXP_SET_STRING) {
911 set_fill_string(trg, src->val.str, strlen(src->val.str));
912 } else {
913 if (trg->type == LYXP_SET_NODE_SET) {
914 free(trg->val.nodes);
915 } else if (trg->type == LYXP_SET_STRING) {
916 free(trg->val.str);
917 }
918
Michal Vaskod3678892020-05-21 10:06:58 +0200919 assert(src->type == LYXP_SET_NODE_SET);
920
921 trg->type = LYXP_SET_NODE_SET;
922 trg->used = src->used;
923 trg->size = src->used;
924 trg->ctx_pos = src->ctx_pos;
925 trg->ctx_size = src->ctx_size;
926
927 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
928 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
929 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
930 if (src->ht) {
931 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200932 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200933 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200934 }
935 }
936}
937
938/**
939 * @brief Clear context of all schema nodes.
940 *
941 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +0200942 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200943 */
944static void
Michal Vasko1a09b212021-05-06 13:00:10 +0200945set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200946{
947 uint32_t i;
948
949 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100950 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +0200951 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100952 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
953 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200954 }
955 }
956}
957
958/**
959 * @brief Remove a node from a set. Removing last node changes
960 * set into LYXP_SET_EMPTY. Context position aware.
961 *
962 * @param[in] set Set to use.
963 * @param[in] idx Index from @p set of the node to be removed.
964 */
965static void
966set_remove_node(struct lyxp_set *set, uint32_t idx)
967{
968 assert(set && (set->type == LYXP_SET_NODE_SET));
969 assert(idx < set->used);
970
971 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
972
973 --set->used;
974 if (set->used) {
975 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
976 (set->used - idx) * sizeof *set->val.nodes);
977 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200978 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200979 }
980}
981
982/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100983 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200984 *
985 * @param[in] set Set to use.
986 * @param[in] idx Index from @p set of the node to be removed.
987 */
988static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100989set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200990{
991 assert(set && (set->type == LYXP_SET_NODE_SET));
992 assert(idx < set->used);
993
Michal Vasko2caefc12019-11-14 16:07:56 +0100994 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
995 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
996 }
997 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +0200998}
999
1000/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001001 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001002 * set into LYXP_SET_EMPTY. Context position aware.
1003 *
1004 * @param[in] set Set to consolidate.
1005 */
1006static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001007set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001008{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001009 uint32_t i, orig_used, end = 0;
1010 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001011
Michal Vaskod3678892020-05-21 10:06:58 +02001012 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001013
1014 orig_used = set->used;
1015 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001016 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001017 start = -1;
1018 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001019 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001020 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001021 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001022 end = i;
1023 ++i;
1024 break;
1025 }
1026
1027 ++i;
1028 if (i == orig_used) {
1029 end = i;
1030 }
1031 } while (i < orig_used);
1032
1033 if (start > -1) {
1034 /* move the whole chunk of valid nodes together */
1035 if (set->used != (unsigned)start) {
1036 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1037 }
1038 set->used += end - start;
1039 }
1040 }
Michal Vasko57eab132019-09-24 11:46:26 +02001041}
1042
1043/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001044 * @brief Check for duplicates in a node set.
1045 *
1046 * @param[in] set Set to check.
1047 * @param[in] node Node to look for in @p set.
1048 * @param[in] node_type Type of @p node.
1049 * @param[in] skip_idx Index from @p set to skip.
1050 * @return LY_ERR
1051 */
1052static LY_ERR
1053set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1054{
1055 uint32_t i;
1056
Michal Vasko2caefc12019-11-14 16:07:56 +01001057 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001058 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1059 }
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.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1067 return LY_EEXIST;
1068 }
1069 }
1070
1071 return LY_SUCCESS;
1072}
1073
Radek Krejci857189e2020-09-01 13:26:36 +02001074ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001075lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1076 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001077{
1078 uint32_t i;
1079
1080 for (i = 0; i < set->used; ++i) {
1081 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1082 continue;
1083 }
1084
1085 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001086 if (index_p) {
1087 *index_p = i;
1088 }
1089 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001090 }
1091 }
1092
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001093 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001094}
1095
Michal Vaskoecd62de2019-11-13 12:35:11 +01001096void
1097lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001098{
1099 uint32_t orig_used, i, j;
1100
Michal Vaskod3678892020-05-21 10:06:58 +02001101 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001102
Michal Vaskod3678892020-05-21 10:06:58 +02001103 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001104 return;
1105 }
1106
Michal Vaskod3678892020-05-21 10:06:58 +02001107 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001108 memcpy(set1, set2, sizeof *set1);
1109 return;
1110 }
1111
1112 if (set1->used + set2->used > set1->size) {
1113 set1->size = set1->used + set2->used;
1114 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1115 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1116 }
1117
1118 orig_used = set1->used;
1119
1120 for (i = 0; i < set2->used; ++i) {
1121 for (j = 0; j < orig_used; ++j) {
1122 /* detect duplicities */
1123 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1124 break;
1125 }
1126 }
1127
Michal Vasko0587bce2020-12-10 12:19:21 +01001128 if (j < orig_used) {
1129 /* node is there, but update its status if needed */
1130 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1131 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001132 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1133 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1134 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001135 }
1136 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001137 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1138 ++set1->used;
1139 }
1140 }
1141
Michal Vaskod3678892020-05-21 10:06:58 +02001142 lyxp_set_free_content(set2);
1143 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001144}
1145
1146/**
1147 * @brief Insert a node into a set. Context position aware.
1148 *
1149 * @param[in] set Set to use.
1150 * @param[in] node Node to insert to @p set.
1151 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1152 * @param[in] node_type Node type of @p node.
1153 * @param[in] idx Index in @p set to insert into.
1154 */
1155static void
1156set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1157{
Michal Vaskod3678892020-05-21 10:06:58 +02001158 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001159
Michal Vaskod3678892020-05-21 10:06:58 +02001160 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001161 /* first item */
1162 if (idx) {
1163 /* no real harm done, but it is a bug */
1164 LOGINT(set->ctx);
1165 idx = 0;
1166 }
1167 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1168 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1169 set->type = LYXP_SET_NODE_SET;
1170 set->used = 0;
1171 set->size = LYXP_SET_SIZE_START;
1172 set->ctx_pos = 1;
1173 set->ctx_size = 1;
1174 set->ht = NULL;
1175 } else {
1176 /* not an empty set */
1177 if (set->used == set->size) {
1178
1179 /* set is full */
1180 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1181 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1182 set->size += LYXP_SET_SIZE_STEP;
1183 }
1184
1185 if (idx > set->used) {
1186 LOGINT(set->ctx);
1187 idx = set->used;
1188 }
1189
1190 /* make space for the new node */
1191 if (idx < set->used) {
1192 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1193 }
1194 }
1195
1196 /* finally assign the value */
1197 set->val.nodes[idx].node = (struct lyd_node *)node;
1198 set->val.nodes[idx].type = node_type;
1199 set->val.nodes[idx].pos = pos;
1200 ++set->used;
1201
Michal Vasko2caefc12019-11-14 16:07:56 +01001202 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1203 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1204 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001205}
1206
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001207LY_ERR
1208lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001209{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001210 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001211
1212 assert(set->type == LYXP_SET_SCNODE_SET);
1213
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001214 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001215 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001216 } else {
1217 if (set->used == set->size) {
1218 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes);
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001219 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001220 set->size += LYXP_SET_SIZE_STEP;
1221 }
1222
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001223 index = set->used;
1224 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1225 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001226 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001227 ++set->used;
1228 }
1229
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001230 if (index_p) {
1231 *index_p = index;
1232 }
1233
1234 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001235}
1236
1237/**
1238 * @brief Replace a node in a set with another. Context position aware.
1239 *
1240 * @param[in] set Set to use.
1241 * @param[in] node Node to insert to @p set.
1242 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1243 * @param[in] node_type Node type of @p node.
1244 * @param[in] idx Index in @p set of the node to replace.
1245 */
1246static void
1247set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1248{
1249 assert(set && (idx < set->used));
1250
Michal Vasko2caefc12019-11-14 16:07:56 +01001251 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1252 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1253 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001254 set->val.nodes[idx].node = (struct lyd_node *)node;
1255 set->val.nodes[idx].type = node_type;
1256 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001257 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1258 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1259 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001260}
1261
1262/**
1263 * @brief Set all nodes with ctx 1 to a new unique context value.
1264 *
1265 * @param[in] set Set to modify.
1266 * @return New context value.
1267 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001268static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001269set_scnode_new_in_ctx(struct lyxp_set *set)
1270{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001271 uint32_t i;
1272 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001273
1274 assert(set->type == LYXP_SET_SCNODE_SET);
1275
Radek Krejcif13b87b2020-12-01 22:02:17 +01001276 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001277retry:
1278 for (i = 0; i < set->used; ++i) {
1279 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1280 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1281 goto retry;
1282 }
1283 }
1284 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001285 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001286 set->val.scnodes[i].in_ctx = ret_ctx;
1287 }
1288 }
1289
1290 return ret_ctx;
1291}
1292
1293/**
1294 * @brief Get unique @p node position in the data.
1295 *
1296 * @param[in] node Node to find.
1297 * @param[in] node_type Node type of @p node.
1298 * @param[in] root Root node.
1299 * @param[in] root_type Type of the XPath @p root node.
1300 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1301 * be used to increase efficiency and start the DFS from this node.
1302 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1303 * @return Node position.
1304 */
1305static uint32_t
1306get_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 +02001307 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001308{
Michal Vasko56daf732020-08-10 10:57:18 +02001309 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001310 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001311 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001312
1313 assert(prev && prev_pos && !root->prev->next);
1314
1315 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1316 return 0;
1317 }
1318
1319 if (*prev) {
1320 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001321 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001322 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001323 goto dfs_search;
1324 }
1325
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001326 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001327 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001328dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001329 if (*prev && !elem) {
1330 /* resume previous DFS */
1331 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1332 LYD_TREE_DFS_continue = 0;
1333 }
1334
Michal Vasko03ff5a72019-09-11 13:49:33 +02001335 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001336 /* skip */
1337 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001338 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001339 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001340 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001341 break;
1342 }
Michal Vasko56daf732020-08-10 10:57:18 +02001343 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001344 }
Michal Vasko56daf732020-08-10 10:57:18 +02001345
1346 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001347 }
1348
1349 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001350 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001351 break;
1352 }
1353 }
1354
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001355 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001356 if (!(*prev)) {
1357 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001358 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001359 return 0;
1360 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001361 /* start the search again from the beginning */
1362 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001363
Michal Vasko56daf732020-08-10 10:57:18 +02001364 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001365 pos = 1;
1366 goto dfs_search;
1367 }
1368 }
1369
1370 /* remember the last found node for next time */
1371 *prev = node;
1372 *prev_pos = pos;
1373
1374 return pos;
1375}
1376
1377/**
1378 * @brief Assign (fill) missing node positions.
1379 *
1380 * @param[in] set Set to fill positions in.
1381 * @param[in] root Context root node.
1382 * @param[in] root_type Context root type.
1383 * @return LY_ERR
1384 */
1385static LY_ERR
1386set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1387{
1388 const struct lyd_node *prev = NULL, *tmp_node;
1389 uint32_t i, tmp_pos = 0;
1390
1391 for (i = 0; i < set->used; ++i) {
1392 if (!set->val.nodes[i].pos) {
1393 tmp_node = NULL;
1394 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001395 case LYXP_NODE_META:
1396 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001397 if (!tmp_node) {
1398 LOGINT_RET(root->schema->module->ctx);
1399 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001400 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001401 case LYXP_NODE_ELEM:
1402 case LYXP_NODE_TEXT:
1403 if (!tmp_node) {
1404 tmp_node = set->val.nodes[i].node;
1405 }
1406 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1407 break;
1408 default:
1409 /* all roots have position 0 */
1410 break;
1411 }
1412 }
1413 }
1414
1415 return LY_SUCCESS;
1416}
1417
1418/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001419 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001420 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001421 * @param[in] meta Metadata to use.
1422 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001423 */
1424static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001425get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001426{
1427 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001428 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001429
Michal Vasko9f96a052020-03-10 09:41:45 +01001430 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001431 ++pos;
1432 }
1433
Michal Vasko9f96a052020-03-10 09:41:45 +01001434 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001435 return pos;
1436}
1437
1438/**
1439 * @brief Compare 2 nodes in respect to XPath document order.
1440 *
1441 * @param[in] item1 1st node.
1442 * @param[in] item2 2nd node.
1443 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1444 */
1445static int
1446set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1447{
Michal Vasko9f96a052020-03-10 09:41:45 +01001448 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001449
1450 if (item1->pos < item2->pos) {
1451 return -1;
1452 }
1453
1454 if (item1->pos > item2->pos) {
1455 return 1;
1456 }
1457
1458 /* node positions are equal, the fun case */
1459
1460 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1461 /* special case since text nodes are actually saved as their parents */
1462 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1463 if (item1->type == LYXP_NODE_ELEM) {
1464 assert(item2->type == LYXP_NODE_TEXT);
1465 return -1;
1466 } else {
1467 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1468 return 1;
1469 }
1470 }
1471
Michal Vasko9f96a052020-03-10 09:41:45 +01001472 /* we need meta positions now */
1473 if (item1->type == LYXP_NODE_META) {
1474 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001475 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001476 if (item2->type == LYXP_NODE_META) {
1477 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001478 }
1479
Michal Vasko9f96a052020-03-10 09:41:45 +01001480 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001481 /* check for duplicates */
1482 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001483 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001484 return 0;
1485 }
1486
Michal Vasko9f96a052020-03-10 09:41:45 +01001487 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001488 /* elem is always first, 2nd node is after it */
1489 if (item1->type == LYXP_NODE_ELEM) {
1490 assert(item2->type != LYXP_NODE_ELEM);
1491 return -1;
1492 }
1493
Michal Vasko9f96a052020-03-10 09:41:45 +01001494 /* 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 +02001495 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001496 if (((item1->type == LYXP_NODE_TEXT) &&
1497 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1498 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1499 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1500 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001501 return 1;
1502 }
1503
Michal Vasko9f96a052020-03-10 09:41:45 +01001504 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001505 /* 2nd is after 1st */
1506 return -1;
1507}
1508
1509/**
1510 * @brief Set cast for comparisons.
1511 *
1512 * @param[in] trg Target set to cast source into.
1513 * @param[in] src Source set.
1514 * @param[in] type Target set type.
1515 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001516 * @return LY_ERR
1517 */
1518static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001519set_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 +02001520{
1521 assert(src->type == LYXP_SET_NODE_SET);
1522
1523 set_init(trg, src);
1524
1525 /* insert node into target set */
1526 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1527
1528 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001529 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001530}
1531
Michal Vasko4c7763f2020-07-27 17:40:37 +02001532/**
1533 * @brief Set content canonization for comparisons.
1534 *
1535 * @param[in] trg Target set to put the canononized source into.
1536 * @param[in] src Source set.
1537 * @param[in] xp_node Source XPath node/meta to use for canonization.
1538 * @return LY_ERR
1539 */
1540static LY_ERR
1541set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1542{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001543 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001544 struct lyd_value val;
1545 struct ly_err_item *err = NULL;
1546 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001547 LY_ERR rc;
1548
1549 /* is there anything to canonize even? */
1550 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1551 /* do we have a type to use for canonization? */
1552 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1553 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1554 } else if (xp_node->type == LYXP_NODE_META) {
1555 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1556 }
1557 }
1558 if (!type) {
1559 goto fill;
1560 }
1561
1562 if (src->type == LYXP_SET_NUMBER) {
1563 /* canonize number */
1564 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1565 LOGMEM(src->ctx);
1566 return LY_EMEM;
1567 }
1568 } else {
1569 /* canonize string */
1570 str = strdup(src->val.str);
1571 }
1572
1573 /* ignore errors, the value may not satisfy schema constraints */
Radek Krejci0b013302021-03-29 15:22:32 +02001574 rc = type->plugin->store(src->ctx, type, str, strlen(str), LYPLG_TYPE_STORE_DYNAMIC, src->format, src->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001575 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001576 ly_err_free(err);
1577 if (rc) {
1578 /* invalid value */
Radek IÅ¡a48460222021-03-02 15:18:32 +01001579 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001580 goto fill;
1581 }
1582
Michal Vasko4c7763f2020-07-27 17:40:37 +02001583 /* use the canonized value */
1584 set_init(trg, src);
1585 trg->type = src->type;
1586 if (src->type == LYXP_SET_NUMBER) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001587 trg->val.num = strtold(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001588 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1589 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001590 trg->val.str = strdup(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko4c7763f2020-07-27 17:40:37 +02001591 }
1592 type->plugin->free(src->ctx, &val);
1593 return LY_SUCCESS;
1594
1595fill:
1596 /* no canonization needed/possible */
1597 set_fill_set(trg, src);
1598 return LY_SUCCESS;
1599}
1600
Michal Vasko03ff5a72019-09-11 13:49:33 +02001601#ifndef NDEBUG
1602
1603/**
1604 * @brief Bubble sort @p set into XPath document order.
1605 * Context position aware. Unused in the 'Release' build target.
1606 *
1607 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001608 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1609 */
1610static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001611set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001612{
1613 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001614 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001615 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001616 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001617 struct lyxp_set_node item;
1618 struct lyxp_set_hash_node hnode;
1619 uint64_t hash;
1620
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001621 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001622 return 0;
1623 }
1624
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001625 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001626 for (root = set->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001627 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001628
1629 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001630 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001631 return -1;
1632 }
1633
1634 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1635 print_set_debug(set);
1636
1637 for (i = 0; i < set->used; ++i) {
1638 inverted = 0;
1639 change = 0;
1640
1641 for (j = 1; j < set->used - i; ++j) {
1642 /* compare node positions */
1643 if (inverted) {
1644 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1645 } else {
1646 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1647 }
1648
1649 /* swap if needed */
1650 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1651 change = 1;
1652
1653 item = set->val.nodes[j - 1];
1654 set->val.nodes[j - 1] = set->val.nodes[j];
1655 set->val.nodes[j] = item;
1656 } else {
1657 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1658 inverted = !inverted;
1659 }
1660 }
1661
1662 ++ret;
1663
1664 if (!change) {
1665 break;
1666 }
1667 }
1668
1669 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1670 print_set_debug(set);
1671
1672 /* check node hashes */
1673 if (set->used >= LYD_HT_MIN_ITEMS) {
1674 assert(set->ht);
1675 for (i = 0; i < set->used; ++i) {
1676 hnode.node = set->val.nodes[i].node;
1677 hnode.type = set->val.nodes[i].type;
1678
1679 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1680 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1681 hash = dict_hash_multi(hash, NULL, 0);
1682
1683 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1684 }
1685 }
1686
1687 return ret - 1;
1688}
1689
1690/**
1691 * @brief Remove duplicate entries in a sorted node set.
1692 *
1693 * @param[in] set Sorted set to check.
1694 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1695 */
1696static LY_ERR
1697set_sorted_dup_node_clean(struct lyxp_set *set)
1698{
1699 uint32_t i = 0;
1700 LY_ERR ret = LY_SUCCESS;
1701
1702 if (set->used > 1) {
1703 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001704 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1705 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001706 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001707 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001708 }
Michal Vasko57eab132019-09-24 11:46:26 +02001709 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001710 }
1711 }
1712
Michal Vasko2caefc12019-11-14 16:07:56 +01001713 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001714 return ret;
1715}
1716
1717#endif
1718
1719/**
1720 * @brief Merge 2 sorted sets into one.
1721 *
1722 * @param[in,out] trg Set to merge into. Duplicates are removed.
1723 * @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 +02001724 * @return LY_ERR
1725 */
1726static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001727set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001728{
1729 uint32_t i, j, k, count, dup_count;
1730 int cmp;
1731 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001732
Michal Vaskod3678892020-05-21 10:06:58 +02001733 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001734 return LY_EINVAL;
1735 }
1736
Michal Vaskod3678892020-05-21 10:06:58 +02001737 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001738 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001739 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001740 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001741 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001742 return LY_SUCCESS;
1743 }
1744
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001745 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001746 for (root = trg->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001747 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001748
1749 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001750 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001751 return LY_EINT;
1752 }
1753
1754#ifndef NDEBUG
1755 LOGDBG(LY_LDGXPATH, "MERGE target");
1756 print_set_debug(trg);
1757 LOGDBG(LY_LDGXPATH, "MERGE source");
1758 print_set_debug(src);
1759#endif
1760
1761 /* make memory for the merge (duplicates are not detected yet, so space
1762 * will likely be wasted on them, too bad) */
1763 if (trg->size - trg->used < src->used) {
1764 trg->size = trg->used + src->used;
1765
1766 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1767 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1768 }
1769
1770 i = 0;
1771 j = 0;
1772 count = 0;
1773 dup_count = 0;
1774 do {
1775 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1776 if (!cmp) {
1777 if (!count) {
1778 /* duplicate, just skip it */
1779 ++i;
1780 ++j;
1781 } else {
1782 /* we are copying something already, so let's copy the duplicate too,
1783 * we are hoping that afterwards there are some more nodes to
1784 * copy and this way we can copy them all together */
1785 ++count;
1786 ++dup_count;
1787 ++i;
1788 ++j;
1789 }
1790 } else if (cmp < 0) {
1791 /* inserting src node into trg, just remember it for now */
1792 ++count;
1793 ++i;
1794
1795 /* insert the hash now */
1796 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1797 } else if (count) {
1798copy_nodes:
1799 /* time to actually copy the nodes, we have found the largest block of nodes */
1800 memmove(&trg->val.nodes[j + (count - dup_count)],
1801 &trg->val.nodes[j],
1802 (trg->used - j) * sizeof *trg->val.nodes);
1803 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1804
1805 trg->used += count - dup_count;
1806 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1807 j += count - dup_count;
1808
1809 count = 0;
1810 dup_count = 0;
1811 } else {
1812 ++j;
1813 }
1814 } while ((i < src->used) && (j < trg->used));
1815
1816 if ((i < src->used) || count) {
1817 /* insert all the hashes first */
1818 for (k = i; k < src->used; ++k) {
1819 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1820 }
1821
1822 /* loop ended, but we need to copy something at trg end */
1823 count += src->used - i;
1824 i = src->used;
1825 goto copy_nodes;
1826 }
1827
1828 /* we are inserting hashes before the actual node insert, which causes
1829 * situations when there were initially not enough items for a hash table,
1830 * but even after some were inserted, hash table was not created (during
1831 * insertion the number of items is not updated yet) */
1832 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1833 set_insert_node_hash(trg, NULL, 0);
1834 }
1835
1836#ifndef NDEBUG
1837 LOGDBG(LY_LDGXPATH, "MERGE result");
1838 print_set_debug(trg);
1839#endif
1840
Michal Vaskod3678892020-05-21 10:06:58 +02001841 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001842 return LY_SUCCESS;
1843}
1844
1845/*
1846 * (re)parse functions
1847 *
1848 * Parse functions parse the expression into
1849 * tokens (syntactic analysis).
1850 *
1851 * Reparse functions perform semantic analysis
1852 * (do not save the result, just a check) of
1853 * the expression and fill repeat indices.
1854 */
1855
Michal Vasko14676352020-05-29 11:35:55 +02001856LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001857lyxp_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 +02001858{
Michal Vasko004d3152020-06-11 19:59:22 +02001859 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001860 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001861 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001862 }
Michal Vasko14676352020-05-29 11:35:55 +02001863 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001864 }
1865
Michal Vasko004d3152020-06-11 19:59:22 +02001866 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001867 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001868 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001869 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001870 }
Michal Vasko14676352020-05-29 11:35:55 +02001871 return LY_ENOT;
1872 }
1873
1874 return LY_SUCCESS;
1875}
1876
Michal Vasko004d3152020-06-11 19:59:22 +02001877LY_ERR
1878lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1879{
1880 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1881
1882 /* skip the token */
1883 ++(*tok_idx);
1884
1885 return LY_SUCCESS;
1886}
1887
Michal Vasko14676352020-05-29 11:35:55 +02001888/* just like lyxp_check_token() but tests for 2 tokens */
1889static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001890exp_check_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok1,
Radek Krejci0f969882020-08-21 16:56:47 +02001891 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001892{
Michal Vasko004d3152020-06-11 19:59:22 +02001893 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001894 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001895 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001896 }
1897 return LY_EINCOMPLETE;
1898 }
1899
Michal Vasko004d3152020-06-11 19:59:22 +02001900 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001901 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001902 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001903 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001904 }
1905 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001906 }
1907
1908 return LY_SUCCESS;
1909}
1910
1911/**
1912 * @brief Stack operation push on the repeat array.
1913 *
1914 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001915 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001916 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1917 */
1918static void
Michal Vasko004d3152020-06-11 19:59:22 +02001919exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001920{
1921 uint16_t i;
1922
Michal Vasko004d3152020-06-11 19:59:22 +02001923 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001924 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001925 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1926 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1927 exp->repeat[tok_idx][i] = repeat_op_idx;
1928 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001929 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001930 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1931 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1932 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001933 }
1934}
1935
1936/**
1937 * @brief Reparse Predicate. Logs directly on error.
1938 *
1939 * [7] Predicate ::= '[' Expr ']'
1940 *
1941 * @param[in] ctx Context for logging.
1942 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001943 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001944 * @return LY_ERR
1945 */
1946static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001947reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001948{
1949 LY_ERR rc;
1950
Michal Vasko004d3152020-06-11 19:59:22 +02001951 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001952 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001953 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001954
Michal Vasko004d3152020-06-11 19:59:22 +02001955 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001956 LY_CHECK_RET(rc);
1957
Michal Vasko004d3152020-06-11 19:59:22 +02001958 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001959 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001960 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001961
1962 return LY_SUCCESS;
1963}
1964
1965/**
1966 * @brief Reparse RelativeLocationPath. Logs directly on error.
1967 *
1968 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1969 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1970 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1971 *
1972 * @param[in] ctx Context for logging.
1973 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001974 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001975 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1976 */
1977static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001978reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001979{
1980 LY_ERR rc;
1981
Michal Vasko004d3152020-06-11 19:59:22 +02001982 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001983 LY_CHECK_RET(rc);
1984
1985 goto step;
1986 do {
1987 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001988 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001989
Michal Vasko004d3152020-06-11 19:59:22 +02001990 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001991 LY_CHECK_RET(rc);
1992step:
1993 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001994 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001995 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001996 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001997 break;
1998
1999 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002000 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002001 break;
2002
2003 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002004 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002005
Michal Vasko004d3152020-06-11 19:59:22 +02002006 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002007 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002008 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002009 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002010 return LY_EVALID;
2011 }
Radek Krejci0f969882020-08-21 16:56:47 +02002012 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002013 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002014 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002015 goto reparse_predicate;
2016 break;
2017
2018 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002019 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002020
2021 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002022 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002023 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002024 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002025
2026 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002027 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002028 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002029 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002030
2031reparse_predicate:
2032 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002033 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2034 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002035 LY_CHECK_RET(rc);
2036 }
2037 break;
2038 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002039 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002040 return LY_EVALID;
2041 }
Michal Vasko004d3152020-06-11 19:59:22 +02002042 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002043
2044 return LY_SUCCESS;
2045}
2046
2047/**
2048 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2049 *
2050 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2051 *
2052 * @param[in] ctx Context for logging.
2053 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002054 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002055 * @return LY_ERR
2056 */
2057static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002058reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002059{
2060 LY_ERR rc;
2061
Michal Vasko004d3152020-06-11 19:59:22 +02002062 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 +02002063
2064 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002065 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002066 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002067 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002068
Michal Vasko004d3152020-06-11 19:59:22 +02002069 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002070 return LY_SUCCESS;
2071 }
Michal Vasko004d3152020-06-11 19:59:22 +02002072 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002073 case LYXP_TOKEN_DOT:
2074 case LYXP_TOKEN_DDOT:
2075 case LYXP_TOKEN_AT:
2076 case LYXP_TOKEN_NAMETEST:
2077 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002078 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002079 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002080 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002081 default:
2082 break;
2083 }
2084
Michal Vasko03ff5a72019-09-11 13:49:33 +02002085 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002086 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002087 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002088
Michal Vasko004d3152020-06-11 19:59:22 +02002089 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002090 LY_CHECK_RET(rc);
2091 }
2092
2093 return LY_SUCCESS;
2094}
2095
2096/**
2097 * @brief Reparse FunctionCall. Logs directly on error.
2098 *
2099 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2100 *
2101 * @param[in] ctx Context for logging.
2102 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002103 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002104 * @return LY_ERR
2105 */
2106static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002107reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002108{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002109 int8_t min_arg_count = -1;
2110 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002111 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002112 LY_ERR rc;
2113
Michal Vasko004d3152020-06-11 19:59:22 +02002114 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002115 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002116 func_tok_idx = *tok_idx;
2117 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002118 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002119 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002120 min_arg_count = 1;
2121 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002122 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002123 min_arg_count = 1;
2124 max_arg_count = 1;
2125 }
2126 break;
2127 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002128 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002129 min_arg_count = 1;
2130 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002131 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002132 min_arg_count = 0;
2133 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002134 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002135 min_arg_count = 0;
2136 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002137 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002138 min_arg_count = 0;
2139 max_arg_count = 0;
2140 }
2141 break;
2142 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002143 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002144 min_arg_count = 1;
2145 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002146 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002147 min_arg_count = 0;
2148 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002149 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002150 min_arg_count = 1;
2151 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002152 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002153 min_arg_count = 1;
2154 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002155 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002156 min_arg_count = 1;
2157 max_arg_count = 1;
2158 }
2159 break;
2160 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002161 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002162 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002163 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002164 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002165 min_arg_count = 0;
2166 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002167 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002168 min_arg_count = 0;
2169 max_arg_count = 1;
2170 }
2171 break;
2172 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002173 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002174 min_arg_count = 1;
2175 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002176 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002177 min_arg_count = 1;
2178 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002179 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002180 min_arg_count = 0;
2181 max_arg_count = 0;
2182 }
2183 break;
2184 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002185 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002186 min_arg_count = 2;
2187 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002188 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002189 min_arg_count = 0;
2190 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002191 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002192 min_arg_count = 2;
2193 max_arg_count = 2;
2194 }
2195 break;
2196 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002197 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002198 min_arg_count = 2;
2199 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002200 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002201 min_arg_count = 3;
2202 max_arg_count = 3;
2203 }
2204 break;
2205 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002206 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002207 min_arg_count = 0;
2208 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002209 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002210 min_arg_count = 1;
2211 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002212 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002213 min_arg_count = 2;
2214 max_arg_count = 2;
2215 }
2216 break;
2217 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002218 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002219 min_arg_count = 2;
2220 max_arg_count = 2;
2221 }
2222 break;
2223 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002224 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002225 min_arg_count = 2;
2226 max_arg_count = 2;
2227 }
2228 break;
2229 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002230 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002231 min_arg_count = 0;
2232 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002233 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002234 min_arg_count = 0;
2235 max_arg_count = 1;
2236 }
2237 break;
2238 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002239 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002240 min_arg_count = 0;
2241 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002242 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002243 min_arg_count = 2;
2244 max_arg_count = 2;
2245 }
2246 break;
2247 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002248 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002249 min_arg_count = 2;
2250 max_arg_count = 2;
2251 }
2252 break;
2253 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002254 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002255 min_arg_count = 2;
2256 max_arg_count = 2;
2257 }
2258 break;
2259 }
2260 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002261 LOGVAL(ctx, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002262 return LY_EINVAL;
2263 }
Michal Vasko004d3152020-06-11 19:59:22 +02002264 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002265
2266 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002267 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002268 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002269 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002270
2271 /* ( Expr ( ',' Expr )* )? */
2272 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002273 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002274 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002275 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002276 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002277 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002278 LY_CHECK_RET(rc);
2279 }
Michal Vasko004d3152020-06-11 19:59:22 +02002280 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2281 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002282
2283 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002284 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002285 LY_CHECK_RET(rc);
2286 }
2287
2288 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002289 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002290 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002291 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002292
Radek Krejci857189e2020-09-01 13:26:36 +02002293 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002294 LOGVAL(ctx, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002295 return LY_EVALID;
2296 }
2297
2298 return LY_SUCCESS;
2299}
2300
2301/**
2302 * @brief Reparse PathExpr. Logs directly on error.
2303 *
2304 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2305 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2306 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2307 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2308 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2309 *
2310 * @param[in] ctx Context for logging.
2311 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002312 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002313 * @return LY_ERR
2314 */
2315static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002316reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002317{
2318 LY_ERR rc;
2319
Michal Vasko004d3152020-06-11 19:59:22 +02002320 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002321 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002322 }
2323
Michal Vasko004d3152020-06-11 19:59:22 +02002324 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002325 case LYXP_TOKEN_PAR1:
2326 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002327 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002328
Michal Vasko004d3152020-06-11 19:59:22 +02002329 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002330 LY_CHECK_RET(rc);
2331
Michal Vasko004d3152020-06-11 19:59:22 +02002332 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002333 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002334 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002335 goto predicate;
2336 break;
2337 case LYXP_TOKEN_DOT:
2338 case LYXP_TOKEN_DDOT:
2339 case LYXP_TOKEN_AT:
2340 case LYXP_TOKEN_NAMETEST:
2341 case LYXP_TOKEN_NODETYPE:
2342 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002343 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002344 LY_CHECK_RET(rc);
2345 break;
2346 case LYXP_TOKEN_FUNCNAME:
2347 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002348 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002349 LY_CHECK_RET(rc);
2350 goto predicate;
2351 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002352 case LYXP_TOKEN_OPER_PATH:
2353 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002354 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002355 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002356 LY_CHECK_RET(rc);
2357 break;
2358 case LYXP_TOKEN_LITERAL:
2359 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002360 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002361 goto predicate;
2362 break;
2363 case LYXP_TOKEN_NUMBER:
2364 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002365 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002366 goto predicate;
2367 break;
2368 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002369 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002370 return LY_EVALID;
2371 }
2372
2373 return LY_SUCCESS;
2374
2375predicate:
2376 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002377 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2378 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002379 LY_CHECK_RET(rc);
2380 }
2381
2382 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002383 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002384
2385 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002386 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002387
Michal Vasko004d3152020-06-11 19:59:22 +02002388 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002389 LY_CHECK_RET(rc);
2390 }
2391
2392 return LY_SUCCESS;
2393}
2394
2395/**
2396 * @brief Reparse UnaryExpr. Logs directly on error.
2397 *
2398 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2399 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2400 *
2401 * @param[in] ctx Context for logging.
2402 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002403 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002404 * @return LY_ERR
2405 */
2406static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002407reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002408{
2409 uint16_t prev_exp;
2410 LY_ERR rc;
2411
2412 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002413 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002414 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2415 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002416 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002417 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002418 }
2419
2420 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002421 prev_exp = *tok_idx;
2422 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002423 LY_CHECK_RET(rc);
2424
2425 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002426 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002427 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002428 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002429
Michal Vasko004d3152020-06-11 19:59:22 +02002430 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002431 LY_CHECK_RET(rc);
2432 }
2433
2434 return LY_SUCCESS;
2435}
2436
2437/**
2438 * @brief Reparse AdditiveExpr. Logs directly on error.
2439 *
2440 * [15] AdditiveExpr ::= MultiplicativeExpr
2441 * | AdditiveExpr '+' MultiplicativeExpr
2442 * | AdditiveExpr '-' MultiplicativeExpr
2443 * [16] MultiplicativeExpr ::= UnaryExpr
2444 * | MultiplicativeExpr '*' UnaryExpr
2445 * | MultiplicativeExpr 'div' UnaryExpr
2446 * | MultiplicativeExpr 'mod' UnaryExpr
2447 *
2448 * @param[in] ctx Context for logging.
2449 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002450 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002451 * @return LY_ERR
2452 */
2453static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002454reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002455{
2456 uint16_t prev_add_exp, prev_mul_exp;
2457 LY_ERR rc;
2458
Michal Vasko004d3152020-06-11 19:59:22 +02002459 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002460 goto reparse_multiplicative_expr;
2461
2462 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002463 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2464 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002465 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002466 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002467
2468reparse_multiplicative_expr:
2469 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002470 prev_mul_exp = *tok_idx;
2471 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002472 LY_CHECK_RET(rc);
2473
2474 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002475 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2476 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002477 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002478 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002479
Michal Vasko004d3152020-06-11 19:59:22 +02002480 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002481 LY_CHECK_RET(rc);
2482 }
2483 }
2484
2485 return LY_SUCCESS;
2486}
2487
2488/**
2489 * @brief Reparse EqualityExpr. Logs directly on error.
2490 *
2491 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2492 * | EqualityExpr '!=' RelationalExpr
2493 * [14] RelationalExpr ::= AdditiveExpr
2494 * | RelationalExpr '<' AdditiveExpr
2495 * | RelationalExpr '>' AdditiveExpr
2496 * | RelationalExpr '<=' AdditiveExpr
2497 * | RelationalExpr '>=' AdditiveExpr
2498 *
2499 * @param[in] ctx Context for logging.
2500 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002501 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002502 * @return LY_ERR
2503 */
2504static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002505reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002506{
2507 uint16_t prev_eq_exp, prev_rel_exp;
2508 LY_ERR rc;
2509
Michal Vasko004d3152020-06-11 19:59:22 +02002510 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002511 goto reparse_additive_expr;
2512
2513 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002514 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002515 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002516 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002517
2518reparse_additive_expr:
2519 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002520 prev_rel_exp = *tok_idx;
2521 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002522 LY_CHECK_RET(rc);
2523
2524 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002525 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002526 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002527 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002528
Michal Vasko004d3152020-06-11 19:59:22 +02002529 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002530 LY_CHECK_RET(rc);
2531 }
2532 }
2533
2534 return LY_SUCCESS;
2535}
2536
2537/**
2538 * @brief Reparse OrExpr. Logs directly on error.
2539 *
2540 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2541 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2542 *
2543 * @param[in] ctx Context for logging.
2544 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002545 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002546 * @return LY_ERR
2547 */
2548static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002549reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002550{
2551 uint16_t prev_or_exp, prev_and_exp;
2552 LY_ERR rc;
2553
Michal Vasko004d3152020-06-11 19:59:22 +02002554 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002555 goto reparse_equality_expr;
2556
2557 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002558 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 +02002559 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002560 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002561
2562reparse_equality_expr:
2563 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002564 prev_and_exp = *tok_idx;
2565 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002566 LY_CHECK_RET(rc);
2567
2568 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002569 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 +02002570 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002571 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002572
Michal Vasko004d3152020-06-11 19:59:22 +02002573 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002574 LY_CHECK_RET(rc);
2575 }
2576 }
2577
2578 return LY_SUCCESS;
2579}
Radek Krejcib1646a92018-11-02 16:08:26 +01002580
2581/**
2582 * @brief Parse NCName.
2583 *
2584 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002585 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002586 */
Radek Krejcid4270262019-01-07 15:07:25 +01002587static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002588parse_ncname(const char *ncname)
2589{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002590 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002591 size_t size;
2592 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002593
2594 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2595 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2596 return len;
2597 }
2598
2599 do {
2600 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002601 if (!*ncname) {
2602 break;
2603 }
Radek Krejcid4270262019-01-07 15:07:25 +01002604 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002605 } while (is_xmlqnamechar(uc) && (uc != ':'));
2606
2607 return len;
2608}
2609
2610/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002611 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002612 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002613 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002614 * @param[in] exp Expression to use.
2615 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002616 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002617 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002618 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002619 */
2620static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002621exp_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 +01002622{
2623 uint32_t prev;
2624
2625 if (exp->used == exp->size) {
2626 prev = exp->size;
2627 exp->size += LYXP_EXPR_SIZE_STEP;
2628 if (prev > exp->size) {
2629 LOGINT(ctx);
2630 return LY_EINT;
2631 }
2632
2633 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2634 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2635 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2636 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2637 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2638 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2639 }
2640
2641 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002642 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002643 exp->tok_len[exp->used] = tok_len;
2644 ++exp->used;
2645 return LY_SUCCESS;
2646}
2647
2648void
Michal Vasko14676352020-05-29 11:35:55 +02002649lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002650{
2651 uint16_t i;
2652
2653 if (!expr) {
2654 return;
2655 }
2656
2657 lydict_remove(ctx, expr->expr);
2658 free(expr->tokens);
2659 free(expr->tok_pos);
2660 free(expr->tok_len);
2661 if (expr->repeat) {
2662 for (i = 0; i < expr->used; ++i) {
2663 free(expr->repeat[i]);
2664 }
2665 }
2666 free(expr->repeat);
2667 free(expr);
2668}
2669
Radek Krejcif03a9e22020-09-18 20:09:31 +02002670LY_ERR
2671lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr_str, size_t expr_len, ly_bool reparse, struct lyxp_expr **expr_p)
Radek Krejcib1646a92018-11-02 16:08:26 +01002672{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002673 LY_ERR ret = LY_SUCCESS;
2674 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002675 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002676 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002677 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002678 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002679
Radek Krejcif03a9e22020-09-18 20:09:31 +02002680 assert(expr_p);
2681
2682 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002683 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002684 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002685 }
2686
2687 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002688 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002689 }
2690 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002691 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002692 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002693 }
2694
2695 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002696 expr = calloc(1, sizeof *expr);
2697 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2698 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2699 expr->used = 0;
2700 expr->size = LYXP_EXPR_SIZE_START;
2701 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2702 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002703
Radek Krejcif03a9e22020-09-18 20:09:31 +02002704 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2705 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002706
Radek Krejcif03a9e22020-09-18 20:09:31 +02002707 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2708 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002709
Michal Vasko004d3152020-06-11 19:59:22 +02002710 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002711 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002712
Radek Krejcif03a9e22020-09-18 20:09:31 +02002713 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002714 ++parsed;
2715 }
2716
2717 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002718 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002719
2720 /* '(' */
2721 tok_len = 1;
2722 tok_type = LYXP_TOKEN_PAR1;
2723
Radek Krejcif03a9e22020-09-18 20:09:31 +02002724 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002725 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002726 if (((expr->tok_len[expr->used - 1] == 4) &&
2727 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2728 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2729 ((expr->tok_len[expr->used - 1] == 7) &&
2730 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002731 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002732 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002733 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002734 }
2735 prev_function_check = 0;
2736 }
2737
Radek Krejcif03a9e22020-09-18 20:09:31 +02002738 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002739
2740 /* ')' */
2741 tok_len = 1;
2742 tok_type = LYXP_TOKEN_PAR2;
2743
Radek Krejcif03a9e22020-09-18 20:09:31 +02002744 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002745
2746 /* '[' */
2747 tok_len = 1;
2748 tok_type = LYXP_TOKEN_BRACK1;
2749
Radek Krejcif03a9e22020-09-18 20:09:31 +02002750 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002751
2752 /* ']' */
2753 tok_len = 1;
2754 tok_type = LYXP_TOKEN_BRACK2;
2755
Radek Krejcif03a9e22020-09-18 20:09:31 +02002756 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002757
2758 /* '..' */
2759 tok_len = 2;
2760 tok_type = LYXP_TOKEN_DDOT;
2761
Radek Krejcif03a9e22020-09-18 20:09:31 +02002762 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002763
2764 /* '.' */
2765 tok_len = 1;
2766 tok_type = LYXP_TOKEN_DOT;
2767
Radek Krejcif03a9e22020-09-18 20:09:31 +02002768 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002769
2770 /* '@' */
2771 tok_len = 1;
2772 tok_type = LYXP_TOKEN_AT;
2773
Radek Krejcif03a9e22020-09-18 20:09:31 +02002774 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002775
2776 /* ',' */
2777 tok_len = 1;
2778 tok_type = LYXP_TOKEN_COMMA;
2779
Radek Krejcif03a9e22020-09-18 20:09:31 +02002780 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002781
2782 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002783 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2784 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002785 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002786 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002787 ++tok_len;
2788 tok_type = LYXP_TOKEN_LITERAL;
2789
Radek Krejcif03a9e22020-09-18 20:09:31 +02002790 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002791
2792 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002793 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2794 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002795 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002796 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002797 ++tok_len;
2798 tok_type = LYXP_TOKEN_LITERAL;
2799
Radek Krejcif03a9e22020-09-18 20:09:31 +02002800 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002801
2802 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002803 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2804 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002805 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002806 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002807 }
2808 tok_type = LYXP_TOKEN_NUMBER;
2809
Radek Krejcif03a9e22020-09-18 20:09:31 +02002810 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002811
2812 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002813 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002814 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002815 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002816 } else {
2817 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002818 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002819 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002820
Radek Krejcif03a9e22020-09-18 20:09:31 +02002821 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002822
Michal Vasko3e48bf32020-06-01 08:39:07 +02002823 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002824 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002825 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2826
Radek Krejcif03a9e22020-09-18 20:09:31 +02002827 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002828
2829 /* Operator '<=', '>=' */
2830 tok_len = 2;
2831 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002832
Radek Krejcif03a9e22020-09-18 20:09:31 +02002833 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002834
2835 /* Operator '|' */
2836 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002837 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002838
Radek Krejcif03a9e22020-09-18 20:09:31 +02002839 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002840
2841 /* Operator '+', '-' */
2842 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002843 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002844
Radek Krejcif03a9e22020-09-18 20:09:31 +02002845 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002846
Michal Vasko3e48bf32020-06-01 08:39:07 +02002847 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002848 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002849 tok_type = LYXP_TOKEN_OPER_EQUAL;
2850
Radek Krejcif03a9e22020-09-18 20:09:31 +02002851 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002852
2853 /* Operator '<', '>' */
2854 tok_len = 1;
2855 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002856
Michal Vasko69730152020-10-09 16:30:07 +02002857 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2858 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2859 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2860 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2861 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2862 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2863 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2864 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2865 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2866 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2867 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2868 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002869
2870 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002871 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002872 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002873 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002874
Radek Krejcif03a9e22020-09-18 20:09:31 +02002875 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002876 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002877 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002878
Radek Krejcif03a9e22020-09-18 20:09:31 +02002879 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002880 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002881 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002882
Radek Krejcif03a9e22020-09-18 20:09:31 +02002883 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002884 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002885 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002886
2887 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002888 LOGVAL(ctx, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
Michal Vasko69730152020-10-09 16:30:07 +02002889 expr_str[parsed], expr_str[parsed], expr->tok_len[expr->used - 1], &expr->expr[expr->tok_pos[expr->used - 1]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002890 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002891 goto error;
2892 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02002893 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002894 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002895 goto error;
2896 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002897 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002898
2899 /* NameTest '*' */
2900 tok_len = 1;
2901 tok_type = LYXP_TOKEN_NAMETEST;
2902
2903 } else {
2904
2905 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002906 long int ncname_len = parse_ncname(&expr_str[parsed]);
Michal Vasko774ce402021-04-14 15:35:06 +02002907 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2908 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002909 tok_len = ncname_len;
2910
Radek Krejcif03a9e22020-09-18 20:09:31 +02002911 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002912 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002913 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002914 ++tok_len;
2915 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002916 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vasko774ce402021-04-14 15:35:06 +02002917 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2918 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002919 tok_len += ncname_len;
2920 }
2921 /* remove old flag to prevent ambiguities */
2922 prev_function_check = 0;
2923 tok_type = LYXP_TOKEN_NAMETEST;
2924 } else {
2925 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2926 prev_function_check = 1;
2927 tok_type = LYXP_TOKEN_NAMETEST;
2928 }
2929 }
2930
2931 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002932 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002933 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002934 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002935 ++parsed;
2936 }
2937
Radek Krejcif03a9e22020-09-18 20:09:31 +02002938 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002939
Michal Vasko004d3152020-06-11 19:59:22 +02002940 if (reparse) {
2941 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002942 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2943 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002944
Michal Vasko004d3152020-06-11 19:59:22 +02002945 /* fill repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002946 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error);
2947 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002948 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002949 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002950 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002951 goto error;
2952 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002953 }
2954
Radek Krejcif03a9e22020-09-18 20:09:31 +02002955 print_expr_struct_debug(expr);
2956 *expr_p = expr;
2957 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002958
2959error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002960 lyxp_expr_free(ctx, expr);
2961 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002962}
2963
Michal Vasko1734be92020-09-22 08:55:10 +02002964LY_ERR
2965lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02002966{
Michal Vasko1734be92020-09-22 08:55:10 +02002967 LY_ERR ret = LY_SUCCESS;
2968 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02002969 uint32_t i, j;
2970
Michal Vasko7f45cf22020-10-01 12:49:44 +02002971 if (!exp) {
2972 goto cleanup;
2973 }
2974
Michal Vasko004d3152020-06-11 19:59:22 +02002975 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02002976 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002977
2978 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
Michal Vasko1734be92020-09-22 08:55:10 +02002979 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002980 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2981
2982 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
Michal Vasko1734be92020-09-22 08:55:10 +02002983 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002984 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2985
2986 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
Michal Vasko1734be92020-09-22 08:55:10 +02002987 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002988 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2989
2990 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002991 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002992 for (i = 0; i < exp->used; ++i) {
2993 if (!exp->repeat[i]) {
2994 dup->repeat[i] = NULL;
2995 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02002996 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002997 /* the ending 0 as well */
2998 ++j;
2999
Michal Vasko99c71642020-07-03 13:33:36 +02003000 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02003001 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003002 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3003 dup->repeat[i][j - 1] = 0;
3004 }
3005 }
3006
3007 dup->used = exp->used;
3008 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003009 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003010
Michal Vasko1734be92020-09-22 08:55:10 +02003011cleanup:
3012 if (ret) {
3013 lyxp_expr_free(ctx, dup);
3014 } else {
3015 *dup_p = dup;
3016 }
3017 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003018}
3019
Michal Vasko03ff5a72019-09-11 13:49:33 +02003020/*
3021 * warn functions
3022 *
3023 * Warn functions check specific reasonable conditions for schema XPath
3024 * and print a warning if they are not satisfied.
3025 */
3026
3027/**
3028 * @brief Get the last-added schema node that is currently in the context.
3029 *
3030 * @param[in] set Set to search in.
3031 * @return Last-added schema context node, NULL if no node is in context.
3032 */
3033static struct lysc_node *
3034warn_get_scnode_in_ctx(struct lyxp_set *set)
3035{
3036 uint32_t i;
3037
3038 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3039 return NULL;
3040 }
3041
3042 i = set->used;
3043 do {
3044 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003045 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003046 /* if there are more, simply return the first found (last added) */
3047 return set->val.scnodes[i].scnode;
3048 }
3049 } while (i);
3050
3051 return NULL;
3052}
3053
3054/**
3055 * @brief Test whether a type is numeric - integer type or decimal64.
3056 *
3057 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003058 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003059 */
Radek Krejci857189e2020-09-01 13:26:36 +02003060static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003061warn_is_numeric_type(struct lysc_type *type)
3062{
3063 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003064 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003065 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003066
3067 switch (type->basetype) {
3068 case LY_TYPE_DEC64:
3069 case LY_TYPE_INT8:
3070 case LY_TYPE_UINT8:
3071 case LY_TYPE_INT16:
3072 case LY_TYPE_UINT16:
3073 case LY_TYPE_INT32:
3074 case LY_TYPE_UINT32:
3075 case LY_TYPE_INT64:
3076 case LY_TYPE_UINT64:
3077 return 1;
3078 case LY_TYPE_UNION:
3079 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003080 LY_ARRAY_FOR(uni->types, u) {
3081 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003082 if (ret) {
3083 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003084 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003085 }
3086 }
3087 /* did not find any suitable type */
3088 return 0;
3089 case LY_TYPE_LEAFREF:
3090 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3091 default:
3092 return 0;
3093 }
3094}
3095
3096/**
3097 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3098 *
3099 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003100 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003101 */
Radek Krejci857189e2020-09-01 13:26:36 +02003102static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003103warn_is_string_type(struct lysc_type *type)
3104{
3105 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003106 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003107 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003108
3109 switch (type->basetype) {
3110 case LY_TYPE_BITS:
3111 case LY_TYPE_ENUM:
3112 case LY_TYPE_IDENT:
3113 case LY_TYPE_INST:
3114 case LY_TYPE_STRING:
3115 return 1;
3116 case LY_TYPE_UNION:
3117 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003118 LY_ARRAY_FOR(uni->types, u) {
3119 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003120 if (ret) {
3121 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003122 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003123 }
3124 }
3125 /* did not find any suitable type */
3126 return 0;
3127 case LY_TYPE_LEAFREF:
3128 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3129 default:
3130 return 0;
3131 }
3132}
3133
3134/**
3135 * @brief Test whether a type is one specific type.
3136 *
3137 * @param[in] type Type to test.
3138 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003139 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003140 */
Radek Krejci857189e2020-09-01 13:26:36 +02003141static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003142warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3143{
3144 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003145 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003146 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003147
3148 if (type->basetype == base) {
3149 return 1;
3150 } else if (type->basetype == LY_TYPE_UNION) {
3151 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003152 LY_ARRAY_FOR(uni->types, u) {
3153 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003154 if (ret) {
3155 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003156 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003157 }
3158 }
3159 /* did not find any suitable type */
3160 return 0;
3161 } else if (type->basetype == LY_TYPE_LEAFREF) {
3162 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3163 }
3164
3165 return 0;
3166}
3167
3168/**
3169 * @brief Get next type of a (union) type.
3170 *
3171 * @param[in] type Base type.
3172 * @param[in] prev_type Previously returned type.
3173 * @return Next type or NULL.
3174 */
3175static struct lysc_type *
3176warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3177{
3178 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003179 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003180 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003181
3182 switch (type->basetype) {
3183 case LY_TYPE_UNION:
3184 uni = (struct lysc_type_union *)type;
3185 if (!prev_type) {
3186 return uni->types[0];
3187 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003188 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003189 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003190 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003191 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003192 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003193 found = 1;
3194 }
3195 }
3196 return NULL;
3197 default:
3198 if (prev_type) {
3199 assert(type == prev_type);
3200 return NULL;
3201 } else {
3202 return type;
3203 }
3204 }
3205}
3206
3207/**
3208 * @brief Test whether 2 types have a common type.
3209 *
3210 * @param[in] type1 First type.
3211 * @param[in] type2 Second type.
3212 * @return 1 if they do, 0 otherwise.
3213 */
3214static int
3215warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3216{
3217 struct lysc_type *t1, *rt1, *t2, *rt2;
3218
3219 t1 = NULL;
3220 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3221 if (t1->basetype == LY_TYPE_LEAFREF) {
3222 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3223 } else {
3224 rt1 = t1;
3225 }
3226
3227 t2 = NULL;
3228 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3229 if (t2->basetype == LY_TYPE_LEAFREF) {
3230 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3231 } else {
3232 rt2 = t2;
3233 }
3234
3235 if (rt2->basetype == rt1->basetype) {
3236 /* match found */
3237 return 1;
3238 }
3239 }
3240 }
3241
3242 return 0;
3243}
3244
3245/**
3246 * @brief Check both operands of comparison operators.
3247 *
3248 * @param[in] ctx Context for errors.
3249 * @param[in] set1 First operand set.
3250 * @param[in] set2 Second operand set.
3251 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3252 * @param[in] expr Start of the expression to print with the warning.
3253 * @param[in] tok_pos Token position.
3254 */
3255static void
Radek Krejci857189e2020-09-01 13:26:36 +02003256warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr, uint16_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003257{
3258 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003259 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003260
3261 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3262 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3263
3264 if (!node1 && !node2) {
3265 /* no node-sets involved, nothing to do */
3266 return;
3267 }
3268
3269 if (node1) {
3270 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3271 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3272 warning = 1;
3273 leaves = 0;
3274 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3275 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3276 warning = 1;
3277 }
3278 }
3279
3280 if (node2) {
3281 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3282 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3283 warning = 1;
3284 leaves = 0;
3285 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3286 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3287 warning = 1;
3288 }
3289 }
3290
3291 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003292 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3293 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3294 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3295 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003296 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3297 warning = 1;
3298 }
3299 }
3300
3301 if (warning) {
3302 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3303 }
3304}
3305
3306/**
3307 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3308 *
3309 * @param[in] exp Parsed XPath expression.
3310 * @param[in] set Set with the leaf/leaf-list.
3311 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3312 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3313 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3314 */
3315static void
Michal Vasko40308e72020-10-20 16:38:40 +02003316warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3317 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003318{
3319 struct lysc_node *scnode;
3320 struct lysc_type *type;
3321 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003322 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003323 LY_ERR rc;
3324 struct ly_err_item *err = NULL;
3325
Michal Vasko69730152020-10-09 16:30:07 +02003326 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3327 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003328 /* check that the node can have the specified value */
3329 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3330 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3331 } else {
3332 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3333 }
3334 if (!value) {
3335 LOGMEM(set->ctx);
3336 return;
3337 }
3338
3339 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3340 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003341 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003342 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003343 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003344 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003345 }
3346
3347 type = ((struct lysc_node_leaf *)scnode)->type;
3348 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003349 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003350 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003351 if (rc == LY_EINCOMPLETE) {
3352 rc = LY_SUCCESS;
3353 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003354
3355 if (err) {
3356 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3357 ly_err_free(err);
3358 } else if (rc != LY_SUCCESS) {
3359 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3360 }
3361 if (rc != LY_SUCCESS) {
3362 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003363 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003364 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003365 } else {
3366 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003367 }
3368 }
3369 free(value);
3370 }
3371}
3372
3373/*
3374 * XPath functions
3375 */
3376
3377/**
3378 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3379 * depending on whether the first node bit value from the second argument is set.
3380 *
3381 * @param[in] args Array of arguments.
3382 * @param[in] arg_count Count of elements in @p args.
3383 * @param[in,out] set Context and result set at the same time.
3384 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003385 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003386 */
3387static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003388xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003389{
3390 struct lyd_node_term *leaf;
3391 struct lysc_node_leaf *sleaf;
3392 struct lysc_type_bits *bits;
3393 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003394 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003395
3396 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003397 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003398 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003399 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3400 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3401 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3402 sleaf->name);
3403 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3404 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3405 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003406 }
3407
3408 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3409 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003410 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3411 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003412 } else if (!warn_is_string_type(sleaf->type)) {
3413 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003414 }
3415 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003416 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003417 return rc;
3418 }
3419
Michal Vaskod3678892020-05-21 10:06:58 +02003420 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003421 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "bit-is-set(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003422 return LY_EVALID;
3423 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003424 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003425 LY_CHECK_RET(rc);
3426
3427 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003428 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003429 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003430 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3431 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003432 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003433 LY_ARRAY_FOR(bits->bits, u) {
3434 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003435 set_fill_boolean(set, 1);
3436 break;
3437 }
3438 }
3439 }
3440 }
3441
3442 return LY_SUCCESS;
3443}
3444
3445/**
3446 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3447 * with the argument converted to boolean.
3448 *
3449 * @param[in] args Array of arguments.
3450 * @param[in] arg_count Count of elements in @p args.
3451 * @param[in,out] set Context and result set at the same time.
3452 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003453 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003454 */
3455static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003456xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003457{
3458 LY_ERR rc;
3459
3460 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003461 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003462 return LY_SUCCESS;
3463 }
3464
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003465 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003466 LY_CHECK_RET(rc);
3467 set_fill_set(set, args[0]);
3468
3469 return LY_SUCCESS;
3470}
3471
3472/**
3473 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3474 * with the first argument rounded up to the nearest integer.
3475 *
3476 * @param[in] args Array of arguments.
3477 * @param[in] arg_count Count of elements in @p args.
3478 * @param[in,out] set Context and result set at the same time.
3479 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003480 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003481 */
3482static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003483xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003484{
3485 struct lysc_node_leaf *sleaf;
3486 LY_ERR rc = LY_SUCCESS;
3487
3488 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003489 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003490 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003491 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3492 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3493 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3494 sleaf->name);
3495 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3496 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3497 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003498 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003499 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003500 return rc;
3501 }
3502
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003503 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003504 LY_CHECK_RET(rc);
3505 if ((long long)args[0]->val.num != args[0]->val.num) {
3506 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3507 } else {
3508 set_fill_number(set, args[0]->val.num);
3509 }
3510
3511 return LY_SUCCESS;
3512}
3513
3514/**
3515 * @brief Execute the XPath concat(string, string, string*) function.
3516 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3517 *
3518 * @param[in] args Array of arguments.
3519 * @param[in] arg_count Count of elements in @p args.
3520 * @param[in,out] set Context and result set at the same time.
3521 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003522 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003523 */
3524static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003525xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003526{
3527 uint16_t i;
3528 char *str = NULL;
3529 size_t used = 1;
3530 LY_ERR rc = LY_SUCCESS;
3531 struct lysc_node_leaf *sleaf;
3532
3533 if (options & LYXP_SCNODE_ALL) {
3534 for (i = 0; i < arg_count; ++i) {
3535 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3536 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3537 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003538 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003539 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003540 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 +02003541 }
3542 }
3543 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003544 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003545 return rc;
3546 }
3547
3548 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003549 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003550 if (rc != LY_SUCCESS) {
3551 free(str);
3552 return rc;
3553 }
3554
3555 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3556 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3557 strcpy(str + used - 1, args[i]->val.str);
3558 used += strlen(args[i]->val.str);
3559 }
3560
3561 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003562 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003563 set->type = LYXP_SET_STRING;
3564 set->val.str = str;
3565
3566 return LY_SUCCESS;
3567}
3568
3569/**
3570 * @brief Execute the XPath contains(string, string) function.
3571 * Returns LYXP_SET_BOOLEAN whether the second argument can
3572 * be found in the first or not.
3573 *
3574 * @param[in] args Array of arguments.
3575 * @param[in] arg_count Count of elements in @p args.
3576 * @param[in,out] set Context and result set at the same time.
3577 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003578 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003579 */
3580static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003581xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003582{
3583 struct lysc_node_leaf *sleaf;
3584 LY_ERR rc = LY_SUCCESS;
3585
3586 if (options & LYXP_SCNODE_ALL) {
3587 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3588 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003589 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3590 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003591 } else if (!warn_is_string_type(sleaf->type)) {
3592 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003593 }
3594 }
3595
3596 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3597 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003598 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3599 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003600 } else if (!warn_is_string_type(sleaf->type)) {
3601 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003602 }
3603 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003604 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003605 return rc;
3606 }
3607
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003608 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003609 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003610 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003611 LY_CHECK_RET(rc);
3612
3613 if (strstr(args[0]->val.str, args[1]->val.str)) {
3614 set_fill_boolean(set, 1);
3615 } else {
3616 set_fill_boolean(set, 0);
3617 }
3618
3619 return LY_SUCCESS;
3620}
3621
3622/**
3623 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3624 * with the size of the node-set from the argument.
3625 *
3626 * @param[in] args Array of arguments.
3627 * @param[in] arg_count Count of elements in @p args.
3628 * @param[in,out] set Context and result set at the same time.
3629 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003630 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003631 */
3632static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003633xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003634{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003635 LY_ERR rc = LY_SUCCESS;
3636
3637 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003638 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003639 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003640 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003641 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003642 return rc;
3643 }
3644
Michal Vasko03ff5a72019-09-11 13:49:33 +02003645 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003646 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003647 return LY_EVALID;
3648 }
3649
3650 set_fill_number(set, args[0]->used);
3651 return LY_SUCCESS;
3652}
3653
3654/**
3655 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3656 * with the context with the intial node.
3657 *
3658 * @param[in] args Array of arguments.
3659 * @param[in] arg_count Count of elements in @p args.
3660 * @param[in,out] set Context and result set at the same time.
3661 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003662 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003663 */
3664static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003665xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003666{
3667 if (arg_count || args) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003668 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003669 return LY_EVALID;
3670 }
3671
3672 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003673 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003674
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003675 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003676 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003677 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003678
3679 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003680 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003681 }
3682
3683 return LY_SUCCESS;
3684}
3685
3686/**
3687 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3688 * leafref or instance-identifier target node(s).
3689 *
3690 * @param[in] args Array of arguments.
3691 * @param[in] arg_count Count of elements in @p args.
3692 * @param[in,out] set Context and result set at the same time.
3693 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003694 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003695 */
3696static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003697xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003698{
3699 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003700 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003701 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003702 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003703 struct ly_path *p;
3704 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003705 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003706 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003707 LY_ERR rc = LY_SUCCESS;
3708
3709 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003710 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003711 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003712 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3713 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3714 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3715 sleaf->name);
3716 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3717 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3718 __func__, sleaf->name);
3719 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003720 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003721 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko42e497c2020-01-06 08:38:25 +01003722 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003723 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003724 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003725
3726 /* it was already evaluated on schema, it must succeed */
Radek Krejcid5d37432021-03-12 13:46:40 +01003727 rc = ly_path_compile(set->ctx, lref->cur_mod, &sleaf->node, NULL, lref->path, LY_PATH_LREF_TRUE, oper,
Radek Krejci8df109d2021-04-23 12:19:08 +02003728 LY_PATH_TARGET_MANY, LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, NULL, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003729 assert(!rc);
3730
3731 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003732 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003733 ly_path_free(set->ctx, p);
3734
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003735 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003736 }
3737
Michal Vasko03ff5a72019-09-11 13:49:33 +02003738 return rc;
3739 }
3740
Michal Vaskod3678892020-05-21 10:06:58 +02003741 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003742 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003743 return LY_EVALID;
3744 }
3745
Michal Vaskod3678892020-05-21 10:06:58 +02003746 lyxp_set_free_content(set);
3747 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003748 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3749 sleaf = (struct lysc_node_leaf *)leaf->schema;
3750 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3751 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3752 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02003753 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01003754 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003755 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003756 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003757 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003758 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003759 } else {
3760 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003761 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003762 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003763 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003764 return LY_EVALID;
3765 }
3766 }
Michal Vasko004d3152020-06-11 19:59:22 +02003767
3768 /* insert it */
3769 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003770 }
3771 }
3772
3773 return LY_SUCCESS;
3774}
3775
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003776static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003777xpath_derived_(struct lyxp_set **args, struct lyxp_set *set, uint32_t options, ly_bool self_match, const char *func)
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003778{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01003779 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003780 LY_ARRAY_COUNT_TYPE u;
3781 struct lyd_node_term *leaf;
3782 struct lysc_node_leaf *sleaf;
3783 struct lyd_meta *meta;
3784 struct lyd_value data = {0}, *val;
3785 struct ly_err_item *err = NULL;
3786 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003787 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003788
3789 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003790 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003791 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003792 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3793 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3794 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3795 sleaf->name);
3796 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3797 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3798 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003799 }
3800
3801 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3802 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3803 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003804 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003805 } else if (!warn_is_string_type(sleaf->type)) {
3806 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3807 }
3808 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003809 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003810 return rc;
3811 }
3812
3813 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003814 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "derived-from(-or-self)(node-set, string)");
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003815 return LY_EVALID;
3816 }
3817 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3818 LY_CHECK_RET(rc);
3819
3820 set_fill_boolean(set, 0);
3821 found = 0;
3822 for (i = 0; i < args[0]->used; ++i) {
3823 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3824 continue;
3825 }
3826
3827 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3828 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3829 sleaf = (struct lysc_node_leaf *)leaf->schema;
3830 val = &leaf->value;
3831 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3832 /* uninteresting */
3833 continue;
3834 }
3835
3836 /* store args[1] as ident */
3837 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str),
Michal Vasko405cc9e2020-12-01 12:01:27 +01003838 0, set->format, set->prefix_data, LYD_HINT_DATA, leaf->schema, &data, NULL, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003839 } else {
3840 meta = args[0]->val.meta[i].meta;
3841 val = &meta->value;
3842 if (val->realtype->basetype != LY_TYPE_IDENT) {
3843 /* uninteresting */
3844 continue;
3845 }
3846
3847 /* store args[1] as ident */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003848 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), 0,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003849 set->format, set->prefix_data, LYD_HINT_DATA, meta->parent->schema, &data, NULL, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003850 }
3851
3852 if (err) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01003853 ly_err_print(set->ctx, err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003854 ly_err_free(err);
3855 }
3856 LY_CHECK_RET(rc);
3857
3858 /* finally check the identity itself */
3859 if (self_match && (data.ident == val->ident)) {
3860 set_fill_boolean(set, 1);
3861 found = 1;
3862 }
3863 if (!found) {
3864 LY_ARRAY_FOR(data.ident->derived, u) {
3865 if (data.ident->derived[u] == val->ident) {
3866 set_fill_boolean(set, 1);
3867 found = 1;
3868 break;
3869 }
3870 }
3871 }
3872
3873 /* free temporary value */
3874 val->realtype->plugin->free(set->ctx, &data);
3875 if (found) {
3876 break;
3877 }
3878 }
3879
3880 return LY_SUCCESS;
3881}
3882
Michal Vasko03ff5a72019-09-11 13:49:33 +02003883/**
3884 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3885 * on whether the first argument nodes contain a node of an identity derived from the second
3886 * argument identity.
3887 *
3888 * @param[in] args Array of arguments.
3889 * @param[in] arg_count Count of elements in @p args.
3890 * @param[in,out] set Context and result set at the same time.
3891 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003892 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003893 */
3894static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003895xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003896{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003897 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003898}
3899
3900/**
3901 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3902 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3903 * the second argument identity.
3904 *
3905 * @param[in] args Array of arguments.
3906 * @param[in] arg_count Count of elements in @p args.
3907 * @param[in,out] set Context and result set at the same time.
3908 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003909 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003910 */
3911static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003912xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003913{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003914 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003915}
3916
3917/**
3918 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3919 * with the integer value of the first node's enum value, otherwise NaN.
3920 *
3921 * @param[in] args Array of arguments.
3922 * @param[in] arg_count Count of elements in @p args.
3923 * @param[in,out] set Context and result set at the same time.
3924 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003925 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003926 */
3927static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003928xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003929{
3930 struct lyd_node_term *leaf;
3931 struct lysc_node_leaf *sleaf;
3932 LY_ERR rc = LY_SUCCESS;
3933
3934 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003935 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003936 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003937 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3938 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3939 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3940 sleaf->name);
3941 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3942 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
3943 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003944 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003945 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003946 return rc;
3947 }
3948
Michal Vaskod3678892020-05-21 10:06:58 +02003949 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003950 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003951 return LY_EVALID;
3952 }
3953
3954 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003955 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003956 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3957 sleaf = (struct lysc_node_leaf *)leaf->schema;
3958 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3959 set_fill_number(set, leaf->value.enum_item->value);
3960 }
3961 }
3962
3963 return LY_SUCCESS;
3964}
3965
3966/**
3967 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3968 * with false value.
3969 *
3970 * @param[in] args Array of arguments.
3971 * @param[in] arg_count Count of elements in @p args.
3972 * @param[in,out] set Context and result set at the same time.
3973 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003974 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003975 */
3976static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003977xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003978{
3979 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003980 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003981 return LY_SUCCESS;
3982 }
3983
3984 set_fill_boolean(set, 0);
3985 return LY_SUCCESS;
3986}
3987
3988/**
3989 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3990 * with the first argument floored (truncated).
3991 *
3992 * @param[in] args Array of arguments.
3993 * @param[in] arg_count Count of elements in @p args.
3994 * @param[in,out] set Context and result set at the same time.
3995 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003996 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003997 */
3998static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003999xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t UNUSED(options))
Michal Vasko03ff5a72019-09-11 13:49:33 +02004000{
4001 LY_ERR rc;
4002
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004003 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004004 LY_CHECK_RET(rc);
4005 if (isfinite(args[0]->val.num)) {
4006 set_fill_number(set, (long long)args[0]->val.num);
4007 }
4008
4009 return LY_SUCCESS;
4010}
4011
4012/**
4013 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4014 * whether the language of the text matches the one from the argument.
4015 *
4016 * @param[in] args Array of arguments.
4017 * @param[in] arg_count Count of elements in @p args.
4018 * @param[in,out] set Context and result set at the same time.
4019 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004020 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004021 */
4022static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004023xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004024{
4025 const struct lyd_node *node;
4026 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004027 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004028 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004029 LY_ERR rc = LY_SUCCESS;
4030
4031 if (options & LYXP_SCNODE_ALL) {
4032 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4033 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004034 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4035 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004036 } else if (!warn_is_string_type(sleaf->type)) {
4037 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004038 }
4039 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004040 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004041 return rc;
4042 }
4043
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004044 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004045 LY_CHECK_RET(rc);
4046
Michal Vasko03ff5a72019-09-11 13:49:33 +02004047 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004048 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004049 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004050 } else if (!set->used) {
4051 set_fill_boolean(set, 0);
4052 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004053 }
4054
4055 switch (set->val.nodes[0].type) {
4056 case LYXP_NODE_ELEM:
4057 case LYXP_NODE_TEXT:
4058 node = set->val.nodes[0].node;
4059 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004060 case LYXP_NODE_META:
4061 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004062 break;
4063 default:
4064 /* nothing to do with roots */
4065 set_fill_boolean(set, 0);
4066 return LY_SUCCESS;
4067 }
4068
Michal Vasko9f96a052020-03-10 09:41:45 +01004069 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004070 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004071 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004072 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004073 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004074 break;
4075 }
4076 }
4077
Michal Vasko9f96a052020-03-10 09:41:45 +01004078 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004079 break;
4080 }
4081 }
4082
4083 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004084 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004085 set_fill_boolean(set, 0);
4086 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004087 uint64_t i;
4088
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004089 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004090 for (i = 0; args[0]->val.str[i]; ++i) {
4091 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4092 set_fill_boolean(set, 0);
4093 break;
4094 }
4095 }
4096 if (!args[0]->val.str[i]) {
4097 if (!val[i] || (val[i] == '-')) {
4098 set_fill_boolean(set, 1);
4099 } else {
4100 set_fill_boolean(set, 0);
4101 }
4102 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004103 }
4104
4105 return LY_SUCCESS;
4106}
4107
4108/**
4109 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4110 * with the context size.
4111 *
4112 * @param[in] args Array of arguments.
4113 * @param[in] arg_count Count of elements in @p args.
4114 * @param[in,out] set Context and result set at the same time.
4115 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004116 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004117 */
4118static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004119xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004120{
4121 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004122 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004123 return LY_SUCCESS;
4124 }
4125
Michal Vasko03ff5a72019-09-11 13:49:33 +02004126 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004127 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004128 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004129 } else if (!set->used) {
4130 set_fill_number(set, 0);
4131 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004132 }
4133
4134 set_fill_number(set, set->ctx_size);
4135 return LY_SUCCESS;
4136}
4137
4138/**
4139 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4140 * with the node name without namespace from the argument or the context.
4141 *
4142 * @param[in] args Array of arguments.
4143 * @param[in] arg_count Count of elements in @p args.
4144 * @param[in,out] set Context and result set at the same time.
4145 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004146 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004147 */
4148static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004149xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004150{
4151 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004152
Michal Vasko03ff5a72019-09-11 13:49:33 +02004153 /* suppress unused variable warning */
4154 (void)options;
4155
4156 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004157 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004158 return LY_SUCCESS;
4159 }
4160
4161 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004162 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004163 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004164 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004165 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004166 } else if (!args[0]->used) {
4167 set_fill_string(set, "", 0);
4168 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004169 }
4170
4171 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004172 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004173
4174 item = &args[0]->val.nodes[0];
4175 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004176 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004177 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004178 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004179 } else if (!set->used) {
4180 set_fill_string(set, "", 0);
4181 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004182 }
4183
4184 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004185 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004186
4187 item = &set->val.nodes[0];
4188 }
4189
4190 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004191 case LYXP_NODE_NONE:
4192 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004193 case LYXP_NODE_ROOT:
4194 case LYXP_NODE_ROOT_CONFIG:
4195 case LYXP_NODE_TEXT:
4196 set_fill_string(set, "", 0);
4197 break;
4198 case LYXP_NODE_ELEM:
4199 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4200 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004201 case LYXP_NODE_META:
4202 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004203 break;
4204 }
4205
4206 return LY_SUCCESS;
4207}
4208
4209/**
4210 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4211 * with the node name fully qualified (with namespace) from the argument or the context.
4212 *
4213 * @param[in] args Array of arguments.
4214 * @param[in] arg_count Count of elements in @p args.
4215 * @param[in,out] set Context and result set at the same time.
4216 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004217 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004218 */
4219static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004220xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004221{
4222 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004223 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004224 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004225 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004226
4227 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004228 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004229 return LY_SUCCESS;
4230 }
4231
4232 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004233 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004234 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004235 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004236 } else if (!args[0]->used) {
4237 set_fill_string(set, "", 0);
4238 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004239 }
4240
4241 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004242 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004243
4244 item = &args[0]->val.nodes[0];
4245 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004246 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004247 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004248 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004249 } else if (!set->used) {
4250 set_fill_string(set, "", 0);
4251 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004252 }
4253
4254 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004255 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004256
4257 item = &set->val.nodes[0];
4258 }
4259
4260 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004261 case LYXP_NODE_NONE:
4262 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004263 case LYXP_NODE_ROOT:
4264 case LYXP_NODE_ROOT_CONFIG:
4265 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004266 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004267 break;
4268 case LYXP_NODE_ELEM:
4269 mod = item->node->schema->module;
4270 name = item->node->schema->name;
4271 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004272 case LYXP_NODE_META:
4273 mod = ((struct lyd_meta *)item->node)->annotation->module;
4274 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004275 break;
4276 }
4277
4278 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004279 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004280 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4281 set_fill_string(set, str, strlen(str));
4282 free(str);
4283 } else {
4284 set_fill_string(set, "", 0);
4285 }
4286
4287 return LY_SUCCESS;
4288}
4289
4290/**
4291 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4292 * with the namespace of the node from the argument or the context.
4293 *
4294 * @param[in] args Array of arguments.
4295 * @param[in] arg_count Count of elements in @p args.
4296 * @param[in,out] set Context and result set at the same time.
4297 * @param[in] options XPath options.
4298 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4299 */
4300static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004301xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004302{
4303 struct lyxp_set_node *item;
4304 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004305
Michal Vasko03ff5a72019-09-11 13:49:33 +02004306 /* suppress unused variable warning */
4307 (void)options;
4308
4309 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004310 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004311 return LY_SUCCESS;
4312 }
4313
4314 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004315 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004316 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004317 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004318 return LY_EVALID;
4319 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004320 set_fill_string(set, "", 0);
4321 return LY_SUCCESS;
4322 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004323
4324 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004325 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004326
4327 item = &args[0]->val.nodes[0];
4328 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004329 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004330 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004331 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004332 } else if (!set->used) {
4333 set_fill_string(set, "", 0);
4334 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004335 }
4336
4337 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004338 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004339
4340 item = &set->val.nodes[0];
4341 }
4342
4343 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004344 case LYXP_NODE_NONE:
4345 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004346 case LYXP_NODE_ROOT:
4347 case LYXP_NODE_ROOT_CONFIG:
4348 case LYXP_NODE_TEXT:
4349 set_fill_string(set, "", 0);
4350 break;
4351 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004352 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004353 if (item->type == LYXP_NODE_ELEM) {
4354 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004355 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004356 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004357 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004358 }
4359
4360 set_fill_string(set, mod->ns, strlen(mod->ns));
4361 break;
4362 }
4363
4364 return LY_SUCCESS;
4365}
4366
4367/**
4368 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4369 * with only nodes from the context. In practice it either leaves the context
4370 * as it is or returns an empty node set.
4371 *
4372 * @param[in] args Array of arguments.
4373 * @param[in] arg_count Count of elements in @p args.
4374 * @param[in,out] set Context and result set at the same time.
4375 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004376 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004377 */
4378static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004379xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004380{
4381 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004382 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004383 return LY_SUCCESS;
4384 }
4385
4386 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004387 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004388 }
4389 return LY_SUCCESS;
4390}
4391
4392/**
4393 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4394 * with normalized value (no leading, trailing, double white spaces) of the node
4395 * from the argument or the context.
4396 *
4397 * @param[in] args Array of arguments.
4398 * @param[in] arg_count Count of elements in @p args.
4399 * @param[in,out] set Context and result set at the same time.
4400 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004401 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004402 */
4403static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004404xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004405{
4406 uint16_t i, new_used;
4407 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004408 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004409 struct lysc_node_leaf *sleaf;
4410 LY_ERR rc = LY_SUCCESS;
4411
4412 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004413 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4414 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004415 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004416 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4417 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004418 } else if (!warn_is_string_type(sleaf->type)) {
4419 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004420 }
4421 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004422 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004423 return rc;
4424 }
4425
4426 if (arg_count) {
4427 set_fill_set(set, args[0]);
4428 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004429 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004430 LY_CHECK_RET(rc);
4431
4432 /* is there any normalization necessary? */
4433 for (i = 0; set->val.str[i]; ++i) {
4434 if (is_xmlws(set->val.str[i])) {
4435 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4436 have_spaces = 1;
4437 break;
4438 }
4439 space_before = 1;
4440 } else {
4441 space_before = 0;
4442 }
4443 }
4444
4445 /* yep, there is */
4446 if (have_spaces) {
4447 /* it's enough, at least one character will go, makes space for ending '\0' */
4448 new = malloc(strlen(set->val.str) * sizeof(char));
4449 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4450 new_used = 0;
4451
4452 space_before = 0;
4453 for (i = 0; set->val.str[i]; ++i) {
4454 if (is_xmlws(set->val.str[i])) {
4455 if ((i == 0) || space_before) {
4456 space_before = 1;
4457 continue;
4458 } else {
4459 space_before = 1;
4460 }
4461 } else {
4462 space_before = 0;
4463 }
4464
4465 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4466 ++new_used;
4467 }
4468
4469 /* at worst there is one trailing space now */
4470 if (new_used && is_xmlws(new[new_used - 1])) {
4471 --new_used;
4472 }
4473
4474 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4475 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4476 new[new_used] = '\0';
4477
4478 free(set->val.str);
4479 set->val.str = new;
4480 }
4481
4482 return LY_SUCCESS;
4483}
4484
4485/**
4486 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4487 * with the argument converted to boolean and logically inverted.
4488 *
4489 * @param[in] args Array of arguments.
4490 * @param[in] arg_count Count of elements in @p args.
4491 * @param[in,out] set Context and result set at the same time.
4492 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004493 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004494 */
4495static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004496xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004497{
4498 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004499 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004500 return LY_SUCCESS;
4501 }
4502
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004503 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004504 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004505 set_fill_boolean(set, 0);
4506 } else {
4507 set_fill_boolean(set, 1);
4508 }
4509
4510 return LY_SUCCESS;
4511}
4512
4513/**
4514 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4515 * with the number representation of either the argument or the context.
4516 *
4517 * @param[in] args Array of arguments.
4518 * @param[in] arg_count Count of elements in @p args.
4519 * @param[in,out] set Context and result set at the same time.
4520 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004521 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004522 */
4523static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004524xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004525{
4526 LY_ERR rc;
4527
4528 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004529 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004530 return LY_SUCCESS;
4531 }
4532
4533 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004534 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004535 LY_CHECK_RET(rc);
4536 set_fill_set(set, args[0]);
4537 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004538 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004539 LY_CHECK_RET(rc);
4540 }
4541
4542 return LY_SUCCESS;
4543}
4544
4545/**
4546 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4547 * with the context position.
4548 *
4549 * @param[in] args Array of arguments.
4550 * @param[in] arg_count Count of elements in @p args.
4551 * @param[in,out] set Context and result set at the same time.
4552 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004553 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004554 */
4555static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004556xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004557{
4558 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004559 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004560 return LY_SUCCESS;
4561 }
4562
Michal Vasko03ff5a72019-09-11 13:49:33 +02004563 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004564 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004565 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004566 } else if (!set->used) {
4567 set_fill_number(set, 0);
4568 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004569 }
4570
4571 set_fill_number(set, set->ctx_pos);
4572
4573 /* UNUSED in 'Release' build type */
4574 (void)options;
4575 return LY_SUCCESS;
4576}
4577
4578/**
4579 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4580 * depending on whether the second argument regex matches the first argument string. For details refer to
4581 * YANG 1.1 RFC section 10.2.1.
4582 *
4583 * @param[in] args Array of arguments.
4584 * @param[in] arg_count Count of elements in @p args.
4585 * @param[in,out] set Context and result set at the same time.
4586 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004587 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004588 */
4589static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004590xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004591{
4592 struct lysc_pattern **patterns = NULL, **pattern;
4593 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004594 LY_ERR rc = LY_SUCCESS;
4595 struct ly_err_item *err;
4596
4597 if (options & LYXP_SCNODE_ALL) {
4598 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4599 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4600 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 +02004601 } else if (!warn_is_string_type(sleaf->type)) {
4602 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004603 }
4604 }
4605
4606 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4607 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4608 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 +02004609 } else if (!warn_is_string_type(sleaf->type)) {
4610 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004611 }
4612 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004613 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004614 return rc;
4615 }
4616
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004617 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004618 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004619 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004620 LY_CHECK_RET(rc);
4621
4622 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek IÅ¡a45802b52021-02-09 09:21:58 +01004623 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004624 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004625 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Radek Krejciddace2c2021-01-08 11:30:56 +01004626 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004627 if (rc != LY_SUCCESS) {
4628 LY_ARRAY_FREE(patterns);
4629 return rc;
4630 }
4631
Radek Krejci0b013302021-03-29 15:22:32 +02004632 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004633 pcre2_code_free((*pattern)->code);
4634 free(*pattern);
4635 LY_ARRAY_FREE(patterns);
4636 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004637 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004638 ly_err_free(err);
4639 return rc;
4640 }
4641
4642 if (rc == LY_EVALID) {
4643 ly_err_free(err);
4644 set_fill_boolean(set, 0);
4645 } else {
4646 set_fill_boolean(set, 1);
4647 }
4648
4649 return LY_SUCCESS;
4650}
4651
4652/**
4653 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4654 * with the rounded first argument. For details refer to
4655 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4656 *
4657 * @param[in] args Array of arguments.
4658 * @param[in] arg_count Count of elements in @p args.
4659 * @param[in,out] set Context and result set at the same time.
4660 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004661 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004662 */
4663static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004664xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004665{
4666 struct lysc_node_leaf *sleaf;
4667 LY_ERR rc = LY_SUCCESS;
4668
4669 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004670 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004671 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004672 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4673 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4674 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4675 sleaf->name);
4676 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4677 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4678 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004679 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004680 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004681 return rc;
4682 }
4683
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004684 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004685 LY_CHECK_RET(rc);
4686
4687 /* cover only the cases where floor can't be used */
4688 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4689 set_fill_number(set, -0.0f);
4690 } else {
4691 args[0]->val.num += 0.5;
4692 rc = xpath_floor(args, 1, args[0], options);
4693 LY_CHECK_RET(rc);
4694 set_fill_number(set, args[0]->val.num);
4695 }
4696
4697 return LY_SUCCESS;
4698}
4699
4700/**
4701 * @brief Execute the XPath starts-with(string, string) function.
4702 * Returns LYXP_SET_BOOLEAN whether the second argument is
4703 * the prefix of the first or not.
4704 *
4705 * @param[in] args Array of arguments.
4706 * @param[in] arg_count Count of elements in @p args.
4707 * @param[in,out] set Context and result set at the same time.
4708 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004709 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004710 */
4711static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004712xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004713{
4714 struct lysc_node_leaf *sleaf;
4715 LY_ERR rc = LY_SUCCESS;
4716
4717 if (options & LYXP_SCNODE_ALL) {
4718 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4719 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4720 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 +02004721 } else if (!warn_is_string_type(sleaf->type)) {
4722 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004723 }
4724 }
4725
4726 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4727 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4728 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 +02004729 } else if (!warn_is_string_type(sleaf->type)) {
4730 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004731 }
4732 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004733 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004734 return rc;
4735 }
4736
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004737 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004738 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004739 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004740 LY_CHECK_RET(rc);
4741
4742 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4743 set_fill_boolean(set, 0);
4744 } else {
4745 set_fill_boolean(set, 1);
4746 }
4747
4748 return LY_SUCCESS;
4749}
4750
4751/**
4752 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4753 * with the string representation of either the argument or the context.
4754 *
4755 * @param[in] args Array of arguments.
4756 * @param[in] arg_count Count of elements in @p args.
4757 * @param[in,out] set Context and result set at the same time.
4758 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004759 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004760 */
4761static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004762xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004763{
4764 LY_ERR rc;
4765
4766 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004767 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004768 return LY_SUCCESS;
4769 }
4770
4771 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004772 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004773 LY_CHECK_RET(rc);
4774 set_fill_set(set, args[0]);
4775 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004776 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004777 LY_CHECK_RET(rc);
4778 }
4779
4780 return LY_SUCCESS;
4781}
4782
4783/**
4784 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4785 * with the length of the string in either the argument or the context.
4786 *
4787 * @param[in] args Array of arguments.
4788 * @param[in] arg_count Count of elements in @p args.
4789 * @param[in,out] set Context and result set at the same time.
4790 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004791 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004792 */
4793static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004794xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004795{
4796 struct lysc_node_leaf *sleaf;
4797 LY_ERR rc = LY_SUCCESS;
4798
4799 if (options & LYXP_SCNODE_ALL) {
4800 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4801 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4802 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 +02004803 } else if (!warn_is_string_type(sleaf->type)) {
4804 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004805 }
4806 }
4807 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4808 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4809 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 +02004810 } else if (!warn_is_string_type(sleaf->type)) {
4811 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004812 }
4813 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004814 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004815 return rc;
4816 }
4817
4818 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004819 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004820 LY_CHECK_RET(rc);
4821 set_fill_number(set, strlen(args[0]->val.str));
4822 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004823 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004824 LY_CHECK_RET(rc);
4825 set_fill_number(set, strlen(set->val.str));
4826 }
4827
4828 return LY_SUCCESS;
4829}
4830
4831/**
4832 * @brief Execute the XPath substring(string, number, number?) function.
4833 * Returns LYXP_SET_STRING substring of the first argument starting
4834 * on the second argument index ending on the third argument index,
4835 * indexed from 1. For exact definition refer to
4836 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4837 *
4838 * @param[in] args Array of arguments.
4839 * @param[in] arg_count Count of elements in @p args.
4840 * @param[in,out] set Context and result set at the same time.
4841 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004842 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004843 */
4844static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004845xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004846{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004847 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004848 uint16_t str_start, str_len, pos;
4849 struct lysc_node_leaf *sleaf;
4850 LY_ERR rc = LY_SUCCESS;
4851
4852 if (options & LYXP_SCNODE_ALL) {
4853 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4854 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4855 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 +02004856 } else if (!warn_is_string_type(sleaf->type)) {
4857 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004858 }
4859 }
4860
4861 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4862 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4863 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 +02004864 } else if (!warn_is_numeric_type(sleaf->type)) {
4865 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004866 }
4867 }
4868
Michal Vasko69730152020-10-09 16:30:07 +02004869 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4870 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004871 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4872 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 +02004873 } else if (!warn_is_numeric_type(sleaf->type)) {
4874 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004875 }
4876 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004877 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004878 return rc;
4879 }
4880
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004881 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004882 LY_CHECK_RET(rc);
4883
4884 /* start */
4885 if (xpath_round(&args[1], 1, args[1], options)) {
4886 return -1;
4887 }
4888 if (isfinite(args[1]->val.num)) {
4889 start = args[1]->val.num - 1;
4890 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004891 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004892 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004893 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004894 }
4895
4896 /* len */
4897 if (arg_count == 3) {
4898 rc = xpath_round(&args[2], 1, args[2], options);
4899 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004900 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004901 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004902 } else if (isfinite(args[2]->val.num)) {
4903 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004904 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004905 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004906 }
4907 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004908 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004909 }
4910
4911 /* find matching character positions */
4912 str_start = 0;
4913 str_len = 0;
4914 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4915 if (pos < start) {
4916 ++str_start;
4917 } else if (pos < start + len) {
4918 ++str_len;
4919 } else {
4920 break;
4921 }
4922 }
4923
4924 set_fill_string(set, args[0]->val.str + str_start, str_len);
4925 return LY_SUCCESS;
4926}
4927
4928/**
4929 * @brief Execute the XPath substring-after(string, string) function.
4930 * Returns LYXP_SET_STRING with the string succeeding the occurance
4931 * of the second argument in the first or an empty string.
4932 *
4933 * @param[in] args Array of arguments.
4934 * @param[in] arg_count Count of elements in @p args.
4935 * @param[in,out] set Context and result set at the same time.
4936 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004937 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004938 */
4939static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004940xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004941{
4942 char *ptr;
4943 struct lysc_node_leaf *sleaf;
4944 LY_ERR rc = LY_SUCCESS;
4945
4946 if (options & LYXP_SCNODE_ALL) {
4947 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4948 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4949 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 +02004950 } else if (!warn_is_string_type(sleaf->type)) {
4951 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004952 }
4953 }
4954
4955 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4956 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4957 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 +02004958 } else if (!warn_is_string_type(sleaf->type)) {
4959 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004960 }
4961 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004962 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004963 return rc;
4964 }
4965
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004966 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004967 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004968 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004969 LY_CHECK_RET(rc);
4970
4971 ptr = strstr(args[0]->val.str, args[1]->val.str);
4972 if (ptr) {
4973 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4974 } else {
4975 set_fill_string(set, "", 0);
4976 }
4977
4978 return LY_SUCCESS;
4979}
4980
4981/**
4982 * @brief Execute the XPath substring-before(string, string) function.
4983 * Returns LYXP_SET_STRING with the string preceding the occurance
4984 * of the second argument in the first or an empty string.
4985 *
4986 * @param[in] args Array of arguments.
4987 * @param[in] arg_count Count of elements in @p args.
4988 * @param[in,out] set Context and result set at the same time.
4989 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004990 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004991 */
4992static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004993xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004994{
4995 char *ptr;
4996 struct lysc_node_leaf *sleaf;
4997 LY_ERR rc = LY_SUCCESS;
4998
4999 if (options & LYXP_SCNODE_ALL) {
5000 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5001 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5002 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 +02005003 } else if (!warn_is_string_type(sleaf->type)) {
5004 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005005 }
5006 }
5007
5008 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5009 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5010 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 +02005011 } else if (!warn_is_string_type(sleaf->type)) {
5012 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005013 }
5014 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005015 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005016 return rc;
5017 }
5018
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005019 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005020 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005021 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005022 LY_CHECK_RET(rc);
5023
5024 ptr = strstr(args[0]->val.str, args[1]->val.str);
5025 if (ptr) {
5026 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5027 } else {
5028 set_fill_string(set, "", 0);
5029 }
5030
5031 return LY_SUCCESS;
5032}
5033
5034/**
5035 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5036 * with the sum of all the nodes in the context.
5037 *
5038 * @param[in] args Array of arguments.
5039 * @param[in] arg_count Count of elements in @p args.
5040 * @param[in,out] set Context and result set at the same time.
5041 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005042 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005043 */
5044static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005045xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005046{
5047 long double num;
5048 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005049 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005050 struct lyxp_set set_item;
5051 struct lysc_node_leaf *sleaf;
5052 LY_ERR rc = LY_SUCCESS;
5053
5054 if (options & LYXP_SCNODE_ALL) {
5055 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5056 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005057 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005058 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5059 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5060 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005061 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005062 } else if (!warn_is_numeric_type(sleaf->type)) {
5063 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005064 }
5065 }
5066 }
5067 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005068 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005069 return rc;
5070 }
5071
5072 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005073
5074 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005075 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005076 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005077 } else if (!args[0]->used) {
5078 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005079 }
5080
Michal Vasko5c4e5892019-11-14 12:31:38 +01005081 set_init(&set_item, set);
5082
Michal Vasko03ff5a72019-09-11 13:49:33 +02005083 set_item.type = LYXP_SET_NODE_SET;
5084 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5085 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5086
5087 set_item.used = 1;
5088 set_item.size = 1;
5089
5090 for (i = 0; i < args[0]->used; ++i) {
5091 set_item.val.nodes[0] = args[0]->val.nodes[i];
5092
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005093 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005094 LY_CHECK_RET(rc);
5095 num = cast_string_to_number(str);
5096 free(str);
5097 set->val.num += num;
5098 }
5099
5100 free(set_item.val.nodes);
5101
5102 return LY_SUCCESS;
5103}
5104
5105/**
5106 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5107 * with the text content of the nodes in the context.
5108 *
5109 * @param[in] args Array of arguments.
5110 * @param[in] arg_count Count of elements in @p args.
5111 * @param[in,out] set Context and result set at the same time.
5112 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005113 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005114 */
5115static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005116xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005117{
5118 uint32_t i;
5119
5120 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005121 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005122 return LY_SUCCESS;
5123 }
5124
Michal Vasko03ff5a72019-09-11 13:49:33 +02005125 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005126 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005127 return LY_EVALID;
5128 }
5129
Michal Vaskod989ba02020-08-24 10:59:24 +02005130 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005131 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005132 case LYXP_NODE_NONE:
5133 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005134 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005135 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5136 set->val.nodes[i].type = LYXP_NODE_TEXT;
5137 ++i;
5138 break;
5139 }
Radek Krejci0f969882020-08-21 16:56:47 +02005140 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005141 case LYXP_NODE_ROOT:
5142 case LYXP_NODE_ROOT_CONFIG:
5143 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005144 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005145 set_remove_node(set, i);
5146 break;
5147 }
5148 }
5149
5150 return LY_SUCCESS;
5151}
5152
5153/**
5154 * @brief Execute the XPath translate(string, string, string) function.
5155 * Returns LYXP_SET_STRING with the first argument with the characters
5156 * from the second argument replaced by those on the corresponding
5157 * positions in the third argument.
5158 *
5159 * @param[in] args Array of arguments.
5160 * @param[in] arg_count Count of elements in @p args.
5161 * @param[in,out] set Context and result set at the same time.
5162 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005163 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005164 */
5165static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005166xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005167{
5168 uint16_t i, j, new_used;
5169 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005170 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005171 struct lysc_node_leaf *sleaf;
5172 LY_ERR rc = LY_SUCCESS;
5173
5174 if (options & LYXP_SCNODE_ALL) {
5175 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5176 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5177 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 +02005178 } else if (!warn_is_string_type(sleaf->type)) {
5179 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005180 }
5181 }
5182
5183 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5184 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5185 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 +02005186 } else if (!warn_is_string_type(sleaf->type)) {
5187 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005188 }
5189 }
5190
5191 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5192 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5193 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 +02005194 } else if (!warn_is_string_type(sleaf->type)) {
5195 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005196 }
5197 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005198 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005199 return rc;
5200 }
5201
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005202 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005203 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005204 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005205 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005206 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005207 LY_CHECK_RET(rc);
5208
5209 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5210 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5211 new_used = 0;
5212
5213 have_removed = 0;
5214 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005215 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005216
5217 for (j = 0; args[1]->val.str[j]; ++j) {
5218 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5219 /* removing this char */
5220 if (j >= strlen(args[2]->val.str)) {
5221 have_removed = 1;
5222 found = 1;
5223 break;
5224 }
5225 /* replacing this char */
5226 new[new_used] = args[2]->val.str[j];
5227 ++new_used;
5228 found = 1;
5229 break;
5230 }
5231 }
5232
5233 /* copying this char */
5234 if (!found) {
5235 new[new_used] = args[0]->val.str[i];
5236 ++new_used;
5237 }
5238 }
5239
5240 if (have_removed) {
5241 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5242 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5243 }
5244 new[new_used] = '\0';
5245
Michal Vaskod3678892020-05-21 10:06:58 +02005246 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005247 set->type = LYXP_SET_STRING;
5248 set->val.str = new;
5249
5250 return LY_SUCCESS;
5251}
5252
5253/**
5254 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5255 * with true value.
5256 *
5257 * @param[in] args Array of arguments.
5258 * @param[in] arg_count Count of elements in @p args.
5259 * @param[in,out] set Context and result set at the same time.
5260 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005261 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005262 */
5263static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005264xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005265{
5266 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005267 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005268 return LY_SUCCESS;
5269 }
5270
5271 set_fill_boolean(set, 1);
5272 return LY_SUCCESS;
5273}
5274
5275/*
5276 * moveto functions
5277 *
5278 * They and only they actually change the context (set).
5279 */
5280
5281/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005282 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005283 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005284 * XPath @p set is expected to be a (sc)node set!
5285 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005286 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5287 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005288 * @param[in] set Set with general XPath context.
5289 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005290 * @param[out] moveto_mod Expected module of a matching node.
5291 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005292 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005293static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005294moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5295 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005296{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005297 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005298 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005299 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005300
Michal Vasko2104e9f2020-03-06 08:23:25 +01005301 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5302
Michal Vasko6346ece2019-09-24 13:12:53 +02005303 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5304 /* specific module */
5305 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005306 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005307
Michal Vasko004d3152020-06-11 19:59:22 +02005308 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005309 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005310 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005311 return LY_EVALID;
5312 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005313
Michal Vasko6346ece2019-09-24 13:12:53 +02005314 *qname += pref_len + 1;
5315 *qname_len -= pref_len + 1;
5316 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5317 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005318 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005319 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005320 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005321 case LY_VALUE_SCHEMA:
5322 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005323 /* current module */
5324 mod = set->cur_mod;
5325 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005326 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005327 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005328 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005329 /* inherit parent (context node) module */
5330 if (ctx_scnode) {
5331 mod = ctx_scnode->module;
5332 } else {
5333 mod = NULL;
5334 }
5335 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005336 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005337 /* all nodes need to be prefixed */
5338 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5339 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005340 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005341 }
5342
Michal Vasko6346ece2019-09-24 13:12:53 +02005343 *moveto_mod = mod;
5344 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005345}
5346
5347/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005348 * @brief Move context @p set to the root. Handles absolute path.
5349 * Result is LYXP_SET_NODE_SET.
5350 *
5351 * @param[in,out] set Set to use.
5352 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005353 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005354 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005355static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005356moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005357{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005358 if (!set) {
Michal Vaskob0099a92020-08-31 14:55:23 +02005359 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005360 }
5361
5362 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005363 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskob0099a92020-08-31 14:55:23 +02005364 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005365 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005366 set->type = LYXP_SET_NODE_SET;
5367 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005368 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005369 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005370
5371 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005372}
5373
5374/**
5375 * @brief Check @p node as a part of NameTest processing.
5376 *
5377 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005378 * @param[in] ctx_node Context node.
5379 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005380 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005381 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005382 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005383 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5384 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005385 */
5386static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005387moveto_node_check(const struct lyd_node *node, const struct lyd_node *ctx_node, const struct lyxp_set *set,
Michal Vaskocdad7122020-11-09 21:04:44 +01005388 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005389{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005390 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5391 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005392 case LY_VALUE_SCHEMA:
5393 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005394 /* use current module */
5395 moveto_mod = set->cur_mod;
5396 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005397 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005398 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005399 /* inherit module of the context node, if any */
5400 if (ctx_node) {
5401 moveto_mod = ctx_node->schema->module;
5402 }
5403 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005404 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005405 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005406 /* not defined */
5407 LOGINT(set->ctx);
5408 return LY_EINVAL;
5409 }
5410 }
5411
Michal Vasko03ff5a72019-09-11 13:49:33 +02005412 /* module check */
5413 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005414 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005415 }
5416
Michal Vasko5c4e5892019-11-14 12:31:38 +01005417 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005418 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005419 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005420 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5421 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005422 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005423 }
5424
5425 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005426 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005427 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005428 }
5429
Michal Vaskoa1424542019-11-14 16:08:52 +01005430 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005431 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005432 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005433 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005434
5435 /* match */
5436 return LY_SUCCESS;
5437}
5438
5439/**
5440 * @brief Check @p node as a part of schema NameTest processing.
5441 *
5442 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005443 * @param[in] ctx_scnode Context node.
5444 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005445 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005446 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005447 * @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 +02005448 */
5449static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005450moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set,
Radek Krejci0f969882020-08-21 16:56:47 +02005451 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005452{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005453 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5454 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005455 case LY_VALUE_SCHEMA:
5456 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005457 /* use current module */
5458 moveto_mod = set->cur_mod;
5459 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005460 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005461 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005462 /* inherit module of the context node, if any */
5463 if (ctx_scnode) {
5464 moveto_mod = ctx_scnode->module;
5465 }
5466 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005467 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005468 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005469 /* not defined */
5470 LOGINT(set->ctx);
5471 return LY_EINVAL;
5472 }
5473 }
5474
Michal Vasko03ff5a72019-09-11 13:49:33 +02005475 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005476 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005477 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005478 }
5479
5480 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005481 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005482 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005483 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005484 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005485 }
5486
5487 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005488 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005489 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005490 }
5491
5492 /* match */
5493 return LY_SUCCESS;
5494}
5495
5496/**
Michal Vaskod3678892020-05-21 10:06:58 +02005497 * @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 +02005498 *
5499 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005500 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005501 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005502 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005503 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5504 */
5505static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005506moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005507{
Michal Vaskof03ed032020-03-04 13:31:44 +01005508 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005509 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005510 LY_ERR rc;
5511
Michal Vaskod3678892020-05-21 10:06:58 +02005512 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005513 return LY_SUCCESS;
5514 }
5515
5516 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005517 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005518 return LY_EVALID;
5519 }
5520
Michal Vasko03ff5a72019-09-11 13:49:33 +02005521 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005522 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005523
5524 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 +01005525 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005526
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005527 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005528 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005529 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005530 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005531 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005532 ctx_node = set->val.nodes[i].node;
5533 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005534 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005535
Michal Vaskod3678892020-05-21 10:06:58 +02005536 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005537 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005538 if (rc == LY_SUCCESS) {
5539 if (!replaced) {
5540 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5541 replaced = 1;
5542 } else {
5543 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005544 }
Michal Vaskod3678892020-05-21 10:06:58 +02005545 ++i;
5546 } else if (rc == LY_EINCOMPLETE) {
5547 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005548 }
5549 }
5550
5551 if (!replaced) {
5552 /* no match */
5553 set_remove_node(set, i);
5554 }
5555 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005556
5557 return LY_SUCCESS;
5558}
5559
5560/**
Michal Vaskod3678892020-05-21 10:06:58 +02005561 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5562 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005563 *
5564 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005565 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005566 * @param[in] predicates If @p scnode is ::LYS_LIST or ::LYS_LEAFLIST, the predicates specifying a single instance.
Michal Vaskocdad7122020-11-09 21:04:44 +01005567 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005568 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5569 */
5570static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005571moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5572 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005573{
Michal Vasko004d3152020-06-11 19:59:22 +02005574 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005575 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005576 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005577 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005578
Michal Vasko004d3152020-06-11 19:59:22 +02005579 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005580
5581 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005582 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005583 }
5584
5585 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005586 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005587 ret = LY_EVALID;
5588 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005589 }
5590
5591 /* context check for all the nodes since we have the schema node */
5592 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5593 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005594 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005595 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5596 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005597 lyxp_set_free_content(set);
5598 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005599 }
5600
5601 /* create specific data instance if needed */
5602 if (scnode->nodetype == LYS_LIST) {
5603 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5604 } else if (scnode->nodetype == LYS_LEAFLIST) {
5605 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005606 }
5607
5608 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005609 siblings = NULL;
5610
5611 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5612 assert(!set->val.nodes[i].node);
5613
5614 /* search in all the trees */
5615 siblings = set->tree;
5616 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5617 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005618 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005619 }
5620
5621 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005622 if (inst) {
5623 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005624 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005625 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005626 }
5627
5628 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005629 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005630 ret = LY_EINCOMPLETE;
5631 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005632 }
5633
5634 if (sub) {
5635 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005636 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005637 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005638 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005639 /* no match */
5640 set_remove_node(set, i);
5641 }
5642 }
5643
Michal Vasko004d3152020-06-11 19:59:22 +02005644cleanup:
5645 lyd_free_tree(inst);
5646 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005647}
5648
5649/**
5650 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5651 *
5652 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005653 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005654 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005655 * @param[in] options XPath options.
5656 * @return LY_ERR
5657 */
5658static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005659moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005660{
Radek Krejci857189e2020-09-01 13:26:36 +02005661 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005662 uint32_t getnext_opts;
5663 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005664 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005665 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005666 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005667
Michal Vaskod3678892020-05-21 10:06:58 +02005668 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005669 return LY_SUCCESS;
5670 }
5671
5672 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005673 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005674 return LY_EVALID;
5675 }
5676
Michal Vaskocafad9d2019-11-07 15:20:03 +01005677 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005678 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005679 if (options & LYXP_SCNODE_OUTPUT) {
5680 getnext_opts |= LYS_GETNEXT_OUTPUT;
5681 }
5682
Michal Vasko03ff5a72019-09-11 13:49:33 +02005683 orig_used = set->used;
5684 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005685 uint32_t idx;
5686
Radek Krejcif13b87b2020-12-01 22:02:17 +01005687 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5688 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005689 continue;
5690 }
5691
5692 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005693 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005694 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005695 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005696 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005697
5698 start_parent = set->val.scnodes[i].scnode;
5699
5700 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005701 /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set),
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005702 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005703 mod_idx = 0;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005704 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005705 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005706 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005707 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005708 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005709 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5710
Michal Vasko03ff5a72019-09-11 13:49:33 +02005711 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005712 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005713 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005714 temp_ctx = 1;
5715 }
5716 }
5717 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005718 }
5719
Michal Vasko519fd602020-05-26 12:17:39 +02005720 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5721 iter = NULL;
5722 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005723 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005724 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5725
5726 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005727 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005728 temp_ctx = 1;
5729 }
5730 }
5731 }
5732 }
5733 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005734
5735 /* correct temporary in_ctx values */
5736 if (temp_ctx) {
5737 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005738 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5739 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005740 }
5741 }
5742 }
5743
5744 return LY_SUCCESS;
5745}
5746
5747/**
Michal Vaskod3678892020-05-21 10:06:58 +02005748 * @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 +02005749 * Context position aware.
5750 *
5751 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005752 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005753 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005754 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005755 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5756 */
5757static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005758moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005759{
5760 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005761 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005762 struct lyxp_set ret_set;
5763 LY_ERR rc;
5764
Michal Vaskod3678892020-05-21 10:06:58 +02005765 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005766 return LY_SUCCESS;
5767 }
5768
5769 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005770 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005771 return LY_EVALID;
5772 }
5773
Michal Vasko9f96a052020-03-10 09:41:45 +01005774 /* replace the original nodes (and throws away all text and meta nodes, root is replaced by a child) */
Michal Vaskocdad7122020-11-09 21:04:44 +01005775 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005776 LY_CHECK_RET(rc);
5777
Michal Vasko6346ece2019-09-24 13:12:53 +02005778 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005779 set_init(&ret_set, set);
5780 for (i = 0; i < set->used; ++i) {
5781
5782 /* TREE DFS */
5783 start = set->val.nodes[i].node;
5784 for (elem = next = start; elem; elem = next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005785 rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005786 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005787 /* add matching node into result set */
5788 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5789 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5790 /* the node is a duplicate, we'll process it later in the set */
5791 goto skip_children;
5792 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005793 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005794 return rc;
5795 } else if (rc == LY_EINVAL) {
5796 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005797 }
5798
5799 /* TREE DFS NEXT ELEM */
5800 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005801 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005802 if (!next) {
5803skip_children:
5804 /* no children, so try siblings, but only if it's not the start,
5805 * that is considered to be the root and it's siblings are not traversed */
5806 if (elem != start) {
5807 next = elem->next;
5808 } else {
5809 break;
5810 }
5811 }
5812 while (!next) {
5813 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01005814 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005815 /* we are done, no next element to process */
5816 break;
5817 }
5818 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01005819 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005820 next = elem->next;
5821 }
5822 }
5823 }
5824
5825 /* make the temporary set the current one */
5826 ret_set.ctx_pos = set->ctx_pos;
5827 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005828 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005829 memcpy(set, &ret_set, sizeof *set);
5830
5831 return LY_SUCCESS;
5832}
5833
5834/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005835 * @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 +02005836 *
5837 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005838 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005839 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005840 * @param[in] options XPath options.
5841 * @return LY_ERR
5842 */
5843static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005844moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005845{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005846 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005847 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005848 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005849
Michal Vaskod3678892020-05-21 10:06:58 +02005850 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005851 return LY_SUCCESS;
5852 }
5853
5854 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005855 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005856 return LY_EVALID;
5857 }
5858
Michal Vasko03ff5a72019-09-11 13:49:33 +02005859 orig_used = set->used;
5860 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005861 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5862 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005863 continue;
5864 }
5865
5866 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005867 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005868 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005869 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005870 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005871
5872 /* TREE DFS */
5873 start = set->val.scnodes[i].scnode;
5874 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005875 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5876 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005877 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005878 }
5879
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005880 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005881 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005882 uint32_t idx;
5883
5884 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005885 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005886 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005887 /* we will process it later in the set */
5888 goto skip_children;
5889 }
5890 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005891 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005892 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005893 } else if (rc == LY_EINVAL) {
5894 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005895 }
5896
5897next_iter:
5898 /* TREE DFS NEXT ELEM */
5899 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005900 next = lysc_node_child(elem);
5901 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5902 next = next->next;
5903 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5904 next = next->next;
5905 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005906 if (!next) {
5907skip_children:
5908 /* no children, so try siblings, but only if it's not the start,
5909 * that is considered to be the root and it's siblings are not traversed */
5910 if (elem != start) {
5911 next = elem->next;
5912 } else {
5913 break;
5914 }
5915 }
5916 while (!next) {
5917 /* no siblings, go back through the parents */
5918 if (elem->parent == start) {
5919 /* we are done, no next element to process */
5920 break;
5921 }
5922 /* parent is already processed, go to its sibling */
5923 elem = elem->parent;
5924 next = elem->next;
5925 }
5926 }
5927 }
5928
5929 return LY_SUCCESS;
5930}
5931
5932/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005933 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005934 * Indirectly context position aware.
5935 *
5936 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005937 * @param[in] mod Matching metadata module, NULL for any.
5938 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005939 * @return LY_ERR
5940 */
5941static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005942moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005943{
Michal Vasko9f96a052020-03-10 09:41:45 +01005944 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005945
Michal Vaskod3678892020-05-21 10:06:58 +02005946 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005947 return LY_SUCCESS;
5948 }
5949
5950 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005951 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005952 return LY_EVALID;
5953 }
5954
Radek Krejci1deb5be2020-08-26 16:43:36 +02005955 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005956 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005957
5958 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5959 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005960 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005961 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005962
5963 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005964 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005965 continue;
5966 }
5967
Michal Vaskod3678892020-05-21 10:06:58 +02005968 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005969 /* match */
5970 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005971 set->val.meta[i].meta = sub;
5972 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005973 /* pos does not change */
5974 replaced = 1;
5975 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005976 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 +02005977 }
5978 ++i;
5979 }
5980 }
5981 }
5982
5983 if (!replaced) {
5984 /* no match */
5985 set_remove_node(set, i);
5986 }
5987 }
5988
5989 return LY_SUCCESS;
5990}
5991
5992/**
5993 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005994 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005995 *
5996 * @param[in,out] set1 Set to use for the result.
5997 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005998 * @return LY_ERR
5999 */
6000static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006001moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006002{
6003 LY_ERR rc;
6004
Michal Vaskod3678892020-05-21 10:06:58 +02006005 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006006 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006007 return LY_EVALID;
6008 }
6009
6010 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02006011 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006012 return LY_SUCCESS;
6013 }
6014
Michal Vaskod3678892020-05-21 10:06:58 +02006015 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006016 memcpy(set1, set2, sizeof *set1);
6017 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02006018 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006019 return LY_SUCCESS;
6020 }
6021
6022 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006023 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006024
6025 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006026 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006027 LY_CHECK_RET(rc);
6028
6029 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006030 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006031
6032 return LY_SUCCESS;
6033}
6034
6035/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006036 * @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 +02006037 * Context position aware.
6038 *
6039 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006040 * @param[in] mod Matching metadata module, NULL for any.
6041 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006042 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006043 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6044 */
6045static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006046moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006047{
Michal Vasko9f96a052020-03-10 09:41:45 +01006048 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006049 struct lyxp_set *set_all_desc = NULL;
6050 LY_ERR rc;
6051
Michal Vaskod3678892020-05-21 10:06:58 +02006052 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006053 return LY_SUCCESS;
6054 }
6055
6056 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006057 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006058 return LY_EVALID;
6059 }
6060
Michal Vasko03ff5a72019-09-11 13:49:33 +02006061 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6062 * but it likely won't be used much, so it's a waste of time */
6063 /* copy the context */
6064 set_all_desc = set_copy(set);
6065 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006066 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006067 if (rc != LY_SUCCESS) {
6068 lyxp_set_free(set_all_desc);
6069 return rc;
6070 }
6071 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006072 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006073 if (rc != LY_SUCCESS) {
6074 lyxp_set_free(set_all_desc);
6075 return rc;
6076 }
6077 lyxp_set_free(set_all_desc);
6078
Radek Krejci1deb5be2020-08-26 16:43:36 +02006079 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006080 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006081
6082 /* only attributes of an elem can be in the result, skip all the rest,
6083 * we have all attributes qualified in lyd tree */
6084 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006085 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006086 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006087 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006088 continue;
6089 }
6090
Michal Vaskod3678892020-05-21 10:06:58 +02006091 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006092 /* match */
6093 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006094 set->val.meta[i].meta = sub;
6095 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006096 /* pos does not change */
6097 replaced = 1;
6098 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006099 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 +02006100 }
6101 ++i;
6102 }
6103 }
6104 }
6105
6106 if (!replaced) {
6107 /* no match */
6108 set_remove_node(set, i);
6109 }
6110 }
6111
6112 return LY_SUCCESS;
6113}
6114
6115/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006116 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6117 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006118 *
6119 * @param[in] parent Current parent.
6120 * @param[in] parent_pos Position of @p parent.
6121 * @param[in] parent_type Node type of @p parent.
6122 * @param[in,out] to_set Set to use.
6123 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006124 * @param[in] options XPath options.
6125 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6126 */
6127static LY_ERR
6128moveto_self_add_children_r(const struct lyd_node *parent, uint32_t parent_pos, enum lyxp_node_type parent_type,
Radek Krejci1deb5be2020-08-26 16:43:36 +02006129 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006130{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006131 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006132 LY_ERR rc;
6133
6134 switch (parent_type) {
6135 case LYXP_NODE_ROOT:
6136 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006137 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006138
Michal Vasko61ac2f62020-05-25 12:39:51 +02006139 /* add all top-level nodes as elements */
6140 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006141 break;
6142 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006143 /* add just the text node of this term element node */
6144 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006145 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6146 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6147 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006148 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006149 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006150
6151 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006152 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006153 break;
6154 default:
6155 LOGINT_RET(parent->schema->module->ctx);
6156 }
6157
Michal Vasko61ac2f62020-05-25 12:39:51 +02006158 /* add all top-level nodes as elements */
6159 LY_LIST_FOR(first, iter) {
6160 /* context check */
6161 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6162 continue;
6163 }
6164
6165 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006166 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006167 return LY_EINCOMPLETE;
6168 }
6169
6170 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6171 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6172
6173 /* also add all the children of this node, recursively */
6174 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6175 LY_CHECK_RET(rc);
6176 }
6177 }
6178
Michal Vasko03ff5a72019-09-11 13:49:33 +02006179 return LY_SUCCESS;
6180}
6181
6182/**
6183 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6184 * (or LYXP_SET_EMPTY). Context position aware.
6185 *
6186 * @param[in,out] set Set to use.
6187 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6188 * @param[in] options XPath options.
6189 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6190 */
6191static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006192moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006193{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006194 struct lyxp_set ret_set;
6195 LY_ERR rc;
6196
Michal Vaskod3678892020-05-21 10:06:58 +02006197 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006198 return LY_SUCCESS;
6199 }
6200
6201 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006202 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006203 return LY_EVALID;
6204 }
6205
6206 /* nothing to do */
6207 if (!all_desc) {
6208 return LY_SUCCESS;
6209 }
6210
Michal Vasko03ff5a72019-09-11 13:49:33 +02006211 /* add all the children, they get added recursively */
6212 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006213 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006214 /* copy the current node to tmp */
6215 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6216
6217 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006218 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006219 continue;
6220 }
6221
Michal Vasko03ff5a72019-09-11 13:49:33 +02006222 /* add all the children */
6223 rc = moveto_self_add_children_r(set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, &ret_set,
Michal Vasko69730152020-10-09 16:30:07 +02006224 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006225 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006226 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006227 return rc;
6228 }
6229 }
6230
6231 /* use the temporary set as the current one */
6232 ret_set.ctx_pos = set->ctx_pos;
6233 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006234 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006235 memcpy(set, &ret_set, sizeof *set);
6236
6237 return LY_SUCCESS;
6238}
6239
6240/**
6241 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6242 * (or LYXP_SET_EMPTY).
6243 *
6244 * @param[in,out] set Set to use.
6245 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6246 * @param[in] options XPath options.
6247 * @return LY_ERR
6248 */
6249static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006250moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006251{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006252 uint32_t getnext_opts;
6253 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006254 const struct lysc_node *iter, *start_parent;
6255 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006256
Michal Vaskod3678892020-05-21 10:06:58 +02006257 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006258 return LY_SUCCESS;
6259 }
6260
6261 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006262 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006263 return LY_EVALID;
6264 }
6265
Michal Vasko519fd602020-05-26 12:17:39 +02006266 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006267 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006268 if (options & LYXP_SCNODE_OUTPUT) {
6269 getnext_opts |= LYS_GETNEXT_OUTPUT;
6270 }
6271
6272 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006273 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006274 if (!all_desc) {
6275 /* traverse the start node */
6276 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6277 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6278 }
6279 continue;
6280 }
6281
Radek Krejcif13b87b2020-12-01 22:02:17 +01006282 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6283 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006284 continue;
6285 }
6286
Michal Vasko519fd602020-05-26 12:17:39 +02006287 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006288 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006289 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006290 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006291 }
6292
Michal Vasko519fd602020-05-26 12:17:39 +02006293 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006294
Michal Vasko519fd602020-05-26 12:17:39 +02006295 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6296 /* it can actually be in any module, it's all <running> */
6297 mod_idx = 0;
6298 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6299 iter = NULL;
6300 /* module may not be implemented */
6301 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6302 /* context check */
6303 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6304 continue;
6305 }
6306
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006307 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006308 /* throw away the insert index, we want to consider that node again, recursively */
6309 }
6310 }
6311
6312 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6313 iter = NULL;
6314 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006315 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006316 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006317 continue;
6318 }
6319
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006320 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006321 }
6322 }
6323 }
6324
6325 return LY_SUCCESS;
6326}
6327
6328/**
6329 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6330 * (or LYXP_SET_EMPTY). Context position aware.
6331 *
6332 * @param[in] set Set to use.
6333 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6334 * @param[in] options XPath options.
6335 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6336 */
6337static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006338moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006339{
6340 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006341 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006342 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006343
Michal Vaskod3678892020-05-21 10:06:58 +02006344 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006345 return LY_SUCCESS;
6346 }
6347
6348 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006349 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006350 return LY_EVALID;
6351 }
6352
6353 if (all_desc) {
6354 /* <path>//.. == <path>//./.. */
6355 rc = moveto_self(set, 1, options);
6356 LY_CHECK_RET(rc);
6357 }
6358
Radek Krejci1deb5be2020-08-26 16:43:36 +02006359 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006360 node = set->val.nodes[i].node;
6361
6362 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9e685082021-01-29 14:49:09 +01006363 new_node = lyd_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006364 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6365 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006366 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6367 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006368 if (!new_node) {
6369 LOGINT_RET(set->ctx);
6370 }
6371 } else {
6372 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006373 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006374 continue;
6375 }
6376
Michal Vaskoa1424542019-11-14 16:08:52 +01006377 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006378 if (!(options & LYXP_IGNORE_WHEN) && new_node && lysc_has_when(new_node->schema) && !(new_node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006379 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006380 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006381
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006382 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006383 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006384 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006385
Michal Vasko03ff5a72019-09-11 13:49:33 +02006386 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006387 /* 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 +02006388 new_type = LYXP_NODE_ELEM;
6389 }
6390
Michal Vasko03ff5a72019-09-11 13:49:33 +02006391 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006392 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006393 } else {
6394 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006395 }
6396 }
6397
Michal Vasko2caefc12019-11-14 16:07:56 +01006398 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006399 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006400
6401 return LY_SUCCESS;
6402}
6403
6404/**
6405 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6406 * (or LYXP_SET_EMPTY).
6407 *
6408 * @param[in] set Set to use.
6409 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6410 * @param[in] options XPath options.
6411 * @return LY_ERR
6412 */
6413static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006414moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006415{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006416 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006417 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006418 const struct lysc_node *node, *new_node;
6419 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006420
Michal Vaskod3678892020-05-21 10:06:58 +02006421 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006422 return LY_SUCCESS;
6423 }
6424
6425 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006426 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006427 return LY_EVALID;
6428 }
6429
6430 if (all_desc) {
6431 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006432 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006433 }
6434
Michal Vasko03ff5a72019-09-11 13:49:33 +02006435 orig_used = set->used;
6436 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006437 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6438 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006439 continue;
6440 }
6441
6442 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006443 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006444 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006445 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006446 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006447
6448 node = set->val.scnodes[i].scnode;
6449
6450 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006451 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006452 } else {
6453 /* root does not have a parent */
6454 continue;
6455 }
6456
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006457 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006458 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006459 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006460
Michal Vasko03ff5a72019-09-11 13:49:33 +02006461 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006462 /* 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 +02006463 new_type = LYXP_NODE_ELEM;
6464 }
6465
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006466 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6467 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006468 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006469 temp_ctx = 1;
6470 }
6471 }
6472
6473 if (temp_ctx) {
6474 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006475 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6476 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006477 }
6478 }
6479 }
6480
6481 return LY_SUCCESS;
6482}
6483
6484/**
6485 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6486 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6487 *
6488 * @param[in,out] set1 Set to use for the result.
6489 * @param[in] set2 Set acting as the second operand for @p op.
6490 * @param[in] op Comparison operator to process.
6491 * @param[in] options XPath options.
6492 * @return LY_ERR
6493 */
6494static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006495moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006496{
6497 /*
6498 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6499 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6500 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6501 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6502 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6503 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6504 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6505 *
6506 * '=' or '!='
6507 * BOOLEAN + BOOLEAN
6508 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6509 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6510 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6511 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6512 * NUMBER + NUMBER
6513 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6514 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6515 * STRING + STRING
6516 *
6517 * '<=', '<', '>=', '>'
6518 * NUMBER + NUMBER
6519 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6520 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6521 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6522 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6523 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6524 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6525 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6526 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6527 */
6528 struct lyxp_set iter1, iter2;
6529 int result;
6530 int64_t i;
6531 LY_ERR rc;
6532
Michal Vasko004d3152020-06-11 19:59:22 +02006533 memset(&iter1, 0, sizeof iter1);
6534 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006535
6536 /* iterative evaluation with node-sets */
6537 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6538 if (set1->type == LYXP_SET_NODE_SET) {
6539 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006540 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006541 switch (set2->type) {
6542 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006543 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006544 break;
6545 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006546 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006547 break;
6548 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006549 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006550 break;
6551 }
6552 LY_CHECK_RET(rc);
6553
Michal Vasko4c7763f2020-07-27 17:40:37 +02006554 /* canonize set2 */
6555 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6556
6557 /* compare recursively */
6558 rc = moveto_op_comp(&iter1, &iter2, op, options);
6559 lyxp_set_free_content(&iter2);
6560 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006561
6562 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006563 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006564 set_fill_boolean(set1, 1);
6565 return LY_SUCCESS;
6566 }
6567 }
6568 } else {
6569 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006570 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006571 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006572 case LYXP_SET_NUMBER:
6573 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6574 break;
6575 case LYXP_SET_BOOLEAN:
6576 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6577 break;
6578 default:
6579 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6580 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006581 }
6582 LY_CHECK_RET(rc);
6583
Michal Vasko4c7763f2020-07-27 17:40:37 +02006584 /* canonize set1 */
6585 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 +02006586
Michal Vasko4c7763f2020-07-27 17:40:37 +02006587 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006588 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006589 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006590 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006591
6592 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006593 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006594 set_fill_boolean(set1, 1);
6595 return LY_SUCCESS;
6596 }
6597 }
6598 }
6599
6600 /* false for all nodes */
6601 set_fill_boolean(set1, 0);
6602 return LY_SUCCESS;
6603 }
6604
6605 /* first convert properly */
6606 if ((op[0] == '=') || (op[0] == '!')) {
6607 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006608 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6609 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006610 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006611 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006612 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006613 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006614 LY_CHECK_RET(rc);
6615 } /* else we have 2 strings */
6616 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006617 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006618 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006619 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006620 LY_CHECK_RET(rc);
6621 }
6622
6623 assert(set1->type == set2->type);
6624
6625 /* compute result */
6626 if (op[0] == '=') {
6627 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006628 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006629 } else if (set1->type == LYXP_SET_NUMBER) {
6630 result = (set1->val.num == set2->val.num);
6631 } else {
6632 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006633 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006634 }
6635 } else if (op[0] == '!') {
6636 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006637 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006638 } else if (set1->type == LYXP_SET_NUMBER) {
6639 result = (set1->val.num != set2->val.num);
6640 } else {
6641 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006642 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006643 }
6644 } else {
6645 assert(set1->type == LYXP_SET_NUMBER);
6646 if (op[0] == '<') {
6647 if (op[1] == '=') {
6648 result = (set1->val.num <= set2->val.num);
6649 } else {
6650 result = (set1->val.num < set2->val.num);
6651 }
6652 } else {
6653 if (op[1] == '=') {
6654 result = (set1->val.num >= set2->val.num);
6655 } else {
6656 result = (set1->val.num > set2->val.num);
6657 }
6658 }
6659 }
6660
6661 /* assign result */
6662 if (result) {
6663 set_fill_boolean(set1, 1);
6664 } else {
6665 set_fill_boolean(set1, 0);
6666 }
6667
6668 return LY_SUCCESS;
6669}
6670
6671/**
6672 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6673 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6674 *
6675 * @param[in,out] set1 Set to use for the result.
6676 * @param[in] set2 Set acting as the second operand for @p op.
6677 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006678 * @return LY_ERR
6679 */
6680static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006681moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006682{
6683 LY_ERR rc;
6684
6685 /* unary '-' */
6686 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006687 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006688 LY_CHECK_RET(rc);
6689 set1->val.num *= -1;
6690 lyxp_set_free(set2);
6691 return LY_SUCCESS;
6692 }
6693
6694 assert(set1 && set2);
6695
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006696 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006697 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006698 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006699 LY_CHECK_RET(rc);
6700
6701 switch (op[0]) {
6702 /* '+' */
6703 case '+':
6704 set1->val.num += set2->val.num;
6705 break;
6706
6707 /* '-' */
6708 case '-':
6709 set1->val.num -= set2->val.num;
6710 break;
6711
6712 /* '*' */
6713 case '*':
6714 set1->val.num *= set2->val.num;
6715 break;
6716
6717 /* 'div' */
6718 case 'd':
6719 set1->val.num /= set2->val.num;
6720 break;
6721
6722 /* 'mod' */
6723 case 'm':
6724 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6725 break;
6726
6727 default:
6728 LOGINT_RET(set1->ctx);
6729 }
6730
6731 return LY_SUCCESS;
6732}
6733
6734/*
6735 * eval functions
6736 *
6737 * They execute a parsed XPath expression on some data subtree.
6738 */
6739
6740/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006741 * @brief Evaluate Predicate. Logs directly on error.
6742 *
Michal Vaskod3678892020-05-21 10:06:58 +02006743 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006744 *
6745 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006746 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006747 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6748 * @param[in] options XPath options.
6749 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6750 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6751 */
6752static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006753eval_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options, ly_bool parent_pos_pred)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006754{
6755 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006756 uint16_t orig_exp;
6757 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006758 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006759 struct lyxp_set set2;
6760 struct lyd_node *orig_parent;
6761
6762 /* '[' */
6763 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006764 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006765 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006766
6767 if (!set) {
6768only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006769 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006770 LY_CHECK_RET(rc);
6771 } else if (set->type == LYXP_SET_NODE_SET) {
6772 /* 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 +01006773 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006774
6775 /* empty set, nothing to evaluate */
6776 if (!set->used) {
6777 goto only_parse;
6778 }
6779
Michal Vasko004d3152020-06-11 19:59:22 +02006780 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006781 orig_pos = 0;
6782 orig_size = set->used;
6783 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006784 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006785 set_init(&set2, set);
6786 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6787 /* remember the node context position for position() and context size for last(),
6788 * predicates should always be evaluated with respect to the child axis (since we do
6789 * not support explicit axes) so we assign positions based on their parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006790 if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) {
6791 orig_parent = lyd_parent(set->val.nodes[i].node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006792 orig_pos = 1;
6793 } else {
6794 ++orig_pos;
6795 }
6796
6797 set2.ctx_pos = orig_pos;
6798 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006799 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006800
Michal Vasko004d3152020-06-11 19:59:22 +02006801 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006802 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006803 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006804 return rc;
6805 }
6806
6807 /* number is a position */
6808 if (set2.type == LYXP_SET_NUMBER) {
6809 if ((long long)set2.val.num == orig_pos) {
6810 set2.val.num = 1;
6811 } else {
6812 set2.val.num = 0;
6813 }
6814 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006815 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006816
6817 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006818 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006819 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006820 }
6821 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006822 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006823
6824 } else if (set->type == LYXP_SET_SCNODE_SET) {
6825 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006826 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006827 /* there is a currently-valid node */
6828 break;
6829 }
6830 }
6831 /* empty set, nothing to evaluate */
6832 if (i == set->used) {
6833 goto only_parse;
6834 }
6835
Michal Vasko004d3152020-06-11 19:59:22 +02006836 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006837
Michal Vasko03ff5a72019-09-11 13:49:33 +02006838 /* set special in_ctx to all the valid snodes */
6839 pred_in_ctx = set_scnode_new_in_ctx(set);
6840
6841 /* use the valid snodes one-by-one */
6842 for (i = 0; i < set->used; ++i) {
6843 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6844 continue;
6845 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006846 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006847
Michal Vasko004d3152020-06-11 19:59:22 +02006848 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006849
Michal Vasko004d3152020-06-11 19:59:22 +02006850 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006851 LY_CHECK_RET(rc);
6852
6853 set->val.scnodes[i].in_ctx = pred_in_ctx;
6854 }
6855
6856 /* restore the state as it was before the predicate */
6857 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006858 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02006859 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006860 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006861 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006862 }
6863 }
6864
6865 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006866 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006867 set_fill_set(&set2, set);
6868
Michal Vasko004d3152020-06-11 19:59:22 +02006869 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006870 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006871 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006872 return rc;
6873 }
6874
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006875 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006876 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006877 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006878 }
Michal Vaskod3678892020-05-21 10:06:58 +02006879 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006880 }
6881
6882 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006883 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006884 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006885 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006886 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006887
6888 return LY_SUCCESS;
6889}
6890
6891/**
Michal Vaskod3678892020-05-21 10:06:58 +02006892 * @brief Evaluate Literal. Logs directly on error.
6893 *
6894 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006895 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006896 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6897 */
6898static void
Michal Vasko40308e72020-10-20 16:38:40 +02006899eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006900{
6901 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006902 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006903 set_fill_string(set, "", 0);
6904 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006905 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 +02006906 }
6907 }
6908 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006909 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006910 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006911}
6912
6913/**
Michal Vasko004d3152020-06-11 19:59:22 +02006914 * @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 +02006915 *
Michal Vasko004d3152020-06-11 19:59:22 +02006916 * @param[in] exp Full parsed XPath expression.
6917 * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006918 * @param[in] ctx_node Found schema node as the context for the predicate.
6919 * @param[in] cur_mod Current module for the expression.
6920 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006921 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006922 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006923 * @param[out] predicates Parsed predicates.
6924 * @param[out] pred_type Type of @p predicates.
6925 * @return LY_SUCCESS on success,
6926 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006927 */
6928static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006929eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *ctx_node,
Radek Krejci8df109d2021-04-23 12:19:08 +02006930 const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_VALUE_FORMAT format, void *prefix_data,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006931 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006932{
Michal Vasko004d3152020-06-11 19:59:22 +02006933 LY_ERR ret = LY_SUCCESS;
6934 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006935 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006936 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006937 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006938 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006939
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006940 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006941
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006942 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006943 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006944 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006945 return LY_EINVAL;
6946 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006947 for (key_count = 0, key = lysc_node_child(ctx_node); key && (key->flags & LYS_KEY); key = key->next, ++key_count) {}
Michal Vasko004d3152020-06-11 19:59:22 +02006948 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006949
Michal Vasko004d3152020-06-11 19:59:22 +02006950 /* learn where the predicates end */
6951 e_idx = *tok_idx;
6952 while (key_count) {
6953 /* '[' */
6954 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6955 return LY_EINVAL;
6956 }
6957 ++e_idx;
6958
Michal Vasko3354d272021-04-06 09:40:06 +02006959 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
6960 /* definitely not a key */
6961 return LY_EINVAL;
6962 }
6963
Michal Vasko004d3152020-06-11 19:59:22 +02006964 /* ']' */
6965 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6966 ++e_idx;
6967 }
6968 ++e_idx;
6969
6970 /* another presumably key predicate parsed */
6971 --key_count;
6972 }
Michal Vasko004d3152020-06-11 19:59:22 +02006973 } else {
6974 /* learn just where this single predicate ends */
6975 e_idx = *tok_idx;
6976
Michal Vaskod3678892020-05-21 10:06:58 +02006977 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006978 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6979 return LY_EINVAL;
6980 }
6981 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006982
Michal Vasko3354d272021-04-06 09:40:06 +02006983 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
6984 /* definitely not the value */
6985 return LY_EINVAL;
6986 }
6987
Michal Vaskod3678892020-05-21 10:06:58 +02006988 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006989 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6990 ++e_idx;
6991 }
6992 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006993 }
6994
Michal Vasko004d3152020-06-11 19:59:22 +02006995 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6996 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6997
6998 /* turn logging off */
6999 prev_lo = ly_log_options(0);
7000
7001 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007002 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
7003 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007004
7005 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007006 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
7007 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02007008 LY_CHECK_GOTO(ret, cleanup);
7009
7010 /* success, the predicate must include all the needed information for hash-based search */
7011 *tok_idx = e_idx;
7012
7013cleanup:
7014 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007015 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02007016 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02007017}
7018
7019/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007020 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7021 * data node(s) could be found using a single hash-based search.
7022 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007023 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007024 * @param[in] node Next context node to check.
7025 * @param[in] name Expected node name.
7026 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007027 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007028 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007029 * @param[in] format Prefix format.
7030 * @param[in,out] found Previously found node, is updated.
7031 * @return LY_SUCCESS on success,
7032 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7033 */
7034static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007035eval_name_test_with_predicate_get_scnode(const struct ly_ctx *ctx, const struct lyd_node *node, const char *name,
Radek Krejci8df109d2021-04-23 12:19:08 +02007036 uint16_t name_len, const struct lys_module *moveto_mod, enum lyxp_node_type root_type, LY_VALUE_FORMAT format,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007037 const struct lysc_node **found)
7038{
7039 const struct lysc_node *scnode;
7040 const struct lys_module *mod;
7041 uint32_t idx = 0;
7042
Radek Krejci8df109d2021-04-23 12:19:08 +02007043 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007044
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007045continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007046 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007047 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007048 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007049 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007050 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007051 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7052 if (scnode) {
7053 /* we have found a match */
7054 break;
7055 }
7056 }
7057
Michal Vasko7d1d0912020-10-16 08:38:30 +02007058 if (!scnode) {
7059 /* all modules searched */
7060 idx = 0;
7061 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007062 } else {
7063 /* search in top-level */
7064 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7065 }
7066 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007067 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007068 /* we must adjust the module to inherit the one from the context node */
7069 moveto_mod = node->schema->module;
7070 }
7071
7072 /* search in children, do not repeat the same search */
7073 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007074 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007075
7076 /* additional context check */
7077 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7078 scnode = NULL;
7079 }
7080
7081 if (scnode) {
7082 if (*found) {
7083 /* we found a schema node with the same name but at different level, give up, too complicated
7084 * (more hash-based searches would be required, not supported) */
7085 return LY_ENOT;
7086 } else {
7087 /* remember the found schema node and continue to make sure it can be used */
7088 *found = scnode;
7089 }
7090 }
7091
7092 if (idx) {
7093 /* continue searching all the following models */
7094 goto continue_search;
7095 }
7096
7097 return LY_SUCCESS;
7098}
7099
7100/**
Michal Vaskod3678892020-05-21 10:06:58 +02007101 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7102 *
7103 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7104 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7105 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7106 *
7107 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007108 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007109 * @param[in] attr_axis Whether to search attributes or standard nodes.
7110 * @param[in] all_desc Whether to search all the descendants or children only.
7111 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7112 * @param[in] options XPath options.
7113 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7114 */
7115static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007116eval_name_test_with_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool attr_axis, ly_bool all_desc,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007117 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007118{
Michal Vaskod3678892020-05-21 10:06:58 +02007119 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007120 const char *ncname, *ncname_dict = NULL;
7121 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007122 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007123 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007124 struct ly_path_predicate *predicates = NULL;
7125 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007126 LY_ERR rc = LY_SUCCESS;
7127
7128 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007129 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007130 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007131
7132 if (!set) {
7133 goto moveto;
7134 }
7135
Michal Vasko004d3152020-06-11 19:59:22 +02007136 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7137 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007138
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007139 if ((ncname[0] == '*') && (ncname_len == 1)) {
7140 /* all nodes will match */
7141 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007142 }
7143
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007144 /* parse (and skip) module name */
7145 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007146 LY_CHECK_GOTO(rc, cleanup);
7147
Radek Krejci8df109d2021-04-23 12:19:08 +02007148 if (((set->format == LY_VALUE_JSON) || moveto_mod) && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007149 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007150 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007151 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7152 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007153 /* check failed */
7154 scnode = NULL;
7155 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007156 }
7157 }
7158
Michal Vasko004d3152020-06-11 19:59:22 +02007159 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7160 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007161 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7162 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007163 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007164 scnode = NULL;
7165 }
7166 }
7167 }
7168
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007169 if (!scnode) {
7170 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007171 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007172 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007173 }
7174
7175moveto:
7176 /* move to the attribute(s), data node(s), or schema node(s) */
7177 if (attr_axis) {
7178 if (set && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007179 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007180 } else {
7181 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007182 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007183 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007184 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007185 }
7186 LY_CHECK_GOTO(rc, cleanup);
7187 }
7188 } else {
7189 if (set && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007190 int64_t i;
7191
Michal Vaskod3678892020-05-21 10:06:58 +02007192 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007193 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007194 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007195 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007196 }
7197 LY_CHECK_GOTO(rc, cleanup);
7198
7199 for (i = set->used - 1; i > -1; --i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007200 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vaskod3678892020-05-21 10:06:58 +02007201 break;
7202 }
7203 }
7204 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007205 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vasko90f12dc2020-12-03 14:20:42 +01007206 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".",
7207 ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007208 free(path);
7209 }
7210 } else {
7211 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007212 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007213 } else {
7214 if (scnode) {
7215 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007216 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007217 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007218 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007219 }
7220 }
7221 LY_CHECK_GOTO(rc, cleanup);
7222 }
7223 }
7224
7225 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007226 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7227 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007228 LY_CHECK_RET(rc);
7229 }
7230
7231cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007232 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007233 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007234 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007235 }
Michal Vaskod3678892020-05-21 10:06:58 +02007236 return rc;
7237}
7238
7239/**
7240 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7241 *
7242 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7243 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7244 * [8] NodeType ::= 'text' | 'node'
7245 *
7246 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007247 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007248 * @param[in] attr_axis Whether to search attributes or standard nodes.
7249 * @param[in] all_desc Whether to search all the descendants or children only.
7250 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7251 * @param[in] options XPath options.
7252 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7253 */
7254static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007255eval_node_type_with_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool attr_axis, ly_bool all_desc,
Radek Krejci1deb5be2020-08-26 16:43:36 +02007256 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007257{
7258 LY_ERR rc;
7259
7260 /* TODO */
7261 (void)attr_axis;
7262 (void)all_desc;
7263
7264 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007265 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007266 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007267 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007268 /* just for the debug message below */
7269 set = NULL;
7270 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007271 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007272 rc = xpath_node(NULL, 0, set, options);
7273 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007274 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007275 rc = xpath_text(NULL, 0, set, options);
7276 }
7277 LY_CHECK_RET(rc);
7278 }
7279 }
7280 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007281 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007282 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007283
7284 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007285 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007286 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007287 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007288 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007289
7290 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007291 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007292 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007293 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007294 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007295
7296 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007297 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7298 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007299 LY_CHECK_RET(rc);
7300 }
7301
7302 return LY_SUCCESS;
7303}
7304
7305/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007306 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7307 *
7308 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7309 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007310 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007311 *
7312 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007313 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007314 * @param[in] all_desc Whether to search all the descendants or children only.
7315 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7316 * @param[in] options XPath options.
7317 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7318 */
7319static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007320eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7321 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007322{
Radek Krejci857189e2020-09-01 13:26:36 +02007323 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007324 LY_ERR rc;
7325
7326 goto step;
7327 do {
7328 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007329 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007330 all_desc = 0;
7331 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007332 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007333 all_desc = 1;
7334 }
7335 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007336 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007337 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007338
7339step:
Michal Vaskod3678892020-05-21 10:06:58 +02007340 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007341 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007342 attr_axis = 1;
7343
7344 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007345 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007346 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007347 } else {
7348 attr_axis = 0;
7349 }
7350
Michal Vasko03ff5a72019-09-11 13:49:33 +02007351 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007352 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007353 case LYXP_TOKEN_DOT:
7354 /* evaluate '.' */
7355 if (set && (options & LYXP_SCNODE_ALL)) {
7356 rc = moveto_scnode_self(set, all_desc, options);
7357 } else {
7358 rc = moveto_self(set, all_desc, options);
7359 }
7360 LY_CHECK_RET(rc);
7361 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007362 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007363 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007364 break;
7365
7366 case LYXP_TOKEN_DDOT:
7367 /* evaluate '..' */
7368 if (set && (options & LYXP_SCNODE_ALL)) {
7369 rc = moveto_scnode_parent(set, all_desc, options);
7370 } else {
7371 rc = moveto_parent(set, all_desc, options);
7372 }
7373 LY_CHECK_RET(rc);
7374 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007375 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007376 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007377 break;
7378
Michal Vasko03ff5a72019-09-11 13:49:33 +02007379 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007380 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007381 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007382 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007383 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007384
Michal Vaskod3678892020-05-21 10:06:58 +02007385 case LYXP_TOKEN_NODETYPE:
7386 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007387 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007388 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007389 break;
7390
7391 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007392 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007393 }
Michal Vasko004d3152020-06-11 19:59:22 +02007394 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007395
7396 return LY_SUCCESS;
7397}
7398
7399/**
7400 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7401 *
7402 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7403 *
7404 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007405 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007406 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7407 * @param[in] options XPath options.
7408 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7409 */
7410static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007411eval_absolute_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007412{
Radek Krejci857189e2020-09-01 13:26:36 +02007413 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007414
7415 if (set) {
7416 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007417 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007418 }
7419
7420 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007421 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007422 /* evaluate '/' - deferred */
7423 all_desc = 0;
7424 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007425 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007426 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007427
Michal Vasko004d3152020-06-11 19:59:22 +02007428 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007429 return LY_SUCCESS;
7430 }
Michal Vasko004d3152020-06-11 19:59:22 +02007431 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007432 case LYXP_TOKEN_DOT:
7433 case LYXP_TOKEN_DDOT:
7434 case LYXP_TOKEN_AT:
7435 case LYXP_TOKEN_NAMETEST:
7436 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007437 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007438 break;
7439 default:
7440 break;
7441 }
7442
Michal Vasko03ff5a72019-09-11 13:49:33 +02007443 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007444 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007445 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7446 all_desc = 1;
7447 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007448 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007449 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007450
Michal Vaskob0099a92020-08-31 14:55:23 +02007451 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007452 }
7453
7454 return LY_SUCCESS;
7455}
7456
7457/**
7458 * @brief Evaluate FunctionCall. Logs directly on error.
7459 *
Michal Vaskod3678892020-05-21 10:06:58 +02007460 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007461 *
7462 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007463 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007464 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7465 * @param[in] options XPath options.
7466 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7467 */
7468static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007469eval_function_call(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007470{
7471 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007472
Radek Krejci1deb5be2020-08-26 16:43:36 +02007473 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007474 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007475 struct lyxp_set **args = NULL, **args_aux;
7476
7477 if (set) {
7478 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007479 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007480 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007481 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007482 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007483 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007484 xpath_func = &xpath_sum;
7485 }
7486 break;
7487 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007488 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007489 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007490 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007491 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007492 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007493 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007494 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007495 xpath_func = &xpath_true;
7496 }
7497 break;
7498 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007499 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007500 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007501 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007502 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007503 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007504 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007505 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007506 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007507 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007508 xpath_func = &xpath_deref;
7509 }
7510 break;
7511 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007512 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007513 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007514 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007515 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007516 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007517 xpath_func = &xpath_string;
7518 }
7519 break;
7520 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007521 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007522 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007523 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007524 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007525 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007526 xpath_func = &xpath_current;
7527 }
7528 break;
7529 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007530 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007531 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007532 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007533 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007534 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007535 xpath_func = &xpath_re_match;
7536 }
7537 break;
7538 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007539 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007540 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007541 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007542 xpath_func = &xpath_translate;
7543 }
7544 break;
7545 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007546 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007547 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007548 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007549 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007550 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007551 xpath_func = &xpath_bit_is_set;
7552 }
7553 break;
7554 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007555 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007556 xpath_func = &xpath_starts_with;
7557 }
7558 break;
7559 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007560 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007561 xpath_func = &xpath_derived_from;
7562 }
7563 break;
7564 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007565 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007566 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007567 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007568 xpath_func = &xpath_string_length;
7569 }
7570 break;
7571 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007572 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007573 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007574 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007575 xpath_func = &xpath_substring_after;
7576 }
7577 break;
7578 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007579 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007580 xpath_func = &xpath_substring_before;
7581 }
7582 break;
7583 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007584 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007585 xpath_func = &xpath_derived_from_or_self;
7586 }
7587 break;
7588 }
7589
7590 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007591 LOGVAL(set->ctx, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007592 return LY_EVALID;
7593 }
7594 }
7595
7596 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007597 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007598 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007599
7600 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007601 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007602 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007603 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007604 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007605
7606 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007607 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007608 if (set) {
7609 args = malloc(sizeof *args);
7610 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7611 arg_count = 1;
7612 args[0] = set_copy(set);
7613 if (!args[0]) {
7614 rc = LY_EMEM;
7615 goto cleanup;
7616 }
7617
Michal Vasko004d3152020-06-11 19:59:22 +02007618 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007619 LY_CHECK_GOTO(rc, cleanup);
7620 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007621 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007622 LY_CHECK_GOTO(rc, cleanup);
7623 }
7624 }
Michal Vasko004d3152020-06-11 19:59:22 +02007625 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007626 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007627 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007628 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007629
7630 if (set) {
7631 ++arg_count;
7632 args_aux = realloc(args, arg_count * sizeof *args);
7633 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7634 args = args_aux;
7635 args[arg_count - 1] = set_copy(set);
7636 if (!args[arg_count - 1]) {
7637 rc = LY_EMEM;
7638 goto cleanup;
7639 }
7640
Michal Vasko004d3152020-06-11 19:59:22 +02007641 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007642 LY_CHECK_GOTO(rc, cleanup);
7643 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007644 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007645 LY_CHECK_GOTO(rc, cleanup);
7646 }
7647 }
7648
7649 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007650 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007651 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007652 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007653 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007654
7655 if (set) {
7656 /* evaluate function */
7657 rc = xpath_func(args, arg_count, set, options);
7658
7659 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007660 /* merge all nodes from arg evaluations */
7661 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007662 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007663 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007664 }
7665 }
7666 } else {
7667 rc = LY_SUCCESS;
7668 }
7669
7670cleanup:
7671 for (i = 0; i < arg_count; ++i) {
7672 lyxp_set_free(args[i]);
7673 }
7674 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007675 return rc;
7676}
7677
7678/**
7679 * @brief Evaluate Number. Logs directly on error.
7680 *
7681 * @param[in] ctx Context for errors.
7682 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007683 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007684 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7685 * @return LY_ERR
7686 */
7687static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007688eval_number(struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007689{
7690 long double num;
7691 char *endptr;
7692
7693 if (set) {
7694 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007695 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007696 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007697 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7698 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007699 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007700 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007701 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007702 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7703 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007704 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007705 return LY_EVALID;
7706 }
7707
7708 set_fill_number(set, num);
7709 }
7710
7711 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007712 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007713 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007714 return LY_SUCCESS;
7715}
7716
7717/**
7718 * @brief Evaluate PathExpr. Logs directly on error.
7719 *
Michal Vaskod3678892020-05-21 10:06:58 +02007720 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007721 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7722 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7723 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007724 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007725 *
7726 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007727 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007728 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7729 * @param[in] options XPath options.
7730 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7731 */
7732static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007733eval_path_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007734{
Radek Krejci857189e2020-09-01 13:26:36 +02007735 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007736 LY_ERR rc;
7737
Michal Vasko004d3152020-06-11 19:59:22 +02007738 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007739 case LYXP_TOKEN_PAR1:
7740 /* '(' Expr ')' */
7741
7742 /* '(' */
7743 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007744 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007745 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007746
7747 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007748 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007749 LY_CHECK_RET(rc);
7750
7751 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007752 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007753 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007754 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007755 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007756
7757 parent_pos_pred = 0;
7758 goto predicate;
7759
7760 case LYXP_TOKEN_DOT:
7761 case LYXP_TOKEN_DDOT:
7762 case LYXP_TOKEN_AT:
7763 case LYXP_TOKEN_NAMETEST:
7764 case LYXP_TOKEN_NODETYPE:
7765 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007766 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007767 LY_CHECK_RET(rc);
7768 break;
7769
7770 case LYXP_TOKEN_FUNCNAME:
7771 /* FunctionCall */
7772 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007773 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007774 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007775 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007776 }
7777 LY_CHECK_RET(rc);
7778
7779 parent_pos_pred = 1;
7780 goto predicate;
7781
Michal Vasko3e48bf32020-06-01 08:39:07 +02007782 case LYXP_TOKEN_OPER_PATH:
7783 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007784 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007785 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007786 LY_CHECK_RET(rc);
7787 break;
7788
7789 case LYXP_TOKEN_LITERAL:
7790 /* Literal */
7791 if (!set || (options & LYXP_SCNODE_ALL)) {
7792 if (set) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007793 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007794 }
Michal Vasko004d3152020-06-11 19:59:22 +02007795 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007796 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007797 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007798 }
7799
7800 parent_pos_pred = 1;
7801 goto predicate;
7802
7803 case LYXP_TOKEN_NUMBER:
7804 /* Number */
7805 if (!set || (options & LYXP_SCNODE_ALL)) {
7806 if (set) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007807 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007808 }
Michal Vasko004d3152020-06-11 19:59:22 +02007809 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007810 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007811 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007812 }
7813 LY_CHECK_RET(rc);
7814
7815 parent_pos_pred = 1;
7816 goto predicate;
7817
7818 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01007819 LOGVAL(set->ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007820 return LY_EVALID;
7821 }
7822
7823 return LY_SUCCESS;
7824
7825predicate:
7826 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007827 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7828 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007829 LY_CHECK_RET(rc);
7830 }
7831
7832 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007833 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007834
7835 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007836 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007837 all_desc = 0;
7838 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007839 all_desc = 1;
7840 }
7841
7842 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007843 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007844 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007845
Michal Vasko004d3152020-06-11 19:59:22 +02007846 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007847 LY_CHECK_RET(rc);
7848 }
7849
7850 return LY_SUCCESS;
7851}
7852
7853/**
7854 * @brief Evaluate UnionExpr. Logs directly on error.
7855 *
Michal Vaskod3678892020-05-21 10:06:58 +02007856 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007857 *
7858 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007859 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007860 * @param[in] repeat How many times this expression is repeated.
7861 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7862 * @param[in] options XPath options.
7863 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7864 */
7865static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007866eval_union_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007867{
7868 LY_ERR rc = LY_SUCCESS;
7869 struct lyxp_set orig_set, set2;
7870 uint16_t i;
7871
7872 assert(repeat);
7873
7874 set_init(&orig_set, set);
7875 set_init(&set2, set);
7876
7877 set_fill_set(&orig_set, set);
7878
Michal Vasko004d3152020-06-11 19:59:22 +02007879 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007880 LY_CHECK_GOTO(rc, cleanup);
7881
7882 /* ('|' PathExpr)* */
7883 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007884 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007885 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007886 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007887 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007888
7889 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007890 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007891 LY_CHECK_GOTO(rc, cleanup);
7892 continue;
7893 }
7894
7895 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007896 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007897 LY_CHECK_GOTO(rc, cleanup);
7898
7899 /* eval */
7900 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007901 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007902 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007903 rc = moveto_union(set, &set2);
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 UnaryExpr. Logs directly on error.
7916 *
Michal Vaskod3678892020-05-21 10:06:58 +02007917 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007918 *
7919 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007920 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007921 * @param[in] repeat How many times this expression is repeated.
7922 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7923 * @param[in] options XPath options.
7924 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7925 */
7926static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007927eval_unary_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007928{
7929 LY_ERR rc;
7930 uint16_t this_op, i;
7931
7932 assert(repeat);
7933
7934 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007935 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007936 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007937 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 +02007938
7939 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007940 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007941 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007942 }
7943
Michal Vasko004d3152020-06-11 19:59:22 +02007944 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007945 LY_CHECK_RET(rc);
7946
7947 if (set && (repeat % 2)) {
7948 if (options & LYXP_SCNODE_ALL) {
7949 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7950 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007951 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007952 LY_CHECK_RET(rc);
7953 }
7954 }
7955
7956 return LY_SUCCESS;
7957}
7958
7959/**
7960 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7961 *
Michal Vaskod3678892020-05-21 10:06:58 +02007962 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007963 * | MultiplicativeExpr '*' UnaryExpr
7964 * | MultiplicativeExpr 'div' UnaryExpr
7965 * | MultiplicativeExpr 'mod' UnaryExpr
7966 *
7967 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007968 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007969 * @param[in] repeat How many times this expression is repeated.
7970 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7971 * @param[in] options XPath options.
7972 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7973 */
7974static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007975eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
7976 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007977{
7978 LY_ERR rc;
7979 uint16_t this_op;
7980 struct lyxp_set orig_set, set2;
7981 uint16_t i;
7982
7983 assert(repeat);
7984
7985 set_init(&orig_set, set);
7986 set_init(&set2, set);
7987
7988 set_fill_set(&orig_set, set);
7989
Michal Vasko004d3152020-06-11 19:59:22 +02007990 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007991 LY_CHECK_GOTO(rc, cleanup);
7992
7993 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7994 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007995 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007996
Michal Vasko004d3152020-06-11 19:59:22 +02007997 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007998 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007999 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008000 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008001
8002 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008003 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008004 LY_CHECK_GOTO(rc, cleanup);
8005 continue;
8006 }
8007
8008 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008009 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008010 LY_CHECK_GOTO(rc, cleanup);
8011
8012 /* eval */
8013 if (options & LYXP_SCNODE_ALL) {
8014 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008015 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008016 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008017 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008018 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008019 LY_CHECK_GOTO(rc, cleanup);
8020 }
8021 }
8022
8023cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008024 lyxp_set_free_content(&orig_set);
8025 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008026 return rc;
8027}
8028
8029/**
8030 * @brief Evaluate AdditiveExpr. Logs directly on error.
8031 *
Michal Vaskod3678892020-05-21 10:06:58 +02008032 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008033 * | AdditiveExpr '+' MultiplicativeExpr
8034 * | AdditiveExpr '-' MultiplicativeExpr
8035 *
8036 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008037 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008038 * @param[in] repeat How many times this expression is repeated.
8039 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8040 * @param[in] options XPath options.
8041 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8042 */
8043static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008044eval_additive_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008045{
8046 LY_ERR rc;
8047 uint16_t this_op;
8048 struct lyxp_set orig_set, set2;
8049 uint16_t i;
8050
8051 assert(repeat);
8052
8053 set_init(&orig_set, set);
8054 set_init(&set2, set);
8055
8056 set_fill_set(&orig_set, set);
8057
Michal Vasko004d3152020-06-11 19:59:22 +02008058 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008059 LY_CHECK_GOTO(rc, cleanup);
8060
8061 /* ('+' / '-' MultiplicativeExpr)* */
8062 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008063 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008064
Michal Vasko004d3152020-06-11 19:59:22 +02008065 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008066 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008067 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008068 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008069
8070 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008071 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008072 LY_CHECK_GOTO(rc, cleanup);
8073 continue;
8074 }
8075
8076 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008077 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008078 LY_CHECK_GOTO(rc, cleanup);
8079
8080 /* eval */
8081 if (options & LYXP_SCNODE_ALL) {
8082 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008083 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008084 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008085 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008086 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008087 LY_CHECK_GOTO(rc, cleanup);
8088 }
8089 }
8090
8091cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008092 lyxp_set_free_content(&orig_set);
8093 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008094 return rc;
8095}
8096
8097/**
8098 * @brief Evaluate RelationalExpr. Logs directly on error.
8099 *
Michal Vaskod3678892020-05-21 10:06:58 +02008100 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008101 * | RelationalExpr '<' AdditiveExpr
8102 * | RelationalExpr '>' AdditiveExpr
8103 * | RelationalExpr '<=' AdditiveExpr
8104 * | RelationalExpr '>=' AdditiveExpr
8105 *
8106 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008107 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008108 * @param[in] repeat How many times this expression is repeated.
8109 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8110 * @param[in] options XPath options.
8111 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8112 */
8113static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008114eval_relational_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008115{
8116 LY_ERR rc;
8117 uint16_t this_op;
8118 struct lyxp_set orig_set, set2;
8119 uint16_t i;
8120
8121 assert(repeat);
8122
8123 set_init(&orig_set, set);
8124 set_init(&set2, set);
8125
8126 set_fill_set(&orig_set, set);
8127
Michal Vasko004d3152020-06-11 19:59:22 +02008128 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008129 LY_CHECK_GOTO(rc, cleanup);
8130
8131 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8132 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008133 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008134
Michal Vasko004d3152020-06-11 19:59:22 +02008135 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008136 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008137 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008138 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008139
8140 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008141 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008142 LY_CHECK_GOTO(rc, cleanup);
8143 continue;
8144 }
8145
8146 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008147 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008148 LY_CHECK_GOTO(rc, cleanup);
8149
8150 /* eval */
8151 if (options & LYXP_SCNODE_ALL) {
8152 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008153 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008154 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008155 } else {
8156 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8157 LY_CHECK_GOTO(rc, cleanup);
8158 }
8159 }
8160
8161cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008162 lyxp_set_free_content(&orig_set);
8163 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008164 return rc;
8165}
8166
8167/**
8168 * @brief Evaluate EqualityExpr. Logs directly on error.
8169 *
Michal Vaskod3678892020-05-21 10:06:58 +02008170 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008171 * | EqualityExpr '!=' RelationalExpr
8172 *
8173 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008174 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008175 * @param[in] repeat How many times this expression is repeated.
8176 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8177 * @param[in] options XPath options.
8178 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8179 */
8180static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008181eval_equality_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008182{
8183 LY_ERR rc;
8184 uint16_t this_op;
8185 struct lyxp_set orig_set, set2;
8186 uint16_t i;
8187
8188 assert(repeat);
8189
8190 set_init(&orig_set, set);
8191 set_init(&set2, set);
8192
8193 set_fill_set(&orig_set, set);
8194
Michal Vasko004d3152020-06-11 19:59:22 +02008195 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008196 LY_CHECK_GOTO(rc, cleanup);
8197
8198 /* ('=' / '!=' RelationalExpr)* */
8199 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008200 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008201
Michal Vasko004d3152020-06-11 19:59:22 +02008202 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008203 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008204 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008205 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008206
8207 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008208 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008209 LY_CHECK_GOTO(rc, cleanup);
8210 continue;
8211 }
8212
8213 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008214 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008215 LY_CHECK_GOTO(rc, cleanup);
8216
8217 /* eval */
8218 if (options & LYXP_SCNODE_ALL) {
8219 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008220 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8221 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008222 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008223 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008224 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008225 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8226 LY_CHECK_GOTO(rc, cleanup);
8227 }
8228 }
8229
8230cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008231 lyxp_set_free_content(&orig_set);
8232 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008233 return rc;
8234}
8235
8236/**
8237 * @brief Evaluate AndExpr. Logs directly on error.
8238 *
Michal Vaskod3678892020-05-21 10:06:58 +02008239 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008240 *
8241 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008242 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008243 * @param[in] repeat How many times this expression is repeated.
8244 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8245 * @param[in] options XPath options.
8246 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8247 */
8248static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008249eval_and_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008250{
8251 LY_ERR rc;
8252 struct lyxp_set orig_set, set2;
8253 uint16_t i;
8254
8255 assert(repeat);
8256
8257 set_init(&orig_set, set);
8258 set_init(&set2, set);
8259
8260 set_fill_set(&orig_set, set);
8261
Michal Vasko004d3152020-06-11 19:59:22 +02008262 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008263 LY_CHECK_GOTO(rc, cleanup);
8264
8265 /* cast to boolean, we know that will be the final result */
8266 if (set && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008267 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008268 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008269 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008270 }
8271
8272 /* ('and' EqualityExpr)* */
8273 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008274 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8275 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008276 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008277 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008278
8279 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008280 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8281 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008282 LY_CHECK_GOTO(rc, cleanup);
8283 continue;
8284 }
8285
8286 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008287 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008288 LY_CHECK_GOTO(rc, cleanup);
8289
8290 /* eval - just get boolean value actually */
8291 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008292 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008293 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008294 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008295 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008296 set_fill_set(set, &set2);
8297 }
8298 }
8299
8300cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008301 lyxp_set_free_content(&orig_set);
8302 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008303 return rc;
8304}
8305
8306/**
8307 * @brief Evaluate OrExpr. Logs directly on error.
8308 *
Michal Vaskod3678892020-05-21 10:06:58 +02008309 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008310 *
8311 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008312 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008313 * @param[in] repeat How many times this expression is repeated.
8314 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8315 * @param[in] options XPath options.
8316 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8317 */
8318static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008319eval_or_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008320{
8321 LY_ERR rc;
8322 struct lyxp_set orig_set, set2;
8323 uint16_t i;
8324
8325 assert(repeat);
8326
8327 set_init(&orig_set, set);
8328 set_init(&set2, set);
8329
8330 set_fill_set(&orig_set, set);
8331
Michal Vasko004d3152020-06-11 19:59:22 +02008332 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008333 LY_CHECK_GOTO(rc, cleanup);
8334
8335 /* cast to boolean, we know that will be the final result */
8336 if (set && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008337 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008338 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008339 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008340 }
8341
8342 /* ('or' AndExpr)* */
8343 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008344 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8345 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008346 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008347 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008348
8349 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008350 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8351 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008352 LY_CHECK_GOTO(rc, cleanup);
8353 continue;
8354 }
8355
8356 set_fill_set(&set2, &orig_set);
8357 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8358 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008359 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008360 LY_CHECK_GOTO(rc, cleanup);
8361
8362 /* eval - just get boolean value actually */
8363 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008364 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008365 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008366 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008367 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008368 set_fill_set(set, &set2);
8369 }
8370 }
8371
8372cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008373 lyxp_set_free_content(&orig_set);
8374 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008375 return rc;
8376}
8377
8378/**
Michal Vasko004d3152020-06-11 19:59:22 +02008379 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008380 *
8381 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008382 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008383 * @param[in] etype Expression type to evaluate.
8384 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8385 * @param[in] options XPath options.
8386 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8387 */
8388static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008389eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8390 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008391{
8392 uint16_t i, count;
8393 enum lyxp_expr_type next_etype;
8394 LY_ERR rc;
8395
8396 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008397 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008398 next_etype = LYXP_EXPR_NONE;
8399 } else {
8400 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008401 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008402
8403 /* select one-priority lower because etype expression called us */
8404 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008405 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008406 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008407 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008408 } else {
8409 next_etype = LYXP_EXPR_NONE;
8410 }
8411 }
8412
8413 /* decide what expression are we parsing based on the repeat */
8414 switch (next_etype) {
8415 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008416 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008417 break;
8418 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008419 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008420 break;
8421 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008422 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008423 break;
8424 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008425 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008426 break;
8427 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008428 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008429 break;
8430 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008431 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008432 break;
8433 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008434 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008435 break;
8436 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008437 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008438 break;
8439 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008440 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008441 break;
8442 default:
8443 LOGINT_RET(set->ctx);
8444 }
8445
8446 return rc;
8447}
8448
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008449/**
8450 * @brief Get root type.
8451 *
8452 * @param[in] ctx_node Context node.
8453 * @param[in] ctx_scnode Schema context node.
8454 * @param[in] options XPath options.
8455 * @return Root type.
8456 */
8457static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008458lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, uint32_t options)
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008459{
Michal Vasko6b26e742020-07-17 15:02:10 +02008460 const struct lysc_node *op;
8461
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008462 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008463 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008464 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008465
8466 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008467 /* general root that can access everything */
8468 return LYXP_NODE_ROOT;
8469 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8470 /* root context node can access only config data (because we said so, it is unspecified) */
8471 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008472 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008473 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008474 }
8475
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008476 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008477 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008478 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008479
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008480 if (op || !(options & LYXP_SCHEMA)) {
8481 /* general root that can access everything */
8482 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05008483 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008484 /* root context node can access only config data (because we said so, it is unspecified) */
8485 return LYXP_NODE_ROOT_CONFIG;
8486 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008487 return LYXP_NODE_ROOT;
8488}
8489
Michal Vasko03ff5a72019-09-11 13:49:33 +02008490LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008491lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008492 LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node, const struct lyd_node *tree,
Michal Vasko400e9672021-01-11 13:39:17 +01008493 struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008494{
Michal Vasko004d3152020-06-11 19:59:22 +02008495 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008496 LY_ERR rc;
8497
Michal Vasko400e9672021-01-11 13:39:17 +01008498 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008499 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008500 LOGARG(NULL, "Current module must be set if schema format is used.");
8501 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008502 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008503
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008504 if (tree) {
8505 /* adjust the pointer to be the first top-level sibling */
8506 while (tree->parent) {
8507 tree = lyd_parent(tree);
8508 }
8509 tree = lyd_first_sibling(tree);
8510 }
8511
Michal Vasko03ff5a72019-09-11 13:49:33 +02008512 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008513 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008514 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008515 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8516 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8517
Michal Vasko400e9672021-01-11 13:39:17 +01008518 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008519 set->cur_node = ctx_node;
8520 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008521 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8522 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008523 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008524 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008525 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008526 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008527
Radek Krejciddace2c2021-01-08 11:30:56 +01008528 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008529
Michal Vasko03ff5a72019-09-11 13:49:33 +02008530 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008531 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008532 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008533 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008534 }
8535
Radek Krejciddace2c2021-01-08 11:30:56 +01008536 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008537 return rc;
8538}
8539
8540#if 0
8541
8542/* full xml printing of set elements, not used currently */
8543
8544void
8545lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8546{
8547 uint32_t i;
8548 char *str_num;
8549 struct lyout out;
8550
8551 memset(&out, 0, sizeof out);
8552
8553 out.type = LYOUT_STREAM;
8554 out.method.f = f;
8555
8556 switch (set->type) {
8557 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008558 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008559 break;
8560 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008561 ly_print_(&out, "Boolean XPath set:\n");
8562 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008563 break;
8564 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008565 ly_print_(&out, "String XPath set:\n");
8566 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008567 break;
8568 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008569 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008570
8571 if (isnan(set->value.num)) {
8572 str_num = strdup("NaN");
8573 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8574 str_num = strdup("0");
8575 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8576 str_num = strdup("Infinity");
8577 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8578 str_num = strdup("-Infinity");
8579 } else if ((long long)set->value.num == set->value.num) {
8580 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8581 str_num = NULL;
8582 }
8583 } else {
8584 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8585 str_num = NULL;
8586 }
8587 }
8588 if (!str_num) {
8589 LOGMEM;
8590 return;
8591 }
Michal Vasko5233e962020-08-14 14:26:20 +02008592 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008593 free(str_num);
8594 break;
8595 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008596 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008597
8598 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008599 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008600 switch (set->node_type[i]) {
8601 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008602 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008603 break;
8604 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008605 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008606 break;
8607 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008608 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008609 break;
8610 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008611 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008612 break;
8613 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008614 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008615 break;
8616 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008617 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008618 break;
8619 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008620 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008621 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008622 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008623 break;
8624 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008625 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 +02008626 break;
8627 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008628 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 +02008629 break;
8630 }
8631 }
8632 break;
8633 }
8634}
8635
8636#endif
8637
8638LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008639lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008640{
8641 long double num;
8642 char *str;
8643 LY_ERR rc;
8644
8645 if (!set || (set->type == target)) {
8646 return LY_SUCCESS;
8647 }
8648
8649 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008650 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008651
8652 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008653 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008654 return LY_EINVAL;
8655 }
8656
8657 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008658 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008659 switch (set->type) {
8660 case LYXP_SET_NUMBER:
8661 if (isnan(set->val.num)) {
8662 set->val.str = strdup("NaN");
8663 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8664 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8665 set->val.str = strdup("0");
8666 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8667 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8668 set->val.str = strdup("Infinity");
8669 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8670 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8671 set->val.str = strdup("-Infinity");
8672 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8673 } else if ((long long)set->val.num == set->val.num) {
8674 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8675 LOGMEM_RET(set->ctx);
8676 }
8677 set->val.str = str;
8678 } else {
8679 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8680 LOGMEM_RET(set->ctx);
8681 }
8682 set->val.str = str;
8683 }
8684 break;
8685 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008686 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008687 set->val.str = strdup("true");
8688 } else {
8689 set->val.str = strdup("false");
8690 }
8691 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8692 break;
8693 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008694 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008695 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008696
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008697 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008698 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008699 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008700 set->val.str = str;
8701 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008702 default:
8703 LOGINT_RET(set->ctx);
8704 }
8705 set->type = LYXP_SET_STRING;
8706 }
8707
8708 /* to NUMBER */
8709 if (target == LYXP_SET_NUMBER) {
8710 switch (set->type) {
8711 case LYXP_SET_STRING:
8712 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008713 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008714 set->val.num = num;
8715 break;
8716 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008717 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008718 set->val.num = 1;
8719 } else {
8720 set->val.num = 0;
8721 }
8722 break;
8723 default:
8724 LOGINT_RET(set->ctx);
8725 }
8726 set->type = LYXP_SET_NUMBER;
8727 }
8728
8729 /* to BOOLEAN */
8730 if (target == LYXP_SET_BOOLEAN) {
8731 switch (set->type) {
8732 case LYXP_SET_NUMBER:
8733 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008734 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008735 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008736 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008737 }
8738 break;
8739 case LYXP_SET_STRING:
8740 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008741 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008742 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008743 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008744 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008745 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008746 }
8747 break;
8748 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008749 if (set->used) {
8750 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008751 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008752 } else {
8753 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008754 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008755 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008756 break;
8757 default:
8758 LOGINT_RET(set->ctx);
8759 }
8760 set->type = LYXP_SET_BOOLEAN;
8761 }
8762
Michal Vasko03ff5a72019-09-11 13:49:33 +02008763 return LY_SUCCESS;
8764}
8765
8766LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008767lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008768 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
Michal Vasko400e9672021-01-11 13:39:17 +01008769 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008770{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008771 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008772 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008773
Michal Vasko400e9672021-01-11 13:39:17 +01008774 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008775 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008776 LOGARG(NULL, "Current module must be set if schema format is used.");
8777 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008778 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008779
8780 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008781 memset(set, 0, sizeof *set);
8782 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008783 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8784 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode ? LYXP_NODE_ELEM : set->root_type, NULL));
Radek Krejcif13b87b2020-12-01 22:02:17 +01008785 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008786
Michal Vasko400e9672021-01-11 13:39:17 +01008787 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008788 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008789 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008790 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8791 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008792 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008793 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008794 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008795
Radek Krejciddace2c2021-01-08 11:30:56 +01008796 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008797
Michal Vasko03ff5a72019-09-11 13:49:33 +02008798 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01008799 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
8800
Radek Krejciddace2c2021-01-08 11:30:56 +01008801 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008802 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008803}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008804
8805API const char *
8806lyxp_get_expr(const struct lyxp_expr *path)
8807{
8808 if (!path) {
8809 return NULL;
8810 }
8811
8812 return path->expr;
8813}