blob: 7f5a0d37e6ba271f650cc47a9d3eb478cd37a454 [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 */
Radek Krejcib1646a92018-11-02 16:08:26 +010015
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010017
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010019#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010023#include <stdio.h>
24#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010025#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010026
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020028#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010032#include "out.h"
Radek Krejci7931b192020-06-25 17:05:03 +020033#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020034#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020035#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020037#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "tree.h"
Radek Krejci77114102021-03-10 15:21:57 +010039#include "tree_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020040#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010041#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020042#include "tree_schema_internal.h"
43#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020044
aPiecekbf968d92021-05-27 14:35:05 +020045static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth);
Michal Vasko40308e72020-10-20 16:38:40 +020046static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype,
47 struct lyxp_set *set, uint32_t options);
Michal Vasko93923692021-05-07 15:28:02 +020048static LY_ERR moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
49 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod);
Michal Vasko03ff5a72019-09-11 13:49:33 +020050
51/**
52 * @brief Print the type of an XPath \p set.
53 *
54 * @param[in] set Set to use.
55 * @return Set type string.
56 */
57static const char *
58print_set_type(struct lyxp_set *set)
59{
60 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020061 case LYXP_SET_NODE_SET:
62 return "node set";
63 case LYXP_SET_SCNODE_SET:
64 return "schema node set";
65 case LYXP_SET_BOOLEAN:
66 return "boolean";
67 case LYXP_SET_NUMBER:
68 return "number";
69 case LYXP_SET_STRING:
70 return "string";
71 }
72
73 return NULL;
74}
75
Michal Vasko24cddf82020-06-01 08:17:01 +020076const char *
77lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020078{
79 switch (tok) {
80 case LYXP_TOKEN_PAR1:
81 return "(";
82 case LYXP_TOKEN_PAR2:
83 return ")";
84 case LYXP_TOKEN_BRACK1:
85 return "[";
86 case LYXP_TOKEN_BRACK2:
87 return "]";
88 case LYXP_TOKEN_DOT:
89 return ".";
90 case LYXP_TOKEN_DDOT:
91 return "..";
92 case LYXP_TOKEN_AT:
93 return "@";
94 case LYXP_TOKEN_COMMA:
95 return ",";
96 case LYXP_TOKEN_NAMETEST:
97 return "NameTest";
98 case LYXP_TOKEN_NODETYPE:
99 return "NodeType";
100 case LYXP_TOKEN_FUNCNAME:
101 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200102 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200103 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200104 case LYXP_TOKEN_OPER_EQUAL:
105 return "Operator(Equal)";
106 case LYXP_TOKEN_OPER_NEQUAL:
107 return "Operator(Non-equal)";
108 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200109 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200110 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200111 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200112 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200113 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200114 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200115 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200116 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200117 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200118 case LYXP_TOKEN_LITERAL:
119 return "Literal";
120 case LYXP_TOKEN_NUMBER:
121 return "Number";
122 default:
123 LOGINT(NULL);
124 return "";
125 }
126}
127
128/**
129 * @brief Print the whole expression \p exp to debug output.
130 *
131 * @param[in] exp Expression to use.
132 */
133static void
Michal Vasko40308e72020-10-20 16:38:40 +0200134print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200135{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100136#define MSG_BUFFER_SIZE 128
137 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100138 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200139
Radek Krejci52b6d512020-10-12 12:33:17 +0200140 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200141 return;
142 }
143
144 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
145 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200146 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200147 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100148 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200149 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
150 for (j = 1; exp->repeat[i][j]; ++j) {
151 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
152 }
153 strcat(tmp, ")");
154 }
155 LOGDBG(LY_LDGXPATH, tmp);
156 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100157#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200158}
159
160#ifndef NDEBUG
161
162/**
163 * @brief Print XPath set content to debug output.
164 *
165 * @param[in] set Set to print.
166 */
167static void
168print_set_debug(struct lyxp_set *set)
169{
170 uint32_t i;
171 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200172 struct lyxp_set_node *item;
173 struct lyxp_set_scnode *sitem;
174
Radek Krejci52b6d512020-10-12 12:33:17 +0200175 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200176 return;
177 }
178
179 switch (set->type) {
180 case LYXP_SET_NODE_SET:
181 LOGDBG(LY_LDGXPATH, "set NODE SET:");
182 for (i = 0; i < set->used; ++i) {
183 item = &set->val.nodes[i];
184
185 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100186 case LYXP_NODE_NONE:
187 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
188 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200189 case LYXP_NODE_ROOT:
190 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
191 break;
192 case LYXP_NODE_ROOT_CONFIG:
193 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
194 break;
195 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100196 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200197 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200198 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100199 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200200 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200201 item->node->schema->name, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200202 } else {
203 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
204 }
205 break;
206 case LYXP_NODE_TEXT:
207 if (item->node->schema->nodetype & LYS_ANYDATA) {
208 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200209 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200210 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200211 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 +0200212 }
213 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100214 case LYXP_NODE_META:
215 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 +0200216 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200217 break;
218 }
219 }
220 break;
221
222 case LYXP_SET_SCNODE_SET:
223 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
224 for (i = 0; i < set->used; ++i) {
225 sitem = &set->val.scnodes[i];
226
227 switch (sitem->type) {
228 case LYXP_NODE_ROOT:
229 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
230 break;
231 case LYXP_NODE_ROOT_CONFIG:
232 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
233 break;
234 case LYXP_NODE_ELEM:
235 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
236 break;
237 default:
238 LOGINT(NULL);
239 break;
240 }
241 }
242 break;
243
Michal Vasko03ff5a72019-09-11 13:49:33 +0200244 case LYXP_SET_BOOLEAN:
245 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200246 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200247 break;
248
249 case LYXP_SET_STRING:
250 LOGDBG(LY_LDGXPATH, "set STRING");
251 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
252 break;
253
254 case LYXP_SET_NUMBER:
255 LOGDBG(LY_LDGXPATH, "set NUMBER");
256
257 if (isnan(set->val.num)) {
258 str = strdup("NaN");
259 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
260 str = strdup("0");
261 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
262 str = strdup("Infinity");
263 } else if (isinf(set->val.num) && signbit(set->val.num)) {
264 str = strdup("-Infinity");
265 } else if ((long long)set->val.num == set->val.num) {
266 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
267 str = NULL;
268 }
269 } else {
270 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
271 str = NULL;
272 }
273 }
274 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
275
276 LOGDBG(LY_LDGXPATH, "\t%s", str);
277 free(str);
278 }
279}
280
281#endif
282
283/**
284 * @brief Realloc the string \p str.
285 *
286 * @param[in] ctx libyang context for logging.
287 * @param[in] needed How much free space is required.
288 * @param[in,out] str Pointer to the string to use.
289 * @param[in,out] used Used bytes in \p str.
290 * @param[in,out] size Allocated bytes in \p str.
291 * @return LY_ERR
292 */
293static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100294cast_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 +0200295{
296 if (*size - *used < needed) {
297 do {
298 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
299 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
300 return LY_EINVAL;
301 }
302 *size += LYXP_STRING_CAST_SIZE_STEP;
303 } while (*size - *used < needed);
304 *str = ly_realloc(*str, *size * sizeof(char));
305 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
306 }
307
308 return LY_SUCCESS;
309}
310
311/**
312 * @brief Cast nodes recursively to one string @p str.
313 *
314 * @param[in] node Node to cast.
315 * @param[in] fake_cont Whether to put the data into a "fake" container.
316 * @param[in] root_type Type of the XPath root.
317 * @param[in] indent Current indent.
318 * @param[in,out] str Resulting string.
319 * @param[in,out] used Used bytes in @p str.
320 * @param[in,out] size Allocated bytes in @p str.
321 * @return LY_ERR
322 */
323static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200324cast_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 +0200325 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200326{
Radek Krejci7f769d72020-07-11 23:13:56 +0200327 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200328 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200329 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200330 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200331 struct lyd_node_any *any;
332 LY_ERR rc;
333
334 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
335 return LY_SUCCESS;
336 }
337
338 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200339 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200340 LY_CHECK_RET(rc);
341 strcpy(*str + (*used - 1), "\n");
342 ++(*used);
343
344 ++indent;
345 }
346
347 switch (node->schema->nodetype) {
348 case LYS_CONTAINER:
349 case LYS_LIST:
350 case LYS_RPC:
351 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200352 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200353 LY_CHECK_RET(rc);
354 strcpy(*str + (*used - 1), "\n");
355 ++(*used);
356
Radek Krejcia1c1e542020-09-29 16:06:52 +0200357 for (child = lyd_child(node); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200358 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
359 LY_CHECK_RET(rc);
360 }
361
362 break;
363
364 case LYS_LEAF:
365 case LYS_LEAFLIST:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200366 value_str = lyd_get_value(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200367
368 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200369 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 +0200370 memset(*str + (*used - 1), ' ', indent * 2);
371 *used += indent * 2;
372
373 /* print value */
374 if (*used == 1) {
375 sprintf(*str + (*used - 1), "%s", value_str);
376 *used += strlen(value_str);
377 } else {
378 sprintf(*str + (*used - 1), "%s\n", value_str);
379 *used += strlen(value_str) + 1;
380 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200381
382 break;
383
384 case LYS_ANYXML:
385 case LYS_ANYDATA:
386 any = (struct lyd_node_any *)node;
387 if (!(void *)any->value.tree) {
388 /* no content */
389 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200390 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200391 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200392 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100393
Michal Vasko60ea6352020-06-29 13:39:39 +0200394 if (any->value_type == LYD_ANYDATA_LYB) {
395 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200396 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 +0200397 /* successfully parsed */
398 free(any->value.mem);
399 any->value.tree = tree;
400 any->value_type = LYD_ANYDATA_DATATREE;
401 }
Radek Krejci7931b192020-06-25 17:05:03 +0200402 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200403 }
404
Michal Vasko03ff5a72019-09-11 13:49:33 +0200405 switch (any->value_type) {
406 case LYD_ANYDATA_STRING:
407 case LYD_ANYDATA_XML:
408 case LYD_ANYDATA_JSON:
409 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200410 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200411 break;
412 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200413 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200414 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200415 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100416 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200417 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200418 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200419 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200420 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200421 }
422 }
423
424 line = strtok_r(buf, "\n", &ptr);
425 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200426 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200427 if (rc != LY_SUCCESS) {
428 free(buf);
429 return rc;
430 }
431 memset(*str + (*used - 1), ' ', indent * 2);
432 *used += indent * 2;
433
434 strcpy(*str + (*used - 1), line);
435 *used += strlen(line);
436
437 strcpy(*str + (*used - 1), "\n");
438 *used += 1;
439 } while ((line = strtok_r(NULL, "\n", &ptr)));
440
441 free(buf);
442 break;
443
444 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200445 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200446 }
447
448 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200449 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200450 LY_CHECK_RET(rc);
451 strcpy(*str + (*used - 1), "\n");
452 ++(*used);
453
454 --indent;
455 }
456
457 return LY_SUCCESS;
458}
459
460/**
461 * @brief Cast an element into a string.
462 *
463 * @param[in] node Node to cast.
464 * @param[in] fake_cont Whether to put the data into a "fake" container.
465 * @param[in] root_type Type of the XPath root.
466 * @param[out] str Element cast to dynamically-allocated string.
467 * @return LY_ERR
468 */
469static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200470cast_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 +0200471{
472 uint16_t used, size;
473 LY_ERR rc;
474
475 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200476 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200477 (*str)[0] = '\0';
478 used = 1;
479 size = LYXP_STRING_CAST_SIZE_START;
480
481 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
482 if (rc != LY_SUCCESS) {
483 free(*str);
484 return rc;
485 }
486
487 if (size > used) {
488 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200489 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200490 }
491 return LY_SUCCESS;
492}
493
494/**
495 * @brief Cast a LYXP_SET_NODE_SET set into a string.
496 * Context position aware.
497 *
498 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200499 * @param[out] str Cast dynamically-allocated string.
500 * @return LY_ERR
501 */
502static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100503cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200504{
Michal Vaskoaa677062021-03-09 13:52:53 +0100505 if (!set->used) {
506 *str = strdup("");
507 if (!*str) {
508 LOGMEM_RET(set->ctx);
509 }
510 return LY_SUCCESS;
511 }
512
Michal Vasko03ff5a72019-09-11 13:49:33 +0200513 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100514 case LYXP_NODE_NONE:
515 /* invalid */
516 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200517 case LYXP_NODE_ROOT:
518 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100519 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200520 case LYXP_NODE_ELEM:
521 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100522 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100523 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200524 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200525 if (!*str) {
526 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200527 }
528 return LY_SUCCESS;
529 }
530
531 LOGINT_RET(set->ctx);
532}
533
534/**
535 * @brief Cast a string into an XPath number.
536 *
537 * @param[in] str String to use.
538 * @return Cast number.
539 */
540static long double
541cast_string_to_number(const char *str)
542{
543 long double num;
544 char *ptr;
545
546 errno = 0;
547 num = strtold(str, &ptr);
548 if (errno || *ptr) {
549 num = NAN;
550 }
551 return num;
552}
553
554/**
555 * @brief Callback for checking value equality.
556 *
Michal Vasko62524a92021-02-26 10:08:50 +0100557 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200558 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200559 * @param[in] val1_p First value.
560 * @param[in] val2_p Second value.
561 * @param[in] mod Whether hash table is being modified.
562 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200563 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200564 */
Radek Krejci857189e2020-09-01 13:26:36 +0200565static ly_bool
566set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200567{
568 struct lyxp_set_hash_node *val1, *val2;
569
570 val1 = (struct lyxp_set_hash_node *)val1_p;
571 val2 = (struct lyxp_set_hash_node *)val2_p;
572
573 if ((val1->node == val2->node) && (val1->type == val2->type)) {
574 return 1;
575 }
576
577 return 0;
578}
579
580/**
581 * @brief Insert node and its hash into set.
582 *
583 * @param[in] set et to insert to.
584 * @param[in] node Node with hash.
585 * @param[in] type Node type.
586 */
587static void
588set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
589{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200590 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200591 uint32_t i, hash;
592 struct lyxp_set_hash_node hnode;
593
594 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
595 /* create hash table and add all the nodes */
596 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
597 for (i = 0; i < set->used; ++i) {
598 hnode.node = set->val.nodes[i].node;
599 hnode.type = set->val.nodes[i].type;
600
601 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
602 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
603 hash = dict_hash_multi(hash, NULL, 0);
604
605 r = lyht_insert(set->ht, &hnode, hash, NULL);
606 assert(!r);
607 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200608
Michal Vasko9d6befd2019-12-11 14:56:56 +0100609 if (hnode.node == node) {
610 /* it was just added, do not add it twice */
611 node = NULL;
612 }
613 }
614 }
615
616 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200617 /* add the new node into hash table */
618 hnode.node = node;
619 hnode.type = type;
620
621 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
622 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
623 hash = dict_hash_multi(hash, NULL, 0);
624
625 r = lyht_insert(set->ht, &hnode, hash, NULL);
626 assert(!r);
627 (void)r;
628 }
629}
630
631/**
632 * @brief Remove node and its hash from set.
633 *
634 * @param[in] set Set to remove from.
635 * @param[in] node Node to remove.
636 * @param[in] type Node type.
637 */
638static void
639set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
640{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200641 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200642 struct lyxp_set_hash_node hnode;
643 uint32_t hash;
644
645 if (set->ht) {
646 hnode.node = node;
647 hnode.type = type;
648
649 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
650 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
651 hash = dict_hash_multi(hash, NULL, 0);
652
653 r = lyht_remove(set->ht, &hnode, hash);
654 assert(!r);
655 (void)r;
656
657 if (!set->ht->used) {
658 lyht_free(set->ht);
659 set->ht = NULL;
660 }
661 }
662}
663
664/**
665 * @brief Check whether node is in set based on its hash.
666 *
667 * @param[in] set Set to search in.
668 * @param[in] node Node to search for.
669 * @param[in] type Node type.
670 * @param[in] skip_idx Index in @p set to skip.
671 * @return LY_ERR
672 */
673static LY_ERR
674set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
675{
676 struct lyxp_set_hash_node hnode, *match_p;
677 uint32_t hash;
678
679 hnode.node = node;
680 hnode.type = type;
681
682 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
683 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
684 hash = dict_hash_multi(hash, NULL, 0);
685
686 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
687 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
688 /* we found it on the index that should be skipped, find another */
689 hnode = *match_p;
690 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
691 /* none other found */
692 return LY_SUCCESS;
693 }
694 }
695
696 return LY_EEXIST;
697 }
698
699 /* not found */
700 return LY_SUCCESS;
701}
702
Michal Vaskod3678892020-05-21 10:06:58 +0200703void
704lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200705{
706 if (!set) {
707 return;
708 }
709
710 if (set->type == LYXP_SET_NODE_SET) {
711 free(set->val.nodes);
712 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200713 } else if (set->type == LYXP_SET_SCNODE_SET) {
714 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200715 lyht_free(set->ht);
716 } else {
717 if (set->type == LYXP_SET_STRING) {
718 free(set->val.str);
719 }
720 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200721 }
Michal Vaskod3678892020-05-21 10:06:58 +0200722
723 set->val.nodes = NULL;
724 set->used = 0;
725 set->size = 0;
726 set->ht = NULL;
727 set->ctx_pos = 0;
aPiecek748da732021-06-01 09:56:43 +0200728 set->ctx_size = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200729}
730
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100731/**
732 * @brief Free dynamically-allocated set.
733 *
734 * @param[in] set Set to free.
735 */
736static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200737lyxp_set_free(struct lyxp_set *set)
738{
739 if (!set) {
740 return;
741 }
742
Michal Vaskod3678892020-05-21 10:06:58 +0200743 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200744 free(set);
745}
746
747/**
748 * @brief Initialize set context.
749 *
750 * @param[in] new Set to initialize.
751 * @param[in] set Arbitrary initialized set.
752 */
753static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200754set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200755{
756 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200757 if (set) {
758 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200759 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100760 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200761 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100762 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200763 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200764 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200765 new->prefix_data = set->prefix_data;
Michal Vasko02a77382019-09-12 11:47:35 +0200766 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200767}
768
769/**
770 * @brief Create a deep copy of a set.
771 *
772 * @param[in] set Set to copy.
773 * @return Copy of @p set.
774 */
775static struct lyxp_set *
776set_copy(struct lyxp_set *set)
777{
778 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100779 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200780
781 if (!set) {
782 return NULL;
783 }
784
785 ret = malloc(sizeof *ret);
786 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
787 set_init(ret, set);
788
789 if (set->type == LYXP_SET_SCNODE_SET) {
790 ret->type = set->type;
791
792 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100793 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
794 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200795 uint32_t idx;
796 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
797 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100798 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200799 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200800 lyxp_set_free(ret);
801 return NULL;
802 }
Michal Vaskoba716542019-12-16 10:01:58 +0100803 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200804 }
805 }
806 } else if (set->type == LYXP_SET_NODE_SET) {
807 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200808 if (set->used) {
809 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
810 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
811 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
812 } else {
813 ret->val.nodes = NULL;
814 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200815
816 ret->used = ret->size = set->used;
817 ret->ctx_pos = set->ctx_pos;
818 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100819 if (set->ht) {
820 ret->ht = lyht_dup(set->ht);
821 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200822 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200823 memcpy(ret, set, sizeof *ret);
824 if (set->type == LYXP_SET_STRING) {
825 ret->val.str = strdup(set->val.str);
826 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
827 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200828 }
829
830 return ret;
831}
832
833/**
834 * @brief Fill XPath set with a string. Any current data are disposed of.
835 *
836 * @param[in] set Set to fill.
837 * @param[in] string String to fill into \p set.
838 * @param[in] str_len Length of \p string. 0 is a valid value!
839 */
840static void
841set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
842{
Michal Vaskod3678892020-05-21 10:06:58 +0200843 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200844
845 set->type = LYXP_SET_STRING;
846 if ((str_len == 0) && (string[0] != '\0')) {
847 string = "";
848 }
849 set->val.str = strndup(string, str_len);
850}
851
852/**
853 * @brief Fill XPath set with a number. Any current data are disposed of.
854 *
855 * @param[in] set Set to fill.
856 * @param[in] number Number to fill into \p set.
857 */
858static void
859set_fill_number(struct lyxp_set *set, long double number)
860{
Michal Vaskod3678892020-05-21 10:06:58 +0200861 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200862
863 set->type = LYXP_SET_NUMBER;
864 set->val.num = number;
865}
866
867/**
868 * @brief Fill XPath set with a boolean. Any current data are disposed of.
869 *
870 * @param[in] set Set to fill.
871 * @param[in] boolean Boolean to fill into \p set.
872 */
873static void
Radek Krejci857189e2020-09-01 13:26:36 +0200874set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200875{
Michal Vaskod3678892020-05-21 10:06:58 +0200876 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200877
878 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200879 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200880}
881
882/**
883 * @brief Fill XPath set with the value from another set (deep assign).
884 * Any current data are disposed of.
885 *
886 * @param[in] trg Set to fill.
887 * @param[in] src Source set to copy into \p trg.
888 */
889static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200890set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200891{
892 if (!trg || !src) {
893 return;
894 }
895
896 if (trg->type == LYXP_SET_NODE_SET) {
897 free(trg->val.nodes);
898 } else if (trg->type == LYXP_SET_STRING) {
899 free(trg->val.str);
900 }
901 set_init(trg, src);
902
903 if (src->type == LYXP_SET_SCNODE_SET) {
904 trg->type = LYXP_SET_SCNODE_SET;
905 trg->used = src->used;
906 trg->size = src->used;
907
Michal Vasko08e9b112021-06-11 15:41:17 +0200908 if (trg->size) {
909 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
910 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
911 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
912 } else {
913 trg->val.scnodes = NULL;
914 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200915 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200916 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200917 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200918 set_fill_number(trg, src->val.num);
919 } else if (src->type == LYXP_SET_STRING) {
920 set_fill_string(trg, src->val.str, strlen(src->val.str));
921 } else {
922 if (trg->type == LYXP_SET_NODE_SET) {
923 free(trg->val.nodes);
924 } else if (trg->type == LYXP_SET_STRING) {
925 free(trg->val.str);
926 }
927
Michal Vaskod3678892020-05-21 10:06:58 +0200928 assert(src->type == LYXP_SET_NODE_SET);
929
930 trg->type = LYXP_SET_NODE_SET;
931 trg->used = src->used;
932 trg->size = src->used;
933 trg->ctx_pos = src->ctx_pos;
934 trg->ctx_size = src->ctx_size;
935
Michal Vasko08e9b112021-06-11 15:41:17 +0200936 if (trg->size) {
937 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
938 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
939 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
940 } else {
941 trg->val.nodes = NULL;
942 }
Michal Vaskod3678892020-05-21 10:06:58 +0200943 if (src->ht) {
944 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200945 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200946 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200947 }
948 }
949}
950
951/**
952 * @brief Clear context of all schema nodes.
953 *
954 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +0200955 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200956 */
957static void
Michal Vasko1a09b212021-05-06 13:00:10 +0200958set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200959{
960 uint32_t i;
961
962 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100963 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +0200964 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100965 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
966 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200967 }
968 }
969}
970
971/**
972 * @brief Remove a node from a set. Removing last node changes
973 * set into LYXP_SET_EMPTY. Context position aware.
974 *
975 * @param[in] set Set to use.
976 * @param[in] idx Index from @p set of the node to be removed.
977 */
978static void
979set_remove_node(struct lyxp_set *set, uint32_t idx)
980{
981 assert(set && (set->type == LYXP_SET_NODE_SET));
982 assert(idx < set->used);
983
984 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
985
986 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +0200987 if (idx < set->used) {
988 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
989 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +0200990 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200991 }
992}
993
994/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100995 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200996 *
997 * @param[in] set Set to use.
998 * @param[in] idx Index from @p set of the node to be removed.
999 */
1000static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001001set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001002{
1003 assert(set && (set->type == LYXP_SET_NODE_SET));
1004 assert(idx < set->used);
1005
Michal Vasko2caefc12019-11-14 16:07:56 +01001006 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1007 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1008 }
1009 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001010}
1011
1012/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001013 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001014 * set into LYXP_SET_EMPTY. Context position aware.
1015 *
1016 * @param[in] set Set to consolidate.
1017 */
1018static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001019set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001020{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001021 uint32_t i, orig_used, end = 0;
1022 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001023
Michal Vaskod3678892020-05-21 10:06:58 +02001024 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001025
1026 orig_used = set->used;
1027 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001028 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001029 start = -1;
1030 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001031 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001032 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001033 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001034 end = i;
1035 ++i;
1036 break;
1037 }
1038
1039 ++i;
1040 if (i == orig_used) {
1041 end = i;
1042 }
1043 } while (i < orig_used);
1044
1045 if (start > -1) {
1046 /* move the whole chunk of valid nodes together */
1047 if (set->used != (unsigned)start) {
1048 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1049 }
1050 set->used += end - start;
1051 }
1052 }
Michal Vasko57eab132019-09-24 11:46:26 +02001053}
1054
1055/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001056 * @brief Check for duplicates in a node set.
1057 *
1058 * @param[in] set Set to check.
1059 * @param[in] node Node to look for in @p set.
1060 * @param[in] node_type Type of @p node.
1061 * @param[in] skip_idx Index from @p set to skip.
1062 * @return LY_ERR
1063 */
1064static LY_ERR
1065set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1066{
1067 uint32_t i;
1068
Michal Vasko2caefc12019-11-14 16:07:56 +01001069 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001070 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1071 }
1072
1073 for (i = 0; i < set->used; ++i) {
1074 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1075 continue;
1076 }
1077
1078 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1079 return LY_EEXIST;
1080 }
1081 }
1082
1083 return LY_SUCCESS;
1084}
1085
Radek Krejci857189e2020-09-01 13:26:36 +02001086ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001087lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1088 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001089{
1090 uint32_t i;
1091
1092 for (i = 0; i < set->used; ++i) {
1093 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1094 continue;
1095 }
1096
1097 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001098 if (index_p) {
1099 *index_p = i;
1100 }
1101 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001102 }
1103 }
1104
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001105 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001106}
1107
Michal Vaskoecd62de2019-11-13 12:35:11 +01001108void
1109lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001110{
1111 uint32_t orig_used, i, j;
1112
Michal Vaskod3678892020-05-21 10:06:58 +02001113 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001114
Michal Vaskod3678892020-05-21 10:06:58 +02001115 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001116 return;
1117 }
1118
Michal Vaskod3678892020-05-21 10:06:58 +02001119 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001120 memcpy(set1, set2, sizeof *set1);
1121 return;
1122 }
1123
1124 if (set1->used + set2->used > set1->size) {
1125 set1->size = set1->used + set2->used;
1126 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1127 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1128 }
1129
1130 orig_used = set1->used;
1131
1132 for (i = 0; i < set2->used; ++i) {
1133 for (j = 0; j < orig_used; ++j) {
1134 /* detect duplicities */
1135 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1136 break;
1137 }
1138 }
1139
Michal Vasko0587bce2020-12-10 12:19:21 +01001140 if (j < orig_used) {
1141 /* node is there, but update its status if needed */
1142 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1143 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001144 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1145 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1146 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001147 }
1148 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001149 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1150 ++set1->used;
1151 }
1152 }
1153
Michal Vaskod3678892020-05-21 10:06:58 +02001154 lyxp_set_free_content(set2);
1155 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001156}
1157
1158/**
1159 * @brief Insert a node into a set. Context position aware.
1160 *
1161 * @param[in] set Set to use.
1162 * @param[in] node Node to insert to @p set.
1163 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1164 * @param[in] node_type Node type of @p node.
1165 * @param[in] idx Index in @p set to insert into.
1166 */
1167static void
1168set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1169{
Michal Vaskod3678892020-05-21 10:06:58 +02001170 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001171
Michal Vaskod3678892020-05-21 10:06:58 +02001172 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001173 /* first item */
1174 if (idx) {
1175 /* no real harm done, but it is a bug */
1176 LOGINT(set->ctx);
1177 idx = 0;
1178 }
1179 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1180 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1181 set->type = LYXP_SET_NODE_SET;
1182 set->used = 0;
1183 set->size = LYXP_SET_SIZE_START;
1184 set->ctx_pos = 1;
1185 set->ctx_size = 1;
1186 set->ht = NULL;
1187 } else {
1188 /* not an empty set */
1189 if (set->used == set->size) {
1190
1191 /* set is full */
1192 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1193 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1194 set->size += LYXP_SET_SIZE_STEP;
1195 }
1196
1197 if (idx > set->used) {
1198 LOGINT(set->ctx);
1199 idx = set->used;
1200 }
1201
1202 /* make space for the new node */
1203 if (idx < set->used) {
1204 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1205 }
1206 }
1207
1208 /* finally assign the value */
1209 set->val.nodes[idx].node = (struct lyd_node *)node;
1210 set->val.nodes[idx].type = node_type;
1211 set->val.nodes[idx].pos = pos;
1212 ++set->used;
1213
Michal Vasko2416fdd2021-10-01 11:07:10 +02001214 /* add into hash table */
1215 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001216}
1217
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001218LY_ERR
1219lyxp_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 +02001220{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001221 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001222
1223 assert(set->type == LYXP_SET_SCNODE_SET);
1224
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001225 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
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 } else {
1228 if (set->used == set->size) {
1229 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 +02001230 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001231 set->size += LYXP_SET_SIZE_STEP;
1232 }
1233
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001234 index = set->used;
1235 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1236 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001237 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001238 ++set->used;
1239 }
1240
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001241 if (index_p) {
1242 *index_p = index;
1243 }
1244
1245 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001246}
1247
1248/**
1249 * @brief Replace a node in a set with another. Context position aware.
1250 *
1251 * @param[in] set Set to use.
1252 * @param[in] node Node to insert to @p set.
1253 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1254 * @param[in] node_type Node type of @p node.
1255 * @param[in] idx Index in @p set of the node to replace.
1256 */
1257static void
1258set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1259{
1260 assert(set && (idx < set->used));
1261
Michal Vasko2caefc12019-11-14 16:07:56 +01001262 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1263 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1264 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001265 set->val.nodes[idx].node = (struct lyd_node *)node;
1266 set->val.nodes[idx].type = node_type;
1267 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001268 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1269 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1270 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001271}
1272
1273/**
1274 * @brief Set all nodes with ctx 1 to a new unique context value.
1275 *
1276 * @param[in] set Set to modify.
1277 * @return New context value.
1278 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001279static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001280set_scnode_new_in_ctx(struct lyxp_set *set)
1281{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001282 uint32_t i;
1283 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001284
1285 assert(set->type == LYXP_SET_SCNODE_SET);
1286
Radek Krejcif13b87b2020-12-01 22:02:17 +01001287 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001288retry:
1289 for (i = 0; i < set->used; ++i) {
1290 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1291 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1292 goto retry;
1293 }
1294 }
1295 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001296 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001297 set->val.scnodes[i].in_ctx = ret_ctx;
1298 }
1299 }
1300
1301 return ret_ctx;
1302}
1303
1304/**
1305 * @brief Get unique @p node position in the data.
1306 *
1307 * @param[in] node Node to find.
1308 * @param[in] node_type Node type of @p node.
1309 * @param[in] root Root node.
1310 * @param[in] root_type Type of the XPath @p root node.
1311 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1312 * be used to increase efficiency and start the DFS from this node.
1313 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1314 * @return Node position.
1315 */
1316static uint32_t
1317get_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 +02001318 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001319{
Michal Vasko56daf732020-08-10 10:57:18 +02001320 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001321 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001322 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001323
1324 assert(prev && prev_pos && !root->prev->next);
1325
1326 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1327 return 0;
1328 }
1329
1330 if (*prev) {
1331 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001332 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001333 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001334 goto dfs_search;
1335 }
1336
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001337 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001338 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001339dfs_search:
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001340 LYD_TREE_DFS_continue = 0;
1341
Michal Vasko56daf732020-08-10 10:57:18 +02001342 if (*prev && !elem) {
1343 /* resume previous DFS */
1344 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1345 LYD_TREE_DFS_continue = 0;
1346 }
1347
Michal Vasko03ff5a72019-09-11 13:49:33 +02001348 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001349 /* skip */
1350 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001351 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001352 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001353 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001354 break;
1355 }
Michal Vasko56daf732020-08-10 10:57:18 +02001356 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001357 }
Michal Vasko56daf732020-08-10 10:57:18 +02001358
1359 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001360 }
1361
1362 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001363 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001364 break;
1365 }
1366 }
1367
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001368 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001369 if (!(*prev)) {
1370 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001371 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001372 return 0;
1373 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001374 /* start the search again from the beginning */
1375 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001376
Michal Vasko56daf732020-08-10 10:57:18 +02001377 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001378 pos = 1;
1379 goto dfs_search;
1380 }
1381 }
1382
1383 /* remember the last found node for next time */
1384 *prev = node;
1385 *prev_pos = pos;
1386
1387 return pos;
1388}
1389
1390/**
1391 * @brief Assign (fill) missing node positions.
1392 *
1393 * @param[in] set Set to fill positions in.
1394 * @param[in] root Context root node.
1395 * @param[in] root_type Context root type.
1396 * @return LY_ERR
1397 */
1398static LY_ERR
1399set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1400{
1401 const struct lyd_node *prev = NULL, *tmp_node;
1402 uint32_t i, tmp_pos = 0;
1403
1404 for (i = 0; i < set->used; ++i) {
1405 if (!set->val.nodes[i].pos) {
1406 tmp_node = NULL;
1407 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001408 case LYXP_NODE_META:
1409 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001410 if (!tmp_node) {
1411 LOGINT_RET(root->schema->module->ctx);
1412 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001413 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001414 case LYXP_NODE_ELEM:
1415 case LYXP_NODE_TEXT:
1416 if (!tmp_node) {
1417 tmp_node = set->val.nodes[i].node;
1418 }
1419 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1420 break;
1421 default:
1422 /* all roots have position 0 */
1423 break;
1424 }
1425 }
1426 }
1427
1428 return LY_SUCCESS;
1429}
1430
1431/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001432 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001433 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001434 * @param[in] meta Metadata to use.
1435 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001436 */
1437static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001438get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001439{
1440 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001441 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001442
Michal Vasko9f96a052020-03-10 09:41:45 +01001443 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001444 ++pos;
1445 }
1446
Michal Vasko9f96a052020-03-10 09:41:45 +01001447 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001448 return pos;
1449}
1450
1451/**
1452 * @brief Compare 2 nodes in respect to XPath document order.
1453 *
1454 * @param[in] item1 1st node.
1455 * @param[in] item2 2nd node.
1456 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1457 */
1458static int
1459set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1460{
Michal Vasko9f96a052020-03-10 09:41:45 +01001461 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001462
1463 if (item1->pos < item2->pos) {
1464 return -1;
1465 }
1466
1467 if (item1->pos > item2->pos) {
1468 return 1;
1469 }
1470
1471 /* node positions are equal, the fun case */
1472
1473 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1474 /* special case since text nodes are actually saved as their parents */
1475 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1476 if (item1->type == LYXP_NODE_ELEM) {
1477 assert(item2->type == LYXP_NODE_TEXT);
1478 return -1;
1479 } else {
1480 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1481 return 1;
1482 }
1483 }
1484
Michal Vasko9f96a052020-03-10 09:41:45 +01001485 /* we need meta positions now */
1486 if (item1->type == LYXP_NODE_META) {
1487 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001488 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001489 if (item2->type == LYXP_NODE_META) {
1490 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001491 }
1492
Michal Vasko9f96a052020-03-10 09:41:45 +01001493 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001494 /* check for duplicates */
1495 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001496 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001497 return 0;
1498 }
1499
Michal Vasko9f96a052020-03-10 09:41:45 +01001500 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001501 /* elem is always first, 2nd node is after it */
1502 if (item1->type == LYXP_NODE_ELEM) {
1503 assert(item2->type != LYXP_NODE_ELEM);
1504 return -1;
1505 }
1506
Michal Vasko9f96a052020-03-10 09:41:45 +01001507 /* 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 +02001508 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001509 if (((item1->type == LYXP_NODE_TEXT) &&
1510 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1511 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1512 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1513 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001514 return 1;
1515 }
1516
Michal Vasko9f96a052020-03-10 09:41:45 +01001517 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001518 /* 2nd is after 1st */
1519 return -1;
1520}
1521
1522/**
1523 * @brief Set cast for comparisons.
1524 *
1525 * @param[in] trg Target set to cast source into.
1526 * @param[in] src Source set.
1527 * @param[in] type Target set type.
1528 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001529 * @return LY_ERR
1530 */
1531static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001532set_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 +02001533{
1534 assert(src->type == LYXP_SET_NODE_SET);
1535
1536 set_init(trg, src);
1537
1538 /* insert node into target set */
1539 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1540
1541 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001542 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001543}
1544
Michal Vasko4c7763f2020-07-27 17:40:37 +02001545/**
1546 * @brief Set content canonization for comparisons.
1547 *
1548 * @param[in] trg Target set to put the canononized source into.
1549 * @param[in] src Source set.
1550 * @param[in] xp_node Source XPath node/meta to use for canonization.
1551 * @return LY_ERR
1552 */
1553static LY_ERR
1554set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1555{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001556 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001557 struct lyd_value val;
1558 struct ly_err_item *err = NULL;
1559 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001560 LY_ERR rc;
1561
1562 /* is there anything to canonize even? */
1563 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1564 /* do we have a type to use for canonization? */
1565 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1566 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1567 } else if (xp_node->type == LYXP_NODE_META) {
1568 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1569 }
1570 }
1571 if (!type) {
1572 goto fill;
1573 }
1574
1575 if (src->type == LYXP_SET_NUMBER) {
1576 /* canonize number */
1577 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1578 LOGMEM(src->ctx);
1579 return LY_EMEM;
1580 }
1581 } else {
1582 /* canonize string */
1583 str = strdup(src->val.str);
1584 }
1585
1586 /* ignore errors, the value may not satisfy schema constraints */
Radek Krejci0b013302021-03-29 15:22:32 +02001587 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 +01001588 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001589 ly_err_free(err);
1590 if (rc) {
1591 /* invalid value */
Radek IÅ¡a48460222021-03-02 15:18:32 +01001592 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001593 goto fill;
1594 }
1595
Michal Vasko4c7763f2020-07-27 17:40:37 +02001596 /* use the canonized value */
1597 set_init(trg, src);
1598 trg->type = src->type;
1599 if (src->type == LYXP_SET_NUMBER) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001600 trg->val.num = strtold(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001601 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1602 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001603 trg->val.str = strdup(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko4c7763f2020-07-27 17:40:37 +02001604 }
1605 type->plugin->free(src->ctx, &val);
1606 return LY_SUCCESS;
1607
1608fill:
1609 /* no canonization needed/possible */
1610 set_fill_set(trg, src);
1611 return LY_SUCCESS;
1612}
1613
Michal Vasko03ff5a72019-09-11 13:49:33 +02001614#ifndef NDEBUG
1615
1616/**
1617 * @brief Bubble sort @p set into XPath document order.
1618 * Context position aware. Unused in the 'Release' build target.
1619 *
1620 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001621 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1622 */
1623static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001624set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001625{
1626 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001627 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001628 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001629 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001630 struct lyxp_set_node item;
1631 struct lyxp_set_hash_node hnode;
1632 uint64_t hash;
1633
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001634 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001635 return 0;
1636 }
1637
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001638 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001639 for (root = set->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001640 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001641
1642 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001643 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001644 return -1;
1645 }
1646
1647 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1648 print_set_debug(set);
1649
1650 for (i = 0; i < set->used; ++i) {
1651 inverted = 0;
1652 change = 0;
1653
1654 for (j = 1; j < set->used - i; ++j) {
1655 /* compare node positions */
1656 if (inverted) {
1657 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1658 } else {
1659 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1660 }
1661
1662 /* swap if needed */
1663 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1664 change = 1;
1665
1666 item = set->val.nodes[j - 1];
1667 set->val.nodes[j - 1] = set->val.nodes[j];
1668 set->val.nodes[j] = item;
1669 } else {
1670 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1671 inverted = !inverted;
1672 }
1673 }
1674
1675 ++ret;
1676
1677 if (!change) {
1678 break;
1679 }
1680 }
1681
1682 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1683 print_set_debug(set);
1684
1685 /* check node hashes */
1686 if (set->used >= LYD_HT_MIN_ITEMS) {
1687 assert(set->ht);
1688 for (i = 0; i < set->used; ++i) {
1689 hnode.node = set->val.nodes[i].node;
1690 hnode.type = set->val.nodes[i].type;
1691
1692 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1693 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1694 hash = dict_hash_multi(hash, NULL, 0);
1695
1696 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1697 }
1698 }
1699
1700 return ret - 1;
1701}
1702
1703/**
1704 * @brief Remove duplicate entries in a sorted node set.
1705 *
1706 * @param[in] set Sorted set to check.
1707 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1708 */
1709static LY_ERR
1710set_sorted_dup_node_clean(struct lyxp_set *set)
1711{
1712 uint32_t i = 0;
1713 LY_ERR ret = LY_SUCCESS;
1714
1715 if (set->used > 1) {
1716 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001717 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1718 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001719 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001720 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001721 }
Michal Vasko57eab132019-09-24 11:46:26 +02001722 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001723 }
1724 }
1725
Michal Vasko2caefc12019-11-14 16:07:56 +01001726 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001727 return ret;
1728}
1729
1730#endif
1731
1732/**
1733 * @brief Merge 2 sorted sets into one.
1734 *
1735 * @param[in,out] trg Set to merge into. Duplicates are removed.
1736 * @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 +02001737 * @return LY_ERR
1738 */
1739static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001740set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001741{
1742 uint32_t i, j, k, count, dup_count;
1743 int cmp;
1744 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001745
Michal Vaskod3678892020-05-21 10:06:58 +02001746 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001747 return LY_EINVAL;
1748 }
1749
Michal Vaskod3678892020-05-21 10:06:58 +02001750 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001751 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001752 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001753 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001754 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001755 return LY_SUCCESS;
1756 }
1757
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001758 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001759 for (root = trg->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001760 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001761
1762 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001763 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001764 return LY_EINT;
1765 }
1766
1767#ifndef NDEBUG
1768 LOGDBG(LY_LDGXPATH, "MERGE target");
1769 print_set_debug(trg);
1770 LOGDBG(LY_LDGXPATH, "MERGE source");
1771 print_set_debug(src);
1772#endif
1773
1774 /* make memory for the merge (duplicates are not detected yet, so space
1775 * will likely be wasted on them, too bad) */
1776 if (trg->size - trg->used < src->used) {
1777 trg->size = trg->used + src->used;
1778
1779 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1780 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1781 }
1782
1783 i = 0;
1784 j = 0;
1785 count = 0;
1786 dup_count = 0;
1787 do {
1788 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1789 if (!cmp) {
1790 if (!count) {
1791 /* duplicate, just skip it */
1792 ++i;
1793 ++j;
1794 } else {
1795 /* we are copying something already, so let's copy the duplicate too,
1796 * we are hoping that afterwards there are some more nodes to
1797 * copy and this way we can copy them all together */
1798 ++count;
1799 ++dup_count;
1800 ++i;
1801 ++j;
1802 }
1803 } else if (cmp < 0) {
1804 /* inserting src node into trg, just remember it for now */
1805 ++count;
1806 ++i;
1807
1808 /* insert the hash now */
1809 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1810 } else if (count) {
1811copy_nodes:
1812 /* time to actually copy the nodes, we have found the largest block of nodes */
1813 memmove(&trg->val.nodes[j + (count - dup_count)],
1814 &trg->val.nodes[j],
1815 (trg->used - j) * sizeof *trg->val.nodes);
1816 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1817
1818 trg->used += count - dup_count;
1819 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1820 j += count - dup_count;
1821
1822 count = 0;
1823 dup_count = 0;
1824 } else {
1825 ++j;
1826 }
1827 } while ((i < src->used) && (j < trg->used));
1828
1829 if ((i < src->used) || count) {
1830 /* insert all the hashes first */
1831 for (k = i; k < src->used; ++k) {
1832 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1833 }
1834
1835 /* loop ended, but we need to copy something at trg end */
1836 count += src->used - i;
1837 i = src->used;
1838 goto copy_nodes;
1839 }
1840
1841 /* we are inserting hashes before the actual node insert, which causes
1842 * situations when there were initially not enough items for a hash table,
1843 * but even after some were inserted, hash table was not created (during
1844 * insertion the number of items is not updated yet) */
1845 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1846 set_insert_node_hash(trg, NULL, 0);
1847 }
1848
1849#ifndef NDEBUG
1850 LOGDBG(LY_LDGXPATH, "MERGE result");
1851 print_set_debug(trg);
1852#endif
1853
Michal Vaskod3678892020-05-21 10:06:58 +02001854 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001855 return LY_SUCCESS;
1856}
1857
1858/*
1859 * (re)parse functions
1860 *
1861 * Parse functions parse the expression into
1862 * tokens (syntactic analysis).
1863 *
1864 * Reparse functions perform semantic analysis
1865 * (do not save the result, just a check) of
1866 * the expression and fill repeat indices.
1867 */
1868
Michal Vasko14676352020-05-29 11:35:55 +02001869LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001870lyxp_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 +02001871{
Michal Vasko004d3152020-06-11 19:59:22 +02001872 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001873 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001874 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001875 }
Michal Vasko14676352020-05-29 11:35:55 +02001876 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001877 }
1878
Michal Vasko004d3152020-06-11 19:59:22 +02001879 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001880 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001881 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001882 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001883 }
Michal Vasko14676352020-05-29 11:35:55 +02001884 return LY_ENOT;
1885 }
1886
1887 return LY_SUCCESS;
1888}
1889
Michal Vasko004d3152020-06-11 19:59:22 +02001890LY_ERR
1891lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1892{
1893 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1894
1895 /* skip the token */
1896 ++(*tok_idx);
1897
1898 return LY_SUCCESS;
1899}
1900
Michal Vasko14676352020-05-29 11:35:55 +02001901/* just like lyxp_check_token() but tests for 2 tokens */
1902static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001903exp_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 +02001904 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001905{
Michal Vasko004d3152020-06-11 19:59:22 +02001906 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001907 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001908 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001909 }
1910 return LY_EINCOMPLETE;
1911 }
1912
Michal Vasko004d3152020-06-11 19:59:22 +02001913 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001914 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001915 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001916 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001917 }
1918 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001919 }
1920
1921 return LY_SUCCESS;
1922}
1923
Michal Vasko4911eeb2021-06-28 11:23:05 +02001924LY_ERR
1925lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok1,
1926 enum lyxp_token want_tok2)
1927{
1928 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
1929
1930 /* skip the token */
1931 ++(*tok_idx);
1932
1933 return LY_SUCCESS;
1934}
1935
Michal Vasko03ff5a72019-09-11 13:49:33 +02001936/**
1937 * @brief Stack operation push on the repeat array.
1938 *
1939 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001940 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001941 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1942 */
1943static void
Michal Vasko004d3152020-06-11 19:59:22 +02001944exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001945{
1946 uint16_t i;
1947
Michal Vasko004d3152020-06-11 19:59:22 +02001948 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001949 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001950 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1951 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1952 exp->repeat[tok_idx][i] = repeat_op_idx;
1953 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001954 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001955 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1956 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1957 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001958 }
1959}
1960
1961/**
1962 * @brief Reparse Predicate. Logs directly on error.
1963 *
1964 * [7] Predicate ::= '[' Expr ']'
1965 *
1966 * @param[in] ctx Context for logging.
1967 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001968 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02001969 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001970 * @return LY_ERR
1971 */
1972static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02001973reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001974{
1975 LY_ERR rc;
1976
Michal Vasko004d3152020-06-11 19:59:22 +02001977 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001978 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001979 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001980
aPiecekbf968d92021-05-27 14:35:05 +02001981 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001982 LY_CHECK_RET(rc);
1983
Michal Vasko004d3152020-06-11 19:59:22 +02001984 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001985 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001986 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001987
1988 return LY_SUCCESS;
1989}
1990
1991/**
1992 * @brief Reparse RelativeLocationPath. Logs directly on error.
1993 *
1994 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1995 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1996 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1997 *
1998 * @param[in] ctx Context for logging.
1999 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002000 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002001 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002002 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
2003 */
2004static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002005reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002006{
2007 LY_ERR rc;
2008
Michal Vasko004d3152020-06-11 19:59:22 +02002009 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002010 LY_CHECK_RET(rc);
2011
2012 goto step;
2013 do {
2014 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002015 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002016
Michal Vasko004d3152020-06-11 19:59:22 +02002017 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002018 LY_CHECK_RET(rc);
2019step:
2020 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002021 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002022 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002023 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002024 break;
2025
2026 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002027 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002028 break;
2029
2030 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002031 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002032
Michal Vasko004d3152020-06-11 19:59:22 +02002033 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002034 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002035 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002036 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 +02002037 return LY_EVALID;
2038 }
Radek Krejci0f969882020-08-21 16:56:47 +02002039 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002040 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002041 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002042 goto reparse_predicate;
2043 break;
2044
2045 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002046 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002047
2048 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002049 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002050 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002051 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002052
2053 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002054 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002055 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002056 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002057
2058reparse_predicate:
2059 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002060 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002061 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002062 LY_CHECK_RET(rc);
2063 }
2064 break;
2065 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002066 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 +02002067 return LY_EVALID;
2068 }
Michal Vasko004d3152020-06-11 19:59:22 +02002069 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002070
2071 return LY_SUCCESS;
2072}
2073
2074/**
2075 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2076 *
2077 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2078 *
2079 * @param[in] ctx Context for logging.
2080 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002081 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002082 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002083 * @return LY_ERR
2084 */
2085static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002086reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002087{
2088 LY_ERR rc;
2089
Michal Vasko004d3152020-06-11 19:59:22 +02002090 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 +02002091
2092 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002093 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002094 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002095 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002096
Michal Vasko004d3152020-06-11 19:59:22 +02002097 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002098 return LY_SUCCESS;
2099 }
Michal Vasko004d3152020-06-11 19:59:22 +02002100 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002101 case LYXP_TOKEN_DOT:
2102 case LYXP_TOKEN_DDOT:
2103 case LYXP_TOKEN_AT:
2104 case LYXP_TOKEN_NAMETEST:
2105 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002106 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002107 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002108 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002109 default:
2110 break;
2111 }
2112
Michal Vasko03ff5a72019-09-11 13:49:33 +02002113 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002114 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002115 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002116
aPiecekbf968d92021-05-27 14:35:05 +02002117 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002118 LY_CHECK_RET(rc);
2119 }
2120
2121 return LY_SUCCESS;
2122}
2123
2124/**
2125 * @brief Reparse FunctionCall. Logs directly on error.
2126 *
2127 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2128 *
2129 * @param[in] ctx Context for logging.
2130 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002131 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002132 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002133 * @return LY_ERR
2134 */
2135static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002136reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002137{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002138 int8_t min_arg_count = -1;
2139 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002140 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002141 LY_ERR rc;
2142
Michal Vasko004d3152020-06-11 19:59:22 +02002143 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002144 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002145 func_tok_idx = *tok_idx;
2146 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002147 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002148 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002149 min_arg_count = 1;
2150 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002151 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002152 min_arg_count = 1;
2153 max_arg_count = 1;
2154 }
2155 break;
2156 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002157 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002158 min_arg_count = 1;
2159 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002160 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002161 min_arg_count = 0;
2162 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002163 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002164 min_arg_count = 0;
2165 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002166 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002167 min_arg_count = 0;
2168 max_arg_count = 0;
2169 }
2170 break;
2171 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002172 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002173 min_arg_count = 1;
2174 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002175 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002176 min_arg_count = 0;
2177 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002178 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002179 min_arg_count = 1;
2180 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002181 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002182 min_arg_count = 1;
2183 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002184 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002185 min_arg_count = 1;
2186 max_arg_count = 1;
2187 }
2188 break;
2189 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002190 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002191 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002192 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002193 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002194 min_arg_count = 0;
2195 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002196 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002197 min_arg_count = 0;
2198 max_arg_count = 1;
2199 }
2200 break;
2201 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002202 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002203 min_arg_count = 1;
2204 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002205 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002206 min_arg_count = 1;
2207 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002208 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002209 min_arg_count = 0;
2210 max_arg_count = 0;
2211 }
2212 break;
2213 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002214 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002215 min_arg_count = 2;
2216 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002217 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002218 min_arg_count = 0;
2219 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002220 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002221 min_arg_count = 2;
2222 max_arg_count = 2;
2223 }
2224 break;
2225 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002226 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002227 min_arg_count = 2;
2228 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002229 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002230 min_arg_count = 3;
2231 max_arg_count = 3;
2232 }
2233 break;
2234 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002235 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002236 min_arg_count = 0;
2237 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002238 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002239 min_arg_count = 1;
2240 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002241 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002242 min_arg_count = 2;
2243 max_arg_count = 2;
2244 }
2245 break;
2246 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002247 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002248 min_arg_count = 2;
2249 max_arg_count = 2;
2250 }
2251 break;
2252 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002253 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002254 min_arg_count = 2;
2255 max_arg_count = 2;
2256 }
2257 break;
2258 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002259 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002260 min_arg_count = 0;
2261 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002262 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002263 min_arg_count = 0;
2264 max_arg_count = 1;
2265 }
2266 break;
2267 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002268 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002269 min_arg_count = 0;
2270 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002271 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002272 min_arg_count = 2;
2273 max_arg_count = 2;
2274 }
2275 break;
2276 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002277 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002278 min_arg_count = 2;
2279 max_arg_count = 2;
2280 }
2281 break;
2282 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002283 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002284 min_arg_count = 2;
2285 max_arg_count = 2;
2286 }
2287 break;
2288 }
2289 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002290 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 +02002291 return LY_EINVAL;
2292 }
Michal Vasko004d3152020-06-11 19:59:22 +02002293 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002294
2295 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002296 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002297 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002298 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002299
2300 /* ( Expr ( ',' Expr )* )? */
2301 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002302 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002303 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002304 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002305 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002306 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002307 LY_CHECK_RET(rc);
2308 }
Michal Vasko004d3152020-06-11 19:59:22 +02002309 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2310 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002311
2312 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002313 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002314 LY_CHECK_RET(rc);
2315 }
2316
2317 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002318 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002319 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002320 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002321
Radek Krejci857189e2020-09-01 13:26:36 +02002322 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002323 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 +02002324 return LY_EVALID;
2325 }
2326
2327 return LY_SUCCESS;
2328}
2329
2330/**
2331 * @brief Reparse PathExpr. Logs directly on error.
2332 *
2333 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2334 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2335 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2336 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2337 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2338 *
2339 * @param[in] ctx Context for logging.
2340 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002341 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002342 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002343 * @return LY_ERR
2344 */
2345static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002346reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002347{
2348 LY_ERR rc;
2349
Michal Vasko004d3152020-06-11 19:59:22 +02002350 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002351 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002352 }
2353
Michal Vasko004d3152020-06-11 19:59:22 +02002354 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002355 case LYXP_TOKEN_PAR1:
2356 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002357 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002358
aPiecekbf968d92021-05-27 14:35:05 +02002359 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002360 LY_CHECK_RET(rc);
2361
Michal Vasko004d3152020-06-11 19:59:22 +02002362 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002363 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002364 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002365 goto predicate;
2366 break;
2367 case LYXP_TOKEN_DOT:
2368 case LYXP_TOKEN_DDOT:
2369 case LYXP_TOKEN_AT:
2370 case LYXP_TOKEN_NAMETEST:
2371 case LYXP_TOKEN_NODETYPE:
2372 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002373 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002374 LY_CHECK_RET(rc);
2375 break;
2376 case LYXP_TOKEN_FUNCNAME:
2377 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002378 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002379 LY_CHECK_RET(rc);
2380 goto predicate;
2381 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002382 case LYXP_TOKEN_OPER_PATH:
2383 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002384 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002385 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002386 LY_CHECK_RET(rc);
2387 break;
2388 case LYXP_TOKEN_LITERAL:
2389 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002390 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002391 goto predicate;
2392 break;
2393 case LYXP_TOKEN_NUMBER:
2394 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002395 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002396 goto predicate;
2397 break;
2398 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002399 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 +02002400 return LY_EVALID;
2401 }
2402
2403 return LY_SUCCESS;
2404
2405predicate:
2406 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002407 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002408 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002409 LY_CHECK_RET(rc);
2410 }
2411
2412 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002413 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002414
2415 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002416 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002417
aPiecekbf968d92021-05-27 14:35:05 +02002418 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002419 LY_CHECK_RET(rc);
2420 }
2421
2422 return LY_SUCCESS;
2423}
2424
2425/**
2426 * @brief Reparse UnaryExpr. Logs directly on error.
2427 *
2428 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2429 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2430 *
2431 * @param[in] ctx Context for logging.
2432 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002433 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002434 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002435 * @return LY_ERR
2436 */
2437static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002438reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002439{
2440 uint16_t prev_exp;
2441 LY_ERR rc;
2442
2443 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002444 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002445 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2446 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002447 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002448 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002449 }
2450
2451 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002452 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002453 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002454 LY_CHECK_RET(rc);
2455
2456 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002457 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002458 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002459 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002460
aPiecekbf968d92021-05-27 14:35:05 +02002461 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002462 LY_CHECK_RET(rc);
2463 }
2464
2465 return LY_SUCCESS;
2466}
2467
2468/**
2469 * @brief Reparse AdditiveExpr. Logs directly on error.
2470 *
2471 * [15] AdditiveExpr ::= MultiplicativeExpr
2472 * | AdditiveExpr '+' MultiplicativeExpr
2473 * | AdditiveExpr '-' MultiplicativeExpr
2474 * [16] MultiplicativeExpr ::= UnaryExpr
2475 * | MultiplicativeExpr '*' UnaryExpr
2476 * | MultiplicativeExpr 'div' UnaryExpr
2477 * | MultiplicativeExpr 'mod' UnaryExpr
2478 *
2479 * @param[in] ctx Context for logging.
2480 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002481 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002482 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002483 * @return LY_ERR
2484 */
2485static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002486reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002487{
2488 uint16_t prev_add_exp, prev_mul_exp;
2489 LY_ERR rc;
2490
Michal Vasko004d3152020-06-11 19:59:22 +02002491 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002492 goto reparse_multiplicative_expr;
2493
2494 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002495 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2496 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002497 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002498 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002499
2500reparse_multiplicative_expr:
2501 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002502 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002503 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002504 LY_CHECK_RET(rc);
2505
2506 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002507 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2508 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002509 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002510 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002511
aPiecekbf968d92021-05-27 14:35:05 +02002512 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002513 LY_CHECK_RET(rc);
2514 }
2515 }
2516
2517 return LY_SUCCESS;
2518}
2519
2520/**
2521 * @brief Reparse EqualityExpr. Logs directly on error.
2522 *
2523 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2524 * | EqualityExpr '!=' RelationalExpr
2525 * [14] RelationalExpr ::= AdditiveExpr
2526 * | RelationalExpr '<' AdditiveExpr
2527 * | RelationalExpr '>' AdditiveExpr
2528 * | RelationalExpr '<=' AdditiveExpr
2529 * | RelationalExpr '>=' AdditiveExpr
2530 *
2531 * @param[in] ctx Context for logging.
2532 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002533 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002534 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002535 * @return LY_ERR
2536 */
2537static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002538reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002539{
2540 uint16_t prev_eq_exp, prev_rel_exp;
2541 LY_ERR rc;
2542
Michal Vasko004d3152020-06-11 19:59:22 +02002543 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002544 goto reparse_additive_expr;
2545
2546 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002547 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002548 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002549 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002550
2551reparse_additive_expr:
2552 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002553 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002554 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002555 LY_CHECK_RET(rc);
2556
2557 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002558 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002559 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002560 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002561
aPiecekbf968d92021-05-27 14:35:05 +02002562 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002563 LY_CHECK_RET(rc);
2564 }
2565 }
2566
2567 return LY_SUCCESS;
2568}
2569
2570/**
2571 * @brief Reparse OrExpr. Logs directly on error.
2572 *
2573 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2574 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2575 *
2576 * @param[in] ctx Context for logging.
2577 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002578 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002579 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002580 * @return LY_ERR
2581 */
2582static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002583reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002584{
2585 uint16_t prev_or_exp, prev_and_exp;
2586 LY_ERR rc;
2587
aPiecekbf968d92021-05-27 14:35:05 +02002588 ++depth;
2589 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2590
Michal Vasko004d3152020-06-11 19:59:22 +02002591 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002592 goto reparse_equality_expr;
2593
2594 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002595 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 +02002596 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002597 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002598
2599reparse_equality_expr:
2600 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002601 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002602 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002603 LY_CHECK_RET(rc);
2604
2605 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002606 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 +02002607 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002608 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002609
aPiecekbf968d92021-05-27 14:35:05 +02002610 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002611 LY_CHECK_RET(rc);
2612 }
2613 }
2614
2615 return LY_SUCCESS;
2616}
Radek Krejcib1646a92018-11-02 16:08:26 +01002617
2618/**
2619 * @brief Parse NCName.
2620 *
2621 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002622 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002623 */
Radek Krejcid4270262019-01-07 15:07:25 +01002624static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002625parse_ncname(const char *ncname)
2626{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002627 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002628 size_t size;
2629 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002630
2631 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2632 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2633 return len;
2634 }
2635
2636 do {
2637 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002638 if (!*ncname) {
2639 break;
2640 }
Radek Krejcid4270262019-01-07 15:07:25 +01002641 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002642 } while (is_xmlqnamechar(uc) && (uc != ':'));
2643
2644 return len;
2645}
2646
2647/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002648 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002649 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002650 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002651 * @param[in] exp Expression to use.
2652 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002653 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002654 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002655 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002656 */
2657static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002658exp_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 +01002659{
2660 uint32_t prev;
2661
2662 if (exp->used == exp->size) {
2663 prev = exp->size;
2664 exp->size += LYXP_EXPR_SIZE_STEP;
2665 if (prev > exp->size) {
2666 LOGINT(ctx);
2667 return LY_EINT;
2668 }
2669
2670 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2671 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2672 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2673 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2674 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2675 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2676 }
2677
2678 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002679 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002680 exp->tok_len[exp->used] = tok_len;
2681 ++exp->used;
2682 return LY_SUCCESS;
2683}
2684
2685void
Michal Vasko14676352020-05-29 11:35:55 +02002686lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002687{
2688 uint16_t i;
2689
2690 if (!expr) {
2691 return;
2692 }
2693
2694 lydict_remove(ctx, expr->expr);
2695 free(expr->tokens);
2696 free(expr->tok_pos);
2697 free(expr->tok_len);
2698 if (expr->repeat) {
2699 for (i = 0; i < expr->used; ++i) {
2700 free(expr->repeat[i]);
2701 }
2702 }
2703 free(expr->repeat);
2704 free(expr);
2705}
2706
Radek Krejcif03a9e22020-09-18 20:09:31 +02002707LY_ERR
2708lyxp_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 +01002709{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002710 LY_ERR ret = LY_SUCCESS;
2711 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002712 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002713 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002714 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002715 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002716
Radek Krejcif03a9e22020-09-18 20:09:31 +02002717 assert(expr_p);
2718
2719 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002720 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002721 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002722 }
2723
2724 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002725 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002726 }
2727 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002728 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002729 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002730 }
2731
2732 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002733 expr = calloc(1, sizeof *expr);
2734 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2735 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2736 expr->used = 0;
2737 expr->size = LYXP_EXPR_SIZE_START;
2738 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2739 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002740
Radek Krejcif03a9e22020-09-18 20:09:31 +02002741 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2742 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002743
Radek Krejcif03a9e22020-09-18 20:09:31 +02002744 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2745 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002746
Michal Vasko004d3152020-06-11 19:59:22 +02002747 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002748 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002749
Radek Krejcif03a9e22020-09-18 20:09:31 +02002750 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002751 ++parsed;
2752 }
2753
2754 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002755 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002756
2757 /* '(' */
2758 tok_len = 1;
2759 tok_type = LYXP_TOKEN_PAR1;
2760
Radek Krejcif03a9e22020-09-18 20:09:31 +02002761 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002762 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002763 if (((expr->tok_len[expr->used - 1] == 4) &&
2764 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2765 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2766 ((expr->tok_len[expr->used - 1] == 7) &&
2767 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002768 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002769 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002770 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002771 }
2772 prev_function_check = 0;
2773 }
2774
Radek Krejcif03a9e22020-09-18 20:09:31 +02002775 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002776
2777 /* ')' */
2778 tok_len = 1;
2779 tok_type = LYXP_TOKEN_PAR2;
2780
Radek Krejcif03a9e22020-09-18 20:09:31 +02002781 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002782
2783 /* '[' */
2784 tok_len = 1;
2785 tok_type = LYXP_TOKEN_BRACK1;
2786
Radek Krejcif03a9e22020-09-18 20:09:31 +02002787 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002788
2789 /* ']' */
2790 tok_len = 1;
2791 tok_type = LYXP_TOKEN_BRACK2;
2792
Radek Krejcif03a9e22020-09-18 20:09:31 +02002793 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002794
2795 /* '..' */
2796 tok_len = 2;
2797 tok_type = LYXP_TOKEN_DDOT;
2798
Radek Krejcif03a9e22020-09-18 20:09:31 +02002799 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002800
2801 /* '.' */
2802 tok_len = 1;
2803 tok_type = LYXP_TOKEN_DOT;
2804
Radek Krejcif03a9e22020-09-18 20:09:31 +02002805 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002806
2807 /* '@' */
2808 tok_len = 1;
2809 tok_type = LYXP_TOKEN_AT;
2810
Radek Krejcif03a9e22020-09-18 20:09:31 +02002811 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002812
2813 /* ',' */
2814 tok_len = 1;
2815 tok_type = LYXP_TOKEN_COMMA;
2816
Radek Krejcif03a9e22020-09-18 20:09:31 +02002817 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002818
2819 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002820 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2821 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002822 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002823 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002824 ++tok_len;
2825 tok_type = LYXP_TOKEN_LITERAL;
2826
Radek Krejcif03a9e22020-09-18 20:09:31 +02002827 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002828
2829 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002830 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2831 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002832 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002833 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002834 ++tok_len;
2835 tok_type = LYXP_TOKEN_LITERAL;
2836
Radek Krejcif03a9e22020-09-18 20:09:31 +02002837 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002838
2839 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002840 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2841 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002842 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002843 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002844 }
2845 tok_type = LYXP_TOKEN_NUMBER;
2846
Radek Krejcif03a9e22020-09-18 20:09:31 +02002847 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002848
2849 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002850 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002851 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002852 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002853 } else {
2854 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002855 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002856 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002857
Radek Krejcif03a9e22020-09-18 20:09:31 +02002858 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002859
Michal Vasko3e48bf32020-06-01 08:39:07 +02002860 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002861 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002862 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2863
Radek Krejcif03a9e22020-09-18 20:09:31 +02002864 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002865
2866 /* Operator '<=', '>=' */
2867 tok_len = 2;
2868 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002869
Radek Krejcif03a9e22020-09-18 20:09:31 +02002870 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002871
2872 /* Operator '|' */
2873 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002874 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002875
Radek Krejcif03a9e22020-09-18 20:09:31 +02002876 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002877
2878 /* Operator '+', '-' */
2879 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002880 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002881
Radek Krejcif03a9e22020-09-18 20:09:31 +02002882 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002883
Michal Vasko3e48bf32020-06-01 08:39:07 +02002884 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002885 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002886 tok_type = LYXP_TOKEN_OPER_EQUAL;
2887
Radek Krejcif03a9e22020-09-18 20:09:31 +02002888 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002889
2890 /* Operator '<', '>' */
2891 tok_len = 1;
2892 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002893
Michal Vasko69730152020-10-09 16:30:07 +02002894 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2895 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2896 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2897 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2898 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2899 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2900 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2901 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2902 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2903 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2904 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2905 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002906
2907 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002908 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002909 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002910 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002911
Radek Krejcif03a9e22020-09-18 20:09:31 +02002912 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002913 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002914 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002915
Radek Krejcif03a9e22020-09-18 20:09:31 +02002916 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002917 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002918 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002919
Radek Krejcif03a9e22020-09-18 20:09:31 +02002920 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002921 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002922 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002923
2924 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002925 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 +02002926 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 +02002927 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002928 goto error;
2929 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02002930 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002931 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002932 goto error;
2933 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002934 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002935
2936 /* NameTest '*' */
2937 tok_len = 1;
2938 tok_type = LYXP_TOKEN_NAMETEST;
2939
2940 } else {
2941
2942 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002943 long int ncname_len = parse_ncname(&expr_str[parsed]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02002944 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
Michal Vasko774ce402021-04-14 15:35:06 +02002945 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002946 tok_len = ncname_len;
2947
Radek Krejcif03a9e22020-09-18 20:09:31 +02002948 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002949 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002950 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002951 ++tok_len;
2952 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002953 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02002954 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
Michal Vasko774ce402021-04-14 15:35:06 +02002955 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002956 tok_len += ncname_len;
2957 }
2958 /* remove old flag to prevent ambiguities */
2959 prev_function_check = 0;
2960 tok_type = LYXP_TOKEN_NAMETEST;
2961 } else {
2962 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2963 prev_function_check = 1;
2964 tok_type = LYXP_TOKEN_NAMETEST;
2965 }
2966 }
2967
2968 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002969 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002970 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002971 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002972 ++parsed;
2973 }
2974
Radek Krejcif03a9e22020-09-18 20:09:31 +02002975 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002976
Michal Vasko004d3152020-06-11 19:59:22 +02002977 if (reparse) {
2978 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002979 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2980 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002981
Michal Vasko004d3152020-06-11 19:59:22 +02002982 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02002983 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002984 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002985 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002986 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002987 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002988 goto error;
2989 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002990 }
2991
Radek Krejcif03a9e22020-09-18 20:09:31 +02002992 print_expr_struct_debug(expr);
2993 *expr_p = expr;
2994 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002995
2996error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002997 lyxp_expr_free(ctx, expr);
2998 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002999}
3000
Michal Vasko1734be92020-09-22 08:55:10 +02003001LY_ERR
3002lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003003{
Michal Vasko1734be92020-09-22 08:55:10 +02003004 LY_ERR ret = LY_SUCCESS;
3005 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003006 uint32_t i, j;
3007
Michal Vasko7f45cf22020-10-01 12:49:44 +02003008 if (!exp) {
3009 goto cleanup;
3010 }
3011
Michal Vasko004d3152020-06-11 19:59:22 +02003012 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003013 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003014
Michal Vasko08e9b112021-06-11 15:41:17 +02003015 if (exp->used) {
3016 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
3017 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3018 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003019
Michal Vasko08e9b112021-06-11 15:41:17 +02003020 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
3021 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3022 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003023
Michal Vasko08e9b112021-06-11 15:41:17 +02003024 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
3025 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3026 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003027
Michal Vasko08e9b112021-06-11 15:41:17 +02003028 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
3029 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3030 for (i = 0; i < exp->used; ++i) {
3031 if (!exp->repeat[i]) {
3032 dup->repeat[i] = NULL;
3033 } else {
3034 for (j = 0; exp->repeat[i][j]; ++j) {}
3035 /* the ending 0 as well */
3036 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003037
Michal Vasko08e9b112021-06-11 15:41:17 +02003038 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
3039 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3040 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3041 dup->repeat[i][j - 1] = 0;
3042 }
Michal Vasko004d3152020-06-11 19:59:22 +02003043 }
3044 }
3045
3046 dup->used = exp->used;
3047 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003048 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003049
Michal Vasko1734be92020-09-22 08:55:10 +02003050cleanup:
3051 if (ret) {
3052 lyxp_expr_free(ctx, dup);
3053 } else {
3054 *dup_p = dup;
3055 }
3056 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003057}
3058
Michal Vasko03ff5a72019-09-11 13:49:33 +02003059/*
3060 * warn functions
3061 *
3062 * Warn functions check specific reasonable conditions for schema XPath
3063 * and print a warning if they are not satisfied.
3064 */
3065
3066/**
3067 * @brief Get the last-added schema node that is currently in the context.
3068 *
3069 * @param[in] set Set to search in.
3070 * @return Last-added schema context node, NULL if no node is in context.
3071 */
3072static struct lysc_node *
3073warn_get_scnode_in_ctx(struct lyxp_set *set)
3074{
3075 uint32_t i;
3076
3077 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3078 return NULL;
3079 }
3080
3081 i = set->used;
3082 do {
3083 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003084 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003085 /* if there are more, simply return the first found (last added) */
3086 return set->val.scnodes[i].scnode;
3087 }
3088 } while (i);
3089
3090 return NULL;
3091}
3092
3093/**
3094 * @brief Test whether a type is numeric - integer type or decimal64.
3095 *
3096 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003097 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003098 */
Radek Krejci857189e2020-09-01 13:26:36 +02003099static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003100warn_is_numeric_type(struct lysc_type *type)
3101{
3102 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003103 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003104 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003105
3106 switch (type->basetype) {
3107 case LY_TYPE_DEC64:
3108 case LY_TYPE_INT8:
3109 case LY_TYPE_UINT8:
3110 case LY_TYPE_INT16:
3111 case LY_TYPE_UINT16:
3112 case LY_TYPE_INT32:
3113 case LY_TYPE_UINT32:
3114 case LY_TYPE_INT64:
3115 case LY_TYPE_UINT64:
3116 return 1;
3117 case LY_TYPE_UNION:
3118 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003119 LY_ARRAY_FOR(uni->types, u) {
3120 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003121 if (ret) {
3122 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003123 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003124 }
3125 }
3126 /* did not find any suitable type */
3127 return 0;
3128 case LY_TYPE_LEAFREF:
3129 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3130 default:
3131 return 0;
3132 }
3133}
3134
3135/**
3136 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3137 *
3138 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003139 * @return Boolean value whether @p type's basetype is string type or not.
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_string_type(struct lysc_type *type)
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 switch (type->basetype) {
3149 case LY_TYPE_BITS:
3150 case LY_TYPE_ENUM:
3151 case LY_TYPE_IDENT:
3152 case LY_TYPE_INST:
3153 case LY_TYPE_STRING:
3154 return 1;
3155 case LY_TYPE_UNION:
3156 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003157 LY_ARRAY_FOR(uni->types, u) {
3158 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003159 if (ret) {
3160 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003161 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003162 }
3163 }
3164 /* did not find any suitable type */
3165 return 0;
3166 case LY_TYPE_LEAFREF:
3167 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3168 default:
3169 return 0;
3170 }
3171}
3172
3173/**
3174 * @brief Test whether a type is one specific type.
3175 *
3176 * @param[in] type Type to test.
3177 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003178 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003179 */
Radek Krejci857189e2020-09-01 13:26:36 +02003180static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003181warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3182{
3183 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003184 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003185 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003186
3187 if (type->basetype == base) {
3188 return 1;
3189 } else if (type->basetype == LY_TYPE_UNION) {
3190 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003191 LY_ARRAY_FOR(uni->types, u) {
3192 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003193 if (ret) {
3194 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003195 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003196 }
3197 }
3198 /* did not find any suitable type */
3199 return 0;
3200 } else if (type->basetype == LY_TYPE_LEAFREF) {
3201 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3202 }
3203
3204 return 0;
3205}
3206
3207/**
3208 * @brief Get next type of a (union) type.
3209 *
3210 * @param[in] type Base type.
3211 * @param[in] prev_type Previously returned type.
3212 * @return Next type or NULL.
3213 */
3214static struct lysc_type *
3215warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3216{
3217 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003218 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003219 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003220
3221 switch (type->basetype) {
3222 case LY_TYPE_UNION:
3223 uni = (struct lysc_type_union *)type;
3224 if (!prev_type) {
3225 return uni->types[0];
3226 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003227 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003228 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003229 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003230 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003231 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003232 found = 1;
3233 }
3234 }
3235 return NULL;
3236 default:
3237 if (prev_type) {
3238 assert(type == prev_type);
3239 return NULL;
3240 } else {
3241 return type;
3242 }
3243 }
3244}
3245
3246/**
3247 * @brief Test whether 2 types have a common type.
3248 *
3249 * @param[in] type1 First type.
3250 * @param[in] type2 Second type.
3251 * @return 1 if they do, 0 otherwise.
3252 */
3253static int
3254warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3255{
3256 struct lysc_type *t1, *rt1, *t2, *rt2;
3257
3258 t1 = NULL;
3259 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3260 if (t1->basetype == LY_TYPE_LEAFREF) {
3261 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3262 } else {
3263 rt1 = t1;
3264 }
3265
3266 t2 = NULL;
3267 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3268 if (t2->basetype == LY_TYPE_LEAFREF) {
3269 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3270 } else {
3271 rt2 = t2;
3272 }
3273
3274 if (rt2->basetype == rt1->basetype) {
3275 /* match found */
3276 return 1;
3277 }
3278 }
3279 }
3280
3281 return 0;
3282}
3283
3284/**
3285 * @brief Check both operands of comparison operators.
3286 *
3287 * @param[in] ctx Context for errors.
3288 * @param[in] set1 First operand set.
3289 * @param[in] set2 Second operand set.
3290 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3291 * @param[in] expr Start of the expression to print with the warning.
3292 * @param[in] tok_pos Token position.
3293 */
3294static void
Radek Krejci857189e2020-09-01 13:26:36 +02003295warn_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 +02003296{
3297 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003298 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003299
3300 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3301 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3302
3303 if (!node1 && !node2) {
3304 /* no node-sets involved, nothing to do */
3305 return;
3306 }
3307
3308 if (node1) {
3309 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3310 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3311 warning = 1;
3312 leaves = 0;
3313 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3314 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3315 warning = 1;
3316 }
3317 }
3318
3319 if (node2) {
3320 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3321 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3322 warning = 1;
3323 leaves = 0;
3324 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3325 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3326 warning = 1;
3327 }
3328 }
3329
3330 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003331 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3332 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3333 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3334 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003335 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3336 warning = 1;
3337 }
3338 }
3339
3340 if (warning) {
3341 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3342 }
3343}
3344
3345/**
3346 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3347 *
3348 * @param[in] exp Parsed XPath expression.
3349 * @param[in] set Set with the leaf/leaf-list.
3350 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3351 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3352 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3353 */
3354static void
Michal Vasko40308e72020-10-20 16:38:40 +02003355warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3356 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003357{
3358 struct lysc_node *scnode;
3359 struct lysc_type *type;
3360 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003361 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003362 LY_ERR rc;
3363 struct ly_err_item *err = NULL;
3364
Michal Vasko69730152020-10-09 16:30:07 +02003365 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3366 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003367 /* check that the node can have the specified value */
3368 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3369 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3370 } else {
3371 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3372 }
3373 if (!value) {
3374 LOGMEM(set->ctx);
3375 return;
3376 }
3377
3378 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3379 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003380 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003381 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003382 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003383 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003384 }
3385
3386 type = ((struct lysc_node_leaf *)scnode)->type;
3387 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003388 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003389 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003390 if (rc == LY_EINCOMPLETE) {
3391 rc = LY_SUCCESS;
3392 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003393
3394 if (err) {
3395 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3396 ly_err_free(err);
3397 } else if (rc != LY_SUCCESS) {
3398 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3399 }
3400 if (rc != LY_SUCCESS) {
3401 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003402 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003403 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003404 } else {
3405 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003406 }
3407 }
3408 free(value);
3409 }
3410}
3411
3412/*
3413 * XPath functions
3414 */
3415
3416/**
3417 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3418 * depending on whether the first node bit value from the second argument is set.
3419 *
3420 * @param[in] args Array of arguments.
3421 * @param[in] arg_count Count of elements in @p args.
3422 * @param[in,out] set Context and result set at the same time.
3423 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003424 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003425 */
3426static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003427xpath_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 +02003428{
3429 struct lyd_node_term *leaf;
3430 struct lysc_node_leaf *sleaf;
Michal Vasko2588b952021-07-29 07:43:26 +02003431 struct lyd_value_bits *bits;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003432 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003433 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003434
3435 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003436 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003437 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003438 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3439 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3440 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3441 sleaf->name);
3442 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3443 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3444 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003445 }
3446
3447 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3448 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003449 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3450 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003451 } else if (!warn_is_string_type(sleaf->type)) {
3452 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003453 }
3454 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003455 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003456 return rc;
3457 }
3458
Michal Vaskod3678892020-05-21 10:06:58 +02003459 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003460 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 +02003461 return LY_EVALID;
3462 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003463 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003464 LY_CHECK_RET(rc);
3465
3466 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003467 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003468 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko2588b952021-07-29 07:43:26 +02003469 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) {
3470 LYD_VALUE_GET(&leaf->value, bits);
3471 LY_ARRAY_FOR(bits->items, u) {
3472 if (!strcmp(bits->items[u]->name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003473 set_fill_boolean(set, 1);
3474 break;
3475 }
3476 }
3477 }
3478 }
3479
3480 return LY_SUCCESS;
3481}
3482
3483/**
3484 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3485 * with the argument converted to boolean.
3486 *
3487 * @param[in] args Array of arguments.
3488 * @param[in] arg_count Count of elements in @p args.
3489 * @param[in,out] set Context and result set at the same time.
3490 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003491 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003492 */
3493static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003494xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003495{
3496 LY_ERR rc;
3497
3498 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003499 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003500 return LY_SUCCESS;
3501 }
3502
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003503 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003504 LY_CHECK_RET(rc);
3505 set_fill_set(set, args[0]);
3506
3507 return LY_SUCCESS;
3508}
3509
3510/**
3511 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3512 * with the first argument rounded up to the nearest integer.
3513 *
3514 * @param[in] args Array of arguments.
3515 * @param[in] arg_count Count of elements in @p args.
3516 * @param[in,out] set Context and result set at the same time.
3517 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003518 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003519 */
3520static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003521xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003522{
3523 struct lysc_node_leaf *sleaf;
3524 LY_ERR rc = LY_SUCCESS;
3525
3526 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003527 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003528 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003529 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3530 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3531 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3532 sleaf->name);
3533 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3534 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3535 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003536 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003537 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003538 return rc;
3539 }
3540
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003541 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003542 LY_CHECK_RET(rc);
3543 if ((long long)args[0]->val.num != args[0]->val.num) {
3544 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3545 } else {
3546 set_fill_number(set, args[0]->val.num);
3547 }
3548
3549 return LY_SUCCESS;
3550}
3551
3552/**
3553 * @brief Execute the XPath concat(string, string, string*) function.
3554 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3555 *
3556 * @param[in] args Array of arguments.
3557 * @param[in] arg_count Count of elements in @p args.
3558 * @param[in,out] set Context and result set at the same time.
3559 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003560 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003561 */
3562static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003563xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003564{
3565 uint16_t i;
3566 char *str = NULL;
3567 size_t used = 1;
3568 LY_ERR rc = LY_SUCCESS;
3569 struct lysc_node_leaf *sleaf;
3570
3571 if (options & LYXP_SCNODE_ALL) {
3572 for (i = 0; i < arg_count; ++i) {
3573 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3574 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3575 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003576 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003577 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003578 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 +02003579 }
3580 }
3581 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003582 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003583 return rc;
3584 }
3585
3586 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003587 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003588 if (rc != LY_SUCCESS) {
3589 free(str);
3590 return rc;
3591 }
3592
3593 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3594 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3595 strcpy(str + used - 1, args[i]->val.str);
3596 used += strlen(args[i]->val.str);
3597 }
3598
3599 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003600 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003601 set->type = LYXP_SET_STRING;
3602 set->val.str = str;
3603
3604 return LY_SUCCESS;
3605}
3606
3607/**
3608 * @brief Execute the XPath contains(string, string) function.
3609 * Returns LYXP_SET_BOOLEAN whether the second argument can
3610 * be found in the first or not.
3611 *
3612 * @param[in] args Array of arguments.
3613 * @param[in] arg_count Count of elements in @p args.
3614 * @param[in,out] set Context and result set at the same time.
3615 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003616 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003617 */
3618static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003619xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003620{
3621 struct lysc_node_leaf *sleaf;
3622 LY_ERR rc = LY_SUCCESS;
3623
3624 if (options & LYXP_SCNODE_ALL) {
3625 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3626 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003627 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3628 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003629 } else if (!warn_is_string_type(sleaf->type)) {
3630 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003631 }
3632 }
3633
3634 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3635 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003636 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3637 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003638 } else if (!warn_is_string_type(sleaf->type)) {
3639 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003640 }
3641 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003642 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003643 return rc;
3644 }
3645
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003646 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003647 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003648 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003649 LY_CHECK_RET(rc);
3650
3651 if (strstr(args[0]->val.str, args[1]->val.str)) {
3652 set_fill_boolean(set, 1);
3653 } else {
3654 set_fill_boolean(set, 0);
3655 }
3656
3657 return LY_SUCCESS;
3658}
3659
3660/**
3661 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3662 * with the size of the node-set from the argument.
3663 *
3664 * @param[in] args Array of arguments.
3665 * @param[in] arg_count Count of elements in @p args.
3666 * @param[in,out] set Context and result set at the same time.
3667 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003668 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003669 */
3670static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003671xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003672{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003673 LY_ERR rc = LY_SUCCESS;
3674
3675 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003676 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003677 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003678 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003679 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003680 return rc;
3681 }
3682
Michal Vasko03ff5a72019-09-11 13:49:33 +02003683 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003684 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003685 return LY_EVALID;
3686 }
3687
3688 set_fill_number(set, args[0]->used);
3689 return LY_SUCCESS;
3690}
3691
3692/**
3693 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3694 * with the context with the intial node.
3695 *
3696 * @param[in] args Array of arguments.
3697 * @param[in] arg_count Count of elements in @p args.
3698 * @param[in,out] set Context and result set at the same time.
3699 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003700 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003701 */
3702static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003703xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003704{
3705 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003706 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003707 return LY_EVALID;
3708 }
3709
3710 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003711 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003712
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003713 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003714 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003715 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003716
3717 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003718 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003719 }
3720
3721 return LY_SUCCESS;
3722}
3723
3724/**
3725 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3726 * leafref or instance-identifier target node(s).
3727 *
3728 * @param[in] args Array of arguments.
3729 * @param[in] arg_count Count of elements in @p args.
3730 * @param[in,out] set Context and result set at the same time.
3731 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003732 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003733 */
3734static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003735xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003736{
3737 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003738 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003739 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003740 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003741 struct ly_path *p;
3742 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003743 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003744 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02003745 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003746
3747 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003748 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003749 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003750 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko423ba3f2021-07-19 13:08:50 +02003751 if (!(sleaf->nodetype & LYD_NODE_TERM)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003752 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3753 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02003754 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
3755 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003756 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3757 __func__, sleaf->name);
3758 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003759 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003760 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko423ba3f2021-07-19 13:08:50 +02003761 if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003762 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003763 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003764
3765 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02003766 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02003767 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02003768 if (!r) {
3769 /* get the target node */
3770 target = p[LY_ARRAY_COUNT(p) - 1].node;
3771 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02003772
Michal Vasko741bb562021-06-24 11:59:50 +02003773 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
3774 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003775 }
3776
Michal Vasko741bb562021-06-24 11:59:50 +02003777 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003778 }
3779
Michal Vaskod3678892020-05-21 10:06:58 +02003780 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003781 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003782 return LY_EVALID;
3783 }
3784
Michal Vaskod3678892020-05-21 10:06:58 +02003785 lyxp_set_free_content(set);
3786 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003787 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3788 sleaf = (struct lysc_node_leaf *)leaf->schema;
3789 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3790 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3791 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02003792 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01003793 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003794 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003795 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003796 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003797 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003798 } else {
3799 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003800 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003801 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003802 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003803 return LY_EVALID;
3804 }
3805 }
Michal Vasko004d3152020-06-11 19:59:22 +02003806
3807 /* insert it */
3808 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003809 }
3810 }
3811
3812 return LY_SUCCESS;
3813}
3814
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003815static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003816xpath_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 +02003817{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01003818 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003819 LY_ARRAY_COUNT_TYPE u;
3820 struct lyd_node_term *leaf;
3821 struct lysc_node_leaf *sleaf;
3822 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02003823 struct lyd_value *val;
3824 const struct lys_module *mod;
3825 const char *id_name;
3826 uint16_t id_len;
3827 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003828 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003829 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003830
3831 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003832 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003833 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003834 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3835 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3836 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3837 sleaf->name);
3838 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3839 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3840 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003841 }
3842
3843 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3844 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3845 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003846 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003847 } else if (!warn_is_string_type(sleaf->type)) {
3848 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3849 }
3850 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003851 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003852 return rc;
3853 }
3854
3855 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003856 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 +02003857 return LY_EVALID;
3858 }
3859 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3860 LY_CHECK_RET(rc);
3861
Michal Vasko93923692021-05-07 15:28:02 +02003862 /* parse the identity */
3863 id_name = args[1]->val.str;
3864 id_len = strlen(id_name);
3865 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
3866 LY_CHECK_RET(rc);
3867 if (!mod) {
3868 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
3869 return LY_EVALID;
3870 }
3871
3872 /* find the identity */
3873 found = 0;
3874 LY_ARRAY_FOR(mod->identities, u) {
3875 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
3876 /* we have match */
3877 found = 1;
3878 break;
3879 }
3880 }
3881 if (!found) {
3882 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
3883 return LY_EVALID;
3884 }
3885 id = &mod->identities[u];
3886
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003887 set_fill_boolean(set, 0);
3888 found = 0;
3889 for (i = 0; i < args[0]->used; ++i) {
3890 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3891 continue;
3892 }
3893
3894 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3895 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3896 sleaf = (struct lysc_node_leaf *)leaf->schema;
3897 val = &leaf->value;
3898 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3899 /* uninteresting */
3900 continue;
3901 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003902 } else {
3903 meta = args[0]->val.meta[i].meta;
3904 val = &meta->value;
3905 if (val->realtype->basetype != LY_TYPE_IDENT) {
3906 /* uninteresting */
3907 continue;
3908 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003909 }
3910
Michal Vasko93923692021-05-07 15:28:02 +02003911 /* check the identity itself */
3912 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003913 set_fill_boolean(set, 1);
3914 found = 1;
3915 }
Michal Vasko93923692021-05-07 15:28:02 +02003916 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
3917 set_fill_boolean(set, 1);
3918 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003919 }
3920
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003921 if (found) {
3922 break;
3923 }
3924 }
3925
3926 return LY_SUCCESS;
3927}
3928
Michal Vasko03ff5a72019-09-11 13:49:33 +02003929/**
3930 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3931 * on whether the first argument nodes contain a node of an identity derived from the second
3932 * argument identity.
3933 *
3934 * @param[in] args Array of arguments.
3935 * @param[in] arg_count Count of elements in @p args.
3936 * @param[in,out] set Context and result set at the same time.
3937 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003938 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003939 */
3940static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003941xpath_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 +02003942{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003943 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003944}
3945
3946/**
3947 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3948 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3949 * the second argument identity.
3950 *
3951 * @param[in] args Array of arguments.
3952 * @param[in] arg_count Count of elements in @p args.
3953 * @param[in,out] set Context and result set at the same time.
3954 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003955 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003956 */
3957static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003958xpath_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 +02003959{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003960 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003961}
3962
3963/**
3964 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3965 * with the integer value of the first node's enum value, otherwise NaN.
3966 *
3967 * @param[in] args Array of arguments.
3968 * @param[in] arg_count Count of elements in @p args.
3969 * @param[in,out] set Context and result set at the same time.
3970 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003971 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003972 */
3973static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003974xpath_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 +02003975{
3976 struct lyd_node_term *leaf;
3977 struct lysc_node_leaf *sleaf;
3978 LY_ERR rc = LY_SUCCESS;
3979
3980 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003981 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003982 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003983 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3984 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3985 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3986 sleaf->name);
3987 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3988 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
3989 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003990 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003991 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003992 return rc;
3993 }
3994
Michal Vaskod3678892020-05-21 10:06:58 +02003995 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003996 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003997 return LY_EVALID;
3998 }
3999
4000 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004001 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004002 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4003 sleaf = (struct lysc_node_leaf *)leaf->schema;
4004 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4005 set_fill_number(set, leaf->value.enum_item->value);
4006 }
4007 }
4008
4009 return LY_SUCCESS;
4010}
4011
4012/**
4013 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4014 * with false value.
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_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 +02004024{
4025 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004026 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004027 return LY_SUCCESS;
4028 }
4029
4030 set_fill_boolean(set, 0);
4031 return LY_SUCCESS;
4032}
4033
4034/**
4035 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4036 * with the first argument floored (truncated).
4037 *
4038 * @param[in] args Array of arguments.
4039 * @param[in] arg_count Count of elements in @p args.
4040 * @param[in,out] set Context and result set at the same time.
4041 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004042 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004043 */
4044static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004045xpath_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 +02004046{
4047 LY_ERR rc;
4048
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004049 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004050 LY_CHECK_RET(rc);
4051 if (isfinite(args[0]->val.num)) {
4052 set_fill_number(set, (long long)args[0]->val.num);
4053 }
4054
4055 return LY_SUCCESS;
4056}
4057
4058/**
4059 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4060 * whether the language of the text matches the one from the argument.
4061 *
4062 * @param[in] args Array of arguments.
4063 * @param[in] arg_count Count of elements in @p args.
4064 * @param[in,out] set Context and result set at the same time.
4065 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004066 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004067 */
4068static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004069xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004070{
4071 const struct lyd_node *node;
4072 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004073 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004074 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004075 LY_ERR rc = LY_SUCCESS;
4076
4077 if (options & LYXP_SCNODE_ALL) {
4078 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4079 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004080 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4081 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004082 } else if (!warn_is_string_type(sleaf->type)) {
4083 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004084 }
4085 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004086 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004087 return rc;
4088 }
4089
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004090 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004091 LY_CHECK_RET(rc);
4092
Michal Vasko03ff5a72019-09-11 13:49:33 +02004093 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004094 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004095 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004096 } else if (!set->used) {
4097 set_fill_boolean(set, 0);
4098 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004099 }
4100
4101 switch (set->val.nodes[0].type) {
4102 case LYXP_NODE_ELEM:
4103 case LYXP_NODE_TEXT:
4104 node = set->val.nodes[0].node;
4105 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004106 case LYXP_NODE_META:
4107 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004108 break;
4109 default:
4110 /* nothing to do with roots */
4111 set_fill_boolean(set, 0);
4112 return LY_SUCCESS;
4113 }
4114
Michal Vasko9f96a052020-03-10 09:41:45 +01004115 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004116 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004117 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004118 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004119 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004120 break;
4121 }
4122 }
4123
Michal Vasko9f96a052020-03-10 09:41:45 +01004124 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004125 break;
4126 }
4127 }
4128
4129 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004130 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004131 set_fill_boolean(set, 0);
4132 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004133 uint64_t i;
4134
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004135 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004136 for (i = 0; args[0]->val.str[i]; ++i) {
4137 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4138 set_fill_boolean(set, 0);
4139 break;
4140 }
4141 }
4142 if (!args[0]->val.str[i]) {
4143 if (!val[i] || (val[i] == '-')) {
4144 set_fill_boolean(set, 1);
4145 } else {
4146 set_fill_boolean(set, 0);
4147 }
4148 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004149 }
4150
4151 return LY_SUCCESS;
4152}
4153
4154/**
4155 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4156 * with the context size.
4157 *
4158 * @param[in] args Array of arguments.
4159 * @param[in] arg_count Count of elements in @p args.
4160 * @param[in,out] set Context and result set at the same time.
4161 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004162 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004163 */
4164static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004165xpath_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 +02004166{
4167 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004168 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004169 return LY_SUCCESS;
4170 }
4171
Michal Vasko03ff5a72019-09-11 13:49:33 +02004172 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004173 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004174 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004175 } else if (!set->used) {
4176 set_fill_number(set, 0);
4177 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004178 }
4179
4180 set_fill_number(set, set->ctx_size);
4181 return LY_SUCCESS;
4182}
4183
4184/**
4185 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4186 * with the node name without namespace from the argument or the context.
4187 *
4188 * @param[in] args Array of arguments.
4189 * @param[in] arg_count Count of elements in @p args.
4190 * @param[in,out] set Context and result set at the same time.
4191 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004192 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004193 */
4194static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004195xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004196{
4197 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004198
Michal Vasko03ff5a72019-09-11 13:49:33 +02004199 /* suppress unused variable warning */
4200 (void)options;
4201
4202 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004203 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004204 return LY_SUCCESS;
4205 }
4206
4207 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004208 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004209 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004210 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004211 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004212 } else if (!args[0]->used) {
4213 set_fill_string(set, "", 0);
4214 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004215 }
4216
4217 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004218 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004219
4220 item = &args[0]->val.nodes[0];
4221 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004222 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004223 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004224 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004225 } else if (!set->used) {
4226 set_fill_string(set, "", 0);
4227 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004228 }
4229
4230 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004231 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004232
4233 item = &set->val.nodes[0];
4234 }
4235
4236 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004237 case LYXP_NODE_NONE:
4238 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004239 case LYXP_NODE_ROOT:
4240 case LYXP_NODE_ROOT_CONFIG:
4241 case LYXP_NODE_TEXT:
4242 set_fill_string(set, "", 0);
4243 break;
4244 case LYXP_NODE_ELEM:
4245 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4246 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004247 case LYXP_NODE_META:
4248 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004249 break;
4250 }
4251
4252 return LY_SUCCESS;
4253}
4254
4255/**
4256 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4257 * with the node name fully qualified (with namespace) from the argument or the context.
4258 *
4259 * @param[in] args Array of arguments.
4260 * @param[in] arg_count Count of elements in @p args.
4261 * @param[in,out] set Context and result set at the same time.
4262 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004263 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004264 */
4265static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004266xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004267{
4268 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004269 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004270 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004271 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004272
4273 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004274 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004275 return LY_SUCCESS;
4276 }
4277
4278 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004279 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004280 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004281 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004282 } else if (!args[0]->used) {
4283 set_fill_string(set, "", 0);
4284 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004285 }
4286
4287 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004288 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004289
4290 item = &args[0]->val.nodes[0];
4291 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004292 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004293 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004294 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004295 } else if (!set->used) {
4296 set_fill_string(set, "", 0);
4297 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004298 }
4299
4300 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004301 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004302
4303 item = &set->val.nodes[0];
4304 }
4305
4306 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004307 case LYXP_NODE_NONE:
4308 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004309 case LYXP_NODE_ROOT:
4310 case LYXP_NODE_ROOT_CONFIG:
4311 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004312 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004313 break;
4314 case LYXP_NODE_ELEM:
4315 mod = item->node->schema->module;
4316 name = item->node->schema->name;
4317 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004318 case LYXP_NODE_META:
4319 mod = ((struct lyd_meta *)item->node)->annotation->module;
4320 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004321 break;
4322 }
4323
4324 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004325 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004326 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4327 set_fill_string(set, str, strlen(str));
4328 free(str);
4329 } else {
4330 set_fill_string(set, "", 0);
4331 }
4332
4333 return LY_SUCCESS;
4334}
4335
4336/**
4337 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4338 * with the namespace of the node from the argument or the context.
4339 *
4340 * @param[in] args Array of arguments.
4341 * @param[in] arg_count Count of elements in @p args.
4342 * @param[in,out] set Context and result set at the same time.
4343 * @param[in] options XPath options.
4344 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4345 */
4346static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004347xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004348{
4349 struct lyxp_set_node *item;
4350 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004351
Michal Vasko03ff5a72019-09-11 13:49:33 +02004352 /* suppress unused variable warning */
4353 (void)options;
4354
4355 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004356 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004357 return LY_SUCCESS;
4358 }
4359
4360 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004361 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004362 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004363 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004364 return LY_EVALID;
4365 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004366 set_fill_string(set, "", 0);
4367 return LY_SUCCESS;
4368 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004369
4370 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004371 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004372
4373 item = &args[0]->val.nodes[0];
4374 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004375 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004376 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004377 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004378 } else if (!set->used) {
4379 set_fill_string(set, "", 0);
4380 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004381 }
4382
4383 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004384 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004385
4386 item = &set->val.nodes[0];
4387 }
4388
4389 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004390 case LYXP_NODE_NONE:
4391 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004392 case LYXP_NODE_ROOT:
4393 case LYXP_NODE_ROOT_CONFIG:
4394 case LYXP_NODE_TEXT:
4395 set_fill_string(set, "", 0);
4396 break;
4397 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004398 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004399 if (item->type == LYXP_NODE_ELEM) {
4400 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004401 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004402 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004403 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004404 }
4405
4406 set_fill_string(set, mod->ns, strlen(mod->ns));
4407 break;
4408 }
4409
4410 return LY_SUCCESS;
4411}
4412
4413/**
4414 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4415 * with only nodes from the context. In practice it either leaves the context
4416 * as it is or returns an empty node set.
4417 *
4418 * @param[in] args Array of arguments.
4419 * @param[in] arg_count Count of elements in @p args.
4420 * @param[in,out] set Context and result set at the same time.
4421 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004422 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004423 */
4424static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004425xpath_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 +02004426{
4427 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004428 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004429 return LY_SUCCESS;
4430 }
4431
4432 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004433 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004434 }
4435 return LY_SUCCESS;
4436}
4437
4438/**
4439 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4440 * with normalized value (no leading, trailing, double white spaces) of the node
4441 * from the argument or the context.
4442 *
4443 * @param[in] args Array of arguments.
4444 * @param[in] arg_count Count of elements in @p args.
4445 * @param[in,out] set Context and result set at the same time.
4446 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004447 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004448 */
4449static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004450xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004451{
4452 uint16_t i, new_used;
4453 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004454 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004455 struct lysc_node_leaf *sleaf;
4456 LY_ERR rc = LY_SUCCESS;
4457
4458 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004459 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4460 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004461 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004462 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4463 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004464 } else if (!warn_is_string_type(sleaf->type)) {
4465 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004466 }
4467 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004468 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004469 return rc;
4470 }
4471
4472 if (arg_count) {
4473 set_fill_set(set, args[0]);
4474 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004475 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004476 LY_CHECK_RET(rc);
4477
4478 /* is there any normalization necessary? */
4479 for (i = 0; set->val.str[i]; ++i) {
4480 if (is_xmlws(set->val.str[i])) {
4481 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4482 have_spaces = 1;
4483 break;
4484 }
4485 space_before = 1;
4486 } else {
4487 space_before = 0;
4488 }
4489 }
4490
4491 /* yep, there is */
4492 if (have_spaces) {
4493 /* it's enough, at least one character will go, makes space for ending '\0' */
4494 new = malloc(strlen(set->val.str) * sizeof(char));
4495 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4496 new_used = 0;
4497
4498 space_before = 0;
4499 for (i = 0; set->val.str[i]; ++i) {
4500 if (is_xmlws(set->val.str[i])) {
4501 if ((i == 0) || space_before) {
4502 space_before = 1;
4503 continue;
4504 } else {
4505 space_before = 1;
4506 }
4507 } else {
4508 space_before = 0;
4509 }
4510
4511 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4512 ++new_used;
4513 }
4514
4515 /* at worst there is one trailing space now */
4516 if (new_used && is_xmlws(new[new_used - 1])) {
4517 --new_used;
4518 }
4519
4520 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4521 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4522 new[new_used] = '\0';
4523
4524 free(set->val.str);
4525 set->val.str = new;
4526 }
4527
4528 return LY_SUCCESS;
4529}
4530
4531/**
4532 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4533 * with the argument converted to boolean and logically inverted.
4534 *
4535 * @param[in] args Array of arguments.
4536 * @param[in] arg_count Count of elements in @p args.
4537 * @param[in,out] set Context and result set at the same time.
4538 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004539 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004540 */
4541static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004542xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004543{
4544 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004545 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004546 return LY_SUCCESS;
4547 }
4548
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004549 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004550 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004551 set_fill_boolean(set, 0);
4552 } else {
4553 set_fill_boolean(set, 1);
4554 }
4555
4556 return LY_SUCCESS;
4557}
4558
4559/**
4560 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4561 * with the number representation of either the argument or the context.
4562 *
4563 * @param[in] args Array of arguments.
4564 * @param[in] arg_count Count of elements in @p args.
4565 * @param[in,out] set Context and result set at the same time.
4566 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004567 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004568 */
4569static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004570xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004571{
4572 LY_ERR rc;
4573
4574 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004575 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004576 return LY_SUCCESS;
4577 }
4578
4579 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004580 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004581 LY_CHECK_RET(rc);
4582 set_fill_set(set, args[0]);
4583 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004584 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004585 LY_CHECK_RET(rc);
4586 }
4587
4588 return LY_SUCCESS;
4589}
4590
4591/**
4592 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4593 * with the context position.
4594 *
4595 * @param[in] args Array of arguments.
4596 * @param[in] arg_count Count of elements in @p args.
4597 * @param[in,out] set Context and result set at the same time.
4598 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004599 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004600 */
4601static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004602xpath_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 +02004603{
4604 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004605 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004606 return LY_SUCCESS;
4607 }
4608
Michal Vasko03ff5a72019-09-11 13:49:33 +02004609 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004610 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004611 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004612 } else if (!set->used) {
4613 set_fill_number(set, 0);
4614 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004615 }
4616
4617 set_fill_number(set, set->ctx_pos);
4618
4619 /* UNUSED in 'Release' build type */
4620 (void)options;
4621 return LY_SUCCESS;
4622}
4623
4624/**
4625 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4626 * depending on whether the second argument regex matches the first argument string. For details refer to
4627 * YANG 1.1 RFC section 10.2.1.
4628 *
4629 * @param[in] args Array of arguments.
4630 * @param[in] arg_count Count of elements in @p args.
4631 * @param[in,out] set Context and result set at the same time.
4632 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004633 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004634 */
4635static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004636xpath_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 +02004637{
4638 struct lysc_pattern **patterns = NULL, **pattern;
4639 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004640 LY_ERR rc = LY_SUCCESS;
4641 struct ly_err_item *err;
4642
4643 if (options & LYXP_SCNODE_ALL) {
4644 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4645 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4646 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 +02004647 } else if (!warn_is_string_type(sleaf->type)) {
4648 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004649 }
4650 }
4651
4652 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4653 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4654 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 +02004655 } else if (!warn_is_string_type(sleaf->type)) {
4656 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004657 }
4658 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004659 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004660 return rc;
4661 }
4662
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004663 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004664 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004665 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004666 LY_CHECK_RET(rc);
4667
4668 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek IÅ¡a45802b52021-02-09 09:21:58 +01004669 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004670 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004671 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Radek Krejciddace2c2021-01-08 11:30:56 +01004672 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004673 if (rc != LY_SUCCESS) {
4674 LY_ARRAY_FREE(patterns);
4675 return rc;
4676 }
4677
Radek Krejci0b013302021-03-29 15:22:32 +02004678 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004679 pcre2_code_free((*pattern)->code);
4680 free(*pattern);
4681 LY_ARRAY_FREE(patterns);
4682 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004683 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004684 ly_err_free(err);
4685 return rc;
4686 }
4687
4688 if (rc == LY_EVALID) {
4689 ly_err_free(err);
4690 set_fill_boolean(set, 0);
4691 } else {
4692 set_fill_boolean(set, 1);
4693 }
4694
4695 return LY_SUCCESS;
4696}
4697
4698/**
4699 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4700 * with the rounded first argument. For details refer to
4701 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4702 *
4703 * @param[in] args Array of arguments.
4704 * @param[in] arg_count Count of elements in @p args.
4705 * @param[in,out] set Context and result set at the same time.
4706 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004707 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004708 */
4709static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004710xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004711{
4712 struct lysc_node_leaf *sleaf;
4713 LY_ERR rc = LY_SUCCESS;
4714
4715 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004716 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004717 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004718 } else if ((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),
4721 sleaf->name);
4722 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4723 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4724 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004725 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004726 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004727 return rc;
4728 }
4729
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004730 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004731 LY_CHECK_RET(rc);
4732
4733 /* cover only the cases where floor can't be used */
4734 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4735 set_fill_number(set, -0.0f);
4736 } else {
4737 args[0]->val.num += 0.5;
4738 rc = xpath_floor(args, 1, args[0], options);
4739 LY_CHECK_RET(rc);
4740 set_fill_number(set, args[0]->val.num);
4741 }
4742
4743 return LY_SUCCESS;
4744}
4745
4746/**
4747 * @brief Execute the XPath starts-with(string, string) function.
4748 * Returns LYXP_SET_BOOLEAN whether the second argument is
4749 * the prefix of the first or not.
4750 *
4751 * @param[in] args Array of arguments.
4752 * @param[in] arg_count Count of elements in @p args.
4753 * @param[in,out] set Context and result set at the same time.
4754 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004755 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004756 */
4757static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004758xpath_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 +02004759{
4760 struct lysc_node_leaf *sleaf;
4761 LY_ERR rc = LY_SUCCESS;
4762
4763 if (options & LYXP_SCNODE_ALL) {
4764 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4765 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4766 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 +02004767 } else if (!warn_is_string_type(sleaf->type)) {
4768 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004769 }
4770 }
4771
4772 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4773 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4774 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 +02004775 } else if (!warn_is_string_type(sleaf->type)) {
4776 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004777 }
4778 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004779 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004780 return rc;
4781 }
4782
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004783 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004784 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004785 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004786 LY_CHECK_RET(rc);
4787
4788 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4789 set_fill_boolean(set, 0);
4790 } else {
4791 set_fill_boolean(set, 1);
4792 }
4793
4794 return LY_SUCCESS;
4795}
4796
4797/**
4798 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4799 * with the string representation of either the argument or the context.
4800 *
4801 * @param[in] args Array of arguments.
4802 * @param[in] arg_count Count of elements in @p args.
4803 * @param[in,out] set Context and result set at the same time.
4804 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004805 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004806 */
4807static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004808xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004809{
4810 LY_ERR rc;
4811
4812 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004813 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004814 return LY_SUCCESS;
4815 }
4816
4817 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004818 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004819 LY_CHECK_RET(rc);
4820 set_fill_set(set, args[0]);
4821 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004822 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004823 LY_CHECK_RET(rc);
4824 }
4825
4826 return LY_SUCCESS;
4827}
4828
4829/**
4830 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4831 * with the length of the string in either the argument or the context.
4832 *
4833 * @param[in] args Array of arguments.
4834 * @param[in] arg_count Count of elements in @p args.
4835 * @param[in,out] set Context and result set at the same time.
4836 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004837 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004838 */
4839static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004840xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004841{
4842 struct lysc_node_leaf *sleaf;
4843 LY_ERR rc = LY_SUCCESS;
4844
4845 if (options & LYXP_SCNODE_ALL) {
4846 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4847 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4848 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 +02004849 } else if (!warn_is_string_type(sleaf->type)) {
4850 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004851 }
4852 }
4853 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4854 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4855 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 +02004856 } else if (!warn_is_string_type(sleaf->type)) {
4857 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004858 }
4859 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004860 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004861 return rc;
4862 }
4863
4864 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004865 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004866 LY_CHECK_RET(rc);
4867 set_fill_number(set, strlen(args[0]->val.str));
4868 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004869 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004870 LY_CHECK_RET(rc);
4871 set_fill_number(set, strlen(set->val.str));
4872 }
4873
4874 return LY_SUCCESS;
4875}
4876
4877/**
4878 * @brief Execute the XPath substring(string, number, number?) function.
4879 * Returns LYXP_SET_STRING substring of the first argument starting
4880 * on the second argument index ending on the third argument index,
4881 * indexed from 1. For exact definition refer to
4882 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4883 *
4884 * @param[in] args Array of arguments.
4885 * @param[in] arg_count Count of elements in @p args.
4886 * @param[in,out] set Context and result set at the same time.
4887 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004888 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004889 */
4890static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004891xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004892{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004893 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004894 uint16_t str_start, str_len, pos;
4895 struct lysc_node_leaf *sleaf;
4896 LY_ERR rc = LY_SUCCESS;
4897
4898 if (options & LYXP_SCNODE_ALL) {
4899 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4900 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4901 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 +02004902 } else if (!warn_is_string_type(sleaf->type)) {
4903 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004904 }
4905 }
4906
4907 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4908 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4909 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 +02004910 } else if (!warn_is_numeric_type(sleaf->type)) {
4911 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004912 }
4913 }
4914
Michal Vasko69730152020-10-09 16:30:07 +02004915 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4916 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004917 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4918 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 +02004919 } else if (!warn_is_numeric_type(sleaf->type)) {
4920 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004921 }
4922 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004923 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004924 return rc;
4925 }
4926
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004927 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004928 LY_CHECK_RET(rc);
4929
4930 /* start */
4931 if (xpath_round(&args[1], 1, args[1], options)) {
4932 return -1;
4933 }
4934 if (isfinite(args[1]->val.num)) {
4935 start = args[1]->val.num - 1;
4936 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004937 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004938 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004939 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004940 }
4941
4942 /* len */
4943 if (arg_count == 3) {
4944 rc = xpath_round(&args[2], 1, args[2], options);
4945 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004946 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004947 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004948 } else if (isfinite(args[2]->val.num)) {
4949 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004950 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004951 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004952 }
4953 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004954 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004955 }
4956
4957 /* find matching character positions */
4958 str_start = 0;
4959 str_len = 0;
4960 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4961 if (pos < start) {
4962 ++str_start;
4963 } else if (pos < start + len) {
4964 ++str_len;
4965 } else {
4966 break;
4967 }
4968 }
4969
4970 set_fill_string(set, args[0]->val.str + str_start, str_len);
4971 return LY_SUCCESS;
4972}
4973
4974/**
4975 * @brief Execute the XPath substring-after(string, string) function.
4976 * Returns LYXP_SET_STRING with the string succeeding the occurance
4977 * of the second argument in the first or an empty string.
4978 *
4979 * @param[in] args Array of arguments.
4980 * @param[in] arg_count Count of elements in @p args.
4981 * @param[in,out] set Context and result set at the same time.
4982 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004983 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004984 */
4985static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004986xpath_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 +02004987{
4988 char *ptr;
4989 struct lysc_node_leaf *sleaf;
4990 LY_ERR rc = LY_SUCCESS;
4991
4992 if (options & LYXP_SCNODE_ALL) {
4993 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4994 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4995 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 +02004996 } else if (!warn_is_string_type(sleaf->type)) {
4997 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004998 }
4999 }
5000
5001 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5002 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5003 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 +02005004 } else if (!warn_is_string_type(sleaf->type)) {
5005 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005006 }
5007 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005008 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005009 return rc;
5010 }
5011
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005012 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005013 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005014 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005015 LY_CHECK_RET(rc);
5016
5017 ptr = strstr(args[0]->val.str, args[1]->val.str);
5018 if (ptr) {
5019 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5020 } else {
5021 set_fill_string(set, "", 0);
5022 }
5023
5024 return LY_SUCCESS;
5025}
5026
5027/**
5028 * @brief Execute the XPath substring-before(string, string) function.
5029 * Returns LYXP_SET_STRING with the string preceding the occurance
5030 * of the second argument in the first or an empty string.
5031 *
5032 * @param[in] args Array of arguments.
5033 * @param[in] arg_count Count of elements in @p args.
5034 * @param[in,out] set Context and result set at the same time.
5035 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005036 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005037 */
5038static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005039xpath_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 +02005040{
5041 char *ptr;
5042 struct lysc_node_leaf *sleaf;
5043 LY_ERR rc = LY_SUCCESS;
5044
5045 if (options & LYXP_SCNODE_ALL) {
5046 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5047 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5048 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 +02005049 } else if (!warn_is_string_type(sleaf->type)) {
5050 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005051 }
5052 }
5053
5054 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5055 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5056 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 +02005057 } else if (!warn_is_string_type(sleaf->type)) {
5058 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005059 }
5060 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005061 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005062 return rc;
5063 }
5064
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005065 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005066 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005067 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005068 LY_CHECK_RET(rc);
5069
5070 ptr = strstr(args[0]->val.str, args[1]->val.str);
5071 if (ptr) {
5072 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5073 } else {
5074 set_fill_string(set, "", 0);
5075 }
5076
5077 return LY_SUCCESS;
5078}
5079
5080/**
5081 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5082 * with the sum of all the nodes in the context.
5083 *
5084 * @param[in] args Array of arguments.
5085 * @param[in] arg_count Count of elements in @p args.
5086 * @param[in,out] set Context and result set at the same time.
5087 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005088 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005089 */
5090static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005091xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005092{
5093 long double num;
5094 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005095 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005096 struct lyxp_set set_item;
5097 struct lysc_node_leaf *sleaf;
5098 LY_ERR rc = LY_SUCCESS;
5099
5100 if (options & LYXP_SCNODE_ALL) {
5101 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5102 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005103 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005104 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5105 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5106 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005107 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005108 } else if (!warn_is_numeric_type(sleaf->type)) {
5109 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005110 }
5111 }
5112 }
5113 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005114 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005115 return rc;
5116 }
5117
5118 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005119
5120 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005121 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005122 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005123 } else if (!args[0]->used) {
5124 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005125 }
5126
Michal Vasko5c4e5892019-11-14 12:31:38 +01005127 set_init(&set_item, set);
5128
Michal Vasko03ff5a72019-09-11 13:49:33 +02005129 set_item.type = LYXP_SET_NODE_SET;
5130 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5131 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5132
5133 set_item.used = 1;
5134 set_item.size = 1;
5135
5136 for (i = 0; i < args[0]->used; ++i) {
5137 set_item.val.nodes[0] = args[0]->val.nodes[i];
5138
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005139 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005140 LY_CHECK_RET(rc);
5141 num = cast_string_to_number(str);
5142 free(str);
5143 set->val.num += num;
5144 }
5145
5146 free(set_item.val.nodes);
5147
5148 return LY_SUCCESS;
5149}
5150
5151/**
5152 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5153 * with the text content of the nodes in the context.
5154 *
5155 * @param[in] args Array of arguments.
5156 * @param[in] arg_count Count of elements in @p args.
5157 * @param[in,out] set Context and result set at the same time.
5158 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005159 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005160 */
5161static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005162xpath_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 +02005163{
5164 uint32_t i;
5165
5166 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005167 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005168 return LY_SUCCESS;
5169 }
5170
Michal Vasko03ff5a72019-09-11 13:49:33 +02005171 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005172 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005173 return LY_EVALID;
5174 }
5175
Michal Vaskod989ba02020-08-24 10:59:24 +02005176 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005177 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005178 case LYXP_NODE_NONE:
5179 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005180 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005181 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5182 set->val.nodes[i].type = LYXP_NODE_TEXT;
5183 ++i;
5184 break;
5185 }
Radek Krejci0f969882020-08-21 16:56:47 +02005186 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005187 case LYXP_NODE_ROOT:
5188 case LYXP_NODE_ROOT_CONFIG:
5189 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005190 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005191 set_remove_node(set, i);
5192 break;
5193 }
5194 }
5195
5196 return LY_SUCCESS;
5197}
5198
5199/**
5200 * @brief Execute the XPath translate(string, string, string) function.
5201 * Returns LYXP_SET_STRING with the first argument with the characters
5202 * from the second argument replaced by those on the corresponding
5203 * positions in the third argument.
5204 *
5205 * @param[in] args Array of arguments.
5206 * @param[in] arg_count Count of elements in @p args.
5207 * @param[in,out] set Context and result set at the same time.
5208 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005209 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005210 */
5211static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005212xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005213{
5214 uint16_t i, j, new_used;
5215 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005216 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005217 struct lysc_node_leaf *sleaf;
5218 LY_ERR rc = LY_SUCCESS;
5219
5220 if (options & LYXP_SCNODE_ALL) {
5221 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5222 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5223 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 +02005224 } else if (!warn_is_string_type(sleaf->type)) {
5225 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005226 }
5227 }
5228
5229 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5230 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5231 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 +02005232 } else if (!warn_is_string_type(sleaf->type)) {
5233 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005234 }
5235 }
5236
5237 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5238 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5239 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 +02005240 } else if (!warn_is_string_type(sleaf->type)) {
5241 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005242 }
5243 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005244 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005245 return rc;
5246 }
5247
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005248 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005249 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005250 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005251 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005252 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005253 LY_CHECK_RET(rc);
5254
5255 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5256 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5257 new_used = 0;
5258
5259 have_removed = 0;
5260 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005261 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005262
5263 for (j = 0; args[1]->val.str[j]; ++j) {
5264 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5265 /* removing this char */
5266 if (j >= strlen(args[2]->val.str)) {
5267 have_removed = 1;
5268 found = 1;
5269 break;
5270 }
5271 /* replacing this char */
5272 new[new_used] = args[2]->val.str[j];
5273 ++new_used;
5274 found = 1;
5275 break;
5276 }
5277 }
5278
5279 /* copying this char */
5280 if (!found) {
5281 new[new_used] = args[0]->val.str[i];
5282 ++new_used;
5283 }
5284 }
5285
5286 if (have_removed) {
5287 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5288 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5289 }
5290 new[new_used] = '\0';
5291
Michal Vaskod3678892020-05-21 10:06:58 +02005292 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005293 set->type = LYXP_SET_STRING;
5294 set->val.str = new;
5295
5296 return LY_SUCCESS;
5297}
5298
5299/**
5300 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5301 * with true value.
5302 *
5303 * @param[in] args Array of arguments.
5304 * @param[in] arg_count Count of elements in @p args.
5305 * @param[in,out] set Context and result set at the same time.
5306 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005307 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005308 */
5309static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005310xpath_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 +02005311{
5312 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005313 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005314 return LY_SUCCESS;
5315 }
5316
5317 set_fill_boolean(set, 1);
5318 return LY_SUCCESS;
5319}
5320
5321/*
5322 * moveto functions
5323 *
5324 * They and only they actually change the context (set).
5325 */
5326
5327/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005328 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005329 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005330 * XPath @p set is expected to be a (sc)node set!
5331 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005332 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5333 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005334 * @param[in] set Set with general XPath context.
5335 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005336 * @param[out] moveto_mod Expected module of a matching node.
5337 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005338 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005339static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005340moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5341 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005342{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005343 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005344 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005345 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005346
Michal Vasko2104e9f2020-03-06 08:23:25 +01005347 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5348
Michal Vasko6346ece2019-09-24 13:12:53 +02005349 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5350 /* specific module */
5351 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005352 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005353
Michal Vasko004d3152020-06-11 19:59:22 +02005354 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005355 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005356 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005357 return LY_EVALID;
5358 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005359
Michal Vasko6346ece2019-09-24 13:12:53 +02005360 *qname += pref_len + 1;
5361 *qname_len -= pref_len + 1;
5362 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5363 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005364 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005365 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005366 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005367 case LY_VALUE_SCHEMA:
5368 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005369 /* current module */
5370 mod = set->cur_mod;
5371 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005372 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005373 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005374 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005375 /* inherit parent (context node) module */
5376 if (ctx_scnode) {
5377 mod = ctx_scnode->module;
5378 } else {
5379 mod = NULL;
5380 }
5381 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005382 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005383 /* all nodes need to be prefixed */
5384 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5385 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005386 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005387 }
5388
Michal Vasko6346ece2019-09-24 13:12:53 +02005389 *moveto_mod = mod;
5390 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005391}
5392
5393/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005394 * @brief Move context @p set to the root. Handles absolute path.
5395 * Result is LYXP_SET_NODE_SET.
5396 *
5397 * @param[in,out] set Set to use.
5398 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005399 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005400 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005401static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005402moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005403{
aPiecek8b0cc152021-05-31 16:40:31 +02005404 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005405
5406 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005407 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskob0099a92020-08-31 14:55:23 +02005408 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005409 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005410 set->type = LYXP_SET_NODE_SET;
5411 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005412 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005413 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005414
5415 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005416}
5417
5418/**
5419 * @brief Check @p node as a part of NameTest processing.
5420 *
5421 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005422 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005423 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005424 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005425 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005426 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5427 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005428 */
5429static LY_ERR
Michal Vaskodb08ce52021-10-06 08:57:49 +02005430moveto_node_check(const struct lyd_node *node, const struct lyxp_set *set, const char *node_name,
5431 const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005432{
Michal Vaskodca9f122021-07-16 13:56:22 +02005433 if (!node->schema) {
5434 /* opaque node never matches */
5435 return LY_ENOT;
5436 }
5437
Michal Vasko03ff5a72019-09-11 13:49:33 +02005438 /* module check */
5439 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005440 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005441 }
5442
Michal Vasko5c4e5892019-11-14 12:31:38 +01005443 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005444 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005445 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005446 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5447 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005448 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005449 }
5450
5451 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005452 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005453 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005454 }
5455
Michal Vaskoa1424542019-11-14 16:08:52 +01005456 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005457 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005458 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005459 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005460
5461 /* match */
5462 return LY_SUCCESS;
5463}
5464
5465/**
5466 * @brief Check @p node as a part of schema NameTest processing.
5467 *
5468 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005469 * @param[in] ctx_scnode Context node.
5470 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005471 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005472 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005473 * @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 +02005474 */
5475static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005476moveto_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 +02005477 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005478{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005479 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5480 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005481 case LY_VALUE_SCHEMA:
5482 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005483 /* use current module */
5484 moveto_mod = set->cur_mod;
5485 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005486 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005487 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005488 /* inherit module of the context node, if any */
5489 if (ctx_scnode) {
5490 moveto_mod = ctx_scnode->module;
5491 }
5492 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005493 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005494 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005495 /* not defined */
5496 LOGINT(set->ctx);
5497 return LY_EINVAL;
5498 }
5499 }
5500
Michal Vasko03ff5a72019-09-11 13:49:33 +02005501 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005502 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005503 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005504 }
5505
5506 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005507 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005508 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005509 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005510 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005511 }
5512
5513 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005514 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005515 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005516 }
5517
5518 /* match */
5519 return LY_SUCCESS;
5520}
5521
5522/**
Michal Vaskod3678892020-05-21 10:06:58 +02005523 * @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 +02005524 *
5525 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005526 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005527 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005528 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005529 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5530 */
5531static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005532moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005533{
Michal Vaskof03ed032020-03-04 13:31:44 +01005534 uint32_t i;
Michal Vaskodb08ce52021-10-06 08:57:49 +02005535 const struct lyd_node *siblings, *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005536 LY_ERR rc;
5537
aPiecek8b0cc152021-05-31 16:40:31 +02005538 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005539 return LY_SUCCESS;
5540 }
5541
5542 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005543 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005544 return LY_EVALID;
5545 }
5546
Michal Vasko03ff5a72019-09-11 13:49:33 +02005547 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005548 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005549
5550 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 +01005551 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005552
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005553 /* search in all the trees */
Michal Vaskod3678892020-05-21 10:06:58 +02005554 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005555 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005556 /* search in children */
Michal Vaskodb08ce52021-10-06 08:57:49 +02005557 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005558 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005559
Michal Vaskod3678892020-05-21 10:06:58 +02005560 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskodb08ce52021-10-06 08:57:49 +02005561 rc = moveto_node_check(sub, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005562 if (rc == LY_SUCCESS) {
5563 if (!replaced) {
5564 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5565 replaced = 1;
5566 } else {
5567 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005568 }
Michal Vaskod3678892020-05-21 10:06:58 +02005569 ++i;
5570 } else if (rc == LY_EINCOMPLETE) {
5571 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005572 }
5573 }
5574
5575 if (!replaced) {
5576 /* no match */
5577 set_remove_node(set, i);
5578 }
5579 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005580
5581 return LY_SUCCESS;
5582}
5583
5584/**
Michal Vaskod3678892020-05-21 10:06:58 +02005585 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5586 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005587 *
5588 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005589 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005590 * @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 +01005591 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005592 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5593 */
5594static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005595moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5596 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005597{
Michal Vasko004d3152020-06-11 19:59:22 +02005598 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005599 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005600 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005601 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005602
Michal Vasko004d3152020-06-11 19:59:22 +02005603 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005604
aPiecek8b0cc152021-05-31 16:40:31 +02005605 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02005606 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005607 }
5608
5609 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005610 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005611 ret = LY_EVALID;
5612 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005613 }
5614
5615 /* context check for all the nodes since we have the schema node */
5616 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5617 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005618 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005619 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5620 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005621 lyxp_set_free_content(set);
5622 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005623 }
5624
5625 /* create specific data instance if needed */
5626 if (scnode->nodetype == LYS_LIST) {
5627 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5628 } else if (scnode->nodetype == LYS_LEAFLIST) {
5629 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005630 }
5631
5632 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005633 siblings = NULL;
5634
5635 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5636 assert(!set->val.nodes[i].node);
5637
5638 /* search in all the trees */
5639 siblings = set->tree;
5640 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5641 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005642 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005643 }
5644
5645 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005646 if (inst) {
5647 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005648 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005649 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005650 }
5651
5652 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005653 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005654 ret = LY_EINCOMPLETE;
5655 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005656 }
5657
5658 if (sub) {
5659 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005660 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005661 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005662 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005663 /* no match */
5664 set_remove_node(set, i);
5665 }
5666 }
5667
Michal Vasko004d3152020-06-11 19:59:22 +02005668cleanup:
5669 lyd_free_tree(inst);
5670 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005671}
5672
5673/**
5674 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5675 *
5676 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005677 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005678 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005679 * @param[in] options XPath options.
5680 * @return LY_ERR
5681 */
5682static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005683moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005684{
Radek Krejci857189e2020-09-01 13:26:36 +02005685 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005686 uint32_t getnext_opts;
5687 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005688 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005689 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005690 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005691
aPiecek8b0cc152021-05-31 16:40:31 +02005692 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005693 return LY_SUCCESS;
5694 }
5695
5696 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005697 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005698 return LY_EVALID;
5699 }
5700
Michal Vaskocafad9d2019-11-07 15:20:03 +01005701 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005702 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005703 if (options & LYXP_SCNODE_OUTPUT) {
5704 getnext_opts |= LYS_GETNEXT_OUTPUT;
5705 }
5706
Michal Vasko03ff5a72019-09-11 13:49:33 +02005707 orig_used = set->used;
5708 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005709 uint32_t idx;
5710
Radek Krejcif13b87b2020-12-01 22:02:17 +01005711 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5712 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005713 continue;
5714 }
5715
5716 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005717 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005718 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005719 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005720 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005721
5722 start_parent = set->val.scnodes[i].scnode;
5723
5724 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 +02005725 /* 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 +02005726 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005727 mod_idx = 0;
Michal Vaskoa51ef072021-07-02 10:40:30 +02005728 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005729 iter = NULL;
Michal Vasko919954a2021-07-26 09:21:42 +02005730 /* module may not be implemented or not compiled yet */
5731 while (mod->compiled && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005732 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005733 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5734
Michal Vasko03ff5a72019-09-11 13:49:33 +02005735 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005736 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005737 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005738 temp_ctx = 1;
5739 }
5740 }
5741 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005742 }
5743
Michal Vasko519fd602020-05-26 12:17:39 +02005744 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5745 iter = NULL;
5746 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005747 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005748 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5749
5750 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005751 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005752 temp_ctx = 1;
5753 }
5754 }
5755 }
5756 }
5757 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005758
5759 /* correct temporary in_ctx values */
5760 if (temp_ctx) {
5761 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005762 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5763 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005764 }
5765 }
5766 }
5767
5768 return LY_SUCCESS;
5769}
5770
5771/**
Michal Vaskod3678892020-05-21 10:06:58 +02005772 * @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 +02005773 * Context position aware.
5774 *
5775 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005776 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005777 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005778 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005779 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5780 */
5781static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005782moveto_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 +02005783{
5784 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005785 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005786 struct lyxp_set ret_set;
5787 LY_ERR rc;
5788
aPiecek8b0cc152021-05-31 16:40:31 +02005789 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005790 return LY_SUCCESS;
5791 }
5792
5793 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005794 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005795 return LY_EVALID;
5796 }
5797
Michal Vasko9f96a052020-03-10 09:41:45 +01005798 /* 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 +01005799 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005800 LY_CHECK_RET(rc);
5801
Michal Vasko6346ece2019-09-24 13:12:53 +02005802 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005803 set_init(&ret_set, set);
5804 for (i = 0; i < set->used; ++i) {
5805
5806 /* TREE DFS */
5807 start = set->val.nodes[i].node;
5808 for (elem = next = start; elem; elem = next) {
Michal Vaskodb08ce52021-10-06 08:57:49 +02005809 rc = moveto_node_check(elem, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005810 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005811 /* add matching node into result set */
5812 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5813 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5814 /* the node is a duplicate, we'll process it later in the set */
5815 goto skip_children;
5816 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005817 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005818 return rc;
5819 } else if (rc == LY_EINVAL) {
5820 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005821 }
5822
5823 /* TREE DFS NEXT ELEM */
5824 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005825 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005826 if (!next) {
5827skip_children:
5828 /* no children, so try siblings, but only if it's not the start,
5829 * that is considered to be the root and it's siblings are not traversed */
5830 if (elem != start) {
5831 next = elem->next;
5832 } else {
5833 break;
5834 }
5835 }
5836 while (!next) {
5837 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01005838 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005839 /* we are done, no next element to process */
5840 break;
5841 }
5842 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01005843 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005844 next = elem->next;
5845 }
5846 }
5847 }
5848
5849 /* make the temporary set the current one */
5850 ret_set.ctx_pos = set->ctx_pos;
5851 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005852 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005853 memcpy(set, &ret_set, sizeof *set);
5854
5855 return LY_SUCCESS;
5856}
5857
5858/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005859 * @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 +02005860 *
5861 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005862 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005863 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005864 * @param[in] options XPath options.
5865 * @return LY_ERR
5866 */
5867static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005868moveto_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 +02005869{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005870 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005871 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005872 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005873
aPiecek8b0cc152021-05-31 16:40:31 +02005874 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005875 return LY_SUCCESS;
5876 }
5877
5878 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005879 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005880 return LY_EVALID;
5881 }
5882
Michal Vasko03ff5a72019-09-11 13:49:33 +02005883 orig_used = set->used;
5884 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005885 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5886 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005887 continue;
5888 }
5889
5890 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005891 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005892 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005893 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005894 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005895
5896 /* TREE DFS */
5897 start = set->val.scnodes[i].scnode;
5898 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005899 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5900 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005901 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005902 }
5903
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005904 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005905 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005906 uint32_t idx;
5907
5908 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005909 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005910 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005911 /* we will process it later in the set */
5912 goto skip_children;
5913 }
5914 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005915 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005916 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005917 } else if (rc == LY_EINVAL) {
5918 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005919 }
5920
5921next_iter:
5922 /* TREE DFS NEXT ELEM */
5923 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005924 next = lysc_node_child(elem);
5925 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5926 next = next->next;
5927 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5928 next = next->next;
5929 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005930 if (!next) {
5931skip_children:
5932 /* no children, so try siblings, but only if it's not the start,
5933 * that is considered to be the root and it's siblings are not traversed */
5934 if (elem != start) {
5935 next = elem->next;
5936 } else {
5937 break;
5938 }
5939 }
5940 while (!next) {
5941 /* no siblings, go back through the parents */
5942 if (elem->parent == start) {
5943 /* we are done, no next element to process */
5944 break;
5945 }
5946 /* parent is already processed, go to its sibling */
5947 elem = elem->parent;
5948 next = elem->next;
5949 }
5950 }
5951 }
5952
5953 return LY_SUCCESS;
5954}
5955
5956/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005957 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005958 * Indirectly context position aware.
5959 *
5960 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005961 * @param[in] mod Matching metadata module, NULL for any.
5962 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02005963 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005964 * @return LY_ERR
5965 */
5966static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02005967moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005968{
Michal Vasko9f96a052020-03-10 09:41:45 +01005969 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005970
aPiecek8b0cc152021-05-31 16:40:31 +02005971 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005972 return LY_SUCCESS;
5973 }
5974
5975 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005976 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005977 return LY_EVALID;
5978 }
5979
Radek Krejci1deb5be2020-08-26 16:43:36 +02005980 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005981 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005982
5983 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5984 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005985 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005986 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005987
5988 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005989 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005990 continue;
5991 }
5992
Michal Vaskod3678892020-05-21 10:06:58 +02005993 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005994 /* match */
5995 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005996 set->val.meta[i].meta = sub;
5997 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005998 /* pos does not change */
5999 replaced = 1;
6000 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006001 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 +02006002 }
6003 ++i;
6004 }
6005 }
6006 }
6007
6008 if (!replaced) {
6009 /* no match */
6010 set_remove_node(set, i);
6011 }
6012 }
6013
6014 return LY_SUCCESS;
6015}
6016
6017/**
6018 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02006019 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006020 *
6021 * @param[in,out] set1 Set to use for the result.
6022 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006023 * @return LY_ERR
6024 */
6025static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006026moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006027{
6028 LY_ERR rc;
6029
Michal Vaskod3678892020-05-21 10:06:58 +02006030 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006031 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006032 return LY_EVALID;
6033 }
6034
6035 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02006036 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006037 return LY_SUCCESS;
6038 }
6039
Michal Vaskod3678892020-05-21 10:06:58 +02006040 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006041 memcpy(set1, set2, sizeof *set1);
6042 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02006043 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006044 return LY_SUCCESS;
6045 }
6046
6047 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006048 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006049
6050 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006051 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006052 LY_CHECK_RET(rc);
6053
6054 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006055 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006056
6057 return LY_SUCCESS;
6058}
6059
6060/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006061 * @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 +02006062 * Context position aware.
6063 *
6064 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006065 * @param[in] mod Matching metadata module, NULL for any.
6066 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006067 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006068 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6069 */
6070static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006071moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006072{
Michal Vasko9f96a052020-03-10 09:41:45 +01006073 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006074 struct lyxp_set *set_all_desc = NULL;
6075 LY_ERR rc;
6076
aPiecek8b0cc152021-05-31 16:40:31 +02006077 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006078 return LY_SUCCESS;
6079 }
6080
6081 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006082 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006083 return LY_EVALID;
6084 }
6085
Michal Vasko03ff5a72019-09-11 13:49:33 +02006086 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6087 * but it likely won't be used much, so it's a waste of time */
6088 /* copy the context */
6089 set_all_desc = set_copy(set);
6090 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006091 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006092 if (rc != LY_SUCCESS) {
6093 lyxp_set_free(set_all_desc);
6094 return rc;
6095 }
6096 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006097 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006098 if (rc != LY_SUCCESS) {
6099 lyxp_set_free(set_all_desc);
6100 return rc;
6101 }
6102 lyxp_set_free(set_all_desc);
6103
Radek Krejci1deb5be2020-08-26 16:43:36 +02006104 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006105 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006106
6107 /* only attributes of an elem can be in the result, skip all the rest,
6108 * we have all attributes qualified in lyd tree */
6109 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006110 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006111 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006112 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006113 continue;
6114 }
6115
Michal Vaskod3678892020-05-21 10:06:58 +02006116 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006117 /* match */
6118 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006119 set->val.meta[i].meta = sub;
6120 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006121 /* pos does not change */
6122 replaced = 1;
6123 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006124 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 +02006125 }
6126 ++i;
6127 }
6128 }
6129 }
6130
6131 if (!replaced) {
6132 /* no match */
6133 set_remove_node(set, i);
6134 }
6135 }
6136
6137 return LY_SUCCESS;
6138}
6139
6140/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006141 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6142 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006143 *
6144 * @param[in] parent Current parent.
6145 * @param[in] parent_pos Position of @p parent.
6146 * @param[in] parent_type Node type of @p parent.
6147 * @param[in,out] to_set Set to use.
6148 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006149 * @param[in] options XPath options.
6150 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6151 */
6152static LY_ERR
6153moveto_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 +02006154 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006155{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006156 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006157 LY_ERR rc;
6158
6159 switch (parent_type) {
6160 case LYXP_NODE_ROOT:
6161 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006162 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006163
Michal Vasko61ac2f62020-05-25 12:39:51 +02006164 /* add all top-level nodes as elements */
6165 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006166 break;
6167 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006168 /* add just the text node of this term element node */
6169 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006170 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6171 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6172 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006173 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006174 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006175
6176 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006177 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006178 break;
6179 default:
6180 LOGINT_RET(parent->schema->module->ctx);
6181 }
6182
Michal Vasko61ac2f62020-05-25 12:39:51 +02006183 /* add all top-level nodes as elements */
6184 LY_LIST_FOR(first, iter) {
6185 /* context check */
6186 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6187 continue;
6188 }
6189
6190 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006191 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006192 return LY_EINCOMPLETE;
6193 }
6194
6195 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6196 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6197
6198 /* also add all the children of this node, recursively */
6199 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6200 LY_CHECK_RET(rc);
6201 }
6202 }
6203
Michal Vasko03ff5a72019-09-11 13:49:33 +02006204 return LY_SUCCESS;
6205}
6206
6207/**
6208 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6209 * (or LYXP_SET_EMPTY). Context position aware.
6210 *
6211 * @param[in,out] set Set to use.
6212 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6213 * @param[in] options XPath options.
6214 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6215 */
6216static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006217moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006218{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006219 struct lyxp_set ret_set;
6220 LY_ERR rc;
6221
aPiecek8b0cc152021-05-31 16:40:31 +02006222 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006223 return LY_SUCCESS;
6224 }
6225
6226 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006227 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006228 return LY_EVALID;
6229 }
6230
6231 /* nothing to do */
6232 if (!all_desc) {
6233 return LY_SUCCESS;
6234 }
6235
Michal Vasko03ff5a72019-09-11 13:49:33 +02006236 /* add all the children, they get added recursively */
6237 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006238 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006239 /* copy the current node to tmp */
6240 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6241
6242 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006243 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006244 continue;
6245 }
6246
Michal Vasko03ff5a72019-09-11 13:49:33 +02006247 /* add all the children */
6248 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 +02006249 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006250 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006251 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006252 return rc;
6253 }
6254 }
6255
6256 /* use the temporary set as the current one */
6257 ret_set.ctx_pos = set->ctx_pos;
6258 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006259 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006260 memcpy(set, &ret_set, sizeof *set);
6261
6262 return LY_SUCCESS;
6263}
6264
6265/**
6266 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6267 * (or LYXP_SET_EMPTY).
6268 *
6269 * @param[in,out] set Set to use.
6270 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6271 * @param[in] options XPath options.
6272 * @return LY_ERR
6273 */
6274static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006275moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006276{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006277 uint32_t getnext_opts;
6278 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006279 const struct lysc_node *iter, *start_parent;
6280 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006281
aPiecek8b0cc152021-05-31 16:40:31 +02006282 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006283 return LY_SUCCESS;
6284 }
6285
6286 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006287 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006288 return LY_EVALID;
6289 }
6290
Michal Vasko519fd602020-05-26 12:17:39 +02006291 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006292 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006293 if (options & LYXP_SCNODE_OUTPUT) {
6294 getnext_opts |= LYS_GETNEXT_OUTPUT;
6295 }
6296
6297 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006298 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006299 if (!all_desc) {
6300 /* traverse the start node */
6301 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6302 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6303 }
6304 continue;
6305 }
6306
Radek Krejcif13b87b2020-12-01 22:02:17 +01006307 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6308 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006309 continue;
6310 }
6311
Michal Vasko519fd602020-05-26 12:17:39 +02006312 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006313 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006314 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006315 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006316 }
6317
Michal Vasko519fd602020-05-26 12:17:39 +02006318 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006319
Michal Vasko519fd602020-05-26 12:17:39 +02006320 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6321 /* it can actually be in any module, it's all <running> */
6322 mod_idx = 0;
Michal Vaskoa51ef072021-07-02 10:40:30 +02006323 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02006324 iter = NULL;
6325 /* module may not be implemented */
6326 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6327 /* context check */
6328 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6329 continue;
6330 }
6331
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006332 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006333 /* throw away the insert index, we want to consider that node again, recursively */
6334 }
6335 }
6336
6337 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6338 iter = NULL;
6339 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006340 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006341 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006342 continue;
6343 }
6344
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006345 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006346 }
6347 }
6348 }
6349
6350 return LY_SUCCESS;
6351}
6352
6353/**
6354 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6355 * (or LYXP_SET_EMPTY). Context position aware.
6356 *
6357 * @param[in] set Set to use.
6358 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6359 * @param[in] options XPath options.
6360 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6361 */
6362static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006363moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006364{
6365 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006366 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006367 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006368
aPiecek8b0cc152021-05-31 16:40:31 +02006369 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006370 return LY_SUCCESS;
6371 }
6372
6373 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006374 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006375 return LY_EVALID;
6376 }
6377
6378 if (all_desc) {
6379 /* <path>//.. == <path>//./.. */
6380 rc = moveto_self(set, 1, options);
6381 LY_CHECK_RET(rc);
6382 }
6383
Radek Krejci1deb5be2020-08-26 16:43:36 +02006384 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006385 node = set->val.nodes[i].node;
6386
6387 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9e685082021-01-29 14:49:09 +01006388 new_node = lyd_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006389 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6390 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006391 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6392 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006393 if (!new_node) {
6394 LOGINT_RET(set->ctx);
6395 }
6396 } else {
6397 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006398 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006399 continue;
6400 }
6401
Michal Vaskoa1424542019-11-14 16:08:52 +01006402 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006403 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 +02006404 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006405 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006406
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006407 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006408 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006409 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006410
Michal Vasko03ff5a72019-09-11 13:49:33 +02006411 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006412 /* 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 +02006413 new_type = LYXP_NODE_ELEM;
6414 }
6415
Michal Vasko03ff5a72019-09-11 13:49:33 +02006416 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006417 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006418 } else {
6419 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006420 }
6421 }
6422
Michal Vasko2caefc12019-11-14 16:07:56 +01006423 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006424 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006425
6426 return LY_SUCCESS;
6427}
6428
6429/**
6430 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6431 * (or LYXP_SET_EMPTY).
6432 *
6433 * @param[in] set Set to use.
6434 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6435 * @param[in] options XPath options.
6436 * @return LY_ERR
6437 */
6438static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006439moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006440{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006441 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006442 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006443 const struct lysc_node *node, *new_node;
6444 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006445
aPiecek8b0cc152021-05-31 16:40:31 +02006446 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006447 return LY_SUCCESS;
6448 }
6449
6450 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006451 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006452 return LY_EVALID;
6453 }
6454
6455 if (all_desc) {
6456 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006457 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006458 }
6459
Michal Vasko03ff5a72019-09-11 13:49:33 +02006460 orig_used = set->used;
6461 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006462 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6463 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006464 continue;
6465 }
6466
6467 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006468 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006469 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006470 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006471 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006472
6473 node = set->val.scnodes[i].scnode;
6474
6475 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006476 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006477 } else {
6478 /* root does not have a parent */
6479 continue;
6480 }
6481
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006482 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006483 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006484 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006485
Michal Vasko03ff5a72019-09-11 13:49:33 +02006486 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006487 /* 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 +02006488 new_type = LYXP_NODE_ELEM;
6489 }
6490
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006491 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6492 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006493 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006494 temp_ctx = 1;
6495 }
6496 }
6497
6498 if (temp_ctx) {
6499 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006500 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6501 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006502 }
6503 }
6504 }
6505
6506 return LY_SUCCESS;
6507}
6508
6509/**
6510 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6511 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6512 *
6513 * @param[in,out] set1 Set to use for the result.
6514 * @param[in] set2 Set acting as the second operand for @p op.
6515 * @param[in] op Comparison operator to process.
6516 * @param[in] options XPath options.
6517 * @return LY_ERR
6518 */
6519static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006520moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006521{
6522 /*
6523 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6524 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6525 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6526 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6527 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6528 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6529 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6530 *
6531 * '=' or '!='
6532 * BOOLEAN + BOOLEAN
6533 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6534 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6535 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6536 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6537 * NUMBER + NUMBER
6538 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6539 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6540 * STRING + STRING
6541 *
6542 * '<=', '<', '>=', '>'
6543 * NUMBER + NUMBER
6544 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6545 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6546 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6547 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6548 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6549 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6550 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6551 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6552 */
6553 struct lyxp_set iter1, iter2;
6554 int result;
6555 int64_t i;
6556 LY_ERR rc;
6557
Michal Vasko004d3152020-06-11 19:59:22 +02006558 memset(&iter1, 0, sizeof iter1);
6559 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006560
6561 /* iterative evaluation with node-sets */
6562 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6563 if (set1->type == LYXP_SET_NODE_SET) {
6564 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006565 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006566 switch (set2->type) {
6567 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006568 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006569 break;
6570 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006571 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006572 break;
6573 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006574 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006575 break;
6576 }
6577 LY_CHECK_RET(rc);
6578
Michal Vasko4c7763f2020-07-27 17:40:37 +02006579 /* canonize set2 */
6580 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6581
6582 /* compare recursively */
6583 rc = moveto_op_comp(&iter1, &iter2, op, options);
6584 lyxp_set_free_content(&iter2);
6585 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006586
6587 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006588 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006589 set_fill_boolean(set1, 1);
6590 return LY_SUCCESS;
6591 }
6592 }
6593 } else {
6594 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006595 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006596 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006597 case LYXP_SET_NUMBER:
6598 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6599 break;
6600 case LYXP_SET_BOOLEAN:
6601 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6602 break;
6603 default:
6604 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6605 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006606 }
6607 LY_CHECK_RET(rc);
6608
Michal Vasko4c7763f2020-07-27 17:40:37 +02006609 /* canonize set1 */
6610 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 +02006611
Michal Vasko4c7763f2020-07-27 17:40:37 +02006612 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006613 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006614 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006615 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006616
6617 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006618 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006619 set_fill_boolean(set1, 1);
6620 return LY_SUCCESS;
6621 }
6622 }
6623 }
6624
6625 /* false for all nodes */
6626 set_fill_boolean(set1, 0);
6627 return LY_SUCCESS;
6628 }
6629
6630 /* first convert properly */
6631 if ((op[0] == '=') || (op[0] == '!')) {
6632 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006633 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6634 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006635 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006636 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006637 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006638 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006639 LY_CHECK_RET(rc);
6640 } /* else we have 2 strings */
6641 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006642 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006643 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006644 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006645 LY_CHECK_RET(rc);
6646 }
6647
6648 assert(set1->type == set2->type);
6649
6650 /* compute result */
6651 if (op[0] == '=') {
6652 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006653 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006654 } else if (set1->type == LYXP_SET_NUMBER) {
6655 result = (set1->val.num == set2->val.num);
6656 } else {
6657 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006658 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006659 }
6660 } else if (op[0] == '!') {
6661 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006662 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006663 } else if (set1->type == LYXP_SET_NUMBER) {
6664 result = (set1->val.num != set2->val.num);
6665 } else {
6666 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006667 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006668 }
6669 } else {
6670 assert(set1->type == LYXP_SET_NUMBER);
6671 if (op[0] == '<') {
6672 if (op[1] == '=') {
6673 result = (set1->val.num <= set2->val.num);
6674 } else {
6675 result = (set1->val.num < set2->val.num);
6676 }
6677 } else {
6678 if (op[1] == '=') {
6679 result = (set1->val.num >= set2->val.num);
6680 } else {
6681 result = (set1->val.num > set2->val.num);
6682 }
6683 }
6684 }
6685
6686 /* assign result */
6687 if (result) {
6688 set_fill_boolean(set1, 1);
6689 } else {
6690 set_fill_boolean(set1, 0);
6691 }
6692
6693 return LY_SUCCESS;
6694}
6695
6696/**
6697 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6698 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6699 *
6700 * @param[in,out] set1 Set to use for the result.
6701 * @param[in] set2 Set acting as the second operand for @p op.
6702 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006703 * @return LY_ERR
6704 */
6705static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006706moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006707{
6708 LY_ERR rc;
6709
6710 /* unary '-' */
6711 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006712 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006713 LY_CHECK_RET(rc);
6714 set1->val.num *= -1;
6715 lyxp_set_free(set2);
6716 return LY_SUCCESS;
6717 }
6718
6719 assert(set1 && set2);
6720
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006721 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006722 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006723 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006724 LY_CHECK_RET(rc);
6725
6726 switch (op[0]) {
6727 /* '+' */
6728 case '+':
6729 set1->val.num += set2->val.num;
6730 break;
6731
6732 /* '-' */
6733 case '-':
6734 set1->val.num -= set2->val.num;
6735 break;
6736
6737 /* '*' */
6738 case '*':
6739 set1->val.num *= set2->val.num;
6740 break;
6741
6742 /* 'div' */
6743 case 'd':
6744 set1->val.num /= set2->val.num;
6745 break;
6746
6747 /* 'mod' */
6748 case 'm':
6749 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6750 break;
6751
6752 default:
6753 LOGINT_RET(set1->ctx);
6754 }
6755
6756 return LY_SUCCESS;
6757}
6758
6759/*
6760 * eval functions
6761 *
6762 * They execute a parsed XPath expression on some data subtree.
6763 */
6764
6765/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006766 * @brief Evaluate Predicate. Logs directly on error.
6767 *
Michal Vaskod3678892020-05-21 10:06:58 +02006768 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006769 *
6770 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006771 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02006772 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006773 * @param[in] options XPath options.
6774 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6775 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6776 */
6777static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006778eval_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 +02006779{
6780 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006781 uint16_t orig_exp;
6782 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006783 int32_t pred_in_ctx;
aPiecekfff4dca2021-10-07 10:59:53 +02006784 struct lyxp_set set2 = {0};
Michal Vasko03ff5a72019-09-11 13:49:33 +02006785 struct lyd_node *orig_parent;
6786
6787 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02006788 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006789 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006790 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006791
aPiecek8b0cc152021-05-31 16:40:31 +02006792 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006793only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02006794 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006795 LY_CHECK_RET(rc);
6796 } else if (set->type == LYXP_SET_NODE_SET) {
6797 /* 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 +01006798 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006799
6800 /* empty set, nothing to evaluate */
6801 if (!set->used) {
6802 goto only_parse;
6803 }
6804
Michal Vasko004d3152020-06-11 19:59:22 +02006805 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006806 orig_pos = 0;
6807 orig_size = set->used;
6808 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006809 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006810 set_init(&set2, set);
6811 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6812 /* remember the node context position for position() and context size for last(),
6813 * predicates should always be evaluated with respect to the child axis (since we do
6814 * not support explicit axes) so we assign positions based on their parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006815 if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) {
6816 orig_parent = lyd_parent(set->val.nodes[i].node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006817 orig_pos = 1;
6818 } else {
6819 ++orig_pos;
6820 }
6821
6822 set2.ctx_pos = orig_pos;
6823 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006824 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006825
Michal Vasko004d3152020-06-11 19:59:22 +02006826 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006827 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006828 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006829 return rc;
6830 }
6831
6832 /* number is a position */
6833 if (set2.type == LYXP_SET_NUMBER) {
6834 if ((long long)set2.val.num == orig_pos) {
6835 set2.val.num = 1;
6836 } else {
6837 set2.val.num = 0;
6838 }
6839 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006840 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006841
6842 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006843 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006844 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006845 }
6846 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006847 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006848
6849 } else if (set->type == LYXP_SET_SCNODE_SET) {
6850 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006851 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006852 /* there is a currently-valid node */
6853 break;
6854 }
6855 }
6856 /* empty set, nothing to evaluate */
6857 if (i == set->used) {
6858 goto only_parse;
6859 }
6860
Michal Vasko004d3152020-06-11 19:59:22 +02006861 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006862
Michal Vasko03ff5a72019-09-11 13:49:33 +02006863 /* set special in_ctx to all the valid snodes */
6864 pred_in_ctx = set_scnode_new_in_ctx(set);
6865
6866 /* use the valid snodes one-by-one */
6867 for (i = 0; i < set->used; ++i) {
6868 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6869 continue;
6870 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006871 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006872
Michal Vasko004d3152020-06-11 19:59:22 +02006873 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006874
Michal Vasko004d3152020-06-11 19:59:22 +02006875 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006876 LY_CHECK_RET(rc);
6877
6878 set->val.scnodes[i].in_ctx = pred_in_ctx;
6879 }
6880
6881 /* restore the state as it was before the predicate */
6882 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006883 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02006884 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006885 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006886 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006887 }
6888 }
6889
6890 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006891 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006892 set_fill_set(&set2, set);
6893
Michal Vasko004d3152020-06-11 19:59:22 +02006894 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006895 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006896 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006897 return rc;
6898 }
6899
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006900 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006901 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006902 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006903 }
Michal Vaskod3678892020-05-21 10:06:58 +02006904 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006905 }
6906
6907 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006908 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02006909 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006910 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006911 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006912
6913 return LY_SUCCESS;
6914}
6915
6916/**
Michal Vaskod3678892020-05-21 10:06:58 +02006917 * @brief Evaluate Literal. Logs directly on error.
6918 *
6919 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006920 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006921 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6922 */
6923static void
Michal Vasko40308e72020-10-20 16:38:40 +02006924eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006925{
6926 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006927 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006928 set_fill_string(set, "", 0);
6929 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006930 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 +02006931 }
6932 }
6933 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006934 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006935 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006936}
6937
6938/**
Michal Vasko004d3152020-06-11 19:59:22 +02006939 * @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 +02006940 *
Michal Vasko004d3152020-06-11 19:59:22 +02006941 * @param[in] exp Full parsed XPath expression.
6942 * @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 +02006943 * @param[in] ctx_node Found schema node as the context for the predicate.
6944 * @param[in] cur_mod Current module for the expression.
6945 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006946 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006947 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006948 * @param[out] predicates Parsed predicates.
6949 * @param[out] pred_type Type of @p predicates.
6950 * @return LY_SUCCESS on success,
6951 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006952 */
6953static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006954eval_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 +02006955 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 +02006956 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006957{
Michal Vasko004d3152020-06-11 19:59:22 +02006958 LY_ERR ret = LY_SUCCESS;
6959 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006960 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006961 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006962 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006963 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006964
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006965 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006966
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006967 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006968 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006969 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006970 return LY_EINVAL;
6971 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006972 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 +02006973 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006974
Michal Vasko004d3152020-06-11 19:59:22 +02006975 /* learn where the predicates end */
6976 e_idx = *tok_idx;
6977 while (key_count) {
6978 /* '[' */
6979 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6980 return LY_EINVAL;
6981 }
6982 ++e_idx;
6983
Michal Vasko3354d272021-04-06 09:40:06 +02006984 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
6985 /* definitely not a key */
6986 return LY_EINVAL;
6987 }
6988
Michal Vasko004d3152020-06-11 19:59:22 +02006989 /* ']' */
6990 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6991 ++e_idx;
6992 }
6993 ++e_idx;
6994
6995 /* another presumably key predicate parsed */
6996 --key_count;
6997 }
Michal Vasko004d3152020-06-11 19:59:22 +02006998 } else {
6999 /* learn just where this single predicate ends */
7000 e_idx = *tok_idx;
7001
Michal Vaskod3678892020-05-21 10:06:58 +02007002 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007003 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7004 return LY_EINVAL;
7005 }
7006 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007007
Michal Vasko3354d272021-04-06 09:40:06 +02007008 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
7009 /* definitely not the value */
7010 return LY_EINVAL;
7011 }
7012
Michal Vaskod3678892020-05-21 10:06:58 +02007013 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007014 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7015 ++e_idx;
7016 }
7017 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007018 }
7019
Michal Vasko004d3152020-06-11 19:59:22 +02007020 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
7021 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
7022
7023 /* turn logging off */
7024 prev_lo = ly_log_options(0);
7025
7026 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007027 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
7028 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007029
7030 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007031 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
7032 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02007033 LY_CHECK_GOTO(ret, cleanup);
7034
7035 /* success, the predicate must include all the needed information for hash-based search */
7036 *tok_idx = e_idx;
7037
7038cleanup:
7039 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007040 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02007041 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02007042}
7043
7044/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007045 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7046 * data node(s) could be found using a single hash-based search.
7047 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007048 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007049 * @param[in] node Next context node to check.
7050 * @param[in] name Expected node name.
7051 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007052 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007053 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007054 * @param[in] format Prefix format.
7055 * @param[in,out] found Previously found node, is updated.
7056 * @return LY_SUCCESS on success,
7057 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7058 */
7059static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007060eval_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 +02007061 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 +02007062 const struct lysc_node **found)
7063{
7064 const struct lysc_node *scnode;
7065 const struct lys_module *mod;
7066 uint32_t idx = 0;
7067
Radek Krejci8df109d2021-04-23 12:19:08 +02007068 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007069
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007070continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007071 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007072 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007073 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007074 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007075 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007076 if (!mod->implemented) {
7077 continue;
7078 }
7079
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007080 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7081 if (scnode) {
7082 /* we have found a match */
7083 break;
7084 }
7085 }
7086
Michal Vasko7d1d0912020-10-16 08:38:30 +02007087 if (!scnode) {
7088 /* all modules searched */
7089 idx = 0;
7090 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007091 } else {
7092 /* search in top-level */
7093 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7094 }
7095 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007096 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007097 /* we must adjust the module to inherit the one from the context node */
7098 moveto_mod = node->schema->module;
7099 }
7100
7101 /* search in children, do not repeat the same search */
7102 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007103 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007104
7105 /* additional context check */
7106 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7107 scnode = NULL;
7108 }
7109
7110 if (scnode) {
7111 if (*found) {
7112 /* we found a schema node with the same name but at different level, give up, too complicated
7113 * (more hash-based searches would be required, not supported) */
7114 return LY_ENOT;
7115 } else {
7116 /* remember the found schema node and continue to make sure it can be used */
7117 *found = scnode;
7118 }
7119 }
7120
7121 if (idx) {
7122 /* continue searching all the following models */
7123 goto continue_search;
7124 }
7125
7126 return LY_SUCCESS;
7127}
7128
7129/**
Michal Vaskod3678892020-05-21 10:06:58 +02007130 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7131 *
7132 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7133 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7134 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7135 *
7136 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007137 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007138 * @param[in] attr_axis Whether to search attributes or standard nodes.
7139 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007140 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007141 * @param[in] options XPath options.
7142 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7143 */
7144static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007145eval_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 +02007146 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007147{
Michal Vaskod3678892020-05-21 10:06:58 +02007148 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007149 const char *ncname, *ncname_dict = NULL;
7150 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007151 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007152 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007153 struct ly_path_predicate *predicates = NULL;
7154 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007155 LY_ERR rc = LY_SUCCESS;
7156
aPiecek8b0cc152021-05-31 16:40:31 +02007157 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007158 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007159 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007160
aPiecek8b0cc152021-05-31 16:40:31 +02007161 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02007162 goto moveto;
7163 }
7164
Michal Vasko004d3152020-06-11 19:59:22 +02007165 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7166 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007167
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007168 if ((ncname[0] == '*') && (ncname_len == 1)) {
7169 /* all nodes will match */
7170 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007171 }
7172
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007173 /* parse (and skip) module name */
7174 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007175 LY_CHECK_GOTO(rc, cleanup);
7176
Radek Krejci8df109d2021-04-23 12:19:08 +02007177 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 +02007178 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007179 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007180 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7181 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007182 /* check failed */
7183 scnode = NULL;
7184 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007185 }
7186 }
7187
Michal Vasko004d3152020-06-11 19:59:22 +02007188 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7189 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007190 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7191 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007192 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007193 scnode = NULL;
7194 }
7195 }
7196 }
7197
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007198 if (!scnode) {
7199 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007200 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007201 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007202 }
7203
7204moveto:
7205 /* move to the attribute(s), data node(s), or schema node(s) */
7206 if (attr_axis) {
aPiecek8b0cc152021-05-31 16:40:31 +02007207 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007208 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007209 } else {
7210 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007211 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007212 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007213 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007214 }
7215 LY_CHECK_GOTO(rc, cleanup);
7216 }
7217 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007218 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007219 int64_t i;
7220
Michal Vaskod3678892020-05-21 10:06:58 +02007221 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007222 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007223 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007224 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007225 }
7226 LY_CHECK_GOTO(rc, cleanup);
7227
7228 for (i = set->used - 1; i > -1; --i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007229 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vaskod3678892020-05-21 10:06:58 +02007230 break;
7231 }
7232 }
7233 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007234 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vasko90f12dc2020-12-03 14:20:42 +01007235 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".",
7236 ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007237 free(path);
7238 }
7239 } else {
7240 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007241 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007242 } else {
7243 if (scnode) {
7244 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007245 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007246 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007247 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007248 }
7249 }
7250 LY_CHECK_GOTO(rc, cleanup);
7251 }
7252 }
7253
7254 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007255 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7256 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007257 LY_CHECK_RET(rc);
7258 }
7259
7260cleanup:
aPiecek8b0cc152021-05-31 16:40:31 +02007261 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007262 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007263 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007264 }
Michal Vaskod3678892020-05-21 10:06:58 +02007265 return rc;
7266}
7267
7268/**
7269 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7270 *
7271 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7272 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7273 * [8] NodeType ::= 'text' | 'node'
7274 *
7275 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007276 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007277 * @param[in] attr_axis Whether to search attributes or standard nodes.
7278 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007279 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007280 * @param[in] options XPath options.
7281 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7282 */
7283static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007284eval_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 +02007285 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007286{
7287 LY_ERR rc;
7288
7289 /* TODO */
7290 (void)attr_axis;
7291 (void)all_desc;
7292
aPiecek8b0cc152021-05-31 16:40:31 +02007293 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007294 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007295 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007296 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
aPiecek8b0cc152021-05-31 16:40:31 +02007297 options |= LYXP_SKIP_EXPR;
Michal Vaskod3678892020-05-21 10:06:58 +02007298 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007299 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007300 rc = xpath_node(NULL, 0, set, options);
7301 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007302 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007303 rc = xpath_text(NULL, 0, set, options);
7304 }
7305 LY_CHECK_RET(rc);
7306 }
7307 }
aPiecek8b0cc152021-05-31 16:40:31 +02007308 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007309 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007310 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007311
7312 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007313 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007314 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007315 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007316 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007317
7318 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007319 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007320 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007321 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007322 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007323
7324 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007325 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7326 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007327 LY_CHECK_RET(rc);
7328 }
7329
7330 return LY_SUCCESS;
7331}
7332
7333/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007334 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7335 *
7336 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7337 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007338 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007339 *
7340 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007341 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007342 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007343 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007344 * @param[in] options XPath options.
7345 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7346 */
7347static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007348eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7349 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007350{
Radek Krejci857189e2020-09-01 13:26:36 +02007351 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007352 LY_ERR rc;
7353
7354 goto step;
7355 do {
7356 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007357 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007358 all_desc = 0;
7359 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007360 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007361 all_desc = 1;
7362 }
aPiecek8b0cc152021-05-31 16:40:31 +02007363 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007364 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007365 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007366
7367step:
Michal Vaskod3678892020-05-21 10:06:58 +02007368 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007369 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007370 attr_axis = 1;
7371
aPiecek8b0cc152021-05-31 16:40:31 +02007372 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007373 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007374 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007375 } else {
7376 attr_axis = 0;
7377 }
7378
Michal Vasko03ff5a72019-09-11 13:49:33 +02007379 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007380 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007381 case LYXP_TOKEN_DOT:
7382 /* evaluate '.' */
aPiecek8b0cc152021-05-31 16:40:31 +02007383 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007384 rc = moveto_scnode_self(set, all_desc, options);
7385 } else {
7386 rc = moveto_self(set, all_desc, options);
7387 }
7388 LY_CHECK_RET(rc);
7389 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007390 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007391 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007392 break;
7393
7394 case LYXP_TOKEN_DDOT:
7395 /* evaluate '..' */
aPiecek8b0cc152021-05-31 16:40:31 +02007396 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007397 rc = moveto_scnode_parent(set, all_desc, options);
7398 } else {
7399 rc = moveto_parent(set, all_desc, options);
7400 }
7401 LY_CHECK_RET(rc);
aPiecek8b0cc152021-05-31 16:40:31 +02007402 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007403 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007404 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007405 break;
7406
Michal Vasko03ff5a72019-09-11 13:49:33 +02007407 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007408 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007409 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007410 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007411 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007412
Michal Vaskod3678892020-05-21 10:06:58 +02007413 case LYXP_TOKEN_NODETYPE:
7414 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007415 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007416 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007417 break;
7418
7419 default:
aPiecek8b0cc152021-05-31 16:40:31 +02007420 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007421 }
Michal Vasko004d3152020-06-11 19:59:22 +02007422 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007423
7424 return LY_SUCCESS;
7425}
7426
7427/**
7428 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7429 *
7430 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7431 *
7432 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007433 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007434 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007435 * @param[in] options XPath options.
7436 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7437 */
7438static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007439eval_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 +02007440{
Radek Krejci857189e2020-09-01 13:26:36 +02007441 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007442
aPiecek8b0cc152021-05-31 16:40:31 +02007443 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007444 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007445 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007446 }
7447
7448 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007449 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007450 /* evaluate '/' - deferred */
7451 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02007452 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007453 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007454 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007455
Michal Vasko004d3152020-06-11 19:59:22 +02007456 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007457 return LY_SUCCESS;
7458 }
Michal Vasko004d3152020-06-11 19:59:22 +02007459 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007460 case LYXP_TOKEN_DOT:
7461 case LYXP_TOKEN_DDOT:
7462 case LYXP_TOKEN_AT:
7463 case LYXP_TOKEN_NAMETEST:
7464 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007465 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007466 break;
7467 default:
7468 break;
7469 }
7470
Michal Vasko03ff5a72019-09-11 13:49:33 +02007471 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007472 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007473 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7474 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02007475 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007476 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007477 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007478
Michal Vaskob0099a92020-08-31 14:55:23 +02007479 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007480 }
7481
7482 return LY_SUCCESS;
7483}
7484
7485/**
7486 * @brief Evaluate FunctionCall. Logs directly on error.
7487 *
Michal Vaskod3678892020-05-21 10:06:58 +02007488 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007489 *
7490 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007491 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007492 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007493 * @param[in] options XPath options.
7494 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7495 */
7496static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007497eval_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 +02007498{
7499 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007500
Radek Krejci1deb5be2020-08-26 16:43:36 +02007501 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007502 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007503 struct lyxp_set **args = NULL, **args_aux;
7504
aPiecek8b0cc152021-05-31 16:40:31 +02007505 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007506 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007507 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007508 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007509 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007510 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007511 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007512 xpath_func = &xpath_sum;
7513 }
7514 break;
7515 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007516 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007517 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007518 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007519 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007520 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007521 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007522 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007523 xpath_func = &xpath_true;
7524 }
7525 break;
7526 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007527 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007528 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007529 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007530 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007531 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007532 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007533 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007534 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007535 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007536 xpath_func = &xpath_deref;
7537 }
7538 break;
7539 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007540 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007541 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007542 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007543 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007544 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545 xpath_func = &xpath_string;
7546 }
7547 break;
7548 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007549 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007550 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007551 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007552 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007553 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007554 xpath_func = &xpath_current;
7555 }
7556 break;
7557 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007558 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007559 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007560 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007561 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007562 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007563 xpath_func = &xpath_re_match;
7564 }
7565 break;
7566 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007567 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007568 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007569 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570 xpath_func = &xpath_translate;
7571 }
7572 break;
7573 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007574 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007575 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007576 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007577 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007578 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007579 xpath_func = &xpath_bit_is_set;
7580 }
7581 break;
7582 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007583 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007584 xpath_func = &xpath_starts_with;
7585 }
7586 break;
7587 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007588 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007589 xpath_func = &xpath_derived_from;
7590 }
7591 break;
7592 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007593 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007594 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007595 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007596 xpath_func = &xpath_string_length;
7597 }
7598 break;
7599 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007600 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007601 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007602 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007603 xpath_func = &xpath_substring_after;
7604 }
7605 break;
7606 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007607 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007608 xpath_func = &xpath_substring_before;
7609 }
7610 break;
7611 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007612 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007613 xpath_func = &xpath_derived_from_or_self;
7614 }
7615 break;
7616 }
7617
7618 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007619 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 +02007620 return LY_EVALID;
7621 }
7622 }
7623
aPiecek8b0cc152021-05-31 16:40:31 +02007624 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007625 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007626 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007627
7628 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007629 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007630 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007631 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007632 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007633
7634 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007635 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02007636 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007637 args = malloc(sizeof *args);
7638 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7639 arg_count = 1;
7640 args[0] = set_copy(set);
7641 if (!args[0]) {
7642 rc = LY_EMEM;
7643 goto cleanup;
7644 }
7645
Michal Vasko004d3152020-06-11 19:59:22 +02007646 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007647 LY_CHECK_GOTO(rc, cleanup);
7648 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007649 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007650 LY_CHECK_GOTO(rc, cleanup);
7651 }
7652 }
Michal Vasko004d3152020-06-11 19:59:22 +02007653 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02007654 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007655 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007656 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007657
aPiecek8b0cc152021-05-31 16:40:31 +02007658 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007659 ++arg_count;
7660 args_aux = realloc(args, arg_count * sizeof *args);
7661 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7662 args = args_aux;
7663 args[arg_count - 1] = set_copy(set);
7664 if (!args[arg_count - 1]) {
7665 rc = LY_EMEM;
7666 goto cleanup;
7667 }
7668
Michal Vasko004d3152020-06-11 19:59:22 +02007669 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007670 LY_CHECK_GOTO(rc, cleanup);
7671 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007672 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007673 LY_CHECK_GOTO(rc, cleanup);
7674 }
7675 }
7676
7677 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007678 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007679 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007680 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007681 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007682
aPiecek8b0cc152021-05-31 16:40:31 +02007683 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007684 /* evaluate function */
7685 rc = xpath_func(args, arg_count, set, options);
7686
7687 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007688 /* merge all nodes from arg evaluations */
7689 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007690 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007691 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007692 }
7693 }
7694 } else {
7695 rc = LY_SUCCESS;
7696 }
7697
7698cleanup:
7699 for (i = 0; i < arg_count; ++i) {
7700 lyxp_set_free(args[i]);
7701 }
7702 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007703 return rc;
7704}
7705
7706/**
7707 * @brief Evaluate Number. Logs directly on error.
7708 *
7709 * @param[in] ctx Context for errors.
7710 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007711 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007712 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7713 * @return LY_ERR
7714 */
7715static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007716eval_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 +02007717{
7718 long double num;
7719 char *endptr;
7720
7721 if (set) {
7722 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007723 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007724 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007725 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7726 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007727 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007728 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007729 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007730 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7731 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007732 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007733 return LY_EVALID;
7734 }
7735
7736 set_fill_number(set, num);
7737 }
7738
7739 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007740 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007741 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007742 return LY_SUCCESS;
7743}
7744
7745/**
7746 * @brief Evaluate PathExpr. Logs directly on error.
7747 *
Michal Vaskod3678892020-05-21 10:06:58 +02007748 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007749 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7750 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7751 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007752 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007753 *
7754 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007755 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007756 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007757 * @param[in] options XPath options.
7758 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7759 */
7760static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007761eval_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 +02007762{
Radek Krejci857189e2020-09-01 13:26:36 +02007763 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007764 LY_ERR rc;
7765
Michal Vasko004d3152020-06-11 19:59:22 +02007766 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007767 case LYXP_TOKEN_PAR1:
7768 /* '(' Expr ')' */
7769
7770 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02007771 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007772 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007773 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007774
7775 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007776 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007777 LY_CHECK_RET(rc);
7778
7779 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007780 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007781 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007782 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007783 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007784
7785 parent_pos_pred = 0;
7786 goto predicate;
7787
7788 case LYXP_TOKEN_DOT:
7789 case LYXP_TOKEN_DDOT:
7790 case LYXP_TOKEN_AT:
7791 case LYXP_TOKEN_NAMETEST:
7792 case LYXP_TOKEN_NODETYPE:
7793 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007794 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007795 LY_CHECK_RET(rc);
7796 break;
7797
7798 case LYXP_TOKEN_FUNCNAME:
7799 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02007800 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007801 LY_CHECK_RET(rc);
7802
7803 parent_pos_pred = 1;
7804 goto predicate;
7805
Michal Vasko3e48bf32020-06-01 08:39:07 +02007806 case LYXP_TOKEN_OPER_PATH:
7807 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007808 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007809 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007810 LY_CHECK_RET(rc);
7811 break;
7812
7813 case LYXP_TOKEN_LITERAL:
7814 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02007815 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7816 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007817 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007818 }
Michal Vasko004d3152020-06-11 19:59:22 +02007819 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007820 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007821 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007822 }
7823
7824 parent_pos_pred = 1;
7825 goto predicate;
7826
7827 case LYXP_TOKEN_NUMBER:
7828 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02007829 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7830 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007831 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007832 }
Michal Vasko004d3152020-06-11 19:59:22 +02007833 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007834 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007835 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007836 }
7837 LY_CHECK_RET(rc);
7838
7839 parent_pos_pred = 1;
7840 goto predicate;
7841
7842 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01007843 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 +02007844 return LY_EVALID;
7845 }
7846
7847 return LY_SUCCESS;
7848
7849predicate:
7850 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007851 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7852 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007853 LY_CHECK_RET(rc);
7854 }
7855
7856 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007857 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007858
7859 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007860 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007861 all_desc = 0;
7862 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007863 all_desc = 1;
7864 }
7865
aPiecek8b0cc152021-05-31 16:40:31 +02007866 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007867 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007868 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007869
Michal Vasko004d3152020-06-11 19:59:22 +02007870 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007871 LY_CHECK_RET(rc);
7872 }
7873
7874 return LY_SUCCESS;
7875}
7876
7877/**
7878 * @brief Evaluate UnionExpr. Logs directly on error.
7879 *
Michal Vaskod3678892020-05-21 10:06:58 +02007880 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007881 *
7882 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007883 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007884 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007885 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007886 * @param[in] options XPath options.
7887 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7888 */
7889static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007890eval_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 +02007891{
7892 LY_ERR rc = LY_SUCCESS;
7893 struct lyxp_set orig_set, set2;
7894 uint16_t i;
7895
7896 assert(repeat);
7897
7898 set_init(&orig_set, set);
7899 set_init(&set2, set);
7900
7901 set_fill_set(&orig_set, set);
7902
Michal Vasko004d3152020-06-11 19:59:22 +02007903 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007904 LY_CHECK_GOTO(rc, cleanup);
7905
7906 /* ('|' PathExpr)* */
7907 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007908 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02007909 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007910 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007911 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007912
aPiecek8b0cc152021-05-31 16:40:31 +02007913 if (options & LYXP_SKIP_EXPR) {
7914 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007915 LY_CHECK_GOTO(rc, cleanup);
7916 continue;
7917 }
7918
7919 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007920 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007921 LY_CHECK_GOTO(rc, cleanup);
7922
7923 /* eval */
7924 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007925 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007926 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007927 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007928 LY_CHECK_GOTO(rc, cleanup);
7929 }
7930 }
7931
7932cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007933 lyxp_set_free_content(&orig_set);
7934 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007935 return rc;
7936}
7937
7938/**
7939 * @brief Evaluate UnaryExpr. Logs directly on error.
7940 *
Michal Vaskod3678892020-05-21 10:06:58 +02007941 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007942 *
7943 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007944 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007945 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007946 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007947 * @param[in] options XPath options.
7948 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7949 */
7950static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007951eval_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 +02007952{
7953 LY_ERR rc;
7954 uint16_t this_op, i;
7955
7956 assert(repeat);
7957
7958 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007959 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007960 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007961 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 +02007962
aPiecek8b0cc152021-05-31 16:40:31 +02007963 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007964 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007965 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007966 }
7967
Michal Vasko004d3152020-06-11 19:59:22 +02007968 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007969 LY_CHECK_RET(rc);
7970
aPiecek8b0cc152021-05-31 16:40:31 +02007971 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007972 if (options & LYXP_SCNODE_ALL) {
7973 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7974 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007975 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007976 LY_CHECK_RET(rc);
7977 }
7978 }
7979
7980 return LY_SUCCESS;
7981}
7982
7983/**
7984 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7985 *
Michal Vaskod3678892020-05-21 10:06:58 +02007986 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007987 * | MultiplicativeExpr '*' UnaryExpr
7988 * | MultiplicativeExpr 'div' UnaryExpr
7989 * | MultiplicativeExpr 'mod' UnaryExpr
7990 *
7991 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007992 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007993 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007994 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007995 * @param[in] options XPath options.
7996 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7997 */
7998static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007999eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
8000 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008001{
8002 LY_ERR rc;
8003 uint16_t this_op;
8004 struct lyxp_set orig_set, set2;
8005 uint16_t i;
8006
8007 assert(repeat);
8008
8009 set_init(&orig_set, set);
8010 set_init(&set2, set);
8011
8012 set_fill_set(&orig_set, set);
8013
Michal Vasko004d3152020-06-11 19:59:22 +02008014 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008015 LY_CHECK_GOTO(rc, cleanup);
8016
8017 /* ('*' / 'div' / 'mod' UnaryExpr)* */
8018 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008019 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008020
Michal Vasko004d3152020-06-11 19:59:22 +02008021 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02008022 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008023 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008024 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008025
aPiecek8b0cc152021-05-31 16:40:31 +02008026 if (options & LYXP_SKIP_EXPR) {
8027 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008028 LY_CHECK_GOTO(rc, cleanup);
8029 continue;
8030 }
8031
8032 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008033 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008034 LY_CHECK_GOTO(rc, cleanup);
8035
8036 /* eval */
8037 if (options & LYXP_SCNODE_ALL) {
8038 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008039 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008040 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008041 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008042 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008043 LY_CHECK_GOTO(rc, cleanup);
8044 }
8045 }
8046
8047cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008048 lyxp_set_free_content(&orig_set);
8049 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008050 return rc;
8051}
8052
8053/**
8054 * @brief Evaluate AdditiveExpr. Logs directly on error.
8055 *
Michal Vaskod3678892020-05-21 10:06:58 +02008056 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008057 * | AdditiveExpr '+' MultiplicativeExpr
8058 * | AdditiveExpr '-' MultiplicativeExpr
8059 *
8060 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008061 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008062 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008063 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008064 * @param[in] options XPath options.
8065 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8066 */
8067static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008068eval_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 +02008069{
8070 LY_ERR rc;
8071 uint16_t this_op;
8072 struct lyxp_set orig_set, set2;
8073 uint16_t i;
8074
8075 assert(repeat);
8076
8077 set_init(&orig_set, set);
8078 set_init(&set2, set);
8079
8080 set_fill_set(&orig_set, set);
8081
Michal Vasko004d3152020-06-11 19:59:22 +02008082 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008083 LY_CHECK_GOTO(rc, cleanup);
8084
8085 /* ('+' / '-' MultiplicativeExpr)* */
8086 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008087 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008088
Michal Vasko004d3152020-06-11 19:59:22 +02008089 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008090 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008091 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008092 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008093
aPiecek8b0cc152021-05-31 16:40:31 +02008094 if (options & LYXP_SKIP_EXPR) {
8095 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008096 LY_CHECK_GOTO(rc, cleanup);
8097 continue;
8098 }
8099
8100 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008101 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008102 LY_CHECK_GOTO(rc, cleanup);
8103
8104 /* eval */
8105 if (options & LYXP_SCNODE_ALL) {
8106 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008107 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008108 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008109 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008110 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008111 LY_CHECK_GOTO(rc, cleanup);
8112 }
8113 }
8114
8115cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008116 lyxp_set_free_content(&orig_set);
8117 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008118 return rc;
8119}
8120
8121/**
8122 * @brief Evaluate RelationalExpr. Logs directly on error.
8123 *
Michal Vaskod3678892020-05-21 10:06:58 +02008124 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008125 * | RelationalExpr '<' AdditiveExpr
8126 * | RelationalExpr '>' AdditiveExpr
8127 * | RelationalExpr '<=' AdditiveExpr
8128 * | RelationalExpr '>=' AdditiveExpr
8129 *
8130 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008131 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008132 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008133 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008134 * @param[in] options XPath options.
8135 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8136 */
8137static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008138eval_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 +02008139{
8140 LY_ERR rc;
8141 uint16_t this_op;
8142 struct lyxp_set orig_set, set2;
8143 uint16_t i;
8144
8145 assert(repeat);
8146
8147 set_init(&orig_set, set);
8148 set_init(&set2, set);
8149
8150 set_fill_set(&orig_set, set);
8151
Michal Vasko004d3152020-06-11 19:59:22 +02008152 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008153 LY_CHECK_GOTO(rc, cleanup);
8154
8155 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8156 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008157 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008158
Michal Vasko004d3152020-06-11 19:59:22 +02008159 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02008160 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008161 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008162 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008163
aPiecek8b0cc152021-05-31 16:40:31 +02008164 if (options & LYXP_SKIP_EXPR) {
8165 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008166 LY_CHECK_GOTO(rc, cleanup);
8167 continue;
8168 }
8169
8170 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008171 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008172 LY_CHECK_GOTO(rc, cleanup);
8173
8174 /* eval */
8175 if (options & LYXP_SCNODE_ALL) {
8176 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008177 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008178 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008179 } else {
8180 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8181 LY_CHECK_GOTO(rc, cleanup);
8182 }
8183 }
8184
8185cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008186 lyxp_set_free_content(&orig_set);
8187 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008188 return rc;
8189}
8190
8191/**
8192 * @brief Evaluate EqualityExpr. Logs directly on error.
8193 *
Michal Vaskod3678892020-05-21 10:06:58 +02008194 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008195 * | EqualityExpr '!=' RelationalExpr
8196 *
8197 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008198 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008199 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008200 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008201 * @param[in] options XPath options.
8202 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8203 */
8204static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008205eval_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 +02008206{
8207 LY_ERR rc;
8208 uint16_t this_op;
8209 struct lyxp_set orig_set, set2;
8210 uint16_t i;
8211
8212 assert(repeat);
8213
8214 set_init(&orig_set, set);
8215 set_init(&set2, set);
8216
8217 set_fill_set(&orig_set, set);
8218
Michal Vasko004d3152020-06-11 19:59:22 +02008219 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008220 LY_CHECK_GOTO(rc, cleanup);
8221
8222 /* ('=' / '!=' RelationalExpr)* */
8223 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008224 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008225
Michal Vasko004d3152020-06-11 19:59:22 +02008226 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02008227 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008228 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008229 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008230
aPiecek8b0cc152021-05-31 16:40:31 +02008231 if (options & LYXP_SKIP_EXPR) {
8232 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008233 LY_CHECK_GOTO(rc, cleanup);
8234 continue;
8235 }
8236
8237 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008238 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008239 LY_CHECK_GOTO(rc, cleanup);
8240
8241 /* eval */
8242 if (options & LYXP_SCNODE_ALL) {
8243 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008244 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8245 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008246 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008247 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008248 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008249 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8250 LY_CHECK_GOTO(rc, cleanup);
8251 }
8252 }
8253
8254cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008255 lyxp_set_free_content(&orig_set);
8256 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008257 return rc;
8258}
8259
8260/**
8261 * @brief Evaluate AndExpr. Logs directly on error.
8262 *
Michal Vaskod3678892020-05-21 10:06:58 +02008263 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008264 *
8265 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008266 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008267 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008268 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008269 * @param[in] options XPath options.
8270 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8271 */
8272static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008273eval_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 +02008274{
8275 LY_ERR rc;
8276 struct lyxp_set orig_set, set2;
8277 uint16_t i;
8278
8279 assert(repeat);
8280
8281 set_init(&orig_set, set);
8282 set_init(&set2, set);
8283
8284 set_fill_set(&orig_set, set);
8285
Michal Vasko004d3152020-06-11 19:59:22 +02008286 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008287 LY_CHECK_GOTO(rc, cleanup);
8288
8289 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008290 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008291 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008292 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008293 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008294 }
8295
8296 /* ('and' EqualityExpr)* */
8297 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008298 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008299 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008300 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008301 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008302
8303 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008304 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8305 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008306 LY_CHECK_GOTO(rc, cleanup);
8307 continue;
8308 }
8309
8310 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008311 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008312 LY_CHECK_GOTO(rc, cleanup);
8313
8314 /* eval - just get boolean value actually */
8315 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008316 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008317 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008318 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008319 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008320 set_fill_set(set, &set2);
8321 }
8322 }
8323
8324cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008325 lyxp_set_free_content(&orig_set);
8326 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008327 return rc;
8328}
8329
8330/**
8331 * @brief Evaluate OrExpr. Logs directly on error.
8332 *
Michal Vaskod3678892020-05-21 10:06:58 +02008333 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008334 *
8335 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008336 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008337 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008338 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008339 * @param[in] options XPath options.
8340 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8341 */
8342static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008343eval_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 +02008344{
8345 LY_ERR rc;
8346 struct lyxp_set orig_set, set2;
8347 uint16_t i;
8348
8349 assert(repeat);
8350
8351 set_init(&orig_set, set);
8352 set_init(&set2, set);
8353
8354 set_fill_set(&orig_set, set);
8355
Michal Vasko004d3152020-06-11 19:59:22 +02008356 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008357 LY_CHECK_GOTO(rc, cleanup);
8358
8359 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008360 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008361 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008362 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008363 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008364 }
8365
8366 /* ('or' AndExpr)* */
8367 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008368 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008369 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008370 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008371 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008372
8373 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008374 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8375 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008376 LY_CHECK_GOTO(rc, cleanup);
8377 continue;
8378 }
8379
8380 set_fill_set(&set2, &orig_set);
8381 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8382 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008383 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008384 LY_CHECK_GOTO(rc, cleanup);
8385
8386 /* eval - just get boolean value actually */
8387 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008388 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008389 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008390 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008391 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008392 set_fill_set(set, &set2);
8393 }
8394 }
8395
8396cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008397 lyxp_set_free_content(&orig_set);
8398 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008399 return rc;
8400}
8401
8402/**
Michal Vasko004d3152020-06-11 19:59:22 +02008403 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008404 *
8405 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008406 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008407 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02008408 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008409 * @param[in] options XPath options.
8410 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8411 */
8412static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008413eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8414 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008415{
8416 uint16_t i, count;
8417 enum lyxp_expr_type next_etype;
8418 LY_ERR rc;
8419
8420 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008421 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008422 next_etype = LYXP_EXPR_NONE;
8423 } else {
8424 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008425 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008426
8427 /* select one-priority lower because etype expression called us */
8428 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008429 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008430 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008431 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008432 } else {
8433 next_etype = LYXP_EXPR_NONE;
8434 }
8435 }
8436
8437 /* decide what expression are we parsing based on the repeat */
8438 switch (next_etype) {
8439 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008440 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008441 break;
8442 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008443 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008444 break;
8445 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008446 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008447 break;
8448 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008449 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008450 break;
8451 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008452 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008453 break;
8454 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008455 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008456 break;
8457 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008458 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008459 break;
8460 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008461 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008462 break;
8463 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008464 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008465 break;
8466 default:
8467 LOGINT_RET(set->ctx);
8468 }
8469
8470 return rc;
8471}
8472
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008473/**
8474 * @brief Get root type.
8475 *
8476 * @param[in] ctx_node Context node.
8477 * @param[in] ctx_scnode Schema context node.
8478 * @param[in] options XPath options.
8479 * @return Root type.
8480 */
8481static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008482lyxp_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 +01008483{
Michal Vasko6b26e742020-07-17 15:02:10 +02008484 const struct lysc_node *op;
8485
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008486 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008487 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008488 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008489
8490 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008491 /* general root that can access everything */
8492 return LYXP_NODE_ROOT;
8493 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8494 /* root context node can access only config data (because we said so, it is unspecified) */
8495 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008496 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008497 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008498 }
8499
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008500 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008501 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008502 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008503
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008504 if (op || !(options & LYXP_SCHEMA)) {
8505 /* general root that can access everything */
8506 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05008507 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008508 /* root context node can access only config data (because we said so, it is unspecified) */
8509 return LYXP_NODE_ROOT_CONFIG;
8510 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008511 return LYXP_NODE_ROOT;
8512}
8513
Michal Vasko03ff5a72019-09-11 13:49:33 +02008514LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008515lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008516 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 +01008517 struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008518{
Michal Vasko004d3152020-06-11 19:59:22 +02008519 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008520 LY_ERR rc;
8521
Michal Vasko400e9672021-01-11 13:39:17 +01008522 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008523 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008524 LOGARG(NULL, "Current module must be set if schema format is used.");
8525 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008526 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008527
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008528 if (tree) {
8529 /* adjust the pointer to be the first top-level sibling */
8530 while (tree->parent) {
8531 tree = lyd_parent(tree);
8532 }
8533 tree = lyd_first_sibling(tree);
8534 }
8535
Michal Vasko03ff5a72019-09-11 13:49:33 +02008536 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008537 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008538 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008539 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8540 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8541
Michal Vasko400e9672021-01-11 13:39:17 +01008542 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008543 set->cur_node = ctx_node;
8544 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008545 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8546 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008547 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008548 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008549 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008550 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008551
Radek Krejciddace2c2021-01-08 11:30:56 +01008552 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008553
Michal Vasko03ff5a72019-09-11 13:49:33 +02008554 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008555 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008556 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008557 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008558 }
8559
Radek Krejciddace2c2021-01-08 11:30:56 +01008560 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008561 return rc;
8562}
8563
8564#if 0
8565
8566/* full xml printing of set elements, not used currently */
8567
8568void
8569lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8570{
8571 uint32_t i;
8572 char *str_num;
8573 struct lyout out;
8574
8575 memset(&out, 0, sizeof out);
8576
8577 out.type = LYOUT_STREAM;
8578 out.method.f = f;
8579
8580 switch (set->type) {
8581 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008582 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008583 break;
8584 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008585 ly_print_(&out, "Boolean XPath set:\n");
8586 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008587 break;
8588 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008589 ly_print_(&out, "String XPath set:\n");
8590 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008591 break;
8592 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008593 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008594
8595 if (isnan(set->value.num)) {
8596 str_num = strdup("NaN");
8597 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8598 str_num = strdup("0");
8599 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8600 str_num = strdup("Infinity");
8601 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8602 str_num = strdup("-Infinity");
8603 } else if ((long long)set->value.num == set->value.num) {
8604 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8605 str_num = NULL;
8606 }
8607 } else {
8608 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8609 str_num = NULL;
8610 }
8611 }
8612 if (!str_num) {
8613 LOGMEM;
8614 return;
8615 }
Michal Vasko5233e962020-08-14 14:26:20 +02008616 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008617 free(str_num);
8618 break;
8619 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008620 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008621
8622 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008623 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008624 switch (set->node_type[i]) {
8625 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008626 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008627 break;
8628 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008629 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008630 break;
8631 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008632 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008633 break;
8634 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008635 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008636 break;
8637 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008638 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008639 break;
8640 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008641 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008642 break;
8643 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008644 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008645 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008646 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008647 break;
8648 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008649 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 +02008650 break;
8651 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008652 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 +02008653 break;
8654 }
8655 }
8656 break;
8657 }
8658}
8659
8660#endif
8661
8662LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008663lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008664{
8665 long double num;
8666 char *str;
8667 LY_ERR rc;
8668
8669 if (!set || (set->type == target)) {
8670 return LY_SUCCESS;
8671 }
8672
8673 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008674 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008675
8676 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008677 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008678 return LY_EINVAL;
8679 }
8680
8681 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008682 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008683 switch (set->type) {
8684 case LYXP_SET_NUMBER:
8685 if (isnan(set->val.num)) {
8686 set->val.str = strdup("NaN");
8687 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8688 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8689 set->val.str = strdup("0");
8690 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8691 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8692 set->val.str = strdup("Infinity");
8693 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8694 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8695 set->val.str = strdup("-Infinity");
8696 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8697 } else if ((long long)set->val.num == set->val.num) {
8698 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8699 LOGMEM_RET(set->ctx);
8700 }
8701 set->val.str = str;
8702 } else {
8703 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8704 LOGMEM_RET(set->ctx);
8705 }
8706 set->val.str = str;
8707 }
8708 break;
8709 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008710 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008711 set->val.str = strdup("true");
8712 } else {
8713 set->val.str = strdup("false");
8714 }
8715 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8716 break;
8717 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008718 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008719 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008720
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008721 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008722 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008723 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008724 set->val.str = str;
8725 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008726 default:
8727 LOGINT_RET(set->ctx);
8728 }
8729 set->type = LYXP_SET_STRING;
8730 }
8731
8732 /* to NUMBER */
8733 if (target == LYXP_SET_NUMBER) {
8734 switch (set->type) {
8735 case LYXP_SET_STRING:
8736 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008737 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008738 set->val.num = num;
8739 break;
8740 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008741 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008742 set->val.num = 1;
8743 } else {
8744 set->val.num = 0;
8745 }
8746 break;
8747 default:
8748 LOGINT_RET(set->ctx);
8749 }
8750 set->type = LYXP_SET_NUMBER;
8751 }
8752
8753 /* to BOOLEAN */
8754 if (target == LYXP_SET_BOOLEAN) {
8755 switch (set->type) {
8756 case LYXP_SET_NUMBER:
8757 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008758 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008759 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008760 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008761 }
8762 break;
8763 case LYXP_SET_STRING:
8764 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008765 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008766 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008767 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008768 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008769 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008770 }
8771 break;
8772 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008773 if (set->used) {
8774 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008775 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008776 } else {
8777 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008778 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008779 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008780 break;
8781 default:
8782 LOGINT_RET(set->ctx);
8783 }
8784 set->type = LYXP_SET_BOOLEAN;
8785 }
8786
Michal Vasko03ff5a72019-09-11 13:49:33 +02008787 return LY_SUCCESS;
8788}
8789
8790LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008791lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008792 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
Michal Vasko400e9672021-01-11 13:39:17 +01008793 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008794{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008795 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008796 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008797
Michal Vasko400e9672021-01-11 13:39:17 +01008798 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008799 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008800 LOGARG(NULL, "Current module must be set if schema format is used.");
8801 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008802 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008803
8804 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008805 memset(set, 0, sizeof *set);
8806 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008807 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8808 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 +01008809 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008810
Michal Vasko400e9672021-01-11 13:39:17 +01008811 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008812 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008813 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008814 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8815 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008816 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008817 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008818 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008819
Radek Krejciddace2c2021-01-08 11:30:56 +01008820 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008821
Michal Vasko03ff5a72019-09-11 13:49:33 +02008822 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01008823 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
8824
Radek Krejciddace2c2021-01-08 11:30:56 +01008825 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008826 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008827}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008828
8829API const char *
8830lyxp_get_expr(const struct lyxp_expr *path)
8831{
8832 if (!path) {
8833 return NULL;
8834 }
8835
8836 return path->expr;
8837}