blob: ea1cdfc9f8c373561538bd1a6d793feb057c2eff [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;
808 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
809 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
810 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
811
812 ret->used = ret->size = set->used;
813 ret->ctx_pos = set->ctx_pos;
814 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100815 if (set->ht) {
816 ret->ht = lyht_dup(set->ht);
817 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200818 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200819 memcpy(ret, set, sizeof *ret);
820 if (set->type == LYXP_SET_STRING) {
821 ret->val.str = strdup(set->val.str);
822 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
823 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200824 }
825
826 return ret;
827}
828
829/**
830 * @brief Fill XPath set with a string. Any current data are disposed of.
831 *
832 * @param[in] set Set to fill.
833 * @param[in] string String to fill into \p set.
834 * @param[in] str_len Length of \p string. 0 is a valid value!
835 */
836static void
837set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
838{
Michal Vaskod3678892020-05-21 10:06:58 +0200839 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200840
841 set->type = LYXP_SET_STRING;
842 if ((str_len == 0) && (string[0] != '\0')) {
843 string = "";
844 }
845 set->val.str = strndup(string, str_len);
846}
847
848/**
849 * @brief Fill XPath set with a number. Any current data are disposed of.
850 *
851 * @param[in] set Set to fill.
852 * @param[in] number Number to fill into \p set.
853 */
854static void
855set_fill_number(struct lyxp_set *set, long double number)
856{
Michal Vaskod3678892020-05-21 10:06:58 +0200857 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200858
859 set->type = LYXP_SET_NUMBER;
860 set->val.num = number;
861}
862
863/**
864 * @brief Fill XPath set with a boolean. Any current data are disposed of.
865 *
866 * @param[in] set Set to fill.
867 * @param[in] boolean Boolean to fill into \p set.
868 */
869static void
Radek Krejci857189e2020-09-01 13:26:36 +0200870set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200871{
Michal Vaskod3678892020-05-21 10:06:58 +0200872 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200873
874 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200875 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200876}
877
878/**
879 * @brief Fill XPath set with the value from another set (deep assign).
880 * Any current data are disposed of.
881 *
882 * @param[in] trg Set to fill.
883 * @param[in] src Source set to copy into \p trg.
884 */
885static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200886set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200887{
888 if (!trg || !src) {
889 return;
890 }
891
892 if (trg->type == LYXP_SET_NODE_SET) {
893 free(trg->val.nodes);
894 } else if (trg->type == LYXP_SET_STRING) {
895 free(trg->val.str);
896 }
897 set_init(trg, src);
898
899 if (src->type == LYXP_SET_SCNODE_SET) {
900 trg->type = LYXP_SET_SCNODE_SET;
901 trg->used = src->used;
902 trg->size = src->used;
903
904 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
905 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
906 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
907 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200908 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200909 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200910 set_fill_number(trg, src->val.num);
911 } else if (src->type == LYXP_SET_STRING) {
912 set_fill_string(trg, src->val.str, strlen(src->val.str));
913 } else {
914 if (trg->type == LYXP_SET_NODE_SET) {
915 free(trg->val.nodes);
916 } else if (trg->type == LYXP_SET_STRING) {
917 free(trg->val.str);
918 }
919
Michal Vaskod3678892020-05-21 10:06:58 +0200920 assert(src->type == LYXP_SET_NODE_SET);
921
922 trg->type = LYXP_SET_NODE_SET;
923 trg->used = src->used;
924 trg->size = src->used;
925 trg->ctx_pos = src->ctx_pos;
926 trg->ctx_size = src->ctx_size;
927
928 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
929 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
930 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
931 if (src->ht) {
932 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200933 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200934 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200935 }
936 }
937}
938
939/**
940 * @brief Clear context of all schema nodes.
941 *
942 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +0200943 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200944 */
945static void
Michal Vasko1a09b212021-05-06 13:00:10 +0200946set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200947{
948 uint32_t i;
949
950 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100951 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +0200952 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100953 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
954 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200955 }
956 }
957}
958
959/**
960 * @brief Remove a node from a set. Removing last node changes
961 * set into LYXP_SET_EMPTY. Context position aware.
962 *
963 * @param[in] set Set to use.
964 * @param[in] idx Index from @p set of the node to be removed.
965 */
966static void
967set_remove_node(struct lyxp_set *set, uint32_t idx)
968{
969 assert(set && (set->type == LYXP_SET_NODE_SET));
970 assert(idx < set->used);
971
972 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
973
974 --set->used;
975 if (set->used) {
976 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
977 (set->used - idx) * sizeof *set->val.nodes);
978 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200979 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200980 }
981}
982
983/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100984 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200985 *
986 * @param[in] set Set to use.
987 * @param[in] idx Index from @p set of the node to be removed.
988 */
989static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100990set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200991{
992 assert(set && (set->type == LYXP_SET_NODE_SET));
993 assert(idx < set->used);
994
Michal Vasko2caefc12019-11-14 16:07:56 +0100995 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
996 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
997 }
998 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +0200999}
1000
1001/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001002 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001003 * set into LYXP_SET_EMPTY. Context position aware.
1004 *
1005 * @param[in] set Set to consolidate.
1006 */
1007static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001008set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001009{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001010 uint32_t i, orig_used, end = 0;
1011 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001012
Michal Vaskod3678892020-05-21 10:06:58 +02001013 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001014
1015 orig_used = set->used;
1016 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001017 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001018 start = -1;
1019 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001020 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001021 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001022 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001023 end = i;
1024 ++i;
1025 break;
1026 }
1027
1028 ++i;
1029 if (i == orig_used) {
1030 end = i;
1031 }
1032 } while (i < orig_used);
1033
1034 if (start > -1) {
1035 /* move the whole chunk of valid nodes together */
1036 if (set->used != (unsigned)start) {
1037 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1038 }
1039 set->used += end - start;
1040 }
1041 }
Michal Vasko57eab132019-09-24 11:46:26 +02001042}
1043
1044/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001045 * @brief Check for duplicates in a node set.
1046 *
1047 * @param[in] set Set to check.
1048 * @param[in] node Node to look for in @p set.
1049 * @param[in] node_type Type of @p node.
1050 * @param[in] skip_idx Index from @p set to skip.
1051 * @return LY_ERR
1052 */
1053static LY_ERR
1054set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1055{
1056 uint32_t i;
1057
Michal Vasko2caefc12019-11-14 16:07:56 +01001058 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001059 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1060 }
1061
1062 for (i = 0; i < set->used; ++i) {
1063 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1064 continue;
1065 }
1066
1067 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1068 return LY_EEXIST;
1069 }
1070 }
1071
1072 return LY_SUCCESS;
1073}
1074
Radek Krejci857189e2020-09-01 13:26:36 +02001075ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001076lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1077 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001078{
1079 uint32_t i;
1080
1081 for (i = 0; i < set->used; ++i) {
1082 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1083 continue;
1084 }
1085
1086 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001087 if (index_p) {
1088 *index_p = i;
1089 }
1090 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001091 }
1092 }
1093
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001094 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001095}
1096
Michal Vaskoecd62de2019-11-13 12:35:11 +01001097void
1098lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001099{
1100 uint32_t orig_used, i, j;
1101
Michal Vaskod3678892020-05-21 10:06:58 +02001102 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001103
Michal Vaskod3678892020-05-21 10:06:58 +02001104 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001105 return;
1106 }
1107
Michal Vaskod3678892020-05-21 10:06:58 +02001108 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001109 memcpy(set1, set2, sizeof *set1);
1110 return;
1111 }
1112
1113 if (set1->used + set2->used > set1->size) {
1114 set1->size = set1->used + set2->used;
1115 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1116 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1117 }
1118
1119 orig_used = set1->used;
1120
1121 for (i = 0; i < set2->used; ++i) {
1122 for (j = 0; j < orig_used; ++j) {
1123 /* detect duplicities */
1124 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1125 break;
1126 }
1127 }
1128
Michal Vasko0587bce2020-12-10 12:19:21 +01001129 if (j < orig_used) {
1130 /* node is there, but update its status if needed */
1131 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1132 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001133 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1134 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1135 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001136 }
1137 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001138 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1139 ++set1->used;
1140 }
1141 }
1142
Michal Vaskod3678892020-05-21 10:06:58 +02001143 lyxp_set_free_content(set2);
1144 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001145}
1146
1147/**
1148 * @brief Insert a node into a set. Context position aware.
1149 *
1150 * @param[in] set Set to use.
1151 * @param[in] node Node to insert to @p set.
1152 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1153 * @param[in] node_type Node type of @p node.
1154 * @param[in] idx Index in @p set to insert into.
1155 */
1156static void
1157set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1158{
Michal Vaskod3678892020-05-21 10:06:58 +02001159 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001160
Michal Vaskod3678892020-05-21 10:06:58 +02001161 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001162 /* first item */
1163 if (idx) {
1164 /* no real harm done, but it is a bug */
1165 LOGINT(set->ctx);
1166 idx = 0;
1167 }
1168 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1169 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1170 set->type = LYXP_SET_NODE_SET;
1171 set->used = 0;
1172 set->size = LYXP_SET_SIZE_START;
1173 set->ctx_pos = 1;
1174 set->ctx_size = 1;
1175 set->ht = NULL;
1176 } else {
1177 /* not an empty set */
1178 if (set->used == set->size) {
1179
1180 /* set is full */
1181 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1182 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1183 set->size += LYXP_SET_SIZE_STEP;
1184 }
1185
1186 if (idx > set->used) {
1187 LOGINT(set->ctx);
1188 idx = set->used;
1189 }
1190
1191 /* make space for the new node */
1192 if (idx < set->used) {
1193 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1194 }
1195 }
1196
1197 /* finally assign the value */
1198 set->val.nodes[idx].node = (struct lyd_node *)node;
1199 set->val.nodes[idx].type = node_type;
1200 set->val.nodes[idx].pos = pos;
1201 ++set->used;
1202
Michal Vasko2caefc12019-11-14 16:07:56 +01001203 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1204 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1205 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001206}
1207
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001208LY_ERR
1209lyxp_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 +02001210{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001211 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001212
1213 assert(set->type == LYXP_SET_SCNODE_SET);
1214
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001215 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001216 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001217 } else {
1218 if (set->used == set->size) {
1219 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 +02001220 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001221 set->size += LYXP_SET_SIZE_STEP;
1222 }
1223
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001224 index = set->used;
1225 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1226 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001227 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001228 ++set->used;
1229 }
1230
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001231 if (index_p) {
1232 *index_p = index;
1233 }
1234
1235 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001236}
1237
1238/**
1239 * @brief Replace a node in a set with another. Context position aware.
1240 *
1241 * @param[in] set Set to use.
1242 * @param[in] node Node to insert to @p set.
1243 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1244 * @param[in] node_type Node type of @p node.
1245 * @param[in] idx Index in @p set of the node to replace.
1246 */
1247static void
1248set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1249{
1250 assert(set && (idx < set->used));
1251
Michal Vasko2caefc12019-11-14 16:07:56 +01001252 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1253 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1254 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001255 set->val.nodes[idx].node = (struct lyd_node *)node;
1256 set->val.nodes[idx].type = node_type;
1257 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001258 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1259 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1260 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001261}
1262
1263/**
1264 * @brief Set all nodes with ctx 1 to a new unique context value.
1265 *
1266 * @param[in] set Set to modify.
1267 * @return New context value.
1268 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001269static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001270set_scnode_new_in_ctx(struct lyxp_set *set)
1271{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001272 uint32_t i;
1273 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001274
1275 assert(set->type == LYXP_SET_SCNODE_SET);
1276
Radek Krejcif13b87b2020-12-01 22:02:17 +01001277 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001278retry:
1279 for (i = 0; i < set->used; ++i) {
1280 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1281 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1282 goto retry;
1283 }
1284 }
1285 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001286 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001287 set->val.scnodes[i].in_ctx = ret_ctx;
1288 }
1289 }
1290
1291 return ret_ctx;
1292}
1293
1294/**
1295 * @brief Get unique @p node position in the data.
1296 *
1297 * @param[in] node Node to find.
1298 * @param[in] node_type Node type of @p node.
1299 * @param[in] root Root node.
1300 * @param[in] root_type Type of the XPath @p root node.
1301 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1302 * be used to increase efficiency and start the DFS from this node.
1303 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1304 * @return Node position.
1305 */
1306static uint32_t
1307get_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 +02001308 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001309{
Michal Vasko56daf732020-08-10 10:57:18 +02001310 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001311 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001312 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001313
1314 assert(prev && prev_pos && !root->prev->next);
1315
1316 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1317 return 0;
1318 }
1319
1320 if (*prev) {
1321 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001322 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001323 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001324 goto dfs_search;
1325 }
1326
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001327 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001328 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001329dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001330 if (*prev && !elem) {
1331 /* resume previous DFS */
1332 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1333 LYD_TREE_DFS_continue = 0;
1334 }
1335
Michal Vasko03ff5a72019-09-11 13:49:33 +02001336 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001337 /* skip */
1338 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001339 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001340 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001341 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001342 break;
1343 }
Michal Vasko56daf732020-08-10 10:57:18 +02001344 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001345 }
Michal Vasko56daf732020-08-10 10:57:18 +02001346
1347 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001348 }
1349
1350 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001351 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001352 break;
1353 }
1354 }
1355
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001356 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001357 if (!(*prev)) {
1358 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001359 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001360 return 0;
1361 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001362 /* start the search again from the beginning */
1363 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001364
Michal Vasko56daf732020-08-10 10:57:18 +02001365 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001366 pos = 1;
1367 goto dfs_search;
1368 }
1369 }
1370
1371 /* remember the last found node for next time */
1372 *prev = node;
1373 *prev_pos = pos;
1374
1375 return pos;
1376}
1377
1378/**
1379 * @brief Assign (fill) missing node positions.
1380 *
1381 * @param[in] set Set to fill positions in.
1382 * @param[in] root Context root node.
1383 * @param[in] root_type Context root type.
1384 * @return LY_ERR
1385 */
1386static LY_ERR
1387set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1388{
1389 const struct lyd_node *prev = NULL, *tmp_node;
1390 uint32_t i, tmp_pos = 0;
1391
1392 for (i = 0; i < set->used; ++i) {
1393 if (!set->val.nodes[i].pos) {
1394 tmp_node = NULL;
1395 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001396 case LYXP_NODE_META:
1397 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001398 if (!tmp_node) {
1399 LOGINT_RET(root->schema->module->ctx);
1400 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001401 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001402 case LYXP_NODE_ELEM:
1403 case LYXP_NODE_TEXT:
1404 if (!tmp_node) {
1405 tmp_node = set->val.nodes[i].node;
1406 }
1407 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1408 break;
1409 default:
1410 /* all roots have position 0 */
1411 break;
1412 }
1413 }
1414 }
1415
1416 return LY_SUCCESS;
1417}
1418
1419/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001420 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001421 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001422 * @param[in] meta Metadata to use.
1423 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001424 */
1425static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001426get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001427{
1428 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001429 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001430
Michal Vasko9f96a052020-03-10 09:41:45 +01001431 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001432 ++pos;
1433 }
1434
Michal Vasko9f96a052020-03-10 09:41:45 +01001435 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001436 return pos;
1437}
1438
1439/**
1440 * @brief Compare 2 nodes in respect to XPath document order.
1441 *
1442 * @param[in] item1 1st node.
1443 * @param[in] item2 2nd node.
1444 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1445 */
1446static int
1447set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1448{
Michal Vasko9f96a052020-03-10 09:41:45 +01001449 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001450
1451 if (item1->pos < item2->pos) {
1452 return -1;
1453 }
1454
1455 if (item1->pos > item2->pos) {
1456 return 1;
1457 }
1458
1459 /* node positions are equal, the fun case */
1460
1461 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1462 /* special case since text nodes are actually saved as their parents */
1463 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1464 if (item1->type == LYXP_NODE_ELEM) {
1465 assert(item2->type == LYXP_NODE_TEXT);
1466 return -1;
1467 } else {
1468 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1469 return 1;
1470 }
1471 }
1472
Michal Vasko9f96a052020-03-10 09:41:45 +01001473 /* we need meta positions now */
1474 if (item1->type == LYXP_NODE_META) {
1475 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001476 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001477 if (item2->type == LYXP_NODE_META) {
1478 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001479 }
1480
Michal Vasko9f96a052020-03-10 09:41:45 +01001481 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001482 /* check for duplicates */
1483 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001484 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001485 return 0;
1486 }
1487
Michal Vasko9f96a052020-03-10 09:41:45 +01001488 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001489 /* elem is always first, 2nd node is after it */
1490 if (item1->type == LYXP_NODE_ELEM) {
1491 assert(item2->type != LYXP_NODE_ELEM);
1492 return -1;
1493 }
1494
Michal Vasko9f96a052020-03-10 09:41:45 +01001495 /* 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 +02001496 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001497 if (((item1->type == LYXP_NODE_TEXT) &&
1498 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1499 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1500 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1501 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001502 return 1;
1503 }
1504
Michal Vasko9f96a052020-03-10 09:41:45 +01001505 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001506 /* 2nd is after 1st */
1507 return -1;
1508}
1509
1510/**
1511 * @brief Set cast for comparisons.
1512 *
1513 * @param[in] trg Target set to cast source into.
1514 * @param[in] src Source set.
1515 * @param[in] type Target set type.
1516 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001517 * @return LY_ERR
1518 */
1519static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001520set_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 +02001521{
1522 assert(src->type == LYXP_SET_NODE_SET);
1523
1524 set_init(trg, src);
1525
1526 /* insert node into target set */
1527 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1528
1529 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001530 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001531}
1532
Michal Vasko4c7763f2020-07-27 17:40:37 +02001533/**
1534 * @brief Set content canonization for comparisons.
1535 *
1536 * @param[in] trg Target set to put the canononized source into.
1537 * @param[in] src Source set.
1538 * @param[in] xp_node Source XPath node/meta to use for canonization.
1539 * @return LY_ERR
1540 */
1541static LY_ERR
1542set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1543{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001544 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001545 struct lyd_value val;
1546 struct ly_err_item *err = NULL;
1547 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001548 LY_ERR rc;
1549
1550 /* is there anything to canonize even? */
1551 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1552 /* do we have a type to use for canonization? */
1553 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1554 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1555 } else if (xp_node->type == LYXP_NODE_META) {
1556 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1557 }
1558 }
1559 if (!type) {
1560 goto fill;
1561 }
1562
1563 if (src->type == LYXP_SET_NUMBER) {
1564 /* canonize number */
1565 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1566 LOGMEM(src->ctx);
1567 return LY_EMEM;
1568 }
1569 } else {
1570 /* canonize string */
1571 str = strdup(src->val.str);
1572 }
1573
1574 /* ignore errors, the value may not satisfy schema constraints */
Radek Krejci0b013302021-03-29 15:22:32 +02001575 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 +01001576 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001577 ly_err_free(err);
1578 if (rc) {
1579 /* invalid value */
Radek IÅ¡a48460222021-03-02 15:18:32 +01001580 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001581 goto fill;
1582 }
1583
Michal Vasko4c7763f2020-07-27 17:40:37 +02001584 /* use the canonized value */
1585 set_init(trg, src);
1586 trg->type = src->type;
1587 if (src->type == LYXP_SET_NUMBER) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001588 trg->val.num = strtold(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001589 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1590 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001591 trg->val.str = strdup(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko4c7763f2020-07-27 17:40:37 +02001592 }
1593 type->plugin->free(src->ctx, &val);
1594 return LY_SUCCESS;
1595
1596fill:
1597 /* no canonization needed/possible */
1598 set_fill_set(trg, src);
1599 return LY_SUCCESS;
1600}
1601
Michal Vasko03ff5a72019-09-11 13:49:33 +02001602#ifndef NDEBUG
1603
1604/**
1605 * @brief Bubble sort @p set into XPath document order.
1606 * Context position aware. Unused in the 'Release' build target.
1607 *
1608 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001609 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1610 */
1611static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001612set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001613{
1614 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001615 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001616 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001617 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001618 struct lyxp_set_node item;
1619 struct lyxp_set_hash_node hnode;
1620 uint64_t hash;
1621
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001622 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001623 return 0;
1624 }
1625
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001626 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001627 for (root = set->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001628 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001629
1630 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001631 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001632 return -1;
1633 }
1634
1635 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1636 print_set_debug(set);
1637
1638 for (i = 0; i < set->used; ++i) {
1639 inverted = 0;
1640 change = 0;
1641
1642 for (j = 1; j < set->used - i; ++j) {
1643 /* compare node positions */
1644 if (inverted) {
1645 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1646 } else {
1647 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1648 }
1649
1650 /* swap if needed */
1651 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1652 change = 1;
1653
1654 item = set->val.nodes[j - 1];
1655 set->val.nodes[j - 1] = set->val.nodes[j];
1656 set->val.nodes[j] = item;
1657 } else {
1658 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1659 inverted = !inverted;
1660 }
1661 }
1662
1663 ++ret;
1664
1665 if (!change) {
1666 break;
1667 }
1668 }
1669
1670 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1671 print_set_debug(set);
1672
1673 /* check node hashes */
1674 if (set->used >= LYD_HT_MIN_ITEMS) {
1675 assert(set->ht);
1676 for (i = 0; i < set->used; ++i) {
1677 hnode.node = set->val.nodes[i].node;
1678 hnode.type = set->val.nodes[i].type;
1679
1680 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1681 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1682 hash = dict_hash_multi(hash, NULL, 0);
1683
1684 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1685 }
1686 }
1687
1688 return ret - 1;
1689}
1690
1691/**
1692 * @brief Remove duplicate entries in a sorted node set.
1693 *
1694 * @param[in] set Sorted set to check.
1695 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1696 */
1697static LY_ERR
1698set_sorted_dup_node_clean(struct lyxp_set *set)
1699{
1700 uint32_t i = 0;
1701 LY_ERR ret = LY_SUCCESS;
1702
1703 if (set->used > 1) {
1704 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001705 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1706 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001707 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001708 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001709 }
Michal Vasko57eab132019-09-24 11:46:26 +02001710 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001711 }
1712 }
1713
Michal Vasko2caefc12019-11-14 16:07:56 +01001714 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001715 return ret;
1716}
1717
1718#endif
1719
1720/**
1721 * @brief Merge 2 sorted sets into one.
1722 *
1723 * @param[in,out] trg Set to merge into. Duplicates are removed.
1724 * @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 +02001725 * @return LY_ERR
1726 */
1727static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001728set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001729{
1730 uint32_t i, j, k, count, dup_count;
1731 int cmp;
1732 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001733
Michal Vaskod3678892020-05-21 10:06:58 +02001734 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001735 return LY_EINVAL;
1736 }
1737
Michal Vaskod3678892020-05-21 10:06:58 +02001738 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001739 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001740 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001741 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001742 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001743 return LY_SUCCESS;
1744 }
1745
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001746 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001747 for (root = trg->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001748 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001749
1750 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001751 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001752 return LY_EINT;
1753 }
1754
1755#ifndef NDEBUG
1756 LOGDBG(LY_LDGXPATH, "MERGE target");
1757 print_set_debug(trg);
1758 LOGDBG(LY_LDGXPATH, "MERGE source");
1759 print_set_debug(src);
1760#endif
1761
1762 /* make memory for the merge (duplicates are not detected yet, so space
1763 * will likely be wasted on them, too bad) */
1764 if (trg->size - trg->used < src->used) {
1765 trg->size = trg->used + src->used;
1766
1767 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1768 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1769 }
1770
1771 i = 0;
1772 j = 0;
1773 count = 0;
1774 dup_count = 0;
1775 do {
1776 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1777 if (!cmp) {
1778 if (!count) {
1779 /* duplicate, just skip it */
1780 ++i;
1781 ++j;
1782 } else {
1783 /* we are copying something already, so let's copy the duplicate too,
1784 * we are hoping that afterwards there are some more nodes to
1785 * copy and this way we can copy them all together */
1786 ++count;
1787 ++dup_count;
1788 ++i;
1789 ++j;
1790 }
1791 } else if (cmp < 0) {
1792 /* inserting src node into trg, just remember it for now */
1793 ++count;
1794 ++i;
1795
1796 /* insert the hash now */
1797 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1798 } else if (count) {
1799copy_nodes:
1800 /* time to actually copy the nodes, we have found the largest block of nodes */
1801 memmove(&trg->val.nodes[j + (count - dup_count)],
1802 &trg->val.nodes[j],
1803 (trg->used - j) * sizeof *trg->val.nodes);
1804 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1805
1806 trg->used += count - dup_count;
1807 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1808 j += count - dup_count;
1809
1810 count = 0;
1811 dup_count = 0;
1812 } else {
1813 ++j;
1814 }
1815 } while ((i < src->used) && (j < trg->used));
1816
1817 if ((i < src->used) || count) {
1818 /* insert all the hashes first */
1819 for (k = i; k < src->used; ++k) {
1820 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1821 }
1822
1823 /* loop ended, but we need to copy something at trg end */
1824 count += src->used - i;
1825 i = src->used;
1826 goto copy_nodes;
1827 }
1828
1829 /* we are inserting hashes before the actual node insert, which causes
1830 * situations when there were initially not enough items for a hash table,
1831 * but even after some were inserted, hash table was not created (during
1832 * insertion the number of items is not updated yet) */
1833 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1834 set_insert_node_hash(trg, NULL, 0);
1835 }
1836
1837#ifndef NDEBUG
1838 LOGDBG(LY_LDGXPATH, "MERGE result");
1839 print_set_debug(trg);
1840#endif
1841
Michal Vaskod3678892020-05-21 10:06:58 +02001842 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001843 return LY_SUCCESS;
1844}
1845
1846/*
1847 * (re)parse functions
1848 *
1849 * Parse functions parse the expression into
1850 * tokens (syntactic analysis).
1851 *
1852 * Reparse functions perform semantic analysis
1853 * (do not save the result, just a check) of
1854 * the expression and fill repeat indices.
1855 */
1856
Michal Vasko14676352020-05-29 11:35:55 +02001857LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001858lyxp_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 +02001859{
Michal Vasko004d3152020-06-11 19:59:22 +02001860 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001861 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001862 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001863 }
Michal Vasko14676352020-05-29 11:35:55 +02001864 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001865 }
1866
Michal Vasko004d3152020-06-11 19:59:22 +02001867 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001868 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001869 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001870 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001871 }
Michal Vasko14676352020-05-29 11:35:55 +02001872 return LY_ENOT;
1873 }
1874
1875 return LY_SUCCESS;
1876}
1877
Michal Vasko004d3152020-06-11 19:59:22 +02001878LY_ERR
1879lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1880{
1881 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1882
1883 /* skip the token */
1884 ++(*tok_idx);
1885
1886 return LY_SUCCESS;
1887}
1888
Michal Vasko14676352020-05-29 11:35:55 +02001889/* just like lyxp_check_token() but tests for 2 tokens */
1890static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001891exp_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 +02001892 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001893{
Michal Vasko004d3152020-06-11 19:59:22 +02001894 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001895 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001896 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001897 }
1898 return LY_EINCOMPLETE;
1899 }
1900
Michal Vasko004d3152020-06-11 19:59:22 +02001901 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001902 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001903 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001904 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001905 }
1906 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001907 }
1908
1909 return LY_SUCCESS;
1910}
1911
1912/**
1913 * @brief Stack operation push on the repeat array.
1914 *
1915 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001916 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001917 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1918 */
1919static void
Michal Vasko004d3152020-06-11 19:59:22 +02001920exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001921{
1922 uint16_t i;
1923
Michal Vasko004d3152020-06-11 19:59:22 +02001924 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001925 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001926 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1927 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1928 exp->repeat[tok_idx][i] = repeat_op_idx;
1929 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001930 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001931 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1932 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1933 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001934 }
1935}
1936
1937/**
1938 * @brief Reparse Predicate. Logs directly on error.
1939 *
1940 * [7] Predicate ::= '[' Expr ']'
1941 *
1942 * @param[in] ctx Context for logging.
1943 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001944 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02001945 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001946 * @return LY_ERR
1947 */
1948static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02001949reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001950{
1951 LY_ERR rc;
1952
Michal Vasko004d3152020-06-11 19:59:22 +02001953 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001954 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001955 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001956
aPiecekbf968d92021-05-27 14:35:05 +02001957 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001958 LY_CHECK_RET(rc);
1959
Michal Vasko004d3152020-06-11 19:59:22 +02001960 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001961 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001962 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001963
1964 return LY_SUCCESS;
1965}
1966
1967/**
1968 * @brief Reparse RelativeLocationPath. Logs directly on error.
1969 *
1970 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1971 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1972 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1973 *
1974 * @param[in] ctx Context for logging.
1975 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001976 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02001977 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001978 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1979 */
1980static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02001981reparse_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 +02001982{
1983 LY_ERR rc;
1984
Michal Vasko004d3152020-06-11 19:59:22 +02001985 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001986 LY_CHECK_RET(rc);
1987
1988 goto step;
1989 do {
1990 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001991 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001992
Michal Vasko004d3152020-06-11 19:59:22 +02001993 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001994 LY_CHECK_RET(rc);
1995step:
1996 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001997 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001998 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001999 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002000 break;
2001
2002 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002003 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002004 break;
2005
2006 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002007 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002008
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);
Michal Vasko004d3152020-06-11 19:59:22 +02002011 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002012 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 +02002013 return LY_EVALID;
2014 }
Radek Krejci0f969882020-08-21 16:56:47 +02002015 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002016 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002017 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002018 goto reparse_predicate;
2019 break;
2020
2021 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002022 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002023
2024 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002025 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002026 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002027 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002028
2029 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002030 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002031 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002032 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002033
2034reparse_predicate:
2035 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002036 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002037 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002038 LY_CHECK_RET(rc);
2039 }
2040 break;
2041 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002042 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 +02002043 return LY_EVALID;
2044 }
Michal Vasko004d3152020-06-11 19:59:22 +02002045 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002046
2047 return LY_SUCCESS;
2048}
2049
2050/**
2051 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2052 *
2053 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2054 *
2055 * @param[in] ctx Context for logging.
2056 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002057 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002058 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002059 * @return LY_ERR
2060 */
2061static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002062reparse_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 +02002063{
2064 LY_ERR rc;
2065
Michal Vasko004d3152020-06-11 19:59:22 +02002066 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 +02002067
2068 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002069 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002070 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002071 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002072
Michal Vasko004d3152020-06-11 19:59:22 +02002073 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002074 return LY_SUCCESS;
2075 }
Michal Vasko004d3152020-06-11 19:59:22 +02002076 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002077 case LYXP_TOKEN_DOT:
2078 case LYXP_TOKEN_DDOT:
2079 case LYXP_TOKEN_AT:
2080 case LYXP_TOKEN_NAMETEST:
2081 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002082 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002083 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002084 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002085 default:
2086 break;
2087 }
2088
Michal Vasko03ff5a72019-09-11 13:49:33 +02002089 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002090 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002091 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002092
aPiecekbf968d92021-05-27 14:35:05 +02002093 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002094 LY_CHECK_RET(rc);
2095 }
2096
2097 return LY_SUCCESS;
2098}
2099
2100/**
2101 * @brief Reparse FunctionCall. Logs directly on error.
2102 *
2103 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2104 *
2105 * @param[in] ctx Context for logging.
2106 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002107 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002108 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002109 * @return LY_ERR
2110 */
2111static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002112reparse_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 +02002113{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002114 int8_t min_arg_count = -1;
2115 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002116 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002117 LY_ERR rc;
2118
Michal Vasko004d3152020-06-11 19:59:22 +02002119 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002120 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002121 func_tok_idx = *tok_idx;
2122 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002123 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002124 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002125 min_arg_count = 1;
2126 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002127 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002128 min_arg_count = 1;
2129 max_arg_count = 1;
2130 }
2131 break;
2132 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002133 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002134 min_arg_count = 1;
2135 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002136 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002137 min_arg_count = 0;
2138 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002139 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002140 min_arg_count = 0;
2141 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002142 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002143 min_arg_count = 0;
2144 max_arg_count = 0;
2145 }
2146 break;
2147 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002148 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
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]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002152 min_arg_count = 0;
2153 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002154 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002155 min_arg_count = 1;
2156 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002157 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
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]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002161 min_arg_count = 1;
2162 max_arg_count = 1;
2163 }
2164 break;
2165 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002166 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002167 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002168 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002169 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002170 min_arg_count = 0;
2171 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002172 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002173 min_arg_count = 0;
2174 max_arg_count = 1;
2175 }
2176 break;
2177 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002178 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
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]], "ceiling", 7)) {
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]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002185 min_arg_count = 0;
2186 max_arg_count = 0;
2187 }
2188 break;
2189 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002190 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002191 min_arg_count = 2;
2192 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002193 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002194 min_arg_count = 0;
2195 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002196 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002197 min_arg_count = 2;
2198 max_arg_count = 2;
2199 }
2200 break;
2201 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002202 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002203 min_arg_count = 2;
2204 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002205 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002206 min_arg_count = 3;
2207 max_arg_count = 3;
2208 }
2209 break;
2210 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002211 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002212 min_arg_count = 0;
2213 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002214 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002215 min_arg_count = 1;
2216 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002217 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002218 min_arg_count = 2;
2219 max_arg_count = 2;
2220 }
2221 break;
2222 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002223 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002224 min_arg_count = 2;
2225 max_arg_count = 2;
2226 }
2227 break;
2228 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002229 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002230 min_arg_count = 2;
2231 max_arg_count = 2;
2232 }
2233 break;
2234 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002235 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
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]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002239 min_arg_count = 0;
2240 max_arg_count = 1;
2241 }
2242 break;
2243 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002244 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002245 min_arg_count = 0;
2246 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002247 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002248 min_arg_count = 2;
2249 max_arg_count = 2;
2250 }
2251 break;
2252 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002253 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002254 min_arg_count = 2;
2255 max_arg_count = 2;
2256 }
2257 break;
2258 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002259 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002260 min_arg_count = 2;
2261 max_arg_count = 2;
2262 }
2263 break;
2264 }
2265 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002266 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 +02002267 return LY_EINVAL;
2268 }
Michal Vasko004d3152020-06-11 19:59:22 +02002269 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002270
2271 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002272 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002273 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002274 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002275
2276 /* ( Expr ( ',' Expr )* )? */
2277 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002278 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002279 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002280 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002281 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002282 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002283 LY_CHECK_RET(rc);
2284 }
Michal Vasko004d3152020-06-11 19:59:22 +02002285 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2286 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002287
2288 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002289 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002290 LY_CHECK_RET(rc);
2291 }
2292
2293 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002294 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002295 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002296 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002297
Radek Krejci857189e2020-09-01 13:26:36 +02002298 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002299 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 +02002300 return LY_EVALID;
2301 }
2302
2303 return LY_SUCCESS;
2304}
2305
2306/**
2307 * @brief Reparse PathExpr. Logs directly on error.
2308 *
2309 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2310 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2311 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2312 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2313 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2314 *
2315 * @param[in] ctx Context for logging.
2316 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002317 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002318 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002319 * @return LY_ERR
2320 */
2321static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002322reparse_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 +02002323{
2324 LY_ERR rc;
2325
Michal Vasko004d3152020-06-11 19:59:22 +02002326 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002327 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002328 }
2329
Michal Vasko004d3152020-06-11 19:59:22 +02002330 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002331 case LYXP_TOKEN_PAR1:
2332 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002333 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002334
aPiecekbf968d92021-05-27 14:35:05 +02002335 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002336 LY_CHECK_RET(rc);
2337
Michal Vasko004d3152020-06-11 19:59:22 +02002338 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002339 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002340 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002341 goto predicate;
2342 break;
2343 case LYXP_TOKEN_DOT:
2344 case LYXP_TOKEN_DDOT:
2345 case LYXP_TOKEN_AT:
2346 case LYXP_TOKEN_NAMETEST:
2347 case LYXP_TOKEN_NODETYPE:
2348 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002349 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002350 LY_CHECK_RET(rc);
2351 break;
2352 case LYXP_TOKEN_FUNCNAME:
2353 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002354 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002355 LY_CHECK_RET(rc);
2356 goto predicate;
2357 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002358 case LYXP_TOKEN_OPER_PATH:
2359 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002360 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002361 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002362 LY_CHECK_RET(rc);
2363 break;
2364 case LYXP_TOKEN_LITERAL:
2365 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002366 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002367 goto predicate;
2368 break;
2369 case LYXP_TOKEN_NUMBER:
2370 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002371 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002372 goto predicate;
2373 break;
2374 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002375 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 +02002376 return LY_EVALID;
2377 }
2378
2379 return LY_SUCCESS;
2380
2381predicate:
2382 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002383 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002384 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002385 LY_CHECK_RET(rc);
2386 }
2387
2388 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002389 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002390
2391 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002392 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002393
aPiecekbf968d92021-05-27 14:35:05 +02002394 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002395 LY_CHECK_RET(rc);
2396 }
2397
2398 return LY_SUCCESS;
2399}
2400
2401/**
2402 * @brief Reparse UnaryExpr. Logs directly on error.
2403 *
2404 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2405 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2406 *
2407 * @param[in] ctx Context for logging.
2408 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002409 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002410 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002411 * @return LY_ERR
2412 */
2413static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002414reparse_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 +02002415{
2416 uint16_t prev_exp;
2417 LY_ERR rc;
2418
2419 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002420 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002421 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2422 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002423 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002424 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002425 }
2426
2427 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002428 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002429 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002430 LY_CHECK_RET(rc);
2431
2432 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002433 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002434 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002435 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002436
aPiecekbf968d92021-05-27 14:35:05 +02002437 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002438 LY_CHECK_RET(rc);
2439 }
2440
2441 return LY_SUCCESS;
2442}
2443
2444/**
2445 * @brief Reparse AdditiveExpr. Logs directly on error.
2446 *
2447 * [15] AdditiveExpr ::= MultiplicativeExpr
2448 * | AdditiveExpr '+' MultiplicativeExpr
2449 * | AdditiveExpr '-' MultiplicativeExpr
2450 * [16] MultiplicativeExpr ::= UnaryExpr
2451 * | MultiplicativeExpr '*' UnaryExpr
2452 * | MultiplicativeExpr 'div' UnaryExpr
2453 * | MultiplicativeExpr 'mod' UnaryExpr
2454 *
2455 * @param[in] ctx Context for logging.
2456 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002457 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002458 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002459 * @return LY_ERR
2460 */
2461static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002462reparse_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 +02002463{
2464 uint16_t prev_add_exp, prev_mul_exp;
2465 LY_ERR rc;
2466
Michal Vasko004d3152020-06-11 19:59:22 +02002467 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002468 goto reparse_multiplicative_expr;
2469
2470 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002471 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2472 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002473 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002474 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002475
2476reparse_multiplicative_expr:
2477 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002478 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002479 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002480 LY_CHECK_RET(rc);
2481
2482 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002483 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2484 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002485 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002486 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002487
aPiecekbf968d92021-05-27 14:35:05 +02002488 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002489 LY_CHECK_RET(rc);
2490 }
2491 }
2492
2493 return LY_SUCCESS;
2494}
2495
2496/**
2497 * @brief Reparse EqualityExpr. Logs directly on error.
2498 *
2499 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2500 * | EqualityExpr '!=' RelationalExpr
2501 * [14] RelationalExpr ::= AdditiveExpr
2502 * | RelationalExpr '<' AdditiveExpr
2503 * | RelationalExpr '>' AdditiveExpr
2504 * | RelationalExpr '<=' AdditiveExpr
2505 * | RelationalExpr '>=' AdditiveExpr
2506 *
2507 * @param[in] ctx Context for logging.
2508 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002509 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002510 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002511 * @return LY_ERR
2512 */
2513static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002514reparse_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 +02002515{
2516 uint16_t prev_eq_exp, prev_rel_exp;
2517 LY_ERR rc;
2518
Michal Vasko004d3152020-06-11 19:59:22 +02002519 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002520 goto reparse_additive_expr;
2521
2522 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002523 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002524 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002525 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002526
2527reparse_additive_expr:
2528 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002529 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002530 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002531 LY_CHECK_RET(rc);
2532
2533 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002534 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002535 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002536 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002537
aPiecekbf968d92021-05-27 14:35:05 +02002538 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002539 LY_CHECK_RET(rc);
2540 }
2541 }
2542
2543 return LY_SUCCESS;
2544}
2545
2546/**
2547 * @brief Reparse OrExpr. Logs directly on error.
2548 *
2549 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2550 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2551 *
2552 * @param[in] ctx Context for logging.
2553 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002554 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002555 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002556 * @return LY_ERR
2557 */
2558static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002559reparse_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 +02002560{
2561 uint16_t prev_or_exp, prev_and_exp;
2562 LY_ERR rc;
2563
aPiecekbf968d92021-05-27 14:35:05 +02002564 ++depth;
2565 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2566
Michal Vasko004d3152020-06-11 19:59:22 +02002567 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002568 goto reparse_equality_expr;
2569
2570 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002571 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 +02002572 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002573 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002574
2575reparse_equality_expr:
2576 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002577 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002578 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002579 LY_CHECK_RET(rc);
2580
2581 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002582 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 +02002583 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002584 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002585
aPiecekbf968d92021-05-27 14:35:05 +02002586 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002587 LY_CHECK_RET(rc);
2588 }
2589 }
2590
2591 return LY_SUCCESS;
2592}
Radek Krejcib1646a92018-11-02 16:08:26 +01002593
2594/**
2595 * @brief Parse NCName.
2596 *
2597 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002598 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002599 */
Radek Krejcid4270262019-01-07 15:07:25 +01002600static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002601parse_ncname(const char *ncname)
2602{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002603 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002604 size_t size;
2605 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002606
2607 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2608 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2609 return len;
2610 }
2611
2612 do {
2613 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002614 if (!*ncname) {
2615 break;
2616 }
Radek Krejcid4270262019-01-07 15:07:25 +01002617 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002618 } while (is_xmlqnamechar(uc) && (uc != ':'));
2619
2620 return len;
2621}
2622
2623/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002624 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002625 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002626 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002627 * @param[in] exp Expression to use.
2628 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002629 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002630 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002631 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002632 */
2633static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002634exp_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 +01002635{
2636 uint32_t prev;
2637
2638 if (exp->used == exp->size) {
2639 prev = exp->size;
2640 exp->size += LYXP_EXPR_SIZE_STEP;
2641 if (prev > exp->size) {
2642 LOGINT(ctx);
2643 return LY_EINT;
2644 }
2645
2646 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2647 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2648 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2649 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2650 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2651 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2652 }
2653
2654 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002655 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002656 exp->tok_len[exp->used] = tok_len;
2657 ++exp->used;
2658 return LY_SUCCESS;
2659}
2660
2661void
Michal Vasko14676352020-05-29 11:35:55 +02002662lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002663{
2664 uint16_t i;
2665
2666 if (!expr) {
2667 return;
2668 }
2669
2670 lydict_remove(ctx, expr->expr);
2671 free(expr->tokens);
2672 free(expr->tok_pos);
2673 free(expr->tok_len);
2674 if (expr->repeat) {
2675 for (i = 0; i < expr->used; ++i) {
2676 free(expr->repeat[i]);
2677 }
2678 }
2679 free(expr->repeat);
2680 free(expr);
2681}
2682
Radek Krejcif03a9e22020-09-18 20:09:31 +02002683LY_ERR
2684lyxp_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 +01002685{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002686 LY_ERR ret = LY_SUCCESS;
2687 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002688 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002689 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002690 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002691 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002692
Radek Krejcif03a9e22020-09-18 20:09:31 +02002693 assert(expr_p);
2694
2695 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002696 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002697 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002698 }
2699
2700 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002701 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002702 }
2703 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002704 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002705 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002706 }
2707
2708 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002709 expr = calloc(1, sizeof *expr);
2710 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2711 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2712 expr->used = 0;
2713 expr->size = LYXP_EXPR_SIZE_START;
2714 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2715 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002716
Radek Krejcif03a9e22020-09-18 20:09:31 +02002717 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2718 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002719
Radek Krejcif03a9e22020-09-18 20:09:31 +02002720 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2721 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002722
Michal Vasko004d3152020-06-11 19:59:22 +02002723 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002724 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002725
Radek Krejcif03a9e22020-09-18 20:09:31 +02002726 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002727 ++parsed;
2728 }
2729
2730 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002731 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002732
2733 /* '(' */
2734 tok_len = 1;
2735 tok_type = LYXP_TOKEN_PAR1;
2736
Radek Krejcif03a9e22020-09-18 20:09:31 +02002737 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002738 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002739 if (((expr->tok_len[expr->used - 1] == 4) &&
2740 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2741 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2742 ((expr->tok_len[expr->used - 1] == 7) &&
2743 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002744 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002745 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002746 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002747 }
2748 prev_function_check = 0;
2749 }
2750
Radek Krejcif03a9e22020-09-18 20:09:31 +02002751 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002752
2753 /* ')' */
2754 tok_len = 1;
2755 tok_type = LYXP_TOKEN_PAR2;
2756
Radek Krejcif03a9e22020-09-18 20:09:31 +02002757 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002758
2759 /* '[' */
2760 tok_len = 1;
2761 tok_type = LYXP_TOKEN_BRACK1;
2762
Radek Krejcif03a9e22020-09-18 20:09:31 +02002763 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002764
2765 /* ']' */
2766 tok_len = 1;
2767 tok_type = LYXP_TOKEN_BRACK2;
2768
Radek Krejcif03a9e22020-09-18 20:09:31 +02002769 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002770
2771 /* '..' */
2772 tok_len = 2;
2773 tok_type = LYXP_TOKEN_DDOT;
2774
Radek Krejcif03a9e22020-09-18 20:09:31 +02002775 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002776
2777 /* '.' */
2778 tok_len = 1;
2779 tok_type = LYXP_TOKEN_DOT;
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_AT;
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_COMMA;
2792
Radek Krejcif03a9e22020-09-18 20:09:31 +02002793 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002794
2795 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002796 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2797 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002798 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002799 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002800 ++tok_len;
2801 tok_type = LYXP_TOKEN_LITERAL;
2802
Radek Krejcif03a9e22020-09-18 20:09:31 +02002803 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002804
2805 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002806 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2807 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002808 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002809 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002810 ++tok_len;
2811 tok_type = LYXP_TOKEN_LITERAL;
2812
Radek Krejcif03a9e22020-09-18 20:09:31 +02002813 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002814
2815 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002816 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2817 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002818 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002819 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002820 }
2821 tok_type = LYXP_TOKEN_NUMBER;
2822
Radek Krejcif03a9e22020-09-18 20:09:31 +02002823 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002824
2825 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002826 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002827 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002828 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002829 } else {
2830 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002831 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002832 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002833
Radek Krejcif03a9e22020-09-18 20:09:31 +02002834 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002835
Michal Vasko3e48bf32020-06-01 08:39:07 +02002836 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002837 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002838 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2839
Radek Krejcif03a9e22020-09-18 20:09:31 +02002840 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002841
2842 /* Operator '<=', '>=' */
2843 tok_len = 2;
2844 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002845
Radek Krejcif03a9e22020-09-18 20:09:31 +02002846 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002847
2848 /* Operator '|' */
2849 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002850 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002851
Radek Krejcif03a9e22020-09-18 20:09:31 +02002852 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002853
2854 /* Operator '+', '-' */
2855 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002856 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002857
Radek Krejcif03a9e22020-09-18 20:09:31 +02002858 } else if (expr_str[parsed] == '=') {
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 = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002862 tok_type = LYXP_TOKEN_OPER_EQUAL;
2863
Radek Krejcif03a9e22020-09-18 20:09:31 +02002864 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002865
2866 /* Operator '<', '>' */
2867 tok_len = 1;
2868 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002869
Michal Vasko69730152020-10-09 16:30:07 +02002870 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2871 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2872 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2873 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2874 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2875 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2876 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2877 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2878 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2879 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2880 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2881 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002882
2883 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002884 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002885 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002886 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002887
Radek Krejcif03a9e22020-09-18 20:09:31 +02002888 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002889 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002890 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002891
Radek Krejcif03a9e22020-09-18 20:09:31 +02002892 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002893 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002894 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002895
Radek Krejcif03a9e22020-09-18 20:09:31 +02002896 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002897 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002898 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002899
2900 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002901 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 +02002902 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 +02002903 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002904 goto error;
2905 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02002906 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002907 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002908 goto error;
2909 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002910 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002911
2912 /* NameTest '*' */
2913 tok_len = 1;
2914 tok_type = LYXP_TOKEN_NAMETEST;
2915
2916 } else {
2917
2918 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002919 long int ncname_len = parse_ncname(&expr_str[parsed]);
Michal Vasko774ce402021-04-14 15:35:06 +02002920 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2921 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002922 tok_len = ncname_len;
2923
Radek Krejcif03a9e22020-09-18 20:09:31 +02002924 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002925 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002926 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002927 ++tok_len;
2928 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002929 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vasko774ce402021-04-14 15:35:06 +02002930 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2931 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002932 tok_len += ncname_len;
2933 }
2934 /* remove old flag to prevent ambiguities */
2935 prev_function_check = 0;
2936 tok_type = LYXP_TOKEN_NAMETEST;
2937 } else {
2938 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2939 prev_function_check = 1;
2940 tok_type = LYXP_TOKEN_NAMETEST;
2941 }
2942 }
2943
2944 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002945 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002946 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002947 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002948 ++parsed;
2949 }
2950
Radek Krejcif03a9e22020-09-18 20:09:31 +02002951 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002952
Michal Vasko004d3152020-06-11 19:59:22 +02002953 if (reparse) {
2954 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002955 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2956 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002957
Michal Vasko004d3152020-06-11 19:59:22 +02002958 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02002959 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002960 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002961 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002962 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002963 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002964 goto error;
2965 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002966 }
2967
Radek Krejcif03a9e22020-09-18 20:09:31 +02002968 print_expr_struct_debug(expr);
2969 *expr_p = expr;
2970 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002971
2972error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002973 lyxp_expr_free(ctx, expr);
2974 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002975}
2976
Michal Vasko1734be92020-09-22 08:55:10 +02002977LY_ERR
2978lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02002979{
Michal Vasko1734be92020-09-22 08:55:10 +02002980 LY_ERR ret = LY_SUCCESS;
2981 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02002982 uint32_t i, j;
2983
Michal Vasko7f45cf22020-10-01 12:49:44 +02002984 if (!exp) {
2985 goto cleanup;
2986 }
2987
Michal Vasko004d3152020-06-11 19:59:22 +02002988 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02002989 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002990
2991 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
Michal Vasko1734be92020-09-22 08:55:10 +02002992 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002993 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2994
2995 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
Michal Vasko1734be92020-09-22 08:55:10 +02002996 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002997 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2998
2999 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
Michal Vasko1734be92020-09-22 08:55:10 +02003000 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003001 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
3002
3003 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02003004 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003005 for (i = 0; i < exp->used; ++i) {
3006 if (!exp->repeat[i]) {
3007 dup->repeat[i] = NULL;
3008 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02003009 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02003010 /* the ending 0 as well */
3011 ++j;
3012
Michal Vasko99c71642020-07-03 13:33:36 +02003013 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02003014 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003015 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3016 dup->repeat[i][j - 1] = 0;
3017 }
3018 }
3019
3020 dup->used = exp->used;
3021 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003022 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003023
Michal Vasko1734be92020-09-22 08:55:10 +02003024cleanup:
3025 if (ret) {
3026 lyxp_expr_free(ctx, dup);
3027 } else {
3028 *dup_p = dup;
3029 }
3030 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003031}
3032
Michal Vasko03ff5a72019-09-11 13:49:33 +02003033/*
3034 * warn functions
3035 *
3036 * Warn functions check specific reasonable conditions for schema XPath
3037 * and print a warning if they are not satisfied.
3038 */
3039
3040/**
3041 * @brief Get the last-added schema node that is currently in the context.
3042 *
3043 * @param[in] set Set to search in.
3044 * @return Last-added schema context node, NULL if no node is in context.
3045 */
3046static struct lysc_node *
3047warn_get_scnode_in_ctx(struct lyxp_set *set)
3048{
3049 uint32_t i;
3050
3051 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3052 return NULL;
3053 }
3054
3055 i = set->used;
3056 do {
3057 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003058 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003059 /* if there are more, simply return the first found (last added) */
3060 return set->val.scnodes[i].scnode;
3061 }
3062 } while (i);
3063
3064 return NULL;
3065}
3066
3067/**
3068 * @brief Test whether a type is numeric - integer type or decimal64.
3069 *
3070 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003071 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003072 */
Radek Krejci857189e2020-09-01 13:26:36 +02003073static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003074warn_is_numeric_type(struct lysc_type *type)
3075{
3076 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003077 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003078 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003079
3080 switch (type->basetype) {
3081 case LY_TYPE_DEC64:
3082 case LY_TYPE_INT8:
3083 case LY_TYPE_UINT8:
3084 case LY_TYPE_INT16:
3085 case LY_TYPE_UINT16:
3086 case LY_TYPE_INT32:
3087 case LY_TYPE_UINT32:
3088 case LY_TYPE_INT64:
3089 case LY_TYPE_UINT64:
3090 return 1;
3091 case LY_TYPE_UNION:
3092 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003093 LY_ARRAY_FOR(uni->types, u) {
3094 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003095 if (ret) {
3096 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003097 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003098 }
3099 }
3100 /* did not find any suitable type */
3101 return 0;
3102 case LY_TYPE_LEAFREF:
3103 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3104 default:
3105 return 0;
3106 }
3107}
3108
3109/**
3110 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3111 *
3112 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003113 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003114 */
Radek Krejci857189e2020-09-01 13:26:36 +02003115static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003116warn_is_string_type(struct lysc_type *type)
3117{
3118 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003119 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003120 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003121
3122 switch (type->basetype) {
3123 case LY_TYPE_BITS:
3124 case LY_TYPE_ENUM:
3125 case LY_TYPE_IDENT:
3126 case LY_TYPE_INST:
3127 case LY_TYPE_STRING:
3128 return 1;
3129 case LY_TYPE_UNION:
3130 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003131 LY_ARRAY_FOR(uni->types, u) {
3132 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003133 if (ret) {
3134 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003135 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003136 }
3137 }
3138 /* did not find any suitable type */
3139 return 0;
3140 case LY_TYPE_LEAFREF:
3141 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3142 default:
3143 return 0;
3144 }
3145}
3146
3147/**
3148 * @brief Test whether a type is one specific type.
3149 *
3150 * @param[in] type Type to test.
3151 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003152 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003153 */
Radek Krejci857189e2020-09-01 13:26:36 +02003154static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003155warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3156{
3157 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003158 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003159 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003160
3161 if (type->basetype == base) {
3162 return 1;
3163 } else if (type->basetype == LY_TYPE_UNION) {
3164 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003165 LY_ARRAY_FOR(uni->types, u) {
3166 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003167 if (ret) {
3168 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003169 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003170 }
3171 }
3172 /* did not find any suitable type */
3173 return 0;
3174 } else if (type->basetype == LY_TYPE_LEAFREF) {
3175 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3176 }
3177
3178 return 0;
3179}
3180
3181/**
3182 * @brief Get next type of a (union) type.
3183 *
3184 * @param[in] type Base type.
3185 * @param[in] prev_type Previously returned type.
3186 * @return Next type or NULL.
3187 */
3188static struct lysc_type *
3189warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3190{
3191 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003192 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003193 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003194
3195 switch (type->basetype) {
3196 case LY_TYPE_UNION:
3197 uni = (struct lysc_type_union *)type;
3198 if (!prev_type) {
3199 return uni->types[0];
3200 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003201 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003202 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003203 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003204 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003205 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003206 found = 1;
3207 }
3208 }
3209 return NULL;
3210 default:
3211 if (prev_type) {
3212 assert(type == prev_type);
3213 return NULL;
3214 } else {
3215 return type;
3216 }
3217 }
3218}
3219
3220/**
3221 * @brief Test whether 2 types have a common type.
3222 *
3223 * @param[in] type1 First type.
3224 * @param[in] type2 Second type.
3225 * @return 1 if they do, 0 otherwise.
3226 */
3227static int
3228warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3229{
3230 struct lysc_type *t1, *rt1, *t2, *rt2;
3231
3232 t1 = NULL;
3233 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3234 if (t1->basetype == LY_TYPE_LEAFREF) {
3235 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3236 } else {
3237 rt1 = t1;
3238 }
3239
3240 t2 = NULL;
3241 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3242 if (t2->basetype == LY_TYPE_LEAFREF) {
3243 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3244 } else {
3245 rt2 = t2;
3246 }
3247
3248 if (rt2->basetype == rt1->basetype) {
3249 /* match found */
3250 return 1;
3251 }
3252 }
3253 }
3254
3255 return 0;
3256}
3257
3258/**
3259 * @brief Check both operands of comparison operators.
3260 *
3261 * @param[in] ctx Context for errors.
3262 * @param[in] set1 First operand set.
3263 * @param[in] set2 Second operand set.
3264 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3265 * @param[in] expr Start of the expression to print with the warning.
3266 * @param[in] tok_pos Token position.
3267 */
3268static void
Radek Krejci857189e2020-09-01 13:26:36 +02003269warn_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 +02003270{
3271 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003272 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003273
3274 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3275 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3276
3277 if (!node1 && !node2) {
3278 /* no node-sets involved, nothing to do */
3279 return;
3280 }
3281
3282 if (node1) {
3283 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3284 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3285 warning = 1;
3286 leaves = 0;
3287 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3288 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3289 warning = 1;
3290 }
3291 }
3292
3293 if (node2) {
3294 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3295 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3296 warning = 1;
3297 leaves = 0;
3298 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3299 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3300 warning = 1;
3301 }
3302 }
3303
3304 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003305 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3306 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3307 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3308 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003309 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3310 warning = 1;
3311 }
3312 }
3313
3314 if (warning) {
3315 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3316 }
3317}
3318
3319/**
3320 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3321 *
3322 * @param[in] exp Parsed XPath expression.
3323 * @param[in] set Set with the leaf/leaf-list.
3324 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3325 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3326 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3327 */
3328static void
Michal Vasko40308e72020-10-20 16:38:40 +02003329warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3330 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003331{
3332 struct lysc_node *scnode;
3333 struct lysc_type *type;
3334 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003335 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003336 LY_ERR rc;
3337 struct ly_err_item *err = NULL;
3338
Michal Vasko69730152020-10-09 16:30:07 +02003339 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3340 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003341 /* check that the node can have the specified value */
3342 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3343 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3344 } else {
3345 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3346 }
3347 if (!value) {
3348 LOGMEM(set->ctx);
3349 return;
3350 }
3351
3352 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3353 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003354 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003355 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003356 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003357 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003358 }
3359
3360 type = ((struct lysc_node_leaf *)scnode)->type;
3361 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003362 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003363 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003364 if (rc == LY_EINCOMPLETE) {
3365 rc = LY_SUCCESS;
3366 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003367
3368 if (err) {
3369 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3370 ly_err_free(err);
3371 } else if (rc != LY_SUCCESS) {
3372 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3373 }
3374 if (rc != LY_SUCCESS) {
3375 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003376 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003377 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003378 } else {
3379 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003380 }
3381 }
3382 free(value);
3383 }
3384}
3385
3386/*
3387 * XPath functions
3388 */
3389
3390/**
3391 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3392 * depending on whether the first node bit value from the second argument is set.
3393 *
3394 * @param[in] args Array of arguments.
3395 * @param[in] arg_count Count of elements in @p args.
3396 * @param[in,out] set Context and result set at the same time.
3397 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003398 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003399 */
3400static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003401xpath_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 +02003402{
3403 struct lyd_node_term *leaf;
3404 struct lysc_node_leaf *sleaf;
3405 struct lysc_type_bits *bits;
3406 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003407 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003408
3409 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003410 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003411 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003412 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3413 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3414 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3415 sleaf->name);
3416 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3417 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3418 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003419 }
3420
3421 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3422 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003423 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3424 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003425 } else if (!warn_is_string_type(sleaf->type)) {
3426 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003427 }
3428 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003429 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003430 return rc;
3431 }
3432
Michal Vaskod3678892020-05-21 10:06:58 +02003433 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003434 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 +02003435 return LY_EVALID;
3436 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003437 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003438 LY_CHECK_RET(rc);
3439
3440 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003441 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003442 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003443 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3444 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003445 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003446 LY_ARRAY_FOR(bits->bits, u) {
3447 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003448 set_fill_boolean(set, 1);
3449 break;
3450 }
3451 }
3452 }
3453 }
3454
3455 return LY_SUCCESS;
3456}
3457
3458/**
3459 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3460 * with the argument converted to boolean.
3461 *
3462 * @param[in] args Array of arguments.
3463 * @param[in] arg_count Count of elements in @p args.
3464 * @param[in,out] set Context and result set at the same time.
3465 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003466 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003467 */
3468static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003469xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003470{
3471 LY_ERR rc;
3472
3473 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003474 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003475 return LY_SUCCESS;
3476 }
3477
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003478 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003479 LY_CHECK_RET(rc);
3480 set_fill_set(set, args[0]);
3481
3482 return LY_SUCCESS;
3483}
3484
3485/**
3486 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3487 * with the first argument rounded up to the nearest integer.
3488 *
3489 * @param[in] args Array of arguments.
3490 * @param[in] arg_count Count of elements in @p args.
3491 * @param[in,out] set Context and result set at the same time.
3492 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003493 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003494 */
3495static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003496xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003497{
3498 struct lysc_node_leaf *sleaf;
3499 LY_ERR rc = LY_SUCCESS;
3500
3501 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003502 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003503 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003504 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3505 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3506 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3507 sleaf->name);
3508 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3509 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3510 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003511 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003512 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003513 return rc;
3514 }
3515
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003516 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003517 LY_CHECK_RET(rc);
3518 if ((long long)args[0]->val.num != args[0]->val.num) {
3519 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3520 } else {
3521 set_fill_number(set, args[0]->val.num);
3522 }
3523
3524 return LY_SUCCESS;
3525}
3526
3527/**
3528 * @brief Execute the XPath concat(string, string, string*) function.
3529 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3530 *
3531 * @param[in] args Array of arguments.
3532 * @param[in] arg_count Count of elements in @p args.
3533 * @param[in,out] set Context and result set at the same time.
3534 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003535 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003536 */
3537static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003538xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003539{
3540 uint16_t i;
3541 char *str = NULL;
3542 size_t used = 1;
3543 LY_ERR rc = LY_SUCCESS;
3544 struct lysc_node_leaf *sleaf;
3545
3546 if (options & LYXP_SCNODE_ALL) {
3547 for (i = 0; i < arg_count; ++i) {
3548 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3549 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3550 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003551 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003552 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003553 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 +02003554 }
3555 }
3556 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003557 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003558 return rc;
3559 }
3560
3561 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003562 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003563 if (rc != LY_SUCCESS) {
3564 free(str);
3565 return rc;
3566 }
3567
3568 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3569 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3570 strcpy(str + used - 1, args[i]->val.str);
3571 used += strlen(args[i]->val.str);
3572 }
3573
3574 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003575 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003576 set->type = LYXP_SET_STRING;
3577 set->val.str = str;
3578
3579 return LY_SUCCESS;
3580}
3581
3582/**
3583 * @brief Execute the XPath contains(string, string) function.
3584 * Returns LYXP_SET_BOOLEAN whether the second argument can
3585 * be found in the first or not.
3586 *
3587 * @param[in] args Array of arguments.
3588 * @param[in] arg_count Count of elements in @p args.
3589 * @param[in,out] set Context and result set at the same time.
3590 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003591 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003592 */
3593static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003594xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003595{
3596 struct lysc_node_leaf *sleaf;
3597 LY_ERR rc = LY_SUCCESS;
3598
3599 if (options & LYXP_SCNODE_ALL) {
3600 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3601 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003602 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3603 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003604 } else if (!warn_is_string_type(sleaf->type)) {
3605 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003606 }
3607 }
3608
3609 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3610 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003611 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3612 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003613 } else if (!warn_is_string_type(sleaf->type)) {
3614 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003615 }
3616 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003617 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003618 return rc;
3619 }
3620
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003621 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003622 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003623 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003624 LY_CHECK_RET(rc);
3625
3626 if (strstr(args[0]->val.str, args[1]->val.str)) {
3627 set_fill_boolean(set, 1);
3628 } else {
3629 set_fill_boolean(set, 0);
3630 }
3631
3632 return LY_SUCCESS;
3633}
3634
3635/**
3636 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3637 * with the size of the node-set from the argument.
3638 *
3639 * @param[in] args Array of arguments.
3640 * @param[in] arg_count Count of elements in @p args.
3641 * @param[in,out] set Context and result set at the same time.
3642 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003643 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003644 */
3645static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003646xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003647{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003648 LY_ERR rc = LY_SUCCESS;
3649
3650 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003651 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003652 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003653 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003654 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003655 return rc;
3656 }
3657
Michal Vasko03ff5a72019-09-11 13:49:33 +02003658 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003659 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003660 return LY_EVALID;
3661 }
3662
3663 set_fill_number(set, args[0]->used);
3664 return LY_SUCCESS;
3665}
3666
3667/**
3668 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3669 * with the context with the intial node.
3670 *
3671 * @param[in] args Array of arguments.
3672 * @param[in] arg_count Count of elements in @p args.
3673 * @param[in,out] set Context and result set at the same time.
3674 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003675 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003676 */
3677static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003678xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003679{
3680 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003681 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003682 return LY_EVALID;
3683 }
3684
3685 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003686 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003687
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003688 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003689 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003690 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003691
3692 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003693 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003694 }
3695
3696 return LY_SUCCESS;
3697}
3698
3699/**
3700 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3701 * leafref or instance-identifier target node(s).
3702 *
3703 * @param[in] args Array of arguments.
3704 * @param[in] arg_count Count of elements in @p args.
3705 * @param[in,out] set Context and result set at the same time.
3706 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003707 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003708 */
3709static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003710xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003711{
3712 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003713 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003714 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003715 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003716 struct ly_path *p;
3717 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003718 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003719 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003720 LY_ERR rc = LY_SUCCESS;
3721
3722 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003723 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003724 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003725 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3726 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3727 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3728 sleaf->name);
3729 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3730 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3731 __func__, sleaf->name);
3732 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003733 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003734 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko42e497c2020-01-06 08:38:25 +01003735 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003736 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003737 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003738
3739 /* it was already evaluated on schema, it must succeed */
Radek Krejcid5d37432021-03-12 13:46:40 +01003740 rc = ly_path_compile(set->ctx, lref->cur_mod, &sleaf->node, NULL, lref->path, LY_PATH_LREF_TRUE, oper,
Radek Krejci8df109d2021-04-23 12:19:08 +02003741 LY_PATH_TARGET_MANY, LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, NULL, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003742 assert(!rc);
3743
3744 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003745 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003746 ly_path_free(set->ctx, p);
3747
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003748 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003749 }
3750
Michal Vasko03ff5a72019-09-11 13:49:33 +02003751 return rc;
3752 }
3753
Michal Vaskod3678892020-05-21 10:06:58 +02003754 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003755 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003756 return LY_EVALID;
3757 }
3758
Michal Vaskod3678892020-05-21 10:06:58 +02003759 lyxp_set_free_content(set);
3760 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003761 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3762 sleaf = (struct lysc_node_leaf *)leaf->schema;
3763 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3764 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3765 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02003766 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01003767 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003768 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003769 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003770 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003771 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003772 } else {
3773 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003774 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003775 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003776 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003777 return LY_EVALID;
3778 }
3779 }
Michal Vasko004d3152020-06-11 19:59:22 +02003780
3781 /* insert it */
3782 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003783 }
3784 }
3785
3786 return LY_SUCCESS;
3787}
3788
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003789static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003790xpath_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 +02003791{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01003792 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003793 LY_ARRAY_COUNT_TYPE u;
3794 struct lyd_node_term *leaf;
3795 struct lysc_node_leaf *sleaf;
3796 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02003797 struct lyd_value *val;
3798 const struct lys_module *mod;
3799 const char *id_name;
3800 uint16_t id_len;
3801 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003802 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003803 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003804
3805 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003806 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003807 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003808 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3809 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3810 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3811 sleaf->name);
3812 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3813 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3814 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003815 }
3816
3817 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3818 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3819 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003820 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003821 } else if (!warn_is_string_type(sleaf->type)) {
3822 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3823 }
3824 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003825 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003826 return rc;
3827 }
3828
3829 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003830 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 +02003831 return LY_EVALID;
3832 }
3833 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3834 LY_CHECK_RET(rc);
3835
Michal Vasko93923692021-05-07 15:28:02 +02003836 /* parse the identity */
3837 id_name = args[1]->val.str;
3838 id_len = strlen(id_name);
3839 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
3840 LY_CHECK_RET(rc);
3841 if (!mod) {
3842 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
3843 return LY_EVALID;
3844 }
3845
3846 /* find the identity */
3847 found = 0;
3848 LY_ARRAY_FOR(mod->identities, u) {
3849 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
3850 /* we have match */
3851 found = 1;
3852 break;
3853 }
3854 }
3855 if (!found) {
3856 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
3857 return LY_EVALID;
3858 }
3859 id = &mod->identities[u];
3860
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003861 set_fill_boolean(set, 0);
3862 found = 0;
3863 for (i = 0; i < args[0]->used; ++i) {
3864 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3865 continue;
3866 }
3867
3868 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3869 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3870 sleaf = (struct lysc_node_leaf *)leaf->schema;
3871 val = &leaf->value;
3872 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3873 /* uninteresting */
3874 continue;
3875 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003876 } else {
3877 meta = args[0]->val.meta[i].meta;
3878 val = &meta->value;
3879 if (val->realtype->basetype != LY_TYPE_IDENT) {
3880 /* uninteresting */
3881 continue;
3882 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003883 }
3884
Michal Vasko93923692021-05-07 15:28:02 +02003885 /* check the identity itself */
3886 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003887 set_fill_boolean(set, 1);
3888 found = 1;
3889 }
Michal Vasko93923692021-05-07 15:28:02 +02003890 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
3891 set_fill_boolean(set, 1);
3892 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003893 }
3894
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003895 if (found) {
3896 break;
3897 }
3898 }
3899
3900 return LY_SUCCESS;
3901}
3902
Michal Vasko03ff5a72019-09-11 13:49:33 +02003903/**
3904 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3905 * on whether the first argument nodes contain a node of an identity derived from the second
3906 * argument identity.
3907 *
3908 * @param[in] args Array of arguments.
3909 * @param[in] arg_count Count of elements in @p args.
3910 * @param[in,out] set Context and result set at the same time.
3911 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003912 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003913 */
3914static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003915xpath_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 +02003916{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003917 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003918}
3919
3920/**
3921 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3922 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3923 * the second argument identity.
3924 *
3925 * @param[in] args Array of arguments.
3926 * @param[in] arg_count Count of elements in @p args.
3927 * @param[in,out] set Context and result set at the same time.
3928 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003929 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003930 */
3931static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003932xpath_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 +02003933{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003934 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003935}
3936
3937/**
3938 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3939 * with the integer value of the first node's enum value, otherwise NaN.
3940 *
3941 * @param[in] args Array of arguments.
3942 * @param[in] arg_count Count of elements in @p args.
3943 * @param[in,out] set Context and result set at the same time.
3944 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003945 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003946 */
3947static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003948xpath_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 +02003949{
3950 struct lyd_node_term *leaf;
3951 struct lysc_node_leaf *sleaf;
3952 LY_ERR rc = LY_SUCCESS;
3953
3954 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003955 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003956 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003957 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3958 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3959 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3960 sleaf->name);
3961 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3962 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
3963 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003964 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003965 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003966 return rc;
3967 }
3968
Michal Vaskod3678892020-05-21 10:06:58 +02003969 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003970 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003971 return LY_EVALID;
3972 }
3973
3974 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003975 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003976 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3977 sleaf = (struct lysc_node_leaf *)leaf->schema;
3978 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3979 set_fill_number(set, leaf->value.enum_item->value);
3980 }
3981 }
3982
3983 return LY_SUCCESS;
3984}
3985
3986/**
3987 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3988 * with false value.
3989 *
3990 * @param[in] args Array of arguments.
3991 * @param[in] arg_count Count of elements in @p args.
3992 * @param[in,out] set Context and result set at the same time.
3993 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003994 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003995 */
3996static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003997xpath_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 +02003998{
3999 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004000 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004001 return LY_SUCCESS;
4002 }
4003
4004 set_fill_boolean(set, 0);
4005 return LY_SUCCESS;
4006}
4007
4008/**
4009 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4010 * with the first argument floored (truncated).
4011 *
4012 * @param[in] args Array of arguments.
4013 * @param[in] arg_count Count of elements in @p args.
4014 * @param[in,out] set Context and result set at the same time.
4015 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004016 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004017 */
4018static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004019xpath_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 +02004020{
4021 LY_ERR rc;
4022
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004023 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004024 LY_CHECK_RET(rc);
4025 if (isfinite(args[0]->val.num)) {
4026 set_fill_number(set, (long long)args[0]->val.num);
4027 }
4028
4029 return LY_SUCCESS;
4030}
4031
4032/**
4033 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4034 * whether the language of the text matches the one from the argument.
4035 *
4036 * @param[in] args Array of arguments.
4037 * @param[in] arg_count Count of elements in @p args.
4038 * @param[in,out] set Context and result set at the same time.
4039 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004040 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004041 */
4042static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004043xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004044{
4045 const struct lyd_node *node;
4046 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004047 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004048 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004049 LY_ERR rc = LY_SUCCESS;
4050
4051 if (options & LYXP_SCNODE_ALL) {
4052 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4053 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004054 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4055 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004056 } else if (!warn_is_string_type(sleaf->type)) {
4057 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004058 }
4059 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004060 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004061 return rc;
4062 }
4063
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004064 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004065 LY_CHECK_RET(rc);
4066
Michal Vasko03ff5a72019-09-11 13:49:33 +02004067 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004068 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004069 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004070 } else if (!set->used) {
4071 set_fill_boolean(set, 0);
4072 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004073 }
4074
4075 switch (set->val.nodes[0].type) {
4076 case LYXP_NODE_ELEM:
4077 case LYXP_NODE_TEXT:
4078 node = set->val.nodes[0].node;
4079 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004080 case LYXP_NODE_META:
4081 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004082 break;
4083 default:
4084 /* nothing to do with roots */
4085 set_fill_boolean(set, 0);
4086 return LY_SUCCESS;
4087 }
4088
Michal Vasko9f96a052020-03-10 09:41:45 +01004089 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004090 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004091 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004092 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004093 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004094 break;
4095 }
4096 }
4097
Michal Vasko9f96a052020-03-10 09:41:45 +01004098 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004099 break;
4100 }
4101 }
4102
4103 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004104 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004105 set_fill_boolean(set, 0);
4106 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004107 uint64_t i;
4108
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004109 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004110 for (i = 0; args[0]->val.str[i]; ++i) {
4111 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4112 set_fill_boolean(set, 0);
4113 break;
4114 }
4115 }
4116 if (!args[0]->val.str[i]) {
4117 if (!val[i] || (val[i] == '-')) {
4118 set_fill_boolean(set, 1);
4119 } else {
4120 set_fill_boolean(set, 0);
4121 }
4122 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004123 }
4124
4125 return LY_SUCCESS;
4126}
4127
4128/**
4129 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4130 * with the context size.
4131 *
4132 * @param[in] args Array of arguments.
4133 * @param[in] arg_count Count of elements in @p args.
4134 * @param[in,out] set Context and result set at the same time.
4135 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004136 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004137 */
4138static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004139xpath_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 +02004140{
4141 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004142 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004143 return LY_SUCCESS;
4144 }
4145
Michal Vasko03ff5a72019-09-11 13:49:33 +02004146 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004147 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004148 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004149 } else if (!set->used) {
4150 set_fill_number(set, 0);
4151 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004152 }
4153
4154 set_fill_number(set, set->ctx_size);
4155 return LY_SUCCESS;
4156}
4157
4158/**
4159 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4160 * with the node name without namespace from the argument or the context.
4161 *
4162 * @param[in] args Array of arguments.
4163 * @param[in] arg_count Count of elements in @p args.
4164 * @param[in,out] set Context and result set at the same time.
4165 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004166 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004167 */
4168static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004169xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004170{
4171 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004172
Michal Vasko03ff5a72019-09-11 13:49:33 +02004173 /* suppress unused variable warning */
4174 (void)options;
4175
4176 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004177 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004178 return LY_SUCCESS;
4179 }
4180
4181 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004182 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004183 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004184 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004185 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004186 } else if (!args[0]->used) {
4187 set_fill_string(set, "", 0);
4188 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004189 }
4190
4191 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004192 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004193
4194 item = &args[0]->val.nodes[0];
4195 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004196 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004197 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004198 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004199 } else if (!set->used) {
4200 set_fill_string(set, "", 0);
4201 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004202 }
4203
4204 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004205 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004206
4207 item = &set->val.nodes[0];
4208 }
4209
4210 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004211 case LYXP_NODE_NONE:
4212 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004213 case LYXP_NODE_ROOT:
4214 case LYXP_NODE_ROOT_CONFIG:
4215 case LYXP_NODE_TEXT:
4216 set_fill_string(set, "", 0);
4217 break;
4218 case LYXP_NODE_ELEM:
4219 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4220 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004221 case LYXP_NODE_META:
4222 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004223 break;
4224 }
4225
4226 return LY_SUCCESS;
4227}
4228
4229/**
4230 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4231 * with the node name fully qualified (with namespace) from the argument or the context.
4232 *
4233 * @param[in] args Array of arguments.
4234 * @param[in] arg_count Count of elements in @p args.
4235 * @param[in,out] set Context and result set at the same time.
4236 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004237 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004238 */
4239static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004240xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004241{
4242 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004243 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004244 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004245 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004246
4247 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004248 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004249 return LY_SUCCESS;
4250 }
4251
4252 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004253 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004254 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004255 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004256 } else if (!args[0]->used) {
4257 set_fill_string(set, "", 0);
4258 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004259 }
4260
4261 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004262 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004263
4264 item = &args[0]->val.nodes[0];
4265 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004266 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004267 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004268 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004269 } else if (!set->used) {
4270 set_fill_string(set, "", 0);
4271 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004272 }
4273
4274 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004275 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004276
4277 item = &set->val.nodes[0];
4278 }
4279
4280 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004281 case LYXP_NODE_NONE:
4282 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004283 case LYXP_NODE_ROOT:
4284 case LYXP_NODE_ROOT_CONFIG:
4285 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004286 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004287 break;
4288 case LYXP_NODE_ELEM:
4289 mod = item->node->schema->module;
4290 name = item->node->schema->name;
4291 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004292 case LYXP_NODE_META:
4293 mod = ((struct lyd_meta *)item->node)->annotation->module;
4294 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004295 break;
4296 }
4297
4298 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004299 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004300 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4301 set_fill_string(set, str, strlen(str));
4302 free(str);
4303 } else {
4304 set_fill_string(set, "", 0);
4305 }
4306
4307 return LY_SUCCESS;
4308}
4309
4310/**
4311 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4312 * with the namespace of the node from the argument or the context.
4313 *
4314 * @param[in] args Array of arguments.
4315 * @param[in] arg_count Count of elements in @p args.
4316 * @param[in,out] set Context and result set at the same time.
4317 * @param[in] options XPath options.
4318 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4319 */
4320static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004321xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004322{
4323 struct lyxp_set_node *item;
4324 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004325
Michal Vasko03ff5a72019-09-11 13:49:33 +02004326 /* suppress unused variable warning */
4327 (void)options;
4328
4329 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004330 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004331 return LY_SUCCESS;
4332 }
4333
4334 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004335 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004336 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004337 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004338 return LY_EVALID;
4339 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004340 set_fill_string(set, "", 0);
4341 return LY_SUCCESS;
4342 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004343
4344 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004345 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004346
4347 item = &args[0]->val.nodes[0];
4348 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004349 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004350 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004351 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004352 } else if (!set->used) {
4353 set_fill_string(set, "", 0);
4354 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004355 }
4356
4357 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004358 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004359
4360 item = &set->val.nodes[0];
4361 }
4362
4363 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004364 case LYXP_NODE_NONE:
4365 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004366 case LYXP_NODE_ROOT:
4367 case LYXP_NODE_ROOT_CONFIG:
4368 case LYXP_NODE_TEXT:
4369 set_fill_string(set, "", 0);
4370 break;
4371 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004372 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004373 if (item->type == LYXP_NODE_ELEM) {
4374 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004375 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004376 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004377 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004378 }
4379
4380 set_fill_string(set, mod->ns, strlen(mod->ns));
4381 break;
4382 }
4383
4384 return LY_SUCCESS;
4385}
4386
4387/**
4388 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4389 * with only nodes from the context. In practice it either leaves the context
4390 * as it is or returns an empty node set.
4391 *
4392 * @param[in] args Array of arguments.
4393 * @param[in] arg_count Count of elements in @p args.
4394 * @param[in,out] set Context and result set at the same time.
4395 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004396 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004397 */
4398static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004399xpath_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 +02004400{
4401 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004402 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004403 return LY_SUCCESS;
4404 }
4405
4406 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004407 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004408 }
4409 return LY_SUCCESS;
4410}
4411
4412/**
4413 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4414 * with normalized value (no leading, trailing, double white spaces) of the node
4415 * from the argument or the context.
4416 *
4417 * @param[in] args Array of arguments.
4418 * @param[in] arg_count Count of elements in @p args.
4419 * @param[in,out] set Context and result set at the same time.
4420 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004421 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004422 */
4423static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004424xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004425{
4426 uint16_t i, new_used;
4427 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004428 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004429 struct lysc_node_leaf *sleaf;
4430 LY_ERR rc = LY_SUCCESS;
4431
4432 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004433 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4434 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004435 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004436 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4437 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004438 } else if (!warn_is_string_type(sleaf->type)) {
4439 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004440 }
4441 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004442 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004443 return rc;
4444 }
4445
4446 if (arg_count) {
4447 set_fill_set(set, args[0]);
4448 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004449 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004450 LY_CHECK_RET(rc);
4451
4452 /* is there any normalization necessary? */
4453 for (i = 0; set->val.str[i]; ++i) {
4454 if (is_xmlws(set->val.str[i])) {
4455 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4456 have_spaces = 1;
4457 break;
4458 }
4459 space_before = 1;
4460 } else {
4461 space_before = 0;
4462 }
4463 }
4464
4465 /* yep, there is */
4466 if (have_spaces) {
4467 /* it's enough, at least one character will go, makes space for ending '\0' */
4468 new = malloc(strlen(set->val.str) * sizeof(char));
4469 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4470 new_used = 0;
4471
4472 space_before = 0;
4473 for (i = 0; set->val.str[i]; ++i) {
4474 if (is_xmlws(set->val.str[i])) {
4475 if ((i == 0) || space_before) {
4476 space_before = 1;
4477 continue;
4478 } else {
4479 space_before = 1;
4480 }
4481 } else {
4482 space_before = 0;
4483 }
4484
4485 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4486 ++new_used;
4487 }
4488
4489 /* at worst there is one trailing space now */
4490 if (new_used && is_xmlws(new[new_used - 1])) {
4491 --new_used;
4492 }
4493
4494 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4495 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4496 new[new_used] = '\0';
4497
4498 free(set->val.str);
4499 set->val.str = new;
4500 }
4501
4502 return LY_SUCCESS;
4503}
4504
4505/**
4506 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4507 * with the argument converted to boolean and logically inverted.
4508 *
4509 * @param[in] args Array of arguments.
4510 * @param[in] arg_count Count of elements in @p args.
4511 * @param[in,out] set Context and result set at the same time.
4512 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004513 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004514 */
4515static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004516xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004517{
4518 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004519 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004520 return LY_SUCCESS;
4521 }
4522
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004523 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004524 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004525 set_fill_boolean(set, 0);
4526 } else {
4527 set_fill_boolean(set, 1);
4528 }
4529
4530 return LY_SUCCESS;
4531}
4532
4533/**
4534 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4535 * with the number representation of either the argument or the context.
4536 *
4537 * @param[in] args Array of arguments.
4538 * @param[in] arg_count Count of elements in @p args.
4539 * @param[in,out] set Context and result set at the same time.
4540 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004541 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004542 */
4543static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004544xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004545{
4546 LY_ERR rc;
4547
4548 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004549 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004550 return LY_SUCCESS;
4551 }
4552
4553 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004554 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004555 LY_CHECK_RET(rc);
4556 set_fill_set(set, args[0]);
4557 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004558 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004559 LY_CHECK_RET(rc);
4560 }
4561
4562 return LY_SUCCESS;
4563}
4564
4565/**
4566 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4567 * with the context position.
4568 *
4569 * @param[in] args Array of arguments.
4570 * @param[in] arg_count Count of elements in @p args.
4571 * @param[in,out] set Context and result set at the same time.
4572 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004573 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004574 */
4575static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004576xpath_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 +02004577{
4578 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004579 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004580 return LY_SUCCESS;
4581 }
4582
Michal Vasko03ff5a72019-09-11 13:49:33 +02004583 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004584 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004585 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004586 } else if (!set->used) {
4587 set_fill_number(set, 0);
4588 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004589 }
4590
4591 set_fill_number(set, set->ctx_pos);
4592
4593 /* UNUSED in 'Release' build type */
4594 (void)options;
4595 return LY_SUCCESS;
4596}
4597
4598/**
4599 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4600 * depending on whether the second argument regex matches the first argument string. For details refer to
4601 * YANG 1.1 RFC section 10.2.1.
4602 *
4603 * @param[in] args Array of arguments.
4604 * @param[in] arg_count Count of elements in @p args.
4605 * @param[in,out] set Context and result set at the same time.
4606 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004607 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004608 */
4609static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004610xpath_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 +02004611{
4612 struct lysc_pattern **patterns = NULL, **pattern;
4613 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004614 LY_ERR rc = LY_SUCCESS;
4615 struct ly_err_item *err;
4616
4617 if (options & LYXP_SCNODE_ALL) {
4618 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4619 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4620 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 +02004621 } else if (!warn_is_string_type(sleaf->type)) {
4622 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004623 }
4624 }
4625
4626 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4627 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4628 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 +02004629 } else if (!warn_is_string_type(sleaf->type)) {
4630 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004631 }
4632 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004633 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004634 return rc;
4635 }
4636
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004637 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004638 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004639 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004640 LY_CHECK_RET(rc);
4641
4642 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek IÅ¡a45802b52021-02-09 09:21:58 +01004643 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004644 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004645 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Radek Krejciddace2c2021-01-08 11:30:56 +01004646 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004647 if (rc != LY_SUCCESS) {
4648 LY_ARRAY_FREE(patterns);
4649 return rc;
4650 }
4651
Radek Krejci0b013302021-03-29 15:22:32 +02004652 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004653 pcre2_code_free((*pattern)->code);
4654 free(*pattern);
4655 LY_ARRAY_FREE(patterns);
4656 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004657 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004658 ly_err_free(err);
4659 return rc;
4660 }
4661
4662 if (rc == LY_EVALID) {
4663 ly_err_free(err);
4664 set_fill_boolean(set, 0);
4665 } else {
4666 set_fill_boolean(set, 1);
4667 }
4668
4669 return LY_SUCCESS;
4670}
4671
4672/**
4673 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4674 * with the rounded first argument. For details refer to
4675 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4676 *
4677 * @param[in] args Array of arguments.
4678 * @param[in] arg_count Count of elements in @p args.
4679 * @param[in,out] set Context and result set at the same time.
4680 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004681 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004682 */
4683static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004684xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004685{
4686 struct lysc_node_leaf *sleaf;
4687 LY_ERR rc = LY_SUCCESS;
4688
4689 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004690 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004691 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004692 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4693 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4694 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4695 sleaf->name);
4696 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4697 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4698 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004699 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004700 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004701 return rc;
4702 }
4703
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004704 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004705 LY_CHECK_RET(rc);
4706
4707 /* cover only the cases where floor can't be used */
4708 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4709 set_fill_number(set, -0.0f);
4710 } else {
4711 args[0]->val.num += 0.5;
4712 rc = xpath_floor(args, 1, args[0], options);
4713 LY_CHECK_RET(rc);
4714 set_fill_number(set, args[0]->val.num);
4715 }
4716
4717 return LY_SUCCESS;
4718}
4719
4720/**
4721 * @brief Execute the XPath starts-with(string, string) function.
4722 * Returns LYXP_SET_BOOLEAN whether the second argument is
4723 * the prefix of the first or not.
4724 *
4725 * @param[in] args Array of arguments.
4726 * @param[in] arg_count Count of elements in @p args.
4727 * @param[in,out] set Context and result set at the same time.
4728 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004729 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004730 */
4731static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004732xpath_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 +02004733{
4734 struct lysc_node_leaf *sleaf;
4735 LY_ERR rc = LY_SUCCESS;
4736
4737 if (options & LYXP_SCNODE_ALL) {
4738 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4739 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4740 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 +02004741 } else if (!warn_is_string_type(sleaf->type)) {
4742 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004743 }
4744 }
4745
4746 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4747 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4748 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 +02004749 } else if (!warn_is_string_type(sleaf->type)) {
4750 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004751 }
4752 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004753 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004754 return rc;
4755 }
4756
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004757 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004758 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004759 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004760 LY_CHECK_RET(rc);
4761
4762 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4763 set_fill_boolean(set, 0);
4764 } else {
4765 set_fill_boolean(set, 1);
4766 }
4767
4768 return LY_SUCCESS;
4769}
4770
4771/**
4772 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4773 * with the string representation of either the argument or the context.
4774 *
4775 * @param[in] args Array of arguments.
4776 * @param[in] arg_count Count of elements in @p args.
4777 * @param[in,out] set Context and result set at the same time.
4778 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004779 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004780 */
4781static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004782xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004783{
4784 LY_ERR rc;
4785
4786 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004787 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004788 return LY_SUCCESS;
4789 }
4790
4791 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004792 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004793 LY_CHECK_RET(rc);
4794 set_fill_set(set, args[0]);
4795 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004796 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004797 LY_CHECK_RET(rc);
4798 }
4799
4800 return LY_SUCCESS;
4801}
4802
4803/**
4804 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4805 * with the length of the string in either the argument or the context.
4806 *
4807 * @param[in] args Array of arguments.
4808 * @param[in] arg_count Count of elements in @p args.
4809 * @param[in,out] set Context and result set at the same time.
4810 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004811 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004812 */
4813static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004814xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004815{
4816 struct lysc_node_leaf *sleaf;
4817 LY_ERR rc = LY_SUCCESS;
4818
4819 if (options & LYXP_SCNODE_ALL) {
4820 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4821 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4822 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 +02004823 } else if (!warn_is_string_type(sleaf->type)) {
4824 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004825 }
4826 }
4827 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4828 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4829 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 +02004830 } else if (!warn_is_string_type(sleaf->type)) {
4831 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004832 }
4833 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004834 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004835 return rc;
4836 }
4837
4838 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004839 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004840 LY_CHECK_RET(rc);
4841 set_fill_number(set, strlen(args[0]->val.str));
4842 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004843 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004844 LY_CHECK_RET(rc);
4845 set_fill_number(set, strlen(set->val.str));
4846 }
4847
4848 return LY_SUCCESS;
4849}
4850
4851/**
4852 * @brief Execute the XPath substring(string, number, number?) function.
4853 * Returns LYXP_SET_STRING substring of the first argument starting
4854 * on the second argument index ending on the third argument index,
4855 * indexed from 1. For exact definition refer to
4856 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4857 *
4858 * @param[in] args Array of arguments.
4859 * @param[in] arg_count Count of elements in @p args.
4860 * @param[in,out] set Context and result set at the same time.
4861 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004862 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004863 */
4864static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004865xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004866{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004867 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004868 uint16_t str_start, str_len, pos;
4869 struct lysc_node_leaf *sleaf;
4870 LY_ERR rc = LY_SUCCESS;
4871
4872 if (options & LYXP_SCNODE_ALL) {
4873 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4874 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4875 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 +02004876 } else if (!warn_is_string_type(sleaf->type)) {
4877 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004878 }
4879 }
4880
4881 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4882 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4883 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 +02004884 } else if (!warn_is_numeric_type(sleaf->type)) {
4885 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004886 }
4887 }
4888
Michal Vasko69730152020-10-09 16:30:07 +02004889 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4890 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004891 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4892 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 +02004893 } else if (!warn_is_numeric_type(sleaf->type)) {
4894 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004895 }
4896 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004897 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004898 return rc;
4899 }
4900
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004901 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004902 LY_CHECK_RET(rc);
4903
4904 /* start */
4905 if (xpath_round(&args[1], 1, args[1], options)) {
4906 return -1;
4907 }
4908 if (isfinite(args[1]->val.num)) {
4909 start = args[1]->val.num - 1;
4910 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004911 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004912 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004913 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004914 }
4915
4916 /* len */
4917 if (arg_count == 3) {
4918 rc = xpath_round(&args[2], 1, args[2], options);
4919 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004920 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004921 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004922 } else if (isfinite(args[2]->val.num)) {
4923 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004924 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004925 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004926 }
4927 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004928 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004929 }
4930
4931 /* find matching character positions */
4932 str_start = 0;
4933 str_len = 0;
4934 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4935 if (pos < start) {
4936 ++str_start;
4937 } else if (pos < start + len) {
4938 ++str_len;
4939 } else {
4940 break;
4941 }
4942 }
4943
4944 set_fill_string(set, args[0]->val.str + str_start, str_len);
4945 return LY_SUCCESS;
4946}
4947
4948/**
4949 * @brief Execute the XPath substring-after(string, string) function.
4950 * Returns LYXP_SET_STRING with the string succeeding the occurance
4951 * of the second argument in the first or an empty string.
4952 *
4953 * @param[in] args Array of arguments.
4954 * @param[in] arg_count Count of elements in @p args.
4955 * @param[in,out] set Context and result set at the same time.
4956 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004957 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004958 */
4959static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004960xpath_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 +02004961{
4962 char *ptr;
4963 struct lysc_node_leaf *sleaf;
4964 LY_ERR rc = LY_SUCCESS;
4965
4966 if (options & LYXP_SCNODE_ALL) {
4967 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4968 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4969 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 +02004970 } else if (!warn_is_string_type(sleaf->type)) {
4971 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004972 }
4973 }
4974
4975 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4976 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4977 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 +02004978 } else if (!warn_is_string_type(sleaf->type)) {
4979 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004980 }
4981 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004982 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004983 return rc;
4984 }
4985
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004986 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004987 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004988 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004989 LY_CHECK_RET(rc);
4990
4991 ptr = strstr(args[0]->val.str, args[1]->val.str);
4992 if (ptr) {
4993 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4994 } else {
4995 set_fill_string(set, "", 0);
4996 }
4997
4998 return LY_SUCCESS;
4999}
5000
5001/**
5002 * @brief Execute the XPath substring-before(string, string) function.
5003 * Returns LYXP_SET_STRING with the string preceding the occurance
5004 * of the second argument in the first or an empty string.
5005 *
5006 * @param[in] args Array of arguments.
5007 * @param[in] arg_count Count of elements in @p args.
5008 * @param[in,out] set Context and result set at the same time.
5009 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005010 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005011 */
5012static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005013xpath_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 +02005014{
5015 char *ptr;
5016 struct lysc_node_leaf *sleaf;
5017 LY_ERR rc = LY_SUCCESS;
5018
5019 if (options & LYXP_SCNODE_ALL) {
5020 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5021 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5022 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 +02005023 } else if (!warn_is_string_type(sleaf->type)) {
5024 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005025 }
5026 }
5027
5028 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5029 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5030 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 +02005031 } else if (!warn_is_string_type(sleaf->type)) {
5032 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005033 }
5034 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005035 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005036 return rc;
5037 }
5038
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005039 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005040 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005041 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005042 LY_CHECK_RET(rc);
5043
5044 ptr = strstr(args[0]->val.str, args[1]->val.str);
5045 if (ptr) {
5046 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5047 } else {
5048 set_fill_string(set, "", 0);
5049 }
5050
5051 return LY_SUCCESS;
5052}
5053
5054/**
5055 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5056 * with the sum of all the nodes in the context.
5057 *
5058 * @param[in] args Array of arguments.
5059 * @param[in] arg_count Count of elements in @p args.
5060 * @param[in,out] set Context and result set at the same time.
5061 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005062 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005063 */
5064static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005065xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005066{
5067 long double num;
5068 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005069 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005070 struct lyxp_set set_item;
5071 struct lysc_node_leaf *sleaf;
5072 LY_ERR rc = LY_SUCCESS;
5073
5074 if (options & LYXP_SCNODE_ALL) {
5075 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5076 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005077 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005078 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5079 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5080 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005081 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005082 } else if (!warn_is_numeric_type(sleaf->type)) {
5083 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005084 }
5085 }
5086 }
5087 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005088 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005089 return rc;
5090 }
5091
5092 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005093
5094 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005095 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005096 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005097 } else if (!args[0]->used) {
5098 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005099 }
5100
Michal Vasko5c4e5892019-11-14 12:31:38 +01005101 set_init(&set_item, set);
5102
Michal Vasko03ff5a72019-09-11 13:49:33 +02005103 set_item.type = LYXP_SET_NODE_SET;
5104 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5105 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5106
5107 set_item.used = 1;
5108 set_item.size = 1;
5109
5110 for (i = 0; i < args[0]->used; ++i) {
5111 set_item.val.nodes[0] = args[0]->val.nodes[i];
5112
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005113 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005114 LY_CHECK_RET(rc);
5115 num = cast_string_to_number(str);
5116 free(str);
5117 set->val.num += num;
5118 }
5119
5120 free(set_item.val.nodes);
5121
5122 return LY_SUCCESS;
5123}
5124
5125/**
5126 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5127 * with the text content of the nodes in the context.
5128 *
5129 * @param[in] args Array of arguments.
5130 * @param[in] arg_count Count of elements in @p args.
5131 * @param[in,out] set Context and result set at the same time.
5132 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005133 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005134 */
5135static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005136xpath_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 +02005137{
5138 uint32_t i;
5139
5140 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005141 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005142 return LY_SUCCESS;
5143 }
5144
Michal Vasko03ff5a72019-09-11 13:49:33 +02005145 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005146 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005147 return LY_EVALID;
5148 }
5149
Michal Vaskod989ba02020-08-24 10:59:24 +02005150 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005151 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005152 case LYXP_NODE_NONE:
5153 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005154 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005155 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5156 set->val.nodes[i].type = LYXP_NODE_TEXT;
5157 ++i;
5158 break;
5159 }
Radek Krejci0f969882020-08-21 16:56:47 +02005160 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005161 case LYXP_NODE_ROOT:
5162 case LYXP_NODE_ROOT_CONFIG:
5163 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005164 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005165 set_remove_node(set, i);
5166 break;
5167 }
5168 }
5169
5170 return LY_SUCCESS;
5171}
5172
5173/**
5174 * @brief Execute the XPath translate(string, string, string) function.
5175 * Returns LYXP_SET_STRING with the first argument with the characters
5176 * from the second argument replaced by those on the corresponding
5177 * positions in the third argument.
5178 *
5179 * @param[in] args Array of arguments.
5180 * @param[in] arg_count Count of elements in @p args.
5181 * @param[in,out] set Context and result set at the same time.
5182 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005183 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005184 */
5185static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005186xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005187{
5188 uint16_t i, j, new_used;
5189 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005190 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005191 struct lysc_node_leaf *sleaf;
5192 LY_ERR rc = LY_SUCCESS;
5193
5194 if (options & LYXP_SCNODE_ALL) {
5195 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5196 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5197 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 +02005198 } else if (!warn_is_string_type(sleaf->type)) {
5199 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005200 }
5201 }
5202
5203 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5204 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5205 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 +02005206 } else if (!warn_is_string_type(sleaf->type)) {
5207 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005208 }
5209 }
5210
5211 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5212 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5213 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 +02005214 } else if (!warn_is_string_type(sleaf->type)) {
5215 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005216 }
5217 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005218 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005219 return rc;
5220 }
5221
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005222 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005223 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005224 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005225 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005226 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005227 LY_CHECK_RET(rc);
5228
5229 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5230 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5231 new_used = 0;
5232
5233 have_removed = 0;
5234 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005235 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005236
5237 for (j = 0; args[1]->val.str[j]; ++j) {
5238 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5239 /* removing this char */
5240 if (j >= strlen(args[2]->val.str)) {
5241 have_removed = 1;
5242 found = 1;
5243 break;
5244 }
5245 /* replacing this char */
5246 new[new_used] = args[2]->val.str[j];
5247 ++new_used;
5248 found = 1;
5249 break;
5250 }
5251 }
5252
5253 /* copying this char */
5254 if (!found) {
5255 new[new_used] = args[0]->val.str[i];
5256 ++new_used;
5257 }
5258 }
5259
5260 if (have_removed) {
5261 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5262 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5263 }
5264 new[new_used] = '\0';
5265
Michal Vaskod3678892020-05-21 10:06:58 +02005266 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005267 set->type = LYXP_SET_STRING;
5268 set->val.str = new;
5269
5270 return LY_SUCCESS;
5271}
5272
5273/**
5274 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5275 * with true value.
5276 *
5277 * @param[in] args Array of arguments.
5278 * @param[in] arg_count Count of elements in @p args.
5279 * @param[in,out] set Context and result set at the same time.
5280 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005281 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005282 */
5283static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005284xpath_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 +02005285{
5286 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005287 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005288 return LY_SUCCESS;
5289 }
5290
5291 set_fill_boolean(set, 1);
5292 return LY_SUCCESS;
5293}
5294
5295/*
5296 * moveto functions
5297 *
5298 * They and only they actually change the context (set).
5299 */
5300
5301/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005302 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005303 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005304 * XPath @p set is expected to be a (sc)node set!
5305 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005306 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5307 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005308 * @param[in] set Set with general XPath context.
5309 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005310 * @param[out] moveto_mod Expected module of a matching node.
5311 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005312 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005313static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005314moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5315 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005316{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005317 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005318 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005319 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005320
Michal Vasko2104e9f2020-03-06 08:23:25 +01005321 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5322
Michal Vasko6346ece2019-09-24 13:12:53 +02005323 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5324 /* specific module */
5325 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005326 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005327
Michal Vasko004d3152020-06-11 19:59:22 +02005328 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005329 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005330 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005331 return LY_EVALID;
5332 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005333
Michal Vasko6346ece2019-09-24 13:12:53 +02005334 *qname += pref_len + 1;
5335 *qname_len -= pref_len + 1;
5336 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5337 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005338 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005339 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005340 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005341 case LY_VALUE_SCHEMA:
5342 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005343 /* current module */
5344 mod = set->cur_mod;
5345 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005346 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005347 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005348 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005349 /* inherit parent (context node) module */
5350 if (ctx_scnode) {
5351 mod = ctx_scnode->module;
5352 } else {
5353 mod = NULL;
5354 }
5355 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005356 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005357 /* all nodes need to be prefixed */
5358 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5359 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005360 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005361 }
5362
Michal Vasko6346ece2019-09-24 13:12:53 +02005363 *moveto_mod = mod;
5364 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005365}
5366
5367/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005368 * @brief Move context @p set to the root. Handles absolute path.
5369 * Result is LYXP_SET_NODE_SET.
5370 *
5371 * @param[in,out] set Set to use.
5372 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005373 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005374 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005375static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005376moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005377{
aPiecek8b0cc152021-05-31 16:40:31 +02005378 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005379
5380 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005381 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskob0099a92020-08-31 14:55:23 +02005382 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005383 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005384 set->type = LYXP_SET_NODE_SET;
5385 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005386 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005387 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005388
5389 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005390}
5391
5392/**
5393 * @brief Check @p node as a part of NameTest processing.
5394 *
5395 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005396 * @param[in] ctx_node Context node.
5397 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005398 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005399 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005400 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005401 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5402 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005403 */
5404static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005405moveto_node_check(const struct lyd_node *node, const struct lyd_node *ctx_node, const struct lyxp_set *set,
Michal Vaskocdad7122020-11-09 21:04:44 +01005406 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005407{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005408 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5409 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005410 case LY_VALUE_SCHEMA:
5411 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005412 /* use current module */
5413 moveto_mod = set->cur_mod;
5414 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005415 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005416 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005417 /* inherit module of the context node, if any */
5418 if (ctx_node) {
5419 moveto_mod = ctx_node->schema->module;
5420 }
5421 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005422 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005423 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005424 /* not defined */
5425 LOGINT(set->ctx);
5426 return LY_EINVAL;
5427 }
5428 }
5429
Michal Vasko03ff5a72019-09-11 13:49:33 +02005430 /* module check */
5431 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005432 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005433 }
5434
Michal Vasko5c4e5892019-11-14 12:31:38 +01005435 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005436 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005437 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005438 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5439 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005440 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005441 }
5442
5443 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005444 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005445 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005446 }
5447
Michal Vaskoa1424542019-11-14 16:08:52 +01005448 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005449 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005450 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005451 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005452
5453 /* match */
5454 return LY_SUCCESS;
5455}
5456
5457/**
5458 * @brief Check @p node as a part of schema NameTest processing.
5459 *
5460 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005461 * @param[in] ctx_scnode Context node.
5462 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005463 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005464 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005465 * @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 +02005466 */
5467static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005468moveto_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 +02005469 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005470{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005471 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5472 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005473 case LY_VALUE_SCHEMA:
5474 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005475 /* use current module */
5476 moveto_mod = set->cur_mod;
5477 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005478 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005479 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005480 /* inherit module of the context node, if any */
5481 if (ctx_scnode) {
5482 moveto_mod = ctx_scnode->module;
5483 }
5484 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005485 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005486 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005487 /* not defined */
5488 LOGINT(set->ctx);
5489 return LY_EINVAL;
5490 }
5491 }
5492
Michal Vasko03ff5a72019-09-11 13:49:33 +02005493 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005494 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005495 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005496 }
5497
5498 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005499 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005500 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005501 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005502 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005503 }
5504
5505 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005506 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005507 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005508 }
5509
5510 /* match */
5511 return LY_SUCCESS;
5512}
5513
5514/**
Michal Vaskod3678892020-05-21 10:06:58 +02005515 * @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 +02005516 *
5517 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005518 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005519 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005520 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005521 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5522 */
5523static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005524moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005525{
Michal Vaskof03ed032020-03-04 13:31:44 +01005526 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005527 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005528 LY_ERR rc;
5529
aPiecek8b0cc152021-05-31 16:40:31 +02005530 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005531 return LY_SUCCESS;
5532 }
5533
5534 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005535 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005536 return LY_EVALID;
5537 }
5538
Michal Vasko03ff5a72019-09-11 13:49:33 +02005539 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005540 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005541
5542 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 +01005543 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005544
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005545 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005546 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005547 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005548 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005549 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005550 ctx_node = set->val.nodes[i].node;
5551 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005552 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005553
Michal Vaskod3678892020-05-21 10:06:58 +02005554 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005555 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005556 if (rc == LY_SUCCESS) {
5557 if (!replaced) {
5558 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5559 replaced = 1;
5560 } else {
5561 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005562 }
Michal Vaskod3678892020-05-21 10:06:58 +02005563 ++i;
5564 } else if (rc == LY_EINCOMPLETE) {
5565 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005566 }
5567 }
5568
5569 if (!replaced) {
5570 /* no match */
5571 set_remove_node(set, i);
5572 }
5573 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005574
5575 return LY_SUCCESS;
5576}
5577
5578/**
Michal Vaskod3678892020-05-21 10:06:58 +02005579 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5580 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005581 *
5582 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005583 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005584 * @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 +01005585 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005586 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5587 */
5588static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005589moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5590 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005591{
Michal Vasko004d3152020-06-11 19:59:22 +02005592 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005593 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005594 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005595 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005596
Michal Vasko004d3152020-06-11 19:59:22 +02005597 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005598
aPiecek8b0cc152021-05-31 16:40:31 +02005599 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02005600 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005601 }
5602
5603 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005604 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005605 ret = LY_EVALID;
5606 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005607 }
5608
5609 /* context check for all the nodes since we have the schema node */
5610 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5611 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005612 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005613 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5614 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005615 lyxp_set_free_content(set);
5616 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005617 }
5618
5619 /* create specific data instance if needed */
5620 if (scnode->nodetype == LYS_LIST) {
5621 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5622 } else if (scnode->nodetype == LYS_LEAFLIST) {
5623 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005624 }
5625
5626 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005627 siblings = NULL;
5628
5629 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5630 assert(!set->val.nodes[i].node);
5631
5632 /* search in all the trees */
5633 siblings = set->tree;
5634 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5635 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005636 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005637 }
5638
5639 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005640 if (inst) {
5641 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005642 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005643 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005644 }
5645
5646 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005647 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005648 ret = LY_EINCOMPLETE;
5649 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005650 }
5651
5652 if (sub) {
5653 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005654 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005655 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005656 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005657 /* no match */
5658 set_remove_node(set, i);
5659 }
5660 }
5661
Michal Vasko004d3152020-06-11 19:59:22 +02005662cleanup:
5663 lyd_free_tree(inst);
5664 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005665}
5666
5667/**
5668 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5669 *
5670 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005671 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005672 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005673 * @param[in] options XPath options.
5674 * @return LY_ERR
5675 */
5676static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005677moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005678{
Radek Krejci857189e2020-09-01 13:26:36 +02005679 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005680 uint32_t getnext_opts;
5681 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005682 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005683 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005684 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005685
aPiecek8b0cc152021-05-31 16:40:31 +02005686 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005687 return LY_SUCCESS;
5688 }
5689
5690 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005691 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005692 return LY_EVALID;
5693 }
5694
Michal Vaskocafad9d2019-11-07 15:20:03 +01005695 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005696 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005697 if (options & LYXP_SCNODE_OUTPUT) {
5698 getnext_opts |= LYS_GETNEXT_OUTPUT;
5699 }
5700
Michal Vasko03ff5a72019-09-11 13:49:33 +02005701 orig_used = set->used;
5702 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005703 uint32_t idx;
5704
Radek Krejcif13b87b2020-12-01 22:02:17 +01005705 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5706 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005707 continue;
5708 }
5709
5710 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005711 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005712 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005713 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005714 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005715
5716 start_parent = set->val.scnodes[i].scnode;
5717
5718 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 +02005719 /* 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 +02005720 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005721 mod_idx = 0;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005722 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005723 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005724 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005725 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005726 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005727 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5728
Michal Vasko03ff5a72019-09-11 13:49:33 +02005729 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005730 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005731 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005732 temp_ctx = 1;
5733 }
5734 }
5735 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005736 }
5737
Michal Vasko519fd602020-05-26 12:17:39 +02005738 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5739 iter = NULL;
5740 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005741 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005742 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5743
5744 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005745 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005746 temp_ctx = 1;
5747 }
5748 }
5749 }
5750 }
5751 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005752
5753 /* correct temporary in_ctx values */
5754 if (temp_ctx) {
5755 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005756 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5757 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005758 }
5759 }
5760 }
5761
5762 return LY_SUCCESS;
5763}
5764
5765/**
Michal Vaskod3678892020-05-21 10:06:58 +02005766 * @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 +02005767 * Context position aware.
5768 *
5769 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005770 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005771 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005772 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005773 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5774 */
5775static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005776moveto_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 +02005777{
5778 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005779 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005780 struct lyxp_set ret_set;
5781 LY_ERR rc;
5782
aPiecek8b0cc152021-05-31 16:40:31 +02005783 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005784 return LY_SUCCESS;
5785 }
5786
5787 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005788 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005789 return LY_EVALID;
5790 }
5791
Michal Vasko9f96a052020-03-10 09:41:45 +01005792 /* 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 +01005793 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005794 LY_CHECK_RET(rc);
5795
Michal Vasko6346ece2019-09-24 13:12:53 +02005796 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005797 set_init(&ret_set, set);
5798 for (i = 0; i < set->used; ++i) {
5799
5800 /* TREE DFS */
5801 start = set->val.nodes[i].node;
5802 for (elem = next = start; elem; elem = next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005803 rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005804 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005805 /* add matching node into result set */
5806 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5807 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5808 /* the node is a duplicate, we'll process it later in the set */
5809 goto skip_children;
5810 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005811 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005812 return rc;
5813 } else if (rc == LY_EINVAL) {
5814 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005815 }
5816
5817 /* TREE DFS NEXT ELEM */
5818 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005819 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005820 if (!next) {
5821skip_children:
5822 /* no children, so try siblings, but only if it's not the start,
5823 * that is considered to be the root and it's siblings are not traversed */
5824 if (elem != start) {
5825 next = elem->next;
5826 } else {
5827 break;
5828 }
5829 }
5830 while (!next) {
5831 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01005832 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005833 /* we are done, no next element to process */
5834 break;
5835 }
5836 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01005837 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005838 next = elem->next;
5839 }
5840 }
5841 }
5842
5843 /* make the temporary set the current one */
5844 ret_set.ctx_pos = set->ctx_pos;
5845 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005846 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005847 memcpy(set, &ret_set, sizeof *set);
5848
5849 return LY_SUCCESS;
5850}
5851
5852/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005853 * @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 +02005854 *
5855 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005856 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005857 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005858 * @param[in] options XPath options.
5859 * @return LY_ERR
5860 */
5861static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005862moveto_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 +02005863{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005864 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005865 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005866 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005867
aPiecek8b0cc152021-05-31 16:40:31 +02005868 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005869 return LY_SUCCESS;
5870 }
5871
5872 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005873 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005874 return LY_EVALID;
5875 }
5876
Michal Vasko03ff5a72019-09-11 13:49:33 +02005877 orig_used = set->used;
5878 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005879 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5880 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005881 continue;
5882 }
5883
5884 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005885 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005886 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005887 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005888 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005889
5890 /* TREE DFS */
5891 start = set->val.scnodes[i].scnode;
5892 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005893 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5894 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005895 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005896 }
5897
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005898 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005899 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005900 uint32_t idx;
5901
5902 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005903 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005904 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005905 /* we will process it later in the set */
5906 goto skip_children;
5907 }
5908 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005909 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005910 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005911 } else if (rc == LY_EINVAL) {
5912 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005913 }
5914
5915next_iter:
5916 /* TREE DFS NEXT ELEM */
5917 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005918 next = lysc_node_child(elem);
5919 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5920 next = next->next;
5921 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5922 next = next->next;
5923 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005924 if (!next) {
5925skip_children:
5926 /* no children, so try siblings, but only if it's not the start,
5927 * that is considered to be the root and it's siblings are not traversed */
5928 if (elem != start) {
5929 next = elem->next;
5930 } else {
5931 break;
5932 }
5933 }
5934 while (!next) {
5935 /* no siblings, go back through the parents */
5936 if (elem->parent == start) {
5937 /* we are done, no next element to process */
5938 break;
5939 }
5940 /* parent is already processed, go to its sibling */
5941 elem = elem->parent;
5942 next = elem->next;
5943 }
5944 }
5945 }
5946
5947 return LY_SUCCESS;
5948}
5949
5950/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005951 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005952 * Indirectly context position aware.
5953 *
5954 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005955 * @param[in] mod Matching metadata module, NULL for any.
5956 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02005957 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005958 * @return LY_ERR
5959 */
5960static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02005961moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005962{
Michal Vasko9f96a052020-03-10 09:41:45 +01005963 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005964
aPiecek8b0cc152021-05-31 16:40:31 +02005965 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005966 return LY_SUCCESS;
5967 }
5968
5969 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005970 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005971 return LY_EVALID;
5972 }
5973
Radek Krejci1deb5be2020-08-26 16:43:36 +02005974 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005975 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005976
5977 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5978 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005979 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005980 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005981
5982 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005983 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005984 continue;
5985 }
5986
Michal Vaskod3678892020-05-21 10:06:58 +02005987 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005988 /* match */
5989 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005990 set->val.meta[i].meta = sub;
5991 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005992 /* pos does not change */
5993 replaced = 1;
5994 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005995 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 +02005996 }
5997 ++i;
5998 }
5999 }
6000 }
6001
6002 if (!replaced) {
6003 /* no match */
6004 set_remove_node(set, i);
6005 }
6006 }
6007
6008 return LY_SUCCESS;
6009}
6010
6011/**
6012 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02006013 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006014 *
6015 * @param[in,out] set1 Set to use for the result.
6016 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006017 * @return LY_ERR
6018 */
6019static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006020moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006021{
6022 LY_ERR rc;
6023
Michal Vaskod3678892020-05-21 10:06:58 +02006024 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006025 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006026 return LY_EVALID;
6027 }
6028
6029 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02006030 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006031 return LY_SUCCESS;
6032 }
6033
Michal Vaskod3678892020-05-21 10:06:58 +02006034 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006035 memcpy(set1, set2, sizeof *set1);
6036 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02006037 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006038 return LY_SUCCESS;
6039 }
6040
6041 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006042 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006043
6044 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006045 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006046 LY_CHECK_RET(rc);
6047
6048 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006049 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006050
6051 return LY_SUCCESS;
6052}
6053
6054/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006055 * @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 +02006056 * Context position aware.
6057 *
6058 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006059 * @param[in] mod Matching metadata module, NULL for any.
6060 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006061 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006062 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6063 */
6064static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006065moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006066{
Michal Vasko9f96a052020-03-10 09:41:45 +01006067 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006068 struct lyxp_set *set_all_desc = NULL;
6069 LY_ERR rc;
6070
aPiecek8b0cc152021-05-31 16:40:31 +02006071 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006072 return LY_SUCCESS;
6073 }
6074
6075 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006076 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006077 return LY_EVALID;
6078 }
6079
Michal Vasko03ff5a72019-09-11 13:49:33 +02006080 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6081 * but it likely won't be used much, so it's a waste of time */
6082 /* copy the context */
6083 set_all_desc = set_copy(set);
6084 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006085 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006086 if (rc != LY_SUCCESS) {
6087 lyxp_set_free(set_all_desc);
6088 return rc;
6089 }
6090 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006091 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006092 if (rc != LY_SUCCESS) {
6093 lyxp_set_free(set_all_desc);
6094 return rc;
6095 }
6096 lyxp_set_free(set_all_desc);
6097
Radek Krejci1deb5be2020-08-26 16:43:36 +02006098 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006099 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006100
6101 /* only attributes of an elem can be in the result, skip all the rest,
6102 * we have all attributes qualified in lyd tree */
6103 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006104 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006105 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006106 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006107 continue;
6108 }
6109
Michal Vaskod3678892020-05-21 10:06:58 +02006110 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006111 /* match */
6112 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006113 set->val.meta[i].meta = sub;
6114 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006115 /* pos does not change */
6116 replaced = 1;
6117 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006118 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 +02006119 }
6120 ++i;
6121 }
6122 }
6123 }
6124
6125 if (!replaced) {
6126 /* no match */
6127 set_remove_node(set, i);
6128 }
6129 }
6130
6131 return LY_SUCCESS;
6132}
6133
6134/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006135 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6136 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006137 *
6138 * @param[in] parent Current parent.
6139 * @param[in] parent_pos Position of @p parent.
6140 * @param[in] parent_type Node type of @p parent.
6141 * @param[in,out] to_set Set to use.
6142 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006143 * @param[in] options XPath options.
6144 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6145 */
6146static LY_ERR
6147moveto_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 +02006148 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006149{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006150 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006151 LY_ERR rc;
6152
6153 switch (parent_type) {
6154 case LYXP_NODE_ROOT:
6155 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006156 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006157
Michal Vasko61ac2f62020-05-25 12:39:51 +02006158 /* add all top-level nodes as elements */
6159 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006160 break;
6161 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006162 /* add just the text node of this term element node */
6163 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006164 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6165 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6166 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006167 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006168 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006169
6170 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006171 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006172 break;
6173 default:
6174 LOGINT_RET(parent->schema->module->ctx);
6175 }
6176
Michal Vasko61ac2f62020-05-25 12:39:51 +02006177 /* add all top-level nodes as elements */
6178 LY_LIST_FOR(first, iter) {
6179 /* context check */
6180 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6181 continue;
6182 }
6183
6184 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006185 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006186 return LY_EINCOMPLETE;
6187 }
6188
6189 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6190 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6191
6192 /* also add all the children of this node, recursively */
6193 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6194 LY_CHECK_RET(rc);
6195 }
6196 }
6197
Michal Vasko03ff5a72019-09-11 13:49:33 +02006198 return LY_SUCCESS;
6199}
6200
6201/**
6202 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6203 * (or LYXP_SET_EMPTY). Context position aware.
6204 *
6205 * @param[in,out] set Set to use.
6206 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6207 * @param[in] options XPath options.
6208 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6209 */
6210static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006211moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006212{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006213 struct lyxp_set ret_set;
6214 LY_ERR rc;
6215
aPiecek8b0cc152021-05-31 16:40:31 +02006216 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006217 return LY_SUCCESS;
6218 }
6219
6220 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006221 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006222 return LY_EVALID;
6223 }
6224
6225 /* nothing to do */
6226 if (!all_desc) {
6227 return LY_SUCCESS;
6228 }
6229
Michal Vasko03ff5a72019-09-11 13:49:33 +02006230 /* add all the children, they get added recursively */
6231 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006232 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006233 /* copy the current node to tmp */
6234 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6235
6236 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006237 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006238 continue;
6239 }
6240
Michal Vasko03ff5a72019-09-11 13:49:33 +02006241 /* add all the children */
6242 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 +02006243 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006244 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006245 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006246 return rc;
6247 }
6248 }
6249
6250 /* use the temporary set as the current one */
6251 ret_set.ctx_pos = set->ctx_pos;
6252 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006253 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006254 memcpy(set, &ret_set, sizeof *set);
6255
6256 return LY_SUCCESS;
6257}
6258
6259/**
6260 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6261 * (or LYXP_SET_EMPTY).
6262 *
6263 * @param[in,out] set Set to use.
6264 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6265 * @param[in] options XPath options.
6266 * @return LY_ERR
6267 */
6268static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006269moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006270{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006271 uint32_t getnext_opts;
6272 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006273 const struct lysc_node *iter, *start_parent;
6274 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006275
aPiecek8b0cc152021-05-31 16:40:31 +02006276 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006277 return LY_SUCCESS;
6278 }
6279
6280 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006281 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006282 return LY_EVALID;
6283 }
6284
Michal Vasko519fd602020-05-26 12:17:39 +02006285 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006286 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006287 if (options & LYXP_SCNODE_OUTPUT) {
6288 getnext_opts |= LYS_GETNEXT_OUTPUT;
6289 }
6290
6291 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006292 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006293 if (!all_desc) {
6294 /* traverse the start node */
6295 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6296 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6297 }
6298 continue;
6299 }
6300
Radek Krejcif13b87b2020-12-01 22:02:17 +01006301 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6302 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006303 continue;
6304 }
6305
Michal Vasko519fd602020-05-26 12:17:39 +02006306 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006307 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006308 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006309 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006310 }
6311
Michal Vasko519fd602020-05-26 12:17:39 +02006312 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006313
Michal Vasko519fd602020-05-26 12:17:39 +02006314 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6315 /* it can actually be in any module, it's all <running> */
6316 mod_idx = 0;
6317 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6318 iter = NULL;
6319 /* module may not be implemented */
6320 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6321 /* context check */
6322 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6323 continue;
6324 }
6325
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006326 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006327 /* throw away the insert index, we want to consider that node again, recursively */
6328 }
6329 }
6330
6331 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6332 iter = NULL;
6333 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006334 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006335 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006336 continue;
6337 }
6338
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006339 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006340 }
6341 }
6342 }
6343
6344 return LY_SUCCESS;
6345}
6346
6347/**
6348 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6349 * (or LYXP_SET_EMPTY). Context position aware.
6350 *
6351 * @param[in] set Set to use.
6352 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6353 * @param[in] options XPath options.
6354 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6355 */
6356static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006357moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006358{
6359 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006360 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006361 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006362
aPiecek8b0cc152021-05-31 16:40:31 +02006363 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006364 return LY_SUCCESS;
6365 }
6366
6367 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006368 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006369 return LY_EVALID;
6370 }
6371
6372 if (all_desc) {
6373 /* <path>//.. == <path>//./.. */
6374 rc = moveto_self(set, 1, options);
6375 LY_CHECK_RET(rc);
6376 }
6377
Radek Krejci1deb5be2020-08-26 16:43:36 +02006378 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006379 node = set->val.nodes[i].node;
6380
6381 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9e685082021-01-29 14:49:09 +01006382 new_node = lyd_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006383 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6384 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006385 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6386 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006387 if (!new_node) {
6388 LOGINT_RET(set->ctx);
6389 }
6390 } else {
6391 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006392 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006393 continue;
6394 }
6395
Michal Vaskoa1424542019-11-14 16:08:52 +01006396 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006397 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 +02006398 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006399 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006400
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006401 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006402 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006403 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006404
Michal Vasko03ff5a72019-09-11 13:49:33 +02006405 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006406 /* 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 +02006407 new_type = LYXP_NODE_ELEM;
6408 }
6409
Michal Vasko03ff5a72019-09-11 13:49:33 +02006410 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006411 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006412 } else {
6413 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006414 }
6415 }
6416
Michal Vasko2caefc12019-11-14 16:07:56 +01006417 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006418 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006419
6420 return LY_SUCCESS;
6421}
6422
6423/**
6424 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6425 * (or LYXP_SET_EMPTY).
6426 *
6427 * @param[in] set Set to use.
6428 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6429 * @param[in] options XPath options.
6430 * @return LY_ERR
6431 */
6432static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006433moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006434{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006435 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006436 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006437 const struct lysc_node *node, *new_node;
6438 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006439
aPiecek8b0cc152021-05-31 16:40:31 +02006440 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006441 return LY_SUCCESS;
6442 }
6443
6444 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006445 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006446 return LY_EVALID;
6447 }
6448
6449 if (all_desc) {
6450 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006451 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006452 }
6453
Michal Vasko03ff5a72019-09-11 13:49:33 +02006454 orig_used = set->used;
6455 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006456 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6457 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006458 continue;
6459 }
6460
6461 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006462 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006463 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006464 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006465 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006466
6467 node = set->val.scnodes[i].scnode;
6468
6469 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006470 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006471 } else {
6472 /* root does not have a parent */
6473 continue;
6474 }
6475
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006476 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006477 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006478 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006479
Michal Vasko03ff5a72019-09-11 13:49:33 +02006480 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006481 /* 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 +02006482 new_type = LYXP_NODE_ELEM;
6483 }
6484
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006485 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6486 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006487 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006488 temp_ctx = 1;
6489 }
6490 }
6491
6492 if (temp_ctx) {
6493 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006494 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6495 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006496 }
6497 }
6498 }
6499
6500 return LY_SUCCESS;
6501}
6502
6503/**
6504 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6505 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6506 *
6507 * @param[in,out] set1 Set to use for the result.
6508 * @param[in] set2 Set acting as the second operand for @p op.
6509 * @param[in] op Comparison operator to process.
6510 * @param[in] options XPath options.
6511 * @return LY_ERR
6512 */
6513static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006514moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006515{
6516 /*
6517 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6518 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6519 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6520 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6521 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6522 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6523 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6524 *
6525 * '=' or '!='
6526 * BOOLEAN + BOOLEAN
6527 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6528 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6529 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6530 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6531 * NUMBER + NUMBER
6532 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6533 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6534 * STRING + STRING
6535 *
6536 * '<=', '<', '>=', '>'
6537 * NUMBER + NUMBER
6538 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6539 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6540 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6541 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6542 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6543 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6544 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6545 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6546 */
6547 struct lyxp_set iter1, iter2;
6548 int result;
6549 int64_t i;
6550 LY_ERR rc;
6551
Michal Vasko004d3152020-06-11 19:59:22 +02006552 memset(&iter1, 0, sizeof iter1);
6553 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006554
6555 /* iterative evaluation with node-sets */
6556 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6557 if (set1->type == LYXP_SET_NODE_SET) {
6558 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006559 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006560 switch (set2->type) {
6561 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006562 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006563 break;
6564 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006565 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006566 break;
6567 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006568 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006569 break;
6570 }
6571 LY_CHECK_RET(rc);
6572
Michal Vasko4c7763f2020-07-27 17:40:37 +02006573 /* canonize set2 */
6574 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6575
6576 /* compare recursively */
6577 rc = moveto_op_comp(&iter1, &iter2, op, options);
6578 lyxp_set_free_content(&iter2);
6579 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006580
6581 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006582 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006583 set_fill_boolean(set1, 1);
6584 return LY_SUCCESS;
6585 }
6586 }
6587 } else {
6588 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006589 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006590 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006591 case LYXP_SET_NUMBER:
6592 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6593 break;
6594 case LYXP_SET_BOOLEAN:
6595 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6596 break;
6597 default:
6598 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6599 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006600 }
6601 LY_CHECK_RET(rc);
6602
Michal Vasko4c7763f2020-07-27 17:40:37 +02006603 /* canonize set1 */
6604 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 +02006605
Michal Vasko4c7763f2020-07-27 17:40:37 +02006606 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006607 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006608 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006609 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006610
6611 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006612 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006613 set_fill_boolean(set1, 1);
6614 return LY_SUCCESS;
6615 }
6616 }
6617 }
6618
6619 /* false for all nodes */
6620 set_fill_boolean(set1, 0);
6621 return LY_SUCCESS;
6622 }
6623
6624 /* first convert properly */
6625 if ((op[0] == '=') || (op[0] == '!')) {
6626 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006627 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6628 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006629 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006630 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006631 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006632 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006633 LY_CHECK_RET(rc);
6634 } /* else we have 2 strings */
6635 } else {
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 }
6641
6642 assert(set1->type == set2->type);
6643
6644 /* compute result */
6645 if (op[0] == '=') {
6646 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006647 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006648 } else if (set1->type == LYXP_SET_NUMBER) {
6649 result = (set1->val.num == set2->val.num);
6650 } else {
6651 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006652 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006653 }
6654 } else if (op[0] == '!') {
6655 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006656 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006657 } else if (set1->type == LYXP_SET_NUMBER) {
6658 result = (set1->val.num != set2->val.num);
6659 } else {
6660 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006661 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006662 }
6663 } else {
6664 assert(set1->type == LYXP_SET_NUMBER);
6665 if (op[0] == '<') {
6666 if (op[1] == '=') {
6667 result = (set1->val.num <= set2->val.num);
6668 } else {
6669 result = (set1->val.num < set2->val.num);
6670 }
6671 } else {
6672 if (op[1] == '=') {
6673 result = (set1->val.num >= set2->val.num);
6674 } else {
6675 result = (set1->val.num > set2->val.num);
6676 }
6677 }
6678 }
6679
6680 /* assign result */
6681 if (result) {
6682 set_fill_boolean(set1, 1);
6683 } else {
6684 set_fill_boolean(set1, 0);
6685 }
6686
6687 return LY_SUCCESS;
6688}
6689
6690/**
6691 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6692 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6693 *
6694 * @param[in,out] set1 Set to use for the result.
6695 * @param[in] set2 Set acting as the second operand for @p op.
6696 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006697 * @return LY_ERR
6698 */
6699static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006700moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006701{
6702 LY_ERR rc;
6703
6704 /* unary '-' */
6705 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006706 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006707 LY_CHECK_RET(rc);
6708 set1->val.num *= -1;
6709 lyxp_set_free(set2);
6710 return LY_SUCCESS;
6711 }
6712
6713 assert(set1 && set2);
6714
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006715 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006716 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006717 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006718 LY_CHECK_RET(rc);
6719
6720 switch (op[0]) {
6721 /* '+' */
6722 case '+':
6723 set1->val.num += set2->val.num;
6724 break;
6725
6726 /* '-' */
6727 case '-':
6728 set1->val.num -= set2->val.num;
6729 break;
6730
6731 /* '*' */
6732 case '*':
6733 set1->val.num *= set2->val.num;
6734 break;
6735
6736 /* 'div' */
6737 case 'd':
6738 set1->val.num /= set2->val.num;
6739 break;
6740
6741 /* 'mod' */
6742 case 'm':
6743 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6744 break;
6745
6746 default:
6747 LOGINT_RET(set1->ctx);
6748 }
6749
6750 return LY_SUCCESS;
6751}
6752
6753/*
6754 * eval functions
6755 *
6756 * They execute a parsed XPath expression on some data subtree.
6757 */
6758
6759/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006760 * @brief Evaluate Predicate. Logs directly on error.
6761 *
Michal Vaskod3678892020-05-21 10:06:58 +02006762 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006763 *
6764 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006765 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02006766 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006767 * @param[in] options XPath options.
6768 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6769 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6770 */
6771static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006772eval_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 +02006773{
6774 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006775 uint16_t orig_exp;
6776 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006777 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006778 struct lyxp_set set2;
6779 struct lyd_node *orig_parent;
6780
6781 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02006782 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006783 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006784 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006785
aPiecek8b0cc152021-05-31 16:40:31 +02006786 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006787only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02006788 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006789 LY_CHECK_RET(rc);
6790 } else if (set->type == LYXP_SET_NODE_SET) {
6791 /* 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 +01006792 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006793
6794 /* empty set, nothing to evaluate */
6795 if (!set->used) {
6796 goto only_parse;
6797 }
6798
Michal Vasko004d3152020-06-11 19:59:22 +02006799 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006800 orig_pos = 0;
6801 orig_size = set->used;
6802 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006803 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006804 set_init(&set2, set);
6805 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6806 /* remember the node context position for position() and context size for last(),
6807 * predicates should always be evaluated with respect to the child axis (since we do
6808 * not support explicit axes) so we assign positions based on their parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006809 if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) {
6810 orig_parent = lyd_parent(set->val.nodes[i].node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006811 orig_pos = 1;
6812 } else {
6813 ++orig_pos;
6814 }
6815
6816 set2.ctx_pos = orig_pos;
6817 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006818 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006819
Michal Vasko004d3152020-06-11 19:59:22 +02006820 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006821 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006822 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006823 return rc;
6824 }
6825
6826 /* number is a position */
6827 if (set2.type == LYXP_SET_NUMBER) {
6828 if ((long long)set2.val.num == orig_pos) {
6829 set2.val.num = 1;
6830 } else {
6831 set2.val.num = 0;
6832 }
6833 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006834 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006835
6836 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006837 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006838 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006839 }
6840 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006841 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006842
6843 } else if (set->type == LYXP_SET_SCNODE_SET) {
6844 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006845 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006846 /* there is a currently-valid node */
6847 break;
6848 }
6849 }
6850 /* empty set, nothing to evaluate */
6851 if (i == set->used) {
6852 goto only_parse;
6853 }
6854
Michal Vasko004d3152020-06-11 19:59:22 +02006855 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006856
Michal Vasko03ff5a72019-09-11 13:49:33 +02006857 /* set special in_ctx to all the valid snodes */
6858 pred_in_ctx = set_scnode_new_in_ctx(set);
6859
6860 /* use the valid snodes one-by-one */
6861 for (i = 0; i < set->used; ++i) {
6862 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6863 continue;
6864 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006865 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006866
Michal Vasko004d3152020-06-11 19:59:22 +02006867 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006868
Michal Vasko004d3152020-06-11 19:59:22 +02006869 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006870 LY_CHECK_RET(rc);
6871
6872 set->val.scnodes[i].in_ctx = pred_in_ctx;
6873 }
6874
6875 /* restore the state as it was before the predicate */
6876 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006877 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02006878 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006879 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006880 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006881 }
6882 }
6883
6884 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006885 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006886 set_fill_set(&set2, set);
6887
Michal Vasko004d3152020-06-11 19:59:22 +02006888 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006889 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006890 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006891 return rc;
6892 }
6893
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006894 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006895 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006896 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006897 }
Michal Vaskod3678892020-05-21 10:06:58 +02006898 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006899 }
6900
6901 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006902 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02006903 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006904 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006905 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006906
6907 return LY_SUCCESS;
6908}
6909
6910/**
Michal Vaskod3678892020-05-21 10:06:58 +02006911 * @brief Evaluate Literal. Logs directly on error.
6912 *
6913 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006914 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006915 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6916 */
6917static void
Michal Vasko40308e72020-10-20 16:38:40 +02006918eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006919{
6920 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006921 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006922 set_fill_string(set, "", 0);
6923 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006924 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 +02006925 }
6926 }
6927 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006928 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006929 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006930}
6931
6932/**
Michal Vasko004d3152020-06-11 19:59:22 +02006933 * @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 +02006934 *
Michal Vasko004d3152020-06-11 19:59:22 +02006935 * @param[in] exp Full parsed XPath expression.
6936 * @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 +02006937 * @param[in] ctx_node Found schema node as the context for the predicate.
6938 * @param[in] cur_mod Current module for the expression.
6939 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006940 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006941 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006942 * @param[out] predicates Parsed predicates.
6943 * @param[out] pred_type Type of @p predicates.
6944 * @return LY_SUCCESS on success,
6945 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006946 */
6947static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006948eval_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 +02006949 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 +02006950 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006951{
Michal Vasko004d3152020-06-11 19:59:22 +02006952 LY_ERR ret = LY_SUCCESS;
6953 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006954 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006955 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006956 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006957 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006958
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006959 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006960
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006961 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006962 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006963 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006964 return LY_EINVAL;
6965 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006966 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 +02006967 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006968
Michal Vasko004d3152020-06-11 19:59:22 +02006969 /* learn where the predicates end */
6970 e_idx = *tok_idx;
6971 while (key_count) {
6972 /* '[' */
6973 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6974 return LY_EINVAL;
6975 }
6976 ++e_idx;
6977
Michal Vasko3354d272021-04-06 09:40:06 +02006978 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
6979 /* definitely not a key */
6980 return LY_EINVAL;
6981 }
6982
Michal Vasko004d3152020-06-11 19:59:22 +02006983 /* ']' */
6984 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6985 ++e_idx;
6986 }
6987 ++e_idx;
6988
6989 /* another presumably key predicate parsed */
6990 --key_count;
6991 }
Michal Vasko004d3152020-06-11 19:59:22 +02006992 } else {
6993 /* learn just where this single predicate ends */
6994 e_idx = *tok_idx;
6995
Michal Vaskod3678892020-05-21 10:06:58 +02006996 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006997 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6998 return LY_EINVAL;
6999 }
7000 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007001
Michal Vasko3354d272021-04-06 09:40:06 +02007002 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
7003 /* definitely not the value */
7004 return LY_EINVAL;
7005 }
7006
Michal Vaskod3678892020-05-21 10:06:58 +02007007 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007008 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7009 ++e_idx;
7010 }
7011 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007012 }
7013
Michal Vasko004d3152020-06-11 19:59:22 +02007014 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
7015 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
7016
7017 /* turn logging off */
7018 prev_lo = ly_log_options(0);
7019
7020 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007021 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
7022 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007023
7024 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007025 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
7026 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02007027 LY_CHECK_GOTO(ret, cleanup);
7028
7029 /* success, the predicate must include all the needed information for hash-based search */
7030 *tok_idx = e_idx;
7031
7032cleanup:
7033 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007034 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02007035 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02007036}
7037
7038/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007039 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7040 * data node(s) could be found using a single hash-based search.
7041 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007042 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007043 * @param[in] node Next context node to check.
7044 * @param[in] name Expected node name.
7045 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007046 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007047 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007048 * @param[in] format Prefix format.
7049 * @param[in,out] found Previously found node, is updated.
7050 * @return LY_SUCCESS on success,
7051 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7052 */
7053static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007054eval_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 +02007055 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 +02007056 const struct lysc_node **found)
7057{
7058 const struct lysc_node *scnode;
7059 const struct lys_module *mod;
7060 uint32_t idx = 0;
7061
Radek Krejci8df109d2021-04-23 12:19:08 +02007062 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007063
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007064continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007065 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007066 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007067 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007068 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007069 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007070 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7071 if (scnode) {
7072 /* we have found a match */
7073 break;
7074 }
7075 }
7076
Michal Vasko7d1d0912020-10-16 08:38:30 +02007077 if (!scnode) {
7078 /* all modules searched */
7079 idx = 0;
7080 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007081 } else {
7082 /* search in top-level */
7083 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7084 }
7085 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007086 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007087 /* we must adjust the module to inherit the one from the context node */
7088 moveto_mod = node->schema->module;
7089 }
7090
7091 /* search in children, do not repeat the same search */
7092 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007093 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007094
7095 /* additional context check */
7096 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7097 scnode = NULL;
7098 }
7099
7100 if (scnode) {
7101 if (*found) {
7102 /* we found a schema node with the same name but at different level, give up, too complicated
7103 * (more hash-based searches would be required, not supported) */
7104 return LY_ENOT;
7105 } else {
7106 /* remember the found schema node and continue to make sure it can be used */
7107 *found = scnode;
7108 }
7109 }
7110
7111 if (idx) {
7112 /* continue searching all the following models */
7113 goto continue_search;
7114 }
7115
7116 return LY_SUCCESS;
7117}
7118
7119/**
Michal Vaskod3678892020-05-21 10:06:58 +02007120 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7121 *
7122 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7123 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7124 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7125 *
7126 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007127 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007128 * @param[in] attr_axis Whether to search attributes or standard nodes.
7129 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007130 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007131 * @param[in] options XPath options.
7132 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7133 */
7134static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007135eval_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 +02007136 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007137{
Michal Vaskod3678892020-05-21 10:06:58 +02007138 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007139 const char *ncname, *ncname_dict = NULL;
7140 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007141 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007142 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007143 struct ly_path_predicate *predicates = NULL;
7144 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007145 LY_ERR rc = LY_SUCCESS;
7146
aPiecek8b0cc152021-05-31 16:40:31 +02007147 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007148 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007149 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007150
aPiecek8b0cc152021-05-31 16:40:31 +02007151 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02007152 goto moveto;
7153 }
7154
Michal Vasko004d3152020-06-11 19:59:22 +02007155 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7156 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007157
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007158 if ((ncname[0] == '*') && (ncname_len == 1)) {
7159 /* all nodes will match */
7160 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007161 }
7162
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007163 /* parse (and skip) module name */
7164 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007165 LY_CHECK_GOTO(rc, cleanup);
7166
Radek Krejci8df109d2021-04-23 12:19:08 +02007167 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 +02007168 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007169 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007170 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7171 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007172 /* check failed */
7173 scnode = NULL;
7174 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007175 }
7176 }
7177
Michal Vasko004d3152020-06-11 19:59:22 +02007178 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7179 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007180 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7181 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007182 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007183 scnode = NULL;
7184 }
7185 }
7186 }
7187
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007188 if (!scnode) {
7189 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007190 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007191 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007192 }
7193
7194moveto:
7195 /* move to the attribute(s), data node(s), or schema node(s) */
7196 if (attr_axis) {
aPiecek8b0cc152021-05-31 16:40:31 +02007197 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007198 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007199 } else {
7200 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007201 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007202 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007203 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007204 }
7205 LY_CHECK_GOTO(rc, cleanup);
7206 }
7207 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007208 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007209 int64_t i;
7210
Michal Vaskod3678892020-05-21 10:06:58 +02007211 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007212 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007213 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007214 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007215 }
7216 LY_CHECK_GOTO(rc, cleanup);
7217
7218 for (i = set->used - 1; i > -1; --i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007219 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vaskod3678892020-05-21 10:06:58 +02007220 break;
7221 }
7222 }
7223 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007224 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vasko90f12dc2020-12-03 14:20:42 +01007225 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".",
7226 ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007227 free(path);
7228 }
7229 } else {
7230 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007231 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007232 } else {
7233 if (scnode) {
7234 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007235 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007236 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007237 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007238 }
7239 }
7240 LY_CHECK_GOTO(rc, cleanup);
7241 }
7242 }
7243
7244 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007245 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7246 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007247 LY_CHECK_RET(rc);
7248 }
7249
7250cleanup:
aPiecek8b0cc152021-05-31 16:40:31 +02007251 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007252 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007253 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007254 }
Michal Vaskod3678892020-05-21 10:06:58 +02007255 return rc;
7256}
7257
7258/**
7259 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7260 *
7261 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7262 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7263 * [8] NodeType ::= 'text' | 'node'
7264 *
7265 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007266 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007267 * @param[in] attr_axis Whether to search attributes or standard nodes.
7268 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007269 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007270 * @param[in] options XPath options.
7271 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7272 */
7273static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007274eval_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 +02007275 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007276{
7277 LY_ERR rc;
7278
7279 /* TODO */
7280 (void)attr_axis;
7281 (void)all_desc;
7282
aPiecek8b0cc152021-05-31 16:40:31 +02007283 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007284 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007285 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007286 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
aPiecek8b0cc152021-05-31 16:40:31 +02007287 options |= LYXP_SKIP_EXPR;
Michal Vaskod3678892020-05-21 10:06:58 +02007288 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007289 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007290 rc = xpath_node(NULL, 0, set, options);
7291 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007292 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007293 rc = xpath_text(NULL, 0, set, options);
7294 }
7295 LY_CHECK_RET(rc);
7296 }
7297 }
aPiecek8b0cc152021-05-31 16:40:31 +02007298 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007299 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007300 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007301
7302 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007303 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007304 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007305 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007306 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007307
7308 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007309 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007310 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007311 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007312 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007313
7314 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007315 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7316 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007317 LY_CHECK_RET(rc);
7318 }
7319
7320 return LY_SUCCESS;
7321}
7322
7323/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007324 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7325 *
7326 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7327 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007328 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007329 *
7330 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007331 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007332 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007333 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007334 * @param[in] options XPath options.
7335 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7336 */
7337static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007338eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7339 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007340{
Radek Krejci857189e2020-09-01 13:26:36 +02007341 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007342 LY_ERR rc;
7343
7344 goto step;
7345 do {
7346 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007347 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007348 all_desc = 0;
7349 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007350 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007351 all_desc = 1;
7352 }
aPiecek8b0cc152021-05-31 16:40:31 +02007353 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007354 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007355 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007356
7357step:
Michal Vaskod3678892020-05-21 10:06:58 +02007358 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007359 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007360 attr_axis = 1;
7361
aPiecek8b0cc152021-05-31 16:40:31 +02007362 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007363 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007364 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007365 } else {
7366 attr_axis = 0;
7367 }
7368
Michal Vasko03ff5a72019-09-11 13:49:33 +02007369 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007370 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007371 case LYXP_TOKEN_DOT:
7372 /* evaluate '.' */
aPiecek8b0cc152021-05-31 16:40:31 +02007373 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007374 rc = moveto_scnode_self(set, all_desc, options);
7375 } else {
7376 rc = moveto_self(set, all_desc, options);
7377 }
7378 LY_CHECK_RET(rc);
7379 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007380 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007381 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007382 break;
7383
7384 case LYXP_TOKEN_DDOT:
7385 /* evaluate '..' */
aPiecek8b0cc152021-05-31 16:40:31 +02007386 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007387 rc = moveto_scnode_parent(set, all_desc, options);
7388 } else {
7389 rc = moveto_parent(set, all_desc, options);
7390 }
7391 LY_CHECK_RET(rc);
aPiecek8b0cc152021-05-31 16:40:31 +02007392 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007393 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007394 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007395 break;
7396
Michal Vasko03ff5a72019-09-11 13:49:33 +02007397 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007398 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007399 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007400 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007401 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007402
Michal Vaskod3678892020-05-21 10:06:58 +02007403 case LYXP_TOKEN_NODETYPE:
7404 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007405 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007406 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007407 break;
7408
7409 default:
aPiecek8b0cc152021-05-31 16:40:31 +02007410 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007411 }
Michal Vasko004d3152020-06-11 19:59:22 +02007412 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007413
7414 return LY_SUCCESS;
7415}
7416
7417/**
7418 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7419 *
7420 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7421 *
7422 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007423 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007424 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007425 * @param[in] options XPath options.
7426 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7427 */
7428static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007429eval_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 +02007430{
Radek Krejci857189e2020-09-01 13:26:36 +02007431 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007432
aPiecek8b0cc152021-05-31 16:40:31 +02007433 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007434 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007435 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007436 }
7437
7438 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007439 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007440 /* evaluate '/' - deferred */
7441 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02007442 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007443 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007444 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007445
Michal Vasko004d3152020-06-11 19:59:22 +02007446 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007447 return LY_SUCCESS;
7448 }
Michal Vasko004d3152020-06-11 19:59:22 +02007449 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007450 case LYXP_TOKEN_DOT:
7451 case LYXP_TOKEN_DDOT:
7452 case LYXP_TOKEN_AT:
7453 case LYXP_TOKEN_NAMETEST:
7454 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007455 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007456 break;
7457 default:
7458 break;
7459 }
7460
Michal Vasko03ff5a72019-09-11 13:49:33 +02007461 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007462 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007463 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7464 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02007465 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007466 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007467 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007468
Michal Vaskob0099a92020-08-31 14:55:23 +02007469 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007470 }
7471
7472 return LY_SUCCESS;
7473}
7474
7475/**
7476 * @brief Evaluate FunctionCall. Logs directly on error.
7477 *
Michal Vaskod3678892020-05-21 10:06:58 +02007478 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007479 *
7480 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007481 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007482 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007483 * @param[in] options XPath options.
7484 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7485 */
7486static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007487eval_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 +02007488{
7489 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007490
Radek Krejci1deb5be2020-08-26 16:43:36 +02007491 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007492 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007493 struct lyxp_set **args = NULL, **args_aux;
7494
aPiecek8b0cc152021-05-31 16:40:31 +02007495 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007496 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007497 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007498 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007499 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007500 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007501 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007502 xpath_func = &xpath_sum;
7503 }
7504 break;
7505 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007506 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007507 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007508 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007509 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007510 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007511 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007512 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007513 xpath_func = &xpath_true;
7514 }
7515 break;
7516 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007517 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007518 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007519 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007520 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007521 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007522 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007523 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007524 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007525 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007526 xpath_func = &xpath_deref;
7527 }
7528 break;
7529 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007530 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007531 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007532 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007533 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007534 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007535 xpath_func = &xpath_string;
7536 }
7537 break;
7538 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007539 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007540 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007541 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007542 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007543 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007544 xpath_func = &xpath_current;
7545 }
7546 break;
7547 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007548 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007549 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007550 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007551 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007552 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007553 xpath_func = &xpath_re_match;
7554 }
7555 break;
7556 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007557 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007558 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007559 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007560 xpath_func = &xpath_translate;
7561 }
7562 break;
7563 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007564 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007565 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007566 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007567 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007568 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007569 xpath_func = &xpath_bit_is_set;
7570 }
7571 break;
7572 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007573 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007574 xpath_func = &xpath_starts_with;
7575 }
7576 break;
7577 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007578 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007579 xpath_func = &xpath_derived_from;
7580 }
7581 break;
7582 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007583 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007584 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007585 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007586 xpath_func = &xpath_string_length;
7587 }
7588 break;
7589 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007590 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007591 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007592 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007593 xpath_func = &xpath_substring_after;
7594 }
7595 break;
7596 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007597 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007598 xpath_func = &xpath_substring_before;
7599 }
7600 break;
7601 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007602 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007603 xpath_func = &xpath_derived_from_or_self;
7604 }
7605 break;
7606 }
7607
7608 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007609 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 +02007610 return LY_EVALID;
7611 }
7612 }
7613
aPiecek8b0cc152021-05-31 16:40:31 +02007614 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007615 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007616 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007617
7618 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007619 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007620 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007621 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007622 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007623
7624 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007625 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02007626 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007627 args = malloc(sizeof *args);
7628 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7629 arg_count = 1;
7630 args[0] = set_copy(set);
7631 if (!args[0]) {
7632 rc = LY_EMEM;
7633 goto cleanup;
7634 }
7635
Michal Vasko004d3152020-06-11 19:59:22 +02007636 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007637 LY_CHECK_GOTO(rc, cleanup);
7638 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007639 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007640 LY_CHECK_GOTO(rc, cleanup);
7641 }
7642 }
Michal Vasko004d3152020-06-11 19:59:22 +02007643 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02007644 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007645 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007646 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007647
aPiecek8b0cc152021-05-31 16:40:31 +02007648 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007649 ++arg_count;
7650 args_aux = realloc(args, arg_count * sizeof *args);
7651 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7652 args = args_aux;
7653 args[arg_count - 1] = set_copy(set);
7654 if (!args[arg_count - 1]) {
7655 rc = LY_EMEM;
7656 goto cleanup;
7657 }
7658
Michal Vasko004d3152020-06-11 19:59:22 +02007659 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007660 LY_CHECK_GOTO(rc, cleanup);
7661 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007662 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007663 LY_CHECK_GOTO(rc, cleanup);
7664 }
7665 }
7666
7667 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007668 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007669 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007670 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007671 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007672
aPiecek8b0cc152021-05-31 16:40:31 +02007673 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007674 /* evaluate function */
7675 rc = xpath_func(args, arg_count, set, options);
7676
7677 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007678 /* merge all nodes from arg evaluations */
7679 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007680 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007681 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007682 }
7683 }
7684 } else {
7685 rc = LY_SUCCESS;
7686 }
7687
7688cleanup:
7689 for (i = 0; i < arg_count; ++i) {
7690 lyxp_set_free(args[i]);
7691 }
7692 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007693 return rc;
7694}
7695
7696/**
7697 * @brief Evaluate Number. Logs directly on error.
7698 *
7699 * @param[in] ctx Context for errors.
7700 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007701 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007702 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7703 * @return LY_ERR
7704 */
7705static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007706eval_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 +02007707{
7708 long double num;
7709 char *endptr;
7710
7711 if (set) {
7712 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007713 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007714 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007715 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7716 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007717 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007718 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007719 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007720 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7721 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007722 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007723 return LY_EVALID;
7724 }
7725
7726 set_fill_number(set, num);
7727 }
7728
7729 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007730 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007731 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007732 return LY_SUCCESS;
7733}
7734
7735/**
7736 * @brief Evaluate PathExpr. Logs directly on error.
7737 *
Michal Vaskod3678892020-05-21 10:06:58 +02007738 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007739 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7740 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7741 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007742 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007743 *
7744 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007745 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007746 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007747 * @param[in] options XPath options.
7748 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7749 */
7750static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007751eval_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 +02007752{
Radek Krejci857189e2020-09-01 13:26:36 +02007753 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007754 LY_ERR rc;
7755
Michal Vasko004d3152020-06-11 19:59:22 +02007756 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007757 case LYXP_TOKEN_PAR1:
7758 /* '(' Expr ')' */
7759
7760 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02007761 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007762 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007763 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007764
7765 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007766 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007767 LY_CHECK_RET(rc);
7768
7769 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007770 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
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 parent_pos_pred = 0;
7776 goto predicate;
7777
7778 case LYXP_TOKEN_DOT:
7779 case LYXP_TOKEN_DDOT:
7780 case LYXP_TOKEN_AT:
7781 case LYXP_TOKEN_NAMETEST:
7782 case LYXP_TOKEN_NODETYPE:
7783 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007784 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007785 LY_CHECK_RET(rc);
7786 break;
7787
7788 case LYXP_TOKEN_FUNCNAME:
7789 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02007790 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007791 LY_CHECK_RET(rc);
7792
7793 parent_pos_pred = 1;
7794 goto predicate;
7795
Michal Vasko3e48bf32020-06-01 08:39:07 +02007796 case LYXP_TOKEN_OPER_PATH:
7797 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007798 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007799 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007800 LY_CHECK_RET(rc);
7801 break;
7802
7803 case LYXP_TOKEN_LITERAL:
7804 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02007805 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7806 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007807 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007808 }
Michal Vasko004d3152020-06-11 19:59:22 +02007809 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007810 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007811 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007812 }
7813
7814 parent_pos_pred = 1;
7815 goto predicate;
7816
7817 case LYXP_TOKEN_NUMBER:
7818 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02007819 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7820 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007821 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007822 }
Michal Vasko004d3152020-06-11 19:59:22 +02007823 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007824 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007825 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007826 }
7827 LY_CHECK_RET(rc);
7828
7829 parent_pos_pred = 1;
7830 goto predicate;
7831
7832 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01007833 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 +02007834 return LY_EVALID;
7835 }
7836
7837 return LY_SUCCESS;
7838
7839predicate:
7840 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007841 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7842 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007843 LY_CHECK_RET(rc);
7844 }
7845
7846 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007847 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007848
7849 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007850 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007851 all_desc = 0;
7852 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007853 all_desc = 1;
7854 }
7855
aPiecek8b0cc152021-05-31 16:40:31 +02007856 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007857 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007858 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007859
Michal Vasko004d3152020-06-11 19:59:22 +02007860 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007861 LY_CHECK_RET(rc);
7862 }
7863
7864 return LY_SUCCESS;
7865}
7866
7867/**
7868 * @brief Evaluate UnionExpr. Logs directly on error.
7869 *
Michal Vaskod3678892020-05-21 10:06:58 +02007870 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007871 *
7872 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007873 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007874 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007875 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007876 * @param[in] options XPath options.
7877 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7878 */
7879static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007880eval_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 +02007881{
7882 LY_ERR rc = LY_SUCCESS;
7883 struct lyxp_set orig_set, set2;
7884 uint16_t i;
7885
7886 assert(repeat);
7887
7888 set_init(&orig_set, set);
7889 set_init(&set2, set);
7890
7891 set_fill_set(&orig_set, set);
7892
Michal Vasko004d3152020-06-11 19:59:22 +02007893 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007894 LY_CHECK_GOTO(rc, cleanup);
7895
7896 /* ('|' PathExpr)* */
7897 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007898 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02007899 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007900 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007901 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007902
aPiecek8b0cc152021-05-31 16:40:31 +02007903 if (options & LYXP_SKIP_EXPR) {
7904 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007905 LY_CHECK_GOTO(rc, cleanup);
7906 continue;
7907 }
7908
7909 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007910 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007911 LY_CHECK_GOTO(rc, cleanup);
7912
7913 /* eval */
7914 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007915 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007916 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007917 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007918 LY_CHECK_GOTO(rc, cleanup);
7919 }
7920 }
7921
7922cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007923 lyxp_set_free_content(&orig_set);
7924 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007925 return rc;
7926}
7927
7928/**
7929 * @brief Evaluate UnaryExpr. Logs directly on error.
7930 *
Michal Vaskod3678892020-05-21 10:06:58 +02007931 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007932 *
7933 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007934 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007935 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007936 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007937 * @param[in] options XPath options.
7938 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7939 */
7940static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007941eval_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 +02007942{
7943 LY_ERR rc;
7944 uint16_t this_op, i;
7945
7946 assert(repeat);
7947
7948 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007949 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007950 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007951 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 +02007952
aPiecek8b0cc152021-05-31 16:40:31 +02007953 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007954 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007955 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007956 }
7957
Michal Vasko004d3152020-06-11 19:59:22 +02007958 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007959 LY_CHECK_RET(rc);
7960
aPiecek8b0cc152021-05-31 16:40:31 +02007961 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007962 if (options & LYXP_SCNODE_ALL) {
7963 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7964 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007965 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007966 LY_CHECK_RET(rc);
7967 }
7968 }
7969
7970 return LY_SUCCESS;
7971}
7972
7973/**
7974 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7975 *
Michal Vaskod3678892020-05-21 10:06:58 +02007976 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007977 * | MultiplicativeExpr '*' UnaryExpr
7978 * | MultiplicativeExpr 'div' UnaryExpr
7979 * | MultiplicativeExpr 'mod' UnaryExpr
7980 *
7981 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007982 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007983 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007984 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007985 * @param[in] options XPath options.
7986 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7987 */
7988static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007989eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
7990 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007991{
7992 LY_ERR rc;
7993 uint16_t this_op;
7994 struct lyxp_set orig_set, set2;
7995 uint16_t i;
7996
7997 assert(repeat);
7998
7999 set_init(&orig_set, set);
8000 set_init(&set2, set);
8001
8002 set_fill_set(&orig_set, set);
8003
Michal Vasko004d3152020-06-11 19:59:22 +02008004 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008005 LY_CHECK_GOTO(rc, cleanup);
8006
8007 /* ('*' / 'div' / 'mod' UnaryExpr)* */
8008 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008009 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008010
Michal Vasko004d3152020-06-11 19:59:22 +02008011 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02008012 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008013 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008014 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008015
aPiecek8b0cc152021-05-31 16:40:31 +02008016 if (options & LYXP_SKIP_EXPR) {
8017 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008018 LY_CHECK_GOTO(rc, cleanup);
8019 continue;
8020 }
8021
8022 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008023 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008024 LY_CHECK_GOTO(rc, cleanup);
8025
8026 /* eval */
8027 if (options & LYXP_SCNODE_ALL) {
8028 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008029 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008030 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008031 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008032 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008033 LY_CHECK_GOTO(rc, cleanup);
8034 }
8035 }
8036
8037cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008038 lyxp_set_free_content(&orig_set);
8039 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008040 return rc;
8041}
8042
8043/**
8044 * @brief Evaluate AdditiveExpr. Logs directly on error.
8045 *
Michal Vaskod3678892020-05-21 10:06:58 +02008046 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008047 * | AdditiveExpr '+' MultiplicativeExpr
8048 * | AdditiveExpr '-' MultiplicativeExpr
8049 *
8050 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008051 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008052 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008053 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008054 * @param[in] options XPath options.
8055 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8056 */
8057static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008058eval_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 +02008059{
8060 LY_ERR rc;
8061 uint16_t this_op;
8062 struct lyxp_set orig_set, set2;
8063 uint16_t i;
8064
8065 assert(repeat);
8066
8067 set_init(&orig_set, set);
8068 set_init(&set2, set);
8069
8070 set_fill_set(&orig_set, set);
8071
Michal Vasko004d3152020-06-11 19:59:22 +02008072 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008073 LY_CHECK_GOTO(rc, cleanup);
8074
8075 /* ('+' / '-' MultiplicativeExpr)* */
8076 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008077 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008078
Michal Vasko004d3152020-06-11 19:59:22 +02008079 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008080 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008081 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008082 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008083
aPiecek8b0cc152021-05-31 16:40:31 +02008084 if (options & LYXP_SKIP_EXPR) {
8085 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008086 LY_CHECK_GOTO(rc, cleanup);
8087 continue;
8088 }
8089
8090 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008091 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008092 LY_CHECK_GOTO(rc, cleanup);
8093
8094 /* eval */
8095 if (options & LYXP_SCNODE_ALL) {
8096 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008097 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008098 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008099 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008100 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008101 LY_CHECK_GOTO(rc, cleanup);
8102 }
8103 }
8104
8105cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008106 lyxp_set_free_content(&orig_set);
8107 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008108 return rc;
8109}
8110
8111/**
8112 * @brief Evaluate RelationalExpr. Logs directly on error.
8113 *
Michal Vaskod3678892020-05-21 10:06:58 +02008114 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008115 * | RelationalExpr '<' AdditiveExpr
8116 * | RelationalExpr '>' AdditiveExpr
8117 * | RelationalExpr '<=' AdditiveExpr
8118 * | RelationalExpr '>=' AdditiveExpr
8119 *
8120 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008121 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008122 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008123 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008124 * @param[in] options XPath options.
8125 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8126 */
8127static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008128eval_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 +02008129{
8130 LY_ERR rc;
8131 uint16_t this_op;
8132 struct lyxp_set orig_set, set2;
8133 uint16_t i;
8134
8135 assert(repeat);
8136
8137 set_init(&orig_set, set);
8138 set_init(&set2, set);
8139
8140 set_fill_set(&orig_set, set);
8141
Michal Vasko004d3152020-06-11 19:59:22 +02008142 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008143 LY_CHECK_GOTO(rc, cleanup);
8144
8145 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8146 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008147 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008148
Michal Vasko004d3152020-06-11 19:59:22 +02008149 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02008150 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008151 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008152 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008153
aPiecek8b0cc152021-05-31 16:40:31 +02008154 if (options & LYXP_SKIP_EXPR) {
8155 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008156 LY_CHECK_GOTO(rc, cleanup);
8157 continue;
8158 }
8159
8160 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008161 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008162 LY_CHECK_GOTO(rc, cleanup);
8163
8164 /* eval */
8165 if (options & LYXP_SCNODE_ALL) {
8166 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008167 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008168 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008169 } else {
8170 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8171 LY_CHECK_GOTO(rc, cleanup);
8172 }
8173 }
8174
8175cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008176 lyxp_set_free_content(&orig_set);
8177 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008178 return rc;
8179}
8180
8181/**
8182 * @brief Evaluate EqualityExpr. Logs directly on error.
8183 *
Michal Vaskod3678892020-05-21 10:06:58 +02008184 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008185 * | EqualityExpr '!=' RelationalExpr
8186 *
8187 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008188 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008189 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008190 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008191 * @param[in] options XPath options.
8192 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8193 */
8194static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008195eval_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 +02008196{
8197 LY_ERR rc;
8198 uint16_t this_op;
8199 struct lyxp_set orig_set, set2;
8200 uint16_t i;
8201
8202 assert(repeat);
8203
8204 set_init(&orig_set, set);
8205 set_init(&set2, set);
8206
8207 set_fill_set(&orig_set, set);
8208
Michal Vasko004d3152020-06-11 19:59:22 +02008209 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008210 LY_CHECK_GOTO(rc, cleanup);
8211
8212 /* ('=' / '!=' RelationalExpr)* */
8213 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008214 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008215
Michal Vasko004d3152020-06-11 19:59:22 +02008216 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02008217 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008218 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008219 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008220
aPiecek8b0cc152021-05-31 16:40:31 +02008221 if (options & LYXP_SKIP_EXPR) {
8222 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008223 LY_CHECK_GOTO(rc, cleanup);
8224 continue;
8225 }
8226
8227 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008228 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008229 LY_CHECK_GOTO(rc, cleanup);
8230
8231 /* eval */
8232 if (options & LYXP_SCNODE_ALL) {
8233 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008234 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8235 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008236 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008237 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008238 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008239 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8240 LY_CHECK_GOTO(rc, cleanup);
8241 }
8242 }
8243
8244cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008245 lyxp_set_free_content(&orig_set);
8246 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008247 return rc;
8248}
8249
8250/**
8251 * @brief Evaluate AndExpr. Logs directly on error.
8252 *
Michal Vaskod3678892020-05-21 10:06:58 +02008253 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008254 *
8255 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008256 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008257 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008258 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008259 * @param[in] options XPath options.
8260 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8261 */
8262static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008263eval_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 +02008264{
8265 LY_ERR rc;
8266 struct lyxp_set orig_set, set2;
8267 uint16_t i;
8268
8269 assert(repeat);
8270
8271 set_init(&orig_set, set);
8272 set_init(&set2, set);
8273
8274 set_fill_set(&orig_set, set);
8275
Michal Vasko004d3152020-06-11 19:59:22 +02008276 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008277 LY_CHECK_GOTO(rc, cleanup);
8278
8279 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008280 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008281 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008282 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008283 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008284 }
8285
8286 /* ('and' EqualityExpr)* */
8287 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008288 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008289 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008290 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008291 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008292
8293 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008294 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8295 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008296 LY_CHECK_GOTO(rc, cleanup);
8297 continue;
8298 }
8299
8300 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008301 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008302 LY_CHECK_GOTO(rc, cleanup);
8303
8304 /* eval - just get boolean value actually */
8305 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008306 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008307 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008308 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008309 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008310 set_fill_set(set, &set2);
8311 }
8312 }
8313
8314cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008315 lyxp_set_free_content(&orig_set);
8316 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008317 return rc;
8318}
8319
8320/**
8321 * @brief Evaluate OrExpr. Logs directly on error.
8322 *
Michal Vaskod3678892020-05-21 10:06:58 +02008323 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008324 *
8325 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008326 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008327 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008328 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008329 * @param[in] options XPath options.
8330 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8331 */
8332static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008333eval_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 +02008334{
8335 LY_ERR rc;
8336 struct lyxp_set orig_set, set2;
8337 uint16_t i;
8338
8339 assert(repeat);
8340
8341 set_init(&orig_set, set);
8342 set_init(&set2, set);
8343
8344 set_fill_set(&orig_set, set);
8345
Michal Vasko004d3152020-06-11 19:59:22 +02008346 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008347 LY_CHECK_GOTO(rc, cleanup);
8348
8349 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008350 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008351 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008352 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008353 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008354 }
8355
8356 /* ('or' AndExpr)* */
8357 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008358 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008359 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008360 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008361 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008362
8363 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008364 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8365 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008366 LY_CHECK_GOTO(rc, cleanup);
8367 continue;
8368 }
8369
8370 set_fill_set(&set2, &orig_set);
8371 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8372 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008373 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008374 LY_CHECK_GOTO(rc, cleanup);
8375
8376 /* eval - just get boolean value actually */
8377 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008378 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008379 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008380 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008381 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008382 set_fill_set(set, &set2);
8383 }
8384 }
8385
8386cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008387 lyxp_set_free_content(&orig_set);
8388 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008389 return rc;
8390}
8391
8392/**
Michal Vasko004d3152020-06-11 19:59:22 +02008393 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008394 *
8395 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008396 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008397 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02008398 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008399 * @param[in] options XPath options.
8400 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8401 */
8402static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008403eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8404 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008405{
8406 uint16_t i, count;
8407 enum lyxp_expr_type next_etype;
8408 LY_ERR rc;
8409
8410 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008411 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008412 next_etype = LYXP_EXPR_NONE;
8413 } else {
8414 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008415 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008416
8417 /* select one-priority lower because etype expression called us */
8418 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008419 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008420 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008421 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008422 } else {
8423 next_etype = LYXP_EXPR_NONE;
8424 }
8425 }
8426
8427 /* decide what expression are we parsing based on the repeat */
8428 switch (next_etype) {
8429 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008430 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008431 break;
8432 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008433 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008434 break;
8435 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008436 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008437 break;
8438 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008439 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008440 break;
8441 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008442 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008443 break;
8444 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008445 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008446 break;
8447 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008448 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008449 break;
8450 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008451 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008452 break;
8453 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008454 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008455 break;
8456 default:
8457 LOGINT_RET(set->ctx);
8458 }
8459
8460 return rc;
8461}
8462
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008463/**
8464 * @brief Get root type.
8465 *
8466 * @param[in] ctx_node Context node.
8467 * @param[in] ctx_scnode Schema context node.
8468 * @param[in] options XPath options.
8469 * @return Root type.
8470 */
8471static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008472lyxp_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 +01008473{
Michal Vasko6b26e742020-07-17 15:02:10 +02008474 const struct lysc_node *op;
8475
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008476 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008477 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008478 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008479
8480 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008481 /* general root that can access everything */
8482 return LYXP_NODE_ROOT;
8483 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8484 /* root context node can access only config data (because we said so, it is unspecified) */
8485 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008486 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008487 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008488 }
8489
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008490 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008491 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008492 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008493
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008494 if (op || !(options & LYXP_SCHEMA)) {
8495 /* general root that can access everything */
8496 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05008497 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008498 /* root context node can access only config data (because we said so, it is unspecified) */
8499 return LYXP_NODE_ROOT_CONFIG;
8500 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008501 return LYXP_NODE_ROOT;
8502}
8503
Michal Vasko03ff5a72019-09-11 13:49:33 +02008504LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008505lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008506 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 +01008507 struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008508{
Michal Vasko004d3152020-06-11 19:59:22 +02008509 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008510 LY_ERR rc;
8511
Michal Vasko400e9672021-01-11 13:39:17 +01008512 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008513 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008514 LOGARG(NULL, "Current module must be set if schema format is used.");
8515 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008516 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008517
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008518 if (tree) {
8519 /* adjust the pointer to be the first top-level sibling */
8520 while (tree->parent) {
8521 tree = lyd_parent(tree);
8522 }
8523 tree = lyd_first_sibling(tree);
8524 }
8525
Michal Vasko03ff5a72019-09-11 13:49:33 +02008526 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008527 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008528 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008529 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8530 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8531
Michal Vasko400e9672021-01-11 13:39:17 +01008532 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008533 set->cur_node = ctx_node;
8534 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008535 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8536 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008537 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008538 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008539 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008540 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008541
Radek Krejciddace2c2021-01-08 11:30:56 +01008542 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008543
Michal Vasko03ff5a72019-09-11 13:49:33 +02008544 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008545 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008546 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008547 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008548 }
8549
Radek Krejciddace2c2021-01-08 11:30:56 +01008550 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008551 return rc;
8552}
8553
8554#if 0
8555
8556/* full xml printing of set elements, not used currently */
8557
8558void
8559lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8560{
8561 uint32_t i;
8562 char *str_num;
8563 struct lyout out;
8564
8565 memset(&out, 0, sizeof out);
8566
8567 out.type = LYOUT_STREAM;
8568 out.method.f = f;
8569
8570 switch (set->type) {
8571 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008572 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008573 break;
8574 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008575 ly_print_(&out, "Boolean XPath set:\n");
8576 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008577 break;
8578 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008579 ly_print_(&out, "String XPath set:\n");
8580 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008581 break;
8582 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008583 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008584
8585 if (isnan(set->value.num)) {
8586 str_num = strdup("NaN");
8587 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8588 str_num = strdup("0");
8589 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8590 str_num = strdup("Infinity");
8591 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8592 str_num = strdup("-Infinity");
8593 } else if ((long long)set->value.num == set->value.num) {
8594 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8595 str_num = NULL;
8596 }
8597 } else {
8598 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8599 str_num = NULL;
8600 }
8601 }
8602 if (!str_num) {
8603 LOGMEM;
8604 return;
8605 }
Michal Vasko5233e962020-08-14 14:26:20 +02008606 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008607 free(str_num);
8608 break;
8609 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008610 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008611
8612 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008613 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008614 switch (set->node_type[i]) {
8615 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008616 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008617 break;
8618 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008619 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008620 break;
8621 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008622 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008623 break;
8624 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008625 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008626 break;
8627 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008628 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008629 break;
8630 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008631 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008632 break;
8633 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008634 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008635 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008636 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008637 break;
8638 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008639 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 +02008640 break;
8641 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008642 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 +02008643 break;
8644 }
8645 }
8646 break;
8647 }
8648}
8649
8650#endif
8651
8652LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008653lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008654{
8655 long double num;
8656 char *str;
8657 LY_ERR rc;
8658
8659 if (!set || (set->type == target)) {
8660 return LY_SUCCESS;
8661 }
8662
8663 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008664 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008665
8666 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008667 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008668 return LY_EINVAL;
8669 }
8670
8671 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008672 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008673 switch (set->type) {
8674 case LYXP_SET_NUMBER:
8675 if (isnan(set->val.num)) {
8676 set->val.str = strdup("NaN");
8677 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8678 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8679 set->val.str = strdup("0");
8680 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8681 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8682 set->val.str = strdup("Infinity");
8683 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8684 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8685 set->val.str = strdup("-Infinity");
8686 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8687 } else if ((long long)set->val.num == set->val.num) {
8688 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8689 LOGMEM_RET(set->ctx);
8690 }
8691 set->val.str = str;
8692 } else {
8693 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8694 LOGMEM_RET(set->ctx);
8695 }
8696 set->val.str = str;
8697 }
8698 break;
8699 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008700 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008701 set->val.str = strdup("true");
8702 } else {
8703 set->val.str = strdup("false");
8704 }
8705 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8706 break;
8707 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008708 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008709 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008710
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008711 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008712 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008713 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008714 set->val.str = str;
8715 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008716 default:
8717 LOGINT_RET(set->ctx);
8718 }
8719 set->type = LYXP_SET_STRING;
8720 }
8721
8722 /* to NUMBER */
8723 if (target == LYXP_SET_NUMBER) {
8724 switch (set->type) {
8725 case LYXP_SET_STRING:
8726 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008727 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008728 set->val.num = num;
8729 break;
8730 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008731 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008732 set->val.num = 1;
8733 } else {
8734 set->val.num = 0;
8735 }
8736 break;
8737 default:
8738 LOGINT_RET(set->ctx);
8739 }
8740 set->type = LYXP_SET_NUMBER;
8741 }
8742
8743 /* to BOOLEAN */
8744 if (target == LYXP_SET_BOOLEAN) {
8745 switch (set->type) {
8746 case LYXP_SET_NUMBER:
8747 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008748 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008749 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008750 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008751 }
8752 break;
8753 case LYXP_SET_STRING:
8754 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008755 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008756 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008757 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008758 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008759 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008760 }
8761 break;
8762 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008763 if (set->used) {
8764 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008765 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008766 } else {
8767 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008768 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008769 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008770 break;
8771 default:
8772 LOGINT_RET(set->ctx);
8773 }
8774 set->type = LYXP_SET_BOOLEAN;
8775 }
8776
Michal Vasko03ff5a72019-09-11 13:49:33 +02008777 return LY_SUCCESS;
8778}
8779
8780LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008781lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008782 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
Michal Vasko400e9672021-01-11 13:39:17 +01008783 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008784{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008785 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008786 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008787
Michal Vasko400e9672021-01-11 13:39:17 +01008788 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008789 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008790 LOGARG(NULL, "Current module must be set if schema format is used.");
8791 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008792 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008793
8794 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008795 memset(set, 0, sizeof *set);
8796 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008797 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8798 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 +01008799 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008800
Michal Vasko400e9672021-01-11 13:39:17 +01008801 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008802 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008803 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008804 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8805 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008806 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008807 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008808 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008809
Radek Krejciddace2c2021-01-08 11:30:56 +01008810 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008811
Michal Vasko03ff5a72019-09-11 13:49:33 +02008812 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01008813 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
8814
Radek Krejciddace2c2021-01-08 11:30:56 +01008815 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008816 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008817}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008818
8819API const char *
8820lyxp_get_expr(const struct lyxp_expr *path)
8821{
8822 if (!path) {
8823 return NULL;
8824 }
8825
8826 return path->expr;
8827}