blob: a5651fc0c7acbf354e09d5152f8e37c63b3113d3 [file] [log] [blame]
Radek Krejcib1646a92018-11-02 16:08:26 +01001/**
2 * @file xpath.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG XPath evaluation functions
5 *
Michal Vasko61ac2f62020-05-25 12:39:51 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcib1646a92018-11-02 16:08:26 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Christian Hopps32874e12021-05-01 09:43:54 -040014#define _GNU_SOURCE /* asprintf, strdup */
Radek Krejcib1646a92018-11-02 16:08:26 +010015
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010017
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010019#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010023#include <stdio.h>
24#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010025#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010026
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020028#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010032#include "out.h"
Radek Krejci7931b192020-06-25 17:05:03 +020033#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020034#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020035#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020037#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "tree.h"
Radek Krejci77114102021-03-10 15:21:57 +010039#include "tree_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020040#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010041#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020042#include "tree_schema_internal.h"
43#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020044
aPiecekbf968d92021-05-27 14:35:05 +020045static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth);
Michal Vasko40308e72020-10-20 16:38:40 +020046static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype,
47 struct lyxp_set *set, uint32_t options);
Michal Vasko93923692021-05-07 15:28:02 +020048static LY_ERR moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
49 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod);
Michal Vasko03ff5a72019-09-11 13:49:33 +020050
51/**
52 * @brief Print the type of an XPath \p set.
53 *
54 * @param[in] set Set to use.
55 * @return Set type string.
56 */
57static const char *
58print_set_type(struct lyxp_set *set)
59{
60 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020061 case LYXP_SET_NODE_SET:
62 return "node set";
63 case LYXP_SET_SCNODE_SET:
64 return "schema node set";
65 case LYXP_SET_BOOLEAN:
66 return "boolean";
67 case LYXP_SET_NUMBER:
68 return "number";
69 case LYXP_SET_STRING:
70 return "string";
71 }
72
73 return NULL;
74}
75
Michal Vasko24cddf82020-06-01 08:17:01 +020076const char *
77lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020078{
79 switch (tok) {
80 case LYXP_TOKEN_PAR1:
81 return "(";
82 case LYXP_TOKEN_PAR2:
83 return ")";
84 case LYXP_TOKEN_BRACK1:
85 return "[";
86 case LYXP_TOKEN_BRACK2:
87 return "]";
88 case LYXP_TOKEN_DOT:
89 return ".";
90 case LYXP_TOKEN_DDOT:
91 return "..";
92 case LYXP_TOKEN_AT:
93 return "@";
94 case LYXP_TOKEN_COMMA:
95 return ",";
96 case LYXP_TOKEN_NAMETEST:
97 return "NameTest";
98 case LYXP_TOKEN_NODETYPE:
99 return "NodeType";
100 case LYXP_TOKEN_FUNCNAME:
101 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200102 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200103 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200104 case LYXP_TOKEN_OPER_EQUAL:
105 return "Operator(Equal)";
106 case LYXP_TOKEN_OPER_NEQUAL:
107 return "Operator(Non-equal)";
108 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200109 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200110 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200111 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200112 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200113 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200114 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200115 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200116 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200117 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200118 case LYXP_TOKEN_LITERAL:
119 return "Literal";
120 case LYXP_TOKEN_NUMBER:
121 return "Number";
122 default:
123 LOGINT(NULL);
124 return "";
125 }
126}
127
128/**
129 * @brief Print the whole expression \p exp to debug output.
130 *
131 * @param[in] exp Expression to use.
132 */
133static void
Michal Vasko40308e72020-10-20 16:38:40 +0200134print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200135{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100136#define MSG_BUFFER_SIZE 128
137 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100138 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200139
Radek Krejci52b6d512020-10-12 12:33:17 +0200140 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200141 return;
142 }
143
144 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
145 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200146 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200147 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100148 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200149 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
150 for (j = 1; exp->repeat[i][j]; ++j) {
151 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
152 }
153 strcat(tmp, ")");
154 }
155 LOGDBG(LY_LDGXPATH, tmp);
156 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100157#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200158}
159
160#ifndef NDEBUG
161
162/**
163 * @brief Print XPath set content to debug output.
164 *
165 * @param[in] set Set to print.
166 */
167static void
168print_set_debug(struct lyxp_set *set)
169{
170 uint32_t i;
171 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200172 struct lyxp_set_node *item;
173 struct lyxp_set_scnode *sitem;
174
Radek Krejci52b6d512020-10-12 12:33:17 +0200175 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200176 return;
177 }
178
179 switch (set->type) {
180 case LYXP_SET_NODE_SET:
181 LOGDBG(LY_LDGXPATH, "set NODE SET:");
182 for (i = 0; i < set->used; ++i) {
183 item = &set->val.nodes[i];
184
185 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100186 case LYXP_NODE_NONE:
187 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
188 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200189 case LYXP_NODE_ROOT:
190 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
191 break;
192 case LYXP_NODE_ROOT_CONFIG:
193 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
194 break;
195 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100196 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200197 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200198 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100199 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200200 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200201 item->node->schema->name, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200202 } else {
203 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
204 }
205 break;
206 case LYXP_NODE_TEXT:
207 if (item->node->schema->nodetype & LYS_ANYDATA) {
208 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200209 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200210 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200211 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200212 }
213 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100214 case LYXP_NODE_META:
215 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
Michal Vasko69730152020-10-09 16:30:07 +0200216 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200217 break;
218 }
219 }
220 break;
221
222 case LYXP_SET_SCNODE_SET:
223 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
224 for (i = 0; i < set->used; ++i) {
225 sitem = &set->val.scnodes[i];
226
227 switch (sitem->type) {
228 case LYXP_NODE_ROOT:
229 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
230 break;
231 case LYXP_NODE_ROOT_CONFIG:
232 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
233 break;
234 case LYXP_NODE_ELEM:
235 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
236 break;
237 default:
238 LOGINT(NULL);
239 break;
240 }
241 }
242 break;
243
Michal Vasko03ff5a72019-09-11 13:49:33 +0200244 case LYXP_SET_BOOLEAN:
245 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200246 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200247 break;
248
249 case LYXP_SET_STRING:
250 LOGDBG(LY_LDGXPATH, "set STRING");
251 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
252 break;
253
254 case LYXP_SET_NUMBER:
255 LOGDBG(LY_LDGXPATH, "set NUMBER");
256
257 if (isnan(set->val.num)) {
258 str = strdup("NaN");
259 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
260 str = strdup("0");
261 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
262 str = strdup("Infinity");
263 } else if (isinf(set->val.num) && signbit(set->val.num)) {
264 str = strdup("-Infinity");
265 } else if ((long long)set->val.num == set->val.num) {
266 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
267 str = NULL;
268 }
269 } else {
270 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
271 str = NULL;
272 }
273 }
274 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
275
276 LOGDBG(LY_LDGXPATH, "\t%s", str);
277 free(str);
278 }
279}
280
281#endif
282
283/**
284 * @brief Realloc the string \p str.
285 *
286 * @param[in] ctx libyang context for logging.
287 * @param[in] needed How much free space is required.
288 * @param[in,out] str Pointer to the string to use.
289 * @param[in,out] used Used bytes in \p str.
290 * @param[in,out] size Allocated bytes in \p str.
291 * @return LY_ERR
292 */
293static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100294cast_string_realloc(const struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200295{
296 if (*size - *used < needed) {
297 do {
298 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
299 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
300 return LY_EINVAL;
301 }
302 *size += LYXP_STRING_CAST_SIZE_STEP;
303 } while (*size - *used < needed);
304 *str = ly_realloc(*str, *size * sizeof(char));
305 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
306 }
307
308 return LY_SUCCESS;
309}
310
311/**
312 * @brief Cast nodes recursively to one string @p str.
313 *
314 * @param[in] node Node to cast.
315 * @param[in] fake_cont Whether to put the data into a "fake" container.
316 * @param[in] root_type Type of the XPath root.
317 * @param[in] indent Current indent.
318 * @param[in,out] str Resulting string.
319 * @param[in,out] used Used bytes in @p str.
320 * @param[in,out] size Allocated bytes in @p str.
321 * @return LY_ERR
322 */
323static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200324cast_string_recursive(const struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
Radek Krejci0f969882020-08-21 16:56:47 +0200325 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200326{
Radek Krejci7f769d72020-07-11 23:13:56 +0200327 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200328 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200329 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200330 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200331 struct lyd_node_any *any;
332 LY_ERR rc;
333
334 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
335 return LY_SUCCESS;
336 }
337
338 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200339 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200340 LY_CHECK_RET(rc);
341 strcpy(*str + (*used - 1), "\n");
342 ++(*used);
343
344 ++indent;
345 }
346
347 switch (node->schema->nodetype) {
348 case LYS_CONTAINER:
349 case LYS_LIST:
350 case LYS_RPC:
351 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200352 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200353 LY_CHECK_RET(rc);
354 strcpy(*str + (*used - 1), "\n");
355 ++(*used);
356
Radek Krejcia1c1e542020-09-29 16:06:52 +0200357 for (child = lyd_child(node); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200358 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
359 LY_CHECK_RET(rc);
360 }
361
362 break;
363
364 case LYS_LEAF:
365 case LYS_LEAFLIST:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200366 value_str = lyd_get_value(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200367
368 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200369 LY_CHECK_RET(cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200370 memset(*str + (*used - 1), ' ', indent * 2);
371 *used += indent * 2;
372
373 /* print value */
374 if (*used == 1) {
375 sprintf(*str + (*used - 1), "%s", value_str);
376 *used += strlen(value_str);
377 } else {
378 sprintf(*str + (*used - 1), "%s\n", value_str);
379 *used += strlen(value_str) + 1;
380 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200381
382 break;
383
384 case LYS_ANYXML:
385 case LYS_ANYDATA:
386 any = (struct lyd_node_any *)node;
387 if (!(void *)any->value.tree) {
388 /* no content */
389 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200390 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200391 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200392 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100393
Michal Vasko60ea6352020-06-29 13:39:39 +0200394 if (any->value_type == LYD_ANYDATA_LYB) {
395 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200396 if (lyd_parse_data_mem((struct ly_ctx *)LYD_CTX(node), any->value.mem, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200397 /* successfully parsed */
398 free(any->value.mem);
399 any->value.tree = tree;
400 any->value_type = LYD_ANYDATA_DATATREE;
401 }
Radek Krejci7931b192020-06-25 17:05:03 +0200402 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200403 }
404
Michal Vasko03ff5a72019-09-11 13:49:33 +0200405 switch (any->value_type) {
406 case LYD_ANYDATA_STRING:
407 case LYD_ANYDATA_XML:
408 case LYD_ANYDATA_JSON:
409 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200410 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200411 break;
412 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200413 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200414 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200415 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100416 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200417 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200418 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200419 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200420 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200421 }
422 }
423
424 line = strtok_r(buf, "\n", &ptr);
425 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200426 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200427 if (rc != LY_SUCCESS) {
428 free(buf);
429 return rc;
430 }
431 memset(*str + (*used - 1), ' ', indent * 2);
432 *used += indent * 2;
433
434 strcpy(*str + (*used - 1), line);
435 *used += strlen(line);
436
437 strcpy(*str + (*used - 1), "\n");
438 *used += 1;
439 } while ((line = strtok_r(NULL, "\n", &ptr)));
440
441 free(buf);
442 break;
443
444 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200445 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200446 }
447
448 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200449 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200450 LY_CHECK_RET(rc);
451 strcpy(*str + (*used - 1), "\n");
452 ++(*used);
453
454 --indent;
455 }
456
457 return LY_SUCCESS;
458}
459
460/**
461 * @brief Cast an element into a string.
462 *
463 * @param[in] node Node to cast.
464 * @param[in] fake_cont Whether to put the data into a "fake" container.
465 * @param[in] root_type Type of the XPath root.
466 * @param[out] str Element cast to dynamically-allocated string.
467 * @return LY_ERR
468 */
469static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200470cast_string_elem(struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200471{
472 uint16_t used, size;
473 LY_ERR rc;
474
475 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200476 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200477 (*str)[0] = '\0';
478 used = 1;
479 size = LYXP_STRING_CAST_SIZE_START;
480
481 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
482 if (rc != LY_SUCCESS) {
483 free(*str);
484 return rc;
485 }
486
487 if (size > used) {
488 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200489 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200490 }
491 return LY_SUCCESS;
492}
493
494/**
495 * @brief Cast a LYXP_SET_NODE_SET set into a string.
496 * Context position aware.
497 *
498 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200499 * @param[out] str Cast dynamically-allocated string.
500 * @return LY_ERR
501 */
502static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100503cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200504{
Michal Vaskoaa677062021-03-09 13:52:53 +0100505 if (!set->used) {
506 *str = strdup("");
507 if (!*str) {
508 LOGMEM_RET(set->ctx);
509 }
510 return LY_SUCCESS;
511 }
512
Michal Vasko03ff5a72019-09-11 13:49:33 +0200513 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100514 case LYXP_NODE_NONE:
515 /* invalid */
516 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200517 case LYXP_NODE_ROOT:
518 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100519 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200520 case LYXP_NODE_ELEM:
521 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100522 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100523 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200524 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200525 if (!*str) {
526 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200527 }
528 return LY_SUCCESS;
529 }
530
531 LOGINT_RET(set->ctx);
532}
533
534/**
535 * @brief Cast a string into an XPath number.
536 *
537 * @param[in] str String to use.
538 * @return Cast number.
539 */
540static long double
541cast_string_to_number(const char *str)
542{
543 long double num;
544 char *ptr;
545
546 errno = 0;
547 num = strtold(str, &ptr);
548 if (errno || *ptr) {
549 num = NAN;
550 }
551 return num;
552}
553
554/**
555 * @brief Callback for checking value equality.
556 *
Michal Vasko62524a92021-02-26 10:08:50 +0100557 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200558 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200559 * @param[in] val1_p First value.
560 * @param[in] val2_p Second value.
561 * @param[in] mod Whether hash table is being modified.
562 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200563 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200564 */
Radek Krejci857189e2020-09-01 13:26:36 +0200565static ly_bool
566set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200567{
568 struct lyxp_set_hash_node *val1, *val2;
569
570 val1 = (struct lyxp_set_hash_node *)val1_p;
571 val2 = (struct lyxp_set_hash_node *)val2_p;
572
573 if ((val1->node == val2->node) && (val1->type == val2->type)) {
574 return 1;
575 }
576
577 return 0;
578}
579
580/**
581 * @brief Insert node and its hash into set.
582 *
583 * @param[in] set et to insert to.
584 * @param[in] node Node with hash.
585 * @param[in] type Node type.
586 */
587static void
588set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
589{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200590 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200591 uint32_t i, hash;
592 struct lyxp_set_hash_node hnode;
593
594 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
595 /* create hash table and add all the nodes */
596 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
597 for (i = 0; i < set->used; ++i) {
598 hnode.node = set->val.nodes[i].node;
599 hnode.type = set->val.nodes[i].type;
600
601 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
602 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
603 hash = dict_hash_multi(hash, NULL, 0);
604
605 r = lyht_insert(set->ht, &hnode, hash, NULL);
606 assert(!r);
607 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200608
Michal Vasko9d6befd2019-12-11 14:56:56 +0100609 if (hnode.node == node) {
610 /* it was just added, do not add it twice */
611 node = NULL;
612 }
613 }
614 }
615
616 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200617 /* add the new node into hash table */
618 hnode.node = node;
619 hnode.type = type;
620
621 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
622 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
623 hash = dict_hash_multi(hash, NULL, 0);
624
625 r = lyht_insert(set->ht, &hnode, hash, NULL);
626 assert(!r);
627 (void)r;
628 }
629}
630
631/**
632 * @brief Remove node and its hash from set.
633 *
634 * @param[in] set Set to remove from.
635 * @param[in] node Node to remove.
636 * @param[in] type Node type.
637 */
638static void
639set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
640{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200641 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200642 struct lyxp_set_hash_node hnode;
643 uint32_t hash;
644
645 if (set->ht) {
646 hnode.node = node;
647 hnode.type = type;
648
649 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
650 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
651 hash = dict_hash_multi(hash, NULL, 0);
652
653 r = lyht_remove(set->ht, &hnode, hash);
654 assert(!r);
655 (void)r;
656
657 if (!set->ht->used) {
658 lyht_free(set->ht);
659 set->ht = NULL;
660 }
661 }
662}
663
664/**
665 * @brief Check whether node is in set based on its hash.
666 *
667 * @param[in] set Set to search in.
668 * @param[in] node Node to search for.
669 * @param[in] type Node type.
670 * @param[in] skip_idx Index in @p set to skip.
671 * @return LY_ERR
672 */
673static LY_ERR
674set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
675{
676 struct lyxp_set_hash_node hnode, *match_p;
677 uint32_t hash;
678
679 hnode.node = node;
680 hnode.type = type;
681
682 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
683 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
684 hash = dict_hash_multi(hash, NULL, 0);
685
686 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
687 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
688 /* we found it on the index that should be skipped, find another */
689 hnode = *match_p;
690 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
691 /* none other found */
692 return LY_SUCCESS;
693 }
694 }
695
696 return LY_EEXIST;
697 }
698
699 /* not found */
700 return LY_SUCCESS;
701}
702
Michal Vaskod3678892020-05-21 10:06:58 +0200703void
704lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200705{
706 if (!set) {
707 return;
708 }
709
710 if (set->type == LYXP_SET_NODE_SET) {
711 free(set->val.nodes);
712 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200713 } else if (set->type == LYXP_SET_SCNODE_SET) {
714 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200715 lyht_free(set->ht);
716 } else {
717 if (set->type == LYXP_SET_STRING) {
718 free(set->val.str);
719 }
720 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200721 }
Michal Vaskod3678892020-05-21 10:06:58 +0200722
723 set->val.nodes = NULL;
724 set->used = 0;
725 set->size = 0;
726 set->ht = NULL;
727 set->ctx_pos = 0;
aPiecek748da732021-06-01 09:56:43 +0200728 set->ctx_size = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200729}
730
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100731/**
732 * @brief Free dynamically-allocated set.
733 *
734 * @param[in] set Set to free.
735 */
736static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200737lyxp_set_free(struct lyxp_set *set)
738{
739 if (!set) {
740 return;
741 }
742
Michal Vaskod3678892020-05-21 10:06:58 +0200743 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200744 free(set);
745}
746
747/**
748 * @brief Initialize set context.
749 *
750 * @param[in] new Set to initialize.
751 * @param[in] set Arbitrary initialized set.
752 */
753static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200754set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200755{
756 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200757 if (set) {
758 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200759 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100760 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200761 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100762 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200763 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200764 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200765 new->prefix_data = set->prefix_data;
Michal Vasko02a77382019-09-12 11:47:35 +0200766 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200767}
768
769/**
770 * @brief Create a deep copy of a set.
771 *
772 * @param[in] set Set to copy.
773 * @return Copy of @p set.
774 */
775static struct lyxp_set *
776set_copy(struct lyxp_set *set)
777{
778 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100779 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200780
781 if (!set) {
782 return NULL;
783 }
784
785 ret = malloc(sizeof *ret);
786 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
787 set_init(ret, set);
788
789 if (set->type == LYXP_SET_SCNODE_SET) {
790 ret->type = set->type;
791
792 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100793 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
794 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200795 uint32_t idx;
796 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
797 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100798 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200799 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200800 lyxp_set_free(ret);
801 return NULL;
802 }
Michal Vaskoba716542019-12-16 10:01:58 +0100803 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200804 }
805 }
806 } else if (set->type == LYXP_SET_NODE_SET) {
807 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200808 if (set->used) {
809 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
810 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
811 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
812 } else {
813 ret->val.nodes = NULL;
814 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200815
816 ret->used = ret->size = set->used;
817 ret->ctx_pos = set->ctx_pos;
818 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100819 if (set->ht) {
820 ret->ht = lyht_dup(set->ht);
821 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200822 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200823 memcpy(ret, set, sizeof *ret);
824 if (set->type == LYXP_SET_STRING) {
825 ret->val.str = strdup(set->val.str);
826 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
827 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200828 }
829
830 return ret;
831}
832
833/**
834 * @brief Fill XPath set with a string. Any current data are disposed of.
835 *
836 * @param[in] set Set to fill.
837 * @param[in] string String to fill into \p set.
838 * @param[in] str_len Length of \p string. 0 is a valid value!
839 */
840static void
841set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
842{
Michal Vaskod3678892020-05-21 10:06:58 +0200843 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200844
845 set->type = LYXP_SET_STRING;
846 if ((str_len == 0) && (string[0] != '\0')) {
847 string = "";
848 }
849 set->val.str = strndup(string, str_len);
850}
851
852/**
853 * @brief Fill XPath set with a number. Any current data are disposed of.
854 *
855 * @param[in] set Set to fill.
856 * @param[in] number Number to fill into \p set.
857 */
858static void
859set_fill_number(struct lyxp_set *set, long double number)
860{
Michal Vaskod3678892020-05-21 10:06:58 +0200861 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200862
863 set->type = LYXP_SET_NUMBER;
864 set->val.num = number;
865}
866
867/**
868 * @brief Fill XPath set with a boolean. Any current data are disposed of.
869 *
870 * @param[in] set Set to fill.
871 * @param[in] boolean Boolean to fill into \p set.
872 */
873static void
Radek Krejci857189e2020-09-01 13:26:36 +0200874set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200875{
Michal Vaskod3678892020-05-21 10:06:58 +0200876 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200877
878 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200879 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200880}
881
882/**
883 * @brief Fill XPath set with the value from another set (deep assign).
884 * Any current data are disposed of.
885 *
886 * @param[in] trg Set to fill.
887 * @param[in] src Source set to copy into \p trg.
888 */
889static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200890set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200891{
892 if (!trg || !src) {
893 return;
894 }
895
896 if (trg->type == LYXP_SET_NODE_SET) {
897 free(trg->val.nodes);
898 } else if (trg->type == LYXP_SET_STRING) {
899 free(trg->val.str);
900 }
901 set_init(trg, src);
902
903 if (src->type == LYXP_SET_SCNODE_SET) {
904 trg->type = LYXP_SET_SCNODE_SET;
905 trg->used = src->used;
906 trg->size = src->used;
907
Michal Vasko08e9b112021-06-11 15:41:17 +0200908 if (trg->size) {
909 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
910 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
911 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
912 } else {
913 trg->val.scnodes = NULL;
914 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200915 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200916 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200917 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200918 set_fill_number(trg, src->val.num);
919 } else if (src->type == LYXP_SET_STRING) {
920 set_fill_string(trg, src->val.str, strlen(src->val.str));
921 } else {
922 if (trg->type == LYXP_SET_NODE_SET) {
923 free(trg->val.nodes);
924 } else if (trg->type == LYXP_SET_STRING) {
925 free(trg->val.str);
926 }
927
Michal Vaskod3678892020-05-21 10:06:58 +0200928 assert(src->type == LYXP_SET_NODE_SET);
929
930 trg->type = LYXP_SET_NODE_SET;
931 trg->used = src->used;
932 trg->size = src->used;
933 trg->ctx_pos = src->ctx_pos;
934 trg->ctx_size = src->ctx_size;
935
Michal Vasko08e9b112021-06-11 15:41:17 +0200936 if (trg->size) {
937 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
938 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
939 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
940 } else {
941 trg->val.nodes = NULL;
942 }
Michal Vaskod3678892020-05-21 10:06:58 +0200943 if (src->ht) {
944 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200945 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200946 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200947 }
948 }
949}
950
951/**
952 * @brief Clear context of all schema nodes.
953 *
954 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +0200955 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200956 */
957static void
Michal Vasko1a09b212021-05-06 13:00:10 +0200958set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200959{
960 uint32_t i;
961
962 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100963 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +0200964 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100965 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
966 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200967 }
968 }
969}
970
971/**
972 * @brief Remove a node from a set. Removing last node changes
973 * set into LYXP_SET_EMPTY. Context position aware.
974 *
975 * @param[in] set Set to use.
976 * @param[in] idx Index from @p set of the node to be removed.
977 */
978static void
979set_remove_node(struct lyxp_set *set, uint32_t idx)
980{
981 assert(set && (set->type == LYXP_SET_NODE_SET));
982 assert(idx < set->used);
983
984 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
985
986 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +0200987 if (idx < set->used) {
988 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
989 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +0200990 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200991 }
992}
993
994/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100995 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200996 *
997 * @param[in] set Set to use.
998 * @param[in] idx Index from @p set of the node to be removed.
999 */
1000static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001001set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001002{
1003 assert(set && (set->type == LYXP_SET_NODE_SET));
1004 assert(idx < set->used);
1005
Michal Vasko2caefc12019-11-14 16:07:56 +01001006 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1007 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1008 }
1009 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001010}
1011
1012/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001013 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001014 * set into LYXP_SET_EMPTY. Context position aware.
1015 *
1016 * @param[in] set Set to consolidate.
1017 */
1018static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001019set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001020{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001021 uint32_t i, orig_used, end = 0;
1022 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001023
Michal Vaskod3678892020-05-21 10:06:58 +02001024 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001025
1026 orig_used = set->used;
1027 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001028 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001029 start = -1;
1030 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001031 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001032 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001033 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001034 end = i;
1035 ++i;
1036 break;
1037 }
1038
1039 ++i;
1040 if (i == orig_used) {
1041 end = i;
1042 }
1043 } while (i < orig_used);
1044
1045 if (start > -1) {
1046 /* move the whole chunk of valid nodes together */
1047 if (set->used != (unsigned)start) {
1048 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1049 }
1050 set->used += end - start;
1051 }
1052 }
Michal Vasko57eab132019-09-24 11:46:26 +02001053}
1054
1055/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001056 * @brief Check for duplicates in a node set.
1057 *
1058 * @param[in] set Set to check.
1059 * @param[in] node Node to look for in @p set.
1060 * @param[in] node_type Type of @p node.
1061 * @param[in] skip_idx Index from @p set to skip.
1062 * @return LY_ERR
1063 */
1064static LY_ERR
1065set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1066{
1067 uint32_t i;
1068
Michal Vasko2caefc12019-11-14 16:07:56 +01001069 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001070 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1071 }
1072
1073 for (i = 0; i < set->used; ++i) {
1074 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1075 continue;
1076 }
1077
1078 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1079 return LY_EEXIST;
1080 }
1081 }
1082
1083 return LY_SUCCESS;
1084}
1085
Radek Krejci857189e2020-09-01 13:26:36 +02001086ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001087lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1088 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001089{
1090 uint32_t i;
1091
1092 for (i = 0; i < set->used; ++i) {
1093 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1094 continue;
1095 }
1096
1097 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001098 if (index_p) {
1099 *index_p = i;
1100 }
1101 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001102 }
1103 }
1104
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001105 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001106}
1107
Michal Vaskoecd62de2019-11-13 12:35:11 +01001108void
1109lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001110{
1111 uint32_t orig_used, i, j;
1112
Michal Vaskod3678892020-05-21 10:06:58 +02001113 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001114
Michal Vaskod3678892020-05-21 10:06:58 +02001115 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001116 return;
1117 }
1118
Michal Vaskod3678892020-05-21 10:06:58 +02001119 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001120 memcpy(set1, set2, sizeof *set1);
1121 return;
1122 }
1123
1124 if (set1->used + set2->used > set1->size) {
1125 set1->size = set1->used + set2->used;
1126 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1127 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1128 }
1129
1130 orig_used = set1->used;
1131
1132 for (i = 0; i < set2->used; ++i) {
1133 for (j = 0; j < orig_used; ++j) {
1134 /* detect duplicities */
1135 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1136 break;
1137 }
1138 }
1139
Michal Vasko0587bce2020-12-10 12:19:21 +01001140 if (j < orig_used) {
1141 /* node is there, but update its status if needed */
1142 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1143 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001144 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1145 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1146 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001147 }
1148 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001149 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1150 ++set1->used;
1151 }
1152 }
1153
Michal Vaskod3678892020-05-21 10:06:58 +02001154 lyxp_set_free_content(set2);
1155 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001156}
1157
1158/**
1159 * @brief Insert a node into a set. Context position aware.
1160 *
1161 * @param[in] set Set to use.
1162 * @param[in] node Node to insert to @p set.
1163 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1164 * @param[in] node_type Node type of @p node.
1165 * @param[in] idx Index in @p set to insert into.
1166 */
1167static void
1168set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1169{
Michal Vaskod3678892020-05-21 10:06:58 +02001170 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001171
Michal Vaskod3678892020-05-21 10:06:58 +02001172 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001173 /* first item */
1174 if (idx) {
1175 /* no real harm done, but it is a bug */
1176 LOGINT(set->ctx);
1177 idx = 0;
1178 }
1179 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1180 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1181 set->type = LYXP_SET_NODE_SET;
1182 set->used = 0;
1183 set->size = LYXP_SET_SIZE_START;
1184 set->ctx_pos = 1;
1185 set->ctx_size = 1;
1186 set->ht = NULL;
1187 } else {
1188 /* not an empty set */
1189 if (set->used == set->size) {
1190
1191 /* set is full */
1192 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1193 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1194 set->size += LYXP_SET_SIZE_STEP;
1195 }
1196
1197 if (idx > set->used) {
1198 LOGINT(set->ctx);
1199 idx = set->used;
1200 }
1201
1202 /* make space for the new node */
1203 if (idx < set->used) {
1204 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1205 }
1206 }
1207
1208 /* finally assign the value */
1209 set->val.nodes[idx].node = (struct lyd_node *)node;
1210 set->val.nodes[idx].type = node_type;
1211 set->val.nodes[idx].pos = pos;
1212 ++set->used;
1213
Michal Vasko2caefc12019-11-14 16:07:56 +01001214 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1215 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1216 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001217}
1218
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001219LY_ERR
1220lyxp_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 +02001221{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001222 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001223
1224 assert(set->type == LYXP_SET_SCNODE_SET);
1225
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001226 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
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 } else {
1229 if (set->used == set->size) {
1230 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 +02001231 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001232 set->size += LYXP_SET_SIZE_STEP;
1233 }
1234
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001235 index = set->used;
1236 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1237 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001238 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001239 ++set->used;
1240 }
1241
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001242 if (index_p) {
1243 *index_p = index;
1244 }
1245
1246 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001247}
1248
1249/**
1250 * @brief Replace a node in a set with another. Context position aware.
1251 *
1252 * @param[in] set Set to use.
1253 * @param[in] node Node to insert to @p set.
1254 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1255 * @param[in] node_type Node type of @p node.
1256 * @param[in] idx Index in @p set of the node to replace.
1257 */
1258static void
1259set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1260{
1261 assert(set && (idx < set->used));
1262
Michal Vasko2caefc12019-11-14 16:07:56 +01001263 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1264 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1265 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001266 set->val.nodes[idx].node = (struct lyd_node *)node;
1267 set->val.nodes[idx].type = node_type;
1268 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001269 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1270 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1271 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001272}
1273
1274/**
1275 * @brief Set all nodes with ctx 1 to a new unique context value.
1276 *
1277 * @param[in] set Set to modify.
1278 * @return New context value.
1279 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001280static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001281set_scnode_new_in_ctx(struct lyxp_set *set)
1282{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001283 uint32_t i;
1284 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001285
1286 assert(set->type == LYXP_SET_SCNODE_SET);
1287
Radek Krejcif13b87b2020-12-01 22:02:17 +01001288 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001289retry:
1290 for (i = 0; i < set->used; ++i) {
1291 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1292 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1293 goto retry;
1294 }
1295 }
1296 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001297 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001298 set->val.scnodes[i].in_ctx = ret_ctx;
1299 }
1300 }
1301
1302 return ret_ctx;
1303}
1304
1305/**
1306 * @brief Get unique @p node position in the data.
1307 *
1308 * @param[in] node Node to find.
1309 * @param[in] node_type Node type of @p node.
1310 * @param[in] root Root node.
1311 * @param[in] root_type Type of the XPath @p root node.
1312 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1313 * be used to increase efficiency and start the DFS from this node.
1314 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1315 * @return Node position.
1316 */
1317static uint32_t
1318get_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 +02001319 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001320{
Michal Vasko56daf732020-08-10 10:57:18 +02001321 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001322 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001323 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001324
1325 assert(prev && prev_pos && !root->prev->next);
1326
1327 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1328 return 0;
1329 }
1330
1331 if (*prev) {
1332 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001333 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001334 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001335 goto dfs_search;
1336 }
1337
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001338 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001339 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001340dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001341 if (*prev && !elem) {
1342 /* resume previous DFS */
1343 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1344 LYD_TREE_DFS_continue = 0;
1345 }
1346
Michal Vasko03ff5a72019-09-11 13:49:33 +02001347 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001348 /* skip */
1349 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001350 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001351 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001352 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001353 break;
1354 }
Michal Vasko56daf732020-08-10 10:57:18 +02001355 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001356 }
Michal Vasko56daf732020-08-10 10:57:18 +02001357
1358 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001359 }
1360
1361 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001362 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001363 break;
1364 }
1365 }
1366
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001367 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001368 if (!(*prev)) {
1369 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001370 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001371 return 0;
1372 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001373 /* start the search again from the beginning */
1374 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001375
Michal Vasko56daf732020-08-10 10:57:18 +02001376 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001377 pos = 1;
1378 goto dfs_search;
1379 }
1380 }
1381
1382 /* remember the last found node for next time */
1383 *prev = node;
1384 *prev_pos = pos;
1385
1386 return pos;
1387}
1388
1389/**
1390 * @brief Assign (fill) missing node positions.
1391 *
1392 * @param[in] set Set to fill positions in.
1393 * @param[in] root Context root node.
1394 * @param[in] root_type Context root type.
1395 * @return LY_ERR
1396 */
1397static LY_ERR
1398set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1399{
1400 const struct lyd_node *prev = NULL, *tmp_node;
1401 uint32_t i, tmp_pos = 0;
1402
1403 for (i = 0; i < set->used; ++i) {
1404 if (!set->val.nodes[i].pos) {
1405 tmp_node = NULL;
1406 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001407 case LYXP_NODE_META:
1408 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001409 if (!tmp_node) {
1410 LOGINT_RET(root->schema->module->ctx);
1411 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001412 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001413 case LYXP_NODE_ELEM:
1414 case LYXP_NODE_TEXT:
1415 if (!tmp_node) {
1416 tmp_node = set->val.nodes[i].node;
1417 }
1418 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1419 break;
1420 default:
1421 /* all roots have position 0 */
1422 break;
1423 }
1424 }
1425 }
1426
1427 return LY_SUCCESS;
1428}
1429
1430/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001431 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001432 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001433 * @param[in] meta Metadata to use.
1434 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001435 */
1436static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001437get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001438{
1439 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001440 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001441
Michal Vasko9f96a052020-03-10 09:41:45 +01001442 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001443 ++pos;
1444 }
1445
Michal Vasko9f96a052020-03-10 09:41:45 +01001446 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001447 return pos;
1448}
1449
1450/**
1451 * @brief Compare 2 nodes in respect to XPath document order.
1452 *
1453 * @param[in] item1 1st node.
1454 * @param[in] item2 2nd node.
1455 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1456 */
1457static int
1458set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1459{
Michal Vasko9f96a052020-03-10 09:41:45 +01001460 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001461
1462 if (item1->pos < item2->pos) {
1463 return -1;
1464 }
1465
1466 if (item1->pos > item2->pos) {
1467 return 1;
1468 }
1469
1470 /* node positions are equal, the fun case */
1471
1472 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1473 /* special case since text nodes are actually saved as their parents */
1474 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1475 if (item1->type == LYXP_NODE_ELEM) {
1476 assert(item2->type == LYXP_NODE_TEXT);
1477 return -1;
1478 } else {
1479 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1480 return 1;
1481 }
1482 }
1483
Michal Vasko9f96a052020-03-10 09:41:45 +01001484 /* we need meta positions now */
1485 if (item1->type == LYXP_NODE_META) {
1486 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001487 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001488 if (item2->type == LYXP_NODE_META) {
1489 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001490 }
1491
Michal Vasko9f96a052020-03-10 09:41:45 +01001492 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001493 /* check for duplicates */
1494 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001495 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001496 return 0;
1497 }
1498
Michal Vasko9f96a052020-03-10 09:41:45 +01001499 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001500 /* elem is always first, 2nd node is after it */
1501 if (item1->type == LYXP_NODE_ELEM) {
1502 assert(item2->type != LYXP_NODE_ELEM);
1503 return -1;
1504 }
1505
Michal Vasko9f96a052020-03-10 09:41:45 +01001506 /* 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 +02001507 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001508 if (((item1->type == LYXP_NODE_TEXT) &&
1509 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1510 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1511 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1512 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001513 return 1;
1514 }
1515
Michal Vasko9f96a052020-03-10 09:41:45 +01001516 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001517 /* 2nd is after 1st */
1518 return -1;
1519}
1520
1521/**
1522 * @brief Set cast for comparisons.
1523 *
1524 * @param[in] trg Target set to cast source into.
1525 * @param[in] src Source set.
1526 * @param[in] type Target set type.
1527 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001528 * @return LY_ERR
1529 */
1530static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001531set_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 +02001532{
1533 assert(src->type == LYXP_SET_NODE_SET);
1534
1535 set_init(trg, src);
1536
1537 /* insert node into target set */
1538 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1539
1540 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001541 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001542}
1543
Michal Vasko4c7763f2020-07-27 17:40:37 +02001544/**
1545 * @brief Set content canonization for comparisons.
1546 *
1547 * @param[in] trg Target set to put the canononized source into.
1548 * @param[in] src Source set.
1549 * @param[in] xp_node Source XPath node/meta to use for canonization.
1550 * @return LY_ERR
1551 */
1552static LY_ERR
1553set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1554{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001555 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001556 struct lyd_value val;
1557 struct ly_err_item *err = NULL;
1558 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001559 LY_ERR rc;
1560
1561 /* is there anything to canonize even? */
1562 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1563 /* do we have a type to use for canonization? */
1564 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1565 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1566 } else if (xp_node->type == LYXP_NODE_META) {
1567 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1568 }
1569 }
1570 if (!type) {
1571 goto fill;
1572 }
1573
1574 if (src->type == LYXP_SET_NUMBER) {
1575 /* canonize number */
1576 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1577 LOGMEM(src->ctx);
1578 return LY_EMEM;
1579 }
1580 } else {
1581 /* canonize string */
1582 str = strdup(src->val.str);
1583 }
1584
1585 /* ignore errors, the value may not satisfy schema constraints */
Radek Krejci0b013302021-03-29 15:22:32 +02001586 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 +01001587 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001588 ly_err_free(err);
1589 if (rc) {
1590 /* invalid value */
Radek IÅ¡a48460222021-03-02 15:18:32 +01001591 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001592 goto fill;
1593 }
1594
Michal Vasko4c7763f2020-07-27 17:40:37 +02001595 /* use the canonized value */
1596 set_init(trg, src);
1597 trg->type = src->type;
1598 if (src->type == LYXP_SET_NUMBER) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001599 trg->val.num = strtold(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001600 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1601 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001602 trg->val.str = strdup(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko4c7763f2020-07-27 17:40:37 +02001603 }
1604 type->plugin->free(src->ctx, &val);
1605 return LY_SUCCESS;
1606
1607fill:
1608 /* no canonization needed/possible */
1609 set_fill_set(trg, src);
1610 return LY_SUCCESS;
1611}
1612
Michal Vasko03ff5a72019-09-11 13:49:33 +02001613#ifndef NDEBUG
1614
1615/**
1616 * @brief Bubble sort @p set into XPath document order.
1617 * Context position aware. Unused in the 'Release' build target.
1618 *
1619 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001620 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1621 */
1622static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001623set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001624{
1625 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001626 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001627 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001628 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001629 struct lyxp_set_node item;
1630 struct lyxp_set_hash_node hnode;
1631 uint64_t hash;
1632
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001633 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001634 return 0;
1635 }
1636
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001637 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001638 for (root = set->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001639 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001640
1641 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001642 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001643 return -1;
1644 }
1645
1646 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1647 print_set_debug(set);
1648
1649 for (i = 0; i < set->used; ++i) {
1650 inverted = 0;
1651 change = 0;
1652
1653 for (j = 1; j < set->used - i; ++j) {
1654 /* compare node positions */
1655 if (inverted) {
1656 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1657 } else {
1658 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1659 }
1660
1661 /* swap if needed */
1662 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1663 change = 1;
1664
1665 item = set->val.nodes[j - 1];
1666 set->val.nodes[j - 1] = set->val.nodes[j];
1667 set->val.nodes[j] = item;
1668 } else {
1669 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1670 inverted = !inverted;
1671 }
1672 }
1673
1674 ++ret;
1675
1676 if (!change) {
1677 break;
1678 }
1679 }
1680
1681 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1682 print_set_debug(set);
1683
1684 /* check node hashes */
1685 if (set->used >= LYD_HT_MIN_ITEMS) {
1686 assert(set->ht);
1687 for (i = 0; i < set->used; ++i) {
1688 hnode.node = set->val.nodes[i].node;
1689 hnode.type = set->val.nodes[i].type;
1690
1691 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1692 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1693 hash = dict_hash_multi(hash, NULL, 0);
1694
1695 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1696 }
1697 }
1698
1699 return ret - 1;
1700}
1701
1702/**
1703 * @brief Remove duplicate entries in a sorted node set.
1704 *
1705 * @param[in] set Sorted set to check.
1706 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1707 */
1708static LY_ERR
1709set_sorted_dup_node_clean(struct lyxp_set *set)
1710{
1711 uint32_t i = 0;
1712 LY_ERR ret = LY_SUCCESS;
1713
1714 if (set->used > 1) {
1715 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001716 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1717 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001718 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001719 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001720 }
Michal Vasko57eab132019-09-24 11:46:26 +02001721 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001722 }
1723 }
1724
Michal Vasko2caefc12019-11-14 16:07:56 +01001725 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001726 return ret;
1727}
1728
1729#endif
1730
1731/**
1732 * @brief Merge 2 sorted sets into one.
1733 *
1734 * @param[in,out] trg Set to merge into. Duplicates are removed.
1735 * @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 +02001736 * @return LY_ERR
1737 */
1738static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001739set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001740{
1741 uint32_t i, j, k, count, dup_count;
1742 int cmp;
1743 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001744
Michal Vaskod3678892020-05-21 10:06:58 +02001745 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001746 return LY_EINVAL;
1747 }
1748
Michal Vaskod3678892020-05-21 10:06:58 +02001749 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001750 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001751 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001752 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001753 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001754 return LY_SUCCESS;
1755 }
1756
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001757 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001758 for (root = trg->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001759 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001760
1761 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001762 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001763 return LY_EINT;
1764 }
1765
1766#ifndef NDEBUG
1767 LOGDBG(LY_LDGXPATH, "MERGE target");
1768 print_set_debug(trg);
1769 LOGDBG(LY_LDGXPATH, "MERGE source");
1770 print_set_debug(src);
1771#endif
1772
1773 /* make memory for the merge (duplicates are not detected yet, so space
1774 * will likely be wasted on them, too bad) */
1775 if (trg->size - trg->used < src->used) {
1776 trg->size = trg->used + src->used;
1777
1778 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1779 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1780 }
1781
1782 i = 0;
1783 j = 0;
1784 count = 0;
1785 dup_count = 0;
1786 do {
1787 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1788 if (!cmp) {
1789 if (!count) {
1790 /* duplicate, just skip it */
1791 ++i;
1792 ++j;
1793 } else {
1794 /* we are copying something already, so let's copy the duplicate too,
1795 * we are hoping that afterwards there are some more nodes to
1796 * copy and this way we can copy them all together */
1797 ++count;
1798 ++dup_count;
1799 ++i;
1800 ++j;
1801 }
1802 } else if (cmp < 0) {
1803 /* inserting src node into trg, just remember it for now */
1804 ++count;
1805 ++i;
1806
1807 /* insert the hash now */
1808 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1809 } else if (count) {
1810copy_nodes:
1811 /* time to actually copy the nodes, we have found the largest block of nodes */
1812 memmove(&trg->val.nodes[j + (count - dup_count)],
1813 &trg->val.nodes[j],
1814 (trg->used - j) * sizeof *trg->val.nodes);
1815 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1816
1817 trg->used += count - dup_count;
1818 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1819 j += count - dup_count;
1820
1821 count = 0;
1822 dup_count = 0;
1823 } else {
1824 ++j;
1825 }
1826 } while ((i < src->used) && (j < trg->used));
1827
1828 if ((i < src->used) || count) {
1829 /* insert all the hashes first */
1830 for (k = i; k < src->used; ++k) {
1831 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1832 }
1833
1834 /* loop ended, but we need to copy something at trg end */
1835 count += src->used - i;
1836 i = src->used;
1837 goto copy_nodes;
1838 }
1839
1840 /* we are inserting hashes before the actual node insert, which causes
1841 * situations when there were initially not enough items for a hash table,
1842 * but even after some were inserted, hash table was not created (during
1843 * insertion the number of items is not updated yet) */
1844 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1845 set_insert_node_hash(trg, NULL, 0);
1846 }
1847
1848#ifndef NDEBUG
1849 LOGDBG(LY_LDGXPATH, "MERGE result");
1850 print_set_debug(trg);
1851#endif
1852
Michal Vaskod3678892020-05-21 10:06:58 +02001853 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001854 return LY_SUCCESS;
1855}
1856
1857/*
1858 * (re)parse functions
1859 *
1860 * Parse functions parse the expression into
1861 * tokens (syntactic analysis).
1862 *
1863 * Reparse functions perform semantic analysis
1864 * (do not save the result, just a check) of
1865 * the expression and fill repeat indices.
1866 */
1867
Michal Vasko14676352020-05-29 11:35:55 +02001868LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001869lyxp_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 +02001870{
Michal Vasko004d3152020-06-11 19:59:22 +02001871 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001872 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001873 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001874 }
Michal Vasko14676352020-05-29 11:35:55 +02001875 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001876 }
1877
Michal Vasko004d3152020-06-11 19:59:22 +02001878 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001879 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001880 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001881 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001882 }
Michal Vasko14676352020-05-29 11:35:55 +02001883 return LY_ENOT;
1884 }
1885
1886 return LY_SUCCESS;
1887}
1888
Michal Vasko004d3152020-06-11 19:59:22 +02001889LY_ERR
1890lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1891{
1892 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1893
1894 /* skip the token */
1895 ++(*tok_idx);
1896
1897 return LY_SUCCESS;
1898}
1899
Michal Vasko14676352020-05-29 11:35:55 +02001900/* just like lyxp_check_token() but tests for 2 tokens */
1901static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001902exp_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 +02001903 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001904{
Michal Vasko004d3152020-06-11 19:59:22 +02001905 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001906 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001907 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001908 }
1909 return LY_EINCOMPLETE;
1910 }
1911
Michal Vasko004d3152020-06-11 19:59:22 +02001912 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001913 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001914 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001915 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001916 }
1917 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001918 }
1919
1920 return LY_SUCCESS;
1921}
1922
1923/**
1924 * @brief Stack operation push on the repeat array.
1925 *
1926 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001927 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001928 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1929 */
1930static void
Michal Vasko004d3152020-06-11 19:59:22 +02001931exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001932{
1933 uint16_t i;
1934
Michal Vasko004d3152020-06-11 19:59:22 +02001935 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001936 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001937 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1938 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1939 exp->repeat[tok_idx][i] = repeat_op_idx;
1940 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001941 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001942 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1943 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1944 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001945 }
1946}
1947
1948/**
1949 * @brief Reparse Predicate. Logs directly on error.
1950 *
1951 * [7] Predicate ::= '[' Expr ']'
1952 *
1953 * @param[in] ctx Context for logging.
1954 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001955 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02001956 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001957 * @return LY_ERR
1958 */
1959static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02001960reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001961{
1962 LY_ERR rc;
1963
Michal Vasko004d3152020-06-11 19:59:22 +02001964 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001965 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001966 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001967
aPiecekbf968d92021-05-27 14:35:05 +02001968 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001969 LY_CHECK_RET(rc);
1970
Michal Vasko004d3152020-06-11 19:59:22 +02001971 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001972 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001973 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001974
1975 return LY_SUCCESS;
1976}
1977
1978/**
1979 * @brief Reparse RelativeLocationPath. Logs directly on error.
1980 *
1981 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1982 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1983 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1984 *
1985 * @param[in] ctx Context for logging.
1986 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001987 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02001988 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001989 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1990 */
1991static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02001992reparse_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 +02001993{
1994 LY_ERR rc;
1995
Michal Vasko004d3152020-06-11 19:59:22 +02001996 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001997 LY_CHECK_RET(rc);
1998
1999 goto step;
2000 do {
2001 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002002 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002003
Michal Vasko004d3152020-06-11 19:59:22 +02002004 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002005 LY_CHECK_RET(rc);
2006step:
2007 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002008 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002009 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002010 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002011 break;
2012
2013 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002014 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002015 break;
2016
2017 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002018 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002019
Michal Vasko004d3152020-06-11 19:59:22 +02002020 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002021 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002022 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002023 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 +02002024 return LY_EVALID;
2025 }
Radek Krejci0f969882020-08-21 16:56:47 +02002026 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002027 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002028 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002029 goto reparse_predicate;
2030 break;
2031
2032 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002033 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002034
2035 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002036 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002037 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002038 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002039
2040 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002041 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002042 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002043 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002044
2045reparse_predicate:
2046 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002047 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002048 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002049 LY_CHECK_RET(rc);
2050 }
2051 break;
2052 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002053 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 +02002054 return LY_EVALID;
2055 }
Michal Vasko004d3152020-06-11 19:59:22 +02002056 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002057
2058 return LY_SUCCESS;
2059}
2060
2061/**
2062 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2063 *
2064 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2065 *
2066 * @param[in] ctx Context for logging.
2067 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002068 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002069 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002070 * @return LY_ERR
2071 */
2072static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002073reparse_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 +02002074{
2075 LY_ERR rc;
2076
Michal Vasko004d3152020-06-11 19:59:22 +02002077 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 +02002078
2079 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002080 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002081 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002082 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002083
Michal Vasko004d3152020-06-11 19:59:22 +02002084 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002085 return LY_SUCCESS;
2086 }
Michal Vasko004d3152020-06-11 19:59:22 +02002087 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002088 case LYXP_TOKEN_DOT:
2089 case LYXP_TOKEN_DDOT:
2090 case LYXP_TOKEN_AT:
2091 case LYXP_TOKEN_NAMETEST:
2092 case LYXP_TOKEN_NODETYPE:
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);
Radek Krejci0f969882020-08-21 16:56:47 +02002095 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002096 default:
2097 break;
2098 }
2099
Michal Vasko03ff5a72019-09-11 13:49:33 +02002100 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002101 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002102 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002103
aPiecekbf968d92021-05-27 14:35:05 +02002104 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002105 LY_CHECK_RET(rc);
2106 }
2107
2108 return LY_SUCCESS;
2109}
2110
2111/**
2112 * @brief Reparse FunctionCall. Logs directly on error.
2113 *
2114 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2115 *
2116 * @param[in] ctx Context for logging.
2117 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002118 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002119 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002120 * @return LY_ERR
2121 */
2122static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002123reparse_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 +02002124{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002125 int8_t min_arg_count = -1;
2126 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002127 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002128 LY_ERR rc;
2129
Michal Vasko004d3152020-06-11 19:59:22 +02002130 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002131 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002132 func_tok_idx = *tok_idx;
2133 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002134 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002135 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002136 min_arg_count = 1;
2137 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002138 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002139 min_arg_count = 1;
2140 max_arg_count = 1;
2141 }
2142 break;
2143 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002144 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002145 min_arg_count = 1;
2146 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002147 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002148 min_arg_count = 0;
2149 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002150 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002151 min_arg_count = 0;
2152 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002153 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002154 min_arg_count = 0;
2155 max_arg_count = 0;
2156 }
2157 break;
2158 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002159 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002160 min_arg_count = 1;
2161 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002162 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002163 min_arg_count = 0;
2164 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002165 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002166 min_arg_count = 1;
2167 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002168 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002169 min_arg_count = 1;
2170 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002171 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002172 min_arg_count = 1;
2173 max_arg_count = 1;
2174 }
2175 break;
2176 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002177 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002178 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002179 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002180 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002181 min_arg_count = 0;
2182 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002183 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002184 min_arg_count = 0;
2185 max_arg_count = 1;
2186 }
2187 break;
2188 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002189 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002190 min_arg_count = 1;
2191 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002192 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002193 min_arg_count = 1;
2194 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002195 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002196 min_arg_count = 0;
2197 max_arg_count = 0;
2198 }
2199 break;
2200 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002201 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002202 min_arg_count = 2;
2203 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002204 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002205 min_arg_count = 0;
2206 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002207 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002208 min_arg_count = 2;
2209 max_arg_count = 2;
2210 }
2211 break;
2212 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002213 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002214 min_arg_count = 2;
2215 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002216 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002217 min_arg_count = 3;
2218 max_arg_count = 3;
2219 }
2220 break;
2221 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002222 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002223 min_arg_count = 0;
2224 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002225 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002226 min_arg_count = 1;
2227 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002228 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002229 min_arg_count = 2;
2230 max_arg_count = 2;
2231 }
2232 break;
2233 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002234 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002235 min_arg_count = 2;
2236 max_arg_count = 2;
2237 }
2238 break;
2239 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002240 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002241 min_arg_count = 2;
2242 max_arg_count = 2;
2243 }
2244 break;
2245 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002246 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002247 min_arg_count = 0;
2248 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002249 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002250 min_arg_count = 0;
2251 max_arg_count = 1;
2252 }
2253 break;
2254 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002255 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002256 min_arg_count = 0;
2257 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002258 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002259 min_arg_count = 2;
2260 max_arg_count = 2;
2261 }
2262 break;
2263 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002264 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002265 min_arg_count = 2;
2266 max_arg_count = 2;
2267 }
2268 break;
2269 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002270 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002271 min_arg_count = 2;
2272 max_arg_count = 2;
2273 }
2274 break;
2275 }
2276 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002277 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 +02002278 return LY_EINVAL;
2279 }
Michal Vasko004d3152020-06-11 19:59:22 +02002280 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002281
2282 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002283 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002284 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002285 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002286
2287 /* ( Expr ( ',' Expr )* )? */
2288 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002289 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002290 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002291 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002292 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002293 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002294 LY_CHECK_RET(rc);
2295 }
Michal Vasko004d3152020-06-11 19:59:22 +02002296 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2297 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002298
2299 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002300 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002301 LY_CHECK_RET(rc);
2302 }
2303
2304 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002305 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002306 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002307 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002308
Radek Krejci857189e2020-09-01 13:26:36 +02002309 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002310 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 +02002311 return LY_EVALID;
2312 }
2313
2314 return LY_SUCCESS;
2315}
2316
2317/**
2318 * @brief Reparse PathExpr. Logs directly on error.
2319 *
2320 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2321 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2322 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2323 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2324 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2325 *
2326 * @param[in] ctx Context for logging.
2327 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002328 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002329 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002330 * @return LY_ERR
2331 */
2332static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002333reparse_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 +02002334{
2335 LY_ERR rc;
2336
Michal Vasko004d3152020-06-11 19:59:22 +02002337 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002338 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002339 }
2340
Michal Vasko004d3152020-06-11 19:59:22 +02002341 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002342 case LYXP_TOKEN_PAR1:
2343 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002344 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002345
aPiecekbf968d92021-05-27 14:35:05 +02002346 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002347 LY_CHECK_RET(rc);
2348
Michal Vasko004d3152020-06-11 19:59:22 +02002349 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002350 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002351 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002352 goto predicate;
2353 break;
2354 case LYXP_TOKEN_DOT:
2355 case LYXP_TOKEN_DDOT:
2356 case LYXP_TOKEN_AT:
2357 case LYXP_TOKEN_NAMETEST:
2358 case LYXP_TOKEN_NODETYPE:
2359 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002360 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002361 LY_CHECK_RET(rc);
2362 break;
2363 case LYXP_TOKEN_FUNCNAME:
2364 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002365 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002366 LY_CHECK_RET(rc);
2367 goto predicate;
2368 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002369 case LYXP_TOKEN_OPER_PATH:
2370 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002371 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002372 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002373 LY_CHECK_RET(rc);
2374 break;
2375 case LYXP_TOKEN_LITERAL:
2376 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002377 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002378 goto predicate;
2379 break;
2380 case LYXP_TOKEN_NUMBER:
2381 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002382 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002383 goto predicate;
2384 break;
2385 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002386 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 +02002387 return LY_EVALID;
2388 }
2389
2390 return LY_SUCCESS;
2391
2392predicate:
2393 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002394 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002395 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002396 LY_CHECK_RET(rc);
2397 }
2398
2399 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002400 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002401
2402 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002403 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002404
aPiecekbf968d92021-05-27 14:35:05 +02002405 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002406 LY_CHECK_RET(rc);
2407 }
2408
2409 return LY_SUCCESS;
2410}
2411
2412/**
2413 * @brief Reparse UnaryExpr. Logs directly on error.
2414 *
2415 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2416 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2417 *
2418 * @param[in] ctx Context for logging.
2419 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002420 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002421 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002422 * @return LY_ERR
2423 */
2424static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002425reparse_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 +02002426{
2427 uint16_t prev_exp;
2428 LY_ERR rc;
2429
2430 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002431 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002432 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2433 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002434 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002435 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002436 }
2437
2438 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002439 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002440 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002441 LY_CHECK_RET(rc);
2442
2443 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002444 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002445 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002446 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002447
aPiecekbf968d92021-05-27 14:35:05 +02002448 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002449 LY_CHECK_RET(rc);
2450 }
2451
2452 return LY_SUCCESS;
2453}
2454
2455/**
2456 * @brief Reparse AdditiveExpr. Logs directly on error.
2457 *
2458 * [15] AdditiveExpr ::= MultiplicativeExpr
2459 * | AdditiveExpr '+' MultiplicativeExpr
2460 * | AdditiveExpr '-' MultiplicativeExpr
2461 * [16] MultiplicativeExpr ::= UnaryExpr
2462 * | MultiplicativeExpr '*' UnaryExpr
2463 * | MultiplicativeExpr 'div' UnaryExpr
2464 * | MultiplicativeExpr 'mod' UnaryExpr
2465 *
2466 * @param[in] ctx Context for logging.
2467 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002468 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002469 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002470 * @return LY_ERR
2471 */
2472static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002473reparse_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 +02002474{
2475 uint16_t prev_add_exp, prev_mul_exp;
2476 LY_ERR rc;
2477
Michal Vasko004d3152020-06-11 19:59:22 +02002478 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002479 goto reparse_multiplicative_expr;
2480
2481 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002482 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2483 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002484 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002485 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002486
2487reparse_multiplicative_expr:
2488 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002489 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002490 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002491 LY_CHECK_RET(rc);
2492
2493 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002494 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2495 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002496 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002497 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002498
aPiecekbf968d92021-05-27 14:35:05 +02002499 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002500 LY_CHECK_RET(rc);
2501 }
2502 }
2503
2504 return LY_SUCCESS;
2505}
2506
2507/**
2508 * @brief Reparse EqualityExpr. Logs directly on error.
2509 *
2510 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2511 * | EqualityExpr '!=' RelationalExpr
2512 * [14] RelationalExpr ::= AdditiveExpr
2513 * | RelationalExpr '<' AdditiveExpr
2514 * | RelationalExpr '>' AdditiveExpr
2515 * | RelationalExpr '<=' AdditiveExpr
2516 * | RelationalExpr '>=' AdditiveExpr
2517 *
2518 * @param[in] ctx Context for logging.
2519 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002520 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002521 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002522 * @return LY_ERR
2523 */
2524static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002525reparse_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 +02002526{
2527 uint16_t prev_eq_exp, prev_rel_exp;
2528 LY_ERR rc;
2529
Michal Vasko004d3152020-06-11 19:59:22 +02002530 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002531 goto reparse_additive_expr;
2532
2533 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002534 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002535 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002536 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002537
2538reparse_additive_expr:
2539 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002540 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002541 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002542 LY_CHECK_RET(rc);
2543
2544 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002545 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002546 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002547 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002548
aPiecekbf968d92021-05-27 14:35:05 +02002549 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002550 LY_CHECK_RET(rc);
2551 }
2552 }
2553
2554 return LY_SUCCESS;
2555}
2556
2557/**
2558 * @brief Reparse OrExpr. Logs directly on error.
2559 *
2560 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2561 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2562 *
2563 * @param[in] ctx Context for logging.
2564 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002565 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002566 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002567 * @return LY_ERR
2568 */
2569static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002570reparse_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 +02002571{
2572 uint16_t prev_or_exp, prev_and_exp;
2573 LY_ERR rc;
2574
aPiecekbf968d92021-05-27 14:35:05 +02002575 ++depth;
2576 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2577
Michal Vasko004d3152020-06-11 19:59:22 +02002578 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002579 goto reparse_equality_expr;
2580
2581 /* ('or' AndExpr)* */
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] == 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002583 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002584 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002585
2586reparse_equality_expr:
2587 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002588 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002589 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002590 LY_CHECK_RET(rc);
2591
2592 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002593 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 +02002594 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002595 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002596
aPiecekbf968d92021-05-27 14:35:05 +02002597 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002598 LY_CHECK_RET(rc);
2599 }
2600 }
2601
2602 return LY_SUCCESS;
2603}
Radek Krejcib1646a92018-11-02 16:08:26 +01002604
2605/**
2606 * @brief Parse NCName.
2607 *
2608 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002609 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002610 */
Radek Krejcid4270262019-01-07 15:07:25 +01002611static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002612parse_ncname(const char *ncname)
2613{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002614 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002615 size_t size;
2616 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002617
2618 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2619 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2620 return len;
2621 }
2622
2623 do {
2624 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002625 if (!*ncname) {
2626 break;
2627 }
Radek Krejcid4270262019-01-07 15:07:25 +01002628 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002629 } while (is_xmlqnamechar(uc) && (uc != ':'));
2630
2631 return len;
2632}
2633
2634/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002635 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002636 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002637 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002638 * @param[in] exp Expression to use.
2639 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002640 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002641 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002642 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002643 */
2644static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002645exp_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 +01002646{
2647 uint32_t prev;
2648
2649 if (exp->used == exp->size) {
2650 prev = exp->size;
2651 exp->size += LYXP_EXPR_SIZE_STEP;
2652 if (prev > exp->size) {
2653 LOGINT(ctx);
2654 return LY_EINT;
2655 }
2656
2657 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2658 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2659 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2660 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2661 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2662 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2663 }
2664
2665 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002666 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002667 exp->tok_len[exp->used] = tok_len;
2668 ++exp->used;
2669 return LY_SUCCESS;
2670}
2671
2672void
Michal Vasko14676352020-05-29 11:35:55 +02002673lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002674{
2675 uint16_t i;
2676
2677 if (!expr) {
2678 return;
2679 }
2680
2681 lydict_remove(ctx, expr->expr);
2682 free(expr->tokens);
2683 free(expr->tok_pos);
2684 free(expr->tok_len);
2685 if (expr->repeat) {
2686 for (i = 0; i < expr->used; ++i) {
2687 free(expr->repeat[i]);
2688 }
2689 }
2690 free(expr->repeat);
2691 free(expr);
2692}
2693
Radek Krejcif03a9e22020-09-18 20:09:31 +02002694LY_ERR
2695lyxp_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 +01002696{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002697 LY_ERR ret = LY_SUCCESS;
2698 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002699 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002700 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002701 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002702 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002703
Radek Krejcif03a9e22020-09-18 20:09:31 +02002704 assert(expr_p);
2705
2706 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002707 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002708 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002709 }
2710
2711 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002712 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002713 }
2714 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002715 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002716 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002717 }
2718
2719 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002720 expr = calloc(1, sizeof *expr);
2721 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2722 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2723 expr->used = 0;
2724 expr->size = LYXP_EXPR_SIZE_START;
2725 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2726 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002727
Radek Krejcif03a9e22020-09-18 20:09:31 +02002728 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2729 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002730
Radek Krejcif03a9e22020-09-18 20:09:31 +02002731 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2732 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002733
Michal Vasko004d3152020-06-11 19:59:22 +02002734 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002735 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002736
Radek Krejcif03a9e22020-09-18 20:09:31 +02002737 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002738 ++parsed;
2739 }
2740
2741 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002742 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002743
2744 /* '(' */
2745 tok_len = 1;
2746 tok_type = LYXP_TOKEN_PAR1;
2747
Radek Krejcif03a9e22020-09-18 20:09:31 +02002748 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002749 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002750 if (((expr->tok_len[expr->used - 1] == 4) &&
2751 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2752 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2753 ((expr->tok_len[expr->used - 1] == 7) &&
2754 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002755 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002756 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002757 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002758 }
2759 prev_function_check = 0;
2760 }
2761
Radek Krejcif03a9e22020-09-18 20:09:31 +02002762 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002763
2764 /* ')' */
2765 tok_len = 1;
2766 tok_type = LYXP_TOKEN_PAR2;
2767
Radek Krejcif03a9e22020-09-18 20:09:31 +02002768 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002769
2770 /* '[' */
2771 tok_len = 1;
2772 tok_type = LYXP_TOKEN_BRACK1;
2773
Radek Krejcif03a9e22020-09-18 20:09:31 +02002774 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002775
2776 /* ']' */
2777 tok_len = 1;
2778 tok_type = LYXP_TOKEN_BRACK2;
2779
Radek Krejcif03a9e22020-09-18 20:09:31 +02002780 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002781
2782 /* '..' */
2783 tok_len = 2;
2784 tok_type = LYXP_TOKEN_DDOT;
2785
Radek Krejcif03a9e22020-09-18 20:09:31 +02002786 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002787
2788 /* '.' */
2789 tok_len = 1;
2790 tok_type = LYXP_TOKEN_DOT;
2791
Radek Krejcif03a9e22020-09-18 20:09:31 +02002792 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002793
2794 /* '@' */
2795 tok_len = 1;
2796 tok_type = LYXP_TOKEN_AT;
2797
Radek Krejcif03a9e22020-09-18 20:09:31 +02002798 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002799
2800 /* ',' */
2801 tok_len = 1;
2802 tok_type = LYXP_TOKEN_COMMA;
2803
Radek Krejcif03a9e22020-09-18 20:09:31 +02002804 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002805
2806 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002807 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2808 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002809 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002810 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002811 ++tok_len;
2812 tok_type = LYXP_TOKEN_LITERAL;
2813
Radek Krejcif03a9e22020-09-18 20:09:31 +02002814 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002815
2816 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002817 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2818 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002819 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002820 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002821 ++tok_len;
2822 tok_type = LYXP_TOKEN_LITERAL;
2823
Radek Krejcif03a9e22020-09-18 20:09:31 +02002824 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002825
2826 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002827 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2828 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002829 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002830 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002831 }
2832 tok_type = LYXP_TOKEN_NUMBER;
2833
Radek Krejcif03a9e22020-09-18 20:09:31 +02002834 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002835
2836 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002837 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002838 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002839 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002840 } else {
2841 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002842 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002843 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002844
Radek Krejcif03a9e22020-09-18 20:09:31 +02002845 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002846
Michal Vasko3e48bf32020-06-01 08:39:07 +02002847 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002848 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002849 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2850
Radek Krejcif03a9e22020-09-18 20:09:31 +02002851 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002852
2853 /* Operator '<=', '>=' */
2854 tok_len = 2;
2855 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002856
Radek Krejcif03a9e22020-09-18 20:09:31 +02002857 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002858
2859 /* Operator '|' */
2860 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002861 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002862
Radek Krejcif03a9e22020-09-18 20:09:31 +02002863 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002864
2865 /* Operator '+', '-' */
2866 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002867 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002868
Radek Krejcif03a9e22020-09-18 20:09:31 +02002869 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002870
Michal Vasko3e48bf32020-06-01 08:39:07 +02002871 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002872 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002873 tok_type = LYXP_TOKEN_OPER_EQUAL;
2874
Radek Krejcif03a9e22020-09-18 20:09:31 +02002875 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002876
2877 /* Operator '<', '>' */
2878 tok_len = 1;
2879 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002880
Michal Vasko69730152020-10-09 16:30:07 +02002881 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2882 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2883 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2884 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2885 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2886 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2887 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2888 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2889 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2890 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2891 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2892 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002893
2894 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002895 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002896 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002897 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002898
Radek Krejcif03a9e22020-09-18 20:09:31 +02002899 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002900 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002901 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002902
Radek Krejcif03a9e22020-09-18 20:09:31 +02002903 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002904 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002905 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002906
Radek Krejcif03a9e22020-09-18 20:09:31 +02002907 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002908 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002909 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002910
2911 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002912 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 +02002913 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 +02002914 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002915 goto error;
2916 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02002917 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002918 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002919 goto error;
2920 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002921 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002922
2923 /* NameTest '*' */
2924 tok_len = 1;
2925 tok_type = LYXP_TOKEN_NAMETEST;
2926
2927 } else {
2928
2929 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002930 long int ncname_len = parse_ncname(&expr_str[parsed]);
Michal Vasko774ce402021-04-14 15:35:06 +02002931 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2932 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002933 tok_len = ncname_len;
2934
Radek Krejcif03a9e22020-09-18 20:09:31 +02002935 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002936 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002937 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002938 ++tok_len;
2939 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002940 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vasko774ce402021-04-14 15:35:06 +02002941 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2942 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002943 tok_len += ncname_len;
2944 }
2945 /* remove old flag to prevent ambiguities */
2946 prev_function_check = 0;
2947 tok_type = LYXP_TOKEN_NAMETEST;
2948 } else {
2949 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2950 prev_function_check = 1;
2951 tok_type = LYXP_TOKEN_NAMETEST;
2952 }
2953 }
2954
2955 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002956 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002957 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002958 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002959 ++parsed;
2960 }
2961
Radek Krejcif03a9e22020-09-18 20:09:31 +02002962 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002963
Michal Vasko004d3152020-06-11 19:59:22 +02002964 if (reparse) {
2965 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002966 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2967 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002968
Michal Vasko004d3152020-06-11 19:59:22 +02002969 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02002970 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002971 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002972 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002973 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002974 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002975 goto error;
2976 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002977 }
2978
Radek Krejcif03a9e22020-09-18 20:09:31 +02002979 print_expr_struct_debug(expr);
2980 *expr_p = expr;
2981 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002982
2983error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002984 lyxp_expr_free(ctx, expr);
2985 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002986}
2987
Michal Vasko1734be92020-09-22 08:55:10 +02002988LY_ERR
2989lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02002990{
Michal Vasko1734be92020-09-22 08:55:10 +02002991 LY_ERR ret = LY_SUCCESS;
2992 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02002993 uint32_t i, j;
2994
Michal Vasko7f45cf22020-10-01 12:49:44 +02002995 if (!exp) {
2996 goto cleanup;
2997 }
2998
Michal Vasko004d3152020-06-11 19:59:22 +02002999 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003000 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003001
Michal Vasko08e9b112021-06-11 15:41:17 +02003002 if (exp->used) {
3003 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
3004 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3005 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003006
Michal Vasko08e9b112021-06-11 15:41:17 +02003007 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
3008 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3009 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003010
Michal Vasko08e9b112021-06-11 15:41:17 +02003011 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
3012 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3013 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003014
Michal Vasko08e9b112021-06-11 15:41:17 +02003015 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
3016 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3017 for (i = 0; i < exp->used; ++i) {
3018 if (!exp->repeat[i]) {
3019 dup->repeat[i] = NULL;
3020 } else {
3021 for (j = 0; exp->repeat[i][j]; ++j) {}
3022 /* the ending 0 as well */
3023 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003024
Michal Vasko08e9b112021-06-11 15:41:17 +02003025 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
3026 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3027 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3028 dup->repeat[i][j - 1] = 0;
3029 }
Michal Vasko004d3152020-06-11 19:59:22 +02003030 }
3031 }
3032
3033 dup->used = exp->used;
3034 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003035 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003036
Michal Vasko1734be92020-09-22 08:55:10 +02003037cleanup:
3038 if (ret) {
3039 lyxp_expr_free(ctx, dup);
3040 } else {
3041 *dup_p = dup;
3042 }
3043 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003044}
3045
Michal Vasko03ff5a72019-09-11 13:49:33 +02003046/*
3047 * warn functions
3048 *
3049 * Warn functions check specific reasonable conditions for schema XPath
3050 * and print a warning if they are not satisfied.
3051 */
3052
3053/**
3054 * @brief Get the last-added schema node that is currently in the context.
3055 *
3056 * @param[in] set Set to search in.
3057 * @return Last-added schema context node, NULL if no node is in context.
3058 */
3059static struct lysc_node *
3060warn_get_scnode_in_ctx(struct lyxp_set *set)
3061{
3062 uint32_t i;
3063
3064 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3065 return NULL;
3066 }
3067
3068 i = set->used;
3069 do {
3070 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003071 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003072 /* if there are more, simply return the first found (last added) */
3073 return set->val.scnodes[i].scnode;
3074 }
3075 } while (i);
3076
3077 return NULL;
3078}
3079
3080/**
3081 * @brief Test whether a type is numeric - integer type or decimal64.
3082 *
3083 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003084 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003085 */
Radek Krejci857189e2020-09-01 13:26:36 +02003086static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003087warn_is_numeric_type(struct lysc_type *type)
3088{
3089 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003090 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003091 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003092
3093 switch (type->basetype) {
3094 case LY_TYPE_DEC64:
3095 case LY_TYPE_INT8:
3096 case LY_TYPE_UINT8:
3097 case LY_TYPE_INT16:
3098 case LY_TYPE_UINT16:
3099 case LY_TYPE_INT32:
3100 case LY_TYPE_UINT32:
3101 case LY_TYPE_INT64:
3102 case LY_TYPE_UINT64:
3103 return 1;
3104 case LY_TYPE_UNION:
3105 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003106 LY_ARRAY_FOR(uni->types, u) {
3107 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003108 if (ret) {
3109 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003110 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003111 }
3112 }
3113 /* did not find any suitable type */
3114 return 0;
3115 case LY_TYPE_LEAFREF:
3116 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3117 default:
3118 return 0;
3119 }
3120}
3121
3122/**
3123 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3124 *
3125 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003126 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003127 */
Radek Krejci857189e2020-09-01 13:26:36 +02003128static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003129warn_is_string_type(struct lysc_type *type)
3130{
3131 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003132 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003133 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003134
3135 switch (type->basetype) {
3136 case LY_TYPE_BITS:
3137 case LY_TYPE_ENUM:
3138 case LY_TYPE_IDENT:
3139 case LY_TYPE_INST:
3140 case LY_TYPE_STRING:
3141 return 1;
3142 case LY_TYPE_UNION:
3143 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003144 LY_ARRAY_FOR(uni->types, u) {
3145 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003146 if (ret) {
3147 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003148 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003149 }
3150 }
3151 /* did not find any suitable type */
3152 return 0;
3153 case LY_TYPE_LEAFREF:
3154 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3155 default:
3156 return 0;
3157 }
3158}
3159
3160/**
3161 * @brief Test whether a type is one specific type.
3162 *
3163 * @param[in] type Type to test.
3164 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003165 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003166 */
Radek Krejci857189e2020-09-01 13:26:36 +02003167static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003168warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3169{
3170 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003171 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003172 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003173
3174 if (type->basetype == base) {
3175 return 1;
3176 } else if (type->basetype == LY_TYPE_UNION) {
3177 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003178 LY_ARRAY_FOR(uni->types, u) {
3179 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003180 if (ret) {
3181 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003182 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003183 }
3184 }
3185 /* did not find any suitable type */
3186 return 0;
3187 } else if (type->basetype == LY_TYPE_LEAFREF) {
3188 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3189 }
3190
3191 return 0;
3192}
3193
3194/**
3195 * @brief Get next type of a (union) type.
3196 *
3197 * @param[in] type Base type.
3198 * @param[in] prev_type Previously returned type.
3199 * @return Next type or NULL.
3200 */
3201static struct lysc_type *
3202warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3203{
3204 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003205 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003206 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003207
3208 switch (type->basetype) {
3209 case LY_TYPE_UNION:
3210 uni = (struct lysc_type_union *)type;
3211 if (!prev_type) {
3212 return uni->types[0];
3213 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003214 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003215 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003216 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003217 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003218 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003219 found = 1;
3220 }
3221 }
3222 return NULL;
3223 default:
3224 if (prev_type) {
3225 assert(type == prev_type);
3226 return NULL;
3227 } else {
3228 return type;
3229 }
3230 }
3231}
3232
3233/**
3234 * @brief Test whether 2 types have a common type.
3235 *
3236 * @param[in] type1 First type.
3237 * @param[in] type2 Second type.
3238 * @return 1 if they do, 0 otherwise.
3239 */
3240static int
3241warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3242{
3243 struct lysc_type *t1, *rt1, *t2, *rt2;
3244
3245 t1 = NULL;
3246 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3247 if (t1->basetype == LY_TYPE_LEAFREF) {
3248 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3249 } else {
3250 rt1 = t1;
3251 }
3252
3253 t2 = NULL;
3254 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3255 if (t2->basetype == LY_TYPE_LEAFREF) {
3256 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3257 } else {
3258 rt2 = t2;
3259 }
3260
3261 if (rt2->basetype == rt1->basetype) {
3262 /* match found */
3263 return 1;
3264 }
3265 }
3266 }
3267
3268 return 0;
3269}
3270
3271/**
3272 * @brief Check both operands of comparison operators.
3273 *
3274 * @param[in] ctx Context for errors.
3275 * @param[in] set1 First operand set.
3276 * @param[in] set2 Second operand set.
3277 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3278 * @param[in] expr Start of the expression to print with the warning.
3279 * @param[in] tok_pos Token position.
3280 */
3281static void
Radek Krejci857189e2020-09-01 13:26:36 +02003282warn_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 +02003283{
3284 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003285 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003286
3287 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3288 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3289
3290 if (!node1 && !node2) {
3291 /* no node-sets involved, nothing to do */
3292 return;
3293 }
3294
3295 if (node1) {
3296 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3297 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3298 warning = 1;
3299 leaves = 0;
3300 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3301 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3302 warning = 1;
3303 }
3304 }
3305
3306 if (node2) {
3307 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3308 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3309 warning = 1;
3310 leaves = 0;
3311 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3312 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3313 warning = 1;
3314 }
3315 }
3316
3317 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003318 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3319 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3320 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3321 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003322 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3323 warning = 1;
3324 }
3325 }
3326
3327 if (warning) {
3328 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3329 }
3330}
3331
3332/**
3333 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3334 *
3335 * @param[in] exp Parsed XPath expression.
3336 * @param[in] set Set with the leaf/leaf-list.
3337 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3338 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3339 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3340 */
3341static void
Michal Vasko40308e72020-10-20 16:38:40 +02003342warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3343 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003344{
3345 struct lysc_node *scnode;
3346 struct lysc_type *type;
3347 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003348 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003349 LY_ERR rc;
3350 struct ly_err_item *err = NULL;
3351
Michal Vasko69730152020-10-09 16:30:07 +02003352 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3353 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003354 /* check that the node can have the specified value */
3355 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3356 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3357 } else {
3358 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3359 }
3360 if (!value) {
3361 LOGMEM(set->ctx);
3362 return;
3363 }
3364
3365 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3366 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003367 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003368 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003369 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003370 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003371 }
3372
3373 type = ((struct lysc_node_leaf *)scnode)->type;
3374 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003375 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003376 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003377 if (rc == LY_EINCOMPLETE) {
3378 rc = LY_SUCCESS;
3379 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003380
3381 if (err) {
3382 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3383 ly_err_free(err);
3384 } else if (rc != LY_SUCCESS) {
3385 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3386 }
3387 if (rc != LY_SUCCESS) {
3388 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003389 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003390 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003391 } else {
3392 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003393 }
3394 }
3395 free(value);
3396 }
3397}
3398
3399/*
3400 * XPath functions
3401 */
3402
3403/**
3404 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3405 * depending on whether the first node bit value from the second argument is set.
3406 *
3407 * @param[in] args Array of arguments.
3408 * @param[in] arg_count Count of elements in @p args.
3409 * @param[in,out] set Context and result set at the same time.
3410 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003411 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003412 */
3413static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003414xpath_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 +02003415{
3416 struct lyd_node_term *leaf;
3417 struct lysc_node_leaf *sleaf;
3418 struct lysc_type_bits *bits;
3419 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003420 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003421
3422 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003423 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003424 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003425 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3426 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3427 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3428 sleaf->name);
3429 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3430 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3431 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003432 }
3433
3434 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3435 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003436 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3437 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003438 } else if (!warn_is_string_type(sleaf->type)) {
3439 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003440 }
3441 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003442 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003443 return rc;
3444 }
3445
Michal Vaskod3678892020-05-21 10:06:58 +02003446 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003447 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 +02003448 return LY_EVALID;
3449 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003450 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003451 LY_CHECK_RET(rc);
3452
3453 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003454 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003455 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003456 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3457 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003458 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003459 LY_ARRAY_FOR(bits->bits, u) {
3460 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003461 set_fill_boolean(set, 1);
3462 break;
3463 }
3464 }
3465 }
3466 }
3467
3468 return LY_SUCCESS;
3469}
3470
3471/**
3472 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3473 * with the argument converted to boolean.
3474 *
3475 * @param[in] args Array of arguments.
3476 * @param[in] arg_count Count of elements in @p args.
3477 * @param[in,out] set Context and result set at the same time.
3478 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003479 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003480 */
3481static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003482xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003483{
3484 LY_ERR rc;
3485
3486 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003487 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003488 return LY_SUCCESS;
3489 }
3490
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003491 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003492 LY_CHECK_RET(rc);
3493 set_fill_set(set, args[0]);
3494
3495 return LY_SUCCESS;
3496}
3497
3498/**
3499 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3500 * with the first argument rounded up to the nearest integer.
3501 *
3502 * @param[in] args Array of arguments.
3503 * @param[in] arg_count Count of elements in @p args.
3504 * @param[in,out] set Context and result set at the same time.
3505 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003506 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003507 */
3508static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003509xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003510{
3511 struct lysc_node_leaf *sleaf;
3512 LY_ERR rc = LY_SUCCESS;
3513
3514 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003515 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003516 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003517 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3518 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3519 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3520 sleaf->name);
3521 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3522 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3523 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003524 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003525 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003526 return rc;
3527 }
3528
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003529 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003530 LY_CHECK_RET(rc);
3531 if ((long long)args[0]->val.num != args[0]->val.num) {
3532 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3533 } else {
3534 set_fill_number(set, args[0]->val.num);
3535 }
3536
3537 return LY_SUCCESS;
3538}
3539
3540/**
3541 * @brief Execute the XPath concat(string, string, string*) function.
3542 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3543 *
3544 * @param[in] args Array of arguments.
3545 * @param[in] arg_count Count of elements in @p args.
3546 * @param[in,out] set Context and result set at the same time.
3547 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003548 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003549 */
3550static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003551xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003552{
3553 uint16_t i;
3554 char *str = NULL;
3555 size_t used = 1;
3556 LY_ERR rc = LY_SUCCESS;
3557 struct lysc_node_leaf *sleaf;
3558
3559 if (options & LYXP_SCNODE_ALL) {
3560 for (i = 0; i < arg_count; ++i) {
3561 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3562 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3563 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003564 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003565 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003566 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 +02003567 }
3568 }
3569 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003570 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003571 return rc;
3572 }
3573
3574 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003575 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003576 if (rc != LY_SUCCESS) {
3577 free(str);
3578 return rc;
3579 }
3580
3581 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3582 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3583 strcpy(str + used - 1, args[i]->val.str);
3584 used += strlen(args[i]->val.str);
3585 }
3586
3587 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003588 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003589 set->type = LYXP_SET_STRING;
3590 set->val.str = str;
3591
3592 return LY_SUCCESS;
3593}
3594
3595/**
3596 * @brief Execute the XPath contains(string, string) function.
3597 * Returns LYXP_SET_BOOLEAN whether the second argument can
3598 * be found in the first or not.
3599 *
3600 * @param[in] args Array of arguments.
3601 * @param[in] arg_count Count of elements in @p args.
3602 * @param[in,out] set Context and result set at the same time.
3603 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003604 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003605 */
3606static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003607xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003608{
3609 struct lysc_node_leaf *sleaf;
3610 LY_ERR rc = LY_SUCCESS;
3611
3612 if (options & LYXP_SCNODE_ALL) {
3613 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3614 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003615 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3616 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003617 } else if (!warn_is_string_type(sleaf->type)) {
3618 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003619 }
3620 }
3621
3622 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3623 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003624 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3625 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003626 } else if (!warn_is_string_type(sleaf->type)) {
3627 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003628 }
3629 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003630 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003631 return rc;
3632 }
3633
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003634 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003635 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003636 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003637 LY_CHECK_RET(rc);
3638
3639 if (strstr(args[0]->val.str, args[1]->val.str)) {
3640 set_fill_boolean(set, 1);
3641 } else {
3642 set_fill_boolean(set, 0);
3643 }
3644
3645 return LY_SUCCESS;
3646}
3647
3648/**
3649 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3650 * with the size of the node-set from the argument.
3651 *
3652 * @param[in] args Array of arguments.
3653 * @param[in] arg_count Count of elements in @p args.
3654 * @param[in,out] set Context and result set at the same time.
3655 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003656 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003657 */
3658static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003659xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003660{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003661 LY_ERR rc = LY_SUCCESS;
3662
3663 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003664 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003665 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003666 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003667 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003668 return rc;
3669 }
3670
Michal Vasko03ff5a72019-09-11 13:49:33 +02003671 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003672 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003673 return LY_EVALID;
3674 }
3675
3676 set_fill_number(set, args[0]->used);
3677 return LY_SUCCESS;
3678}
3679
3680/**
3681 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3682 * with the context with the intial node.
3683 *
3684 * @param[in] args Array of arguments.
3685 * @param[in] arg_count Count of elements in @p args.
3686 * @param[in,out] set Context and result set at the same time.
3687 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003688 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003689 */
3690static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003691xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003692{
3693 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003694 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003695 return LY_EVALID;
3696 }
3697
3698 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003699 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003700
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003701 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003702 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003703 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003704
3705 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003706 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003707 }
3708
3709 return LY_SUCCESS;
3710}
3711
3712/**
3713 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3714 * leafref or instance-identifier target node(s).
3715 *
3716 * @param[in] args Array of arguments.
3717 * @param[in] arg_count Count of elements in @p args.
3718 * @param[in,out] set Context and result set at the same time.
3719 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003720 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003721 */
3722static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003723xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003724{
3725 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003726 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003727 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003728 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003729 struct ly_path *p;
3730 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003731 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003732 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003733 LY_ERR rc = LY_SUCCESS;
3734
3735 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003736 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003737 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003738 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3739 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3740 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3741 sleaf->name);
3742 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3743 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3744 __func__, sleaf->name);
3745 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003746 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003747 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko42e497c2020-01-06 08:38:25 +01003748 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003749 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003750 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003751
3752 /* it was already evaluated on schema, it must succeed */
Radek Krejcid5d37432021-03-12 13:46:40 +01003753 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 +02003754 LY_PATH_TARGET_MANY, LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, NULL, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003755 assert(!rc);
3756
3757 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003758 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003759 ly_path_free(set->ctx, p);
3760
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003761 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003762 }
3763
Michal Vasko03ff5a72019-09-11 13:49:33 +02003764 return rc;
3765 }
3766
Michal Vaskod3678892020-05-21 10:06:58 +02003767 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003768 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003769 return LY_EVALID;
3770 }
3771
Michal Vaskod3678892020-05-21 10:06:58 +02003772 lyxp_set_free_content(set);
3773 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003774 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3775 sleaf = (struct lysc_node_leaf *)leaf->schema;
3776 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3777 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3778 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02003779 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01003780 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003781 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003782 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003783 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003784 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003785 } else {
3786 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003787 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003788 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003789 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003790 return LY_EVALID;
3791 }
3792 }
Michal Vasko004d3152020-06-11 19:59:22 +02003793
3794 /* insert it */
3795 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003796 }
3797 }
3798
3799 return LY_SUCCESS;
3800}
3801
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003802static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003803xpath_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 +02003804{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01003805 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003806 LY_ARRAY_COUNT_TYPE u;
3807 struct lyd_node_term *leaf;
3808 struct lysc_node_leaf *sleaf;
3809 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02003810 struct lyd_value *val;
3811 const struct lys_module *mod;
3812 const char *id_name;
3813 uint16_t id_len;
3814 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003815 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003816 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003817
3818 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003819 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003820 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003821 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3822 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3823 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3824 sleaf->name);
3825 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3826 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3827 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003828 }
3829
3830 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3831 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3832 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003833 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003834 } else if (!warn_is_string_type(sleaf->type)) {
3835 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3836 }
3837 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003838 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003839 return rc;
3840 }
3841
3842 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003843 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 +02003844 return LY_EVALID;
3845 }
3846 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3847 LY_CHECK_RET(rc);
3848
Michal Vasko93923692021-05-07 15:28:02 +02003849 /* parse the identity */
3850 id_name = args[1]->val.str;
3851 id_len = strlen(id_name);
3852 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
3853 LY_CHECK_RET(rc);
3854 if (!mod) {
3855 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
3856 return LY_EVALID;
3857 }
3858
3859 /* find the identity */
3860 found = 0;
3861 LY_ARRAY_FOR(mod->identities, u) {
3862 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
3863 /* we have match */
3864 found = 1;
3865 break;
3866 }
3867 }
3868 if (!found) {
3869 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
3870 return LY_EVALID;
3871 }
3872 id = &mod->identities[u];
3873
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003874 set_fill_boolean(set, 0);
3875 found = 0;
3876 for (i = 0; i < args[0]->used; ++i) {
3877 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3878 continue;
3879 }
3880
3881 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3882 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3883 sleaf = (struct lysc_node_leaf *)leaf->schema;
3884 val = &leaf->value;
3885 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3886 /* uninteresting */
3887 continue;
3888 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003889 } else {
3890 meta = args[0]->val.meta[i].meta;
3891 val = &meta->value;
3892 if (val->realtype->basetype != LY_TYPE_IDENT) {
3893 /* uninteresting */
3894 continue;
3895 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003896 }
3897
Michal Vasko93923692021-05-07 15:28:02 +02003898 /* check the identity itself */
3899 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003900 set_fill_boolean(set, 1);
3901 found = 1;
3902 }
Michal Vasko93923692021-05-07 15:28:02 +02003903 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
3904 set_fill_boolean(set, 1);
3905 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003906 }
3907
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003908 if (found) {
3909 break;
3910 }
3911 }
3912
3913 return LY_SUCCESS;
3914}
3915
Michal Vasko03ff5a72019-09-11 13:49:33 +02003916/**
3917 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3918 * on whether the first argument nodes contain a node of an identity derived from the second
3919 * argument identity.
3920 *
3921 * @param[in] args Array of arguments.
3922 * @param[in] arg_count Count of elements in @p args.
3923 * @param[in,out] set Context and result set at the same time.
3924 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003925 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003926 */
3927static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003928xpath_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 +02003929{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003930 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003931}
3932
3933/**
3934 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3935 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3936 * the second argument identity.
3937 *
3938 * @param[in] args Array of arguments.
3939 * @param[in] arg_count Count of elements in @p args.
3940 * @param[in,out] set Context and result set at the same time.
3941 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003942 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003943 */
3944static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003945xpath_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 +02003946{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003947 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003948}
3949
3950/**
3951 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3952 * with the integer value of the first node's enum value, otherwise NaN.
3953 *
3954 * @param[in] args Array of arguments.
3955 * @param[in] arg_count Count of elements in @p args.
3956 * @param[in,out] set Context and result set at the same time.
3957 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003958 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003959 */
3960static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003961xpath_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 +02003962{
3963 struct lyd_node_term *leaf;
3964 struct lysc_node_leaf *sleaf;
3965 LY_ERR rc = LY_SUCCESS;
3966
3967 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003968 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003969 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003970 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3971 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3972 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3973 sleaf->name);
3974 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3975 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
3976 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003977 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003978 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003979 return rc;
3980 }
3981
Michal Vaskod3678892020-05-21 10:06:58 +02003982 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003983 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003984 return LY_EVALID;
3985 }
3986
3987 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003988 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003989 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3990 sleaf = (struct lysc_node_leaf *)leaf->schema;
3991 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3992 set_fill_number(set, leaf->value.enum_item->value);
3993 }
3994 }
3995
3996 return LY_SUCCESS;
3997}
3998
3999/**
4000 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4001 * with false value.
4002 *
4003 * @param[in] args Array of arguments.
4004 * @param[in] arg_count Count of elements in @p args.
4005 * @param[in,out] set Context and result set at the same time.
4006 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004007 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004008 */
4009static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004010xpath_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 +02004011{
4012 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004013 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004014 return LY_SUCCESS;
4015 }
4016
4017 set_fill_boolean(set, 0);
4018 return LY_SUCCESS;
4019}
4020
4021/**
4022 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4023 * with the first argument floored (truncated).
4024 *
4025 * @param[in] args Array of arguments.
4026 * @param[in] arg_count Count of elements in @p args.
4027 * @param[in,out] set Context and result set at the same time.
4028 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004029 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004030 */
4031static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004032xpath_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 +02004033{
4034 LY_ERR rc;
4035
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004036 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004037 LY_CHECK_RET(rc);
4038 if (isfinite(args[0]->val.num)) {
4039 set_fill_number(set, (long long)args[0]->val.num);
4040 }
4041
4042 return LY_SUCCESS;
4043}
4044
4045/**
4046 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4047 * whether the language of the text matches the one from the argument.
4048 *
4049 * @param[in] args Array of arguments.
4050 * @param[in] arg_count Count of elements in @p args.
4051 * @param[in,out] set Context and result set at the same time.
4052 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004053 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004054 */
4055static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004056xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004057{
4058 const struct lyd_node *node;
4059 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004060 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004061 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004062 LY_ERR rc = LY_SUCCESS;
4063
4064 if (options & LYXP_SCNODE_ALL) {
4065 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4066 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004067 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4068 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004069 } else if (!warn_is_string_type(sleaf->type)) {
4070 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004071 }
4072 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004073 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004074 return rc;
4075 }
4076
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004077 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004078 LY_CHECK_RET(rc);
4079
Michal Vasko03ff5a72019-09-11 13:49:33 +02004080 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004081 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004082 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004083 } else if (!set->used) {
4084 set_fill_boolean(set, 0);
4085 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004086 }
4087
4088 switch (set->val.nodes[0].type) {
4089 case LYXP_NODE_ELEM:
4090 case LYXP_NODE_TEXT:
4091 node = set->val.nodes[0].node;
4092 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004093 case LYXP_NODE_META:
4094 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004095 break;
4096 default:
4097 /* nothing to do with roots */
4098 set_fill_boolean(set, 0);
4099 return LY_SUCCESS;
4100 }
4101
Michal Vasko9f96a052020-03-10 09:41:45 +01004102 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004103 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004104 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004105 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004106 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004107 break;
4108 }
4109 }
4110
Michal Vasko9f96a052020-03-10 09:41:45 +01004111 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004112 break;
4113 }
4114 }
4115
4116 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004117 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004118 set_fill_boolean(set, 0);
4119 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004120 uint64_t i;
4121
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004122 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004123 for (i = 0; args[0]->val.str[i]; ++i) {
4124 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4125 set_fill_boolean(set, 0);
4126 break;
4127 }
4128 }
4129 if (!args[0]->val.str[i]) {
4130 if (!val[i] || (val[i] == '-')) {
4131 set_fill_boolean(set, 1);
4132 } else {
4133 set_fill_boolean(set, 0);
4134 }
4135 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004136 }
4137
4138 return LY_SUCCESS;
4139}
4140
4141/**
4142 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4143 * with the context size.
4144 *
4145 * @param[in] args Array of arguments.
4146 * @param[in] arg_count Count of elements in @p args.
4147 * @param[in,out] set Context and result set at the same time.
4148 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004149 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004150 */
4151static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004152xpath_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 +02004153{
4154 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004155 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004156 return LY_SUCCESS;
4157 }
4158
Michal Vasko03ff5a72019-09-11 13:49:33 +02004159 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004160 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004161 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004162 } else if (!set->used) {
4163 set_fill_number(set, 0);
4164 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004165 }
4166
4167 set_fill_number(set, set->ctx_size);
4168 return LY_SUCCESS;
4169}
4170
4171/**
4172 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4173 * with the node name without namespace from the argument or the context.
4174 *
4175 * @param[in] args Array of arguments.
4176 * @param[in] arg_count Count of elements in @p args.
4177 * @param[in,out] set Context and result set at the same time.
4178 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004179 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004180 */
4181static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004182xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004183{
4184 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004185
Michal Vasko03ff5a72019-09-11 13:49:33 +02004186 /* suppress unused variable warning */
4187 (void)options;
4188
4189 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004190 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004191 return LY_SUCCESS;
4192 }
4193
4194 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004195 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004196 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004197 "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 (!args[0]->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(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004206
4207 item = &args[0]->val.nodes[0];
4208 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004209 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004210 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004211 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004212 } else if (!set->used) {
4213 set_fill_string(set, "", 0);
4214 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004215 }
4216
4217 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004218 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004219
4220 item = &set->val.nodes[0];
4221 }
4222
4223 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004224 case LYXP_NODE_NONE:
4225 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004226 case LYXP_NODE_ROOT:
4227 case LYXP_NODE_ROOT_CONFIG:
4228 case LYXP_NODE_TEXT:
4229 set_fill_string(set, "", 0);
4230 break;
4231 case LYXP_NODE_ELEM:
4232 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4233 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004234 case LYXP_NODE_META:
4235 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004236 break;
4237 }
4238
4239 return LY_SUCCESS;
4240}
4241
4242/**
4243 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4244 * with the node name fully qualified (with namespace) from the argument or the context.
4245 *
4246 * @param[in] args Array of arguments.
4247 * @param[in] arg_count Count of elements in @p args.
4248 * @param[in,out] set Context and result set at the same time.
4249 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004250 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004251 */
4252static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004253xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004254{
4255 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004256 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004257 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004258 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004259
4260 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004261 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004262 return LY_SUCCESS;
4263 }
4264
4265 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004266 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004267 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004268 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004269 } else if (!args[0]->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(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004276
4277 item = &args[0]->val.nodes[0];
4278 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004279 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004280 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004281 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004282 } else if (!set->used) {
4283 set_fill_string(set, "", 0);
4284 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004285 }
4286
4287 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004288 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004289
4290 item = &set->val.nodes[0];
4291 }
4292
4293 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004294 case LYXP_NODE_NONE:
4295 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004296 case LYXP_NODE_ROOT:
4297 case LYXP_NODE_ROOT_CONFIG:
4298 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004299 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004300 break;
4301 case LYXP_NODE_ELEM:
4302 mod = item->node->schema->module;
4303 name = item->node->schema->name;
4304 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004305 case LYXP_NODE_META:
4306 mod = ((struct lyd_meta *)item->node)->annotation->module;
4307 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004308 break;
4309 }
4310
4311 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004312 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004313 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4314 set_fill_string(set, str, strlen(str));
4315 free(str);
4316 } else {
4317 set_fill_string(set, "", 0);
4318 }
4319
4320 return LY_SUCCESS;
4321}
4322
4323/**
4324 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4325 * with the namespace of the node from the argument or the context.
4326 *
4327 * @param[in] args Array of arguments.
4328 * @param[in] arg_count Count of elements in @p args.
4329 * @param[in,out] set Context and result set at the same time.
4330 * @param[in] options XPath options.
4331 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4332 */
4333static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004334xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004335{
4336 struct lyxp_set_node *item;
4337 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004338
Michal Vasko03ff5a72019-09-11 13:49:33 +02004339 /* suppress unused variable warning */
4340 (void)options;
4341
4342 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004343 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004344 return LY_SUCCESS;
4345 }
4346
4347 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004348 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004349 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004350 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004351 return LY_EVALID;
4352 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004353 set_fill_string(set, "", 0);
4354 return LY_SUCCESS;
4355 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004356
4357 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004358 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004359
4360 item = &args[0]->val.nodes[0];
4361 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004362 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004363 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004364 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004365 } else if (!set->used) {
4366 set_fill_string(set, "", 0);
4367 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004368 }
4369
4370 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004371 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004372
4373 item = &set->val.nodes[0];
4374 }
4375
4376 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004377 case LYXP_NODE_NONE:
4378 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004379 case LYXP_NODE_ROOT:
4380 case LYXP_NODE_ROOT_CONFIG:
4381 case LYXP_NODE_TEXT:
4382 set_fill_string(set, "", 0);
4383 break;
4384 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004385 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004386 if (item->type == LYXP_NODE_ELEM) {
4387 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004388 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004389 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004390 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004391 }
4392
4393 set_fill_string(set, mod->ns, strlen(mod->ns));
4394 break;
4395 }
4396
4397 return LY_SUCCESS;
4398}
4399
4400/**
4401 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4402 * with only nodes from the context. In practice it either leaves the context
4403 * as it is or returns an empty node set.
4404 *
4405 * @param[in] args Array of arguments.
4406 * @param[in] arg_count Count of elements in @p args.
4407 * @param[in,out] set Context and result set at the same time.
4408 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004409 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004410 */
4411static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004412xpath_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 +02004413{
4414 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004415 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004416 return LY_SUCCESS;
4417 }
4418
4419 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004420 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004421 }
4422 return LY_SUCCESS;
4423}
4424
4425/**
4426 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4427 * with normalized value (no leading, trailing, double white spaces) of the node
4428 * from the argument or the context.
4429 *
4430 * @param[in] args Array of arguments.
4431 * @param[in] arg_count Count of elements in @p args.
4432 * @param[in,out] set Context and result set at the same time.
4433 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004434 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004435 */
4436static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004437xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004438{
4439 uint16_t i, new_used;
4440 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004441 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004442 struct lysc_node_leaf *sleaf;
4443 LY_ERR rc = LY_SUCCESS;
4444
4445 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004446 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4447 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004448 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004449 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4450 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004451 } else if (!warn_is_string_type(sleaf->type)) {
4452 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004453 }
4454 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004455 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004456 return rc;
4457 }
4458
4459 if (arg_count) {
4460 set_fill_set(set, args[0]);
4461 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004462 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004463 LY_CHECK_RET(rc);
4464
4465 /* is there any normalization necessary? */
4466 for (i = 0; set->val.str[i]; ++i) {
4467 if (is_xmlws(set->val.str[i])) {
4468 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4469 have_spaces = 1;
4470 break;
4471 }
4472 space_before = 1;
4473 } else {
4474 space_before = 0;
4475 }
4476 }
4477
4478 /* yep, there is */
4479 if (have_spaces) {
4480 /* it's enough, at least one character will go, makes space for ending '\0' */
4481 new = malloc(strlen(set->val.str) * sizeof(char));
4482 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4483 new_used = 0;
4484
4485 space_before = 0;
4486 for (i = 0; set->val.str[i]; ++i) {
4487 if (is_xmlws(set->val.str[i])) {
4488 if ((i == 0) || space_before) {
4489 space_before = 1;
4490 continue;
4491 } else {
4492 space_before = 1;
4493 }
4494 } else {
4495 space_before = 0;
4496 }
4497
4498 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4499 ++new_used;
4500 }
4501
4502 /* at worst there is one trailing space now */
4503 if (new_used && is_xmlws(new[new_used - 1])) {
4504 --new_used;
4505 }
4506
4507 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4508 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4509 new[new_used] = '\0';
4510
4511 free(set->val.str);
4512 set->val.str = new;
4513 }
4514
4515 return LY_SUCCESS;
4516}
4517
4518/**
4519 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4520 * with the argument converted to boolean and logically inverted.
4521 *
4522 * @param[in] args Array of arguments.
4523 * @param[in] arg_count Count of elements in @p args.
4524 * @param[in,out] set Context and result set at the same time.
4525 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004526 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004527 */
4528static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004529xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004530{
4531 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004532 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004533 return LY_SUCCESS;
4534 }
4535
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004536 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004537 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004538 set_fill_boolean(set, 0);
4539 } else {
4540 set_fill_boolean(set, 1);
4541 }
4542
4543 return LY_SUCCESS;
4544}
4545
4546/**
4547 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4548 * with the number representation of either the argument or the context.
4549 *
4550 * @param[in] args Array of arguments.
4551 * @param[in] arg_count Count of elements in @p args.
4552 * @param[in,out] set Context and result set at the same time.
4553 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004554 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004555 */
4556static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004557xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004558{
4559 LY_ERR rc;
4560
4561 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004562 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004563 return LY_SUCCESS;
4564 }
4565
4566 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004567 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004568 LY_CHECK_RET(rc);
4569 set_fill_set(set, args[0]);
4570 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004571 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004572 LY_CHECK_RET(rc);
4573 }
4574
4575 return LY_SUCCESS;
4576}
4577
4578/**
4579 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4580 * with the context position.
4581 *
4582 * @param[in] args Array of arguments.
4583 * @param[in] arg_count Count of elements in @p args.
4584 * @param[in,out] set Context and result set at the same time.
4585 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004586 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004587 */
4588static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004589xpath_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 +02004590{
4591 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004592 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004593 return LY_SUCCESS;
4594 }
4595
Michal Vasko03ff5a72019-09-11 13:49:33 +02004596 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004597 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004598 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004599 } else if (!set->used) {
4600 set_fill_number(set, 0);
4601 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004602 }
4603
4604 set_fill_number(set, set->ctx_pos);
4605
4606 /* UNUSED in 'Release' build type */
4607 (void)options;
4608 return LY_SUCCESS;
4609}
4610
4611/**
4612 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4613 * depending on whether the second argument regex matches the first argument string. For details refer to
4614 * YANG 1.1 RFC section 10.2.1.
4615 *
4616 * @param[in] args Array of arguments.
4617 * @param[in] arg_count Count of elements in @p args.
4618 * @param[in,out] set Context and result set at the same time.
4619 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004620 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004621 */
4622static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004623xpath_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 +02004624{
4625 struct lysc_pattern **patterns = NULL, **pattern;
4626 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004627 LY_ERR rc = LY_SUCCESS;
4628 struct ly_err_item *err;
4629
4630 if (options & LYXP_SCNODE_ALL) {
4631 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4632 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4633 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 +02004634 } else if (!warn_is_string_type(sleaf->type)) {
4635 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004636 }
4637 }
4638
4639 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4640 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4641 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 +02004642 } else if (!warn_is_string_type(sleaf->type)) {
4643 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004644 }
4645 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004646 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004647 return rc;
4648 }
4649
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004650 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004651 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004652 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004653 LY_CHECK_RET(rc);
4654
4655 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek IÅ¡a45802b52021-02-09 09:21:58 +01004656 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004657 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004658 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Radek Krejciddace2c2021-01-08 11:30:56 +01004659 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004660 if (rc != LY_SUCCESS) {
4661 LY_ARRAY_FREE(patterns);
4662 return rc;
4663 }
4664
Radek Krejci0b013302021-03-29 15:22:32 +02004665 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004666 pcre2_code_free((*pattern)->code);
4667 free(*pattern);
4668 LY_ARRAY_FREE(patterns);
4669 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004670 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004671 ly_err_free(err);
4672 return rc;
4673 }
4674
4675 if (rc == LY_EVALID) {
4676 ly_err_free(err);
4677 set_fill_boolean(set, 0);
4678 } else {
4679 set_fill_boolean(set, 1);
4680 }
4681
4682 return LY_SUCCESS;
4683}
4684
4685/**
4686 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4687 * with the rounded first argument. For details refer to
4688 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4689 *
4690 * @param[in] args Array of arguments.
4691 * @param[in] arg_count Count of elements in @p args.
4692 * @param[in,out] set Context and result set at the same time.
4693 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004694 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004695 */
4696static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004697xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004698{
4699 struct lysc_node_leaf *sleaf;
4700 LY_ERR rc = LY_SUCCESS;
4701
4702 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004703 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004704 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004705 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4706 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4707 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4708 sleaf->name);
4709 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4710 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4711 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004712 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004713 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004714 return rc;
4715 }
4716
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004717 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004718 LY_CHECK_RET(rc);
4719
4720 /* cover only the cases where floor can't be used */
4721 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4722 set_fill_number(set, -0.0f);
4723 } else {
4724 args[0]->val.num += 0.5;
4725 rc = xpath_floor(args, 1, args[0], options);
4726 LY_CHECK_RET(rc);
4727 set_fill_number(set, args[0]->val.num);
4728 }
4729
4730 return LY_SUCCESS;
4731}
4732
4733/**
4734 * @brief Execute the XPath starts-with(string, string) function.
4735 * Returns LYXP_SET_BOOLEAN whether the second argument is
4736 * the prefix of the first or not.
4737 *
4738 * @param[in] args Array of arguments.
4739 * @param[in] arg_count Count of elements in @p args.
4740 * @param[in,out] set Context and result set at the same time.
4741 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004742 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004743 */
4744static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004745xpath_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 +02004746{
4747 struct lysc_node_leaf *sleaf;
4748 LY_ERR rc = LY_SUCCESS;
4749
4750 if (options & LYXP_SCNODE_ALL) {
4751 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4752 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4753 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 +02004754 } else if (!warn_is_string_type(sleaf->type)) {
4755 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004756 }
4757 }
4758
4759 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4760 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4761 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 +02004762 } else if (!warn_is_string_type(sleaf->type)) {
4763 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004764 }
4765 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004766 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004767 return rc;
4768 }
4769
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004770 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004771 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004772 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004773 LY_CHECK_RET(rc);
4774
4775 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4776 set_fill_boolean(set, 0);
4777 } else {
4778 set_fill_boolean(set, 1);
4779 }
4780
4781 return LY_SUCCESS;
4782}
4783
4784/**
4785 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4786 * with the string representation of either the argument or the context.
4787 *
4788 * @param[in] args Array of arguments.
4789 * @param[in] arg_count Count of elements in @p args.
4790 * @param[in,out] set Context and result set at the same time.
4791 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004792 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004793 */
4794static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004795xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004796{
4797 LY_ERR rc;
4798
4799 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004800 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004801 return LY_SUCCESS;
4802 }
4803
4804 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004805 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004806 LY_CHECK_RET(rc);
4807 set_fill_set(set, args[0]);
4808 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004809 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004810 LY_CHECK_RET(rc);
4811 }
4812
4813 return LY_SUCCESS;
4814}
4815
4816/**
4817 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4818 * with the length of the string in either the argument or the context.
4819 *
4820 * @param[in] args Array of arguments.
4821 * @param[in] arg_count Count of elements in @p args.
4822 * @param[in,out] set Context and result set at the same time.
4823 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004824 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004825 */
4826static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004827xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004828{
4829 struct lysc_node_leaf *sleaf;
4830 LY_ERR rc = LY_SUCCESS;
4831
4832 if (options & LYXP_SCNODE_ALL) {
4833 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4834 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4835 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 +02004836 } else if (!warn_is_string_type(sleaf->type)) {
4837 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004838 }
4839 }
4840 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4841 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4842 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 +02004843 } else if (!warn_is_string_type(sleaf->type)) {
4844 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004845 }
4846 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004847 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004848 return rc;
4849 }
4850
4851 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004852 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004853 LY_CHECK_RET(rc);
4854 set_fill_number(set, strlen(args[0]->val.str));
4855 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004856 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004857 LY_CHECK_RET(rc);
4858 set_fill_number(set, strlen(set->val.str));
4859 }
4860
4861 return LY_SUCCESS;
4862}
4863
4864/**
4865 * @brief Execute the XPath substring(string, number, number?) function.
4866 * Returns LYXP_SET_STRING substring of the first argument starting
4867 * on the second argument index ending on the third argument index,
4868 * indexed from 1. For exact definition refer to
4869 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4870 *
4871 * @param[in] args Array of arguments.
4872 * @param[in] arg_count Count of elements in @p args.
4873 * @param[in,out] set Context and result set at the same time.
4874 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004875 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004876 */
4877static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004878xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004879{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004880 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004881 uint16_t str_start, str_len, pos;
4882 struct lysc_node_leaf *sleaf;
4883 LY_ERR rc = LY_SUCCESS;
4884
4885 if (options & LYXP_SCNODE_ALL) {
4886 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4887 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4888 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 +02004889 } else if (!warn_is_string_type(sleaf->type)) {
4890 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004891 }
4892 }
4893
4894 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4895 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4896 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 +02004897 } else if (!warn_is_numeric_type(sleaf->type)) {
4898 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004899 }
4900 }
4901
Michal Vasko69730152020-10-09 16:30:07 +02004902 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4903 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004904 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4905 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 +02004906 } else if (!warn_is_numeric_type(sleaf->type)) {
4907 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004908 }
4909 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004910 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004911 return rc;
4912 }
4913
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004914 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004915 LY_CHECK_RET(rc);
4916
4917 /* start */
4918 if (xpath_round(&args[1], 1, args[1], options)) {
4919 return -1;
4920 }
4921 if (isfinite(args[1]->val.num)) {
4922 start = args[1]->val.num - 1;
4923 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004924 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004925 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004926 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004927 }
4928
4929 /* len */
4930 if (arg_count == 3) {
4931 rc = xpath_round(&args[2], 1, args[2], options);
4932 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004933 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004934 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004935 } else if (isfinite(args[2]->val.num)) {
4936 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004937 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004938 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004939 }
4940 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004941 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004942 }
4943
4944 /* find matching character positions */
4945 str_start = 0;
4946 str_len = 0;
4947 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4948 if (pos < start) {
4949 ++str_start;
4950 } else if (pos < start + len) {
4951 ++str_len;
4952 } else {
4953 break;
4954 }
4955 }
4956
4957 set_fill_string(set, args[0]->val.str + str_start, str_len);
4958 return LY_SUCCESS;
4959}
4960
4961/**
4962 * @brief Execute the XPath substring-after(string, string) function.
4963 * Returns LYXP_SET_STRING with the string succeeding the occurance
4964 * of the second argument in the first or an empty string.
4965 *
4966 * @param[in] args Array of arguments.
4967 * @param[in] arg_count Count of elements in @p args.
4968 * @param[in,out] set Context and result set at the same time.
4969 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004970 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004971 */
4972static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004973xpath_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 +02004974{
4975 char *ptr;
4976 struct lysc_node_leaf *sleaf;
4977 LY_ERR rc = LY_SUCCESS;
4978
4979 if (options & LYXP_SCNODE_ALL) {
4980 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4981 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4982 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 +02004983 } else if (!warn_is_string_type(sleaf->type)) {
4984 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004985 }
4986 }
4987
4988 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4989 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4990 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 +02004991 } else if (!warn_is_string_type(sleaf->type)) {
4992 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004993 }
4994 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004995 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004996 return rc;
4997 }
4998
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004999 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005000 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005001 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005002 LY_CHECK_RET(rc);
5003
5004 ptr = strstr(args[0]->val.str, args[1]->val.str);
5005 if (ptr) {
5006 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5007 } else {
5008 set_fill_string(set, "", 0);
5009 }
5010
5011 return LY_SUCCESS;
5012}
5013
5014/**
5015 * @brief Execute the XPath substring-before(string, string) function.
5016 * Returns LYXP_SET_STRING with the string preceding the occurance
5017 * of the second argument in the first or an empty string.
5018 *
5019 * @param[in] args Array of arguments.
5020 * @param[in] arg_count Count of elements in @p args.
5021 * @param[in,out] set Context and result set at the same time.
5022 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005023 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005024 */
5025static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005026xpath_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 +02005027{
5028 char *ptr;
5029 struct lysc_node_leaf *sleaf;
5030 LY_ERR rc = LY_SUCCESS;
5031
5032 if (options & LYXP_SCNODE_ALL) {
5033 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5034 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5035 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 +02005036 } else if (!warn_is_string_type(sleaf->type)) {
5037 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005038 }
5039 }
5040
5041 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5042 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5043 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 +02005044 } else if (!warn_is_string_type(sleaf->type)) {
5045 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005046 }
5047 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005048 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005049 return rc;
5050 }
5051
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005052 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005053 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005054 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005055 LY_CHECK_RET(rc);
5056
5057 ptr = strstr(args[0]->val.str, args[1]->val.str);
5058 if (ptr) {
5059 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5060 } else {
5061 set_fill_string(set, "", 0);
5062 }
5063
5064 return LY_SUCCESS;
5065}
5066
5067/**
5068 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5069 * with the sum of all the nodes in the context.
5070 *
5071 * @param[in] args Array of arguments.
5072 * @param[in] arg_count Count of elements in @p args.
5073 * @param[in,out] set Context and result set at the same time.
5074 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005075 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005076 */
5077static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005078xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005079{
5080 long double num;
5081 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005082 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005083 struct lyxp_set set_item;
5084 struct lysc_node_leaf *sleaf;
5085 LY_ERR rc = LY_SUCCESS;
5086
5087 if (options & LYXP_SCNODE_ALL) {
5088 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5089 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005090 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005091 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5092 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5093 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005094 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005095 } else if (!warn_is_numeric_type(sleaf->type)) {
5096 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005097 }
5098 }
5099 }
5100 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005101 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005102 return rc;
5103 }
5104
5105 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005106
5107 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005108 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005109 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005110 } else if (!args[0]->used) {
5111 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005112 }
5113
Michal Vasko5c4e5892019-11-14 12:31:38 +01005114 set_init(&set_item, set);
5115
Michal Vasko03ff5a72019-09-11 13:49:33 +02005116 set_item.type = LYXP_SET_NODE_SET;
5117 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5118 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5119
5120 set_item.used = 1;
5121 set_item.size = 1;
5122
5123 for (i = 0; i < args[0]->used; ++i) {
5124 set_item.val.nodes[0] = args[0]->val.nodes[i];
5125
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005126 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005127 LY_CHECK_RET(rc);
5128 num = cast_string_to_number(str);
5129 free(str);
5130 set->val.num += num;
5131 }
5132
5133 free(set_item.val.nodes);
5134
5135 return LY_SUCCESS;
5136}
5137
5138/**
5139 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5140 * with the text content of the nodes in the context.
5141 *
5142 * @param[in] args Array of arguments.
5143 * @param[in] arg_count Count of elements in @p args.
5144 * @param[in,out] set Context and result set at the same time.
5145 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005146 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005147 */
5148static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005149xpath_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 +02005150{
5151 uint32_t i;
5152
5153 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005154 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005155 return LY_SUCCESS;
5156 }
5157
Michal Vasko03ff5a72019-09-11 13:49:33 +02005158 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005159 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005160 return LY_EVALID;
5161 }
5162
Michal Vaskod989ba02020-08-24 10:59:24 +02005163 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005164 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005165 case LYXP_NODE_NONE:
5166 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005167 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005168 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5169 set->val.nodes[i].type = LYXP_NODE_TEXT;
5170 ++i;
5171 break;
5172 }
Radek Krejci0f969882020-08-21 16:56:47 +02005173 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005174 case LYXP_NODE_ROOT:
5175 case LYXP_NODE_ROOT_CONFIG:
5176 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005177 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005178 set_remove_node(set, i);
5179 break;
5180 }
5181 }
5182
5183 return LY_SUCCESS;
5184}
5185
5186/**
5187 * @brief Execute the XPath translate(string, string, string) function.
5188 * Returns LYXP_SET_STRING with the first argument with the characters
5189 * from the second argument replaced by those on the corresponding
5190 * positions in the third argument.
5191 *
5192 * @param[in] args Array of arguments.
5193 * @param[in] arg_count Count of elements in @p args.
5194 * @param[in,out] set Context and result set at the same time.
5195 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005196 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005197 */
5198static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005199xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005200{
5201 uint16_t i, j, new_used;
5202 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005203 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005204 struct lysc_node_leaf *sleaf;
5205 LY_ERR rc = LY_SUCCESS;
5206
5207 if (options & LYXP_SCNODE_ALL) {
5208 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5209 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5210 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 +02005211 } else if (!warn_is_string_type(sleaf->type)) {
5212 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005213 }
5214 }
5215
5216 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5217 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5218 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 +02005219 } else if (!warn_is_string_type(sleaf->type)) {
5220 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005221 }
5222 }
5223
5224 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5225 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5226 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 +02005227 } else if (!warn_is_string_type(sleaf->type)) {
5228 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005229 }
5230 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005231 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005232 return rc;
5233 }
5234
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005235 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005236 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005237 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005238 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005239 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005240 LY_CHECK_RET(rc);
5241
5242 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5243 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5244 new_used = 0;
5245
5246 have_removed = 0;
5247 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005248 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005249
5250 for (j = 0; args[1]->val.str[j]; ++j) {
5251 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5252 /* removing this char */
5253 if (j >= strlen(args[2]->val.str)) {
5254 have_removed = 1;
5255 found = 1;
5256 break;
5257 }
5258 /* replacing this char */
5259 new[new_used] = args[2]->val.str[j];
5260 ++new_used;
5261 found = 1;
5262 break;
5263 }
5264 }
5265
5266 /* copying this char */
5267 if (!found) {
5268 new[new_used] = args[0]->val.str[i];
5269 ++new_used;
5270 }
5271 }
5272
5273 if (have_removed) {
5274 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5275 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5276 }
5277 new[new_used] = '\0';
5278
Michal Vaskod3678892020-05-21 10:06:58 +02005279 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005280 set->type = LYXP_SET_STRING;
5281 set->val.str = new;
5282
5283 return LY_SUCCESS;
5284}
5285
5286/**
5287 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5288 * with true value.
5289 *
5290 * @param[in] args Array of arguments.
5291 * @param[in] arg_count Count of elements in @p args.
5292 * @param[in,out] set Context and result set at the same time.
5293 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005294 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005295 */
5296static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005297xpath_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 +02005298{
5299 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005300 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005301 return LY_SUCCESS;
5302 }
5303
5304 set_fill_boolean(set, 1);
5305 return LY_SUCCESS;
5306}
5307
5308/*
5309 * moveto functions
5310 *
5311 * They and only they actually change the context (set).
5312 */
5313
5314/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005315 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005316 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005317 * XPath @p set is expected to be a (sc)node set!
5318 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005319 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5320 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005321 * @param[in] set Set with general XPath context.
5322 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005323 * @param[out] moveto_mod Expected module of a matching node.
5324 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005325 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005326static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005327moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5328 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005329{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005330 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005331 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005332 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005333
Michal Vasko2104e9f2020-03-06 08:23:25 +01005334 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5335
Michal Vasko6346ece2019-09-24 13:12:53 +02005336 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5337 /* specific module */
5338 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005339 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005340
Michal Vasko004d3152020-06-11 19:59:22 +02005341 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005342 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005343 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005344 return LY_EVALID;
5345 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005346
Michal Vasko6346ece2019-09-24 13:12:53 +02005347 *qname += pref_len + 1;
5348 *qname_len -= pref_len + 1;
5349 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5350 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005351 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005352 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005353 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005354 case LY_VALUE_SCHEMA:
5355 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005356 /* current module */
5357 mod = set->cur_mod;
5358 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005359 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005360 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005361 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005362 /* inherit parent (context node) module */
5363 if (ctx_scnode) {
5364 mod = ctx_scnode->module;
5365 } else {
5366 mod = NULL;
5367 }
5368 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005369 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005370 /* all nodes need to be prefixed */
5371 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5372 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005373 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005374 }
5375
Michal Vasko6346ece2019-09-24 13:12:53 +02005376 *moveto_mod = mod;
5377 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005378}
5379
5380/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005381 * @brief Move context @p set to the root. Handles absolute path.
5382 * Result is LYXP_SET_NODE_SET.
5383 *
5384 * @param[in,out] set Set to use.
5385 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005386 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005387 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005388static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005389moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005390{
aPiecek8b0cc152021-05-31 16:40:31 +02005391 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005392
5393 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005394 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskob0099a92020-08-31 14:55:23 +02005395 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005396 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005397 set->type = LYXP_SET_NODE_SET;
5398 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005399 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005400 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005401
5402 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005403}
5404
5405/**
5406 * @brief Check @p node as a part of NameTest processing.
5407 *
5408 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005409 * @param[in] ctx_node Context node.
5410 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005411 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005412 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005413 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005414 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5415 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005416 */
5417static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005418moveto_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 +01005419 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005420{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005421 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5422 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005423 case LY_VALUE_SCHEMA:
5424 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005425 /* use current module */
5426 moveto_mod = set->cur_mod;
5427 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005428 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005429 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005430 /* inherit module of the context node, if any */
5431 if (ctx_node) {
5432 moveto_mod = ctx_node->schema->module;
5433 }
5434 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005435 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005436 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005437 /* not defined */
5438 LOGINT(set->ctx);
5439 return LY_EINVAL;
5440 }
5441 }
5442
Michal Vasko03ff5a72019-09-11 13:49:33 +02005443 /* module check */
5444 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005445 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005446 }
5447
Michal Vasko5c4e5892019-11-14 12:31:38 +01005448 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005449 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005450 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005451 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5452 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005453 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005454 }
5455
5456 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005457 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005458 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005459 }
5460
Michal Vaskoa1424542019-11-14 16:08:52 +01005461 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005462 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005463 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005464 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005465
5466 /* match */
5467 return LY_SUCCESS;
5468}
5469
5470/**
5471 * @brief Check @p node as a part of schema NameTest processing.
5472 *
5473 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005474 * @param[in] ctx_scnode Context node.
5475 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005476 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005477 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005478 * @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 +02005479 */
5480static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005481moveto_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 +02005482 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005483{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005484 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5485 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005486 case LY_VALUE_SCHEMA:
5487 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005488 /* use current module */
5489 moveto_mod = set->cur_mod;
5490 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005491 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005492 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005493 /* inherit module of the context node, if any */
5494 if (ctx_scnode) {
5495 moveto_mod = ctx_scnode->module;
5496 }
5497 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005498 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005499 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005500 /* not defined */
5501 LOGINT(set->ctx);
5502 return LY_EINVAL;
5503 }
5504 }
5505
Michal Vasko03ff5a72019-09-11 13:49:33 +02005506 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005507 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005508 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005509 }
5510
5511 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005512 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005513 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005514 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005515 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005516 }
5517
5518 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005519 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005520 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005521 }
5522
5523 /* match */
5524 return LY_SUCCESS;
5525}
5526
5527/**
Michal Vaskod3678892020-05-21 10:06:58 +02005528 * @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 +02005529 *
5530 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005531 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005532 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005533 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005534 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5535 */
5536static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005537moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005538{
Michal Vaskof03ed032020-03-04 13:31:44 +01005539 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005540 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005541 LY_ERR rc;
5542
aPiecek8b0cc152021-05-31 16:40:31 +02005543 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005544 return LY_SUCCESS;
5545 }
5546
5547 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005548 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005549 return LY_EVALID;
5550 }
5551
Michal Vasko03ff5a72019-09-11 13:49:33 +02005552 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005553 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005554
5555 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 +01005556 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005557
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005558 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005559 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005560 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005561 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005562 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005563 ctx_node = set->val.nodes[i].node;
5564 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005565 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005566
Michal Vaskod3678892020-05-21 10:06:58 +02005567 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005568 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005569 if (rc == LY_SUCCESS) {
5570 if (!replaced) {
5571 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5572 replaced = 1;
5573 } else {
5574 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005575 }
Michal Vaskod3678892020-05-21 10:06:58 +02005576 ++i;
5577 } else if (rc == LY_EINCOMPLETE) {
5578 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005579 }
5580 }
5581
5582 if (!replaced) {
5583 /* no match */
5584 set_remove_node(set, i);
5585 }
5586 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005587
5588 return LY_SUCCESS;
5589}
5590
5591/**
Michal Vaskod3678892020-05-21 10:06:58 +02005592 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5593 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005594 *
5595 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005596 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005597 * @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 +01005598 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005599 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5600 */
5601static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005602moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5603 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005604{
Michal Vasko004d3152020-06-11 19:59:22 +02005605 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005606 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005607 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005608 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005609
Michal Vasko004d3152020-06-11 19:59:22 +02005610 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005611
aPiecek8b0cc152021-05-31 16:40:31 +02005612 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02005613 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005614 }
5615
5616 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005617 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005618 ret = LY_EVALID;
5619 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005620 }
5621
5622 /* context check for all the nodes since we have the schema node */
5623 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5624 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005625 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005626 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5627 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005628 lyxp_set_free_content(set);
5629 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005630 }
5631
5632 /* create specific data instance if needed */
5633 if (scnode->nodetype == LYS_LIST) {
5634 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5635 } else if (scnode->nodetype == LYS_LEAFLIST) {
5636 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005637 }
5638
5639 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005640 siblings = NULL;
5641
5642 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5643 assert(!set->val.nodes[i].node);
5644
5645 /* search in all the trees */
5646 siblings = set->tree;
5647 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5648 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005649 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005650 }
5651
5652 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005653 if (inst) {
5654 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005655 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005656 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005657 }
5658
5659 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005660 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005661 ret = LY_EINCOMPLETE;
5662 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005663 }
5664
5665 if (sub) {
5666 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005667 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005668 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005669 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005670 /* no match */
5671 set_remove_node(set, i);
5672 }
5673 }
5674
Michal Vasko004d3152020-06-11 19:59:22 +02005675cleanup:
5676 lyd_free_tree(inst);
5677 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005678}
5679
5680/**
5681 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5682 *
5683 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005684 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005685 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005686 * @param[in] options XPath options.
5687 * @return LY_ERR
5688 */
5689static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005690moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005691{
Radek Krejci857189e2020-09-01 13:26:36 +02005692 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005693 uint32_t getnext_opts;
5694 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005695 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005696 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005697 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005698
aPiecek8b0cc152021-05-31 16:40:31 +02005699 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005700 return LY_SUCCESS;
5701 }
5702
5703 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005704 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005705 return LY_EVALID;
5706 }
5707
Michal Vaskocafad9d2019-11-07 15:20:03 +01005708 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005709 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005710 if (options & LYXP_SCNODE_OUTPUT) {
5711 getnext_opts |= LYS_GETNEXT_OUTPUT;
5712 }
5713
Michal Vasko03ff5a72019-09-11 13:49:33 +02005714 orig_used = set->used;
5715 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005716 uint32_t idx;
5717
Radek Krejcif13b87b2020-12-01 22:02:17 +01005718 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5719 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005720 continue;
5721 }
5722
5723 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005724 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005725 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005726 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005727 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005728
5729 start_parent = set->val.scnodes[i].scnode;
5730
5731 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 +02005732 /* 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 +02005733 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005734 mod_idx = 0;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005735 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005736 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005737 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005738 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005739 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005740 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5741
Michal Vasko03ff5a72019-09-11 13:49:33 +02005742 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005743 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005744 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005745 temp_ctx = 1;
5746 }
5747 }
5748 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005749 }
5750
Michal Vasko519fd602020-05-26 12:17:39 +02005751 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5752 iter = NULL;
5753 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005754 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005755 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5756
5757 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005758 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005759 temp_ctx = 1;
5760 }
5761 }
5762 }
5763 }
5764 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005765
5766 /* correct temporary in_ctx values */
5767 if (temp_ctx) {
5768 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005769 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5770 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005771 }
5772 }
5773 }
5774
5775 return LY_SUCCESS;
5776}
5777
5778/**
Michal Vaskod3678892020-05-21 10:06:58 +02005779 * @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 +02005780 * Context position aware.
5781 *
5782 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005783 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005784 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005785 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005786 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5787 */
5788static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005789moveto_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 +02005790{
5791 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005792 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005793 struct lyxp_set ret_set;
5794 LY_ERR rc;
5795
aPiecek8b0cc152021-05-31 16:40:31 +02005796 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005797 return LY_SUCCESS;
5798 }
5799
5800 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005801 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005802 return LY_EVALID;
5803 }
5804
Michal Vasko9f96a052020-03-10 09:41:45 +01005805 /* 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 +01005806 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005807 LY_CHECK_RET(rc);
5808
Michal Vasko6346ece2019-09-24 13:12:53 +02005809 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005810 set_init(&ret_set, set);
5811 for (i = 0; i < set->used; ++i) {
5812
5813 /* TREE DFS */
5814 start = set->val.nodes[i].node;
5815 for (elem = next = start; elem; elem = next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005816 rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005817 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005818 /* add matching node into result set */
5819 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5820 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5821 /* the node is a duplicate, we'll process it later in the set */
5822 goto skip_children;
5823 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005824 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005825 return rc;
5826 } else if (rc == LY_EINVAL) {
5827 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005828 }
5829
5830 /* TREE DFS NEXT ELEM */
5831 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005832 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005833 if (!next) {
5834skip_children:
5835 /* no children, so try siblings, but only if it's not the start,
5836 * that is considered to be the root and it's siblings are not traversed */
5837 if (elem != start) {
5838 next = elem->next;
5839 } else {
5840 break;
5841 }
5842 }
5843 while (!next) {
5844 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01005845 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005846 /* we are done, no next element to process */
5847 break;
5848 }
5849 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01005850 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005851 next = elem->next;
5852 }
5853 }
5854 }
5855
5856 /* make the temporary set the current one */
5857 ret_set.ctx_pos = set->ctx_pos;
5858 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005859 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005860 memcpy(set, &ret_set, sizeof *set);
5861
5862 return LY_SUCCESS;
5863}
5864
5865/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005866 * @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 +02005867 *
5868 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005869 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005870 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005871 * @param[in] options XPath options.
5872 * @return LY_ERR
5873 */
5874static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005875moveto_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 +02005876{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005877 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005878 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005879 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005880
aPiecek8b0cc152021-05-31 16:40:31 +02005881 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005882 return LY_SUCCESS;
5883 }
5884
5885 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005886 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005887 return LY_EVALID;
5888 }
5889
Michal Vasko03ff5a72019-09-11 13:49:33 +02005890 orig_used = set->used;
5891 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005892 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5893 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005894 continue;
5895 }
5896
5897 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005898 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005899 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005900 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005901 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005902
5903 /* TREE DFS */
5904 start = set->val.scnodes[i].scnode;
5905 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005906 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5907 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005908 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005909 }
5910
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005911 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005912 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005913 uint32_t idx;
5914
5915 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005916 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005917 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005918 /* we will process it later in the set */
5919 goto skip_children;
5920 }
5921 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005922 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005923 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005924 } else if (rc == LY_EINVAL) {
5925 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005926 }
5927
5928next_iter:
5929 /* TREE DFS NEXT ELEM */
5930 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005931 next = lysc_node_child(elem);
5932 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5933 next = next->next;
5934 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5935 next = next->next;
5936 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005937 if (!next) {
5938skip_children:
5939 /* no children, so try siblings, but only if it's not the start,
5940 * that is considered to be the root and it's siblings are not traversed */
5941 if (elem != start) {
5942 next = elem->next;
5943 } else {
5944 break;
5945 }
5946 }
5947 while (!next) {
5948 /* no siblings, go back through the parents */
5949 if (elem->parent == start) {
5950 /* we are done, no next element to process */
5951 break;
5952 }
5953 /* parent is already processed, go to its sibling */
5954 elem = elem->parent;
5955 next = elem->next;
5956 }
5957 }
5958 }
5959
5960 return LY_SUCCESS;
5961}
5962
5963/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005964 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005965 * Indirectly context position aware.
5966 *
5967 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005968 * @param[in] mod Matching metadata module, NULL for any.
5969 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02005970 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005971 * @return LY_ERR
5972 */
5973static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02005974moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005975{
Michal Vasko9f96a052020-03-10 09:41:45 +01005976 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005977
aPiecek8b0cc152021-05-31 16:40:31 +02005978 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005979 return LY_SUCCESS;
5980 }
5981
5982 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005983 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005984 return LY_EVALID;
5985 }
5986
Radek Krejci1deb5be2020-08-26 16:43:36 +02005987 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005988 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005989
5990 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5991 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005992 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005993 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005994
5995 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005996 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005997 continue;
5998 }
5999
Michal Vaskod3678892020-05-21 10:06:58 +02006000 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006001 /* match */
6002 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006003 set->val.meta[i].meta = sub;
6004 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006005 /* pos does not change */
6006 replaced = 1;
6007 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006008 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 +02006009 }
6010 ++i;
6011 }
6012 }
6013 }
6014
6015 if (!replaced) {
6016 /* no match */
6017 set_remove_node(set, i);
6018 }
6019 }
6020
6021 return LY_SUCCESS;
6022}
6023
6024/**
6025 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02006026 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006027 *
6028 * @param[in,out] set1 Set to use for the result.
6029 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006030 * @return LY_ERR
6031 */
6032static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006033moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006034{
6035 LY_ERR rc;
6036
Michal Vaskod3678892020-05-21 10:06:58 +02006037 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006038 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006039 return LY_EVALID;
6040 }
6041
6042 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02006043 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006044 return LY_SUCCESS;
6045 }
6046
Michal Vaskod3678892020-05-21 10:06:58 +02006047 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006048 memcpy(set1, set2, sizeof *set1);
6049 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02006050 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006051 return LY_SUCCESS;
6052 }
6053
6054 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006055 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006056
6057 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006058 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006059 LY_CHECK_RET(rc);
6060
6061 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006062 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006063
6064 return LY_SUCCESS;
6065}
6066
6067/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006068 * @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 +02006069 * Context position aware.
6070 *
6071 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006072 * @param[in] mod Matching metadata module, NULL for any.
6073 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006074 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006075 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6076 */
6077static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006078moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006079{
Michal Vasko9f96a052020-03-10 09:41:45 +01006080 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006081 struct lyxp_set *set_all_desc = NULL;
6082 LY_ERR rc;
6083
aPiecek8b0cc152021-05-31 16:40:31 +02006084 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006085 return LY_SUCCESS;
6086 }
6087
6088 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006089 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006090 return LY_EVALID;
6091 }
6092
Michal Vasko03ff5a72019-09-11 13:49:33 +02006093 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6094 * but it likely won't be used much, so it's a waste of time */
6095 /* copy the context */
6096 set_all_desc = set_copy(set);
6097 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006098 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006099 if (rc != LY_SUCCESS) {
6100 lyxp_set_free(set_all_desc);
6101 return rc;
6102 }
6103 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006104 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006105 if (rc != LY_SUCCESS) {
6106 lyxp_set_free(set_all_desc);
6107 return rc;
6108 }
6109 lyxp_set_free(set_all_desc);
6110
Radek Krejci1deb5be2020-08-26 16:43:36 +02006111 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006112 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006113
6114 /* only attributes of an elem can be in the result, skip all the rest,
6115 * we have all attributes qualified in lyd tree */
6116 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006117 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006118 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006119 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006120 continue;
6121 }
6122
Michal Vaskod3678892020-05-21 10:06:58 +02006123 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006124 /* match */
6125 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006126 set->val.meta[i].meta = sub;
6127 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006128 /* pos does not change */
6129 replaced = 1;
6130 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006131 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 +02006132 }
6133 ++i;
6134 }
6135 }
6136 }
6137
6138 if (!replaced) {
6139 /* no match */
6140 set_remove_node(set, i);
6141 }
6142 }
6143
6144 return LY_SUCCESS;
6145}
6146
6147/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006148 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6149 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006150 *
6151 * @param[in] parent Current parent.
6152 * @param[in] parent_pos Position of @p parent.
6153 * @param[in] parent_type Node type of @p parent.
6154 * @param[in,out] to_set Set to use.
6155 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006156 * @param[in] options XPath options.
6157 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6158 */
6159static LY_ERR
6160moveto_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 +02006161 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006162{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006163 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006164 LY_ERR rc;
6165
6166 switch (parent_type) {
6167 case LYXP_NODE_ROOT:
6168 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006169 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006170
Michal Vasko61ac2f62020-05-25 12:39:51 +02006171 /* add all top-level nodes as elements */
6172 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006173 break;
6174 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006175 /* add just the text node of this term element node */
6176 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006177 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6178 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6179 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006180 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006181 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006182
6183 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006184 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006185 break;
6186 default:
6187 LOGINT_RET(parent->schema->module->ctx);
6188 }
6189
Michal Vasko61ac2f62020-05-25 12:39:51 +02006190 /* add all top-level nodes as elements */
6191 LY_LIST_FOR(first, iter) {
6192 /* context check */
6193 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6194 continue;
6195 }
6196
6197 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006198 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006199 return LY_EINCOMPLETE;
6200 }
6201
6202 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6203 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6204
6205 /* also add all the children of this node, recursively */
6206 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6207 LY_CHECK_RET(rc);
6208 }
6209 }
6210
Michal Vasko03ff5a72019-09-11 13:49:33 +02006211 return LY_SUCCESS;
6212}
6213
6214/**
6215 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6216 * (or LYXP_SET_EMPTY). Context position aware.
6217 *
6218 * @param[in,out] set Set to use.
6219 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6220 * @param[in] options XPath options.
6221 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6222 */
6223static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006224moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006225{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006226 struct lyxp_set ret_set;
6227 LY_ERR rc;
6228
aPiecek8b0cc152021-05-31 16:40:31 +02006229 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006230 return LY_SUCCESS;
6231 }
6232
6233 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006234 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006235 return LY_EVALID;
6236 }
6237
6238 /* nothing to do */
6239 if (!all_desc) {
6240 return LY_SUCCESS;
6241 }
6242
Michal Vasko03ff5a72019-09-11 13:49:33 +02006243 /* add all the children, they get added recursively */
6244 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006245 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006246 /* copy the current node to tmp */
6247 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6248
6249 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006250 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006251 continue;
6252 }
6253
Michal Vasko03ff5a72019-09-11 13:49:33 +02006254 /* add all the children */
6255 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 +02006256 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006257 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006258 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006259 return rc;
6260 }
6261 }
6262
6263 /* use the temporary set as the current one */
6264 ret_set.ctx_pos = set->ctx_pos;
6265 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006266 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006267 memcpy(set, &ret_set, sizeof *set);
6268
6269 return LY_SUCCESS;
6270}
6271
6272/**
6273 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6274 * (or LYXP_SET_EMPTY).
6275 *
6276 * @param[in,out] set Set to use.
6277 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6278 * @param[in] options XPath options.
6279 * @return LY_ERR
6280 */
6281static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006282moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006283{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006284 uint32_t getnext_opts;
6285 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006286 const struct lysc_node *iter, *start_parent;
6287 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006288
aPiecek8b0cc152021-05-31 16:40:31 +02006289 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006290 return LY_SUCCESS;
6291 }
6292
6293 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006294 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006295 return LY_EVALID;
6296 }
6297
Michal Vasko519fd602020-05-26 12:17:39 +02006298 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006299 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006300 if (options & LYXP_SCNODE_OUTPUT) {
6301 getnext_opts |= LYS_GETNEXT_OUTPUT;
6302 }
6303
6304 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006305 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006306 if (!all_desc) {
6307 /* traverse the start node */
6308 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6309 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6310 }
6311 continue;
6312 }
6313
Radek Krejcif13b87b2020-12-01 22:02:17 +01006314 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6315 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006316 continue;
6317 }
6318
Michal Vasko519fd602020-05-26 12:17:39 +02006319 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006320 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006321 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006322 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006323 }
6324
Michal Vasko519fd602020-05-26 12:17:39 +02006325 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006326
Michal Vasko519fd602020-05-26 12:17:39 +02006327 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6328 /* it can actually be in any module, it's all <running> */
6329 mod_idx = 0;
6330 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6331 iter = NULL;
6332 /* module may not be implemented */
6333 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6334 /* context check */
6335 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6336 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 Vasko519fd602020-05-26 12:17:39 +02006340 /* throw away the insert index, we want to consider that node again, recursively */
6341 }
6342 }
6343
6344 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6345 iter = NULL;
6346 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006347 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006348 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006349 continue;
6350 }
6351
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006352 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006353 }
6354 }
6355 }
6356
6357 return LY_SUCCESS;
6358}
6359
6360/**
6361 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6362 * (or LYXP_SET_EMPTY). Context position aware.
6363 *
6364 * @param[in] set Set to use.
6365 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6366 * @param[in] options XPath options.
6367 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6368 */
6369static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006370moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006371{
6372 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006373 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006374 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006375
aPiecek8b0cc152021-05-31 16:40:31 +02006376 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006377 return LY_SUCCESS;
6378 }
6379
6380 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006381 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006382 return LY_EVALID;
6383 }
6384
6385 if (all_desc) {
6386 /* <path>//.. == <path>//./.. */
6387 rc = moveto_self(set, 1, options);
6388 LY_CHECK_RET(rc);
6389 }
6390
Radek Krejci1deb5be2020-08-26 16:43:36 +02006391 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006392 node = set->val.nodes[i].node;
6393
6394 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9e685082021-01-29 14:49:09 +01006395 new_node = lyd_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006396 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6397 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006398 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6399 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006400 if (!new_node) {
6401 LOGINT_RET(set->ctx);
6402 }
6403 } else {
6404 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006405 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006406 continue;
6407 }
6408
Michal Vaskoa1424542019-11-14 16:08:52 +01006409 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006410 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 +02006411 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006412 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006413
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006414 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006415 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006416 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006417
Michal Vasko03ff5a72019-09-11 13:49:33 +02006418 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006419 /* 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 +02006420 new_type = LYXP_NODE_ELEM;
6421 }
6422
Michal Vasko03ff5a72019-09-11 13:49:33 +02006423 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006424 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006425 } else {
6426 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006427 }
6428 }
6429
Michal Vasko2caefc12019-11-14 16:07:56 +01006430 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006431 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006432
6433 return LY_SUCCESS;
6434}
6435
6436/**
6437 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6438 * (or LYXP_SET_EMPTY).
6439 *
6440 * @param[in] set Set to use.
6441 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6442 * @param[in] options XPath options.
6443 * @return LY_ERR
6444 */
6445static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006446moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006447{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006448 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006449 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006450 const struct lysc_node *node, *new_node;
6451 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006452
aPiecek8b0cc152021-05-31 16:40:31 +02006453 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006454 return LY_SUCCESS;
6455 }
6456
6457 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006458 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006459 return LY_EVALID;
6460 }
6461
6462 if (all_desc) {
6463 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006464 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006465 }
6466
Michal Vasko03ff5a72019-09-11 13:49:33 +02006467 orig_used = set->used;
6468 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006469 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6470 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006471 continue;
6472 }
6473
6474 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006475 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006476 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006477 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006478 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006479
6480 node = set->val.scnodes[i].scnode;
6481
6482 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006483 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006484 } else {
6485 /* root does not have a parent */
6486 continue;
6487 }
6488
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006489 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006490 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006491 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006492
Michal Vasko03ff5a72019-09-11 13:49:33 +02006493 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006494 /* 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 +02006495 new_type = LYXP_NODE_ELEM;
6496 }
6497
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006498 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6499 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006500 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006501 temp_ctx = 1;
6502 }
6503 }
6504
6505 if (temp_ctx) {
6506 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006507 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6508 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006509 }
6510 }
6511 }
6512
6513 return LY_SUCCESS;
6514}
6515
6516/**
6517 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6518 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6519 *
6520 * @param[in,out] set1 Set to use for the result.
6521 * @param[in] set2 Set acting as the second operand for @p op.
6522 * @param[in] op Comparison operator to process.
6523 * @param[in] options XPath options.
6524 * @return LY_ERR
6525 */
6526static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006527moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006528{
6529 /*
6530 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6531 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6532 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6533 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6534 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6535 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6536 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6537 *
6538 * '=' or '!='
6539 * BOOLEAN + BOOLEAN
6540 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6541 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6542 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6543 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6544 * NUMBER + NUMBER
6545 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6546 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6547 * STRING + STRING
6548 *
6549 * '<=', '<', '>=', '>'
6550 * NUMBER + NUMBER
6551 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6552 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6553 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6554 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6555 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6556 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6557 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6558 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6559 */
6560 struct lyxp_set iter1, iter2;
6561 int result;
6562 int64_t i;
6563 LY_ERR rc;
6564
Michal Vasko004d3152020-06-11 19:59:22 +02006565 memset(&iter1, 0, sizeof iter1);
6566 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006567
6568 /* iterative evaluation with node-sets */
6569 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6570 if (set1->type == LYXP_SET_NODE_SET) {
6571 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006572 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006573 switch (set2->type) {
6574 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006575 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006576 break;
6577 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006578 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006579 break;
6580 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006581 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006582 break;
6583 }
6584 LY_CHECK_RET(rc);
6585
Michal Vasko4c7763f2020-07-27 17:40:37 +02006586 /* canonize set2 */
6587 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6588
6589 /* compare recursively */
6590 rc = moveto_op_comp(&iter1, &iter2, op, options);
6591 lyxp_set_free_content(&iter2);
6592 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006593
6594 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006595 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006596 set_fill_boolean(set1, 1);
6597 return LY_SUCCESS;
6598 }
6599 }
6600 } else {
6601 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006602 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006603 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006604 case LYXP_SET_NUMBER:
6605 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6606 break;
6607 case LYXP_SET_BOOLEAN:
6608 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6609 break;
6610 default:
6611 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6612 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006613 }
6614 LY_CHECK_RET(rc);
6615
Michal Vasko4c7763f2020-07-27 17:40:37 +02006616 /* canonize set1 */
6617 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 +02006618
Michal Vasko4c7763f2020-07-27 17:40:37 +02006619 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006620 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006621 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006622 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006623
6624 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006625 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006626 set_fill_boolean(set1, 1);
6627 return LY_SUCCESS;
6628 }
6629 }
6630 }
6631
6632 /* false for all nodes */
6633 set_fill_boolean(set1, 0);
6634 return LY_SUCCESS;
6635 }
6636
6637 /* first convert properly */
6638 if ((op[0] == '=') || (op[0] == '!')) {
6639 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006640 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6641 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006642 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006643 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006644 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006645 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006646 LY_CHECK_RET(rc);
6647 } /* else we have 2 strings */
6648 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006649 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006650 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006651 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006652 LY_CHECK_RET(rc);
6653 }
6654
6655 assert(set1->type == set2->type);
6656
6657 /* compute result */
6658 if (op[0] == '=') {
6659 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006660 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006661 } else if (set1->type == LYXP_SET_NUMBER) {
6662 result = (set1->val.num == set2->val.num);
6663 } else {
6664 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006665 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006666 }
6667 } else if (op[0] == '!') {
6668 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006669 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006670 } else if (set1->type == LYXP_SET_NUMBER) {
6671 result = (set1->val.num != set2->val.num);
6672 } else {
6673 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006674 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006675 }
6676 } else {
6677 assert(set1->type == LYXP_SET_NUMBER);
6678 if (op[0] == '<') {
6679 if (op[1] == '=') {
6680 result = (set1->val.num <= set2->val.num);
6681 } else {
6682 result = (set1->val.num < set2->val.num);
6683 }
6684 } else {
6685 if (op[1] == '=') {
6686 result = (set1->val.num >= set2->val.num);
6687 } else {
6688 result = (set1->val.num > set2->val.num);
6689 }
6690 }
6691 }
6692
6693 /* assign result */
6694 if (result) {
6695 set_fill_boolean(set1, 1);
6696 } else {
6697 set_fill_boolean(set1, 0);
6698 }
6699
6700 return LY_SUCCESS;
6701}
6702
6703/**
6704 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6705 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6706 *
6707 * @param[in,out] set1 Set to use for the result.
6708 * @param[in] set2 Set acting as the second operand for @p op.
6709 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006710 * @return LY_ERR
6711 */
6712static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006713moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006714{
6715 LY_ERR rc;
6716
6717 /* unary '-' */
6718 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006719 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006720 LY_CHECK_RET(rc);
6721 set1->val.num *= -1;
6722 lyxp_set_free(set2);
6723 return LY_SUCCESS;
6724 }
6725
6726 assert(set1 && set2);
6727
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006728 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006729 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006730 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006731 LY_CHECK_RET(rc);
6732
6733 switch (op[0]) {
6734 /* '+' */
6735 case '+':
6736 set1->val.num += set2->val.num;
6737 break;
6738
6739 /* '-' */
6740 case '-':
6741 set1->val.num -= set2->val.num;
6742 break;
6743
6744 /* '*' */
6745 case '*':
6746 set1->val.num *= set2->val.num;
6747 break;
6748
6749 /* 'div' */
6750 case 'd':
6751 set1->val.num /= set2->val.num;
6752 break;
6753
6754 /* 'mod' */
6755 case 'm':
6756 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6757 break;
6758
6759 default:
6760 LOGINT_RET(set1->ctx);
6761 }
6762
6763 return LY_SUCCESS;
6764}
6765
6766/*
6767 * eval functions
6768 *
6769 * They execute a parsed XPath expression on some data subtree.
6770 */
6771
6772/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006773 * @brief Evaluate Predicate. Logs directly on error.
6774 *
Michal Vaskod3678892020-05-21 10:06:58 +02006775 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006776 *
6777 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006778 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02006779 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006780 * @param[in] options XPath options.
6781 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6782 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6783 */
6784static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006785eval_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 +02006786{
6787 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006788 uint16_t orig_exp;
6789 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006790 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006791 struct lyxp_set set2;
6792 struct lyd_node *orig_parent;
6793
6794 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02006795 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006796 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006797 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006798
aPiecek8b0cc152021-05-31 16:40:31 +02006799 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006800only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02006801 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006802 LY_CHECK_RET(rc);
6803 } else if (set->type == LYXP_SET_NODE_SET) {
6804 /* 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 +01006805 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006806
6807 /* empty set, nothing to evaluate */
6808 if (!set->used) {
6809 goto only_parse;
6810 }
6811
Michal Vasko004d3152020-06-11 19:59:22 +02006812 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006813 orig_pos = 0;
6814 orig_size = set->used;
6815 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006816 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006817 set_init(&set2, set);
6818 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6819 /* remember the node context position for position() and context size for last(),
6820 * predicates should always be evaluated with respect to the child axis (since we do
6821 * not support explicit axes) so we assign positions based on their parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006822 if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) {
6823 orig_parent = lyd_parent(set->val.nodes[i].node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006824 orig_pos = 1;
6825 } else {
6826 ++orig_pos;
6827 }
6828
6829 set2.ctx_pos = orig_pos;
6830 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006831 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006832
Michal Vasko004d3152020-06-11 19:59:22 +02006833 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006834 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006835 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006836 return rc;
6837 }
6838
6839 /* number is a position */
6840 if (set2.type == LYXP_SET_NUMBER) {
6841 if ((long long)set2.val.num == orig_pos) {
6842 set2.val.num = 1;
6843 } else {
6844 set2.val.num = 0;
6845 }
6846 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006847 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006848
6849 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006850 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006851 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006852 }
6853 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006854 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006855
6856 } else if (set->type == LYXP_SET_SCNODE_SET) {
6857 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006858 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006859 /* there is a currently-valid node */
6860 break;
6861 }
6862 }
6863 /* empty set, nothing to evaluate */
6864 if (i == set->used) {
6865 goto only_parse;
6866 }
6867
Michal Vasko004d3152020-06-11 19:59:22 +02006868 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006869
Michal Vasko03ff5a72019-09-11 13:49:33 +02006870 /* set special in_ctx to all the valid snodes */
6871 pred_in_ctx = set_scnode_new_in_ctx(set);
6872
6873 /* use the valid snodes one-by-one */
6874 for (i = 0; i < set->used; ++i) {
6875 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6876 continue;
6877 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006878 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006879
Michal Vasko004d3152020-06-11 19:59:22 +02006880 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006881
Michal Vasko004d3152020-06-11 19:59:22 +02006882 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006883 LY_CHECK_RET(rc);
6884
6885 set->val.scnodes[i].in_ctx = pred_in_ctx;
6886 }
6887
6888 /* restore the state as it was before the predicate */
6889 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006890 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02006891 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006892 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006893 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006894 }
6895 }
6896
6897 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006898 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006899 set_fill_set(&set2, set);
6900
Michal Vasko004d3152020-06-11 19:59:22 +02006901 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006902 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006903 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006904 return rc;
6905 }
6906
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006907 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006908 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006909 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006910 }
Michal Vaskod3678892020-05-21 10:06:58 +02006911 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006912 }
6913
6914 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006915 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02006916 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006917 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006918 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006919
6920 return LY_SUCCESS;
6921}
6922
6923/**
Michal Vaskod3678892020-05-21 10:06:58 +02006924 * @brief Evaluate Literal. Logs directly on error.
6925 *
6926 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006927 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006928 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6929 */
6930static void
Michal Vasko40308e72020-10-20 16:38:40 +02006931eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006932{
6933 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006934 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006935 set_fill_string(set, "", 0);
6936 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006937 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 +02006938 }
6939 }
6940 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006941 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006942 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006943}
6944
6945/**
Michal Vasko004d3152020-06-11 19:59:22 +02006946 * @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 +02006947 *
Michal Vasko004d3152020-06-11 19:59:22 +02006948 * @param[in] exp Full parsed XPath expression.
6949 * @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 +02006950 * @param[in] ctx_node Found schema node as the context for the predicate.
6951 * @param[in] cur_mod Current module for the expression.
6952 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006953 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006954 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006955 * @param[out] predicates Parsed predicates.
6956 * @param[out] pred_type Type of @p predicates.
6957 * @return LY_SUCCESS on success,
6958 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006959 */
6960static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006961eval_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 +02006962 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 +02006963 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006964{
Michal Vasko004d3152020-06-11 19:59:22 +02006965 LY_ERR ret = LY_SUCCESS;
6966 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006967 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006968 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006969 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006970 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006971
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006972 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006973
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006974 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006975 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006976 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006977 return LY_EINVAL;
6978 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006979 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 +02006980 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006981
Michal Vasko004d3152020-06-11 19:59:22 +02006982 /* learn where the predicates end */
6983 e_idx = *tok_idx;
6984 while (key_count) {
6985 /* '[' */
6986 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6987 return LY_EINVAL;
6988 }
6989 ++e_idx;
6990
Michal Vasko3354d272021-04-06 09:40:06 +02006991 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
6992 /* definitely not a key */
6993 return LY_EINVAL;
6994 }
6995
Michal Vasko004d3152020-06-11 19:59:22 +02006996 /* ']' */
6997 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6998 ++e_idx;
6999 }
7000 ++e_idx;
7001
7002 /* another presumably key predicate parsed */
7003 --key_count;
7004 }
Michal Vasko004d3152020-06-11 19:59:22 +02007005 } else {
7006 /* learn just where this single predicate ends */
7007 e_idx = *tok_idx;
7008
Michal Vaskod3678892020-05-21 10:06:58 +02007009 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007010 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7011 return LY_EINVAL;
7012 }
7013 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007014
Michal Vasko3354d272021-04-06 09:40:06 +02007015 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
7016 /* definitely not the value */
7017 return LY_EINVAL;
7018 }
7019
Michal Vaskod3678892020-05-21 10:06:58 +02007020 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007021 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7022 ++e_idx;
7023 }
7024 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007025 }
7026
Michal Vasko004d3152020-06-11 19:59:22 +02007027 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
7028 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
7029
7030 /* turn logging off */
7031 prev_lo = ly_log_options(0);
7032
7033 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007034 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
7035 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007036
7037 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007038 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
7039 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02007040 LY_CHECK_GOTO(ret, cleanup);
7041
7042 /* success, the predicate must include all the needed information for hash-based search */
7043 *tok_idx = e_idx;
7044
7045cleanup:
7046 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007047 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02007048 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02007049}
7050
7051/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007052 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7053 * data node(s) could be found using a single hash-based search.
7054 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007055 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007056 * @param[in] node Next context node to check.
7057 * @param[in] name Expected node name.
7058 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007059 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007060 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007061 * @param[in] format Prefix format.
7062 * @param[in,out] found Previously found node, is updated.
7063 * @return LY_SUCCESS on success,
7064 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7065 */
7066static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007067eval_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 +02007068 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 +02007069 const struct lysc_node **found)
7070{
7071 const struct lysc_node *scnode;
7072 const struct lys_module *mod;
7073 uint32_t idx = 0;
7074
Radek Krejci8df109d2021-04-23 12:19:08 +02007075 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007076
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007077continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007078 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007079 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007080 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007081 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007082 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007083 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7084 if (scnode) {
7085 /* we have found a match */
7086 break;
7087 }
7088 }
7089
Michal Vasko7d1d0912020-10-16 08:38:30 +02007090 if (!scnode) {
7091 /* all modules searched */
7092 idx = 0;
7093 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007094 } else {
7095 /* search in top-level */
7096 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7097 }
7098 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007099 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007100 /* we must adjust the module to inherit the one from the context node */
7101 moveto_mod = node->schema->module;
7102 }
7103
7104 /* search in children, do not repeat the same search */
7105 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007106 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007107
7108 /* additional context check */
7109 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7110 scnode = NULL;
7111 }
7112
7113 if (scnode) {
7114 if (*found) {
7115 /* we found a schema node with the same name but at different level, give up, too complicated
7116 * (more hash-based searches would be required, not supported) */
7117 return LY_ENOT;
7118 } else {
7119 /* remember the found schema node and continue to make sure it can be used */
7120 *found = scnode;
7121 }
7122 }
7123
7124 if (idx) {
7125 /* continue searching all the following models */
7126 goto continue_search;
7127 }
7128
7129 return LY_SUCCESS;
7130}
7131
7132/**
Michal Vaskod3678892020-05-21 10:06:58 +02007133 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7134 *
7135 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7136 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7137 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7138 *
7139 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007140 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007141 * @param[in] attr_axis Whether to search attributes or standard nodes.
7142 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007143 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007144 * @param[in] options XPath options.
7145 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7146 */
7147static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007148eval_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 +02007149 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007150{
Michal Vaskod3678892020-05-21 10:06:58 +02007151 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007152 const char *ncname, *ncname_dict = NULL;
7153 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007154 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007155 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007156 struct ly_path_predicate *predicates = NULL;
7157 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007158 LY_ERR rc = LY_SUCCESS;
7159
aPiecek8b0cc152021-05-31 16:40:31 +02007160 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007161 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007162 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007163
aPiecek8b0cc152021-05-31 16:40:31 +02007164 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02007165 goto moveto;
7166 }
7167
Michal Vasko004d3152020-06-11 19:59:22 +02007168 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7169 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007170
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007171 if ((ncname[0] == '*') && (ncname_len == 1)) {
7172 /* all nodes will match */
7173 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007174 }
7175
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007176 /* parse (and skip) module name */
7177 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007178 LY_CHECK_GOTO(rc, cleanup);
7179
Radek Krejci8df109d2021-04-23 12:19:08 +02007180 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 +02007181 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007182 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007183 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7184 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007185 /* check failed */
7186 scnode = NULL;
7187 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007188 }
7189 }
7190
Michal Vasko004d3152020-06-11 19:59:22 +02007191 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7192 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007193 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7194 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007195 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007196 scnode = NULL;
7197 }
7198 }
7199 }
7200
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007201 if (!scnode) {
7202 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007203 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007204 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007205 }
7206
7207moveto:
7208 /* move to the attribute(s), data node(s), or schema node(s) */
7209 if (attr_axis) {
aPiecek8b0cc152021-05-31 16:40:31 +02007210 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007211 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007212 } else {
7213 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007214 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007215 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007216 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007217 }
7218 LY_CHECK_GOTO(rc, cleanup);
7219 }
7220 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007221 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007222 int64_t i;
7223
Michal Vaskod3678892020-05-21 10:06:58 +02007224 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007225 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007226 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007227 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007228 }
7229 LY_CHECK_GOTO(rc, cleanup);
7230
7231 for (i = set->used - 1; i > -1; --i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007232 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vaskod3678892020-05-21 10:06:58 +02007233 break;
7234 }
7235 }
7236 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007237 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vasko90f12dc2020-12-03 14:20:42 +01007238 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".",
7239 ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007240 free(path);
7241 }
7242 } else {
7243 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007244 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007245 } else {
7246 if (scnode) {
7247 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007248 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007249 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007250 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007251 }
7252 }
7253 LY_CHECK_GOTO(rc, cleanup);
7254 }
7255 }
7256
7257 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007258 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7259 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007260 LY_CHECK_RET(rc);
7261 }
7262
7263cleanup:
aPiecek8b0cc152021-05-31 16:40:31 +02007264 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007265 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007266 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007267 }
Michal Vaskod3678892020-05-21 10:06:58 +02007268 return rc;
7269}
7270
7271/**
7272 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7273 *
7274 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7275 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7276 * [8] NodeType ::= 'text' | 'node'
7277 *
7278 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007279 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007280 * @param[in] attr_axis Whether to search attributes or standard nodes.
7281 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007282 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007283 * @param[in] options XPath options.
7284 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7285 */
7286static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007287eval_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 +02007288 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007289{
7290 LY_ERR rc;
7291
7292 /* TODO */
7293 (void)attr_axis;
7294 (void)all_desc;
7295
aPiecek8b0cc152021-05-31 16:40:31 +02007296 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007297 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007298 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007299 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
aPiecek8b0cc152021-05-31 16:40:31 +02007300 options |= LYXP_SKIP_EXPR;
Michal Vaskod3678892020-05-21 10:06:58 +02007301 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007302 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007303 rc = xpath_node(NULL, 0, set, options);
7304 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007305 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007306 rc = xpath_text(NULL, 0, set, options);
7307 }
7308 LY_CHECK_RET(rc);
7309 }
7310 }
aPiecek8b0cc152021-05-31 16:40:31 +02007311 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007312 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007313 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007314
7315 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007316 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007317 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007318 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007319 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007320
7321 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007322 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007323 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007324 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007325 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007326
7327 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007328 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7329 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007330 LY_CHECK_RET(rc);
7331 }
7332
7333 return LY_SUCCESS;
7334}
7335
7336/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007337 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7338 *
7339 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7340 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007341 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007342 *
7343 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007344 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007345 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007346 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007347 * @param[in] options XPath options.
7348 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7349 */
7350static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007351eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7352 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007353{
Radek Krejci857189e2020-09-01 13:26:36 +02007354 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007355 LY_ERR rc;
7356
7357 goto step;
7358 do {
7359 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007360 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007361 all_desc = 0;
7362 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007363 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007364 all_desc = 1;
7365 }
aPiecek8b0cc152021-05-31 16:40:31 +02007366 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007367 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007368 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007369
7370step:
Michal Vaskod3678892020-05-21 10:06:58 +02007371 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007372 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007373 attr_axis = 1;
7374
aPiecek8b0cc152021-05-31 16:40:31 +02007375 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007376 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007377 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007378 } else {
7379 attr_axis = 0;
7380 }
7381
Michal Vasko03ff5a72019-09-11 13:49:33 +02007382 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007383 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007384 case LYXP_TOKEN_DOT:
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_self(set, all_desc, options);
7388 } else {
7389 rc = moveto_self(set, all_desc, options);
7390 }
7391 LY_CHECK_RET(rc);
7392 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
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
7397 case LYXP_TOKEN_DDOT:
7398 /* evaluate '..' */
aPiecek8b0cc152021-05-31 16:40:31 +02007399 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007400 rc = moveto_scnode_parent(set, all_desc, options);
7401 } else {
7402 rc = moveto_parent(set, all_desc, options);
7403 }
7404 LY_CHECK_RET(rc);
aPiecek8b0cc152021-05-31 16:40:31 +02007405 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007406 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007407 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007408 break;
7409
Michal Vasko03ff5a72019-09-11 13:49:33 +02007410 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007411 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007412 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007413 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007414 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007415
Michal Vaskod3678892020-05-21 10:06:58 +02007416 case LYXP_TOKEN_NODETYPE:
7417 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007418 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007419 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007420 break;
7421
7422 default:
aPiecek8b0cc152021-05-31 16:40:31 +02007423 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007424 }
Michal Vasko004d3152020-06-11 19:59:22 +02007425 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007426
7427 return LY_SUCCESS;
7428}
7429
7430/**
7431 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7432 *
7433 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7434 *
7435 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007436 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007437 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007438 * @param[in] options XPath options.
7439 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7440 */
7441static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007442eval_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 +02007443{
Radek Krejci857189e2020-09-01 13:26:36 +02007444 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007445
aPiecek8b0cc152021-05-31 16:40:31 +02007446 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007447 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007448 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007449 }
7450
7451 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007452 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007453 /* evaluate '/' - deferred */
7454 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02007455 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007456 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007457 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007458
Michal Vasko004d3152020-06-11 19:59:22 +02007459 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007460 return LY_SUCCESS;
7461 }
Michal Vasko004d3152020-06-11 19:59:22 +02007462 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007463 case LYXP_TOKEN_DOT:
7464 case LYXP_TOKEN_DDOT:
7465 case LYXP_TOKEN_AT:
7466 case LYXP_TOKEN_NAMETEST:
7467 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007468 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007469 break;
7470 default:
7471 break;
7472 }
7473
Michal Vasko03ff5a72019-09-11 13:49:33 +02007474 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007475 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007476 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7477 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02007478 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007479 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007480 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007481
Michal Vaskob0099a92020-08-31 14:55:23 +02007482 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007483 }
7484
7485 return LY_SUCCESS;
7486}
7487
7488/**
7489 * @brief Evaluate FunctionCall. Logs directly on error.
7490 *
Michal Vaskod3678892020-05-21 10:06:58 +02007491 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007492 *
7493 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007494 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007495 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007496 * @param[in] options XPath options.
7497 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7498 */
7499static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007500eval_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 +02007501{
7502 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007503
Radek Krejci1deb5be2020-08-26 16:43:36 +02007504 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007505 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007506 struct lyxp_set **args = NULL, **args_aux;
7507
aPiecek8b0cc152021-05-31 16:40:31 +02007508 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007509 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007510 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007511 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007512 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007513 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007514 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007515 xpath_func = &xpath_sum;
7516 }
7517 break;
7518 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007519 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007520 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007521 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007522 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007523 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007524 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007525 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007526 xpath_func = &xpath_true;
7527 }
7528 break;
7529 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007530 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007531 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007532 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007533 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007534 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007535 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007536 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007537 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007538 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007539 xpath_func = &xpath_deref;
7540 }
7541 break;
7542 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007543 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007544 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007545 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007546 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007547 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007548 xpath_func = &xpath_string;
7549 }
7550 break;
7551 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007552 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007553 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007554 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007555 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007556 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007557 xpath_func = &xpath_current;
7558 }
7559 break;
7560 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007561 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007562 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007563 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007564 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007565 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007566 xpath_func = &xpath_re_match;
7567 }
7568 break;
7569 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007570 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007571 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007572 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007573 xpath_func = &xpath_translate;
7574 }
7575 break;
7576 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007577 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007578 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007579 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007580 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007581 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007582 xpath_func = &xpath_bit_is_set;
7583 }
7584 break;
7585 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007586 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007587 xpath_func = &xpath_starts_with;
7588 }
7589 break;
7590 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007591 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007592 xpath_func = &xpath_derived_from;
7593 }
7594 break;
7595 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007596 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007597 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007598 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007599 xpath_func = &xpath_string_length;
7600 }
7601 break;
7602 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007603 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007604 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007605 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007606 xpath_func = &xpath_substring_after;
7607 }
7608 break;
7609 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007610 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007611 xpath_func = &xpath_substring_before;
7612 }
7613 break;
7614 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007615 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007616 xpath_func = &xpath_derived_from_or_self;
7617 }
7618 break;
7619 }
7620
7621 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007622 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 +02007623 return LY_EVALID;
7624 }
7625 }
7626
aPiecek8b0cc152021-05-31 16:40:31 +02007627 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007628 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007629 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007630
7631 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007632 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007633 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007634 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007635 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007636
7637 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007638 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02007639 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007640 args = malloc(sizeof *args);
7641 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7642 arg_count = 1;
7643 args[0] = set_copy(set);
7644 if (!args[0]) {
7645 rc = LY_EMEM;
7646 goto cleanup;
7647 }
7648
Michal Vasko004d3152020-06-11 19:59:22 +02007649 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007650 LY_CHECK_GOTO(rc, cleanup);
7651 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007652 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007653 LY_CHECK_GOTO(rc, cleanup);
7654 }
7655 }
Michal Vasko004d3152020-06-11 19:59:22 +02007656 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02007657 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007658 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007659 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007660
aPiecek8b0cc152021-05-31 16:40:31 +02007661 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007662 ++arg_count;
7663 args_aux = realloc(args, arg_count * sizeof *args);
7664 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7665 args = args_aux;
7666 args[arg_count - 1] = set_copy(set);
7667 if (!args[arg_count - 1]) {
7668 rc = LY_EMEM;
7669 goto cleanup;
7670 }
7671
Michal Vasko004d3152020-06-11 19:59:22 +02007672 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007673 LY_CHECK_GOTO(rc, cleanup);
7674 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007675 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007676 LY_CHECK_GOTO(rc, cleanup);
7677 }
7678 }
7679
7680 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007681 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007682 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007683 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007684 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007685
aPiecek8b0cc152021-05-31 16:40:31 +02007686 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007687 /* evaluate function */
7688 rc = xpath_func(args, arg_count, set, options);
7689
7690 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007691 /* merge all nodes from arg evaluations */
7692 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007693 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007694 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007695 }
7696 }
7697 } else {
7698 rc = LY_SUCCESS;
7699 }
7700
7701cleanup:
7702 for (i = 0; i < arg_count; ++i) {
7703 lyxp_set_free(args[i]);
7704 }
7705 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007706 return rc;
7707}
7708
7709/**
7710 * @brief Evaluate Number. Logs directly on error.
7711 *
7712 * @param[in] ctx Context for errors.
7713 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007714 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007715 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7716 * @return LY_ERR
7717 */
7718static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007719eval_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 +02007720{
7721 long double num;
7722 char *endptr;
7723
7724 if (set) {
7725 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007726 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007727 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007728 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7729 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007730 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007731 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007732 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007733 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7734 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007735 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007736 return LY_EVALID;
7737 }
7738
7739 set_fill_number(set, num);
7740 }
7741
7742 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007743 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007744 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007745 return LY_SUCCESS;
7746}
7747
7748/**
7749 * @brief Evaluate PathExpr. Logs directly on error.
7750 *
Michal Vaskod3678892020-05-21 10:06:58 +02007751 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007752 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7753 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7754 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007755 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007756 *
7757 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007758 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007759 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007760 * @param[in] options XPath options.
7761 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7762 */
7763static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007764eval_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 +02007765{
Radek Krejci857189e2020-09-01 13:26:36 +02007766 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007767 LY_ERR rc;
7768
Michal Vasko004d3152020-06-11 19:59:22 +02007769 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007770 case LYXP_TOKEN_PAR1:
7771 /* '(' Expr ')' */
7772
7773 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02007774 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007775 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007776 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007777
7778 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007779 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007780 LY_CHECK_RET(rc);
7781
7782 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007783 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007784 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007785 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007786 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007787
7788 parent_pos_pred = 0;
7789 goto predicate;
7790
7791 case LYXP_TOKEN_DOT:
7792 case LYXP_TOKEN_DDOT:
7793 case LYXP_TOKEN_AT:
7794 case LYXP_TOKEN_NAMETEST:
7795 case LYXP_TOKEN_NODETYPE:
7796 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007797 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007798 LY_CHECK_RET(rc);
7799 break;
7800
7801 case LYXP_TOKEN_FUNCNAME:
7802 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02007803 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007804 LY_CHECK_RET(rc);
7805
7806 parent_pos_pred = 1;
7807 goto predicate;
7808
Michal Vasko3e48bf32020-06-01 08:39:07 +02007809 case LYXP_TOKEN_OPER_PATH:
7810 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007811 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007812 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007813 LY_CHECK_RET(rc);
7814 break;
7815
7816 case LYXP_TOKEN_LITERAL:
7817 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02007818 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7819 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007820 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007821 }
Michal Vasko004d3152020-06-11 19:59:22 +02007822 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007823 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007824 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007825 }
7826
7827 parent_pos_pred = 1;
7828 goto predicate;
7829
7830 case LYXP_TOKEN_NUMBER:
7831 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02007832 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7833 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007834 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007835 }
Michal Vasko004d3152020-06-11 19:59:22 +02007836 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007837 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007838 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007839 }
7840 LY_CHECK_RET(rc);
7841
7842 parent_pos_pred = 1;
7843 goto predicate;
7844
7845 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01007846 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 +02007847 return LY_EVALID;
7848 }
7849
7850 return LY_SUCCESS;
7851
7852predicate:
7853 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007854 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7855 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007856 LY_CHECK_RET(rc);
7857 }
7858
7859 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007860 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007861
7862 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007863 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007864 all_desc = 0;
7865 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007866 all_desc = 1;
7867 }
7868
aPiecek8b0cc152021-05-31 16:40:31 +02007869 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007870 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007871 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007872
Michal Vasko004d3152020-06-11 19:59:22 +02007873 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007874 LY_CHECK_RET(rc);
7875 }
7876
7877 return LY_SUCCESS;
7878}
7879
7880/**
7881 * @brief Evaluate UnionExpr. Logs directly on error.
7882 *
Michal Vaskod3678892020-05-21 10:06:58 +02007883 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007884 *
7885 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007886 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007887 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007888 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007889 * @param[in] options XPath options.
7890 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7891 */
7892static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007893eval_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 +02007894{
7895 LY_ERR rc = LY_SUCCESS;
7896 struct lyxp_set orig_set, set2;
7897 uint16_t i;
7898
7899 assert(repeat);
7900
7901 set_init(&orig_set, set);
7902 set_init(&set2, set);
7903
7904 set_fill_set(&orig_set, set);
7905
Michal Vasko004d3152020-06-11 19:59:22 +02007906 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007907 LY_CHECK_GOTO(rc, cleanup);
7908
7909 /* ('|' PathExpr)* */
7910 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007911 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02007912 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007913 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007914 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007915
aPiecek8b0cc152021-05-31 16:40:31 +02007916 if (options & LYXP_SKIP_EXPR) {
7917 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007918 LY_CHECK_GOTO(rc, cleanup);
7919 continue;
7920 }
7921
7922 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007923 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007924 LY_CHECK_GOTO(rc, cleanup);
7925
7926 /* eval */
7927 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007928 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007929 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007930 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007931 LY_CHECK_GOTO(rc, cleanup);
7932 }
7933 }
7934
7935cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007936 lyxp_set_free_content(&orig_set);
7937 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007938 return rc;
7939}
7940
7941/**
7942 * @brief Evaluate UnaryExpr. Logs directly on error.
7943 *
Michal Vaskod3678892020-05-21 10:06:58 +02007944 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007945 *
7946 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007947 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007948 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007949 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007950 * @param[in] options XPath options.
7951 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7952 */
7953static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007954eval_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 +02007955{
7956 LY_ERR rc;
7957 uint16_t this_op, i;
7958
7959 assert(repeat);
7960
7961 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007962 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007963 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007964 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 +02007965
aPiecek8b0cc152021-05-31 16:40:31 +02007966 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007967 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007968 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007969 }
7970
Michal Vasko004d3152020-06-11 19:59:22 +02007971 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007972 LY_CHECK_RET(rc);
7973
aPiecek8b0cc152021-05-31 16:40:31 +02007974 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007975 if (options & LYXP_SCNODE_ALL) {
7976 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7977 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007978 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007979 LY_CHECK_RET(rc);
7980 }
7981 }
7982
7983 return LY_SUCCESS;
7984}
7985
7986/**
7987 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7988 *
Michal Vaskod3678892020-05-21 10:06:58 +02007989 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007990 * | MultiplicativeExpr '*' UnaryExpr
7991 * | MultiplicativeExpr 'div' UnaryExpr
7992 * | MultiplicativeExpr 'mod' UnaryExpr
7993 *
7994 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007995 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007996 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007997 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007998 * @param[in] options XPath options.
7999 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8000 */
8001static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008002eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
8003 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008004{
8005 LY_ERR rc;
8006 uint16_t this_op;
8007 struct lyxp_set orig_set, set2;
8008 uint16_t i;
8009
8010 assert(repeat);
8011
8012 set_init(&orig_set, set);
8013 set_init(&set2, set);
8014
8015 set_fill_set(&orig_set, set);
8016
Michal Vasko004d3152020-06-11 19:59:22 +02008017 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
8020 /* ('*' / 'div' / 'mod' UnaryExpr)* */
8021 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008022 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008023
Michal Vasko004d3152020-06-11 19:59:22 +02008024 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02008025 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008026 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008027 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008028
aPiecek8b0cc152021-05-31 16:40:31 +02008029 if (options & LYXP_SKIP_EXPR) {
8030 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008031 LY_CHECK_GOTO(rc, cleanup);
8032 continue;
8033 }
8034
8035 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008036 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008037 LY_CHECK_GOTO(rc, cleanup);
8038
8039 /* eval */
8040 if (options & LYXP_SCNODE_ALL) {
8041 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008042 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008043 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008044 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008045 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008046 LY_CHECK_GOTO(rc, cleanup);
8047 }
8048 }
8049
8050cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008051 lyxp_set_free_content(&orig_set);
8052 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008053 return rc;
8054}
8055
8056/**
8057 * @brief Evaluate AdditiveExpr. Logs directly on error.
8058 *
Michal Vaskod3678892020-05-21 10:06:58 +02008059 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008060 * | AdditiveExpr '+' MultiplicativeExpr
8061 * | AdditiveExpr '-' MultiplicativeExpr
8062 *
8063 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008064 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008065 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008066 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008067 * @param[in] options XPath options.
8068 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8069 */
8070static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008071eval_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 +02008072{
8073 LY_ERR rc;
8074 uint16_t this_op;
8075 struct lyxp_set orig_set, set2;
8076 uint16_t i;
8077
8078 assert(repeat);
8079
8080 set_init(&orig_set, set);
8081 set_init(&set2, set);
8082
8083 set_fill_set(&orig_set, set);
8084
Michal Vasko004d3152020-06-11 19:59:22 +02008085 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
8088 /* ('+' / '-' MultiplicativeExpr)* */
8089 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008090 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008091
Michal Vasko004d3152020-06-11 19:59:22 +02008092 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008093 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008094 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008095 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008096
aPiecek8b0cc152021-05-31 16:40:31 +02008097 if (options & LYXP_SKIP_EXPR) {
8098 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008099 LY_CHECK_GOTO(rc, cleanup);
8100 continue;
8101 }
8102
8103 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008104 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008105 LY_CHECK_GOTO(rc, cleanup);
8106
8107 /* eval */
8108 if (options & LYXP_SCNODE_ALL) {
8109 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008110 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008111 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008112 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008113 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008114 LY_CHECK_GOTO(rc, cleanup);
8115 }
8116 }
8117
8118cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008119 lyxp_set_free_content(&orig_set);
8120 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008121 return rc;
8122}
8123
8124/**
8125 * @brief Evaluate RelationalExpr. Logs directly on error.
8126 *
Michal Vaskod3678892020-05-21 10:06:58 +02008127 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008128 * | RelationalExpr '<' AdditiveExpr
8129 * | RelationalExpr '>' AdditiveExpr
8130 * | RelationalExpr '<=' AdditiveExpr
8131 * | RelationalExpr '>=' AdditiveExpr
8132 *
8133 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008134 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008135 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008136 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008137 * @param[in] options XPath options.
8138 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8139 */
8140static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008141eval_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 +02008142{
8143 LY_ERR rc;
8144 uint16_t this_op;
8145 struct lyxp_set orig_set, set2;
8146 uint16_t i;
8147
8148 assert(repeat);
8149
8150 set_init(&orig_set, set);
8151 set_init(&set2, set);
8152
8153 set_fill_set(&orig_set, set);
8154
Michal Vasko004d3152020-06-11 19:59:22 +02008155 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
8158 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8159 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008160 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008161
Michal Vasko004d3152020-06-11 19:59:22 +02008162 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02008163 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008164 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008165 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008166
aPiecek8b0cc152021-05-31 16:40:31 +02008167 if (options & LYXP_SKIP_EXPR) {
8168 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008169 LY_CHECK_GOTO(rc, cleanup);
8170 continue;
8171 }
8172
8173 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008174 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008175 LY_CHECK_GOTO(rc, cleanup);
8176
8177 /* eval */
8178 if (options & LYXP_SCNODE_ALL) {
8179 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008180 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008181 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008182 } else {
8183 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8184 LY_CHECK_GOTO(rc, cleanup);
8185 }
8186 }
8187
8188cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008189 lyxp_set_free_content(&orig_set);
8190 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008191 return rc;
8192}
8193
8194/**
8195 * @brief Evaluate EqualityExpr. Logs directly on error.
8196 *
Michal Vaskod3678892020-05-21 10:06:58 +02008197 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008198 * | EqualityExpr '!=' RelationalExpr
8199 *
8200 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008201 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008202 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008203 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008204 * @param[in] options XPath options.
8205 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8206 */
8207static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008208eval_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 +02008209{
8210 LY_ERR rc;
8211 uint16_t this_op;
8212 struct lyxp_set orig_set, set2;
8213 uint16_t i;
8214
8215 assert(repeat);
8216
8217 set_init(&orig_set, set);
8218 set_init(&set2, set);
8219
8220 set_fill_set(&orig_set, set);
8221
Michal Vasko004d3152020-06-11 19:59:22 +02008222 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
8225 /* ('=' / '!=' RelationalExpr)* */
8226 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008227 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008228
Michal Vasko004d3152020-06-11 19:59:22 +02008229 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02008230 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008231 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008232 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008233
aPiecek8b0cc152021-05-31 16:40:31 +02008234 if (options & LYXP_SKIP_EXPR) {
8235 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008236 LY_CHECK_GOTO(rc, cleanup);
8237 continue;
8238 }
8239
8240 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008241 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008242 LY_CHECK_GOTO(rc, cleanup);
8243
8244 /* eval */
8245 if (options & LYXP_SCNODE_ALL) {
8246 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008247 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8248 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008249 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008250 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008251 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008252 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8253 LY_CHECK_GOTO(rc, cleanup);
8254 }
8255 }
8256
8257cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008258 lyxp_set_free_content(&orig_set);
8259 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008260 return rc;
8261}
8262
8263/**
8264 * @brief Evaluate AndExpr. Logs directly on error.
8265 *
Michal Vaskod3678892020-05-21 10:06:58 +02008266 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008267 *
8268 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008269 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008270 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008271 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008272 * @param[in] options XPath options.
8273 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8274 */
8275static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008276eval_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 +02008277{
8278 LY_ERR rc;
8279 struct lyxp_set orig_set, set2;
8280 uint16_t i;
8281
8282 assert(repeat);
8283
8284 set_init(&orig_set, set);
8285 set_init(&set2, set);
8286
8287 set_fill_set(&orig_set, set);
8288
Michal Vasko004d3152020-06-11 19:59:22 +02008289 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008290 LY_CHECK_GOTO(rc, cleanup);
8291
8292 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008293 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008294 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008295 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008296 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008297 }
8298
8299 /* ('and' EqualityExpr)* */
8300 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008301 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008302 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008303 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008304 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008305
8306 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008307 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8308 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008309 LY_CHECK_GOTO(rc, cleanup);
8310 continue;
8311 }
8312
8313 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008314 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008315 LY_CHECK_GOTO(rc, cleanup);
8316
8317 /* eval - just get boolean value actually */
8318 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008319 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008320 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008321 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008322 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008323 set_fill_set(set, &set2);
8324 }
8325 }
8326
8327cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008328 lyxp_set_free_content(&orig_set);
8329 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008330 return rc;
8331}
8332
8333/**
8334 * @brief Evaluate OrExpr. Logs directly on error.
8335 *
Michal Vaskod3678892020-05-21 10:06:58 +02008336 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008337 *
8338 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008339 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008340 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008341 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008342 * @param[in] options XPath options.
8343 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8344 */
8345static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008346eval_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 +02008347{
8348 LY_ERR rc;
8349 struct lyxp_set orig_set, set2;
8350 uint16_t i;
8351
8352 assert(repeat);
8353
8354 set_init(&orig_set, set);
8355 set_init(&set2, set);
8356
8357 set_fill_set(&orig_set, set);
8358
Michal Vasko004d3152020-06-11 19:59:22 +02008359 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008360 LY_CHECK_GOTO(rc, cleanup);
8361
8362 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008363 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008364 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008365 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008366 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008367 }
8368
8369 /* ('or' AndExpr)* */
8370 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008371 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008372 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008373 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008374 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008375
8376 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008377 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8378 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008379 LY_CHECK_GOTO(rc, cleanup);
8380 continue;
8381 }
8382
8383 set_fill_set(&set2, &orig_set);
8384 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8385 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008386 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008387 LY_CHECK_GOTO(rc, cleanup);
8388
8389 /* eval - just get boolean value actually */
8390 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008391 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008392 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008393 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008394 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008395 set_fill_set(set, &set2);
8396 }
8397 }
8398
8399cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008400 lyxp_set_free_content(&orig_set);
8401 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008402 return rc;
8403}
8404
8405/**
Michal Vasko004d3152020-06-11 19:59:22 +02008406 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008407 *
8408 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008409 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008410 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02008411 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008412 * @param[in] options XPath options.
8413 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8414 */
8415static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008416eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8417 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008418{
8419 uint16_t i, count;
8420 enum lyxp_expr_type next_etype;
8421 LY_ERR rc;
8422
8423 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008424 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008425 next_etype = LYXP_EXPR_NONE;
8426 } else {
8427 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008428 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008429
8430 /* select one-priority lower because etype expression called us */
8431 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008432 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008433 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008434 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008435 } else {
8436 next_etype = LYXP_EXPR_NONE;
8437 }
8438 }
8439
8440 /* decide what expression are we parsing based on the repeat */
8441 switch (next_etype) {
8442 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008443 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008444 break;
8445 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008446 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008447 break;
8448 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008449 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008450 break;
8451 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008452 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008453 break;
8454 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008455 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008456 break;
8457 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008458 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008459 break;
8460 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008461 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008462 break;
8463 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008464 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008465 break;
8466 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008467 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008468 break;
8469 default:
8470 LOGINT_RET(set->ctx);
8471 }
8472
8473 return rc;
8474}
8475
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008476/**
8477 * @brief Get root type.
8478 *
8479 * @param[in] ctx_node Context node.
8480 * @param[in] ctx_scnode Schema context node.
8481 * @param[in] options XPath options.
8482 * @return Root type.
8483 */
8484static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008485lyxp_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 +01008486{
Michal Vasko6b26e742020-07-17 15:02:10 +02008487 const struct lysc_node *op;
8488
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008489 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008490 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008491 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008492
8493 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008494 /* general root that can access everything */
8495 return LYXP_NODE_ROOT;
8496 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8497 /* root context node can access only config data (because we said so, it is unspecified) */
8498 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008499 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008500 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008501 }
8502
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008503 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008504 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008505 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008506
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008507 if (op || !(options & LYXP_SCHEMA)) {
8508 /* general root that can access everything */
8509 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05008510 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008511 /* root context node can access only config data (because we said so, it is unspecified) */
8512 return LYXP_NODE_ROOT_CONFIG;
8513 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008514 return LYXP_NODE_ROOT;
8515}
8516
Michal Vasko03ff5a72019-09-11 13:49:33 +02008517LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008518lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008519 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 +01008520 struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008521{
Michal Vasko004d3152020-06-11 19:59:22 +02008522 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008523 LY_ERR rc;
8524
Michal Vasko400e9672021-01-11 13:39:17 +01008525 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008526 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008527 LOGARG(NULL, "Current module must be set if schema format is used.");
8528 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008529 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008530
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008531 if (tree) {
8532 /* adjust the pointer to be the first top-level sibling */
8533 while (tree->parent) {
8534 tree = lyd_parent(tree);
8535 }
8536 tree = lyd_first_sibling(tree);
8537 }
8538
Michal Vasko03ff5a72019-09-11 13:49:33 +02008539 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008540 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008541 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008542 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8543 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8544
Michal Vasko400e9672021-01-11 13:39:17 +01008545 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008546 set->cur_node = ctx_node;
8547 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008548 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8549 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008550 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008551 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008552 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008553 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008554
Radek Krejciddace2c2021-01-08 11:30:56 +01008555 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008556
Michal Vasko03ff5a72019-09-11 13:49:33 +02008557 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008558 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008559 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008560 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008561 }
8562
Radek Krejciddace2c2021-01-08 11:30:56 +01008563 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008564 return rc;
8565}
8566
8567#if 0
8568
8569/* full xml printing of set elements, not used currently */
8570
8571void
8572lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8573{
8574 uint32_t i;
8575 char *str_num;
8576 struct lyout out;
8577
8578 memset(&out, 0, sizeof out);
8579
8580 out.type = LYOUT_STREAM;
8581 out.method.f = f;
8582
8583 switch (set->type) {
8584 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008585 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008586 break;
8587 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008588 ly_print_(&out, "Boolean XPath set:\n");
8589 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008590 break;
8591 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008592 ly_print_(&out, "String XPath set:\n");
8593 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008594 break;
8595 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008596 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008597
8598 if (isnan(set->value.num)) {
8599 str_num = strdup("NaN");
8600 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8601 str_num = strdup("0");
8602 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8603 str_num = strdup("Infinity");
8604 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8605 str_num = strdup("-Infinity");
8606 } else if ((long long)set->value.num == set->value.num) {
8607 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8608 str_num = NULL;
8609 }
8610 } else {
8611 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8612 str_num = NULL;
8613 }
8614 }
8615 if (!str_num) {
8616 LOGMEM;
8617 return;
8618 }
Michal Vasko5233e962020-08-14 14:26:20 +02008619 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008620 free(str_num);
8621 break;
8622 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008623 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008624
8625 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008626 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008627 switch (set->node_type[i]) {
8628 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008629 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008630 break;
8631 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008632 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008633 break;
8634 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008635 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008636 break;
8637 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008638 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008639 break;
8640 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008641 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008642 break;
8643 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008644 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008645 break;
8646 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008647 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008648 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008649 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008650 break;
8651 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008652 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 +02008653 break;
8654 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008655 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 +02008656 break;
8657 }
8658 }
8659 break;
8660 }
8661}
8662
8663#endif
8664
8665LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008666lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008667{
8668 long double num;
8669 char *str;
8670 LY_ERR rc;
8671
8672 if (!set || (set->type == target)) {
8673 return LY_SUCCESS;
8674 }
8675
8676 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008677 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008678
8679 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008680 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008681 return LY_EINVAL;
8682 }
8683
8684 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008685 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008686 switch (set->type) {
8687 case LYXP_SET_NUMBER:
8688 if (isnan(set->val.num)) {
8689 set->val.str = strdup("NaN");
8690 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8691 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8692 set->val.str = strdup("0");
8693 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8694 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8695 set->val.str = strdup("Infinity");
8696 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8697 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8698 set->val.str = strdup("-Infinity");
8699 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8700 } else if ((long long)set->val.num == set->val.num) {
8701 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8702 LOGMEM_RET(set->ctx);
8703 }
8704 set->val.str = str;
8705 } else {
8706 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8707 LOGMEM_RET(set->ctx);
8708 }
8709 set->val.str = str;
8710 }
8711 break;
8712 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008713 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008714 set->val.str = strdup("true");
8715 } else {
8716 set->val.str = strdup("false");
8717 }
8718 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8719 break;
8720 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008721 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008722 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008723
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008724 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008725 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008726 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008727 set->val.str = str;
8728 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008729 default:
8730 LOGINT_RET(set->ctx);
8731 }
8732 set->type = LYXP_SET_STRING;
8733 }
8734
8735 /* to NUMBER */
8736 if (target == LYXP_SET_NUMBER) {
8737 switch (set->type) {
8738 case LYXP_SET_STRING:
8739 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008740 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008741 set->val.num = num;
8742 break;
8743 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008744 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008745 set->val.num = 1;
8746 } else {
8747 set->val.num = 0;
8748 }
8749 break;
8750 default:
8751 LOGINT_RET(set->ctx);
8752 }
8753 set->type = LYXP_SET_NUMBER;
8754 }
8755
8756 /* to BOOLEAN */
8757 if (target == LYXP_SET_BOOLEAN) {
8758 switch (set->type) {
8759 case LYXP_SET_NUMBER:
8760 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008761 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008762 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008763 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008764 }
8765 break;
8766 case LYXP_SET_STRING:
8767 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008768 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008769 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008770 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008771 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008772 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008773 }
8774 break;
8775 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008776 if (set->used) {
8777 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008778 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008779 } else {
8780 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008781 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008782 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008783 break;
8784 default:
8785 LOGINT_RET(set->ctx);
8786 }
8787 set->type = LYXP_SET_BOOLEAN;
8788 }
8789
Michal Vasko03ff5a72019-09-11 13:49:33 +02008790 return LY_SUCCESS;
8791}
8792
8793LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008794lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008795 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
Michal Vasko400e9672021-01-11 13:39:17 +01008796 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008797{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008798 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008799 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008800
Michal Vasko400e9672021-01-11 13:39:17 +01008801 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008802 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008803 LOGARG(NULL, "Current module must be set if schema format is used.");
8804 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008805 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008806
8807 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008808 memset(set, 0, sizeof *set);
8809 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008810 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8811 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 +01008812 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008813
Michal Vasko400e9672021-01-11 13:39:17 +01008814 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008815 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008816 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008817 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8818 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008819 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008820 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008821 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008822
Radek Krejciddace2c2021-01-08 11:30:56 +01008823 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008824
Michal Vasko03ff5a72019-09-11 13:49:33 +02008825 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01008826 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
8827
Radek Krejciddace2c2021-01-08 11:30:56 +01008828 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008829 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008830}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008831
8832API const char *
8833lyxp_get_expr(const struct lyxp_expr *path)
8834{
8835 if (!path) {
8836 return NULL;
8837 }
8838
8839 return path->expr;
8840}